@@ -316,31 +316,8 @@ export default class MediaDbPlugin extends Plugin {
316316
317317 options . openNote = this . settings . openNoteInNewTab ;
318318
319- if ( mediaTypeModel . image && typeof mediaTypeModel . image === 'string' && mediaTypeModel . image . startsWith ( 'http' ) ) {
320- if ( this . settings . imageDownload ) {
321- try {
322- const imageurl = mediaTypeModel . image ;
323- const imageext = imageurl . split ( '.' ) . pop ( ) ?. split ( / \# | \? / ) [ 0 ] || 'jpg' ;
324- const imagefileName = `${ replaceIllegalFileNameCharactersInString ( `${ mediaTypeModel . type } _${ mediaTypeModel . title } (${ mediaTypeModel . year } )` ) } .${ imageext } ` ;
325- const imagepath = normalizePath ( `${ this . settings . imageFolder } /${ imagefileName } ` ) ;
326-
327- if ( ! this . app . vault . getAbstractFileByPath ( this . settings . imageFolder ) ) {
328- await this . app . vault . createFolder ( this . settings . imageFolder ) ;
329- }
330-
331- if ( ! this . app . vault . getAbstractFileByPath ( imagepath ) ) {
332- const response = await requestUrl ( { url : imageurl , method : 'GET' } ) ;
333- await this . app . vault . createBinary ( imagepath , response . arrayBuffer ) ;
334- }
335-
336- // Update model to use local image path
337- mediaTypeModel . image = `[[${ imagepath } ]]` ;
338- } catch ( e ) {
339- console . warn ( 'MDB | Failed to download image:' , e ) ;
340- }
341- } else {
342- mediaTypeModel . image = mediaTypeModel . image ;
343- }
319+ if ( this . settings . imageDownload ) {
320+ await this . downloadImageForMediaModel ( mediaTypeModel ) ;
344321 }
345322
346323 const fileContent = await this . generateMediaDbNoteContents ( mediaTypeModel , options ) ;
@@ -360,6 +337,40 @@ export default class MediaDbPlugin extends Plugin {
360337 }
361338 }
362339
340+ /**
341+ * Tries to download the image for a media model.
342+ *
343+ * @param mediaTypeModel
344+ * @returns true if the image was downloaded, false otherwise
345+ */
346+ private async downloadImageForMediaModel ( mediaTypeModel : MediaTypeModel ) : Promise < boolean > {
347+ if ( mediaTypeModel . image && typeof mediaTypeModel . image === 'string' && mediaTypeModel . image . startsWith ( 'http' ) ) {
348+ try {
349+ const imageUrl = mediaTypeModel . image ;
350+ const imageExt = imageUrl . split ( '.' ) . pop ( ) ?. split ( / \# | \? / ) [ 0 ] || 'jpg' ;
351+ const imageFileName = `${ replaceIllegalFileNameCharactersInString ( `${ mediaTypeModel . type } _${ mediaTypeModel . title } (${ mediaTypeModel . year } )` ) } .${ imageExt } ` ;
352+ const imagePath = normalizePath ( `${ this . settings . imageFolder } /${ imageFileName } ` ) ;
353+
354+ if ( ! this . app . vault . getAbstractFileByPath ( this . settings . imageFolder ) ) {
355+ await this . app . vault . createFolder ( this . settings . imageFolder ) ;
356+ }
357+
358+ if ( ! this . app . vault . getAbstractFileByPath ( imagePath ) ) {
359+ const response = await requestUrl ( { url : imageUrl , method : 'GET' } ) ;
360+ await this . app . vault . createBinary ( imagePath , response . arrayBuffer ) ;
361+ }
362+
363+ // Update model to use local image path
364+ mediaTypeModel . image = `[[${ imagePath } ]]` ;
365+ return true ;
366+ } catch ( e ) {
367+ console . warn ( 'MDB | Failed to download image:' , e ) ;
368+ }
369+ }
370+
371+ return false ;
372+ }
373+
363374 generateMediaDbNoteFrontmatterPreview ( mediaTypeModel : MediaTypeModel ) : string {
364375 const fileMetadata = this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ;
365376 return stringifyYaml ( fileMetadata ) ;
0 commit comments