@@ -169,26 +169,25 @@ function ImportSource(req, done) {
169169 var fileStat = fs . statSync ( assetPath ) ;
170170 var assetTitle = assetName ;
171171 var assetDescription = assetName ;
172+ var assetJson = assetsJson [ assetName ] ;
172173 var tags = [ ] ;
173174
174- if ( assetsJson [ assetName ] ) {
175- assetTitle = assetsJson [ assetName ] . title ;
176- assetDescription = assetsJson [ assetName ] . description ;
177-
178- assetsJson [ assetName ] . tags . forEach ( function ( tag ) {
179- dbInstance . retrieve ( 'tag' , { title : tag . title } , { fields : '_id' } , function ( error , results ) {
180- if ( results && results . length > 0 ) {
181- tags . push ( results [ 0 ] . _id ) ;
182- } else {
183- app . contentmanager . getContentPlugin ( 'tag' , function ( error , plugin ) {
184- if ( ! error ) {
185- plugin . create ( { title : tag . title } , function ( error , record ) {
186- if ( error ) return logger . log ( 'warn' , 'Failed to create asset tag: ' + ( tag . title || '' ) + ' ' + error ) ;
187- tags . push ( record . _id ) ;
188- } ) ;
189- } ;
190- } ) ;
191- }
175+ if ( assetJson ) {
176+ assetTitle = assetJson . title ;
177+ assetDescription = assetJson . description ;
178+
179+ assetJson . tags . forEach ( function ( tag ) {
180+ const tagTitle = tag . title ;
181+ const warn = ( error ) => logger . log ( 'warn' , `Failed to create asset tag '${ tagTitle } ' ${ error } ` ) ;
182+
183+ if ( ! tagTitle ) return warn ( new Error ( 'Tag has no title' ) ) ;
184+
185+ app . contentmanager . getContentPlugin ( 'tag' , function ( error , plugin ) {
186+ if ( error ) return warn ( error ) ;
187+ plugin . create ( { title : tagTitle } , function ( error , record ) { // @note retrieves if tag already exists
188+ if ( error ) return warn ( error ) ;
189+ tags . push ( record . _id ) ;
190+ } ) ;
192191 } ) ;
193192 } ) ;
194193 }
0 commit comments