Skip to content

Commit b9b1208

Browse files
committed
feat(sync): Extract code of attachment syncing from main process.
1 parent d71f1af commit b9b1208

File tree

2 files changed

+96
-64
lines changed

2 files changed

+96
-64
lines changed

src/main.ts

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from "./localdb";
3737
import { RemoteClient, ServerDomain } from "./remote";
3838
import { InvioSettingTab, DEFAULT_SETTINGS } from "./settings";
39-
import { fetchMetadataFile, parseRemoteItems, SyncStatusType, RemoteSrcPrefix, RemoteAttPrefix } from "./sync";
39+
import { fetchMetadataFile, parseRemoteItems, SyncStatusType, RemoteSrcPrefix, syncAttachment } from "./sync";
4040
import { doActualSync, getSyncPlan, isPasswordOk } from "./sync";
4141
import { messyConfigToNormal, normalConfigToMessy } from "./configPersist";
4242
import { ObsConfigDirFileType, listFilesInObsFolder } from "./obsFolderLister";
@@ -474,69 +474,19 @@ export default class InvioPlugin extends Plugin {
474474
this.addRecentSyncedFile(syncedFile);
475475
const meta = this.app.metadataCache.getFileCache(syncedFile);
476476
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+
)
540490
}
541491
}
542492
}

src/sync.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,85 @@ export const doActualSync = async (
16531653
}
16541654
}
16551655
};
1656+
1657+
export const syncAttachment = async (vault: Vault, client: RemoteClient, embeds: any[], decision: string, cb: (type: string, link: string, result: any, err?: any) => void) => {
1658+
// @ts-ignore
1659+
const attachmentFolderPath = vault.getConfig('attachmentFolderPath');
1660+
const attachmentFolderPrefix = attachmentFolderPath?.replace(/\/$/, '');
1661+
const attachmentList = await vault.adapter.list(attachmentFolderPrefix + '/');
1662+
1663+
const localAttachmentFiles: string[] = attachmentList.files;
1664+
log.info('local dir list: ', localAttachmentFiles);
1665+
1666+
// TODO: For all embeding formats.
1667+
const embedImages = embeds
1668+
.filter((em: any) => em.link?.startsWith('Pasted image '))
1669+
.map(em => em.link);
1670+
1671+
log.info('embed list: ', embedImages);
1672+
1673+
const getLinkWithPrefix = (link: string) => `${attachmentFolderPrefix}/${link}`.replace(/^\//, '');
1674+
// TODO: Remove deleted attachment files
1675+
if (decision === 'uploadLocalToRemote') {
1676+
const diff = embedImages.filter(link => {
1677+
const exist = localAttachmentFiles.find(f => f === getLinkWithPrefix(link));
1678+
return exist;
1679+
})
1680+
await Promise.all(diff.map(async link => {
1681+
log.info('uploading attachment: ', link);
1682+
return client.uploadToRemote(
1683+
getLinkWithPrefix(link),
1684+
RemoteAttPrefix,
1685+
vault,
1686+
false,
1687+
'',
1688+
'',
1689+
null,
1690+
false,
1691+
null,
1692+
`${RemoteAttPrefix}/${link}`
1693+
)
1694+
.then(resp => {
1695+
cb && cb('upload', link, resp);
1696+
})
1697+
.catch(err => {
1698+
log.error(`upload ${link} failed: ${err}`);
1699+
cb && cb('upload', link, null, err);
1700+
return;
1701+
})
1702+
}))
1703+
.catch(err => {
1704+
log.error('unexpected err: ', err);
1705+
})
1706+
} else {
1707+
const diff: string[] = embedImages.map(link => {
1708+
const exist = localAttachmentFiles.find(f => f === getLinkWithPrefix(link));
1709+
return exist ? null : link;
1710+
})
1711+
.filter(l => !!l);
1712+
1713+
await Promise.all(diff.map(async link => {
1714+
log.info('downloading attachment: ', link);
1715+
return client.downloadFromRemote(
1716+
link,
1717+
RemoteAttPrefix,
1718+
vault,
1719+
0,
1720+
'',
1721+
'',
1722+
false,
1723+
getLinkWithPrefix(link)
1724+
)
1725+
.then(resp => {
1726+
cb && cb('download', link, resp);
1727+
})
1728+
.catch(err => {
1729+
log.error(`download ${link} failed: ${err}`);
1730+
cb && cb('download', link, null, err);
1731+
})
1732+
}))
1733+
.catch(err => {
1734+
log.error('unexpected err: ', err);
1735+
})
1736+
}
1737+
}

0 commit comments

Comments
 (0)