@@ -36,7 +36,7 @@ import {
36
36
} from "./localdb" ;
37
37
import { RemoteClient , ServerDomain } from "./remote" ;
38
38
import { InvioSettingTab , DEFAULT_SETTINGS } from "./settings" ;
39
- import { fetchMetadataFile , parseRemoteItems , SyncStatusType , RemoteSrcPrefix , RemoteAttPrefix } from "./sync" ;
39
+ import { fetchMetadataFile , parseRemoteItems , SyncStatusType , RemoteSrcPrefix , syncAttachment } from "./sync" ;
40
40
import { doActualSync , getSyncPlan , isPasswordOk } from "./sync" ;
41
41
import { messyConfigToNormal , normalConfigToMessy } from "./configPersist" ;
42
42
import { ObsConfigDirFileType , listFilesInObsFolder } from "./obsFolderLister" ;
@@ -474,69 +474,19 @@ export default class InvioPlugin extends Plugin {
474
474
this . addRecentSyncedFile ( syncedFile ) ;
475
475
const meta = this . app . metadataCache . getFileCache ( syncedFile ) ;
476
476
if ( meta ?. embeds ) {
477
- // @ts -ignore
478
- const attachmentFolderPath = app . vault . getConfig ( 'attachmentFolderPath' ) ;
479
- const attachmentFolderPrefix = attachmentFolderPath ?. replace ( / \/ $ / , '' ) ;
480
- const attachmentList = await this . app . vault . adapter . list ( attachmentFolderPrefix + '/' ) ;
481
-
482
- const localAttachmentFiles : string [ ] = attachmentList . files ;
483
- log . info ( 'local dir list: ' , localAttachmentFiles ) ;
484
-
485
- // TODO: For all embeding formats.
486
- const embedImages = meta . embeds
487
- . filter ( ( em : any ) => em . link ?. startsWith ( 'Pasted image ' ) )
488
- . map ( em => em . link ) ;
489
-
490
- log . info ( 'embed list: ' , embedImages ) ;
491
-
492
- const getLinkWithPrefix = ( link : string ) => `${ attachmentFolderPrefix } /${ link } ` . replace ( / ^ \/ / , '' )
493
- // TODO: Remove deleted attachment files
494
- if ( decision === 'uploadLocalToRemote' ) {
495
- const diff = embedImages . filter ( link => {
496
- const exist = localAttachmentFiles . find ( f => f === getLinkWithPrefix ( link ) ) ;
497
- return exist ;
498
- } )
499
- await Promise . all ( diff . map ( async link => {
500
- log . info ( 'uploading attachment: ' , link ) ;
501
- view ?. info ( `uploading attachment: ${ link } ` ) ;
502
-
503
- return client . uploadToRemote (
504
- getLinkWithPrefix ( link ) ,
505
- RemoteAttPrefix ,
506
- this . app . vault ,
507
- false ,
508
- '' ,
509
- '' ,
510
- null ,
511
- false ,
512
- null ,
513
- `${ RemoteAttPrefix } /${ link } `
514
- )
515
- } ) )
516
- } else {
517
- const diff : string [ ] = embedImages . map ( link => {
518
- const exist = localAttachmentFiles . find ( f => f === getLinkWithPrefix ( link ) ) ;
519
- return exist ? null : link ;
520
- } )
521
- . filter ( l => ! ! l ) ;
522
- await Promise . all ( diff . map ( async link => {
523
- view ?. info ( `downloading attachment: ${ link } ` ) ;
524
- log . info ( 'downloading attachment: ' , link ) ;
525
- return client . downloadFromRemote (
526
- link ,
527
- RemoteAttPrefix ,
528
- this . app . vault ,
529
- 0 ,
530
- '' ,
531
- '' ,
532
- false ,
533
- getLinkWithPrefix ( link )
534
- )
535
- . catch ( err => {
536
- log . error ( 'sync attachment failed: ' , err ) ;
537
- } )
538
- } ) )
539
- }
477
+ await syncAttachment (
478
+ this . app . vault ,
479
+ client ,
480
+ meta ?. embeds ,
481
+ decision ,
482
+ ( type : string , link : string , result : any , err ?: any ) => {
483
+ if ( err ) {
484
+ view ?. info ( `Attachment(${ link } ) ${ type } failed` )
485
+ } else {
486
+ view ?. info ( `Attachment(${ link } ) ${ type } success` )
487
+ }
488
+ }
489
+ )
540
490
}
541
491
}
542
492
}
0 commit comments