From 9d8e2af7b981d06325f4c4689cdeb602bbbf80a8 Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 18 May 2024 12:03:53 +0800 Subject: [PATCH] allow skipping empty file in onedrive --- src/baseTypes.ts | 2 ++ src/fsOnedrive.ts | 27 ++++++++++++++++++++++----- src/langs/en.json | 8 ++++++-- src/langs/zh_cn.json | 4 ++++ src/langs/zh_tw.json | 4 ++++ src/main.ts | 3 +++ src/settings.ts | 14 ++++++++++++++ 7 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/baseTypes.ts b/src/baseTypes.ts index 1b2d16e..e4ccda8 100644 --- a/src/baseTypes.ts +++ b/src/baseTypes.ts @@ -89,6 +89,7 @@ export interface OnedriveConfig { username: string; credentialsShouldBeDeletedAtTime?: number; remoteBaseDir?: string; + emptyFile: "skip" | "error"; } export interface WebdisConfig { @@ -230,6 +231,7 @@ export interface Entity { hash?: string; etag?: string; synthesizedFolder?: boolean; + synthesizedFile?: boolean; } export interface UploadedType { diff --git a/src/fsOnedrive.ts b/src/fsOnedrive.ts index 8bdb999..c2ee0e3 100644 --- a/src/fsOnedrive.ts +++ b/src/fsOnedrive.ts @@ -32,6 +32,7 @@ export const DEFAULT_ONEDRIVE_CONFIG: OnedriveConfig = { deltaLink: "", username: "", credentialsShouldBeDeletedAtTime: 0, + emptyFile: "skip", }; //////////////////////////////////////////////////////////////////////////////// @@ -344,6 +345,7 @@ const fromDriveItemToEntity = (x: DriveItem, remoteBaseDir: string): Entity => { mtimeCli: mtimeCli, size: isFolder ? 0 : x.size!, sizeRaw: isFolder ? 0 : x.size!, + synthesizedFile: false, // hash: ?? // TODO }; }; @@ -785,7 +787,8 @@ export class FakeFsOnedrive extends FakeFs { content, mtime, ctime, - key + key, + this.onedriveConfig.emptyFile ); } @@ -794,12 +797,26 @@ export class FakeFsOnedrive extends FakeFs { content: ArrayBuffer, mtime: number, ctime: number, - origKey: string + origKey: string, + emptyFile: "skip" | "error" ): Promise { if (content.byteLength === 0) { - throw Error( - `${origKey}: Empty file is not allowed in OneDrive, and please write something in it.` - ); + if (emptyFile === "error") { + throw Error( + `${origKey}: Empty file is not allowed in OneDrive, and please write something in it.` + ); + } else { + return { + key: origKey, + keyRaw: origKey, + mtimeSvr: mtime, + mtimeCli: mtime, + size: 0, + sizeRaw: 0, + synthesizedFile: true, + // hash: ?? // TODO + }; + } } const ctimeStr = new Date(ctime).toISOString(); diff --git a/src/langs/en.json b/src/langs/en.json index 229fe39..ec6e05b 100644 --- a/src/langs/en.json +++ b/src/langs/en.json @@ -219,8 +219,12 @@ "settings_onedrive_auth": "Auth", "settings_onedrive_auth_desc": "Auth.", "settings_onedrive_auth_button": "Auth", - "settings_onedrive_connect_succ": "Great! We can connect to Onedrive!", - "settings_onedrive_connect_fail": "We cannot connect to Onedrive.", + "settings_onedrive_connect_succ": "Great! We can connect to OneDrive!", + "settings_onedrive_connect_fail": "We cannot connect to OneDrive.", + "settings_onedrive_emptyfile": "Empty File Handling", + "settings_onedrive_emptyfile_desc": "OneDrive doesn't allow uploading empty file (even in its official website). Do you want to show up errors or silently skip the empty files?", + "settings_onedrive_emptyfile_skip": "Skip", + "settings_onedrive_emptyfile_error": "Error and abort", "settings_webdav": "Remote For Webdav", "settings_webdav_disclaimer1": "Disclaimer: The information is stored locally. Other malicious/harmful/faulty plugins may read the info. If you see any unintentional access to your webdav server, please immediately change the username and password.", "settings_webdav_cors_os": "Obsidian desktop>=0.13.25 or iOS>=1.1.1 or Android>=1.2.1 supports bypassing CORS locally. But you are using an old version, and you're suggested to upgrade Obsidian.", diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json index 473d4da..c62bd27 100644 --- a/src/langs/zh_cn.json +++ b/src/langs/zh_cn.json @@ -220,6 +220,10 @@ "settings_onedrive_auth_button": "鉴权", "settings_onedrive_connect_succ": "很好!我们可连接上 OneDrive!", "settings_onedrive_connect_fail": "我们未能连接上 OneDrive。", + "settings_onedrive_emptyfile": "空文件处理", + "settings_onedrive_emptyfile_desc": "OneDrive 不允许上传空文件(即使官网也是不允许的)。那么你想跳过空文件还是返回错误?", + "settings_onedrive_emptyfile_skip": "跳过", + "settings_onedrive_emptyfile_error": "返回错误和中断", "settings_webdav": "Webdav 设置", "settings_webdav_disclaimer1": "声明:您所输入的信息存储于本地。其它有害的或者出错的插件,是有可能读取到这些信息的。如果您发现了 Webdav 服务器有不符合预期的访问,请立刻修改用户名和密码。", "settings_webdav_cors_os": "Obsidian 桌面版>=0.13.25 或 iOS>=1.1.1 或 Android>=1.2.1 支持跳过 CORS 设置。但您正在使用旧版,建议升级。", diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json index 1eb8c1f..b24ff9d 100644 --- a/src/langs/zh_tw.json +++ b/src/langs/zh_tw.json @@ -219,6 +219,10 @@ "settings_onedrive_auth_button": "鑑權", "settings_onedrive_connect_succ": "很好!我們可連線上 OneDrive!", "settings_onedrive_connect_fail": "我們未能連線上 OneDrive。", + "settings_onedrive_emptyfile": "空檔案處理", + "settings_onedrive_emptyfile_desc": "OneDrive 不允許上傳空檔案(即使官網也是不允許的)。那麼你想跳過空檔案還是返回錯誤?", + "settings_onedrive_emptyfile_skip": "跳過", + "settings_onedrive_emptyfile_error": "返回錯誤和中斷", "settings_webdav": "Webdav 設定", "settings_webdav_disclaimer1": "宣告:您所輸入的資訊儲存於本地。其它有害的或者出錯的外掛,是有可能讀取到這些資訊的。如果您發現了 Webdav 伺服器有不符合預期的訪問,請立刻修改使用者名稱和密碼。", "settings_webdav_cors_os": "Obsidian 桌面版>=0.13.25 或 iOS>=1.1.1 或 Android>=1.1.1 支援跳過 CORS 設定。但您正在使用舊版,建議升級。", diff --git a/src/main.ts b/src/main.ts index 015c4bb..e20fb6f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -855,6 +855,9 @@ export default class RemotelySavePlugin extends Plugin { if (this.settings.onedrive.remoteBaseDir === undefined) { this.settings.onedrive.remoteBaseDir = ""; } + if (this.settings.onedrive.emptyFile === undefined) { + this.settings.onedrive.emptyFile = "skip"; + } if (this.settings.webdav.manualRecursive === undefined) { this.settings.webdav.manualRecursive = true; } diff --git a/src/settings.ts b/src/settings.ts index 56bcd26..3894fc8 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1418,6 +1418,20 @@ export class RemotelySaveSettingTab extends PluginSettingTab { }); }); + new Setting(onedriveDiv) + .setName(t("settings_onedrive_emptyfile")) + .setDesc(t("settings_onedrive_emptyfile_desc")) + .addDropdown(async (dropdown) => { + dropdown + .addOption("skip", t("settings_onedrive_emptyfile_skip")) + .addOption("error", t("settings_onedrive_emptyfile_error")) + .setValue(this.plugin.settings.onedrive.emptyFile) + .onChange(async (val) => { + this.plugin.settings.onedrive.emptyFile = val as any; + await this.plugin.saveSettings(); + }); + }); + new Setting(onedriveDiv) .setName(t("settings_checkonnectivity")) .setDesc(t("settings_checkonnectivity_desc"))