commit 9e69f0793f1be27454facdc2688134517a492e8d Author: winters27 Date: Fri Apr 17 14:37:37 2026 -0700 feat: Initial BYOC fork (from Remotely Save) diff --git a/927.main.js b/927.main.js new file mode 100644 index 0000000..361d136 --- /dev/null +++ b/927.main.js @@ -0,0 +1 @@ +"use strict";(this.webpackChunkobsidian_byoc=this.webpackChunkobsidian_byoc||[]).push([[927],{86927(e,t,r){r.d(t,{EventStreamSerde:()=>s});var i=r(65792),n=r(42637);class s{marshaller;serializer;deserializer;serdeContext;defaultContentType;constructor({marshaller:e,serializer:t,deserializer:r,serdeContext:i,defaultContentType:n}){this.marshaller=e,this.serializer=t,this.deserializer=r,this.serdeContext=i,this.defaultContentType=n}async serializeEventStream({eventStream:e,requestSchema:t,initialRequest:r}){const i=this.marshaller,n=t.getEventStreamMember(),s=t.getMemberSchema(n),a=this.serializer,o=this.defaultContentType,l=Symbol("initialRequestMarker"),c={async*[Symbol.asyncIterator](){if(r){const e={":event-type":{type:"string",value:"initial-request"},":message-type":{type:"string",value:"event"},":content-type":{type:"string",value:o}};a.write(t,r);const i=a.flush();yield{[l]:!0,headers:e,body:i}}for await(const t of e)yield t}};return i.serialize(c,e=>{if(e[l])return{headers:e.headers,body:e.body};const t=Object.keys(e).find(e=>"__type"!==e)??"",{additionalHeaders:r,body:i,eventType:n,explicitPayloadContentType:a}=this.writeEventBody(t,s,e);return{headers:{":event-type":{type:"string",value:n},":message-type":{type:"string",value:"event"},":content-type":{type:"string",value:a??o},...r},body:i}})}async deserializeEventStream({response:e,responseSchema:t,initialResponseContainer:r}){const n=this.marshaller,s=t.getEventStreamMember(),a=t.getMemberSchema(s).getMemberSchemas(),o=Symbol("initialResponseMarker"),l=n.deserialize(e.body,async e=>{const r=Object.keys(e).find(e=>"__type"!==e)??"",n=e[r].body;if("initial-response"===r){const e=await this.deserializer.read(t,n);return delete e[s],{[o]:!0,...e}}if(r in a){const t=a[r];if(t.isStructSchema()){const s={};let a=!1;for(const[o,l]of t.structIterator()){const{eventHeader:t,eventPayload:c}=l.getMergedTraits();if(a=a||Boolean(t||c),c)l.isBlobSchema()?s[o]=n:l.isStringSchema()?s[o]=(this.serdeContext?.utf8Encoder??i.P)(n):l.isStructSchema()&&(s[o]=await this.deserializer.read(l,n));else if(t){const t=e[r].headers[o]?.value;null!=t&&(l.isNumericSchema()?s[o]=t&&"object"==typeof t&&"bytes"in t?BigInt(t.toString()):Number(t):s[o]=t)}}if(a)return{[r]:s};if(0===n.byteLength)return{[r]:{}}}return{[r]:await this.deserializer.read(t,n)}}return{$unknown:e}}),c=l[Symbol.asyncIterator](),d=await c.next();if(d.done)return l;if(d.value?.[o]){if(!t)throw new Error("@smithy::core/protocols - initial-response event encountered in event stream but no response schema given.");for(const[e,t]of Object.entries(d.value))r[e]=t}return{async*[Symbol.asyncIterator](){for(d?.value?.[o]||(yield d.value);;){const{done:e,value:t}=await c.next();if(e)break;yield t}}}}writeEventBody(e,t,r){const i=this.serializer;let s,a=e,o=null;const l={};if(t.getSchema()[4].includes(e)){const n=t.getMemberSchema(e);if(n.isStructSchema()){for(const[t,i]of n.structIterator()){const{eventHeader:n,eventPayload:s}=i.getMergedTraits();if(s)o=t;else if(n){const n=r[e][t];let s="binary";i.isNumericSchema()?s=(-2)**31<=n&&n<=2**31-1?"integer":"long":i.isTimestampSchema()?s="timestamp":i.isStringSchema()?s="string":i.isBooleanSchema()&&(s="boolean"),null!=n&&(l[t]={type:s,value:n},delete r[e][t])}}if(null!==o){const t=n.getMemberSchema(o);t.isBlobSchema()?s="application/octet-stream":t.isStringSchema()&&(s="text/plain"),i.write(t,r[e][o])}else i.write(n,r[e])}else{if(!n.isUnitSchema())throw new Error("@smithy/core/event-streams - non-struct member not supported in event stream union.");i.write(n,{})}}else{const[t,n]=r[e];a=t,i.write(15,n)}const c=i.flush()??new Uint8Array;return{body:"string"==typeof c?(this.serdeContext?.utf8Decoder??n.a)(c):c,eventType:a,explicitPayloadContentType:s,additionalHeaders:l}}}}}]); \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7d5e8f8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,3 @@ +The codes or files or subfolders inside the folder `src`, `tests`, `docs`, `assets`, are released under the "Open Source" license: "Apache License, version 2.0", described at: https://www.apache.org/licenses/LICENSE-2.0 . + +The codes or files or subfolders inside the folder `pro`, are released under the "Source Available" license: "PolyForm Strict License 1.0.0", described at: https://polyformproject.org/licenses/strict/1.0.0/ . diff --git a/README.md b/README.md new file mode 100644 index 0000000..3debe0c --- /dev/null +++ b/README.md @@ -0,0 +1,98 @@ +# Bring Your Own Cloud (BYOC) + +> A clean, self-hosted synchronization plugin for [Obsidian](https://obsidian.md). + +BYOC is a community-maintained fork of the excellent [Remotely Save](https://github.com/remotely-save/remotely-save) plugin. This version focuses on providing a clean, completely self-hosted experience while maintaining the robust sync engine created by the original developers. + +--- + +## Features + +- **12 cloud providers** — S3, WebDAV, Dropbox, OneDrive (AppFolder & Full), Google Drive, Box, pCloud, Yandex Disk, Koofr, Azure Blob Storage, Webdis +- **3-Way Merge Sync Engine** — tracks a local baseline to make correct push/pull/conflict decisions across sessions +- **Smart Conflict Resolution** — automatically creates timestamped conflict copies instead of silently overwriting +- **Independent Authorization** — connect your own cloud provider APIs with credentials that persist locally. +- **End-to-end encryption** — optional AES-256 / rclone-crypt encryption before data leaves your device +- **Auto-sync** — timed background sync and sync-on-save +- **Seamless migration** — automatically imports your existing Remotely Save config on first load + +--- + +## Installation + +### Manual (Recommended) + +1. Download the [latest release](../../releases/latest) — grab `main.js`, `manifest.json`, and `styles.css` +2. Copy them into your vault at `.obsidian/plugins/obsidian-byoc/` +3. In Obsidian → Settings → Community Plugins, enable **Bring Your Own Cloud** + +### From Source + +```bash +git clone https://github.com/obsidian-byoc/obsidian-byoc +cd obsidian-byoc +npm install +npm run build +# Copy main.js + manifest.json to your vault's .obsidian/plugins/obsidian-byoc/ +``` + +--- + +## Provider Setup + +### S3-Compatible (AWS, Cloudflare R2, Backblaze B2, MinIO, etc.) + +Enter your endpoint, region, access key ID, secret key, and bucket name in plugin settings. + +### WebDAV + +Enter your server URL, username, and password. Works with Nextcloud, ownCloud, Synology, and any WebDAV server. + +### Dropbox / OneDrive / Google Drive / Box / Yandex Disk / Koofr + +Click **Auth** in the provider settings panel. You'll be redirected to the provider's OAuth page, then back to Obsidian automatically. + +### pCloud + +Click **Auth** in pCloud settings. Select your region (US or EU) during the OAuth flow. Your access token is stored locally and never expires. + +> **Note:** BYOC uses a registered pCloud application. If you have your own pCloud API key, you can override it by setting the `PCLOUD_CLIENT_ID` and `PCLOUD_CLIENT_SECRET` environment variables before building. + +### Azure Blob Storage + +Enter your container SAS URL and container name. + +--- + +## Configuration + +All settings are in Obsidian → Settings → **Bring Your Own Cloud**. + +| Setting | Description | +|---------|-------------| +| **Service** | Which cloud provider to sync with | +| **Password** | Optional encryption passphrase | +| **Auto Sync Interval** | Sync every N milliseconds (−1 to disable) | +| **Sync on Save** | Sync N ms after the last file change | +| **Conflict Action** | `keep_newer`, `keep_larger`, or `smart_conflict` | +| **Protect Modify %** | Abort if more than N% of files would be changed | +| **Sync Config Dir** | Whether to sync `.obsidian/` settings | +| **Delete To** | Move deleted files to system trash or Obsidian trash | + +--- + +## Migrating from Remotely Save + +BYOC automatically detects your Remotely Save `data.json` on first launch and imports all credentials and settings. A backup of your current config is created at `.obsidian/plugins/obsidian-byoc/data.json.byoc-backup` before any changes are made. + +## Credits + +BYOC is a fork of **Remotely Save**, originally created by [fyears](https://github.com/remotely-save). The core sync algorithm, multi-provider architecture, and foundational logic are the result of their exceptional work. + +We are incredibly grateful to the original developer for creating such a robust tool and making it open source. If you are looking for the original plugin, please visit the [Remotely Save Repository](https://github.com/remotely-save/remotely-save). + +--- + +## License + +[Apache 2.0](LICENSE) — inherited from the upstream Remotely Save project. diff --git a/assets/branding/256x256.png b/assets/branding/256x256.png new file mode 100644 index 0000000..074f6d6 --- /dev/null +++ b/assets/branding/256x256.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a9bf1cc2403c3a32e7a6f5d5829bb065573922dc050ae2fdb8b3d34b4c87ecf5 +size 8296 diff --git a/assets/branding/300x300.png b/assets/branding/300x300.png new file mode 100644 index 0000000..114f921 --- /dev/null +++ b/assets/branding/300x300.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6990a598379c457442345e50c3f056c4598a121523bc10980835c08fc7447a54 +size 10119 diff --git a/assets/branding/300x300.svg b/assets/branding/300x300.svg new file mode 100644 index 0000000..e02c13b --- /dev/null +++ b/assets/branding/300x300.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:432198e63fa9676541a41436917e025d1ea6af3772139c1dea450fcc0155cfd0 +size 13054 diff --git a/assets/branding/50x50.png b/assets/branding/50x50.png new file mode 100644 index 0000000..3126d7a --- /dev/null +++ b/assets/branding/50x50.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fe3b5b8bf9c32e57c0af366c455b11ae68eb9fb8a32f371c9c445b3e7e89cc02 +size 1120 diff --git a/assets/branding/64x64.png b/assets/branding/64x64.png new file mode 100644 index 0000000..2fb628a --- /dev/null +++ b/assets/branding/64x64.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f68df7bae94eb4c4cddfa6af47d7819d01a6482d4f6afe6ef3236f72f9adf15f +size 1523 diff --git a/assets/branding/LICENSE.txt b/assets/branding/LICENSE.txt new file mode 100644 index 0000000..7c03569 --- /dev/null +++ b/assets/branding/LICENSE.txt @@ -0,0 +1 @@ +This work is licensed under CC BY-SA 4.0. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ diff --git a/assets/branding/README.md b/assets/branding/README.md new file mode 100644 index 0000000..4fd6509 --- /dev/null +++ b/assets/branding/README.md @@ -0,0 +1,75 @@ +# the logo + +Some different sizes' logos are needed. + +## license + +Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. + +## the process of creation + +No professional designers are here. Thus the following steps involve many programer styles. :-) + +1. use excalidraw and export png and svg. + + ```bash + # results + logo.excalidraw + logo.png + logo.svg + ``` + +2. manually edit the `logo.svg` and make background transparent. + + ```bash + # results + logo-transparent.svg + ``` + +3. use python library [`svgutils`](https://github.com/btel/svg_utils) to make a strictly square figure. The [doc](https://svgutils.readthedocs.io/en/latest/tutorials/composing_multipanel_figures.html) is very useful. + + ```python + from svgutils.compose import * + def get_standard_300x300(file_name): + fig = Figure(300, 300, + Panel( + SVG(file_name), + ).move(-3, 12), + ) + return fig + + get_standard_300x300('logo-transparent.svg').save('300x300.svg') + + # def get_other_size_from_standard(file_name, px): + # fig = Figure(px, px, + # Panel( + # SVG(file_name).scale(px/300.0), + # ).move(-3*px/300.0, 12*px/300.0), + # ) + # return fig + + # get_other_size_from_standard('logo.svg',256).save('256x256.svg') + ``` + + ```bash + # results + 300x300.svg + ``` + +4. use `inkscape` command line to get different sizes' `.png` files. + + ```bash + inkscape 300x300.svg -o 300x300.png + + inkscape 300x300.svg -o 50x50.png -w 50 -h 50 + + inkscape 300x300.svg -o 64x64.png -w 64 -h 64 + inkscape 300x300.svg -o 256x256.png -w 256 -h 256 + ``` + + ```bash + # results + 50x50.png + 64x64.png + 256x256.png + ``` diff --git a/assets/branding/logo-transparent.svg b/assets/branding/logo-transparent.svg new file mode 100644 index 0000000..c9f97eb --- /dev/null +++ b/assets/branding/logo-transparent.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:daedd262170200cb5e8cc288898b40892771b3962cf2bc17dc443f1c82a4aa94 +size 12886 diff --git a/assets/branding/logo.excalidraw b/assets/branding/logo.excalidraw new file mode 100644 index 0000000..3d1b57e --- /dev/null +++ b/assets/branding/logo.excalidraw @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6b33b2a71f2d91988416c17015b59ef205dbf6a8c704ecac1cc95ed03ccc2c8e +size 16537 diff --git a/assets/branding/logo.png b/assets/branding/logo.png new file mode 100644 index 0000000..4a4519d --- /dev/null +++ b/assets/branding/logo.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:98358aa726dcb5565164ff260634f5c527223d1f2ec1d0410b400625c8dad704 +size 14082 diff --git a/assets/branding/logo.svg b/assets/branding/logo.svg new file mode 100644 index 0000000..4332f7f --- /dev/null +++ b/assets/branding/logo.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02a5057ebe38e4c3835c8354f53aca01c4c2335d8a0435e22f509b4ca4a73ca4 +size 12889 diff --git a/assets/cla-process/cla-robot-alert.png b/assets/cla-process/cla-robot-alert.png new file mode 100644 index 0000000..eb79220 --- /dev/null +++ b/assets/cla-process/cla-robot-alert.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:07fd075d0f73c708d77fd97bb31d6f185f094031e8275e8fd582e3a0145f6be8 +size 415757 diff --git a/assets/cla-process/cla-sign-and-pass.png b/assets/cla-process/cla-sign-and-pass.png new file mode 100644 index 0000000..1833798 --- /dev/null +++ b/assets/cla-process/cla-sign-and-pass.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c5754df8b5d6878a7fa58992fc7e37ddcce1ecf578a0fc2ec751b6f7bab6f13 +size 284700 diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..9623dda --- /dev/null +++ b/biome.json @@ -0,0 +1,55 @@ +{ + "$schema": "https://biomejs.dev/schemas/1.8.3/schema.json", + "organizeImports": { + "enabled": true + }, + "files": { + "ignore": ["main.js"] + }, + "formatter": { + "enabled": true, + "formatWithErrors": false, + "ignore": [], + "attributePosition": "auto", + "indentStyle": "space", + "indentWidth": 2, + "lineEnding": "lf", + "lineWidth": 80 + }, + "javascript": { + "formatter": { + "arrowParentheses": "always", + "bracketSameLine": false, + "bracketSpacing": true, + "jsxQuoteStyle": "double", + "quoteProperties": "asNeeded", + "semicolons": "always", + "trailingCommas": "es5" + } + }, + "linter": { + "enabled": true, + "rules": { + "recommended": true, + "suspicious": { + "noExplicitAny": "off", + "noPrototypeBuiltins": "off", + "noControlCharactersInRegex": "off" + }, + "style": { + "noUselessElse": "off", + "useNodejsImportProtocol": "off", + "noUnusedTemplateLiteral": "off", + "useTemplate": "off", + "noNonNullAssertion": "off" + }, + "performance": { + "noDelete": "off" + }, + "complexity": { + "noForEach": "off", + "useLiteralKeys": "off" + } + } + } +} diff --git a/docs/browser_env.md b/docs/browser_env.md new file mode 100644 index 0000000..4ec2651 --- /dev/null +++ b/docs/browser_env.md @@ -0,0 +1,12 @@ +# Limitations From The Browser Environment + +Obsidian desktop is developed by using [Electron](https://www.electronjs.org/). And Obsidian mobile is developed by using [Capacitor](https://capacitorjs.com/) + +Technically, the plugin (or any plugin?) runs in the js environment provided by Obsidian. And to support the mobile Obsidian, the plugin is limited to be developed for the browser environment, instead of the Node.js environment. + +Then some limitations are applied: + +1. [The CORS issue (solved in the new Obsidian version on some platforms).](./browser_env_cors.md) +2. [No Node.js environment.](./browser_env_no_nodejs.md) +3. If the cloud service uses OAuth flow, it needs to support PKCE. More details are [here](./browser_env_oauth2_pkce.md). +4. [No background running after Obsidian is closes.](./browser_env_no_background_after_closing.md) diff --git a/docs/browser_env_cors.md b/docs/browser_env_cors.md new file mode 100644 index 0000000..552c3de --- /dev/null +++ b/docs/browser_env_cors.md @@ -0,0 +1,14 @@ +# Limitations From The Browser Environment: CORS Issue + +The plugin is developed for the browser environment. The "fake" browser behind the scenes also follows the CORS policy. + +[MDN has a doc about CORS.](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) + +1. From Obsidian desktop >= 0.13.25 or mobile >= 1.1.1, Obsidian [provides a new API `requiestUrl`](https://forum.obsidian.md/t/obsidian-release-v0-13-25-insider-build/32701), that allows the plugin to fully bypass the CORS issue. As of Mar 2022, the latest public-released Obsidian desktop has supported this API, but the Obsidian mobile still stays in insider. + +2. For using this plugin in Obsidian desktop < 0.13.25 or mobile < 1.1.1, we need to configure the server side to return the header `Access-Control-Allow-Origin` allowing the origins `app://obsidian.md` and `capacitor://localhost` and `http://localhost`. + + Example configurations: + + - [Amazon S3](./s3_cors_configure.md) + - [Apache](./apache_cors_configure.md) ([contributed by community](https://github.com/remotely-save/remotely-save/pull/31)) diff --git a/docs/browser_env_no_background_after_closing.md b/docs/browser_env_no_background_after_closing.md new file mode 100644 index 0000000..14d0673 --- /dev/null +++ b/docs/browser_env_no_background_after_closing.md @@ -0,0 +1,3 @@ +# Limitations From The Browser Environment: No Background Running After Obsidian Is Closed + +The plugin treats Obsidian as a special browser, and is in fact some js codes. So if Obsidian is closed, then the browser environment stops, then the plugin will be stopped. diff --git a/docs/browser_env_no_nodejs.md b/docs/browser_env_no_nodejs.md new file mode 100644 index 0000000..8cea7cc --- /dev/null +++ b/docs/browser_env_no_nodejs.md @@ -0,0 +1,9 @@ +# Limitations From The Browser Environment: No Node.js + +To support the mobile Obsidian, the plugin is limited to be developed for the browser environment, instead of the Node.js environment. + +Many js libraries are designed to work in both the browser and the Node.js environments. But some are not, because the browser doesn't provide the corresponding abilities. + +For example, there is a popular npm package [`ssh2-sftp-client`](https://www.npmjs.com/package/ssh2-sftp-client) for SFTP. But it relies on the modules (e.g. `http`) from Node.js which cannot be "translated" to the browser environment. So it's impossible to make this plugin support SFTP. The same status applies to FTP / FTPS. + +Likewise, [MEGA](https://mega.nz/) provides a SDK, but the SDK is [for C++ only](https://mega.nz/doc), so it's also impossible to make this plugin support MEGA. diff --git a/docs/browser_env_oauth2_pkce.md b/docs/browser_env_oauth2_pkce.md new file mode 100644 index 0000000..237b467 --- /dev/null +++ b/docs/browser_env_oauth2_pkce.md @@ -0,0 +1,7 @@ +# Limitations From The Browser Environment: OAuth2 PKCE + +If the cloud service uses OAuth flow, it needs to support PKCE, because the plugin is released to the public, and no real secrets can be statically kept in the client. + +Luckily, Dropbox and OneDrive supports PKCE, making it possible for this plugin to connect to them easily. + +Dropbox has an excellent [article](https://dropbox.tech/developers/pkce--what-and-why-) explaining what is and how to use PKCE. diff --git a/docs/check_performance/README.md b/docs/check_performance/README.md new file mode 100644 index 0000000..8f44e06 --- /dev/null +++ b/docs/check_performance/README.md @@ -0,0 +1,13 @@ +# Check performance + +1. Go to settings, scroll to the very end, and enable the "Enable Profiler" option. +2. Also enable "Enable Profiler Printing". +3. Check Console Output (directly or via `vConsole` plugin). More details are [here](../how_to_debug/README.md). +4. Sync! +5. You can also "Export Profiler Results" afterwards. A new file `_debug_remotely_save/profiler_results_exported_on_xxxx.md` will be generated. + +In the console or exported files, you can see the time cost of each steps. + +![profiler settings](./profiler_settings.png) + +![profiler exported](./profiler_exported.png) diff --git a/docs/check_performance/profiler_exported.png b/docs/check_performance/profiler_exported.png new file mode 100644 index 0000000..5ef6711 --- /dev/null +++ b/docs/check_performance/profiler_exported.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a71d0746a75c93aa4cb5be3f7964bfdf74722a10505f5a1965790738eaebdc4 +size 265289 diff --git a/docs/check_performance/profiler_settings.png b/docs/check_performance/profiler_settings.png new file mode 100644 index 0000000..4e3f842 --- /dev/null +++ b/docs/check_performance/profiler_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4ffd1557c2f714c56bfacd8166b85749d6bfa15c2f9c7363942b443d51288083 +size 81259 diff --git a/docs/code_design.md b/docs/code_design.md new file mode 100644 index 0000000..7a8df80 --- /dev/null +++ b/docs/code_design.md @@ -0,0 +1,13 @@ +# Code Design + +## Code Organization + +1. Every function except `main.ts` should be pure. Pass any stateful information in parameters. + +2. `misc.ts` should not depend on any other written code. + +3. Each storage code should not depend on `sync.ts`. + +## File and Folder Representation + +While writing sync codes, folders are always represented by a string ending with `/`. diff --git a/docs/dropbox_review_material/README.md b/docs/dropbox_review_material/README.md new file mode 100644 index 0000000..3e71fac --- /dev/null +++ b/docs/dropbox_review_material/README.md @@ -0,0 +1,47 @@ +## Material Source + +To apply for the production use of Dropbox API, some descriptions are needed to be submitted to Dropbox. Coincidently, this can also be served as a "readme" to use this plugin with Dropbox. + +## Some Backgrounds + +1. Obsidian allows users to take notes using markdown files. +2. The "app", remotely-save. is an unofficial Obsidian plugin, helping users download and upload ("sync") their notes from and to Dropbox. +3. Technically, remotely-save is developed using web technologies. + +## Api Usages + +1. remotely-save uses "App folder", to avoid unnecessary accessing users' other files. +2. remotely-save uses "account_info.read", to get the displayed username, so that users know which of their accounts has been logged in after OAuth steps. +3. remotely-save uses "files.content.read", so that it can read the "last modified time", and the content of files on Dropbox. Under some conditions, the plugin would download the files. For example, the plugin would compare the last modified timestamps of the file in the local device and that on Dropbox, and if the timestamp on Dropbox is larger, the plugin would download the "newer" file from Dropbox to local. +4. remotely-save uses "files.content.write", so that it can upload or overwrite the content of files on Dropbox. Under some conditions, the plugin would do that. For example, the plugin would compare the last modified timestamps of the file in the local device and that on Dropbox, and if the timestamp in the local device is larger, the plugin would upload the "newer" file from local to Dropbox, and overwrite that file on Dropbox. + +## Steps + +Here are the steps to see the functionality of remotely-save. + +Most steps have screenshots. + +1. Download the note-taking app Obsidian (Windows or Mac versions are both ok) from its official website: https://obsidian.md/ . It's free to download and use. Then install it. +2. Open Obsidian, click the "Create" button under "Create new vault". +3. Pick a vault name, "example-vault", and choose a location, then click "Create". + ![step03](./attachments/step03.png) +4. Close any update new if prompted. +5. Create a new note by clicking a button on the left. And write something on the note. + ![step05](./attachments/step05.png) +6. Click "setting" (a gear icon) on the button left of the sidebar. + ![step06](./attachments/step06.png) +7. In the settings panel, go to the "Community plugins" page, turn off the safe mode, and confirm to turn off the safe mode. Then click the "Browse" button for community plugins. + ![step07](./attachments/step07.png) +8. Search "Remotely Save" and install on the result. + ![step08](./attachments/step08.png) +9. After successful installing the plugin, go back to the "Community plugins" page, and enable the plugin. + ![step09](./attachments/step09.png) +10. Go to newly added "Remotely Save" settings, select "Dropbox" in "Choose service", and click the "Auth" button. + ![step10](./attachments/step10.png) +11. The standard auth flow address is shown, users should click the address, and finish the auth steps on the website. Finally, the Dropbox website should automatically redirect users back to the Obsidian app. + ![step11](./attachments/step11.png) +12. The "Auth" button disappears. A new "Revoke Auth" appears. + ![step12](./attachments/step12.png) +13. Go back to the main interface of Obsidian, a new "switch icon" should appear on the left sidebar. Click this, then the plugin would trigger the sync progress. It would compare meta info of local files and remote files (on Dropbox), and decide to download some files and/or upload some files. + ![step13](./attachments/step13.png) +14. Create, edit, remove some notes, and repeat step 13, the files on Dropbox should also change to reflect the changes locally. diff --git a/docs/dropbox_review_material/attachments/step03.png b/docs/dropbox_review_material/attachments/step03.png new file mode 100644 index 0000000..40cba18 --- /dev/null +++ b/docs/dropbox_review_material/attachments/step03.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:655f6f487cc24af0ab86b8592622a7701c30f3369e7847a1fbb783d0962829c3 +size 52910 diff --git a/docs/dropbox_review_material/attachments/step05.png b/docs/dropbox_review_material/attachments/step05.png new file mode 100644 index 0000000..f5edd7d --- /dev/null +++ b/docs/dropbox_review_material/attachments/step05.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fe971af94f1f7b042b7313268e4d2d8432d926ce2392e50ca2a4abc21c8a4aab +size 57033 diff --git a/docs/dropbox_review_material/attachments/step06.png b/docs/dropbox_review_material/attachments/step06.png new file mode 100644 index 0000000..09abd08 --- /dev/null +++ b/docs/dropbox_review_material/attachments/step06.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:025303053f2b2cedd2bdd61cd71e1f1c0fb12e45bfbc0e81f2c0d70747133505 +size 63458 diff --git a/docs/dropbox_review_material/attachments/step07.png b/docs/dropbox_review_material/attachments/step07.png new file mode 100644 index 0000000..9f081cb --- /dev/null +++ b/docs/dropbox_review_material/attachments/step07.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:061707977c8046a6f924881f86aac962e2f05618e201080768246270de792040 +size 100162 diff --git a/docs/dropbox_review_material/attachments/step08.png b/docs/dropbox_review_material/attachments/step08.png new file mode 100644 index 0000000..5b2ad5a --- /dev/null +++ b/docs/dropbox_review_material/attachments/step08.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8add61e6ef34b2470e5a5bea3e10955a86eed5ed1fc4dd398f30835485a6c455 +size 158422 diff --git a/docs/dropbox_review_material/attachments/step09.png b/docs/dropbox_review_material/attachments/step09.png new file mode 100644 index 0000000..5712cb8 --- /dev/null +++ b/docs/dropbox_review_material/attachments/step09.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:021ae1ce29d3932f088f0954787c475be826915b794320bf212d3ba7d58656da +size 110529 diff --git a/docs/dropbox_review_material/attachments/step10.png b/docs/dropbox_review_material/attachments/step10.png new file mode 100644 index 0000000..7c6d934 --- /dev/null +++ b/docs/dropbox_review_material/attachments/step10.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b225d21de5778ddc032893e0f52150ab4260b0dfbe0e9141da4aa8b03131a9da +size 147821 diff --git a/docs/dropbox_review_material/attachments/step11.png b/docs/dropbox_review_material/attachments/step11.png new file mode 100644 index 0000000..f9d764c --- /dev/null +++ b/docs/dropbox_review_material/attachments/step11.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fda4f6e8cf68833f239bdc85c46802fe2131d149011074f747b1829822a38939 +size 59041 diff --git a/docs/dropbox_review_material/attachments/step12.png b/docs/dropbox_review_material/attachments/step12.png new file mode 100644 index 0000000..04559ca --- /dev/null +++ b/docs/dropbox_review_material/attachments/step12.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:576801d5ebd8cf7fcd3c9c0c6df325654338854f7d762a5680e9aab3c9e6508a +size 142155 diff --git a/docs/dropbox_review_material/attachments/step13.png b/docs/dropbox_review_material/attachments/step13.png new file mode 100644 index 0000000..b93b989 --- /dev/null +++ b/docs/dropbox_review_material/attachments/step13.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1c93f681f0f7ce74cee0f2ed193f574ef915554f1a9ef96ba54afdede302f13a +size 112057 diff --git a/docs/encryption/README.md b/docs/encryption/README.md new file mode 100644 index 0000000..b7d4dff --- /dev/null +++ b/docs/encryption/README.md @@ -0,0 +1,8 @@ +# Encryption + +Currently (March 2024), Remotely Save supports two end to end encryption format: + +1. [RClone Crypt](./rclone.md) format, which is the recommend way now. +2. [OpenSSL enc](./openssl.md) format + +Here is also the [comparation](./comparation.md). diff --git a/docs/encryption/comparation.md b/docs/encryption/comparation.md new file mode 100644 index 0000000..9409f8f --- /dev/null +++ b/docs/encryption/comparation.md @@ -0,0 +1,23 @@ +# Comparation Between Encryption Formats + +## Warning + +**ALWAYS BACKUP YOUR VAULT MANUALLY!!!** + +If you switch between RClone Crypt format and OpenSSL enc format, you have to delete the cloud vault files **manually** and **fully**, so that the plugin can re-sync (i.e. re-upload) the newly encrypted versions to the cloud. + +## The feature table + +| | RClone Crypt | OpenSSL enc | comments | +| ------------------------ | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| key generation | scrypt with fixed salt | PBKDF2 with dynamic salt | scrypt is better than PBKDF2 from the algorithm aspect. But RClone uses fixed salt by default. Also the parameters might affect the result. | +| content encryption | XSalsa20Poly1305 on chunks | AES-256-CBC | XSalsa20Poly1305 is way better than AES-256-CBC. And encryption by chunks should require less resources. | +| file name encryption | EME on each segment of the path | AES-256-CBC on the whole path | RClone has the benefit as well as pitfall that the path structure is preserved. Maybe it's more of a design decision difference? No comment on EME and AES-256-CBC. | +| viewing decrypted result | RClone has command that can mount the encrypted vault as if the encryption is transparent. | No convenient way except writing some scripts we are aware of. | RClone is way more convenient. | + +## Some notes + +1. Anyway, security is a hard problem. The author of Remotely Save doesn't have sufficient knowledge to "judge" which one is the better format. **Use them at your own risk.** +2. Currently the RClone Crypt format is recommended by default in Remotely Save. Just because of the taste from the Remotely Save author, who likes RClone. +3. **Always use a long password.** +4. Both algorithms are selected deliberately to **be compatible with some well-known third-party tools** (instead of some home-made methods) and **have many tests to ensure the correctness**. diff --git a/docs/encryption/openssl.md b/docs/encryption/openssl.md new file mode 100644 index 0000000..000609c --- /dev/null +++ b/docs/encryption/openssl.md @@ -0,0 +1,46 @@ +# OpenSSL enc format + +If a password is set, the files are encrypted before being sent to the cloud. + +## Warning + +**ALWAYS BACKUP YOUR VAULT MANUALLY!!!** + +If you switch between RClone Crypt format and OpenSSL enc format, you have to delete the cloud vault files **manually** and **fully**, so that the plugin can re-sync (i.e. re-upload) the newly encrypted versions to the cloud. + +## Comparation between encryption formats + +See the doc [Comparation](./comparation.md). + +## Interoperability with official OpenSSL + +This encryption algorithm is delibrately designed to be aligned with openssl format. + +1. The encryption algorithm is implemented using web-crypto. Using AES-256-CBC. +2. The file content is encrypted using openssl format. Assuming a file named `sometext.txt`, a password `somepassword`, then the encryption is equivalent to the following command: + + ```bash + # file content encryption (ignoring file path encryption) + openssl enc -p -aes-256-cbc -pbkdf2 -iter 20000 -pass pass:somepassword -in ./sometext.txt -out ./sometext.txt.enc + + # file content decryption (ignoring file path decryption) + openssl enc -d -p -aes-256-cbc -pbkdf2 -iter 20000 -pass pass:somepassword -in ./sometext.txt.enc -out ./sometext.txt + ``` + +3. The file/directory path strings, are encrypted using openssl in binary mode and then `base64url without padding` is applied. + Assuming the file path is `a-folder-文件夹/a-file-文件.md`, then the following commands are equivilent: + + ```bash + # prepare the functions + # https://unix.stackexchange.com/questions/628842 + base64url::encode () { base64 -w0 | tr '+/' '-_' | tr -d '='; } + base64url::decode () { awk '{ if (length($0) % 4 == 3) print $0"="; else if (length($0) % 4 == 2) print $0"=="; else print $0; }' | tr -- '-_' '+/' | base64 -d; } + + # pure string encryption then base32 + echo -n 'a-folder-文件夹/a-file-文件.md' | openssl enc -aes-256-cbc -pbkdf2 -iter 20000 -pass pass:mylongpassword | base64url::encode + + # pure string base64url then decryption + echo -n 'U2FsdGVkX19tNkdFL5rZeHxbe7FL-Pp5mkZJkDNFJWFT6lldZlfa57j0C_cKn0I3PZ9YDvOkyoKqfF6lbn0_yg' | base64url::decode | openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -pass pass:mylongpassword + ``` + +4. The directory is considered as special "0-byte" object on remote s3. So this meta infomation may be easily guessed if some third party can access the remote bucket. diff --git a/docs/encryption/rclone.md b/docs/encryption/rclone.md new file mode 100644 index 0000000..bc7f1c7 --- /dev/null +++ b/docs/encryption/rclone.md @@ -0,0 +1,46 @@ +# RClone Crypt format + +The encryption is compatible with RClone Crypt with **base64** name encryption format. + +It's developed based on another js project by the same author of Remotely Save: [`@fyears/rclone-crypt`](https://github.com/fyears/rclone-crypt), which is NOT an official library from RClone, and is NOT affiliated with RClone. + +Reasonable tests are also ported from official RClone code, to ensure the compatibility and correctness of the encryption. + +## Warning + +**ALWAYS BACKUP YOUR VAULT MANUALLY!!!** + +If you switch between RClone Crypt format and OpenSSL enc format, you have to delete the cloud vault files **manually** and **fully**, so that the plugin can re-sync (i.e. re-upload) the newly encrypted versions to the cloud. + +## Comparation between encryption formats + +See the doc [Comparation](./comparation.md). + +## Interoperability with official RClone + +Please pay attention that the plugin uses **base64** of encrypted file names, while official RClone by default uses **base32** file names. The intention is purely for potentially support longer file names. + +You could set up the RClone profile by calling `rclone config`. You need to create two profiles, one for your original connection and the other for RClone Crypt. + +Finally, a working config file should like this: + +```ini +[webdav1] +type = webdav +url = https://example.com/sharefolder1/subfolder1 # the same as the web address in Remotely Save settings. +vendor = other +user = +pass = + +[webdav1crypt] +type = crypt +remote = nas1test:vaultname # the same as your "Remote Base Directory" (usually the vault name) in Remotely Save settings +password = +filename_encoding = base64 # don't forget this!!! +``` + +You can use the `mount` command to view and see the files in file explorer! On Windows, the command should like this (the remote vault is mounted to drive `X:`): + +```bash +rclone mount webdav1crypt: X: --network-mode --vfs-cache-mode writes +``` diff --git a/docs/how_to_debug/README.md b/docs/how_to_debug/README.md new file mode 100644 index 0000000..b6fd5fc --- /dev/null +++ b/docs/how_to_debug/README.md @@ -0,0 +1,25 @@ +# How To Debug + +Ideally, users do not need to debug anything. But if something bad happened, we have to dig into details. + +## Easy: Export Sync Plans + +See [here](./export_sync_plans.md). + +## Medium: Desktop: Check Console Output Directly + +...of desktop and Android. + +See [here](./check_console_output.md). + +## Medium: Mobile: Use `Obsidian vConsole` to Check Console Ouput Directly + +...of mobile (iOS and Android). + +See [here](./check_vconsole_output.md). + +## Advanced: Use `Logstravaganza` to export logs + +This method works for desktop and mobile devices (iOS, Android), especially useful for iOS. + +See [here](./use_logstravaganza.md). diff --git a/docs/how_to_debug/check_console_output.md b/docs/how_to_debug/check_console_output.md new file mode 100644 index 0000000..0d99d53 --- /dev/null +++ b/docs/how_to_debug/check_console_output.md @@ -0,0 +1,25 @@ +# Check Console Output + +If you are using Obsidian on desktop or Android, you can check the Obsidian console. + +## Disable Auto Sync Firstly + +You should disable auto sync to avoid any unexpected running. + +## Set The Output Level To Debug + +Go to the plugin settings, scroll down to the section "Debug" -> "alter console log level", and change it from "info" to "debug". + +## Check The Output + +- If you are on desktop + + Press the keyboard shortcut "ctrl+shift+i" if you are on Windows or Linux, or press "cmd+shift+i" if you are on macOS. You should be able to see the console of Obsidian. + +- If you are using Android + + If you want to check the console **with the help of desktop Chrome**, you need to [enable USB debugging](https://developer.android.com/studio/debug/dev-options#enable) on your Android, then connect your Android to a computer using USB, then open the **desktop** Chrome browser and go to the special web page . You shall be able to see the "inspect" link inside the special page, then click the link to open the console. After debugging, remember to turn off USB debugging. + + If you want to check the console **directly on the mobile device**, check out [this doc](./check_vconsole_output.md). + +Trigger the sync manually (by clicking the icon on the ribbon sidebar). Something (hopefully) helpful should show up in the console. You could understand what happened and what goes wrong more explictly by checking the output. diff --git a/docs/how_to_debug/check_vconsole_output.md b/docs/how_to_debug/check_vconsole_output.md new file mode 100644 index 0000000..b39e6fa --- /dev/null +++ b/docs/how_to_debug/check_vconsole_output.md @@ -0,0 +1,21 @@ +# Check "Obsidian vConsole" Output + +It's quite hard to debug on mobile. But fortunately you can use another plugin to rescue! + +This applies to both iOS (iPhone / iPad) and Android. + +## Disable Auto Sync Firstly + +You should disable auto sync to avoid any unexpected running. + +## Set The Output Level To Debug + +Go to the plugin settings, scroll down to the section "Debug" -> "alter console log level", and change it from "info" to "debug". + +## Use "Obsidian vConsole" + +1. Install the third-party plugin ["Obsidian vConsole"](https://github.com/zhouhua/obsidian-vconsole). +2. Enable "Obsidian vConsole". You shall see a green button in the right buttom corner of the interface of your mobile Obsidian. You can drag this green button to where you like. +3. Sync! And quickly click the vConsole green button, you will see the console logs in its panel! Moreover, you can check browser network and Storage (LocalStorage) in the panel! +4. You can copy (by clicking the small "disk" icon beside each line of the log) or take a screenshot of the logs, and report bugs in Remotely Save GitHub. You can click "Hide" in the panel to hide the panel. +5. If you don't need to debug any more, you can disable Obsidian vConsole plugin, then the green button should disappear. diff --git a/docs/how_to_debug/export_sync_plans.md b/docs/how_to_debug/export_sync_plans.md new file mode 100644 index 0000000..894e697 --- /dev/null +++ b/docs/how_to_debug/export_sync_plans.md @@ -0,0 +1,95 @@ +# Export Sync Plans + +## What's this? + +Everytime the plugin starts a sync, it gathers all required information together, generates a "sync plan" of every operations to every files and folders, and assign the corresponding actual operations. + +Thus, if something goes wrong, we should check the sync plan firstly. + +## How To Export The Plans? + +Please read through the following instructions. + +### Disable Auto Sync Firstly + +You should disable auto sync to avoid any unexpected running. + +### Manual Sync If Not Yet + +You should at least sync once, so that at least one sync plan is generated and saved. If you have synced the vualt before, there should be some sync plans already saved. + +### Export To The File + +Go to the plugin settings, scroll down to the section "Debug" -> "export sync plans", and click the button "Export". \*\*It would generate a new folder `_debug_remotely_save/` in your vault, and generate a file `sync_plans_hist_exported_on_{a_timestamp},md.` inside that folder. + +## How To Read The Plans + +Open the genrated `sync_plans_hist_exported_on_{a_timestamp},md.`. You should see a json, or multiple jsons. Every json represents a sync plan. + +A sync plan looks like this: + +```json +{ + "ts": 1646960867560, + "remoteType": "onedrive", + "mixedStates": { + "abc.md": { + "key": "abc.md", + "existRemote": true, + "mtimeRemote": 1646566632000, + "sizeRemote": 56797, + "remoteEncryptedKey": "abc.md", + "changeMtimeUsingMapping": true, + "existLocal": true, + "mtimeLocal": 1646566632000, + "sizeLocal": 56797, + "decision": "skipUploading", + "decisionBranch": 1 + }, + "New folder/": { + "key": "New folder/", + "deltimeRemote": 1646925354372, + "existLocal": false, + "existRemote": false, + "decision": "keepRemoteDelHistFolder", + "decisionBranch": 9 + } + } +} +``` + +We usually care about the `mixedStates` property. As you may guess, every item in `mixedStates` represent a file or a folder. + +We should find out the file/folder we are interested in (or we believe something goes wrong), then checkout the following properties: + +``` +decision + What decision is made. + +decisionBranch + It's a mark of the actual logic in the sync code. Useful to debug. + +existRemote + Does the file/folder exist on the remote service? + +mtimeRemote + The "last modeified time" on the remote service. + +deltimeRemote + The "deletion time" on the remote record. + +existLocal + Does the file/folder exist locally? + +mtimeLocal + The max of "last modeified time" and "creation time" locally. + +deltimeLocal + The "deletion time" locally. +``` + +The `decision` SHOULD be determined by the modified times and deletion times, by the logic described in [the doc of sync alogorithm](../sync_algorithm_v2.md). In short, we collect four timestamps (`mtimeRemote`, `deltimeRemote`, `mtimeLocal`, `deltimeLocal`), and respect the max timestamp and its corresponding operation. + +## Common Issues + +Some users report that their "last modeified time"s or "creation time"s are not set correctly by the operating system. In this case, the plugin cannot do anything because it determines the sync plan by comparing the timestamps. It's suggested to check the settings of the operating system or check whether other programs are doing something to the files. diff --git a/docs/how_to_debug/use_logstravaganza.md b/docs/how_to_debug/use_logstravaganza.md new file mode 100644 index 0000000..17add7f --- /dev/null +++ b/docs/how_to_debug/use_logstravaganza.md @@ -0,0 +1,14 @@ +# Use `Logstravaganza` + +On iOS, it's quite hard to directly check the console logs. + +Luckily, there is a third-party plugin: [`Logstravaganza`](https://obsidian.md/plugins?search=Logstravaganza#), by Carlo Zottmann, that can redirect the output to a note. + +You can just: + +1. Install it. +2. Enable it. +3. Do something, to trigger some console logs. +4. Checkout `LOGGING-NOTE (device name).md` in the root of your vault. + +See more on its site: . diff --git a/docs/import_export_some_settings.md b/docs/import_export_some_settings.md new file mode 100644 index 0000000..64ee09a --- /dev/null +++ b/docs/import_export_some_settings.md @@ -0,0 +1,19 @@ +## Bonus: Import And Export Not-Oauth2 Plugin Settings By QR Code + +It's often verbose / inconvenient to input not-oauth2 credentials on mobile. (On different desktop computers, it's trivial to copy and paste `data.json` across different devices.) + +So this plugin has a helper function to export those settings as a QR code, then you could use mobile device's camera to import the settings. + +Attention: + +1. Oauth2 - related information is omitted. It means that Dropbox, OneDrive login credentials are NOT included in the QR Code. +2. Please, NEVER share the QR Code to others. It's as equivalent to the login credentials. + +Steps: + +1. Configure the plugin settings on one device, and make sure the sync functions work. (E.g, you could sync notes using S3 credentials.) +2. Open plugin settings page. Then scroll down the page, until the section "Import and Export Settings". Click the button "Get QR Code". A new modal should show up and you should see a QR code. +3. On a second device, make sure the vault name is the same as the first device's. +4. On that second device, use its camera app, or any apps that support scanning QR codes, to scan the QR code from the first device. (On latest iOS, the system's built in camera app should work. On Android, at least one open source app [Binary Eye](https://github.com/markusfisch/BinaryEye) is tested to be working.) +5. A link / url / address should be identified in the scan-QR-code app, and you could follow the instruction in the app, and then you should be redirected to open the Obsidian app. +6. And finally, there should be a new notice in the Obsidian app saying the settings are successfully imported. Otherwise please check the error message in the notice. diff --git a/docs/linux.md b/docs/linux.md new file mode 100644 index 0000000..7580029 --- /dev/null +++ b/docs/linux.md @@ -0,0 +1,56 @@ +# How to receive `obsidian://` in Linux + +## Background + +For example, when we are authorizing OneDrive, we have to jump back to Obsidian automatically using `obsidian://`. + +## Short Desc From Official Obsidian Doc + +Official doc has some explanation: + + + +# Long Desc + +Assuming the username is `somebody`, and the `.AppImage` file is downloaded to `~/Desktop`. + +1. Download and **extract** the app image file in terminal + + ```bash + cd /home/somebody/Desktop + chmod +x Obsidian-x.y.z.AppImage + ./Obsidian-x.y.z.AppImage --appimage-extract + + # you should have the folder squashfs-root + # we want to rename it + mv squashfs-root Obsidian + ``` + +2. Create a `.desktop` file + + ```bash + # copy and paste the follow MULTI LINE command + # you might need to input your password because it requires root privilege + # remember to adjust the path + cat > ~/Desktop/obsidian.desktop < +* Sign up / Sign in: +* User profile: + +# Disclaimer + +It's different from, and NOT affiliated with Obsidian account. + +# Steps + +## Steps of signing up and signing in + +1. Go to the website, sign up and sign in. You can directly visit or click the link in Remotely Save plugin setting page. + ![pro setting](./pro_setting.png) +2. Use an email and your password as usual. Don't need to be GMail account. + +## Steps of connecting + +You need to connect your plugin to your online account. In Obsidian, in your Remotely Save plugin setting, you can click the button "Connect" to start the flow. + +1. You will see a special address on website. Click it and visit the website +2. Click "allow" on the website. +3. In the end of the auth flow on the website, you will be shown up a code, please copy it... +4. And paste the code back to the plugin modal inside Obsidian, and confirm. + +![connect flow](./connect_flow.png) + +## Steps of subscribing to some features. + +1. Firstly please visit your [profile page](https://remotelysave.com/user/profile) online. +2. You can subscribe to some features. Prices vary. + ![PRO features online](./pro_features_enabled_on_website.png) +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. + ![check again PRO features](./check_pro_features_in_settings.png) +4. Sync and enjoy your PRO features! + +## Why so complicated? + +Because we don't have a payment method inside the plugin, so we have to: + +* build a website, +* require users having online accounts +* and connect the plugin to the online account. + +Moreover, an online account allows flexible management of subscriptions. diff --git a/docs/pro/check_pro_features_in_settings.png b/docs/pro/check_pro_features_in_settings.png new file mode 100644 index 0000000..45eb41d --- /dev/null +++ b/docs/pro/check_pro_features_in_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8c73da29a621d225387640b76ec0dcfb56b3ffad151dab374d3b7cc76067a1a0 +size 94377 diff --git a/docs/pro/connect_flow.png b/docs/pro/connect_flow.png new file mode 100644 index 0000000..5f3db02 --- /dev/null +++ b/docs/pro/connect_flow.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:116c2a8ec1a277a3985f4056b390574fc57145ec5635d44accf2a7445666264a +size 626668 diff --git a/docs/pro/pro_features_enabled_on_website.png b/docs/pro/pro_features_enabled_on_website.png new file mode 100644 index 0000000..c0091b6 --- /dev/null +++ b/docs/pro/pro_features_enabled_on_website.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4c45c08e6fc487341032aac511c332e3f9978a44e9b93377a73648b56b684b62 +size 155427 diff --git a/docs/pro/pro_setting.png b/docs/pro/pro_setting.png new file mode 100644 index 0000000..00929fb --- /dev/null +++ b/docs/pro/pro_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08d128125dba033f5dd18a6ea8d942b284ddca6c5c3b0362c47dcb4a7153a8f9 +size 395800 diff --git a/docs/remote_services/azureblobstorage/README.md b/docs/remote_services/azureblobstorage/README.md new file mode 100644 index 0000000..debfbd2 --- /dev/null +++ b/docs/remote_services/azureblobstorage/README.md @@ -0,0 +1,107 @@ +# Azure Blob Storage (GDrive) (PRO) + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Microsoft / Azure product, and just uses Azure Blob Storage's public api.** + +# Disclaimer + +I (author of Remotely Save) is **NOT** an expert of Azure products. The tutorials here are for references only. Azure products are very complex. + +***As far as the law allows, the software (Remotely Save) comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.*** + +# Steps + +Please read through the following steps before you actually connect. + +## Preparation In Azure Blob Storage Side + +You only need to do this **once**, before the Container SAS Url expires in the future. + +In short, you need to: configure CORS and configure a policy and cobtain a Container SAS Url. + +1. Connect to the service: + + Download Microsoft's free [Azure Storage Explorer](https://azure.microsoft.com/en-us/products/storage/storage-explorer). The following tutorial uses this app. Actually you may be able to find the same settings on Azure official website. + + Use this app to connect to your Blob Storage service or account. + +2. CORS: + + Right click on `Blob containers`, click "configure CORS". Add a rule, enter the following and save: + ``` + Allowed Origins: * + Allowed Methods: DELETE,GET,HEAD,MERGE,POST,PATCH,OPTIONS,PUT + Allowed Headers: x-ms-*, content-type + Exposed Headers: x-ms-*, content-type + Max Age (in seconds): 5 + ``` + + ![CORS screenshot](./azure_cors.png) + +3. Create the container: + + Create the container if you don't have one. In this tutorial, we use `example-container`. + + ![container screenshot](./azure_example_container.png) + +4. Generate a policy: + + * Right click on you container, click "Manage Stored Access Policies". + * Choose your id, for example `example-container-0000001`. + * Change to Expiry time to an appropriate date. **By default it is only valid for a week.** After its expiration, you need come back and adjust the expiry date again! In this tutorial, we set it to a year. + * And allow these methods: Read, Add, Create, Write, Delete, List. + * Save + + Read Microsoft's official [doc](https://learn.microsoft.com/en-us/rest/api/storageservices/define-stored-access-policy) for more info. The main benefit of generating an access policy is easier revocation of SAS Url if anything goes bad. + + ![Manage Stored Access Policies screenshot](./azure_policy_1.png) + ![Add Policy screenshot](./azure_policy_3.png) + +5. Generate a Container SAS Url: + + Then we want to create a container level SAS (shared access signature) url. + + * Right click on you container, click "Get Shared Access Signature". + * In access policy, choose the previous one you created: for example `example-container-0000001`. + * Save the setting + * You will see a url is generated, which starts with `https://` or `http://`. It should looks like `https://.blob.core.windows.net/?sv=...&sig=...` + * Save the Container SAS Url somewhere, and you will need this later. + + Read Microsoft's official [doc](https://learn.microsoft.com/en-us/azure/storage/common/storage-sas-overview) for more info. + + ![Container SAS Url screenshot 1](./azure_sas_1.png) + ![Container SAS Url screenshot 2](./azure_sas_2.png) + +### Why so complicated in Azure settings? + +Because Azure Blob Storage's api has some limitation in browser environment, so we need to configure CORS and SAS. Because we want to revoke the SAS when needed, we need to use a policy. + +### Revocation + +If you suspect someone read the resource unexpectedly, you can revoke the SAS by changing the name or expiry date of the policy. And generate the SAS Url again. + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Azure Blob Storage" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Azure Blob Storage should be detected. + +## Steps of Connecting to your Azure Blob Storage + +After you enabled the PRO feature in your Remotely Save plugin, **and prepared the Container SAS Url**, you can connect to your Azure Blob Storage account now. + +1. In Remotely Save settings, change your sync service to Azure Blob Storage. +2. Input your Container SAS Url +3. Input your container name. +4. By default, the plugin will save your vault remotely with a prefix `/`. You can change the prefix. No prefix is not allowed. +5. Check the connection. A notice will tell you that you've connected or not. +7. Sync! The plugin will upload the fils and "folders" of your vault into your Azure Blob Storage with the preix. +8. **Read the caveats below.** + +![RS setting screenshot](./azure_rs_setting.png) + +# The caveats + +* As of June 2024, this feature is in alpha stage. **Back up your vault before using this feature.** diff --git a/docs/remote_services/azureblobstorage/azure_cors.png b/docs/remote_services/azureblobstorage/azure_cors.png new file mode 100644 index 0000000..a1cb25f --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_cors.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83caba9a42772f995035e3c879c6119eabcd6d3c27298cf0fb067b3676680571 +size 107057 diff --git a/docs/remote_services/azureblobstorage/azure_example_container.png b/docs/remote_services/azureblobstorage/azure_example_container.png new file mode 100644 index 0000000..feb5f8b --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_example_container.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ef2d525d3ac7ecca26f7f8b6782fb1056d54f8c558a8d77586571defb7e6b5be +size 41282 diff --git a/docs/remote_services/azureblobstorage/azure_policy_1.png b/docs/remote_services/azureblobstorage/azure_policy_1.png new file mode 100644 index 0000000..6e6b482 --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_policy_1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cba5bf27e76eaf8ea21e51e43172501fc4867539c90c1758a02945477dc261ea +size 132487 diff --git a/docs/remote_services/azureblobstorage/azure_policy_3.png b/docs/remote_services/azureblobstorage/azure_policy_3.png new file mode 100644 index 0000000..c1602e9 --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_policy_3.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b2add8a559eab0b7073f7ed52d4a3e4efae68b499c7b924d1ac50278758961b +size 193894 diff --git a/docs/remote_services/azureblobstorage/azure_rs_setting.png b/docs/remote_services/azureblobstorage/azure_rs_setting.png new file mode 100644 index 0000000..eb25f7d --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_rs_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:582a69a7aee2cfab72dc14324e4a864acab92c145d533e799f8ceea61a8f0102 +size 478941 diff --git a/docs/remote_services/azureblobstorage/azure_sas_1.png b/docs/remote_services/azureblobstorage/azure_sas_1.png new file mode 100644 index 0000000..4a7c4cc --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_sas_1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cbb8863d98d8826739b5bf9fa4c7a2de3ba49fde4bbe3c4988a877ba74f89abd +size 143007 diff --git a/docs/remote_services/azureblobstorage/azure_sas_2.png b/docs/remote_services/azureblobstorage/azure_sas_2.png new file mode 100644 index 0000000..2470ad3 --- /dev/null +++ b/docs/remote_services/azureblobstorage/azure_sas_2.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ddc30f97d75d7c3a6061cbb30cc991cbdf1f4039aad3547ef88998c3dd9be666 +size 132904 diff --git a/docs/remote_services/box/README.md b/docs/remote_services/box/README.md new file mode 100644 index 0000000..99bf33e --- /dev/null +++ b/docs/remote_services/box/README.md @@ -0,0 +1,34 @@ +# Box (PRO) + +# Links + + + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Box product, and just uses Box's public api.** + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Box" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Box should be detected. + +## Steps of Connecting to your Box + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your Box account now. + +1. In Remotely Save settings, change your sync service to Box. +2. Click Auth, visit the link, go to Box website to start. +3. Follow the instruction on Box, and allow Remotely Save to connect. +4. You will be redirected back to Remotely Save plugin. +5. A notice will tell you that you've connected or not. +6. Sync! The plugin will create a vault folder in the root of your Box and upload notes into that folder. +7. **Read the caveats below.** + +# The caveats + +* As of June 2024, this feature is in beta stage. **Back up your vault before using this feature.** diff --git a/docs/remote_services/googledrive/README.md b/docs/remote_services/googledrive/README.md new file mode 100644 index 0000000..c914941 --- /dev/null +++ b/docs/remote_services/googledrive/README.md @@ -0,0 +1,59 @@ +# Google Drive (GDrive) (PRO) + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Google product, and just uses Google Drive's public api.** + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Google Drive" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Google Drive should be detected. + +## Steps of Connecting to your Google Drive + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your Google Drive account now. + +1. In Remotely Save settings, change your sync service to Google Drive. + ![change remote to google drive](./change_remote_to_google_drive.png) +2. Click Auth, visit the link, go to Remotely Save website to start. + ![visit start link](./google_drive_auth_link.png) +3. On the website, click the link to go to Google Drive auth page. +4. Follow the instruction on Google website, and allow (continue) Remotely Save to connect. + ![allow Remotely Save in Google website](./google_drive_auth_allow.png) +5. You will be redirected to Remotely Save website, and you will get a code. Copy it... + ![redirected back and get the code](./google_drive_auth_code_show.png) +6. ... And paste the code back to the plugin inside Obsidian. Click submit. + ![submit the code in setting](./google_drive_code_submit.png) +7. A notice will tell you that you've connected or not. +8. Sync! The plugin will create a vault folder in the root of your Google Drive and upload notes into that folder. +9. **Read the caveats below.** + +# Why so complicated? + +Because Google Drive's api doesn't fit into the special envorinment of Obsidian plugin. So we need a website. + +# The credential + +The website does **NOT** store or save the Google drive credential (the code you obtian in the end of the flow). The website is just a "bridge" to help you obtain that code, and just manage your subscription to PRO features. + +But please be aware that the code is saved locally in your Obsidian. It works like a special password. So that the plugin can upload or download or modify the files for you. + +# The caveats + +* As of June 2024, this feature is in beta stage. **Back up your vault before using this feature.** + +* The plugin can **only** sees, reads or writes the files and folders created by itself! + + It means that, you CANNOT manually create the vault folder in your Google Drive account. And if you manually upload any files using Google's official website, the plugin does **NOT** see them. All operations must go through Obsidian and uploaded by the plugin. + + You can, however, view, and download the files on Google Drive [official web page](https://drive.google.com/drive/u/0/my-drive). + + Precisely speaking, the plugin applies for the `drive.file` scope recommended by Google. See [the doc](https://developers.google.com/drive/api/guides/api-specific-auth#benefits) by Google for the scope's benefits. Basically the plugin will never (is unable to) mess up your other files or folders. + + Moreover, this scope is "not-sensitive", so that the plugin doesn't need to go through Google's complicated verification process while applying for it. + +* Google Drive, unlike other cloud storage, allows files of same name co-existing in the same folder! (hmmmmm...) It may or may not make the plugin stop working. Users might need to remove the duplicated file manually on Google's official website. diff --git a/docs/remote_services/googledrive/change_remote_to_google_drive.png b/docs/remote_services/googledrive/change_remote_to_google_drive.png new file mode 100644 index 0000000..9bf336f --- /dev/null +++ b/docs/remote_services/googledrive/change_remote_to_google_drive.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eaa9aa8ba6b066dfefdce08a8d1da1d97089f5c2de0ba4c0c026d6df9eb9ece8 +size 99896 diff --git a/docs/remote_services/googledrive/google_drive_auth_allow.png b/docs/remote_services/googledrive/google_drive_auth_allow.png new file mode 100644 index 0000000..e118624 --- /dev/null +++ b/docs/remote_services/googledrive/google_drive_auth_allow.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6683a63718053603ae7dde44063a3807ba0def7e935da0b40fcc8e181afd24ae +size 342848 diff --git a/docs/remote_services/googledrive/google_drive_auth_code_show.png b/docs/remote_services/googledrive/google_drive_auth_code_show.png new file mode 100644 index 0000000..d5c283f --- /dev/null +++ b/docs/remote_services/googledrive/google_drive_auth_code_show.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:310bd4c7d5fe19521ec7184b2eee749f0eecf00397f2b8e68480c23b4fd7c943 +size 153504 diff --git a/docs/remote_services/googledrive/google_drive_auth_link.png b/docs/remote_services/googledrive/google_drive_auth_link.png new file mode 100644 index 0000000..dcfa8ee --- /dev/null +++ b/docs/remote_services/googledrive/google_drive_auth_link.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dcebb1f73829670883071447a8eee46c972b26fe8735c7422949cb6fe927b8ab +size 179369 diff --git a/docs/remote_services/googledrive/google_drive_code_submit.png b/docs/remote_services/googledrive/google_drive_code_submit.png new file mode 100644 index 0000000..dae659f --- /dev/null +++ b/docs/remote_services/googledrive/google_drive_code_submit.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7e5a85c28f5614e4de75eacc021c2876d36596bcb77f0744a6edcff7777b9530 +size 155004 diff --git a/docs/remote_services/koofr/README.md b/docs/remote_services/koofr/README.md new file mode 100644 index 0000000..d4abf5b --- /dev/null +++ b/docs/remote_services/koofr/README.md @@ -0,0 +1,38 @@ +# Koofr (PRO) + +# Links + + + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Koofr product, and just uses Koofr's public api.** + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Koofr" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Koofr should be detected. + +## Steps of Connecting to your Koofr + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your Koofr account now. + +1. In Remotely Save settings, change your sync service to Koofr. +2. Click Auth, visit the link, go to Koofr website to start. +3. Follow the instruction on Koofr, and allow Remotely Save to connect. +4. You will be redirected back to Remotely Save plugin. +5. A notice will tell you that you've connected or not. +6. Sync! The plugin will create a vault folder in the root of your Koofr and upload notes into that folder. +7. **Read the caveats below.** + +# The caveats + +* As of June 2024, this feature is in beta stage. **Back up your vault before using this feature.** + +# Why not use webdav? + +The Remotely Save PRO feture "sync with Koofr" is developed using Koofr's native API, instead of webdav interface. It brings benefits such that the last modified time can be preserved. diff --git a/docs/remote_services/onedrive/README.md b/docs/remote_services/onedrive/README.md new file mode 100644 index 0000000..971f526 --- /dev/null +++ b/docs/remote_services/onedrive/README.md @@ -0,0 +1,31 @@ +# OneDrive (App Folder) + +- **This plugin is NOT an official Microsoft / OneDrive product.** The plugin just uses Microsoft's [OneDrive's public API](https://docs.microsoft.com/en-us/onedrive/developer/rest-api). +- After the authorization, the plugin can read your name and email, and read and write files in your OneDrive's `/Apps/remotely-save` folder.**The free version of Remotely Save only connects to App Folder, while the PRO version can connect to the root folder in Onedrive.** +- If you decide to authorize this plugin to connect to OneDrive, please go to plugin's settings, and choose OneDrive then follow the instructions. +- Password-based end-to-end encryption is also supported. But please be aware that **the vault name itself is not encrypted**. +- If you want to sync the files across multiple devices, **your vault name should be the same** while using default settings. + +## FAQ + +### How about OneDrive for Business? + +This plugin only works for "OneDrive for personal", and not works for "OneDrive for Business" (yet). See [#11](https://github.com/fyears/remotely-save/issues/11) to further details. + +### I cannot find `/Apps/remotely-save` folder + +Mystically some users report that their OneDrive generate `/Application/Graph` instead of `/Apps/remotely-save`. See [#517](https://github.com/remotely-save/remotely-save/issues/517). + +The solution is simple: + +1. Backup your vault manually. +2. Go to onedrive website (), and rename `/Application/Graph` to `/Application/remotely-save` (right click on the folder and you will see rename option) +3. Come back to Obsidian and try to sync! + +### Empty file is not uploaded + +Onedrive's API does not allow uploading empty files. You can choose to skip them or raise errors in Remotely Save plugin's settings. + +### How to connect to root folder rather than the App Folder? + +As of June 2024, this is a new PRO feature. See the [doc](../onedrivefull/README.md). diff --git a/docs/remote_services/onedrivefull/README.md b/docs/remote_services/onedrivefull/README.md new file mode 100644 index 0000000..26138e9 --- /dev/null +++ b/docs/remote_services/onedrivefull/README.md @@ -0,0 +1,42 @@ +# Onedrive (Full) (PRO) + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Microsoft / Onedrive product, and just uses Onedrive's public api.** +* This still only applies to Onedrive for personal. The dev team doesn't have Onedrive for business account to develop and test on it. + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Onedrive (Full)" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Onedrive (Full) should be detected. + +## Steps of Connecting to your Onedrive + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your Onedrive account now. + +1. In Remotely Save settings, change your sync service to Onedrive (Full). +2. Click Auth, visit the link, go to Onedrive website to start. +3. Follow the instruction on Onedrive, and allow Remotely Save to connect. +4. You will be redirected back to Remotely Save plugin. +5. A notice will tell you that you've connected or not. +6. Sync! The plugin will create a vault folder **in the root** of your Onedrive and upload notes into that folder. +7. **Read the caveats below.** + +# The caveats + +* As of June 2024, this feature is in beta stage. + * **Back up your vault before using this feature.** + * **Back up everything in your Onedrive (besides the sync sub folder) before using this feature!** +* Onedrive's API does not allow uploading empty files. You can choose to skip them or raise errors in Remotely Save plugin's settings. + +# What's the difference of Onedrive (App Folder) and Onedrive (Full)? + +Due to history reasons, Remotely Save only supported uploading to App Folder back to year 2021. Because that greatly ensured the security and ensure not messing up with others files. + +However, repeatedly some users want to sync to arbitrary root folder rather than `/Apps/remotely-save/` folder. Thus as of June 2024, the new PRO feature is finally developed. + +As of June 2024, connecting to Onedrive (App Folder) is free, but connecting to Onedrive (Full) is a PRO feature. diff --git a/docs/remote_services/pcloud/README.md b/docs/remote_services/pcloud/README.md new file mode 100644 index 0000000..89ca313 --- /dev/null +++ b/docs/remote_services/pcloud/README.md @@ -0,0 +1,40 @@ +# pCloud (PRO) + +# Links + + + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official pCloud product, and just uses pCloud's public api.** + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with pCloud" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with pCloud should be detected. + +## Steps of Connecting to your pCloud + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your pCloud account now. + +1. In Remotely Save settings, change your sync service to pCloud. +2. Click Auth, visit the link, go to pCloud website to start. +3. Follow the instruction on pCloud, and allow Remotely Save to connect. +4. You will be redirected back to Remotely Save plugin. +5. A notice will tell you that you've connected or not. +6. Sync! The plugin will create a vault folder in the root of your pCloud and upload notes into that folder. +7. **Read the caveats below.** + +# The caveats + +* As of June 2024, this feature is in beta stage. **Back up your vault before using this feature.** +* pCloud's API seems to not allow uploading empty files. You can choose to skip them or raise errors in Remotely Save plugin's settings. + +# Why not use webdav? + +1. pCloud only provides webdav for Premium. If you are a Premium user, you can still use pCloud webdav with Remotely Save. +2. The Remotely Save PRO feture "sync with pCloud" is developed using pCloud's native API, instead of webdav interface. It brings benefits such that the last modified time can be preserved. diff --git a/docs/remote_services/s3_backblaze_b2/README.md b/docs/remote_services/s3_backblaze_b2/README.md new file mode 100644 index 0000000..3ef1160 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/README.md @@ -0,0 +1,53 @@ +# Backblaze B2 + +## Links + +https://www.backblaze.com/cloud-storage + +## Steps + +1. Create a Backblaze account [on this page](https://www.backblaze.com/cloud-storage). Credit card info _is not_ required. Backblaze B2 offers 10 GB of free storage. + +2. Please be aware that, though B2 provides some free quota, **it may still cost you money if the usage of storage or api requests exceed a certain value!!!** Especially pay attention to the api requests!!! + +3. Create a **bucket**, you can leave the default settings, or you can enable the encryption (which is different from what you can set in Remotely Save): + + ![](./s3_backblaze_b2-1-bucket.png) + ![](./s3_backblaze_b2-2-create_bucket.png) + +4. Copy `Endpoint`, eg. `s3.us-east-005.backblazeb2.com` — it'll be used later. + +5. Copy `bucketname` near the 🪣 icon (the "bucket icon") — it'll be used later. + + ![](./s3_backblaze_b2-3-copy.png) + +6. Go to **Application Keys**: + + ![](./s3_backblaze_b2-4-app_keys.png) + +7. **Add a new key**: + + ![](./s3_backblaze_b2-5-add_new_app_keys.png) + ![](./s3_backblaze_b2-6-app_keys_copy.png) + +8. Save `keyID` and `applicationKey` — they will be used later. + +9. Go to Remotely Save settings in Obsidian and: + + - Choose `S3 or compatibile` in **Remote Service**: + - Copy `Endpoint` from Backblaze (see 3. above) to `Endpoint` in Remotely Save + - From `endpoint` take `region` (eg. `us-east-005`) and paste it in `endpoint` in Remotely Save + - Copy `keyID` (see 7. above) to `Access Key ID` in Remotely Save + - Copy `applicationKey` (see 7. above) to `Secret Access Key` in Remotely Save + - Copy `bucketname` (see 4. above) to `Bucket Name` in Remotely Save + ![](./s3_backblaze_b2-7-copy_paste.png) + +10. **Enable Bypass CORS**: + ![](./s3_backblaze_b2-8-cors.png) + +11. Click **Check** in _Check Connectivity_ to see if you can connect to B2 bucket: + ![](./s3_backblaze_b2-9-check_connectionpng.png) + +12. Sync! + + ![](./s3_backblaze_b2-10-sync.png) diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-1-bucket.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-1-bucket.png new file mode 100644 index 0000000..13ff07b --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-1-bucket.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fae18a84bb218461aca01c7de47ddadfd2b451d865ec889c098cd6918f6e8773 +size 59679 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-10-sync.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-10-sync.png new file mode 100644 index 0000000..b91a4f9 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-10-sync.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9389f9c4462740371edb548f7e05191ede5277a5d470e3254c60fbcc4a75e552 +size 4558 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-2-create_bucket.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-2-create_bucket.png new file mode 100644 index 0000000..2000f22 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-2-create_bucket.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:46f35968938ebf3bbafae64ee49a4927844fd0abf79e4190522be91776d9fed0 +size 107623 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-3-copy.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-3-copy.png new file mode 100644 index 0000000..22c895d --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-3-copy.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7fb639c7b4e595516594ee9d07c864e7cd15b608a97c81386f4cf72d499d0e5f +size 80121 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-4-app_keys.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-4-app_keys.png new file mode 100644 index 0000000..111dafa --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-4-app_keys.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:299bfca82b77de86f60d8f481b5f74069dc977dece19fa025ee629d1e6f783d4 +size 6499 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-5-add_new_app_keys.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-5-add_new_app_keys.png new file mode 100644 index 0000000..8698138 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-5-add_new_app_keys.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5985e918b521c91cc185f60c58cdd334ee76695b897a02e02eaa17c1bad71609 +size 10214 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-6-app_keys_copy.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-6-app_keys_copy.png new file mode 100644 index 0000000..f93df1b --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-6-app_keys_copy.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5796791bcf89c99c1e1e844c0083739fd46b1a458dc274f3c3fa3f65b87b71b3 +size 70728 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-7-copy_paste.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-7-copy_paste.png new file mode 100644 index 0000000..cb6bdfc --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-7-copy_paste.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3633379fcb12f1a5e0a4d1d68c193ddc7bf0be7edd92f2f6006351817035a94f +size 27164 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-8-cors.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-8-cors.png new file mode 100644 index 0000000..8d44cc9 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-8-cors.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e130be182ae7d3270e5de3fec442ac5e81efd61822e844cd61ed24c7e485d39 +size 16802 diff --git a/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-9-check_connectionpng.png b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-9-check_connectionpng.png new file mode 100644 index 0000000..ffbe3d7 --- /dev/null +++ b/docs/remote_services/s3_backblaze_b2/s3_backblaze_b2-9-check_connectionpng.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:29a8971fea065fd86223850198640f4072ee3fda2112820829cda1348f122c59 +size 11103 diff --git a/docs/remote_services/s3_cloudflare_r2/README.md b/docs/remote_services/s3_cloudflare_r2/README.md new file mode 100644 index 0000000..77343da --- /dev/null +++ b/docs/remote_services/s3_cloudflare_r2/README.md @@ -0,0 +1,27 @@ +# Cloudflare R2 + +## Links + + + +## Steps + +1. **Be aware that it may cost you money.** +2. Create a Cloudflare account and enable R2 feature. **Credit card info might be required by Cloudflare**, though Cloudflare provides generous free tier and zero egress fee. +3. Create a bucket. + ![](./s3_cloudflare_r2_create_bucket.png) +4. Create an Access Key with "Object Read & Write" permission, and add specify to your created bucket. During the creation, you will also get the auto-generated secret key, and the endpoint address. + ![](./s3_cloudflare_r2_create_api_token.png) +5. In remotely-save setting page, input the address / bucket / access key / secret key. **Region being set to `us-east-1` is sufficient.** Enable "Bypass CORS", because usually that's what you want. + + Click "check connectivity". (If you encounter an issue and sure the info are correct, please upgrade remotely-save to **version >= 0.3.29** and try again.) + + ![](./s3_cloudflare_r2_rs_settings.png) + +6. Sync! + +## And Issue Related To "Check Connectivity" + +If you encounter an issue and sure the info are correct, please upgrade remotely-save to **version >= 0.3.29** and try again. + +Cloudflare doesn't allow `HeadBucket` for access keys with "Object Read & Write". So it may be possible that checking connectivity is not ok but actual syncing is ok. New version >= 0.3.29 of the plugin fix this problem by using `ListObjects` instead of `HeadBucket`. diff --git a/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_api_token.png b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_api_token.png new file mode 100644 index 0000000..1ebfa77 --- /dev/null +++ b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_api_token.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:49a5cd07e538205ba733dc3ccb30e4e7da1290a4a80aefe08afac19fd2db2232 +size 467448 diff --git a/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_bucket.png b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_bucket.png new file mode 100644 index 0000000..6507403 --- /dev/null +++ b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_create_bucket.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:61fc0812fe96871eed507d2f6b7bbad790ab70e95ff5fe764f8f8b2ab8ce82c2 +size 153015 diff --git a/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_rs_settings.png b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_rs_settings.png new file mode 100644 index 0000000..2452ad9 --- /dev/null +++ b/docs/remote_services/s3_cloudflare_r2/s3_cloudflare_r2_rs_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02f9c0fea36679e54c8c3fac979bc41d15ae4f7992b269bc2b67255059af1d2f +size 579889 diff --git a/docs/remote_services/s3_general/s3_cors_configure.md b/docs/remote_services/s3_general/s3_cors_configure.md new file mode 100644 index 0000000..b73fd4d --- /dev/null +++ b/docs/remote_services/s3_general/s3_cors_configure.md @@ -0,0 +1,33 @@ +# How To Configure S3 CORS Rules + +If you are using the latest version of this plugin AND Obsidian desktop >= 0.13.25, mobile >= 1.1.1, you do not need to configure it any more. If you are using Obsidian desktop < 0.13.25, moble < 1.1.1, you are required to configure the rules as following. + +Thanks to [@NAL100 in the Discussion](https://github.com/fyears/remotely-save/discussions/28). + +Please try this config: + +```json +[ + { + "AllowedHeaders": ["*"], + "AllowedMethods": ["GET", "PUT", "POST", "DELETE", "HEAD"], + "AllowedOrigins": [ + "app://obsidian.md", + "capacitor://localhost", + "http://localhost" + ], + "ExposeHeaders": [ + "Content-Length", + "Content-Type", + "Connection", + "Date", + "ETag", + "Server", + "x-amz-delete-marker", + "x-amz-id-2", + "x-amz-request-id", + "x-amz-version-id" + ] + } +] +``` diff --git a/docs/remote_services/s3_general/s3_user_policy.md b/docs/remote_services/s3_general/s3_user_policy.md new file mode 100644 index 0000000..02143b9 --- /dev/null +++ b/docs/remote_services/s3_general/s3_user_policy.md @@ -0,0 +1,79 @@ +# AWS S3 Bucket: How to configure user's policy + +## Attention + +Please read the doc carefully and adjust the optional fields accordingly. The doc is not fully tested and contributions are welcome. + +## AWS Official Docs + +- +- +- + +## Prerequisites + +Using the principle of least privilege is crucial for security when allowing a third party system to access your AWS resources. + +**Prerequisites**: Ensure you have an AWS account and administrative access to manage IAM policies. + +## Step 1: Create a new IAM Policy + +1. Log in to your AWS Management Console. +1. Navigate to the IAM Policies section. +1. Create a new policy with the following configuration. + +**Note**: `my-bucket` is a placeholder. For example, if your bucket's name is `obsidian-data`, the resource line should read `arn:aws:s3:::obsidian-data`. + +```JSON +{ + "Version": "2012-10-17", + "Statement": [ + { + "Sid": "ObsidianObjects", + "Effect": "Allow", + "Action": [ + "s3:HeadObject", + "s3:ListBucket", + "s3:PutObject", + "s3:CopyObject", + "s3:UploadPart", + "s3:UploadPartCopy", + "s3:ListMultipartUploads", + "s3:AbortMultipartUpload", + "s3:CompleteMultipartUpload", + "s3:ListObjects", + "s3:ListObjectsV2", + "s3:ListParts", + "s3:GetObject", + "s3:GetObjectAttributes", + "s3:DeleteObject", + "s3:DeleteObjects" + ], + "Resource": [ + "arn:aws:s3:::my-bucket", + "arn:aws:s3:::my-bucket/*" + ] + } + ] +} +``` + +> The policy allows the Obsidian plugin to list, add, retrieve, and delete objects in the specified S3 bucket. + +## Step 2: Attach the Policy to Obsidian user + +1. Create a new user in the IAM console. (Never use your own root user, as it would have full access to your AWS account). +1. When creating the user, select "Attach policy directly" and select the policy created. +1. Edit the recent created user and go to "Security Credentials" tab to create your access key. +1. Create an Access Key. If asked for a "use case", select "other" +1. Use the credentials in the plugin settings. (NEVER share these credentials) + +> PS. The bucket doesn't need to have a policy, only the user. + +## Verifying the Policy + +After attaching the policy, test it by trying to access the S3 bucket through the Obsidian plugin. Ensure that all intended actions can be performed without errors. + +## Troubleshooting + +If you encounter permission errors, check the policy for typos in the bucket name or actions. Ensure the policy is attached to the correct user. diff --git a/docs/remote_services/s3_minio/README.md b/docs/remote_services/s3_minio/README.md new file mode 100644 index 0000000..cdc72d7 --- /dev/null +++ b/docs/remote_services/s3_minio/README.md @@ -0,0 +1,27 @@ +# MinIO + +## Links + + + +## Steps + +1. Configure your minio instance and get an account. +2. Create an Access Key (during the creation, you will also get the auto-generated secret key). + ![](./minio_access_key.png) +3. Check or set the region. + ![](./minio_region.png) +4. Create a bucket. + ![](./minio_create_bucket.png) +5. In remotely-save setting page, input the address / bucket / access key / secret key. **Usually minio instances may need "S3 URL style"="Path Style".** Enable "Bypass CORS", because usually that's what you want. + ![](./minio_rs_settings.png) +6. Sync! + ![](./minio_sync_success.png) + +## Ports In Address + +Just type in the full address with `http(s)://` and `:port` in remotely-save settings, for example `http://192.168.31.198:9000`. + +It's verified that everything is ok. + +![](./minio_custom_port.png) diff --git a/docs/remote_services/s3_minio/minio_access_key.png b/docs/remote_services/s3_minio/minio_access_key.png new file mode 100644 index 0000000..5ecfdef --- /dev/null +++ b/docs/remote_services/s3_minio/minio_access_key.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c384ec32a43fcac1e63dbc81d4fbb7fbfaffcd67d0f5a66104482a39475ad639 +size 323050 diff --git a/docs/remote_services/s3_minio/minio_create_bucket.png b/docs/remote_services/s3_minio/minio_create_bucket.png new file mode 100644 index 0000000..461fae9 --- /dev/null +++ b/docs/remote_services/s3_minio/minio_create_bucket.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:894a6a10b7f41754936d77a935b4a5a4ae722796fa5680c9c6d0dd53c3cdc1a2 +size 269705 diff --git a/docs/remote_services/s3_minio/minio_custom_port.png b/docs/remote_services/s3_minio/minio_custom_port.png new file mode 100644 index 0000000..37b616d --- /dev/null +++ b/docs/remote_services/s3_minio/minio_custom_port.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b25c1209875ad781a29d3ac1cde5bd9c2137622e3919987c4d4e5f77dc8e3ec7 +size 68997 diff --git a/docs/remote_services/s3_minio/minio_region.png b/docs/remote_services/s3_minio/minio_region.png new file mode 100644 index 0000000..80575be --- /dev/null +++ b/docs/remote_services/s3_minio/minio_region.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bfcc526281d2c6a9f1261f489603f3dd768f8b94f099a1b26649fa37c9318575 +size 315030 diff --git a/docs/remote_services/s3_minio/minio_rs_settings.png b/docs/remote_services/s3_minio/minio_rs_settings.png new file mode 100644 index 0000000..2d9af46 --- /dev/null +++ b/docs/remote_services/s3_minio/minio_rs_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83615a6c2fbf7ff74679b480d29d222db8835bc067aee33a646e4b8d500c3aca +size 467575 diff --git a/docs/remote_services/s3_minio/minio_sync_success.png b/docs/remote_services/s3_minio/minio_sync_success.png new file mode 100644 index 0000000..2bd664e --- /dev/null +++ b/docs/remote_services/s3_minio/minio_sync_success.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd1bb9a6e285a1e63b545d714ca269eb714e980a5177392b967556f1f1754afe +size 147218 diff --git a/docs/remote_services/s3_storj_io/README.md b/docs/remote_services/s3_storj_io/README.md new file mode 100644 index 0000000..274871a --- /dev/null +++ b/docs/remote_services/s3_storj_io/README.md @@ -0,0 +1,17 @@ +# Storj + +## Links + + + +## Steps + +1. Register an account. Login. +2. Create a bucket. +3. Create S3 Credentials in Access Management. Allow all permissions for the bucket. Remember the access key and secret key and the end point. The end point is likely to be [`https://gateway.storjshare.io`](https://docs.storj.io/dcs/api/s3/s3-compatible-gateway). + ![](./storj_create_s3_cred_1.png) + ![](./storj_create_s3_cred_2.png) +4. Input your credentials into remotely-save settings. Region [should be `global`](https://docs.storj.io/dcs/api/s3/s3-compatibility). + ![](storj_remotely_save_settings.png) +5. Check connectivity. +6. Sync! diff --git a/docs/remote_services/s3_storj_io/storj_create_s3_cred_1.png b/docs/remote_services/s3_storj_io/storj_create_s3_cred_1.png new file mode 100644 index 0000000..f52de8e --- /dev/null +++ b/docs/remote_services/s3_storj_io/storj_create_s3_cred_1.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a3484f74f45fbdae6110de285edeff10c196c13160cb97f079da77843ae92e80 +size 386188 diff --git a/docs/remote_services/s3_storj_io/storj_create_s3_cred_2.png b/docs/remote_services/s3_storj_io/storj_create_s3_cred_2.png new file mode 100644 index 0000000..c395e11 --- /dev/null +++ b/docs/remote_services/s3_storj_io/storj_create_s3_cred_2.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:42593d98e1b154f4174a932088e72c25db9a976b5613f4d6ae21df1445b4559c +size 278219 diff --git a/docs/remote_services/s3_storj_io/storj_remotely_save_settings.png b/docs/remote_services/s3_storj_io/storj_remotely_save_settings.png new file mode 100644 index 0000000..d1f18b2 --- /dev/null +++ b/docs/remote_services/s3_storj_io/storj_remotely_save_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7843c31153054eb4762dbaf3027c1042b0d7cb4dd774bf584788cd7a97ba4044 +size 571188 diff --git a/docs/remote_services/s3_tencent_cloud_cos/README.md b/docs/remote_services/s3_tencent_cloud_cos/README.md new file mode 100644 index 0000000..82fc091 --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/README.md @@ -0,0 +1,23 @@ +# Tencent Cloud COS + +English | [中文](./README.zh-cn.md) + +## Link + +- international + +## 步骤 Steps + +The example shows the steps of China version. International version should be similar. + +1. Create a bucket with **private read-write permissions** and recommendly enable server-side-encryption. +2. In bucket list page, enter the bucket overview page of the bucket you just created. You should see the bucket name (your texts with the number of your account id), region, and access address. + ![](./cos_bucket_info.png) +3. In CAM page, create api key, and note down the SecretID and SecretKey. + ![](./cos_create_secret.png) +4. **Remove the bucket name from your access address to obtain your endpoint address! If your access address on the website is `https://.cos..myqcloud.com`, then the endpoint address you are going to use is `https://cos..myqcloud.com`.** +5. In remotely-save settings page, enter your endpoint adress, SecretID, SecretKey,and bucket name. + ![](./cos_setting.png) +6. Check Connectivity. + ![](./cos_connection.png) +7. Sync! diff --git a/docs/remote_services/s3_tencent_cloud_cos/README.zh-cn.md b/docs/remote_services/s3_tencent_cloud_cos/README.zh-cn.md new file mode 100644 index 0000000..a4d6257 --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/README.zh-cn.md @@ -0,0 +1,23 @@ +# 腾讯云 COS + +[English](./README.md) | 中文 + +## 链接 + +- 中国区 + +## 步骤 + +注意这里用中国区示例,国际区配置应该类似。 + +1. 在“存储桶列表”页,[“创建存储桶”](https://console.cloud.tencent.com/cos/bucket?action=create)。注意创建**私有读写**,建议打开服务端加密。 +2. 在桶列表页,点击刚刚存储的桶,进入概览页。可以见到桶名称(一般来说是之前指定的英文加账号数字),地域,访问域名。记录下来。 + ![](./cos_bucket_info.png) +3. 在[“访问管理页”](https://console.cloud.tencent.com/cam/capi) ,“API 密钥管理”,“创建密钥”,要记录 SecretID 和 SecretKey。 + ![](./cos_create_secret.png) +4. **把桶名称从访问域名移除,才是你即将输入的服务地址!假如你在腾讯云网站看到访问域名是 `https://.cos..myqcloud.com`,那么“服务地址”是 `https://cos..myqcloud.com`.** +5. 在 remotely-save 设置,输入服务地址,SecretID,SecretKey,和 桶名称。 + ![](./cos_setting.png) +6. 检查连接。 + ![](./cos_connection.png) +7. 可以同步了! diff --git a/docs/remote_services/s3_tencent_cloud_cos/cos_bucket_info.png b/docs/remote_services/s3_tencent_cloud_cos/cos_bucket_info.png new file mode 100644 index 0000000..86c7c9f --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/cos_bucket_info.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08b47288686c8ef8c6426776f7df52411efe431966e8f7f2f9a3a8836ee4d6c6 +size 323127 diff --git a/docs/remote_services/s3_tencent_cloud_cos/cos_connection.png b/docs/remote_services/s3_tencent_cloud_cos/cos_connection.png new file mode 100644 index 0000000..cd355d2 --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/cos_connection.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:94c0ec5db7a75c54c36224f067659682e271aea2e61b1ee50d046cd493ba72ac +size 58410 diff --git a/docs/remote_services/s3_tencent_cloud_cos/cos_create_secret.png b/docs/remote_services/s3_tencent_cloud_cos/cos_create_secret.png new file mode 100644 index 0000000..7c9028d --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/cos_create_secret.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f51f542756def80a77404e47f9793d1cd6917df5ab6e09d5a0ee1f7191855dc6 +size 425021 diff --git a/docs/remote_services/s3_tencent_cloud_cos/cos_setting.png b/docs/remote_services/s3_tencent_cloud_cos/cos_setting.png new file mode 100644 index 0000000..22f0745 --- /dev/null +++ b/docs/remote_services/s3_tencent_cloud_cos/cos_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:55064233031b44066fb7b77e17ba1eb1d0ae8398235a343cc7e137b07aaf7a39 +size 668954 diff --git a/docs/remote_services/s3_upyun/README.zh-cn.md b/docs/remote_services/s3_upyun/README.zh-cn.md new file mode 100644 index 0000000..095a24a --- /dev/null +++ b/docs/remote_services/s3_upyun/README.zh-cn.md @@ -0,0 +1,24 @@ +# 又拍云 + +## 链接 + +- 官网 +- 官网的 S3 文档 + +## 注意!!!!! + +又拍云似乎(?)文件都是默认公开的,强烈建议注意隐私问题,强烈建议设置插件加密。 + +## 步骤 + +1. 注册,新建对象存储。 +2. 参考官网文档 ,创建操作员然后创建 S3 访问凭证。 +3. 在 Remotely Save 设置以下: + - 服务地址(Endpoint):`s3.api.upyun.com` **一定是这个域名** + - 区域(Region):`us-east-1` + - Acccess Key ID:您获取到的访问凭证的 AccessKey + - Secret Access Key:您获取到的访问凭证的 SecretAccessKey + - 存储桶(Bucket)的名字:您创建的“服务名” + - 是否生成文件夹 Object:不生成(默认) **一定要选择不生成** +4. 可以在插件设置里,加上密码。 +5. 同步。 diff --git a/docs/remote_services/webdav_alist/README.md b/docs/remote_services/webdav_alist/README.md new file mode 100644 index 0000000..8154183 --- /dev/null +++ b/docs/remote_services/webdav_alist/README.md @@ -0,0 +1,23 @@ +# AList + +English | [中文](./README.zh-cn.md) + +## Links + +- English official website: and + +## Steps + +1. Install and run AList. Get the account and password. Login using the web page. +2. Add new storage. Pay attention to the mount path. The screenshot shows the mount path as `/alisttest davpath`. + ![](./alist_mount_path.zh.png) + ![](./alist_mount_path.en.png) +3. Construct the webdav address as: **http(s)://domain** + **port** + **`/dav`** + **mount path**, and the space inside the mount path should be replaced with `%20`: + ``` + http[s]://domain:port/dav/[mountpath url encoded] + http://127.0.0.1:5244/dav/alisttest%20davpath + ``` +4. In remotely-save setting page, select webdav type, then input the **full address with mount path**/account/password. + ![](./alist_rs_settings.en.png) +5. In remotely-save setting page, click "Check Connectivity". +6. Sync! diff --git a/docs/remote_services/webdav_alist/README.zh-cn.md b/docs/remote_services/webdav_alist/README.zh-cn.md new file mode 100644 index 0000000..bb8c549 --- /dev/null +++ b/docs/remote_services/webdav_alist/README.zh-cn.md @@ -0,0 +1,23 @@ +# AList + +[English](./README.md) | 中文 + +## 链接 + +- 中文官网: + +## 步骤 + +1. 安装和使用 AList。获取账号名和密码。在网页上登录。 +2. 新建挂载,检查挂载路径。如图所示是 `/alisttest davpath`。 + ![](./alist_mount_path.zh.png) + ![](./alist_mount_path.en.png) +3. 从而构建 webdav 网址如下,**http(s)://域名** + **端口** + **`/dav`** + **挂载路径**,其中挂载路径中假如有空格,换成 `%20`: + ``` + http[s]://domain:port/dav/[mountpath url encoded] + http://127.0.0.1:5244/dav/alisttest%20davpath + ``` +4. 在 remotely-save 设置,输入**带域名端口`/dav`和挂载路径的网址**、账号、密码。 + ![](./alist_rs_settings.en.png) +5. 在 remotely-save 设置,检查连接。 +6. 同步文件! diff --git a/docs/remote_services/webdav_alist/alist_mount_path.en.png b/docs/remote_services/webdav_alist/alist_mount_path.en.png new file mode 100644 index 0000000..0f2b89f --- /dev/null +++ b/docs/remote_services/webdav_alist/alist_mount_path.en.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ea05f67782b8f84518f13cf5587df719bdaa15f9e35b79f2801f91e695de6480 +size 46798 diff --git a/docs/remote_services/webdav_alist/alist_mount_path.zh.png b/docs/remote_services/webdav_alist/alist_mount_path.zh.png new file mode 100644 index 0000000..afcb55c --- /dev/null +++ b/docs/remote_services/webdav_alist/alist_mount_path.zh.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dcaaa5e64e894f0e9c8b0242b64bf4dd364c16d7314b09bc004f7ed0a0f1e0e5 +size 60853 diff --git a/docs/remote_services/webdav_alist/alist_rs_settings.en.png b/docs/remote_services/webdav_alist/alist_rs_settings.en.png new file mode 100644 index 0000000..0e5c4f8 --- /dev/null +++ b/docs/remote_services/webdav_alist/alist_rs_settings.en.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0b792320e2a4aaa16a17d82e80225ab35da05fd1f407fe067de72651beca088e +size 426289 diff --git a/docs/remote_services/webdav_apache/README.md b/docs/remote_services/webdav_apache/README.md new file mode 100644 index 0000000..1090cb5 --- /dev/null +++ b/docs/remote_services/webdav_apache/README.md @@ -0,0 +1,62 @@ +# Apache Webdav + +The following tutorial uses Apache with Docker in Linux, using the image [`bytemark/webdav`](https://hub.docker.com/r/bytemark/webdav/). + +You can also checkout the official doc from Bytemark [here](https://docs.bytemark.co.uk/article/run-your-own-webdav-server-with-docker/). The tutorial you are reading here is a little different from Bytemark's article. + +Assuming you want a webdav server on Linux satisfying that: + +1. Data is saved in your host machine's `./dav1` folder, +2. WebDAV is accessed by a user `user1` with password `password1`, +3. WebDAV is accessed on the host machine and port 8080: `127.0.0.1:8080`. +4. Use Basic auth type. + +Install docker nd set its permission properly. Then run this in terminal: + +```bash +mkdir ./dav1 # create the local folder + +docker container run --rm \ + -p 127.0.0.1:8080:80 \ + -v ./dav1:/var/lib/dav \ + -e USERNAME=user1 \ + -e PASSWORD=password1 \ + -e AUTH_TYPE=Basic \ + bytemark/webdav +``` + +We do not have `--detach` prameter here, so the server will be closed as soon as you terminate the program or close terminal. You might want to adjust the settings further by yourself. + +Then, in Remotely Save's setting, set these (auth type should always be `Basic`): + +``` +Server: http://127.0.0.1:8080 +User: user1 +Password: password1 +Auth Type: Basic +Depth Header Sent To Servers: only supports depth='1' +``` + +![Apache RS Setting](./apache_rs_settings.png) + +Then the server should be connected! You can sync now! + +In you host machine's file system, you should find some files and folders were added into the folder `./dav1`. + +``` +ls ./dav1/ +## data DavLock DavLock.dir DavLock.pag + +ls ./dav1/data/ +## +``` + +# Auth type + +You can change the auth type while calling Docker, between `Basic` and `Digest`. And please rememberto adjust the Remotely Save's settings. Both works. + +# Notice + +1. **Never expose your webdav server to public networks without protections or without strong passwords!** +2. I personally recommend using tailscale to build a LAN instead of expoing the service to public. +3. If you want to connect to the server from iOS (iPhone / iPad), the https is required. I personally recommend using caddy as the reverse proxy. Caddy can also be combined with tailscale. diff --git a/docs/remote_services/webdav_apache/apache_rs_settings.png b/docs/remote_services/webdav_apache/apache_rs_settings.png new file mode 100644 index 0000000..065ede2 --- /dev/null +++ b/docs/remote_services/webdav_apache/apache_rs_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:264e50d3adaeb4fc4e76b00273459d7d021f1b44af7f07b00a5c40741e65194d +size 658482 diff --git a/docs/remote_services/webdav_caddy/README.md b/docs/remote_services/webdav_caddy/README.md new file mode 100644 index 0000000..f42c443 --- /dev/null +++ b/docs/remote_services/webdav_caddy/README.md @@ -0,0 +1,33 @@ +# Caddy with `http.handlers.webdav` module + +> modified from the instruction from @cyruz-git in https://github.com/remotely-save/remotely-save/issues/825 + +## Link + + + +## Steps + +1. Download caddy with webdav module from . Or you can install Caddy then install the plugins. +2. Create a folder for storing webdav server files. Like `/usr/local/mywebdav`. +3. Create a `Caddyfile` (yeah the file name itself is `Caddyfile`.) like this: + ```caddy + :8080 { + route /dav/* { + root /usr/local/mywebdav + basicauth { + # Username "Bob", password "hiccup" + Bob $2a$14$Zkx19XLiW6VYouLHR5NmfOFU0z2GTNmpkT/5qqR7hx4IjWJPDhjvG + } + uri strip_prefix /dav + webdav + } + } + ``` + The password hash is generated like [this](https://caddyserver.com/docs/caddyfile/directives/basic_auth). +4. In Remotely Save, setup: + * address `http://localhost:8080/dav/` + * username `Bob` + * password `hiccup` + * auth type: `basic` +5. Check the connection and sync! diff --git a/docs/remote_services/webdav_dufs/README.md b/docs/remote_services/webdav_dufs/README.md new file mode 100644 index 0000000..cccf77f --- /dev/null +++ b/docs/remote_services/webdav_dufs/README.md @@ -0,0 +1,17 @@ +# dufs webdav + +## Link + + + +## Steps + +1. Download the latest version: and save it to a folder +2. Add the path environment variable. +3. Start the server in terminal, you can change the address (`127.0.0.1`) and username (`user1`) and password (`pass1`) accordingly: + ```bash + dufs -A --enable-cors --bind 127.0.0.1 --port 8080 --auth user1:pass1@/:rw + ``` +4. In remotely-save setting page, select webdav type, then input the address/account/**webdav password**(not your account password). +5. In remotely-save setting page, click "Check Connectivity". +6. Sync! diff --git a/docs/remote_services/webdav_general/webav_cors.md b/docs/remote_services/webdav_general/webav_cors.md new file mode 100644 index 0000000..e01dc12 --- /dev/null +++ b/docs/remote_services/webdav_general/webav_cors.md @@ -0,0 +1,10 @@ +If you are using Obsidian desktop >= 0.13.25 or iOS >= 1.1.1, you can skip this CORS part. + +If you are using Obsidian desktop < 0.13.25 or iOS < 1.1.1 or any Android version: + +- The webdav server has to be enabled CORS for requests from `app://obsidian.md` and `capacitor://localhost` and `http://localhost`, **AND** all webdav HTTP methods, **AND** all webdav headers. These are required, because Obsidian mobile works like a browser and mobile plugins are limited by CORS policies unless under a upgraded Obsidian version. +- Popular software NextCloud, OwnCloud, `rclone serve webdav` do **NOT** enable CORS by default. If you are using any of them, you should evaluate the risk, and find a way to enable CORS, before using this plugin, or use a upgraded Obsidian version. + - **Unofficial** workaround: NextCloud users can **evaluate the risk by themselves**, and if decide to accept the risk, they can install [WebAppPassword](https://apps.nextcloud.com/apps/webapppassword) app, and add `app://obsidian.md`, `capacitor://localhost`, `http://localhost` to `Allowed origins` + - **Unofficial** workaround: OwnCloud users can **evaluate the risk by themselves**, and if decide to accept the risk, they can download `.tar.gz` of `WebAppPassword` above and manually install and configure it on their instances. +- [Apache is also possible](./webdav_apache_cors.md). +- The plugin is tested successfully under python package [`wsgidav` (version 4.0)](https://github.com/mar10/wsgidav). See [this issue](https://github.com/mar10/wsgidav/issues/239) for some details. diff --git a/docs/remote_services/webdav_general/webdav_apache_cors.md b/docs/remote_services/webdav_general/webdav_apache_cors.md new file mode 100644 index 0000000..5bb1947 --- /dev/null +++ b/docs/remote_services/webdav_general/webdav_apache_cors.md @@ -0,0 +1,25 @@ +# How To Configure Apache CORS Rules for Webdav + +This method is [contributed by community](https://github.com/remotely-save/remotely-save/pull/31). + +You should evaluate the risk by yourself before trying this method. + +**You are strongly advised to backup your original Apache configuration files before making any changes.** + +```apacheconf + + # Obsidian webdav + SetEnvIf Origin "^app://obsidian.md$" IS_OBSIDIAN + SetEnvIf Origin "^capacitor://localhost$" IS_OBSIDIAN + SetEnvIf Origin "^http://localhost$" IS_OBSIDIAN + Header always set Access-Control-Allow-Origin "*" env=IS_OBSIDIAN + Header always set Access-Control-Allow-Methods "GET, HEAD, POST, PUT, OPTIONS, MOVE, DELETE, COPY, LOCK, UNLOCK, PROPFIND" env=IS_OBSIDIAN + Header always set Access-Control-Allow-Headers "Authorization, Depth, DNT, User-Agent, Keep-Alive, Content-Type, accept, origin, X-Requested-With" env=IS_OBSIDIAN + Header always set Access-Control-Expose-Headers "etag, dav" env=IS_OBSIDIAN + + # Allow OPTION request without authentication and respond with status 200 + RewriteCond %{ENV:IS_OBSIDIAN} 1 + RewriteCond %{REQUEST_METHOD} OPTIONS + RewriteRule ^(.*)$ $1 [R=200,L] + +``` diff --git a/docs/remote_services/webdav_infinicloud_teracloud/README.md b/docs/remote_services/webdav_infinicloud_teracloud/README.md new file mode 100644 index 0000000..231d945 --- /dev/null +++ b/docs/remote_services/webdav_infinicloud_teracloud/README.md @@ -0,0 +1,15 @@ +# InfiniCLOUD (formally TeraCLOUD) Webdav + +## Link + + + +## Steps + +1. Register an acount. +2. Go to , in section "Apps Connection", enable "Turn on Apps Connection". Here you get the address and account and webdav password (different from your account password): + ![](./infinicloud_account.png) +3. In remotely-save setting page, select webdav type, then input the address/account/**webdav password**(not your account password). + ![](./infinicloud_rs_setting.png) +4. In remotely-save setting page, click "Check Connectivity". +5. Sync! diff --git a/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_account.png b/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_account.png new file mode 100644 index 0000000..53c8b4d --- /dev/null +++ b/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_account.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0e16bc9d86b30ab907fc176087701474f4ca2b8d887f649302f2e184f69d0cbc +size 373845 diff --git a/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_rs_setting.png b/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_rs_setting.png new file mode 100644 index 0000000..b9da8f9 --- /dev/null +++ b/docs/remote_services/webdav_infinicloud_teracloud/infinicloud_rs_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8d707f6997df57b9c6b74d72bcfd567995d15062269761ee404e6596a266a241 +size 91012 diff --git a/docs/remote_services/webdav_jianguoyun/README.md b/docs/remote_services/webdav_jianguoyun/README.md new file mode 100644 index 0000000..3ede80f --- /dev/null +++ b/docs/remote_services/webdav_jianguoyun/README.md @@ -0,0 +1,22 @@ +# JianGuoYun/NutStore + +English | [中文](./README.zh-cn.md) + +## Link + + + +## Attentions!!! + +JianGuoYun/NutStore has api limits. The plugin may generate many queries, and it's possible to reach the api limits if there are many files, then do not work properly. It's not a bug and there's no way to fix this situation. + +## Steps + +1. **Be aware that JianGuoYun/NutStore has api limits, and the plugin may not work properly because of this.** +2. Register an account. +3. Go to "settings"->"Security", click "Add Application", then obtain the WebDAV account (email), and WebDAV password (a string different from web site password). + ![](./webdav_jianguoyun.cn.png) +4. Input the WebDAV address, account, password, **Depth Header Sent To Servers="only supports depth='1'"** in remotely-save settings. + ![](./webdav_jianguoyun_rs_settting.cn.png) +5. In remotely-save setting page, click "Check Connectivity". +6. Sync! diff --git a/docs/remote_services/webdav_jianguoyun/README.zh-cn.md b/docs/remote_services/webdav_jianguoyun/README.zh-cn.md new file mode 100644 index 0000000..937b644 --- /dev/null +++ b/docs/remote_services/webdav_jianguoyun/README.zh-cn.md @@ -0,0 +1,22 @@ +# 坚果云 + +[English](./README.md) | 中文 + +## 链接 + + + +## 注意!!! + +坚果云有限制 api 数量等设定。本插件会产生若干查询,如果文件较多很容易触发 api 上限,从而工作不正常。这不是插件 bug,也没有办法解决。 + +## 步骤 + +1. **知悉坚果云有 api 限制,本插件可能因此工作不正常。** +2. 注册账号,登录。 +3. 去“个人信息”->“安全”,“添加应用”,从而获取了 webDAV 账号(应该是 email)和 WebDAV 密码(一串特殊的字符,不等于网站密码)。 + ![](./webdav_jianguoyun.cn.png) +4. 在 remotely-save 设置,输入网址、账号、密码、**“发送到服务器的 Depth Header”设置为“只支持 depth='1'”**。 + ![](./webdav_jianguoyun_rs_settting.cn.png) +5. 在 remotely-save 设置,检查连接。 +6. 同步文件! diff --git a/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun.cn.png b/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun.cn.png new file mode 100644 index 0000000..995a396 --- /dev/null +++ b/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun.cn.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd5aaab1e1ac0ea8996517c819bf5ac0d2a828483037419ba289b538957f4752 +size 515913 diff --git a/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun_rs_settting.cn.png b/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun_rs_settting.cn.png new file mode 100644 index 0000000..2dcaaa1 --- /dev/null +++ b/docs/remote_services/webdav_jianguoyun/webdav_jianguoyun_rs_settting.cn.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:112778176af954e16c93c582d968e7c14e5b950d64facce8593855f5fb75fbcc +size 431527 diff --git a/docs/remote_services/webdav_nextcloud/README.md b/docs/remote_services/webdav_nextcloud/README.md new file mode 100644 index 0000000..f254028 --- /dev/null +++ b/docs/remote_services/webdav_nextcloud/README.md @@ -0,0 +1,13 @@ +# Nextcloud + +## Link + + + +## Steps + +1. Install, or find a hosted version. + * The docker version for internal network, and [Caddy as reverse proxy](https://caddyserver.com/docs/quick-starts/reverse-proxy) (for https), are personally recommended. + * If you find installing Nextcloud by yourselves is difficult, you can find some "Nextcloud's trusted, certified providers" on [Nextcloud Sign up page](https://nextcloud.com/sign-up/); For example, [The Good Cloud](https://thegood.cloud/) there generously provides 2 GB free stoarage space. + * Remotely Save is tested to be working with the docker version and The Good Cloud. +2. Go to Nextcloud's settings. Find the webdav url (something like `https://cloud.example.com/remote.php/dav/files/USERNAME`). Use this (without tailing slash), and your account and your password, in Remotely Save. diff --git a/docs/remote_services/webdav_nginx/README.md b/docs/remote_services/webdav_nginx/README.md new file mode 100644 index 0000000..ba19999 --- /dev/null +++ b/docs/remote_services/webdav_nginx/README.md @@ -0,0 +1,51 @@ +# Nginx Webdav + +The following tutorial uses Nginx with Docker in Linux, using the image [docker-nginx-webdav-nononsense](https://github.com/dgraziotin/docker-nginx-webdav-nononsense). It contains `ngx_http_dav_module` and `nginx-dav-ext-module`. + +The description is adapted from the pull request [`dgraziotin/docker-nginx-webdav-nononsense#123`](https://github.com/dgraziotin/docker-nginx-webdav-nononsense/pull/123) by Remotely Save author. + +Assuming you want a webdav server on Linux satisfying that: + +1. Data is saved in your host machine's `./dav1` folder, +2. WebDAV is accessed by a user `user1` with password `password1`, +3. WebDAV is accessed on the host machine and port 8080: `127.0.0.1:8080`. +4. (Only Basic auth type is supported.) + +Install docker nd set its permission properly. Then run this in terminal: + +```bash +mkdir ./dav1 # create the local folder + +docker container run --rm \ + -p 127.0.0.1:8080:80 \ + -v ./dav1:/data \ + -e WEBDAV_USERNAME=user1 \ + -e WEBDAV_PASSWORD=password1 \ + -e PUID=1000 \ + -e PGID=1000 \ + dgraziotin/nginx-webdav-nononsense +``` + +We do not have `--detach` prameter here, so the server will be closed as soon as you terminate the program or close terminal. You might want to adjust the settings further by yourself. + +Then, in Remotely Save's setting, set these (auth type should always be `Basic`): + +``` +Server: http://127.0.0.1:8080 +User: user1 +Password: password1 +Auth Type: Basic +Depth Header Sent To Servers: only supports depth='1' +``` + +![Nginx RS Setting](./nginx_rs_setting.png) + +Then the server should be connected! You can sync now! + +In you host machine's file system, you should find some files and folders were added into the folder `./dav1`. + +# Notice + +1. **Never expose your webdav server to public networks without protections or without strong passwords!** +2. I personally recommend using tailscale to build a LAN instead of expoing the service to public. +3. If you want to connect to the server from iOS (iPhone / iPad), the https is required. I personally recommend using caddy as the reverse proxy. Caddy can also be combined with tailscale. diff --git a/docs/remote_services/webdav_nginx/nginx_rs_setting.png b/docs/remote_services/webdav_nginx/nginx_rs_setting.png new file mode 100644 index 0000000..5282fe5 --- /dev/null +++ b/docs/remote_services/webdav_nginx/nginx_rs_setting.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92059e0819d25640a02d2494ba9da0bfa7a69003cdba36ced985bed9e29b0354 +size 254978 diff --git a/docs/remote_services/webdav_openmediavault/README.md b/docs/remote_services/webdav_openmediavault/README.md new file mode 100644 index 0000000..5e0bad2 --- /dev/null +++ b/docs/remote_services/webdav_openmediavault/README.md @@ -0,0 +1,21 @@ +# Open Media Vault WebDAV + +## Link +https://www.openmediavault.org + +## Steps + +1. In Open Media Vault's UI, install the WebDAV plugin: + * System → Plugins → openmediavault-webdav +2. Create a new Shared Folder + * Storage → Shared Folders → Create +3. Enable WebDAV server + * Services → WedDAV → Enable → Select previously created Shared Folder +4. Make sure Group is "webdav-users" +5. Add {your username} to "webdav-users" group + * Users → Groups → webdav-users → Edit → Members +6. Set folder permissions + * Storage → Shared Folders select the corresponding shared folder → Permissions → Set both {your username} and "webdav-users" group to "Read/Write" permissions +7. Back to Shared Folders, set specific Access Control List permissions + * Select the shared folder → Access Control List → set {your username}, "webdav-users" and "www-data" to "Read/Write" permissions +8. Install Remotely Save in Obsidian, select WebDAV, add the URL http://[your-server-ip]/webdav, use your user credentials and connect. diff --git a/docs/remote_services/webdav_owncloud/README.md b/docs/remote_services/webdav_owncloud/README.md new file mode 100644 index 0000000..009df0d --- /dev/null +++ b/docs/remote_services/webdav_owncloud/README.md @@ -0,0 +1,17 @@ +# ownCloud Webdav + +## Link + + + +# Steps + +1. Create an account. +2. Login. +3. In the Settings position, enable the "Show hidden files" and find out the WebDAV address. + ![](./owncloud_address.png) +4. Input the WebDAV address, account, password, **Depth Header Sent To Servers="only supports depth='1'"** in remotely-save settings. + ![](./owncloud_rs_settings.png) +5. In remotely-save setting page, click "Check Connectivity". +6. Sync! + ![](./owncloud_files.png) diff --git a/docs/remote_services/webdav_owncloud/owncloud_address.png b/docs/remote_services/webdav_owncloud/owncloud_address.png new file mode 100644 index 0000000..456ab39 --- /dev/null +++ b/docs/remote_services/webdav_owncloud/owncloud_address.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0d025b4ccf1be7fa2cc74e914e9a1a89a8d8f12a1b917bd4b26b975bf208294 +size 27796 diff --git a/docs/remote_services/webdav_owncloud/owncloud_files.png b/docs/remote_services/webdav_owncloud/owncloud_files.png new file mode 100644 index 0000000..dc136f9 --- /dev/null +++ b/docs/remote_services/webdav_owncloud/owncloud_files.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3d24d416016b2676a86166fcc35fa89f8befb7a872503b8da2be4374ef360ce +size 53503 diff --git a/docs/remote_services/webdav_owncloud/owncloud_rs_settings.png b/docs/remote_services/webdav_owncloud/owncloud_rs_settings.png new file mode 100644 index 0000000..c745930 --- /dev/null +++ b/docs/remote_services/webdav_owncloud/owncloud_rs_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2e8bf9a51e5d5bed66fb45fe085f37943bd90a51b33ef752d898c5b2caa71c2d +size 448797 diff --git a/docs/remote_services/webdav_synology_webdav_server/README.md b/docs/remote_services/webdav_synology_webdav_server/README.md new file mode 100644 index 0000000..8c154a8 --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/README.md @@ -0,0 +1,55 @@ +# Synology Webdav Server + +English | [中文](./README.zh-cn.md) + +## Link + + + +## Attention + +The tutorial author (the author of Remotely Save) is NOT an expert of NAS / Synology. Please read the doc carefully and change it to accommodate your needs by yourself. + +**It's dangerous to expose your NAS into public Internet if you don't know how to set up firewalls and other protections.** + +## Steps + +Synology DSM 7 is used in this tutorial. + +1. Create a new shared folder if you don't have one. For this tutorial, a new shared folder `share2` is created. You should assing a proper user to read / write the shared folder. + ![](./synology_create_shared_folder.png) + +2. Assuming you want to sync your vault into sub folder `哈哈哈/sub folder`, please create the sub folder(s) correspondingly inide the shared folder `share2`. + +3. Install webdav server from package center. + ![](./synology_install_webdav_server.png) + +4. Enter the webdav server settings. + +5. If you know how to configure https certificates correctly, you are strongly recommend to enable https. + + For the demonstration purpose, this tutorial enable http server for the later steps. + + Also "Enable DavDepthInfinity", which could speed up the plugin runnings greatly. + + "Apply". + + ![](./synology_webdav_server_settings.png) + +6. In Remotely Save settings, you should input your address as: + + `http(s)://://` + + For example, in the tutorial, the proper url should be: + + `http://:5000/share2/哈哈哈/sub folder` + + Username and password should be the user you configured before with read / write permissions to `share2`. + + Depth header should be "supports depth="infinity"". + + Check connectivity! + + ![](./synology_remotely_save_settings.png) + +7. Sync! diff --git a/docs/remote_services/webdav_synology_webdav_server/README.zh-cn.md b/docs/remote_services/webdav_synology_webdav_server/README.zh-cn.md new file mode 100644 index 0000000..24ac92f --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/README.zh-cn.md @@ -0,0 +1,56 @@ +# 群晖 Webdav Server + +[English](./README.md) | 中文 + +## 链接 + + + +## 注意 + +教程作者(Remotely Save 作者)**不是** NAS、群晖专家。请仔细阅读文档,并自行改动以适应您自身需求。 + +**没有设置防火墙和其他保护措施的话,将 NAS 暴露到公网上非常危险。** + +## 步骤 + +本教程有用到群晖 DSM 7。 + +1. 创建共享文件夹。本教程示例创建了 `share2`。你需要允许某个账号对此的读写权限。 + + ![](./synology_create_shared_folder.png) + +2. 假设之后你想同步你的库到子文件夹,`哈哈哈/sub folder`,请先在共享文件夹 `share2` 底下创建好。 + +3. 从套件中心安装 webdav server 。 + ![](./synology_install_webdav_server.png) + +4. 进入 webdav server 设置。 + +5. 如果你知道如何正确配置 https 证书的话,强烈建议开启 https。 + + 本教程简化示例,开启了 http。 + + 也设置“Enable DavDepthInfinity”,这可以加速插件连接速度。 + + “Apply”。 + + ![](./synology_webdav_server_settings.png) + +6. 在 Remotely Save 设置页,你的地址应如下格式输入: + + `http(s)://://` + + 比如说,本教程里,正确的地址类似于: + + `http://:5000/share2/哈哈哈/sub folder` + + 用户名和密码是你之前配置了允许读写 `share2` 的那个账号。 + + Depth 设置应为“supports depth="infinity"”。 + + 检查连接! + + ![](./synology_remotely_save_settings.png) + +7. 同步! diff --git a/docs/remote_services/webdav_synology_webdav_server/synology_create_shared_folder.png b/docs/remote_services/webdav_synology_webdav_server/synology_create_shared_folder.png new file mode 100644 index 0000000..715d09b --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/synology_create_shared_folder.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:71e61c25db793c00d4406ea0f46745941e16e268a41b7328b03a183ab0706ec8 +size 59700 diff --git a/docs/remote_services/webdav_synology_webdav_server/synology_install_webdav_server.png b/docs/remote_services/webdav_synology_webdav_server/synology_install_webdav_server.png new file mode 100644 index 0000000..9e3d770 --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/synology_install_webdav_server.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f0725099989b5496048c0e3cb284b4f6537d66866b4618c8e73ee758cb2b642 +size 174986 diff --git a/docs/remote_services/webdav_synology_webdav_server/synology_remotely_save_settings.png b/docs/remote_services/webdav_synology_webdav_server/synology_remotely_save_settings.png new file mode 100644 index 0000000..889eb2c --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/synology_remotely_save_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7bc6d03a9deb0e8babd2ed102e7b85ee1062797abf1146ae71f80237a852ff10 +size 522084 diff --git a/docs/remote_services/webdav_synology_webdav_server/synology_webdav_server_settings.png b/docs/remote_services/webdav_synology_webdav_server/synology_webdav_server_settings.png new file mode 100644 index 0000000..d571ed8 --- /dev/null +++ b/docs/remote_services/webdav_synology_webdav_server/synology_webdav_server_settings.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ba3781f24b5d65ee6915d496f7542c569116141cdced24ca505f4373d9efa9df +size 536163 diff --git a/docs/remote_services/webdav_thegoodcloud/README.md b/docs/remote_services/webdav_thegoodcloud/README.md new file mode 100644 index 0000000..a681451 --- /dev/null +++ b/docs/remote_services/webdav_thegoodcloud/README.md @@ -0,0 +1,9 @@ +# The Good Cloud + +## Link + + + +## Steps + +It's a hosted version of Nextcloud providing 2GB free spaces. See [NextCloud](../webdav_nextcloud/README.md) for more instructions. diff --git a/docs/remote_services/webdis/README.md b/docs/remote_services/webdis/README.md new file mode 100644 index 0000000..a443606 --- /dev/null +++ b/docs/remote_services/webdis/README.md @@ -0,0 +1,35 @@ +# Webdis + +## Links + +- Webdis: +- Redis®: + +## Explanation and Background + +I like the Redis® software very much, and would like to experiment using it as a "file storage". It seems to be nature by using path as the key and the content as the value (Sort of..., see below). + +However, Redis® works by using TCP connections, and browser js cannot establish raw TCP connections. We need a HTTP gateway, to provide the HTTP api. Wedis seems to be the most famous open source one. + +And of course, this method should work for Redis® alternatives: Valkey, Redict, KeyDB, Dragonfly, Garnet, ... + +## Disclaimer + +This app is NOT an official Redis® Ltd / Redis® OSS / Webdis product. Redis is a registered trademark of Redis Ltd. + +**Never expose your Redis® or Webdis to public without security protection!!!** You are response for protecting your server. + +## Usage + +1. Install Redis®. +2. Install Webdis. +3. In `webdis.json`, configure the ACL for using password and username, and / or ip filters. **Never expose your Redis® or Webdis to public without security protection!!!**. +4. Install and configure reverse proxy, firewall, https, etc. (You have to configure HTTPS correctly if you want to use it on iOS) +5. In Remotely Save settings, enter your server address, username, password, and adjust the "base dir". Check connection. +6. Sync! +7. Serveral keys and values will be generated in your Redis® database: + + ``` + rs:fs:v1:${encodeURIComponent(vaultName+'/'+folderStructure+'/'+fileName)}:meta # you can HGETALL it + rs:fs:v1:${encodeURIComponent(vaultName+'/'+folderStructure+'/'+fileName)}:content # you can GET it + ``` diff --git a/docs/remote_services/yandexdisk/README.md b/docs/remote_services/yandexdisk/README.md new file mode 100644 index 0000000..f5fa648 --- /dev/null +++ b/docs/remote_services/yandexdisk/README.md @@ -0,0 +1,44 @@ +# Yandex Disk (PRO) + +# Links + + + +# Intro + +* It's a PRO feature of Remotely Save plugin. +* **This plugin is NOT an official Yandex Disk or Yandex product, and just uses Yandex Disk's public api.** + +# Steps + +## Steps of Remotely Save subscription + +1. Please sign up and sign in an online account, connect your plugin to your online account firstly. See [the PRO tutorial](../../pro/README.md) firstly. +2. Subscribe to "sync with Yandex Disk" feature online. +3. Go back to your Remotely Save plugin inside Obsidian, click "Check again" button in PRO settings. So that the plugin knows some features are enabled. In this case, sync with Yandex Disk should be detected. + +## Steps of Connecting to your Yandex Disk + +After you enabled the PRO feature in your Remotely Save plugin, you can connect to your Yandex Disk account now. + +1. In Remotely Save settings, change your sync service to Yandex Disk. +2. Click Auth, visit the link, go to Yandex Disk website to start. +3. Follow the instruction on Yandex Disk, and allow Remotely Save to connect. +4. You will be redirected back to Remotely Save plugin. +5. A notice will tell you that you've connected or not. +6. Sync! The plugin will create a vault folder in the root of your Yandex Disk and upload notes into that folder. +7. **Read the caveats below.** + +# The caveats + +* As of June 2024, this feature is in beta stage. **Back up your vault before using this feature.** + +# Why not use webdav? + +1. The Remotely Save PRO feture "sync with Yandex Disk" is developed using Yandex Disk's native API, instead of webdav interface. It brings benefits such that the last modified time can be preserved. +2. Some users prefer oauth2 method to authorize themselves. + +# Bonus: where to register the app + +* or +* redirect uri can be set to or . diff --git a/docs/services_connectable_or_not.md b/docs/services_connectable_or_not.md new file mode 100644 index 0000000..4c1f93c --- /dev/null +++ b/docs/services_connectable_or_not.md @@ -0,0 +1,47 @@ +# services connectability + +Here is an overview of the connectability ("connectable" or "not connectable" or "in the plan" or "never") to some services by this plugin. + +The plugin works under the browser environment in Obsidian, so CORS is an issue. Obsidian starts to provide a rich API `requestUrl` for desktop version >= 0.13.25, mobile >= 1.1.1 to bypass the CORS issue. But if the users are still using an older version of Obsidian, they need to configure CORS on server. + +The list is for information purposes only. + + +| Service | Connectable | by S3 | by WebDAV | by other protocol | +| ------ | ------ | ------ | ------ | ------ | +| Amazon S3 | Yes | Yes | | | +| Tencent Cloud - Cloud Object Storage (COS) 腾讯云对象存储 | Yes | Yes | | | +| Alibaba Cloud - Object Storage Service 阿里云对象存储 | Yes | Yes | | | +| Backblaze B2 Cloud Storage | Yes | Yes | | | +| [Wasabi](https://wasabi.com) | ? | ? | | | +| [filebase](https://filebase.com/) | Yes | Yes | | | +| QingStor 青云 | ? | ? | | | +| [MinIO](https://min.io/) | Yes | Yes | | | +| Azure Blob Storage | Yes (PRO) | | | Yes (PRO) | +| [WsgiDAV](https://github.com/mar10/wsgidav) | Yes | | Yes | | +| [Nginx `ngx_http_dav_module`](http://nginx.org/en/docs/http/ngx_http_dav_module.html) | Yes | | Yes | | +| NextCloud | Yes | | Yes | | +| OwnCloud | Yes? | | Yes? | | +| Seafile | Yes | | Yes | | +| `rclone serve webdav` | Yes | | Yes | | +| [Nutstore 坚果云](https://www.jianguoyun.com/) | Yes (partially) | | Yes (partially) | | +| [TeraCLOUD](https://teracloud.jp/en/) | Yes | | Yes | | +| Seafile | ? | | ? | ? | +| Dropbox | Yes | | | Yes | +| OneDrive for personal | Yes | | | Yes | +| OneDrive for Business | Yes | | | ? | +| Google Drive | Yes (with limitations) (PRO) | | | Yes (with limitations) (PRO) | +| [Box](https://www.box.com/) | Yes (PRO) | | | Yes (PRO) | +| [pCloud](https://www.pcloud.com/) | Yes (PRO) | | Yes | Yes (PRO) | +| Google Cloud Storage | ? | | | May be possible but needs further development. | +| Microsoft Azure Blob Storage | ? | | | May be possible but needs further development. | +| [OpenStack Storage (Swift)](https://github.com/openstack/swift) | ? | | | May be possible but needs further development. | +| | ? | | | ? | +| Yandex Disk | Yes (PRO) | | Yes | Yes (PRO) | +| FTP / FTPS | Never | | | Technically never possible to be implemented. | +| SFTP | Never | | | Technically never possible to be implemented. | +| Jottacloud | No | | | No. It seems that no open api is available. | +| Mega | Never | | | No. No js api is available. | +| Git | Never | | | No. Technically very hard, if not impossible, to be implemented. | +| [Koofr](https://koofr.eu/) | ? | | ? | ? | +| [Blomp](https://www.blomp.com/) | ? | | | May be possible but needs further development. | diff --git a/docs/sync-algorithm.md b/docs/sync-algorithm.md new file mode 100644 index 0000000..1fa7197 --- /dev/null +++ b/docs/sync-algorithm.md @@ -0,0 +1,78 @@ +# BYOC Sync Algorithm Specification + +**Version:** 1.0.0 (Clean-Room Implementation) +**Architecture:** 3-Way Merge (Local vs. Remote, anchored by Previous State) + +## Core Philosophy +The Bring Your Own Cloud (BYOC) synchronization engine operates on an event-driven, 3-way merge model. It replaces the proprietary `remotely-save` module. + +The engine must determine the correct action (Push, Pull, Delete, Skip, Conflict Resolution) for every file by comparing current file metadata against a local IndexedDB cache (`prevSyncRecords`), which represents the exact state of the file at the conclusion of the *last successful sync*. + +This guarantees safe, bidirectional true-sync semantics without blindly overwriting files based on remote timestamps. + +--- + +## The Synchronization Phases + +The synchronization pipeline executes linearly through the following phases. + +### Phase 1: Fetch State & Discovery (`fsLocal.walk()` + `fsRemote.walk()`) +The `syncer` orchestrator invokes a deep tree walk on both `fsLocal` and `fsRemote`. +- Retrieves arrays of `Entity` objects containing `{ key, mtime, size, isDir }` for all local and remote files. +- Deserializes the previous successful baseline state from `InternalDBs.prevSyncRecordsTbl`. + +### Phase 2: Matrix Assembly & Planning (`planner.ts`) +The `planner` constructs a master hash map resolving every unique file path (`key`) to a `DiffNode`: +```typescript +interface DiffNode { + key: string; + local?: Entity; // Current Local State + remote?: Entity; // Current Remote State + baseline?: Entity; // Last known synced state +} +``` + +The Planner loops through the array of `DiffNode` and assigns an initial sync intention based strictly on state truth tables: + +#### 1. Additions +- **Local Exists, Remote Missing, Baseline Missing:** `Push` (Upload new local file). +- **Remote Exists, Local Missing, Baseline Missing:** `Pull` (Download new remote file). + +#### 2. Modifications (Mutations) +- **Local `mtime` > Baseline, Remote == Baseline:** `Push` (Local edited, Remote untouched). +- **Remote `mtime` > Baseline, Local == Baseline:** `Pull` (Remote edited, Local untouched). +- **Local `mtime` > Baseline AND Remote `mtime` > Baseline:** `Conflict` (Both edited). + +#### 3. Deletions (Tombstones) +- **Local Missing, Baseline Exists, Remote == Baseline:** `Delete Remote` (File was deleted locally; propagate deletion). +- **Remote Missing, Baseline Exists, Local == Baseline:** `Delete Local` (File was deleted remotely; propagate deletion). + +### Phase 3: Conflict Resolution (`conflict.ts`) +If a `DiffNode` is flagged as `Conflict`, the engine consults user preferences (`settings.conflictAction`): +- **keep_newer**: Evaluates `Math.max(local.mtime, remote.mtime)`. The winner drives the state (Push/Pull). +- **keep_local**: Forces a `Push`. +- **keep_remote**: Forces a `Pull`. +- **keep_both**: Appends a suffix (e.g., `.conflict.md`) to the local file, triggering an upload on the next pass without destroying data. + +### Phase 4: Execution Engine (`syncer.ts`) +The execution engine groups the resolved `ActionPlan` into queues (Deletions first, Additions/Modifications next). +It implements bounded concurrency using `promise.all` limits based on the user's `settings.concurrency` configuration. + +**Operations:** +- **Push:** Maps to `fsRemote.writeFile(fsLocal.readFile())`. +- **Pull:** Maps to `fsLocal.writeFile(fsRemote.readFile())`. +- **Delete Local:** Maps to `fsLocal.rm()`. +- **Delete Remote:** Maps to `fsRemote.rm()`. + +### Phase 5: Committing the Baseline State +Crucially, when the Execution phase finishes, **only files that successfully synced** correctly update their footprint in the `prevSyncRecordsTbl`. +1. The old cache is purged or updated iteratively. +2. The exact Remote `mtime` and Local `mtime` are stored. +3. If the network drops at 99%, the next sync resumes perfectly because the un-pushed files failed to register a new baseline. + +--- + +## Defensive Checks and Safety Constraints +- **Empty File Protection:** If an `Entity` reports a byte size of `0`, the planner flags it. Depending on `settings.emptyFile`, it will either `skip` (standard for syncing configurations) or attempt to `pull/push`. +- **Size Limits:** Honors `settings.skipSizeLargerThan`. Files exceeding this byte boundary are perpetually excluded from the matrix assembly. +- **Dry Run Compatibility:** The planner strictly isolates the *decision state* from the *execution state*, allowing for accurate Dry Run outputs without executing writes. diff --git a/docs/sync_algorithm/README.md b/docs/sync_algorithm/README.md new file mode 100644 index 0000000..cd84fa4 --- /dev/null +++ b/docs/sync_algorithm/README.md @@ -0,0 +1,7 @@ +# Sync Algorithm + +- [v1](./v1/README.md) +- [v2](./v2/README.md) +- v3 + - [intro doc for end users](./v3/intro.md) + - [design doc](./v3/design.md) diff --git a/docs/sync_algorithm/sync_ignoring_large_files.md b/docs/sync_algorithm/sync_ignoring_large_files.md new file mode 100644 index 0000000..c78d7d8 --- /dev/null +++ b/docs/sync_algorithm/sync_ignoring_large_files.md @@ -0,0 +1,17 @@ +# Sync Ignoring Large Files + +Initially, the plugin does not ignore large files. + +From the new version in May 2022, it can ignore all files with some sizes. But we need some rules to make the function compatible with existing conditions. + +1. If users are using E2E password mode, then the file sizes are compared on the **encrypted sizes**, rather than the original unencripted file sizes. The reasons are: the encrypted ones are in transferations, and the encrypted sizes can be computed from unencrypted sizes but not the reverse. + +2. Assuming the file A, is already synced between local device and remote service before. + + - If the local size and remote size are both below the threshold, then the file can be synced normally. + - If the local size and remote size are both above the threshold, then the file will be ignored normally. + - If the local size is below the threshold, and the remote size is above the threshold, then the plugin **rejects** the sync, and throws the error to the user. + - If the local size is above the threshold, and the remote size is below the threshold, then the plugin **rejects** the sync, and throws the error to the user. + - When it somes to deletions, the same rules apply. + + The main point is that, if the file sizes "cross the line", the plugin does not introduce any further trouble and just reject to work for this file. diff --git a/docs/sync_algorithm/v1/README.md b/docs/sync_algorithm/v1/README.md new file mode 100644 index 0000000..2d6b89d --- /dev/null +++ b/docs/sync_algorithm/v1/README.md @@ -0,0 +1,29 @@ +# Sync Algorithm + +## Sources + +We have three record sources: + +1. Local files. By scanning all files in the vault locally. Actually Obsidian provides an api directly returning this. +2. Remote files. By scanning all files on the remote service. Some services provide an api directly returning this, and some other services require the plugin scanning the folders recursively. +3. Local "delete-or-rename" history. It's recorded by using Obsidian's tracking api. So if users delete or rename files/folders outside Obsidian, we could do nothing. + +Assuming all sources are reliable. + +## Deal with them + +We list all combinations mutually exclusive and collectively exhaustive. + +| ID | Remote Files | Local files | Local delete rename history | Extra | Decision | +| --- | ------------ | ----------- | --------------------------- | ----------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| 1 | exist | exist | ignore | mtime_remote > mtime_local | download remote file, create local folder if not exists, clear local history if exists | +| 2 | exist | exist | ignore | mtime_remote < mtime_local | upload local file, create remote folder if not exists, clear local history if exists | +| 3 | exist | exist | ignore | mtime_remote === mtime_local && password === "" && size_remote === size_local | clear local history if exists (the file was synced and no changes after last sync) | +| 4 | exist | exist | ignore | mtime_remote === mtime_local && password === "" && size_remote !== size_local | upload local file, clear local history if exists (we always prefer local to remote) | +| 5 | exist | exist | ignore | mtime_remote === mtime_local && password !== "" | clear local history if exists (in encryption mode, file sizes are unequal. we can only rely on mtime(s)) | +| 6 | exist | exist | ignore | If local is a folder. mtime_local === undefined | clear local history if exists. TODO: what if a folder and a previous file share the same name? | +| 7 | exist | not exist | exist | mtime_remote >= delete_time_local | download remote file, create folder if not exists | +| 8 | exist | not exist | exist | mtime_remote < delete_time_local | delete remote file, clear local history | +| 9 | exist | not exist | not exist | | download remote file, create folder if not exists | +| 10 | not exist | exist | ignore | local may be folder or file | upload local files recursively, create remote folder if not exists, clear local history if exists | +| 11 | not exist | not exist | ignore | | clear local history if exists | diff --git a/docs/sync_algorithm/v2/README.md b/docs/sync_algorithm/v2/README.md new file mode 100644 index 0000000..a8acbc9 --- /dev/null +++ b/docs/sync_algorithm/v2/README.md @@ -0,0 +1,63 @@ +# Sync Algorithm V2 + +## Sources + +We have 4 record sources: + +1. Local files. By scanning all files in the vault locally. Actually Obsidian provides an api directly returning this. +2. Remote files. By scanning all files on the remote service. Some services provide an api directly returning this, and some other services require the plugin scanning the folders recursively. +3. Local "delete-or-rename" history. It's recorded by using Obsidian's tracking api. So if users delete or rename files/folders outside Obsidian, we could do nothing. +4. Remote "delete" history. It's uploaded by the plugin in each sync. + +Assuming all sources are reliable. + +## Deal with them + +We list all combinations mutually exclusive and collectively exhaustive. + +### Files + +In short, we collect four timestamps, and respect the max timestamp and its corresponding operation. + +| t1 | t2 | t3 | t4 | local file to do | remote file to do | local del history to do | remote del history to do | equal to sync v2 branch | +| -------------- | -------------- | -------------- | -------------- | ---------------- | ----------------- | ----------------------- | ------------------------ | ----------------------- | +| mtime_remote | mtime_local | deltime_remote | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | | +| mtime_local | mtime_remote | deltime_remote | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | | +| mtime_remote | deltime_remote | mtime_local | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | | +| deltime_remote | mtime_remote | mtime_local | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | | +| mtime_local | deltime_remote | mtime_remote | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | | +| deltime_remote | mtime_local | mtime_remote | deltime_local | del_if_exists | del_if_exists | clean | upload_local_del_history | 8 | +| mtime_remote | mtime_local | deltime_local | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| mtime_local | mtime_remote | deltime_local | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| mtime_remote | deltime_local | mtime_local | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| deltime_local | mtime_remote | mtime_local | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| mtime_local | deltime_local | mtime_remote | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| deltime_local | mtime_local | mtime_remote | deltime_remote | del_if_exists | del_if_exists | clean | keep | | +| mtime_remote | deltime_remote | deltime_local | mtime_local | skip | upload_local | clean | clean | | +| deltime_remote | mtime_remote | deltime_local | mtime_local | skip | upload_local | clean | clean | 10 | +| mtime_remote | deltime_local | deltime_remote | mtime_local | skip | upload_local | clean | clean | | +| deltime_local | mtime_remote | deltime_remote | mtime_local | skip | upload_local | clean | clean | | +| deltime_remote | deltime_local | mtime_remote | mtime_local | skip | upload_local | clean | clean | 2;3;4;5;6 | +| deltime_local | deltime_remote | mtime_remote | mtime_local | skip | upload_local | clean | clean | | +| mtime_local | deltime_remote | deltime_local | mtime_remote | download_remote | skip | clean | clean | | +| deltime_remote | mtime_local | deltime_local | mtime_remote | download_remote | skip | clean | clean | 7;9 | +| mtime_local | deltime_local | deltime_remote | mtime_remote | download_remote | skip | clean | clean | | +| deltime_local | mtime_local | deltime_remote | mtime_remote | download_remote | skip | clean | clean | | +| deltime_remote | deltime_local | mtime_local | mtime_remote | download_remote | skip | clean | clean | 1;9 | +| deltime_local | deltime_remote | mtime_local | mtime_remote | download_remote | skip | clean | clean | | + +### Folders + +We actually do not use any folders' metadata. Thus the only relevent info is their names, while the mtime is actually ignorable. + +1. Firstly generate all the files' plan. If any files exist, then it's parent folders all should exist. If the should-exist folder doesn't exist locally, the local should create it recursively. If the should-exist folder doesn't exist remotely, the remote should create it recursively. +2. Then, a folder is deletable, if and only if all the following conditions meet: + + - it shows up in the remote deletion history + - it's empty, or all its sub-folders are deletable + + Some examples: + + - A user deletes the folder in device 1, then syncs from the device 1, then creates the same-name folder in device 2, then syncs from the device 2. The folder is deleted (again), on device 2. + - A user deletes the folder in device 1, then syncs from the device 1, then creates the same-name folder in device 2, **then create a new file inside it,** then syncs from the device 2. The folder is **kept** instead of deleted because of the new file, on device 2. + - A user deletes the folder in device 1, then syncs from the device 1, then do not touch the same-name folder in device 2, then syncs from the device 2. The folder and its untouched sub-files should be deleted on device 2. diff --git a/docs/sync_algorithm/v3/README.md b/docs/sync_algorithm/v3/README.md new file mode 100644 index 0000000..1f66f0f --- /dev/null +++ b/docs/sync_algorithm/v3/README.md @@ -0,0 +1,4 @@ +# Sync Algorithm V3 + +- [intro doc for end users](./intro.md) +- [design doc](./design.md) diff --git a/docs/sync_algorithm/v3/design.md b/docs/sync_algorithm/v3/design.md new file mode 100644 index 0000000..8b8c0c7 --- /dev/null +++ b/docs/sync_algorithm/v3/design.md @@ -0,0 +1,89 @@ +# Sync Algorithm V3 + +Drafted on 20240117. + +An absolutely better sync algorithm. Better for tracking deletions and better for subbranching. + +## Huge Thanks + +Basically a combination of algorithm v2 + [synclone](https://github.com/Jwink3101/syncrclone/blob/master/docs/algorithm.md) + [rsinc](https://github.com/ConorWilliams/rsinc) + (some of rclone [bisync](https://rclone.org/bisync/)). All of the later three are released under MIT License so no worries about the licenses. + +## Features + +Must have + +1. true deletion detection +2. deletion protection (blocking) with a setting +3. transaction from the old algorithm +4. user warning show up, **new algorithm needs all clients to be updated!** (deliberately corrput the metadata file??) +5. filters +6. conflict warning +7. partial sync + +Nice to have + +1. true time and hash +2. conflict rename + +## Description + +We have _five_ input sources: + +1. local all files +2. remote all files +3. _local previous succeeded sync history_ +4. local deletions +5. remote deletions. + +Init run, consuming remote deletions : + +Change history data into _local previous succeeded sync history_. + +Later runs, use the first, second, third sources **only**. + +Bidirectional table is modified based on synclone and rsinc. Incremental push / pull only tables is further modified based on the bidirectional table. The number inside the table cell is the decision branch in the code. + +Bidirectional: + +| local\remote | remote unchanged | remote modified | remote deleted | remote created | +| --------------- | ------------------ | ------------------------- | ------------------ | ------------------------- | +| local unchanged | (02/21) do nothing | (09) pull | (07) delete local | (??) conflict | +| local modified | (10) push | (16/17/18/19/20) conflict | (08) push | (??) conflict | +| local deleted | (04) delete remote | (05) pull | (01) clean history | (03) pull | +| local created | (??) conflict | (??) conflict | (06) push | (11/12/13/14/15) conflict | + +Incremental push: + +| local\remote | remote unchanged | remote modified | remote deleted | remote created | +| --------------- | ---------------------------- | ---------------------------- | ---------------------- | ---------------------------- | +| local unchanged | (02/21) do nothing | **(26) conflict push** | **(32) conflict push** | (??) conflict | +| local modified | (10) push | **(25) conflict push** | (08) push | (??) conflict | +| local deleted | **(29) conflict do nothing** | **(30) conflict do nothing** | (01) clean history | **(28) conflict do nothing** | +| local created | (??) conflict | (??) conflict | (06) push | **(23) conflict push** | + +Incremental pull: + +| local\remote | remote unchanged | remote modified | remote deleted | remote created | +| --------------- | ---------------------- | ---------------------- | ---------------------------- | ---------------------- | +| local unchanged | (02/21) do nothing | (09) pull | **(33) conflict do nothing** | (??) conflict | +| local modified | **(27) conflict pull** | **(24) conflict pull** | **(34) conflict do nothing** | (??) conflict | +| local deleted | **(35) conflict pull** | (05) pull | (01) clean history | (03) pull | +| local created | (??) conflict | (??) conflict | **(31) conflict do nothing** | **(22) conflict pull** | + +Incremental push and delete (diff decisionBranch: 38): + +| local\remote | remote unchanged | remote modified | remote deleted | remote created | +| --------------- | ---------------------------- | ---------------------------- | ---------------------- | ---------------------------- | +| local unchanged | (02/21) do nothing | **(26) conflict push** | **(32) conflict push** | (??) conflict | +| local modified | (10) push | **(25) conflict push** | (08) push | (??) conflict | +| local deleted | **(38) delete remote** | **(30) conflict do nothing** | (01) clean history | **(28) conflict do nothing** | +| local created | (??) conflict | (??) conflict | (06) push | **(23) conflict push** | + +Incremental pull and delete (diff decisionBranch: 39): + +| local\remote | remote unchanged | remote modified | remote deleted | remote created | +| --------------- | ---------------------- | ---------------------- | ---------------------------- | ---------------------- | +| local unchanged | (02/21) do nothing | (09) pull | **(39) delete local** | (??) conflict | +| local modified | **(27) conflict pull** | **(24) conflict pull** | **(34) conflict do nothing** | (??) conflict | +| local deleted | **(35) conflict pull** | (05) pull | (01) clean history | (03) pull | +| local created | (??) conflict | (??) conflict | **(31) conflict do nothing** | **(22) conflict pull** | diff --git a/docs/sync_algorithm/v3/intro.md b/docs/sync_algorithm/v3/intro.md new file mode 100644 index 0000000..d1e2caf --- /dev/null +++ b/docs/sync_algorithm/v3/intro.md @@ -0,0 +1,14 @@ +# Introduction To Sync Algorithm V3 + +- [x] sync conflict: keep newer +- [x] sync conflict: keep larger +- [ ] sync conflict: keep both and rename +- [ ] sync conflict: show warning +- [x] deletion: true deletion status computation +- [x] meta data: no remote meta data any more +- [x] migration: old data auto transfer to new db (hopefully) +- [x] sync direction: incremental push only +- [x] sync direction: incremental pull only +- [x] sync protection: warning based on the threshold +- [ ] partial sync: better sync on save +- [x] encrpytion: new encryption method, see [this](../../encryption/) diff --git a/downloaded_azure.svg b/downloaded_azure.svg new file mode 100644 index 0000000..1413b29 --- /dev/null +++ b/downloaded_azure.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:83bf7ff9575ba0ca4eacf2a4a386a6779b044b64b553f0aa9ea66bf1efb40102 +size 5788 diff --git a/downloaded_box.svg b/downloaded_box.svg new file mode 100644 index 0000000..b712a49 --- /dev/null +++ b/downloaded_box.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ebb8ee223a2245ab9a90f0f0951d726b910a6563cb6677287e248cdd20bd4262 +size 2254 diff --git a/downloaded_dropbox.svg b/downloaded_dropbox.svg new file mode 100644 index 0000000..02878ae --- /dev/null +++ b/downloaded_dropbox.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a5b08064d2b52f0e8818fcd3aa7287fc284a754271eed6de8a01f7ed147335f5 +size 444 diff --git a/downloaded_gdrive.svg b/downloaded_gdrive.svg new file mode 100644 index 0000000..afe07bd --- /dev/null +++ b/downloaded_gdrive.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3dc0485d6944a66e31f6db053c1fd11ba9456d9517b9f2db7d7b17cb71be31e1 +size 346 diff --git a/downloaded_koofr.svg b/downloaded_koofr.svg new file mode 100644 index 0000000..85314a3 --- /dev/null +++ b/downloaded_koofr.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2b71238e244bf0ac04deaf88b82965fbd95b38f0f4278549130a9eb5a28ed905 +size 9066 diff --git a/downloaded_onedrive.svg b/downloaded_onedrive.svg new file mode 100644 index 0000000..e6c8a09 --- /dev/null +++ b/downloaded_onedrive.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:92fb38671ba53e550bf6f258d15538552a83e7d3dce9b0b9a4a35d005caf43da +size 1119 diff --git a/downloaded_pcloud.svg b/downloaded_pcloud.svg new file mode 100644 index 0000000..d7c74fa --- /dev/null +++ b/downloaded_pcloud.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:132fb7c75ea9efc21a4daaaed789c49da753a26729dc34d0593ff36b7bf9a9d5 +size 4359 diff --git a/downloaded_s3.svg b/downloaded_s3.svg new file mode 100644 index 0000000..b52b34f --- /dev/null +++ b/downloaded_s3.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:032d97216f0a076794321bb5a498af45e7c6be8b9180573e2d4e603e6eb7aa1d +size 4775 diff --git a/downloaded_yandex.svg b/downloaded_yandex.svg new file mode 100644 index 0000000..c552637 --- /dev/null +++ b/downloaded_yandex.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ad6d0b7ad5b678b1764650c8062fb88cabd21948b5135080a64e8a674272871f +size 14401 diff --git a/esbuild.config.mjs b/esbuild.config.mjs new file mode 100644 index 0000000..8796b80 --- /dev/null +++ b/esbuild.config.mjs @@ -0,0 +1,106 @@ +import "dotenv/config"; +import esbuild from "esbuild"; +import inlineWorkerPlugin from "esbuild-plugin-inline-worker"; +import process from "process"; +// import builtins from 'builtin-modules' + +const banner = `/* +THIS IS A GENERATED/BUNDLED FILE BY ESBUILD +if you want to view the source, please visit the github repository of this plugin +*/ +`; + +console.log(`esbuild version = ${esbuild.version}`); + +const prod = process.argv[2] === "production"; + +const DEFAULT_DROPBOX_APP_KEY = process.env.DROPBOX_APP_KEY || ""; +const DEFAULT_ONEDRIVE_CLIENT_ID = process.env.ONEDRIVE_CLIENT_ID || ""; +const DEFAULT_ONEDRIVE_AUTHORITY = process.env.ONEDRIVE_AUTHORITY || ""; +const DEFAULT_REMOTELYSAVE_WEBSITE = process.env.REMOTELYSAVE_WEBSITE || ""; +const DEFAULT_REMOTELYSAVE_CLIENT_ID = process.env.REMOTELYSAVE_CLIENT_ID || ""; +const DEFAULT_GOOGLEDRIVE_CLIENT_ID = process.env.GOOGLEDRIVE_CLIENT_ID || ""; +const DEFAULT_GOOGLEDRIVE_CLIENT_SECRET = + process.env.GOOGLEDRIVE_CLIENT_SECRET || ""; +const DEFAULT_BOX_CLIENT_ID = process.env.BOX_CLIENT_ID || ""; +const DEFAULT_BOX_CLIENT_SECRET = process.env.BOX_CLIENT_SECRET || ""; +const DEFAULT_PCLOUD_CLIENT_ID = process.env.PCLOUD_CLIENT_ID || ""; +const DEFAULT_PCLOUD_CLIENT_SECRET = process.env.PCLOUD_CLIENT_SECRET || ""; +const DEFAULT_YANDEXDISK_CLIENT_ID = process.env.YANDEXDISK_CLIENT_ID || ""; +const DEFAULT_YANDEXDISK_CLIENT_SECRET = + process.env.YANDEXDISK_CLIENT_SECRET || ""; +const DEFAULT_KOOFR_CLIENT_ID = process.env.KOOFR_CLIENT_ID || ""; +const DEFAULT_KOOFR_CLIENT_SECRET = process.env.KOOFR_CLIENT_SECRET || ""; + +esbuild + .context({ + banner: { + js: banner, + }, + loader: { + ".svg": "text", + }, + entryPoints: ["./src/main.ts"], + bundle: true, + external: [ + "obsidian", + "electron", + "fs", + "tls", + "net", + "http", + "https", + "vm", + // "process", + // ...builtins + ], + inject: ["./esbuild.injecthelper.mjs"], + format: "cjs", + // watch: !prod, // no longer valid in esbuild 0.17 + target: "es2016", + logLevel: "info", + sourcemap: prod ? false : "inline", + treeShaking: true, + minify: prod, + outfile: "main.js", + define: { + "global.DEFAULT_DROPBOX_APP_KEY": `"${DEFAULT_DROPBOX_APP_KEY}"`, + "global.DEFAULT_ONEDRIVE_CLIENT_ID": `"${DEFAULT_ONEDRIVE_CLIENT_ID}"`, + "global.DEFAULT_ONEDRIVE_AUTHORITY": `"${DEFAULT_ONEDRIVE_AUTHORITY}"`, + "global.DEFAULT_REMOTELYSAVE_WEBSITE": `"${DEFAULT_REMOTELYSAVE_WEBSITE}"`, + "global.DEFAULT_REMOTELYSAVE_CLIENT_ID": `"${DEFAULT_REMOTELYSAVE_CLIENT_ID}"`, + "global.DEFAULT_GOOGLEDRIVE_CLIENT_ID": `"${DEFAULT_GOOGLEDRIVE_CLIENT_ID}"`, + "global.DEFAULT_GOOGLEDRIVE_CLIENT_SECRET": `"${DEFAULT_GOOGLEDRIVE_CLIENT_SECRET}"`, + "global.DEFAULT_BOX_CLIENT_ID": `"${DEFAULT_BOX_CLIENT_ID}"`, + "global.DEFAULT_BOX_CLIENT_SECRET": `"${DEFAULT_BOX_CLIENT_SECRET}"`, + "global.DEFAULT_PCLOUD_CLIENT_ID": `"${DEFAULT_PCLOUD_CLIENT_ID}"`, + "global.DEFAULT_PCLOUD_CLIENT_SECRET": `"${DEFAULT_PCLOUD_CLIENT_SECRET}"`, + "global.DEFAULT_YANDEXDISK_CLIENT_ID": `"${DEFAULT_YANDEXDISK_CLIENT_ID}"`, + "global.DEFAULT_YANDEXDISK_CLIENT_SECRET": `"${DEFAULT_YANDEXDISK_CLIENT_SECRET}"`, + "global.DEFAULT_KOOFR_CLIENT_ID": `"${DEFAULT_KOOFR_CLIENT_ID}"`, + "global.DEFAULT_KOOFR_CLIENT_SECRET": `"${DEFAULT_KOOFR_CLIENT_SECRET}"`, + global: "window", + "process.env.NODE_DEBUG": `undefined`, // ugly fix + "process.env.DEBUG": `undefined`, // ugly fix + // "process.version": `"v20.10.0"`, // who's using this? + // "process":`undefined`, + // "global.process":`undefined`, + + // make azure blob storage happy + // https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-util/src/checkEnvironment.ts + "globalThis.process.versions": `undefined`, + }, + plugins: [inlineWorkerPlugin()], + }) + .then((context) => { + if (process.argv.includes("--watch")) { + // Enable watch mode + context.watch(); + } else { + // Build once and exit if not in watch mode + context.rebuild().then((result) => { + context.dispose(); + }); + } + }) + .catch(() => process.exit(1)); diff --git a/esbuild.injecthelper.mjs b/esbuild.injecthelper.mjs new file mode 100644 index 0000000..cdb9664 --- /dev/null +++ b/esbuild.injecthelper.mjs @@ -0,0 +1,2 @@ +export const Buffer = require("buffer").Buffer; +export const process = require("process/browser"); diff --git a/fix_modals.js b/fix_modals.js new file mode 100644 index 0000000..05195ef --- /dev/null +++ b/fix_modals.js @@ -0,0 +1,53 @@ +const fs = require('fs'); + +const files = fs.readdirSync('src').filter(f => f.startsWith('settings') && f.endsWith('.ts')).map(f => 'src/' + f); + +files.forEach(file => { + let text = fs.readFileSync(file, 'utf8'); + + // Strip out any corrupted onOpen declarations completely, safely replacing them with basic onOpen. + // The corrupted syntax literally contains 'async onOpen() {\ +' or '\ \ +' in the text file + // as actual backtick + n, not a newline byte. So we match those literals using regex. + + text = text.replace(/async onOpen\(\) \{\ +[^\n]+/g, 'async onOpen() {'); + text = text.replace(/async onOpen\(\) \{\ \ +[^\n]+/g, 'async onOpen() {'); + + // also regular onOpen + text = text.replace(/onOpen\(\) \{\ +[^\n]+/g, 'onOpen() {'); + text = text.replace(/onOpen\(\) \{\ \ +[^\n]+/g, 'onOpen() {'); + + // Sometimes there are multiple corrupted injections, so let's run it a few times to be sure + text = text.replace(/async onOpen\(\) \{\ +[^\n]+/g, 'async onOpen() {'); + text = text.replace(/async onOpen\(\) \{\ \ +[^\n]+/g, 'async onOpen() {'); + + // Now, safely inject the title and class logic ONLY once! + // We ensure we only inject it if it doesn't already have 'this.modalEl.addClass("byoc-auth-modal")' + + // We'll replace the block from "class X extends Modal { ... async onOpen() {" + text = text.replace(/class ([a-zA-Z0-9_]+Modal) extends Modal \{([\s\S]*?)async onOpen\(\) \{/g, (match, className, body) => { + let title = className.replace('AuthModal', ''); + title = title.replace('Revoke', ''); + let prefix = className.includes('Revoke') ? 'Revoke' : 'Connect'; + + return 'class ' + className + ' extends Modal {' + body + 'async onOpen() {\n this.titleEl.setText("' + prefix + ' ' + title + ' Account");\n this.modalEl.addClass("byoc-auth-modal");'; + }); + + text = text.replace(/class ([a-zA-Z0-9_]+Modal) extends Modal \{([\s\S]*?)(? { + let title = className.replace('AuthModal', ''); + title = title.replace('Revoke', ''); + let prefix = className.includes('Revoke') ? 'Revoke' : 'Connect'; + + return 'class ' + className + ' extends Modal {' + body + 'onOpen() {\n this.titleEl.setText("' + prefix + ' ' + title + ' Account");\n this.modalEl.addClass("byoc-auth-modal");'; + }); + + fs.writeFileSync(file, text, 'utf8'); +}); +console.log('Fixed modals without syntax errors'); diff --git a/fix_settings_manual.js b/fix_settings_manual.js new file mode 100644 index 0000000..7b120b1 --- /dev/null +++ b/fix_settings_manual.js @@ -0,0 +1,27 @@ +const fs = require('fs'); +let content = fs.readFileSync('src/settings.ts', 'utf8'); + +// 1. Fix SVG titles for AuthModals and RevokeAuthModals in settings.ts +// Dropbox +content = content.replace(/this\.titleEl\.setText\("Connect Dropbox Account"\);/g, 'this.titleEl.innerHTML = `${SVG_DROPBOX} Connect Dropbox Account`;'); +content = content.replace(/this\.titleEl\.setText\("Revoke Dropbox Account"\);/g, 'this.titleEl.innerHTML = `${SVG_DROPBOX} Revoke Dropbox Account`;'); + +// OneDrive +content = content.replace(/this\.titleEl\.setText\("Connect Onedrive Account"\);/g, 'this.titleEl.innerHTML = `${SVG_ONEDRIVE} Connect Onedrive Account`;'); +content = content.replace(/this\.titleEl\.setText\("Revoke Onedrive Account"\);/g, 'this.titleEl.innerHTML = `${SVG_ONEDRIVE} Revoke Onedrive Account`;'); + +// 2. Remove the copy button logic and inject the clean button. We will manually target Dropbox and Onedrive auth modals natively. + +// Dropbox AuthButton +content = content.replace(/div2\.createEl\(\s*"button",\s*\{\s*text: t\("modal_dropboxauth_copybutton"\),\s*\},\s*\(el\) => \{[\s\S]*?\}\s*\);/g, 'contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); });\n'); + +// Dropbox anchor removal +content = content.replace(/contentEl\.createEl\("p"\)\.createEl\("a", \{\s*href: authUrl,\s*text: "[^"]*?",\s*\}\);/g, ''); + +// OneDrive AuthButton +content = content.replace(/div2\.createEl\(\s*"button",\s*\{\s*text: t\("modal_onedriveauth_copybutton"\),\s*\},\s*\(el\) => \{[\s\S]*?\}\s*\);/g, 'contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); });\n'); + +// Add specific classes to Modals +content = content.replace(/this\.titleEl\.innerHTML/g, 'this.modalEl.addClass("byoc-auth-modal");\n this.titleEl.innerHTML'); + +fs.writeFileSync('src/settings.ts', content, 'utf8'); diff --git a/fix_svgs.js b/fix_svgs.js new file mode 100644 index 0000000..ad52d23 --- /dev/null +++ b/fix_svgs.js @@ -0,0 +1,34 @@ +const fs = require('fs'); + +const svgMap = { + 'Dropbox': 'SVG_DROPBOX', + 'Onedrive': 'SVG_ONEDRIVE', + 'Onedrivefull': 'SVG_ONEDRIVE', + 'GoogleDrive': 'SVG_GDRIVE', + 'Box': 'SVG_BOX', + 'PCloud': 'SVG_PCLOUD', + 'Koofr': 'SVG_KOOFR', + 'YandexDisk': 'SVG_YANDEX' +}; + +const files = fs.readdirSync('src').filter(f => f.startsWith('settings') && f.endsWith('.ts')).map(f => 'src/' + f); + +files.forEach(file => { + let content = fs.readFileSync(file, 'utf8'); + + // Fix the broken SVG injection + content = content.replace(/this\.titleEl\.innerHTML = \\ (Connect|Revoke) ([A-Za-z0-9_]+) Account<\/span>;/g, (match, action, provider) => { + const svgVar = svgMap[provider] || 'SVG_PCLOUD'; + return 'this.titleEl.innerHTML = `' + '${' + svgVar + '}' + ' ' + action + ' ' + provider + ' Account`;'; + }); + + // Wait, no backslash was literally rendered in TS because TS sees `\ ` as just a space in a syntax error? + // Let's just catch anything between innerHTML = and + + content = content.replace(/this\.titleEl\.innerHTML = [\s\S]*?(Connect|Revoke) ([A-Za-z0-9_]+) Account<\/span>[^;]*;/g, (match, action, provider) => { + const svgVar = svgMap[provider] || 'SVG_PCLOUD'; + return 'this.titleEl.innerHTML = `' + '${' + svgVar + '}' + ' ' + action + ' ' + provider + ' Account`;'; + }); + + fs.writeFileSync(file, content, 'utf8'); +}); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..5cb85f4 --- /dev/null +++ b/manifest.json @@ -0,0 +1,10 @@ +{ + "id": "obsidian-byoc", + "name": "Bring Your Own Cloud (BYOC)", + "version": "1.0.0", + "minAppVersion": "0.15.0", + "description": "A clean, self-hosted synchronization tool for Obsidian. Community fork of Remotely Save.", + "author": "winters27", + "authorUrl": "https://github.com/winters27", + "isDesktopOnly": false +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..9c1f092 --- /dev/null +++ b/package.json @@ -0,0 +1,107 @@ +{ + "name": "obsidian-byoc", + "version": "1.0.0", + "description": "A clean, self-hosted, un-paywalled synchronization tool for Obsidian.", + "scripts": { + "dev2": "node esbuild.config.mjs --watch", + "build2": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", + "build": "webpack --mode production", + "postbuild": "node -e \"const fs=require('fs'); const dest='C:/Users/Brandon/Documents/Obsidian Vault/.obsidian/plugins/bring-your-own-cloud'; fs.mkdirSync(dest,{recursive:true}); ['main.js','manifest.json','styles.css'].forEach(f=>{ try{fs.copyFileSync(f,dest+'/'+f); console.log('Deployed '+f+' → '+dest);}catch(e){console.warn('Skip '+f+':',e.message)} });\"", + "deploy": "npm run build", + "dev": "webpack --mode development --watch", + "format": "npx @biomejs/biome check --write .", + "clean": "npx rimraf main.js", + "test": "mocha --import=tsx 'tests/**/*.ts'" + }, + "browser": { + "path": "path-browserify", + "process": "process/browser", + "stream": "stream-browserify", + "crypto": "crypto-browserify", + "url": "url/", + "fs": false, + "vm": false + }, + "source": "main.ts", + "keywords": [], + "author": "", + "license": "SEE LICENSE IN LICENSE", + "devDependencies": { + "@biomejs/biome": "1.8.3", + "@microsoft/microsoft-graph-types": "^2.40.0", + "@types/chai": "^4.3.16", + "@types/chai-as-promised": "^7.1.8", + "@types/jsdom": "^21.1.7", + "@types/lodash": "^4.17.7", + "@types/mime-types": "^2.1.4", + "@types/mocha": "^10.0.7", + "@types/mustache": "^4.2.5", + "@types/node": "^20.14.12", + "@types/qrcode": "^1.5.5", + "builtin-modules": "^4.0.0", + "cross-env": "^7.0.3", + "dotenv": "^16.4.5", + "esbuild": "^0.23.0", + "esbuild-plugin-inline-worker": "^0.1.1", + "jsdom": "^24.1.1", + "mocha": "^10.7.0", + "npm-check-updates": "^16.14.20", + "obsidian": "^1.5.7", + "openapi-typescript": "^7.1.0", + "ts-loader": "^9.5.1", + "tslib": "^2.6.3", + "tsx": "^4.16.2", + "typescript": "^5.5.4", + "webdav-server": "^2.6.2", + "webpack": "^5.93.0", + "webpack-cli": "^5.1.4", + "worker-loader": "^3.0.8" + }, + "dependencies": { + "@aws-sdk/client-s3": "^3.620.0", + "@aws-sdk/lib-storage": "^3.620.0", + "@aws-sdk/signature-v4-crt": "^3.620.0", + "@aws-sdk/types": "^3.609.0", + "@azure/msal-node": "^2.12.0", + "@azure/storage-blob": "^12.24.0", + "@fyears/rclone-crypt": "^0.0.7", + "@fyears/tsqueue": "^1.0.1", + "@microsoft/microsoft-graph-client": "^3.0.7", + "@sanity/diff-match-patch": "^3.1.1", + "@smithy/fetch-http-handler": "^3.2.4", + "@smithy/protocol-http": "^4.1.0", + "@smithy/querystring-builder": "^3.0.3", + "acorn": "^8.12.1", + "aggregate-error": "^5.0.0", + "assert": "^2.1.0", + "aws-crt": "^1.21.3", + "box-typescript-sdk-gen": "^1.3.0", + "buffer": "^6.0.3", + "crypto-browserify": "^3.12.0", + "dropbox": "^10.34.0", + "emoji-regex": "^10.3.0", + "http-status-codes": "^2.3.0", + "localforage": "^1.10.0", + "localforage-getitems": "^1.4.2", + "localforage-removeitems": "^1.4.0", + "lodash": "^4.17.21", + "lucide": "^0.416.0", + "mime-types": "^2.1.35", + "mustache": "^4.2.0", + "nanoid": "^5.0.7", + "node-diff3": "^3.1.2", + "openapi-fetch": "^0.10.2", + "p-queue": "^8.0.1", + "path-browserify": "^1.0.1", + "pcloud-sdk-js": "^2.0.0", + "process": "^0.11.10", + "qrcode": "^1.5.3", + "rfc4648": "^1.5.3", + "rimraf": "^6.0.1", + "stream-browserify": "^3.0.0", + "url": "^0.11.4", + "util": "^0.12.5", + "webdav": "^5.6.0", + "xregexp": "^5.1.1" + } +} diff --git a/src/LICENSE b/src/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/src/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..b288633 --- /dev/null +++ b/src/README.md @@ -0,0 +1,9 @@ +# Main Basic Source + +## What? + +The main basic source code for Remotely Save. + +## License + +The codes or files or subfolders inside the current folder (`src` in the repo), are released under "open source" license: "Apache License, version 2.0". diff --git a/src/aggregateErrorStub.ts b/src/aggregateErrorStub.ts new file mode 100644 index 0000000..1eb45e8 --- /dev/null +++ b/src/aggregateErrorStub.ts @@ -0,0 +1,23 @@ +/** + * aggregate-error browser stub for BYOC plugin. + * The original aggregate-error uses node:url (fileURLToPath) which is not + * available in browser environments (Obsidian plugin context). + * This provides a minimal drop-in replacement compatible with the usage in main.ts. + */ + +class AggregateError extends Error { + errors: readonly Error[]; + + constructor(errors: Iterable, message?: string) { + const errorArray = [...errors]; + super(message ?? errorArray.map((e) => e.message).join("; ")); + this.name = "AggregateError"; + this.errors = errorArray; + } + + [Symbol.iterator]() { + return this.errors[Symbol.iterator](); + } +} + +export default AggregateError; diff --git a/src/baseTypes.ts b/src/baseTypes.ts new file mode 100644 index 0000000..7f35de8 --- /dev/null +++ b/src/baseTypes.ts @@ -0,0 +1,441 @@ +/** + * BYOC — Base Types + * Inlines all PRO config interfaces. No imports from pro/. + */ + +import type { LangTypeAndAuto } from "./i18n"; + +declare global { + var DEFAULT_DROPBOX_APP_KEY: string; + var DEFAULT_ONEDRIVE_CLIENT_ID: string; + var DEFAULT_ONEDRIVE_AUTHORITY: string; + var DEFAULT_GOOGLEDRIVE_CLIENT_ID: string; + var DEFAULT_GOOGLEDRIVE_CLIENT_SECRET: string; + var DEFAULT_BOX_CLIENT_ID: string; + var DEFAULT_BOX_CLIENT_SECRET: string; + var DEFAULT_PCLOUD_CLIENT_ID: string; + var DEFAULT_PCLOUD_CLIENT_SECRET: string; + var DEFAULT_YANDEXDISK_CLIENT_ID: string; + var DEFAULT_YANDEXDISK_CLIENT_SECRET: string; + var DEFAULT_KOOFR_CLIENT_ID: string; + var DEFAULT_KOOFR_CLIENT_SECRET: string; +} + +export const DROPBOX_APP_KEY = globalThis.DEFAULT_DROPBOX_APP_KEY || ""; +export const ONEDRIVE_CLIENT_ID = globalThis.DEFAULT_ONEDRIVE_CLIENT_ID || "3729fc1c-0af2-4bec-9376-d7ac4f0ff806"; +export const ONEDRIVE_AUTHORITY = globalThis.DEFAULT_ONEDRIVE_AUTHORITY || "https://login.microsoftonline.com/consumers/"; +export const GOOGLEDRIVE_CLIENT_ID = globalThis.DEFAULT_GOOGLEDRIVE_CLIENT_ID || "573787634452-5301p7h0r85rpc03hbnrn5k9qqt3bulf.apps.googleusercontent.com"; +export const GOOGLEDRIVE_CLIENT_SECRET = globalThis.DEFAULT_GOOGLEDRIVE_CLIENT_SECRET || "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"; +export const BOX_CLIENT_ID = globalThis.DEFAULT_BOX_CLIENT_ID || ""; +export const BOX_CLIENT_SECRET = globalThis.DEFAULT_BOX_CLIENT_SECRET || ""; +export const PCLOUD_CLIENT_ID = globalThis.DEFAULT_PCLOUD_CLIENT_ID || "zrDphYT1giQ"; +export const PCLOUD_CLIENT_SECRET = globalThis.DEFAULT_PCLOUD_CLIENT_SECRET || "TqjVEEaXlsHwCOczzm0R9SBmxf2y"; +export const YANDEXDISK_CLIENT_ID = globalThis.DEFAULT_YANDEXDISK_CLIENT_ID || ""; +export const YANDEXDISK_CLIENT_SECRET = globalThis.DEFAULT_YANDEXDISK_CLIENT_SECRET || ""; +export const KOOFR_CLIENT_ID = globalThis.DEFAULT_KOOFR_CLIENT_ID || ""; +export const KOOFR_CLIENT_SECRET = globalThis.DEFAULT_KOOFR_CLIENT_SECRET || ""; + +export const DEFAULT_CONTENT_TYPE = "application/octet-stream"; + +export type SUPPORTED_SERVICES_TYPE = + | "s3" + | "webdav" + | "dropbox" + | "onedrive" + | "onedrivefull" + | "webdis" + | "googledrive" + | "box" + | "pcloud" + | "yandexdisk" + | "koofr" + | "azureblobstorage"; + +export type SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR = Exclude< + SUPPORTED_SERVICES_TYPE, + "s3" | "azureblobstorage" +>; + +export interface S3Config { + s3Endpoint: string; + s3Region: string; + s3AccessKeyID: string; + s3SecretAccessKey: string; + s3BucketName: string; + partsConcurrency?: number; + forcePathStyle?: boolean; + remotePrefix?: string; + useAccurateMTime?: boolean; + reverseProxyNoSignUrl?: string; + generateFolderObject?: boolean; + /** @deprecated */ + bypassCorsLocally?: boolean; +} + +export interface DropboxConfig { + accessToken: string; + clientID: string; + refreshToken: string; + accessTokenExpiresInSeconds: number; + accessTokenExpiresAtTime: number; + accountID: string; + username: string; + credentialsShouldBeDeletedAtTime?: number; + remoteBaseDir?: string; +} + +export type WebdavAuthType = "digest" | "basic"; +export type WebdavDepthType = + | "auto" + | "auto_unknown" + | "auto_1" + | "auto_infinity" + | "manual_1" + | "manual_infinity"; + +export interface WebdavConfig { + address: string; + username: string; + password: string; + authType: WebdavAuthType; + depth?: WebdavDepthType; + remoteBaseDir?: string; + customHeaders?: string; + /** @deprecated */ + manualRecursive: boolean; +} + +export interface OnedriveConfig { + accessToken: string; + clientID: string; + authority: string; + refreshToken: string; + accessTokenExpiresInSeconds: number; + accessTokenExpiresAtTime: number; + deltaLink: string; + username: string; + credentialsShouldBeDeletedAtTime?: number; + remoteBaseDir?: string; + emptyFile: "skip" | "error"; + kind: "onedrive"; +} + +export interface WebdisConfig { + address: string; + username?: string; + password?: string; + remoteBaseDir?: string; +} + +// ─── PRO PROVIDER CONFIGS (inlined from baseTypesPro — clean-room) ─────────── + +export interface GoogleDriveConfig { + accessToken: string; + accessTokenExpiresInMs: number; + accessTokenExpiresAtTimeMs: number; + refreshToken: string; + remoteBaseDir?: string; + credentialsShouldBeDeletedAtTimeMs?: number; + scope: "https://www.googleapis.com/auth/drive.file"; + kind: "googledrive"; +} + +export interface BoxConfig { + accessToken: string; + accessTokenExpiresInMs: number; + accessTokenExpiresAtTimeMs: number; + refreshToken: string; + remoteBaseDir?: string; + credentialsShouldBeDeletedAtTimeMs?: number; + kind: "box"; +} + +export interface PCloudConfig { + accessToken: string; + username?: string; + hostname: "eapi.pcloud.com" | "api.pcloud.com"; + locationid: 1 | 2; + remoteBaseDir?: string; + credentialsShouldBeDeletedAtTimeMs?: number; + kind: "pcloud"; + /** @deprecated */ + emptyFile: "skip" | "error"; +} + +export interface YandexDiskConfig { + accessToken: string; + accessTokenExpiresInMs: number; + accessTokenExpiresAtTimeMs: number; + refreshToken: string; + remoteBaseDir?: string; + credentialsShouldBeDeletedAtTimeMs?: number; + scope: string; + kind: "yandexdisk"; +} + +export interface KoofrConfig { + accessToken: string; + accessTokenExpiresInMs: number; + accessTokenExpiresAtTimeMs: number; + refreshToken: string; + remoteBaseDir?: string; + credentialsShouldBeDeletedAtTimeMs?: number; + scope: string; + api: string; + mountID: string; + kind: "koofr"; +} + +export interface AzureBlobStorageConfig { + containerSasUrl: string; + containerName: string; + remotePrefix: string; + generateFolderObject: boolean; + partsConcurrency: number; + kind: "azureblobstorage"; +} + +export interface OnedriveFullConfig { + accessToken: string; + clientID: string; + authority: string; + refreshToken: string; + accessTokenExpiresInSeconds: number; + accessTokenExpiresAtTime: number; + deltaLink: string; + username: string; + credentialsShouldBeDeletedAtTime?: number; + remoteBaseDir?: string; + emptyFile: "skip" | "error"; + kind: "onedrivefull"; +} + +// ───────────────────────────────────────────────────────────────────────────── + +export type SyncDirectionType = + | "bidirectional" + | "incremental_pull_only" + | "incremental_push_only" + | "incremental_pull_and_delete_only" + | "incremental_push_and_delete_only"; + +export type CipherMethodType = "rclone-base64" | "openssl-base64" | "unknown"; + +export type QRExportType = "basic_and_advanced" | SUPPORTED_SERVICES_TYPE; + +export interface ProfilerConfig { + enable?: boolean; + enablePrinting?: boolean; + recordSize?: boolean; +} + +export interface BYOCPluginSettings { + s3: S3Config; + webdav: WebdavConfig; + dropbox: DropboxConfig; + onedrive: OnedriveConfig; + onedrivefull: OnedriveFullConfig; + webdis: WebdisConfig; + googledrive: GoogleDriveConfig; + box: BoxConfig; + pcloud: PCloudConfig; + yandexdisk: YandexDiskConfig; + koofr: KoofrConfig; + azureblobstorage: AzureBlobStorageConfig; + + password: string; + serviceType: SUPPORTED_SERVICES_TYPE; + currLogLevel?: string; + autoRunEveryMilliseconds?: number; + initRunAfterMilliseconds?: number; + syncOnSaveAfterMilliseconds?: number; + + concurrency?: number; + syncConfigDir?: boolean; + syncBookmarks?: boolean; + syncUnderscoreItems?: boolean; + lang?: LangTypeAndAuto; + agreeToUseSyncV3?: boolean; + skipSizeLargerThan?: number; + ignorePaths?: string[]; + onlyAllowPaths?: string[]; + enableStatusBarInfo?: boolean; + deleteToWhere?: "system" | "obsidian"; + conflictAction?: ConflictActionType; + + protectModifyPercentage?: number; + syncDirection?: SyncDirectionType; + + obfuscateSettingFile?: boolean; + enableMobileStatusBar?: boolean; + encryptionMethod?: CipherMethodType; + + profiler?: ProfilerConfig; + + /** Migration version — tracks which migration has been applied */ + migrationVersion?: number; + + /** @deprecated */ + agreeToUploadExtraMetadata?: boolean; + /** @deprecated */ + vaultRandomID?: string; + /** @deprecated */ + logToDB?: boolean; + /** @deprecated */ + howToCleanEmptyFolder?: EmptyFolderCleanType; + /** @deprecated - removed in BYOC, kept for migration compat */ + pro?: Record; +} + +/** + * Alias for backwards compatibility with any code still using the old name. + * @deprecated Use BYOCPluginSettings + */ +export type RemotelySavePluginSettings = BYOCPluginSettings; + +export const PLUGIN_ID = "obsidian-byoc"; +export const PLUGIN_NAME = "BYOC"; + +// ─── URI / Callback Constants ───────────────────────────────────────────────── + +/** Primary URI for BYOC */ +export const COMMAND_URI = "bring-your-own-cloud"; +/** Legacy alias — accepts old remotely-save:// links */ +export const COMMAND_URI_LEGACY = "remotely-save"; +export const COMMAND_CALLBACK = "bring-your-own-cloud-cb"; +export const COMMAND_CALLBACK_ONEDRIVE = "bring-your-own-cloud-cb-onedrive"; +export const COMMAND_CALLBACK_DROPBOX = "bring-your-own-cloud-cb-dropbox"; +export const COMMAND_CALLBACK_ONEDRIVEFULL = "bring-your-own-cloud-cb-onedrivefull"; +export const COMMAND_CALLBACK_BOX = "bring-your-own-cloud-cb-box"; +export const COMMAND_CALLBACK_PCLOUD = "remotely-save-cb-pcloud"; +export const COMMAND_CALLBACK_YANDEXDISK = "bring-your-own-cloud-cb-yandexdisk"; +export const COMMAND_CALLBACK_KOOFR = "bring-your-own-cloud-cb-koofr"; + +export interface UriParams { + func?: string; + vault?: string; + ver?: string; + data?: string; +} + +export type EmptyFolderCleanType = "skip" | "clean_both"; + +export type ConflictActionType = + | "keep_newer" + | "keep_larger" + | "smart_conflict"; + +export type DecisionTypeForMixedEntity = + | "only_history" + | "equal" + | "local_is_modified_then_push" + | "remote_is_modified_then_pull" + | "local_is_created_then_push" + | "remote_is_created_then_pull" + | "local_is_created_too_large_then_do_nothing" + | "remote_is_created_too_large_then_do_nothing" + | "local_is_deleted_thus_also_delete_remote" + | "remote_is_deleted_thus_also_delete_local" + | "conflict_created_then_keep_local" + | "conflict_created_then_keep_remote" + | "conflict_created_then_smart_conflict" + | "conflict_created_then_do_nothing" + | "conflict_modified_then_keep_local" + | "conflict_modified_then_keep_remote" + | "conflict_modified_then_smart_conflict" + | "folder_existed_both_then_do_nothing" + | "folder_existed_local_then_also_create_remote" + | "folder_existed_remote_then_also_create_local" + | "folder_to_be_created" + | "folder_to_skip" + | "folder_to_be_deleted_on_both" + | "folder_to_be_deleted_on_remote" + | "folder_to_be_deleted_on_local"; + +/** + * Uniform representation of a file/folder entity. + * Everything should be flat and primitive so it can be copied. + */ +export interface Entity { + key?: string; + keyEnc?: string; + keyRaw: string; + mtimeCli?: number; + mtimeCliFmt?: string; + ctimeCli?: number; + ctimeCliFmt?: string; + mtimeSvr?: number; + mtimeSvrFmt?: string; + prevSyncTime?: number; + prevSyncTimeFmt?: string; + size?: number; + sizeEnc?: number; + sizeRaw: number; + hash?: string; + etag?: string; + synthesizedFolder?: boolean; + synthesizedFile?: boolean; +} + +export interface UploadedType { + entity: Entity; + mtimeCli?: number; +} + +export interface MixedEntity { + key: string; + local?: Entity; + prevSync?: Entity; + remote?: Entity; + + decisionBranch?: number; + decision?: DecisionTypeForMixedEntity; + conflictAction?: ConflictActionType; + + change?: boolean; + sideNotes?: any; +} + +/** @deprecated */ +export interface FileOrFolderMixedState { + key: string; + existLocal?: boolean; + existRemote?: boolean; + mtimeLocal?: number; + mtimeRemote?: number; + deltimeLocal?: number; + deltimeRemote?: number; + sizeLocal?: number; + sizeLocalEnc?: number; + sizeRemote?: number; + sizeRemoteEnc?: number; + changeRemoteMtimeUsingMapping?: boolean; + changeLocalMtimeUsingMapping?: boolean; + decision?: string; + decisionBranch?: number; + syncDone?: "done"; + remoteEncryptedKey?: string; + mtimeLocalFmt?: string; + mtimeRemoteFmt?: string; + deltimeLocalFmt?: string; + deltimeRemoteFmt?: string; +} + +export const DEFAULT_DEBUG_FOLDER = "_debug_byoc/"; +export const DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX = "sync_plans_hist_exported_on_"; +export const DEFAULT_LOG_HISTORY_FILE_PREFIX = "log_hist_exported_on_"; +export const DEFAULT_PROFILER_RESULT_FILE_PREFIX = "profiler_results_exported_on_"; + +export type SyncTriggerSourceType = + | "manual" + | "dry" + | "auto" + | "auto_once_init" + | "auto_sync_on_save"; + +export const REMOTELY_SAVE_VERSION_2022 = "0.3.25"; +export const REMOTELY_SAVE_VERSION_2024PREPARE = "0.3.32"; +export const BYOC_VERSION_1_0_0 = "1.0.0"; + +/** Smart conflict merge threshold: files larger than this use duplicate strategy */ +export const SMART_CONFLICT_MERGE_SIZE_LIMIT = 1_000_000; // 1 MB diff --git a/src/baseTypesObs.ts b/src/baseTypesObs.ts new file mode 100644 index 0000000..14aa214 --- /dev/null +++ b/src/baseTypesObs.ts @@ -0,0 +1,14 @@ +/** + * Every utils requiring Obsidian is placed here. + */ + +import { Platform, requireApiVersion } from "obsidian"; + +export const API_VER_STAT_FOLDER = "0.13.27"; +export const API_VER_REQURL = "0.13.26"; // desktop ver 0.13.26, iOS ver 1.1.1 +export const API_VER_REQURL_ANDROID = "0.14.6"; // Android ver 1.2.1 +export const API_VER_ENSURE_REQURL_OK = "1.0.0"; // always bypass CORS here + +export const VALID_REQURL = + (!Platform.isAndroidApp && requireApiVersion(API_VER_REQURL)) || + (Platform.isAndroidApp && requireApiVersion(API_VER_REQURL_ANDROID)); diff --git a/src/configPersist.ts b/src/configPersist.ts new file mode 100644 index 0000000..e14147f --- /dev/null +++ b/src/configPersist.ts @@ -0,0 +1,67 @@ +import { base64url } from "rfc4648"; +import { reverseString } from "./misc"; + +import type { RemotelySavePluginSettings } from "./baseTypes"; + +const DEFAULT_README: string = + "The file contains sensitive info, so DO NOT take screenshot of, copy, or share it to anyone! It's also generated automatically, so do not edit it manually."; + +interface MessyConfigType { + readme: string; + d: string; +} + +/** + * this should accept the result after loadData(); + */ +export const messyConfigToNormal = ( + x: MessyConfigType | RemotelySavePluginSettings | null | undefined +): RemotelySavePluginSettings | null | undefined => { + // console.debug("loading, original config on disk:"); + // console.debug(x); + if (x === null || x === undefined) { + console.debug("the messy config is null or undefined, skip"); + return x as any; + } + if ("readme" in x && "d" in x) { + // we should decode + const y = JSON.parse( + ( + base64url.parse(reverseString(x["d"]), { + out: Buffer.allocUnsafe as any, + loose: true, + }) as Buffer + ).toString("utf-8") + ); + // console.debug("loading, parsed config is:"); + // console.debug(y); + return y; + } else { + // return as is + // console.debug("loading, parsed config is the same"); + return x; + } +}; + +/** + * this should accept the result of original config + */ +export const normalConfigToMessy = ( + x: RemotelySavePluginSettings | null | undefined +) => { + if (x === null || x === undefined) { + console.debug("the normal config is null or undefined, skip"); + return x; + } + const y = { + readme: DEFAULT_README, + d: reverseString( + base64url.stringify(Buffer.from(JSON.stringify(x), "utf-8"), { + pad: false, + }) + ), + }; + // console.debug("encoding, encoded config is:"); + // console.debug(y); + return y; +}; diff --git a/src/copyLogic.ts b/src/copyLogic.ts new file mode 100644 index 0000000..5816bbc --- /dev/null +++ b/src/copyLogic.ts @@ -0,0 +1,60 @@ +import type { FakeFs } from "./fsAll"; + +export async function copyFolder(key: string, left: FakeFs, right: FakeFs) { + if (!key.endsWith("/")) { + throw Error(`should not call ${key} in copyFolder`); + } + const statsLeft = await left.stat(key); + const entity = await right.mkdir(key, statsLeft.mtimeCli); + return { + entity: entity, + content: undefined, + }; +} + +export async function copyFile(key: string, left: FakeFs, right: FakeFs) { + // console.debug(`copyFile: key=${key}, left=${left.kind}, right=${right.kind}`); + if (key.endsWith("/")) { + throw Error(`should not call ${key} in copyFile`); + } + const statsLeft = await left.stat(key); + const content = await left.readFile(key); + + if (statsLeft.size === undefined || statsLeft.size === 0) { + // some weird bugs on android not returning size. just ignore them + statsLeft.size = content.byteLength; + } else { + if (statsLeft.size !== content.byteLength) { + throw Error( + `error copying ${left.kind}=>${right.kind}: size not matched` + ); + } + } + + if (statsLeft.mtimeCli === undefined) { + throw Error(`error copying ${left.kind}=>${right.kind}, no mtimeCli`); + } + + // console.debug(`copyFile: about to start right.writeFile`); + return { + entity: await right.writeFile( + key, + content, + statsLeft.mtimeCli, + statsLeft.ctimeCli ?? statsLeft.mtimeCli + ), + content: content, + }; +} + +export async function copyFileOrFolder( + key: string, + left: FakeFs, + right: FakeFs +) { + if (key.endsWith("/")) { + return await copyFolder(key, left, right); + } else { + return await copyFile(key, left, right); + } +} diff --git a/src/debugMode.ts b/src/debugMode.ts new file mode 100644 index 0000000..fcb7f96 --- /dev/null +++ b/src/debugMode.ts @@ -0,0 +1,96 @@ +import type { Vault } from "obsidian"; + +/** SyncPlanType — local stub; full type defined in Batch 2 sync engine */ +type SyncPlanType = Record; +import { + DEFAULT_DEBUG_FOLDER, + DEFAULT_PROFILER_RESULT_FILE_PREFIX, + DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX, +} from "./baseTypes"; +import { + readAllProfilerResultsByVault, + readAllSyncPlanRecordTextsByVault, +} from "./localdb"; +import type { InternalDBs } from "./localdb"; +import { mkdirpInVault } from "./misc"; + +const getSubsetOfSyncPlan = (x: string, onlyChange: boolean) => { + if (!onlyChange) { + return x; + } + const y: SyncPlanType = JSON.parse(x); + const z: SyncPlanType = Object.fromEntries( + Object.entries(y).filter(([key, val]) => { + if (key === "/$@meta") { + return true; + } + return val.change === undefined || val.change === true; + }) + ); + return JSON.stringify(z, null, 2); +}; + +export const exportVaultSyncPlansToFiles = async ( + db: InternalDBs, + vault: Vault, + vaultRandomID: string, + howMany: number, + onlyChange: boolean +) => { + console.info("exporting sync plans"); + await mkdirpInVault(DEFAULT_DEBUG_FOLDER, vault); + const records = await readAllSyncPlanRecordTextsByVault(db, vaultRandomID); + let md = ""; + if (records.length === 0) { + md = "No sync plans history found"; + } else { + if (howMany <= 0) { + md = + "Sync plans found:\n\n" + + records + .map( + (x) => "```json\n" + getSubsetOfSyncPlan(x, onlyChange) + "\n```\n" + ) + .join("\n"); + } else { + md = + "Sync plans found:\n\n" + + records + .map( + (x) => "```json\n" + getSubsetOfSyncPlan(x, onlyChange) + "\n```\n" + ) + .slice(0, howMany) + .join("\n"); + } + } + const ts = Date.now(); + const filePath = `${DEFAULT_DEBUG_FOLDER}${DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX}${ts}.md`; + await vault.create(filePath, md, { + mtime: ts, + }); + console.info("finish exporting sync plans"); +}; + +export const exportVaultProfilerResultsToFiles = async ( + db: InternalDBs, + vault: Vault, + vaultRandomID: string +) => { + console.info("exporting profiler results"); + await mkdirpInVault(DEFAULT_DEBUG_FOLDER, vault); + const records = await readAllProfilerResultsByVault(db, vaultRandomID); + let md = ""; + if (records.length === 0) { + md = "No profiler results found"; + } else { + md = + "Profiler results found:\n\n" + + records.map((x) => "```\n" + x + "\n```\n").join("\n"); + } + const ts = Date.now(); + const filePath = `${DEFAULT_DEBUG_FOLDER}${DEFAULT_PROFILER_RESULT_FILE_PREFIX}${ts}.md`; + await vault.create(filePath, md, { + mtime: ts, + }); + console.info("finish exporting profiler results"); +}; diff --git a/src/encryptOpenSSL.ts b/src/encryptOpenSSL.ts new file mode 100644 index 0000000..18a49a2 --- /dev/null +++ b/src/encryptOpenSSL.ts @@ -0,0 +1,186 @@ +import { base32, base64url } from "rfc4648"; +import { bufferToArrayBuffer, hexStringToTypedArray } from "./misc"; + +const DEFAULT_ITER = 20000; + +// base32.stringify(Buffer.from('Salted__')) +export const MAGIC_ENCRYPTED_PREFIX_BASE32 = "KNQWY5DFMRPV"; +// base64.stringify(Buffer.from('Salted__')) +export const MAGIC_ENCRYPTED_PREFIX_BASE64URL = "U2FsdGVkX"; + +const getKeyIVFromPassword = async ( + salt: Uint8Array, + password: string, + rounds: number = DEFAULT_ITER +) => { + const k1 = await window.crypto.subtle.importKey( + "raw", + new TextEncoder().encode(password), + { name: "PBKDF2" }, + false, + ["deriveKey", "deriveBits"] + ); + + const k2 = await window.crypto.subtle.deriveBits( + { + name: "PBKDF2", + salt: salt, + iterations: rounds, + hash: "SHA-256", + }, + k1, + 256 + 128 + ); + + return k2; +}; + +export const encryptArrayBuffer = async ( + arrBuf: ArrayBuffer, + password: string, + rounds: number = DEFAULT_ITER, + saltHex = "" +) => { + let salt: Uint8Array; + if (saltHex !== "") { + salt = hexStringToTypedArray(saltHex); + } else { + salt = window.crypto.getRandomValues(new Uint8Array(8)); + } + + const derivedKey = await getKeyIVFromPassword(salt, password, rounds); + const key = derivedKey.slice(0, 32); + const iv = derivedKey.slice(32, 32 + 16); + + const keyCrypt = await window.crypto.subtle.importKey( + "raw", + key, + { name: "AES-CBC" }, + false, + ["encrypt", "decrypt"] + ); + + const enc = (await window.crypto.subtle.encrypt( + { name: "AES-CBC", iv }, + keyCrypt, + arrBuf + )) as ArrayBuffer; + + const prefix = new TextEncoder().encode("Salted__"); + + const res = new Uint8Array([...prefix, ...salt, ...new Uint8Array(enc)]); + + return bufferToArrayBuffer(res); +}; + +export const decryptArrayBuffer = async ( + arrBuf: ArrayBuffer, + password: string, + rounds: number = DEFAULT_ITER +) => { + const prefix = arrBuf.slice(0, 8); + const salt = arrBuf.slice(8, 16); + const derivedKey = await getKeyIVFromPassword( + new Uint8Array(salt), + password, + rounds + ); + const key = derivedKey.slice(0, 32); + const iv = derivedKey.slice(32, 32 + 16); + + const keyCrypt = await window.crypto.subtle.importKey( + "raw", + key, + { name: "AES-CBC" }, + false, + ["encrypt", "decrypt"] + ); + + const dec = (await window.crypto.subtle.decrypt( + { name: "AES-CBC", iv }, + keyCrypt, + arrBuf.slice(16) + )) as ArrayBuffer; + + return dec; +}; + +export const encryptStringToBase32 = async ( + text: string, + password: string, + rounds: number = DEFAULT_ITER, + saltHex = "" +) => { + const enc = await encryptArrayBuffer( + bufferToArrayBuffer(new TextEncoder().encode(text)), + password, + rounds, + saltHex + ); + return base32.stringify(new Uint8Array(enc), { pad: false }); +}; + +export const decryptBase32ToString = async ( + text: string, + password: string, + rounds: number = DEFAULT_ITER +) => { + return new TextDecoder().decode( + await decryptArrayBuffer( + bufferToArrayBuffer(base32.parse(text, { loose: true })), + password, + rounds + ) + ); +}; + +export const encryptStringToBase64url = async ( + text: string, + password: string, + rounds: number = DEFAULT_ITER, + saltHex = "" +) => { + const enc = await encryptArrayBuffer( + bufferToArrayBuffer(new TextEncoder().encode(text)), + password, + rounds, + saltHex + ); + return base64url.stringify(new Uint8Array(enc), { pad: false }); +}; + +export const decryptBase64urlToString = async ( + text: string, + password: string, + rounds: number = DEFAULT_ITER +) => { + return new TextDecoder().decode( + await decryptArrayBuffer( + bufferToArrayBuffer(base64url.parse(text, { loose: true })), + password, + rounds + ) + ); +}; + +export const getSizeFromOrigToEnc = (x: number) => { + if (x < 0 || Number.isNaN(x) || !Number.isInteger(x)) { + throw Error(`getSizeFromOrigToEnc: x=${x} is not a valid size`); + } + return (Math.floor(x / 16) + 1) * 16 + 16; +}; + +export const getSizeFromEncToOrig = (x: number) => { + if (x < 32 || Number.isNaN(x) || !Number.isInteger(x)) { + throw Error(`getSizeFromEncToOrig: ${x} is not a valid size`); + } + if (x % 16 !== 0) { + throw Error( + `getSizeFromEncToOrig: ${x} is not a valid encrypted file size` + ); + } + return { + minSize: ((x - 16) / 16 - 1) * 16, + maxSize: ((x - 16) / 16 - 1) * 16 + 15, + }; +}; diff --git a/src/encryptRClone.ts b/src/encryptRClone.ts new file mode 100644 index 0000000..d132616 --- /dev/null +++ b/src/encryptRClone.ts @@ -0,0 +1,257 @@ +import { + Cipher as CipherRCloneCryptPack, + encryptedSize, +} from "@fyears/rclone-crypt"; + +// @ts-ignore +import EncryptWorker from "./encryptRClone.worker"; + +interface RecvMsg { + status: "ok" | "error"; + outputName?: string; + outputContent?: ArrayBuffer; + error?: any; +} + +export const getSizeFromOrigToEnc = encryptedSize; + +export class CipherRclone { + readonly password: string; + readonly cipher: CipherRCloneCryptPack; + readonly workers: Worker[]; + init: boolean; + workerIdx: number; + constructor(password: string, workerNum: number) { + this.password = password; + this.init = false; + this.workerIdx = 0; + + // console.debug("begin creating CipherRCloneCryptPack"); + this.cipher = new CipherRCloneCryptPack("base64"); + // console.debug("finish creating CipherRCloneCryptPack"); + + // console.debug("begin creating EncryptWorker"); + this.workers = []; + for (let i = 0; i < workerNum; ++i) { + this.workers.push(new (EncryptWorker as any)() as Worker); + } + + // console.debug("finish creating EncryptWorker"); + } + + closeResources() { + for (let i = 0; i < this.workers.length; ++i) { + this.workers[i].terminate(); + } + } + + async prepareByCallingWorker(): Promise { + if (this.init) { + return; + } + // console.debug("begin prepareByCallingWorker"); + await this.cipher.key(this.password, ""); + // console.debug("finish getting key"); + + const res: Promise[] = []; + for (let i = 0; i < this.workers.length; ++i) { + res.push( + new Promise((resolve, reject) => { + const channel = new MessageChannel(); + + channel.port2.onmessage = (event) => { + // console.debug("main: receiving msg in prepare"); + const { status } = event.data as RecvMsg; + if (status === "ok") { + // console.debug("main: receiving init ok in prepare"); + this.init = true; + resolve(); // return the class object itself + } else { + reject("error after prepareByCallingWorker"); + } + }; + + channel.port2.onmessageerror = (event) => { + // console.debug("main: receiving error in prepare"); + reject(event); + }; + + // console.debug("main: before postMessage in prepare"); + this.workers[i].postMessage( + { + action: "prepare", + dataKeyBuf: this.cipher.dataKey.buffer, + nameKeyBuf: this.cipher.nameKey.buffer, + nameTweakBuf: this.cipher.nameTweak.buffer, + }, + [channel.port1 /* buffer no transfered because we need to copy */] + ); + }) + ); + } + await Promise.all(res); + } + + async encryptNameByCallingWorker(inputName: string): Promise { + // console.debug("main: start encryptNameByCallingWorker"); + await this.prepareByCallingWorker(); + // console.debug( + // "main: really start generate promise in encryptNameByCallingWorker" + // ); + ++this.workerIdx; + const whichWorker = this.workerIdx % this.workers.length; + return await new Promise((resolve, reject) => { + const channel = new MessageChannel(); + + channel.port2.onmessage = (event) => { + // console.debug("main: receiving msg in encryptNameByCallingWorker"); + const { outputName } = event.data as RecvMsg; + if (outputName === undefined) { + reject("unknown outputName after encryptNameByCallingWorker"); + } else { + resolve(outputName); + } + }; + + channel.port2.onmessageerror = (event) => { + // console.debug("main: receiving error in encryptNameByCallingWorker"); + reject(event); + }; + + // console.debug("main: before postMessage in encryptNameByCallingWorker"); + this.workers[whichWorker].postMessage( + { + action: "encryptName", + inputName: inputName, + }, + [channel.port1] + ); + }); + } + + async decryptNameByCallingWorker(inputName: string): Promise { + await this.prepareByCallingWorker(); + ++this.workerIdx; + const whichWorker = this.workerIdx % this.workers.length; + return await new Promise((resolve, reject) => { + const channel = new MessageChannel(); + + channel.port2.onmessage = (event) => { + // console.debug("main: receiving msg in decryptNameByCallingWorker"); + const { outputName, status } = event.data as RecvMsg; + + if (status === "error") { + reject("error"); + } else { + if (outputName === undefined) { + reject("unknown outputName after decryptNameByCallingWorker"); + } else { + resolve(outputName); + } + } + }; + + channel.port2.onmessageerror = (event) => { + // console.debug("main: receiving error in decryptNameByCallingWorker"); + reject(event); + channel; + }; + + // console.debug("main: before postMessage in decryptNameByCallingWorker"); + this.workers[whichWorker].postMessage( + { + action: "decryptName", + inputName: inputName, + }, + [channel.port1] + ); + }); + } + + async encryptContentByCallingWorker( + input: ArrayBuffer + ): Promise { + await this.prepareByCallingWorker(); + ++this.workerIdx; + const whichWorker = this.workerIdx % this.workers.length; + return await new Promise((resolve, reject) => { + const channel = new MessageChannel(); + + channel.port2.onmessage = (event) => { + // console.debug("main: receiving msg in encryptContentByCallingWorker"); + const { outputContent } = event.data as RecvMsg; + if (outputContent === undefined) { + reject("unknown outputContent after encryptContentByCallingWorker"); + } else { + resolve(outputContent); + } + }; + + channel.port2.onmessageerror = (event) => { + // console.debug("main: receiving error in encryptContentByCallingWorker"); + reject(event); + }; + + // console.debug( + // "main: before postMessage in encryptContentByCallingWorker" + // ); + this.workers[whichWorker].postMessage( + { + action: "encryptContent", + inputContent: input, + }, + [ + channel.port1, + // input // the array buffer might be re-used later, so we CANNOT transfer here + ] + ); + }); + } + + async decryptContentByCallingWorker( + input: ArrayBuffer + ): Promise { + await this.prepareByCallingWorker(); + ++this.workerIdx; + const whichWorker = this.workerIdx % this.workers.length; + return await new Promise((resolve, reject) => { + const channel = new MessageChannel(); + + channel.port2.onmessage = (event) => { + // console.debug("main: receiving msg in decryptContentByCallingWorker"); + const { outputContent, status } = event.data as RecvMsg; + + if (status === "error") { + reject("error"); + } else { + if (outputContent === undefined) { + reject("unknown outputContent after decryptContentByCallingWorker"); + } else { + resolve(outputContent); + } + } + }; + + channel.port2.onmessageerror = (event) => { + // console.debug( + // "main: receiving onmessageerror in decryptContentByCallingWorker" + // ); + reject(event); + }; + + // console.debug( + // "main: before postMessage in decryptContentByCallingWorker" + // ); + this.workers[whichWorker].postMessage( + { + action: "decryptContent", + inputContent: input, + }, + [ + channel.port1, + input, // not transfer for safety + ] + ); + }); + } +} diff --git a/src/encryptRClone.worker.ts b/src/encryptRClone.worker.ts new file mode 100644 index 0000000..2714807 --- /dev/null +++ b/src/encryptRClone.worker.ts @@ -0,0 +1,184 @@ +import { Cipher as CipherRCloneCryptPack } from "@fyears/rclone-crypt"; +import { nanoid } from "nanoid"; + +const ctx: WorkerGlobalScope = self as any; + +const workerNanoID = nanoid(); +const cipher = new CipherRCloneCryptPack("base64"); + +// console.debug(`worker [${workerNanoID}]: cipher created`); + +async function encryptNameStr(input: string) { + const res = await cipher.encryptFileName(input); + return res; +} + +async function decryptNameStr(input: string) { + return await cipher.decryptFileName(input); +} + +async function encryptContentBuf(input: ArrayBuffer) { + return (await cipher.encryptData(new Uint8Array(input), undefined)).buffer; +} + +async function decryptContentBuf(input: ArrayBuffer) { + return (await cipher.decryptData(new Uint8Array(input))).buffer; +} + +ctx.addEventListener("message", async (event: any) => { + const port: MessagePort = event.ports[0]; + const { + action, + dataKeyBuf, + nameKeyBuf, + nameTweakBuf, + inputName, + inputContent, + } = event.data as { + action: + | "prepare" + | "encryptContent" + | "decryptContent" + | "encryptName" + | "decryptName"; + dataKeyBuf?: ArrayBuffer; + nameKeyBuf?: ArrayBuffer; + nameTweakBuf?: ArrayBuffer; + inputName?: string; + inputContent?: ArrayBuffer; + }; + + // console.debug(`worker [${workerNanoID}]: receiving action=${action}`); + + if (action === "prepare") { + // console.debug(`worker [${workerNanoID}]: prepare: start`); + try { + if ( + dataKeyBuf === undefined || + nameKeyBuf === undefined || + nameTweakBuf === undefined + ) { + // console.debug(`worker [${workerNanoID}]: prepare: no buffer??`); + throw Error( + `worker [${workerNanoID}]: prepare: internal keys not transferred to worker properly` + ); + } + // console.debug(`worker [${workerNanoID}]: prepare: so we update`); + cipher.updateInternalKey( + new Uint8Array(dataKeyBuf), + new Uint8Array(nameKeyBuf), + new Uint8Array(nameTweakBuf) + ); + port.postMessage({ + status: "ok", + }); + } catch (error) { + console.error(error); + port.postMessage({ + status: "error", + error: error, + }); + } + } else if (action === "encryptName") { + try { + if (inputName === undefined) { + throw Error( + `worker [${workerNanoID}]: encryptName: internal inputName not transferred to worker properly` + ); + } + const outputName = await encryptNameStr(inputName); + // console.debug( + // `worker [${workerNanoID}]: after encryptNameStr, before postMessage` + // ); + port.postMessage({ + status: "ok", + outputName: outputName, + }); + } catch (error) { + console.error(`worker [${workerNanoID}]: encryptName=${inputName}`); + console.error(error); + port.postMessage({ + status: "error", + error: error, + }); + } + } else if (action === "decryptName") { + try { + if (inputName === undefined) { + throw Error( + `worker [${workerNanoID}]: decryptName: internal inputName not transferred to worker properly` + ); + } + const outputName = await decryptNameStr(inputName); + // console.debug( + // `worker [${workerNanoID}]: after decryptNameStr, before postMessage` + // ); + port.postMessage({ + status: "ok", + outputName: outputName, + }); + } catch (error) { + console.error(`worker [${workerNanoID}]: decryptName=${inputName}`); + console.error(error); + port.postMessage({ + status: "error", + error: error, + }); + } + } else if (action === "encryptContent") { + try { + if (inputContent === undefined) { + throw Error( + `worker [${workerNanoID}]: encryptContent: internal inputContent not transferred to worker properly` + ); + } + const outputContent = await encryptContentBuf(inputContent); + // console.debug( + // `worker [${workerNanoID}]: after encryptContentBuf, before postMessage` + // ); + port.postMessage( + { + status: "ok", + outputContent: outputContent, + }, + [outputContent] // on worker side, we don't reuse anything so it's safe to transfer + ); + } catch (error) { + console.error(error); + port.postMessage({ + status: "error", + error: error, + }); + } + } else if (action === "decryptContent") { + try { + if (inputContent === undefined) { + throw Error( + `worker [${workerNanoID}]: decryptContent: internal inputContent not transferred to worker properly` + ); + } + const outputContent = await decryptContentBuf(inputContent); + // console.debug( + // `worker [${workerNanoID}]: after decryptContentBuf, before postMessage` + // ); + port.postMessage( + { + status: "ok", + outputContent: outputContent, + }, + [outputContent] // on worker side, we don't reuse anything so it's safe to transfer + ); + } catch (error) { + console.error(error); + port.postMessage({ + status: "error", + error: error, + }); + } + } else { + port.postMessage({ + status: "error", + error: `worker [${workerNanoID}]: unknown action=${action}`, + }); + } +}); diff --git a/src/fsAll.ts b/src/fsAll.ts new file mode 100644 index 0000000..61ddde3 --- /dev/null +++ b/src/fsAll.ts @@ -0,0 +1,67 @@ +import isEqual from "lodash/isEqual"; +import { nanoid } from "nanoid"; +import type { Entity } from "./baseTypes"; + +export abstract class FakeFs { + abstract kind: string; + abstract walk(): Promise; + abstract walkPartial(): Promise; + abstract stat(key: string): Promise; + abstract mkdir(key: string, mtime?: number, ctime?: number): Promise; + abstract writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise; + abstract readFile(key: string): Promise; + abstract rename(key1: string, key2: string): Promise; + abstract rm(key: string): Promise; + abstract checkConnect(callbackFunc?: any): Promise; + async checkConnectCommonOps(callbackFunc?: any) { + try { + console.info(`check connect: create folder`); + const folderName = `rs-test-folder-${nanoid()}/`; + await this.mkdir(folderName); + // await delay(3000); + + console.info(`check connect: upload file`); + const filename = `${folderName}rs-test-file-${nanoid()}`; + const ctime = Date.now(); + const mtime1 = Date.now(); + const content1 = new ArrayBuffer(100); + await this.writeFile(filename, content1, mtime1, ctime); + // await delay(3000); + + console.info(`check connect: overwrite file`); + const mtime2 = Date.now(); + const content2 = new ArrayBuffer(200); + await this.writeFile(filename, content2, mtime2, ctime); + // await delay(3000); + + console.info(`check connect: download file`); + const content3 = await this.readFile(filename); + if (!isEqual(content2, content3)) { + throw Error(`downloaded file is not equal with uploaded file!`); + } + // await delay(3000); + + console.info(`check connect: delete file`); + await this.rm(filename); + // await delay(3000); + + console.info(`check connect: delete folder`); + await this.rm(folderName); + // await delay(3000); + + return true; + } catch (err) { + console.error(err); + callbackFunc?.(err); + return false; + } + } + abstract getUserDisplayName(): Promise; + abstract revokeAuth(): Promise; + abstract allowEmptyFile(): boolean; +} diff --git a/src/fsAzureBlobStorage.ts b/src/fsAzureBlobStorage.ts new file mode 100644 index 0000000..558d266 --- /dev/null +++ b/src/fsAzureBlobStorage.ts @@ -0,0 +1,317 @@ +/** + * BYOC — Azure Blob Storage Filesystem Adapter + * Clean-room implementation using Azure Blob REST API with SAS tokens. + * No OAuth needed — users provide a Container SAS URL directly. + */ + +import type { AzureBlobStorageConfig, Entity } from "./baseTypes"; +import { DEFAULT_CONTENT_TYPE } from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { bufferToArrayBuffer } from "./misc"; + +export const DEFAULT_AZUREBLOBSTORAGE_CONFIG: AzureBlobStorageConfig = { + containerSasUrl: "", + containerName: "", + remotePrefix: "", + generateFolderObject: false, + partsConcurrency: 5, + kind: "azureblobstorage", +}; + +/** + * Parse the container SAS URL into base URL + SAS token components. + */ +function parseSasUrl(containerSasUrl: string): { + baseUrl: string; + sasToken: string; +} { + const questionIdx = containerSasUrl.indexOf("?"); + if (questionIdx === -1) { + return { baseUrl: containerSasUrl, sasToken: "" }; + } + return { + baseUrl: containerSasUrl.slice(0, questionIdx), + sasToken: containerSasUrl.slice(questionIdx), // includes the '?' + }; +} + +/** + * Build the full URL for a blob, including the SAS token. + */ +function getBlobUrl( + baseUrl: string, + sasToken: string, + prefix: string, + blobName: string +): string { + let fullPath = blobName; + if (prefix) { + fullPath = `${prefix}/${blobName}`; + } + // Encode path segments but preserve '/' + const encoded = fullPath + .split("/") + .map((seg) => encodeURIComponent(seg)) + .join("/"); + return `${baseUrl}/${encoded}${sasToken}`; +} + +/** + * Normalize a key from Azure listing to our Entity key format. + * Strips the prefix and ensures folders end with '/'. + */ +function normalizeKey(blobName: string, prefix: string): string { + let key = blobName; + if (prefix && key.startsWith(`${prefix}/`)) { + key = key.slice(prefix.length + 1); + } + return key; +} + +export class FakeFsAzureBlobStorage extends FakeFs { + kind = "azureblobstorage"; + private config: AzureBlobStorageConfig; + private vaultName: string; + private baseUrl: string; + private sasToken: string; + + constructor( + config: AzureBlobStorageConfig, + vaultName: string + ) { + super(); + this.config = config; + this.vaultName = vaultName; + const parsed = parseSasUrl(config.containerSasUrl); + this.baseUrl = parsed.baseUrl; + this.sasToken = parsed.sasToken; + } + + private getPrefix(): string { + return this.config.remotePrefix || this.vaultName; + } + + private blobUrl(blobName: string): string { + return getBlobUrl(this.baseUrl, this.sasToken, this.getPrefix(), blobName); + } + + /** + * List Blobs — Azure Blob REST API + * https://learn.microsoft.com/en-us/rest/api/storageservices/list-blobs + */ + async walk(): Promise { + const entities: Entity[] = []; + let marker = ""; + const prefix = this.getPrefix(); + + do { + let listUrl = `${this.baseUrl}${this.sasToken}&restype=container&comp=list&prefix=${encodeURIComponent(prefix ? `${prefix}/` : "")}`; + if (marker) { + listUrl += `&marker=${encodeURIComponent(marker)}`; + } + + const resp = await fetch(listUrl); + if (!resp.ok) { + throw new Error( + `[BYOC] Azure Blob list failed: ${resp.status} ${resp.statusText}` + ); + } + + const text = await resp.text(); + const parser = new DOMParser(); + const xml = parser.parseFromString(text, "text/xml"); + + // Parse blobs + const blobs = xml.querySelectorAll("Blob"); + for (const blob of blobs) { + const name = blob.querySelector("Name")?.textContent ?? ""; + const key = normalizeKey(name, prefix); + if (!key) continue; + + const propsEl = blob.querySelector("Properties"); + const contentLength = propsEl?.querySelector("Content-Length")?.textContent; + const lastModified = propsEl?.querySelector("Last-Modified")?.textContent; + const etag = propsEl?.querySelector("Etag")?.textContent; + + const size = contentLength ? parseInt(contentLength, 10) : 0; + const mtime = lastModified ? new Date(lastModified).getTime() : 0; + + // Detect "folder" markers (zero-byte blobs ending with '/') + const isFolder = key.endsWith("/"); + + entities.push({ + keyRaw: key, + key: key, + mtimeSvr: mtime, + size: isFolder ? 0 : size, + sizeRaw: isFolder ? 0 : size, + etag: etag ?? undefined, + } as Entity); + } + + // Check for continuation + const nextMarker = xml.querySelector("NextMarker")?.textContent ?? ""; + marker = nextMarker; + } while (marker); + + return entities; + } + + async walkPartial(): Promise { + // Azure Blob doesn't have a delta mechanism; full listing each time + return this.walk(); + } + + async stat(key: string): Promise { + const url = this.blobUrl(key); + const resp = await fetch(url, { method: "HEAD" }); + if (!resp.ok) { + throw new Error( + `[BYOC] Azure Blob stat failed for '${key}': ${resp.status}` + ); + } + + const size = parseInt(resp.headers.get("content-length") ?? "0", 10); + const lastModified = resp.headers.get("last-modified"); + const mtime = lastModified ? new Date(lastModified).getTime() : 0; + const etag = resp.headers.get("etag") ?? undefined; + + return { + keyRaw: key, + key: key, + mtimeSvr: mtime, + size: size, + sizeRaw: size, + etag: etag, + } as Entity; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!this.config.generateFolderObject) { + // Azure Blob doesn't need explicit folder objects; just skip + return { + keyRaw: key, + key: key, + size: 0, + sizeRaw: 0, + } as Entity; + } + + const folderKey = key.endsWith("/") ? key : `${key}/`; + const url = this.blobUrl(folderKey); + + const headers: Record = { + "x-ms-blob-type": "BlockBlob", + "Content-Length": "0", + "Content-Type": DEFAULT_CONTENT_TYPE, + }; + + const resp = await fetch(url, { + method: "PUT", + headers: headers, + body: null, + }); + + if (!resp.ok) { + throw new Error( + `[BYOC] Azure Blob mkdir failed for '${folderKey}': ${resp.status}` + ); + } + + return { + keyRaw: folderKey, + key: folderKey, + size: 0, + sizeRaw: 0, + } as Entity; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const url = this.blobUrl(key); + + const headers: Record = { + "x-ms-blob-type": "BlockBlob", + "Content-Type": DEFAULT_CONTENT_TYPE, + "Content-Length": `${content.byteLength}`, + }; + + const resp = await fetch(url, { + method: "PUT", + headers: headers, + body: content, + }); + + if (!resp.ok) { + const errText = await resp.text().catch(() => ""); + throw new Error( + `[BYOC] Azure Blob writeFile failed for '${key}': ${resp.status} ${errText}` + ); + } + + return { + keyRaw: key, + key: key, + mtimeCli: mtime, + size: content.byteLength, + sizeRaw: content.byteLength, + } as Entity; + } + + async readFile(key: string): Promise { + const url = this.blobUrl(key); + const resp = await fetch(url); + + if (!resp.ok) { + throw new Error( + `[BYOC] Azure Blob readFile failed for '${key}': ${resp.status}` + ); + } + + return await resp.arrayBuffer(); + } + + async rename(key1: string, key2: string): Promise { + // Azure Blob doesn't have a native rename; copy + delete + const content = await this.readFile(key1); + const now = Date.now(); + await this.writeFile(key2, content, now, now); + await this.rm(key1); + } + + async rm(key: string): Promise { + const url = this.blobUrl(key); + const resp = await fetch(url, { method: "DELETE" }); + + if (!resp.ok && resp.status !== 404) { + throw new Error( + `[BYOC] Azure Blob rm failed for '${key}': ${resp.status}` + ); + } + } + + async checkConnect(callbackFunc?: any): Promise { + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + if (!this.config.containerSasUrl) { + return "Azure Blob Storage (not configured)"; + } + return `Azure Blob: ${this.config.containerName || "container"}`; + } + + async revokeAuth(): Promise { + // SAS tokens don't have a revoke flow — user regenerates on Azure portal + this.config.containerSasUrl = ""; + this.config.containerName = ""; + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsBox.ts b/src/fsBox.ts new file mode 100644 index 0000000..4f2aa36 --- /dev/null +++ b/src/fsBox.ts @@ -0,0 +1,621 @@ +/** + * BYOC — Box Filesystem Adapter + * Clean-room implementation using Box Content API v2. + * OAuth2 authorization code flow with silent token refresh. + */ + +import { request, requestUrl } from "obsidian"; +import { + BOX_CLIENT_ID, + BOX_CLIENT_SECRET, + COMMAND_CALLBACK_BOX, + DEFAULT_CONTENT_TYPE, + type BoxConfig, + type Entity, +} from "./baseTypes"; +import { VALID_REQURL } from "./baseTypesObs"; +import { FakeFs } from "./fsAll"; +import { retryFetch } from "./misc"; + +const BOX_API = "https://api.box.com/2.0"; +const BOX_UPLOAD_API = "https://upload.box.com/api/2.0"; +const BOX_AUTH_URL = "https://account.box.com/api/oauth2/authorize"; +const BOX_TOKEN_URL = "https://api.box.com/oauth2/token"; +const REDIRECT_URI = `obsidian://${COMMAND_CALLBACK_BOX}`; + +export const DEFAULT_BOX_CONFIG: BoxConfig = { + accessToken: "", + accessTokenExpiresInMs: 0, + accessTokenExpiresAtTimeMs: 0, + refreshToken: "", + remoteBaseDir: "", + credentialsShouldBeDeletedAtTimeMs: 0, + kind: "box", +}; + +//////////////////////////////////////////////////////////////////////////////// +// OAuth2 Helpers +//////////////////////////////////////////////////////////////////////////////// + +export function generateAuthUrl(): string { + const params = new URLSearchParams({ + response_type: "code", + client_id: BOX_CLIENT_ID, + redirect_uri: REDIRECT_URI, + }); + return `${BOX_AUTH_URL}?${params.toString()}`; +} + +export async function sendAuthReq( + code: string, + errorCallBack: (e: any) => Promise +): Promise { + try { + const rsp = await request({ + url: BOX_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "authorization_code", + code: code, + client_id: BOX_CLIENT_ID, + client_secret: BOX_CLIENT_SECRET, + redirect_uri: REDIRECT_URI, + }).toString(), + }); + return JSON.parse(rsp); + } catch (e) { + console.error(e); + await errorCallBack(e); + } +} + +async function refreshAccessToken( + refreshToken: string +): Promise { + const rsp = await request({ + url: BOX_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: BOX_CLIENT_ID, + client_secret: BOX_CLIENT_SECRET, + }).toString(), + }); + return JSON.parse(rsp); +} + +export async function setConfigBySuccessfullAuthInplace( + config: BoxConfig, + authRes: any, + saveFunc: () => Promise +): Promise { + config.accessToken = authRes.access_token; + config.refreshToken = authRes.refresh_token; + config.accessTokenExpiresInMs = authRes.expires_in * 1000; + config.accessTokenExpiresAtTimeMs = + Date.now() + authRes.expires_in * 1000 - 300_000; + // BYOC: No forced expiry + config.credentialsShouldBeDeletedAtTimeMs = 0; + await saveFunc(); +} + +//////////////////////////////////////////////////////////////////////////////// +// Internal Helpers +//////////////////////////////////////////////////////////////////////////////// + +/** + * Box uses folder IDs, not paths. We need to resolve a path to a folder ID. + * This caches folder IDs as we discover them. + */ +class BoxPathResolver { + private cache: Map = new Map([["", "0"]]); // root = "0" + + async resolve( + path: string, + getJson: (url: string) => Promise, + createIfMissing = false, + postJson?: (url: string, body: any) => Promise + ): Promise { + if (path === "" || path === "/") return "0"; + + // Normalize: remove leading/trailing slashes + const normalized = path.replace(/^\/+|\/+$/g, ""); + if (this.cache.has(normalized)) return this.cache.get(normalized)!; + + // Walk segments + const segments = normalized.split("/"); + let parentId = "0"; + + for (let i = 0; i < segments.length; i++) { + const seg = segments[i]; + const partialPath = segments.slice(0, i + 1).join("/"); + + if (this.cache.has(partialPath)) { + parentId = this.cache.get(partialPath)!; + continue; + } + + // Search for folder in parent + const searchUrl = `${BOX_API}/folders/${parentId}/items?fields=id,name,type&limit=1000`; + const res = await getJson(searchUrl); + const entries = res.entries || []; + + const match = entries.find( + (e: any) => e.name === seg && e.type === "folder" + ); + + if (match) { + parentId = match.id; + this.cache.set(partialPath, parentId); + } else if (createIfMissing && postJson) { + // Create the folder + const created = await postJson(`${BOX_API}/folders`, { + name: seg, + parent: { id: parentId }, + }); + parentId = created.id; + this.cache.set(partialPath, parentId); + } else { + throw Error(`[BYOC] Box: folder '${seg}' not found in parent ${parentId}`); + } + } + + return parentId; + } + + set(path: string, id: string) { + this.cache.set(path.replace(/^\/+|\/+$/g, ""), id); + } +} + +function fromBoxItemToEntity(item: any, baseDirPrefix: string): Entity { + let key = ""; + + // Build the key from path_collection + if (item.path_collection?.entries) { + const parts = item.path_collection.entries + .map((e: any) => e.name) + .filter((n: string) => n !== "All Files"); + parts.push(item.name); + key = parts.join("/"); + + // Strip the base dir prefix + if (baseDirPrefix && key.startsWith(`${baseDirPrefix}/`)) { + key = key.slice(baseDirPrefix.length + 1); + } else if (key === baseDirPrefix) { + key = ""; + } + } else { + key = item.name || ""; + } + + const isFolder = item.type === "folder"; + if (isFolder && !key.endsWith("/")) { + key = `${key}/`; + } + + const mtime = item.modified_at + ? new Date(item.modified_at).getTime() + : Date.now(); + const ctime = item.created_at + ? new Date(item.created_at).getTime() + : mtime; + + return { + key: key, + keyRaw: key, + mtimeSvr: mtime, + mtimeCli: mtime, + ctimeCli: ctime, + size: isFolder ? 0 : (item.size ?? 0), + sizeRaw: isFolder ? 0 : (item.size ?? 0), + etag: item.etag, + } as Entity; +} + +//////////////////////////////////////////////////////////////////////////////// +// The Client +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsBox extends FakeFs { + kind = "box"; + private config: BoxConfig; + private vaultName: string; + private saveFunc: () => Promise; + private pathResolver: BoxPathResolver; + private remoteBaseDir: string; + + constructor( + config: BoxConfig, + vaultName: string, + saveFunc: () => Promise + ) { + super(); + this.config = config; + this.vaultName = vaultName; + this.saveFunc = saveFunc; + this.pathResolver = new BoxPathResolver(); + this.remoteBaseDir = config.remoteBaseDir || vaultName || ""; + } + + private async ensureToken(): Promise { + if (!this.config.accessToken || !this.config.refreshToken) { + throw Error("[BYOC] Box: user has not authorized yet."); + } + + if (this.config.accessTokenExpiresAtTimeMs > Date.now()) { + return this.config.accessToken; + } + + // Refresh + const res = await refreshAccessToken(this.config.refreshToken); + if (res.error) { + throw Error(`[BYOC] Box refresh error: ${res.error_description}`); + } + this.config.accessToken = res.access_token; + this.config.refreshToken = res.refresh_token; + this.config.accessTokenExpiresInMs = res.expires_in * 1000; + this.config.accessTokenExpiresAtTimeMs = + Date.now() + res.expires_in * 1000 - 300_000; + await this.saveFunc(); + return this.config.accessToken; + } + + private async _getJson(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${BOX_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "GET", + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _postJson(url: string, body: any): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${BOX_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "POST", + contentType: "application/json", + body: JSON.stringify(body), + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _putJson(url: string, body: any): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${BOX_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "PUT", + contentType: "application/json", + body: JSON.stringify(body), + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _delete(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${BOX_API}${url}`; + await retryFetch(fullUrl, { + method: "DELETE", + headers: { Authorization: `Bearer ${token}` }, + }); + } + + private async getBaseFolderId(): Promise { + return this.pathResolver.resolve( + this.remoteBaseDir, + (u) => this._getJson(u), + true, + (u, b) => this._postJson(u, b) + ); + } + + /** + * Recursively list all items in a Box folder. + */ + private async listFolder( + folderId: string, + pathPrefix: string + ): Promise { + const items: any[] = []; + let offset = 0; + const limit = 1000; + + while (true) { + const res = await this._getJson( + `${BOX_API}/folders/${folderId}/items?fields=id,name,type,size,modified_at,created_at,etag,path_collection&limit=${limit}&offset=${offset}` + ); + const entries = res.entries || []; + for (const entry of entries) { + items.push(entry); + if (entry.type === "folder") { + const subItems = await this.listFolder( + entry.id, + `${pathPrefix}${entry.name}/` + ); + items.push(...subItems); + } + } + offset += entries.length; + if (offset >= (res.total_count ?? entries.length)) break; + if (entries.length === 0) break; + } + + return items; + } + + async walk(): Promise { + const baseFolderId = await this.getBaseFolderId(); + const items = await this.listFolder(baseFolderId, ""); + + return items.map((item) => { + const isFolder = item.type === "folder"; + // Build key from the item — we need to resolve relative to our base + let key = ""; + if (item.path_collection?.entries) { + const parts = item.path_collection.entries.map((e: any) => e.name); + parts.push(item.name); + // Remove "All Files" and the base dir prefix + const fullPath = parts.filter((n: string) => n !== "All Files").join("/"); + if (fullPath.startsWith(`${this.remoteBaseDir}/`)) { + key = fullPath.slice(this.remoteBaseDir.length + 1); + } else { + key = item.name; + } + } else { + key = item.name; + } + + if (isFolder && !key.endsWith("/")) key = `${key}/`; + + const mtime = item.modified_at ? new Date(item.modified_at).getTime() : 0; + const ctime = item.created_at ? new Date(item.created_at).getTime() : mtime; + + return { + key, keyRaw: key, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: isFolder ? 0 : (item.size ?? 0), + sizeRaw: isFolder ? 0 : (item.size ?? 0), + etag: item.etag, + } as Entity; + }); + } + + async walkPartial(): Promise { + return this.walk(); + } + + async stat(key: string): Promise { + const isFolder = key.endsWith("/"); + if (isFolder) { + const folderId = await this.pathResolver.resolve( + `${this.remoteBaseDir}/${key.replace(/\/+$/, "")}`, + (u) => this._getJson(u) + ); + const res = await this._getJson( + `${BOX_API}/folders/${folderId}?fields=id,name,type,size,modified_at,created_at` + ); + return fromBoxItemToEntity(res, this.remoteBaseDir); + } + + // For files, find by navigating to parent + searching + const parentPath = key.includes("/") + ? key.slice(0, key.lastIndexOf("/")) + : ""; + const fileName = key.includes("/") + ? key.slice(key.lastIndexOf("/") + 1) + : key; + + const parentFolderId = await this.pathResolver.resolve( + parentPath ? `${this.remoteBaseDir}/${parentPath}` : this.remoteBaseDir, + (u) => this._getJson(u) + ); + + const res = await this._getJson( + `${BOX_API}/folders/${parentFolderId}/items?fields=id,name,type,size,modified_at,created_at,etag` + ); + const match = (res.entries || []).find( + (e: any) => e.name === fileName && e.type === "file" + ); + if (!match) throw Error(`[BYOC] Box: file '${key}' not found`); + + return { + key, keyRaw: key, + mtimeSvr: match.modified_at ? new Date(match.modified_at).getTime() : 0, + mtimeCli: match.modified_at ? new Date(match.modified_at).getTime() : 0, + size: match.size ?? 0, + sizeRaw: match.size ?? 0, + etag: match.etag, + } as Entity; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + const fullPath = `${this.remoteBaseDir}/${key.replace(/\/+$/, "")}`; + await this.pathResolver.resolve( + fullPath, + (u) => this._getJson(u), + true, + (u, b) => this._postJson(u, b) + ); + return { key, keyRaw: key, size: 0, sizeRaw: 0 } as Entity; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const parentPath = key.includes("/") + ? key.slice(0, key.lastIndexOf("/")) + : ""; + const fileName = key.includes("/") + ? key.slice(key.lastIndexOf("/") + 1) + : key; + + const parentFolderId = await this.pathResolver.resolve( + parentPath ? `${this.remoteBaseDir}/${parentPath}` : this.remoteBaseDir, + (u) => this._getJson(u), + true, + (u, b) => this._postJson(u, b) + ); + + const token = await this.ensureToken(); + + // Check if file already exists (for overwrite) + let existingFileId: string | null = null; + try { + const res = await this._getJson( + `${BOX_API}/folders/${parentFolderId}/items?fields=id,name,type&limit=1000` + ); + const match = (res.entries || []).find( + (e: any) => e.name === fileName && e.type === "file" + ); + if (match) existingFileId = match.id; + } catch { + // ignore + } + + if (existingFileId) { + // Upload new version + const formData = new FormData(); + formData.append("file", new Blob([content]), fileName); + const resp = await retryFetch( + `${BOX_UPLOAD_API}/files/${existingFileId}/content`, + { + method: "POST", + headers: { Authorization: `Bearer ${token}` }, + body: formData, + } + ); + if (!resp.ok) { + throw Error(`[BYOC] Box: upload new version failed: ${resp.status}`); + } + } else { + // Upload new file + const formData = new FormData(); + formData.append( + "attributes", + JSON.stringify({ name: fileName, parent: { id: parentFolderId } }) + ); + formData.append("file", new Blob([content]), fileName); + const resp = await retryFetch(`${BOX_UPLOAD_API}/files/content`, { + method: "POST", + headers: { Authorization: `Bearer ${token}` }, + body: formData, + }); + if (!resp.ok) { + throw Error(`[BYOC] Box: upload new file failed: ${resp.status}`); + } + } + + return { + key, keyRaw: key, + mtimeCli: mtime, ctimeCli: ctime, + size: content.byteLength, + sizeRaw: content.byteLength, + } as Entity; + } + + async readFile(key: string): Promise { + // Find file ID first + const parentPath = key.includes("/") + ? key.slice(0, key.lastIndexOf("/")) + : ""; + const fileName = key.includes("/") + ? key.slice(key.lastIndexOf("/") + 1) + : key; + + const parentFolderId = await this.pathResolver.resolve( + parentPath ? `${this.remoteBaseDir}/${parentPath}` : this.remoteBaseDir, + (u) => this._getJson(u) + ); + + const res = await this._getJson( + `${BOX_API}/folders/${parentFolderId}/items?fields=id,name,type&limit=1000` + ); + const match = (res.entries || []).find( + (e: any) => e.name === fileName && e.type === "file" + ); + if (!match) throw Error(`[BYOC] Box: file '${key}' not found for read`); + + const token = await this.ensureToken(); + const resp = await retryFetch(`${BOX_API}/files/${match.id}/content`, { + headers: { Authorization: `Bearer ${token}` }, + redirect: "follow", + }); + if (!resp.ok) { + throw Error(`[BYOC] Box: readFile failed: ${resp.status}`); + } + return resp.arrayBuffer(); + } + + async rename(key1: string, key2: string): Promise { + // Box rename = update name on file/folder object + const content = await this.readFile(key1); + const now = Date.now(); + await this.writeFile(key2, content, now, now); + await this.rm(key1); + } + + async rm(key: string): Promise { + const isFolder = key.endsWith("/"); + const parentPath = key.includes("/") + ? key.slice(0, key.lastIndexOf("/") || undefined).replace(/\/+$/, "") + : ""; + const name = isFolder + ? key.replace(/\/+$/, "").split("/").pop()! + : key.split("/").pop()!; + + const parentFolderId = await this.pathResolver.resolve( + parentPath ? `${this.remoteBaseDir}/${parentPath}` : this.remoteBaseDir, + (u) => this._getJson(u) + ); + + const res = await this._getJson( + `${BOX_API}/folders/${parentFolderId}/items?fields=id,name,type&limit=1000` + ); + const itemType = isFolder ? "folder" : "file"; + const match = (res.entries || []).find( + (e: any) => e.name === name && e.type === itemType + ); + if (!match) return; // Already gone + + if (isFolder) { + await this._delete(`${BOX_API}/folders/${match.id}?recursive=true`); + } else { + await this._delete(`${BOX_API}/files/${match.id}`); + } + } + + async checkConnect(callbackFunc?: any): Promise { + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + const res = await this._getJson(`${BOX_API}/users/me?fields=name`); + return res.name || "Box User"; + } + + async revokeAuth(): Promise { + this.config.accessToken = ""; + this.config.refreshToken = ""; + await this.saveFunc(); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsDropbox.ts b/src/fsDropbox.ts new file mode 100644 index 0000000..a158a9e --- /dev/null +++ b/src/fsDropbox.ts @@ -0,0 +1,772 @@ +import { Dropbox, DropboxAuth } from "dropbox"; +import type { DropboxResponse, DropboxResponseError, files } from "dropbox"; +import random from "lodash/random"; +import { + COMMAND_CALLBACK_DROPBOX, + DROPBOX_APP_KEY, + type DropboxConfig, + type Entity, +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { + bufferToArrayBuffer, + delay, + getFolderLevels, + getParentFolder, + hasEmojiInText, + headersToRecord, +} from "./misc"; + +export { Dropbox } from "dropbox"; + +export const DEFAULT_DROPBOX_CONFIG: DropboxConfig = { + accessToken: "", + clientID: DROPBOX_APP_KEY ?? "", + refreshToken: "", + accessTokenExpiresInSeconds: 0, + accessTokenExpiresAtTime: 0, + accountID: "", + username: "", + credentialsShouldBeDeletedAtTime: 0, +}; + +const getDropboxPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + // special + key = `/${remoteBaseDir}`; + } else if (fileOrFolderPath.startsWith("/")) { + console.warn( + `why the path ${fileOrFolderPath} starts with '/'? but we just go on.` + ); + key = `/${remoteBaseDir}${fileOrFolderPath}`; + } else { + key = `/${remoteBaseDir}/${fileOrFolderPath}`; + } + if (key.endsWith("/")) { + key = key.slice(0, key.length - 1); + } + return key; +}; + +const getNormPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + if ( + !( + fileOrFolderPath === `/${remoteBaseDir}` || + fileOrFolderPath.startsWith(`/${remoteBaseDir}/`) + ) + ) { + throw Error( + `"${fileOrFolderPath}" doesn't starts with "/${remoteBaseDir}/"` + ); + } + return fileOrFolderPath.slice(`/${remoteBaseDir}/`.length); +}; + +const fromDropboxItemToEntity = ( + x: + | files.FileMetadataReference + | files.FolderMetadataReference + | files.DeletedMetadataReference, + remoteBaseDir: string +): Entity => { + let key = getNormPath(x.path_display!, remoteBaseDir); + if (x[".tag"] === "folder" && !key.endsWith("/")) { + key = `${key}/`; + } + + if (x[".tag"] === "folder") { + return { + key: key, + keyRaw: key, + size: 0, + sizeRaw: 0, + } as Entity; + } else if (x[".tag"] === "file") { + const mtimeCli = Date.parse(x.client_modified).valueOf(); + const mtimeSvr = Date.parse(x.server_modified).valueOf(); + return { + key: key, + keyRaw: key, + mtimeCli: mtimeCli, + mtimeSvr: mtimeSvr, + size: x.size, + sizeRaw: x.size, + hash: x.content_hash, + } as Entity; + } else { + // x[".tag"] === "deleted" + throw Error("do not support deleted tag"); + } +}; + +/** + * https://github.com/remotely-save/remotely-save/issues/567 + * https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Case-Sensitivity-in-API-2/td-p/191279 + * @param entities + */ +export const fixEntityListCasesInplace = (entities: { key?: string }[]) => { + for (const iterator of entities) { + if (iterator.key === undefined) { + throw Error(`dropbox list should all have key, but meet undefined`); + } + } + + entities.sort((a, b) => a.key!.length - b.key!.length); + // console.log(JSON.stringify(entities,null,2)); + + const caseMapping: Record = { "": "" }; + for (const e of entities) { + // console.log(`looking for: ${JSON.stringify(e, null, 2)}`); + + let parentFolder = getParentFolder(e.key!); + if (parentFolder === "/") { + parentFolder = ""; + } + const parentFolderLower = parentFolder.toLocaleLowerCase(); + const segs = e.key!.split("/"); + if (e.key!.endsWith("/")) { + // folder + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKey = `${caseMapping[parentFolderLower]}${segs + .slice(-2) + .join("/")}`; + caseMapping[newKey.toLocaleLowerCase()] = newKey; + e.key = newKey; + // console.log(JSON.stringify(caseMapping,null,2)); + // continue; + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } else { + // file + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKey = `${caseMapping[parentFolderLower]}${segs + .slice(-1) + .join("/")}`; + e.key = newKey; + // continue; + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } + } + + return entities; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Other usual common methods +//////////////////////////////////////////////////////////////////////////////// + +interface ErrSubType { + error: { + retry_after: number; + }; +} + +async function retryReq( + reqFunc: () => Promise>, + extraHint = "" +): Promise | undefined> { + const waitSeconds = [1, 2, 4, 8]; // hard code exponential backoff + for (let idx = 0; idx < waitSeconds.length; ++idx) { + try { + if (idx !== 0) { + console.warn( + `${extraHint === "" ? "" : extraHint + ": "}The ${ + idx + 1 + }-th try starts at time ${Date.now()}` + ); + } + return await reqFunc(); + } catch (e: unknown) { + const err = e as DropboxResponseError; + if (err.status === undefined) { + // then the err is not DropboxResponseError + throw err; + } + if (err.status !== 429) { + // then the err is not "too many requests", give up + throw err; + } + + if (idx === waitSeconds.length - 1) { + // the last retry also failed, give up + throw new Error( + `${ + extraHint === "" ? "" : extraHint + ": " + }"429 too many requests", after retrying for ${ + idx + 1 + } times still failed.` + ); + } + + const headers = headersToRecord(err.headers); + const svrSec = + err.error.error.retry_after || + Number.parseInt(headers["retry-after"] || "1") || + 1; + const fallbackSec = waitSeconds[idx]; + const secMin = Math.max(svrSec, fallbackSec); + const secMax = Math.max(secMin * 1.8, 2); + console.warn( + `${ + extraHint === "" ? "" : extraHint + ": " + }We have "429 too many requests" error of ${ + idx + 1 + }-th try, at time ${Date.now()}, and wait for ${secMin} ~ ${secMax} seconds to retry. Original info: ${JSON.stringify( + err.error, + null, + 2 + )}` + ); + await delay(random(secMin * 1000, secMax * 1000)); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Dropbox authorization using PKCE +// see https://dropbox.tech/developers/pkce--what-and-why- +//////////////////////////////////////////////////////////////////////////////// + +export const getAuthUrlAndVerifier = async ( + appKey: string, + needManualPatse = false +) => { + const auth = new DropboxAuth({ + clientId: appKey, + }); + + const callback = needManualPatse + ? undefined + : `obsidian://${COMMAND_CALLBACK_DROPBOX}`; + const authUrl = ( + await auth.getAuthenticationUrl( + callback as any, + undefined, + "code", + "offline", + undefined, + "none", + true + ) + ).toString(); + const verifier = auth.getCodeVerifier(); + return { + authUrl: authUrl, + verifier: verifier, + }; +}; + +export interface DropboxSuccessAuthRes { + access_token: string; + token_type: "bearer"; + expires_in: string; + refresh_token?: string; + scope?: string; + uid?: string; + account_id?: string; +} + +export const sendAuthReq = async ( + appKey: string, + verifier: string, + authCode: string, + errorCallBack: any +) => { + try { + const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { + method: "POST", + body: new URLSearchParams({ + code: authCode, + grant_type: "authorization_code", + code_verifier: verifier, + client_id: appKey, + redirect_uri: `obsidian://${COMMAND_CALLBACK_DROPBOX}`, + }), + }); + const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; + return resp2; + } catch (e) { + console.error(e); + if (errorCallBack !== undefined) { + await errorCallBack(e); + } + } +}; + +export const sendRefreshTokenReq = async ( + appKey: string, + refreshToken: string +) => { + try { + console.info("start auto getting refreshed Dropbox access token."); + const resp1 = await fetch("https://api.dropboxapi.com/oauth2/token", { + method: "POST", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: appKey, + }), + }); + const resp2 = (await resp1.json()) as DropboxSuccessAuthRes; + console.info("finish auto getting refreshed Dropbox access token."); + return resp2; + } catch (e) { + console.error(e); + throw e; + } +}; + +export const setConfigBySuccessfullAuthInplace = async ( + config: DropboxConfig, + authRes: DropboxSuccessAuthRes, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + console.info("start updating local info of Dropbox token"); + + config.accessToken = authRes.access_token; + config.accessTokenExpiresInSeconds = Number.parseInt(authRes.expires_in); + config.accessTokenExpiresAtTime = + Date.now() + Number.parseInt(authRes.expires_in) * 1000 - 10 * 1000; + + // BYOC: No forced expiry — credentials persist until revoked by user + config.credentialsShouldBeDeletedAtTime = 0; + + if (authRes.refresh_token !== undefined) { + config.refreshToken = authRes.refresh_token; + config.accountID = authRes.account_id!; + } + + if (saveUpdatedConfigFunc !== undefined) { + await saveUpdatedConfigFunc(); + } + + console.info("finish updating local info of Dropbox token"); +}; + +//////////////////////////////////////////////////////////////////////////////// +// real exported interface +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsDropbox extends FakeFs { + kind: "dropbox"; + dropboxConfig: DropboxConfig; + remoteBaseDir: string; + saveUpdatedConfigFunc: () => Promise; + dropbox!: Dropbox; + vaultFolderExists: boolean; + foldersCreatedBefore: Set; + + constructor( + dropboxConfig: DropboxConfig, + vaultName: string, + saveUpdatedConfigFunc: () => Promise + ) { + super(); + this.kind = "dropbox"; + this.dropboxConfig = dropboxConfig; + this.remoteBaseDir = this.dropboxConfig.remoteBaseDir || vaultName || ""; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + this.vaultFolderExists = false; + this.foldersCreatedBefore = new Set(); + } + + async _init() { + // check token + if ( + this.dropboxConfig.accessToken === "" || + this.dropboxConfig.refreshToken === "" + ) { + throw Error("The user has not manually auth yet."); + } + const currentTs = Date.now(); + const customHeaders = { + "Cache-Control": "no-cache", + }; + if (this.dropboxConfig.accessTokenExpiresAtTime > currentTs) { + this.dropbox = new Dropbox({ + accessToken: this.dropboxConfig.accessToken, + customHeaders: customHeaders, + }); + } else { + if (this.dropboxConfig.refreshToken === "") { + throw Error( + "We need to automatically refresh token but none is stored." + ); + } + const resp = await sendRefreshTokenReq( + this.dropboxConfig.clientID, + this.dropboxConfig.refreshToken + ); + + setConfigBySuccessfullAuthInplace( + this.dropboxConfig, + resp, + this.saveUpdatedConfigFunc + ); + this.dropbox = new Dropbox({ + accessToken: this.dropboxConfig.accessToken, + customHeaders: customHeaders, + }); + } + + // check vault folder + // console.info(`checking remote has folder /${this.remoteBaseDir}`); + if (this.vaultFolderExists) { + // console.info(`already checked, /${this.remoteBaseDir} exist before`) + } else { + const res = await this.dropbox.filesListFolder({ + path: "", + recursive: false, + }); + for (const item of res.result.entries) { + if (item.path_display === `/${this.remoteBaseDir}`) { + this.vaultFolderExists = true; + break; + } + } + if (!this.vaultFolderExists) { + console.info(`remote does not have folder /${this.remoteBaseDir}`); + + if (hasEmojiInText(`/${this.remoteBaseDir}`)) { + throw new Error( + `/${this.remoteBaseDir}: Error: Dropbox does not support emoji in folder names.` + ); + } + + await this.dropbox.filesCreateFolderV2({ + path: `/${this.remoteBaseDir}`, + }); + console.info(`remote folder /${this.remoteBaseDir} created`); + this.vaultFolderExists = true; + } else { + // console.info(`remote folder /${this.remoteBaseDir} exists`); + } + } + + return this; + } + + async walk(): Promise { + return await this._walk(false); + } + + async walkPartial(): Promise { + return await this._walk(true); + } + + async _walk(partial: boolean): Promise { + await this._init(); + + let res = await this.dropbox.filesListFolder({ + path: `/${this.remoteBaseDir}`, + recursive: !partial, + include_deleted: false, + limit: partial ? 10 : 1000, + }); + if (res.status !== 200) { + throw Error(JSON.stringify(res)); + } + // console.info(res); + + const contents = res.result.entries; + const unifiedContents = contents + .filter((x) => x[".tag"] !== "deleted") + .filter((x) => x.path_display !== `/${this.remoteBaseDir}`) + .map((x) => fromDropboxItemToEntity(x, this.remoteBaseDir)); + + if (!partial) { + while (res.result.has_more) { + res = await this.dropbox.filesListFolderContinue({ + cursor: res.result.cursor, + }); + if (res.status !== 200) { + throw Error(JSON.stringify(res)); + } + + const contents2 = res.result.entries; + const unifiedContents2 = contents2 + .filter((x) => x[".tag"] !== "deleted") + .filter((x) => x.path_display !== `/${this.remoteBaseDir}`) + .map((x) => fromDropboxItemToEntity(x, this.remoteBaseDir)); + unifiedContents.push(...unifiedContents2); + } + } + + fixEntityListCasesInplace(unifiedContents); + + return unifiedContents; + } + + async stat(key: string): Promise { + await this._init(); + return await this._statFromRoot(getDropboxPath(key, this.remoteBaseDir)); + } + + async _statFromRoot(key: string): Promise { + // if (key === "" || key === "/") { + // // filesGetMetadata doesn't support root folder + // // we instead try to list files + // // if no error occurs, we ensemble a fake result. + // const rsp = await retryReq(() => + // client.dropbox.filesListFolder({ + // path: `/${client.key}`, + // recursive: false, // don't need to recursive here + // }) + // ); + // if (rsp.status !== 200) { + // throw Error(JSON.stringify(rsp)); + // } + // return { + // key: remotePath, + // lastModified: undefined, + // size: 0, + // remoteType: "dropbox", + // etag: undefined, + // } as Entity; + // } + + const rsp = await retryReq(() => + this.dropbox.filesGetMetadata({ + path: key, + }) + ); + if (rsp === undefined) { + throw Error("dropbox.filesGetMetadata undefinded"); + } + if (rsp.status !== 200) { + throw Error(JSON.stringify(rsp)); + } + return fromDropboxItemToEntity(rsp.result, this.remoteBaseDir); + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!key.endsWith("/")) { + throw Error(`you should not call mkdir on ${key}`); + } + await this._init(); + + const uploadFile = getDropboxPath(key, this.remoteBaseDir); + + return await this._mkdirFromRoot(uploadFile, mtime, ctime); + } + + async _mkdirFromRoot( + key: string, + mtime?: number, + ctime?: number + ): Promise { + if (hasEmojiInText(key)) { + throw new Error( + `${key}: Error: Dropbox does not support emoji in file / folder names.` + ); + } + if (this.foldersCreatedBefore?.has(key)) { + // created, pass + } else { + try { + await retryReq( + () => + this.dropbox.filesCreateFolderV2({ + path: key, + }), + key // just a hint + ); + this.foldersCreatedBefore?.add(key); + } catch (e: unknown) { + const err = e as DropboxResponseError; + if (err.status === undefined) { + throw err; + } + if (err.status === 409) { + // pass + this.foldersCreatedBefore?.add(key); + } else { + throw err; + } + } + } + return await this._statFromRoot(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (key.endsWith("/")) { + throw Error(`you should not call writeFile on ${key}`); + } + + await this._init(); + const uploadFile = getDropboxPath(key, this.remoteBaseDir); + + return await this._writeFileFromRoot( + uploadFile, + content, + mtime, + ctime, + key + ); + } + + async _writeFileFromRoot( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + if (hasEmojiInText(origKey)) { + throw new Error( + `${origKey}: Error: Dropbox does not support emoji in file / folder names.` + ); + } + + const mtimeFixed = Math.floor(mtime / 1000.0) * 1000; + const ctimeFixed = Math.floor(ctime / 1000.0) * 1000; + const mtimeStr = new Date(mtimeFixed) + .toISOString() + .replace(/\.\d{3}Z$/, "Z"); + + // in dropbox, we don't need to create folders before uploading! cool! + // TODO: filesUploadSession for larger files (>=150 MB) + + await retryReq( + () => + this.dropbox.filesUpload({ + path: key, + contents: content, + mode: { + ".tag": "overwrite", + }, + client_modified: mtimeStr, + }), + origKey // hint + ); + + // we want to mark that parent folders are created + if (this.foldersCreatedBefore !== undefined) { + const dirs = getFolderLevels(origKey).map((x) => + getDropboxPath(x, this.remoteBaseDir) + ); + for (const dir of dirs) { + this.foldersCreatedBefore?.add(dir); + } + } + return await this._statFromRoot(key); + } + + async readFile(key: string): Promise { + await this._init(); + if (key.endsWith("/")) { + throw new Error(`you should not call readFile on folder ${key}`); + } + const downloadFile = getDropboxPath(key, this.remoteBaseDir); + return await this._readFileFromRoot(downloadFile); + } + + async _readFileFromRoot(key: string): Promise { + const rsp = await retryReq( + () => + this.dropbox.filesDownload({ + path: key, + }), + `downloadFromRemoteRaw=${key}` + ); + if (rsp === undefined) { + throw Error(`unknown rsp from dropbox download: ${rsp}`); + } + if ((rsp.result as any).fileBlob !== undefined) { + // we get a Blob + const content = (rsp.result as any).fileBlob as Blob; + return await content.arrayBuffer(); + } else if ((rsp.result as any).fileBinary !== undefined) { + // we get a Buffer + const content = (rsp.result as any).fileBinary as Buffer; + return bufferToArrayBuffer(content); + } else { + throw Error(`unknown rsp from dropbox download: ${rsp}`); + } + } + + async rename(key1: string, key2: string): Promise { + const remoteFileName1 = getDropboxPath(key1, this.remoteBaseDir); + const remoteFileName2 = getDropboxPath(key2, this.remoteBaseDir); + await this._init(); + try { + await retryReq( + () => + this.dropbox.filesMoveV2({ + from_path: remoteFileName1, + to_path: remoteFileName2, + }), + `${key1}=>${key2}` // just a hint here + ); + } catch (err) { + console.error("some error while moving"); + console.error(err); + } + } + + async rm(key: string): Promise { + if (key === "/") { + return; + } + const remoteFileName = getDropboxPath(key, this.remoteBaseDir); + + await this._init(); + try { + await retryReq( + () => + this.dropbox.filesDeleteV2({ + path: remoteFileName, + }), + key // just a hint here + ); + } catch (err) { + console.error("some error while deleting"); + console.error(err); + } + } + + async checkConnect(callbackFunc?: any): Promise { + try { + await this._init(); + const results = await this._statFromRoot(`/${this.remoteBaseDir}`); + if (results === undefined) { + throw Error(`cannot check root vault folder!`); + } + } catch (err) { + console.debug(err); + callbackFunc?.(err); + return false; + } + + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName() { + await this._init(); + const acct = await this.dropbox.usersGetCurrentAccount(); + return acct.result.name.display_name; + } + + async revokeAuth() { + try { + await this._init(); + await this.dropbox.authTokenRevoke(); + return true; + } catch (e) { + return false; + } + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsEncrypt.ts b/src/fsEncrypt.ts new file mode 100644 index 0000000..097a1d0 --- /dev/null +++ b/src/fsEncrypt.ts @@ -0,0 +1,587 @@ +import type { CipherMethodType, Entity } from "./baseTypes"; +import * as openssl from "./encryptOpenSSL"; +import * as rclone from "./encryptRClone"; +import { isSpecialFolderNameToSkip, isVaildText } from "./misc"; + +import cloneDeep from "lodash/cloneDeep"; +import { FakeFs } from "./fsAll"; + +/** + * quick guess, no actual decryption here + * @param name + * @returns + */ +function isLikelyOpenSSLEncryptedName(name: string): boolean { + if ( + name.startsWith(openssl.MAGIC_ENCRYPTED_PREFIX_BASE32) || + name.startsWith(openssl.MAGIC_ENCRYPTED_PREFIX_BASE64URL) + ) { + return true; + } + return false; +} + +/** + * quick guess, no actual decryption here + * @param name + * @returns + */ +function isLikelyEncryptedName(name: string): boolean { + return isLikelyOpenSSLEncryptedName(name); +} + +/** + * quick guess, no actual decryption here, only openssl can be guessed here + * @param name + * @returns + */ +function isLikelyEncryptedNameNotMatchMethod( + name: string, + method: CipherMethodType +): boolean { + if (isLikelyOpenSSLEncryptedName(name) && method !== "openssl-base64") { + return true; + } + if (!isLikelyOpenSSLEncryptedName(name) && method === "openssl-base64") { + return true; + } + return false; +} + +export interface PasswordCheckType { + ok: boolean; + reason: + | "empty_remote" + | "unknown_encryption_method" + | "remote_encrypted_local_no_password" + | "password_matched" + | "password_or_method_not_matched_or_remote_not_encrypted" + | "likely_no_password_both_sides" + | "encryption_method_not_matched"; +} + +/** + * Useful if isPasswordEmpty() + */ +function copyEntityAndCopyKeyEncSizeEnc(entity: Entity) { + const res = cloneDeep(entity); + res["keyEnc"] = res["keyRaw"]; + res["sizeEnc"] = res["sizeRaw"]; + return res; +} + +export class FakeFsEncrypt extends FakeFs { + innerFs: FakeFs; + readonly password: string; + readonly method: CipherMethodType; + cipherRClone?: rclone.CipherRclone; + cacheMapOrigToEnc: Record; + hasCacheMap: boolean; + kind: string; + + constructor(innerFs: FakeFs, password: string, method: CipherMethodType) { + super(); + this.innerFs = innerFs; + this.password = password ?? ""; + this.method = method; + this.cacheMapOrigToEnc = {}; + this.hasCacheMap = false; + + this.kind = `encrypt(${this.innerFs.kind},${ + this.password !== "" ? method : "no password" + })`; + + if (this.password !== "" && method === "rclone-base64") { + // no need to init if no password or not rclone + this.cipherRClone = new rclone.CipherRclone(password, 5); + } + } + + isPasswordEmpty() { + return this.password === ""; + } + + isFolderAware() { + if (this.method === "openssl-base64") { + return false; + } + if (this.method === "rclone-base64") { + return true; + } + throw Error(`no idea about isFolderAware for method=${this.method}`); + } + + async isPasswordOk(): Promise { + const innerWalkResult = await this.walkPartial(); + + if (innerWalkResult === undefined || innerWalkResult.length === 0) { + // remote empty + return { + ok: true, + reason: "empty_remote", + }; + } + const santyCheckKey = innerWalkResult[0].keyRaw; + + if (this.isPasswordEmpty()) { + // TODO: no way to distinguish remote rclone encrypted + // if local has no password?? + if (isLikelyEncryptedName(santyCheckKey)) { + return { + ok: false, + reason: "remote_encrypted_local_no_password", + }; + } else { + return { + ok: true, + reason: "likely_no_password_both_sides", + }; + } + } else { + // the config has a password + + if (this.method === "unknown") { + return { + ok: false, + reason: "unknown_encryption_method", + }; + } + if (isLikelyEncryptedNameNotMatchMethod(santyCheckKey, this.method)) { + return { + ok: false, + reason: "encryption_method_not_matched", + }; + } + try { + const k = await this._decryptName(santyCheckKey); + if (k === undefined) { + throw Error(`decryption failed`); + } + return { + ok: true, + reason: "password_matched", + }; + } catch (error) { + return { + ok: false, + reason: "password_or_method_not_matched_or_remote_not_encrypted", + }; + } + } + } + + async walk(): Promise { + const innerWalkResult = await this.innerFs.walk(); + return await this._dealWithWalk(innerWalkResult); + } + + async walkPartial(): Promise { + const innerWalkResult = await this.innerFs.walkPartial(); + return await this._dealWithWalk(innerWalkResult); + } + + async _dealWithWalk(innerWalkResult: Entity[]): Promise { + const res: Entity[] = []; + + if (this.isPasswordEmpty()) { + for (const innerEntity of innerWalkResult) { + res.push(copyEntityAndCopyKeyEncSizeEnc(innerEntity)); + this.cacheMapOrigToEnc[innerEntity.key!] = innerEntity.key!; + } + this.hasCacheMap = true; + return res; + } else { + for (const innerEntity of innerWalkResult) { + if (isSpecialFolderNameToSkip(innerEntity.keyRaw, [])) { + continue; + } + + const key = await this._decryptName(innerEntity.keyRaw); + const size = key.endsWith("/") ? 0 : undefined; + res.push({ + key: key, + keyRaw: innerEntity.keyRaw, + keyEnc: innerEntity.key!, + mtimeCli: innerEntity.mtimeCli, + mtimeSvr: innerEntity.mtimeSvr, + size: size, + sizeEnc: innerEntity.size!, + sizeRaw: innerEntity.sizeRaw, + hash: undefined, + synthesizedFolder: innerEntity.synthesizedFolder, + }); + + this.cacheMapOrigToEnc[key] = innerEntity.keyRaw; + } + this.hasCacheMap = true; + return res; + } + } + + async stat(key: string): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for stat"); + } + const keyEnc = this.cacheMapOrigToEnc[key]; + if (keyEnc === undefined) { + throw new Error(`no encrypted key ${key} before!`); + } + + const innerEntity = await this.innerFs.stat(keyEnc); + if (this.isPasswordEmpty()) { + return copyEntityAndCopyKeyEncSizeEnc(innerEntity); + } else { + return { + key: key, + keyRaw: innerEntity.keyRaw, + keyEnc: innerEntity.key!, + mtimeCli: innerEntity.mtimeCli, + mtimeSvr: innerEntity.mtimeSvr, + size: undefined, + sizeEnc: innerEntity.size!, + sizeRaw: innerEntity.sizeRaw, + hash: undefined, + synthesizedFolder: innerEntity.synthesizedFolder, + }; + } + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for mkdir"); + } + + if (!key.endsWith("/")) { + throw new Error(`should not call mkdir on ${key}`); + } + + let keyEnc = this.cacheMapOrigToEnc[key]; + if (keyEnc === undefined) { + if (this.isPasswordEmpty()) { + keyEnc = key; + } else { + keyEnc = await this._encryptName(key); + } + this.cacheMapOrigToEnc[key] = keyEnc; + } + + if (this.isPasswordEmpty() || this.isFolderAware()) { + const innerEntity = await this.innerFs.mkdir(keyEnc, mtime, ctime); + return copyEntityAndCopyKeyEncSizeEnc(innerEntity); + } else { + const now = Date.now(); + let content = new ArrayBuffer(0); + if (!this.innerFs.allowEmptyFile()) { + content = new ArrayBuffer(1); + } + const innerEntity = await this.innerFs.writeFile( + keyEnc, + content, + mtime ?? now, + ctime ?? now + ); + return { + key: key, + keyRaw: innerEntity.keyRaw, + keyEnc: innerEntity.key!, + mtimeCli: innerEntity.mtimeCli, + mtimeSvr: innerEntity.mtimeSvr, + size: 0, + sizeEnc: innerEntity.size!, + sizeRaw: innerEntity.sizeRaw, + hash: undefined, + synthesizedFolder: innerEntity.synthesizedFolder, + }; + } + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for readFile"); + } + let keyEnc = this.cacheMapOrigToEnc[key]; + if (keyEnc === undefined) { + if (this.isPasswordEmpty()) { + keyEnc = key; + } else { + keyEnc = await this._encryptName(key); + } + this.cacheMapOrigToEnc[key] = keyEnc; + } + + if (this.isPasswordEmpty()) { + const innerEntity = await this.innerFs.writeFile( + keyEnc, + content, + mtime, + ctime + ); + return copyEntityAndCopyKeyEncSizeEnc(innerEntity); + } else { + const contentEnc = await this._encryptContent(content); + const innerEntity = await this.innerFs.writeFile( + keyEnc, + contentEnc, + mtime, + ctime + ); + return { + key: key, + keyRaw: innerEntity.keyRaw, + keyEnc: innerEntity.key!, + mtimeCli: innerEntity.mtimeCli, + mtimeSvr: innerEntity.mtimeSvr, + size: undefined, + sizeEnc: innerEntity.size!, + sizeRaw: innerEntity.sizeRaw, + hash: undefined, + synthesizedFolder: innerEntity.synthesizedFolder, + }; + } + } + + async readFile(key: string): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for readFile"); + } + const keyEnc = this.cacheMapOrigToEnc[key]; + if (keyEnc === undefined) { + throw new Error(`no encrypted key ${key} before! cannot readFile`); + } + + const contentEnc = await this.innerFs.readFile(keyEnc); + if (this.isPasswordEmpty()) { + return contentEnc; + } else { + const res = await this._decryptContent(contentEnc); + return res; + } + } + + async rename(key1: string, key2: string): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for readFile"); + } + let key1Enc = this.cacheMapOrigToEnc[key1]; + if (key1Enc === undefined) { + if (this.isPasswordEmpty()) { + key1Enc = key1; + } else { + key1Enc = await this._encryptName(key1); + } + this.cacheMapOrigToEnc[key1] = key1Enc; + } + let key2Enc = this.cacheMapOrigToEnc[key2]; + if (key2Enc === undefined) { + if (this.isPasswordEmpty()) { + key2Enc = key2; + } else { + key2Enc = await this._encryptName(key2); + } + this.cacheMapOrigToEnc[key2] = key2Enc; + } + return await this.innerFs.rename(key1Enc, key2Enc); + } + + async rm(key: string): Promise { + if (!this.hasCacheMap) { + throw new Error("You have to build the cacheMap firstly for rm"); + } + const keyEnc = this.cacheMapOrigToEnc[key]; + if (keyEnc === undefined) { + throw new Error(`no encrypted key ${key} before! cannot rm`); + } + return await this.innerFs.rm(keyEnc); + } + + async checkConnect(callbackFunc?: any): Promise { + return await this.innerFs.checkConnect(callbackFunc); + } + + async closeResources() { + if (this.method === "rclone-base64" && this.cipherRClone !== undefined) { + this.cipherRClone.closeResources(); + } + } + + async encryptEntity(input: Entity): Promise { + if (input.key === undefined) { + // input.key should always have value + throw Error(`input ${input.keyRaw} is abnormal without key`); + } + + if (this.isPasswordEmpty()) { + return copyEntityAndCopyKeyEncSizeEnc(input); + } + + // below is for having password + const local = cloneDeep(input); + if (local.sizeEnc === undefined && local.size !== undefined) { + // it's not filled yet, we fill it + // local.size is possibly undefined if it's "prevSync" Entity + // but local.key should always have value + local.sizeEnc = this._getSizeFromOrigToEnc(local.size); + } + + if (local.keyEnc === undefined || local.keyEnc === "") { + let keyEnc = this.cacheMapOrigToEnc[input.key]; + if (keyEnc !== undefined && keyEnc !== "" && keyEnc !== local.key) { + // we can reuse remote encrypted key if any + local.keyEnc = keyEnc; + } else { + // we assign a new encrypted key because of no remote + keyEnc = await this._encryptName(input.key); + local.keyEnc = keyEnc; + // remember to add back to cache! + this.cacheMapOrigToEnc[input.key] = keyEnc; + } + } + return local; + } + + async _encryptContent(content: ArrayBuffer) { + // console.debug("start encryptContent"); + if (this.password === "") { + return content; + } + if (this.method === "openssl-base64") { + const res = await openssl.encryptArrayBuffer(content, this.password); + if (res === undefined) { + throw Error(`cannot encrypt content`); + } + return res; + } else if (this.method === "rclone-base64") { + const res = + await this.cipherRClone!.encryptContentByCallingWorker(content); + if (res === undefined) { + throw Error(`cannot encrypt content`); + } + return res; + } else { + throw Error(`not supported encrypt method=${this.method}`); + } + } + + async _decryptContent(content: ArrayBuffer) { + // console.debug("start decryptContent"); + if (this.password === "") { + return content; + } + if (this.method === "openssl-base64") { + const res = await openssl.decryptArrayBuffer(content, this.password); + if (res === undefined) { + throw Error(`cannot decrypt content`); + } + return res; + } else if (this.method === "rclone-base64") { + const res = + await this.cipherRClone!.decryptContentByCallingWorker(content); + if (res === undefined) { + throw Error(`cannot decrypt content`); + } + return res; + } else { + throw Error(`not supported decrypt method=${this.method}`); + } + } + + async _encryptName(name: string) { + // console.debug("start encryptName"); + if (this.password === "") { + return name; + } + if (this.method === "openssl-base64") { + const res = await openssl.encryptStringToBase64url(name, this.password); + if (res === undefined) { + throw Error(`cannot encrypt name=${name}`); + } + return res; + } else if (this.method === "rclone-base64") { + const res = await this.cipherRClone!.encryptNameByCallingWorker(name); + if (res === undefined) { + throw Error(`cannot encrypt name=${name}`); + } + return res; + } else { + throw Error(`not supported encrypt method=${this.method}`); + } + } + + async _decryptName(name: string): Promise { + // console.debug("start decryptName"); + if (this.password === "") { + return name; + } + if (this.method === "openssl-base64") { + if (name.startsWith(openssl.MAGIC_ENCRYPTED_PREFIX_BASE32)) { + // backward compitable with the openssl-base32 + try { + const res = await openssl.decryptBase32ToString(name, this.password); + if (res !== undefined && isVaildText(res)) { + return res; + } else { + throw Error(`cannot decrypt name=${name}`); + } + } catch (error) { + throw Error(`cannot decrypt name=${name}`); + } + } else if (name.startsWith(openssl.MAGIC_ENCRYPTED_PREFIX_BASE64URL)) { + try { + const res = await openssl.decryptBase64urlToString( + name, + this.password + ); + if (res !== undefined && isVaildText(res)) { + return res; + } else { + throw Error(`cannot decrypt name=${name}`); + } + } catch (error) { + throw Error(`cannot decrypt name=${name}`); + } + } else { + throw Error( + `method=${this.method} but the name=${name}, likely mismatch` + ); + } + } else if (this.method === "rclone-base64") { + const res = await this.cipherRClone!.decryptNameByCallingWorker(name); + if (res === undefined) { + throw Error(`cannot decrypt name=${name}`); + } + return res; + } else { + throw Error(`not supported decrypt method=${this.method}`); + } + } + + _getSizeFromOrigToEnc(x: number) { + if (this.password === "") { + return x; + } + if (this.method === "openssl-base64") { + return openssl.getSizeFromOrigToEnc(x); + } else if (this.method === "rclone-base64") { + return rclone.getSizeFromOrigToEnc(x); + } else { + throw Error(`not supported encrypt method=${this.method}`); + } + } + + async getUserDisplayName(): Promise { + return await this.innerFs.getUserDisplayName(); + } + + async revokeAuth(): Promise { + return await this.innerFs.revokeAuth(); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsGetter.ts b/src/fsGetter.ts new file mode 100644 index 0000000..e610237 --- /dev/null +++ b/src/fsGetter.ts @@ -0,0 +1,93 @@ +/** + * BYOC — Provider Factory + * Resolves the active cloud provider from settings. + * No imports from pro/ — all providers are local. + */ + +import type { BYOCPluginSettings } from "./baseTypes"; +import type { FakeFs } from "./fsAll"; +import { FakeFsDropbox } from "./fsDropbox"; +import { FakeFsOnedrive } from "./fsOnedrive"; +import { FakeFsS3 } from "./fsS3"; +import { FakeFsWebdav } from "./fsWebdav"; +import { FakeFsWebdis } from "./fsWebdis"; +import { FakeFsGoogleDrive } from "./fsGoogleDrive"; +import { FakeFsPCloud } from "./fsPCloud"; +import { FakeFsBox } from "./fsBox"; +import { FakeFsYandexDisk } from "./fsYandexDisk"; +import { FakeFsKoofr } from "./fsKoofr"; +import { FakeFsAzureBlobStorage } from "./fsAzureBlobStorage"; +import { FakeFsOnedriveFull } from "./fsOnedriveFull"; + +/** + * Returns the appropriate FakeFs implementation for the configured provider. + * To avoid circular dependency, this lives in a dedicated file. + */ +export function getClient( + settings: BYOCPluginSettings, + vaultName: string, + saveUpdatedConfigFunc: () => Promise +): FakeFs { + switch (settings.serviceType) { + case "s3": + return new FakeFsS3(settings.s3); + case "webdav": + return new FakeFsWebdav( + settings.webdav, + vaultName, + saveUpdatedConfigFunc + ); + case "dropbox": + return new FakeFsDropbox( + settings.dropbox, + vaultName, + saveUpdatedConfigFunc + ); + case "onedrive": + return new FakeFsOnedrive( + settings.onedrive, + vaultName, + saveUpdatedConfigFunc + ); + case "onedrivefull": + return new FakeFsOnedriveFull( + settings.onedrivefull, + vaultName, + saveUpdatedConfigFunc + ); + case "webdis": + return new FakeFsWebdis( + settings.webdis, + vaultName, + saveUpdatedConfigFunc + ); + case "googledrive": + return new FakeFsGoogleDrive( + settings.googledrive, + vaultName, + saveUpdatedConfigFunc + ); + case "box": + return new FakeFsBox(settings.box, vaultName, saveUpdatedConfigFunc); + case "pcloud": + return new FakeFsPCloud( + settings.pcloud, + vaultName, + saveUpdatedConfigFunc + ); + case "yandexdisk": + return new FakeFsYandexDisk( + settings.yandexdisk, + vaultName, + saveUpdatedConfigFunc + ); + case "koofr": + return new FakeFsKoofr(settings.koofr, vaultName, saveUpdatedConfigFunc); + case "azureblobstorage": + return new FakeFsAzureBlobStorage(settings.azureblobstorage, vaultName); + default: + throw new Error( + `[BYOC] Cannot init client for unknown serviceType: ${(settings as any).serviceType}` + ); + } +} diff --git a/src/fsGoogleDrive.ts b/src/fsGoogleDrive.ts new file mode 100644 index 0000000..2380266 --- /dev/null +++ b/src/fsGoogleDrive.ts @@ -0,0 +1,618 @@ +/** + * BYOC — Google Drive Filesystem Adapter + * Clean-room implementation using Google Drive API v3. + * + * Google does NOT support custom URI schemes (obsidian://) as redirect URIs. + * This adapter uses a manual code-paste flow: + * 1. User visits the auth URL in their browser + * 2. Google redirects to localhost (or shows a code page) + * 3. User copies the authorization code + * 4. User pastes the code into BYOC settings + * 5. BYOC exchanges the code for tokens + * + * The scope is drive.file — only files created by this app are accessible. + */ + +import { request } from "obsidian"; +import { + GOOGLEDRIVE_CLIENT_ID, + GOOGLEDRIVE_CLIENT_SECRET, + type Entity, + type GoogleDriveConfig, +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { retryFetch } from "./misc"; + +const DRIVE_API = "https://www.googleapis.com/drive/v3"; +const DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3"; +const GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/v2/auth"; +const GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token"; +const SCOPES = "https://www.googleapis.com/auth/drive.file"; +// For native/desktop apps, Google supports redirect to localhost +const REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"; + +export const DEFAULT_GOOGLEDRIVE_CONFIG: GoogleDriveConfig = { + accessToken: "", + accessTokenExpiresInMs: 0, + accessTokenExpiresAtTimeMs: 0, + refreshToken: "", + remoteBaseDir: "", + credentialsShouldBeDeletedAtTimeMs: 0, + scope: SCOPES as any, + kind: "googledrive", +}; + +//////////////////////////////////////////////////////////////////////////////// +// OAuth2 Helpers +//////////////////////////////////////////////////////////////////////////////// + +export function generateAuthUrl(verifier?: string): string { + const params = new URLSearchParams({ + response_type: "code", + client_id: GOOGLEDRIVE_CLIENT_ID, + redirect_uri: REDIRECT_URI, + scope: SCOPES, + access_type: "offline", + prompt: "consent", + }); + return `${GOOGLE_AUTH_URL}?${params.toString()}`; +} + +export async function sendAuthReq( + code: string, + verifier: string, + errorCallBack: (e: any) => Promise +): Promise { + try { + const rsp = await request({ + url: GOOGLE_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "authorization_code", + code: code, + client_id: GOOGLEDRIVE_CLIENT_ID, + client_secret: GOOGLEDRIVE_CLIENT_SECRET, + redirect_uri: REDIRECT_URI, + }).toString(), + }); + return JSON.parse(rsp); + } catch (e) { + console.error(e); + await errorCallBack(e); + } +} + +async function refreshAccessToken(refreshToken: string): Promise { + const rsp = await request({ + url: GOOGLE_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: GOOGLEDRIVE_CLIENT_ID, + client_secret: GOOGLEDRIVE_CLIENT_SECRET, + }).toString(), + }); + return JSON.parse(rsp); +} + +export async function setConfigBySuccessfullAuthInplace( + config: GoogleDriveConfig, + authRes: any, + saveFunc: () => Promise +): Promise { + config.accessToken = authRes.access_token; + config.refreshToken = authRes.refresh_token || config.refreshToken; + config.accessTokenExpiresInMs = (authRes.expires_in ?? 3600) * 1000; + config.accessTokenExpiresAtTimeMs = + Date.now() + (authRes.expires_in ?? 3600) * 1000 - 300_000; + config.credentialsShouldBeDeletedAtTimeMs = 0; + await saveFunc(); +} + +//////////////////////////////////////////////////////////////////////////////// +// Google Drive Specifics +//////////////////////////////////////////////////////////////////////////////// + +/** + * Google Drive allows multiple files with the same name in the same folder. + * We must use file IDs for all operations and search by name + parent. + */ + +interface GDriveFile { + id: string; + name: string; + mimeType: string; + size?: string; + modifiedTime?: string; + createdTime?: string; + parents?: string[]; + trashed?: boolean; +} + +const FOLDER_MIME = "application/vnd.google-apps.folder"; + +//////////////////////////////////////////////////////////////////////////////// +// The Client +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsGoogleDrive extends FakeFs { + kind = "googledrive"; + private config: GoogleDriveConfig; + private vaultName: string; + private saveFunc: () => Promise; + private remoteBaseDir: string; + private baseFolderId: string | null = null; + // Cache folder name -> id mappings + private folderCache: Map = new Map(); + + constructor( + config: GoogleDriveConfig, + vaultName: string, + saveFunc: () => Promise + ) { + super(); + this.config = config; + this.vaultName = vaultName; + this.saveFunc = saveFunc; + this.remoteBaseDir = config.remoteBaseDir || vaultName || ""; + } + + private async ensureToken(): Promise { + if (!this.config.accessToken) { + throw Error("[BYOC] Google Drive: not authorized."); + } + + if ( + this.config.refreshToken && + this.config.accessTokenExpiresAtTimeMs > 0 && + this.config.accessTokenExpiresAtTimeMs < Date.now() + ) { + const res = await refreshAccessToken(this.config.refreshToken); + if (res.error) { + throw Error(`[BYOC] Google Drive refresh error: ${res.error_description}`); + } + this.config.accessToken = res.access_token; + if (res.refresh_token) this.config.refreshToken = res.refresh_token; + this.config.accessTokenExpiresAtTimeMs = + Date.now() + (res.expires_in ?? 3600) * 1000 - 300_000; + await this.saveFunc(); + } + + return this.config.accessToken; + } + + private async _getJson(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${DRIVE_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "GET", + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _postJson(url: string, body: any): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${DRIVE_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "POST", + contentType: "application/json", + body: JSON.stringify(body), + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _patchJson(url: string, body: any): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${DRIVE_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "PATCH", + contentType: "application/json", + body: JSON.stringify(body), + headers: { Authorization: `Bearer ${token}` }, + }) + ); + } + + private async _delete(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${DRIVE_API}${url}`; + await retryFetch(fullUrl, { + method: "DELETE", + headers: { Authorization: `Bearer ${token}` }, + }); + } + + /** + * Find or create a folder by name under a parent. + */ + private async findOrCreateFolder( + name: string, + parentId: string + ): Promise { + const cacheKey = `${parentId}/${name}`; + if (this.folderCache.has(cacheKey)) { + return this.folderCache.get(cacheKey)!; + } + + // Search for existing + const q = `name='${name.replace(/'/g, "\\'")}' and '${parentId}' in parents and mimeType='${FOLDER_MIME}' and trashed=false`; + const res = await this._getJson( + `/files?q=${encodeURIComponent(q)}&fields=files(id,name)&pageSize=1` + ); + + if (res.files && res.files.length > 0) { + const id = res.files[0].id; + this.folderCache.set(cacheKey, id); + return id; + } + + // Create + const created = await this._postJson("/files", { + name: name, + mimeType: FOLDER_MIME, + parents: [parentId], + }); + this.folderCache.set(cacheKey, created.id); + return created.id; + } + + /** + * Get or create the base vault folder in Drive root. + */ + private async getBaseFolderId(): Promise { + if (this.baseFolderId) return this.baseFolderId; + + // Walk through the path segments + const segments = this.remoteBaseDir.split("/").filter(Boolean); + let parentId = "root"; + for (const seg of segments) { + parentId = await this.findOrCreateFolder(seg, parentId); + } + + this.baseFolderId = parentId; + return parentId; + } + + /** + * Resolve a key like "subfolder/file.md" to a folder ID for its parent + * and return both parent ID and filename. + */ + private async resolveParent( + key: string + ): Promise<{ parentId: string; name: string }> { + const baseFolderId = await this.getBaseFolderId(); + const parts = key.replace(/\/+$/, "").split("/").filter(Boolean); + + if (parts.length === 0) { + return { parentId: baseFolderId, name: "" }; + } + + const name = parts.pop()!; + let parentId = baseFolderId; + + for (const seg of parts) { + parentId = await this.findOrCreateFolder(seg, parentId); + } + + return { parentId, name }; + } + + /** + * Find a file by name in a folder. + */ + private async findFile( + name: string, + parentId: string, + mimeType?: string + ): Promise { + let q = `name='${name.replace(/'/g, "\\'")}' and '${parentId}' in parents and trashed=false`; + if (mimeType) q += ` and mimeType='${mimeType}'`; + + const res = await this._getJson( + `/files?q=${encodeURIComponent(q)}&fields=files(id,name,mimeType,size,modifiedTime,createdTime,parents)&pageSize=1` + ); + + return res.files && res.files.length > 0 ? res.files[0] : null; + } + + /** + * Recursively list all files under a folder. + */ + private async listRecursive( + folderId: string, + pathPrefix: string, + entities: Entity[] + ): Promise { + let pageToken = ""; + + do { + let url = `/files?q='${folderId}'+in+parents+and+trashed%3Dfalse&fields=nextPageToken,files(id,name,mimeType,size,modifiedTime,createdTime)&pageSize=1000`; + if (pageToken) url += `&pageToken=${pageToken}`; + + const res = await this._getJson(url); + const files: GDriveFile[] = res.files || []; + + for (const f of files) { + const isFolder = f.mimeType === FOLDER_MIME; + const key = isFolder + ? `${pathPrefix}${f.name}/` + : `${pathPrefix}${f.name}`; + + const mtime = f.modifiedTime + ? new Date(f.modifiedTime).getTime() + : 0; + const ctime = f.createdTime + ? new Date(f.createdTime).getTime() + : mtime; + const size = f.size ? parseInt(f.size, 10) : 0; + + entities.push({ + key, keyRaw: key, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: isFolder ? 0 : size, + sizeRaw: isFolder ? 0 : size, + } as Entity); + + if (isFolder) { + await this.listRecursive(f.id, `${pathPrefix}${f.name}/`, entities); + } + } + + pageToken = res.nextPageToken || ""; + } while (pageToken); + } + + async walk(): Promise { + const baseFolderId = await this.getBaseFolderId(); + const entities: Entity[] = []; + await this.listRecursive(baseFolderId, "", entities); + return entities; + } + + async walkPartial(): Promise { + return this.walk(); + } + + async stat(key: string): Promise { + const isFolder = key.endsWith("/"); + const { parentId, name } = await this.resolveParent(key); + + if (!name) { + // Stat on root + return { key, keyRaw: key, size: 0, sizeRaw: 0 } as Entity; + } + + const file = await this.findFile( + name, + parentId, + isFolder ? FOLDER_MIME : undefined + ); + + if (!file) { + throw Error(`[BYOC] Google Drive: '${key}' not found`); + } + + const mtime = file.modifiedTime + ? new Date(file.modifiedTime).getTime() + : 0; + const ctime = file.createdTime + ? new Date(file.createdTime).getTime() + : mtime; + const size = file.size ? parseInt(file.size, 10) : 0; + + return { + key, keyRaw: key, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: isFolder ? 0 : size, + sizeRaw: isFolder ? 0 : size, + } as Entity; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + const { parentId, name } = await this.resolveParent(key); + if (name) { + await this.findOrCreateFolder(name, parentId); + } + return { key, keyRaw: key, size: 0, sizeRaw: 0 } as Entity; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const { parentId, name } = await this.resolveParent(key); + const token = await this.ensureToken(); + + // Check if file already exists + const existing = await this.findFile(name, parentId); + + const metadata: any = { + modifiedTime: new Date(mtime).toISOString(), + }; + + if (existing) { + // Update existing file + const resp = await retryFetch( + `${DRIVE_UPLOAD_API}/files/${existing.id}?uploadType=multipart&fields=id,name,size,modifiedTime`, + { + method: "PATCH", + headers: { Authorization: `Bearer ${token}` }, + body: createMultipartBody(metadata, content, name), + } + ); + if (!resp.ok) { + throw Error( + `[BYOC] Google Drive: update failed for '${key}': ${resp.status}` + ); + } + } else { + // Create new file + metadata.name = name; + metadata.parents = [parentId]; + + const resp = await retryFetch( + `${DRIVE_UPLOAD_API}/files?uploadType=multipart&fields=id,name,size,modifiedTime`, + { + method: "POST", + headers: { Authorization: `Bearer ${token}` }, + body: createMultipartBody(metadata, content, name), + } + ); + if (!resp.ok) { + throw Error( + `[BYOC] Google Drive: create failed for '${key}': ${resp.status}` + ); + } + } + + return { + key, keyRaw: key, + mtimeCli: mtime, ctimeCli: ctime, + size: content.byteLength, + sizeRaw: content.byteLength, + } as Entity; + } + + async readFile(key: string): Promise { + const { parentId, name } = await this.resolveParent(key); + const file = await this.findFile(name, parentId); + + if (!file) { + throw Error(`[BYOC] Google Drive: '${key}' not found for read`); + } + + const token = await this.ensureToken(); + const resp = await retryFetch( + `${DRIVE_API}/files/${file.id}?alt=media`, + { + headers: { Authorization: `Bearer ${token}` }, + } + ); + + if (!resp.ok) { + throw Error( + `[BYOC] Google Drive: download failed for '${key}': ${resp.status}` + ); + } + + return resp.arrayBuffer(); + } + + async rename(key1: string, key2: string): Promise { + // Google Drive rename = move + rename in one PATCH call + const { parentId: parent1, name: name1 } = await this.resolveParent(key1); + const { parentId: parent2, name: name2 } = await this.resolveParent(key2); + + const isFolder = key1.endsWith("/"); + const file = await this.findFile( + name1, + parent1, + isFolder ? FOLDER_MIME : undefined + ); + + if (!file) { + throw Error(`[BYOC] Google Drive: '${key1}' not found for rename`); + } + + await this._patchJson( + `/files/${file.id}?addParents=${parent2}&removeParents=${parent1}`, + { name: name2 } + ); + } + + async rm(key: string): Promise { + const isFolder = key.endsWith("/"); + const { parentId, name } = await this.resolveParent(key); + + const file = await this.findFile( + name, + parentId, + isFolder ? FOLDER_MIME : undefined + ); + + if (!file) return; // Already gone + + // Permanently delete (not trash) + await this._delete(`/files/${file.id}`); + } + + async checkConnect(callbackFunc?: any): Promise { + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + try { + const res = await this._getJson("/about?fields=user"); + return res.user?.displayName || res.user?.emailAddress || "Google Drive User"; + } catch { + return "Google Drive (not configured)"; + } + } + + async revokeAuth(): Promise { + // Optionally revoke the token at Google + if (this.config.accessToken) { + try { + await fetch( + `https://oauth2.googleapis.com/revoke?token=${this.config.accessToken}`, + { method: "POST" } + ); + } catch { + // Best effort + } + } + this.config.accessToken = ""; + this.config.refreshToken = ""; + await this.saveFunc(); + } + + allowEmptyFile(): boolean { + return true; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// Multipart Upload Helper +//////////////////////////////////////////////////////////////////////////////// + +/** + * Create a multipart/related body for Google Drive API uploads. + * This combines JSON metadata and file content in one request. + */ +function createMultipartBody( + metadata: any, + content: ArrayBuffer, + filename: string +): Blob { + const boundary = "byoc_boundary_" + Date.now(); + const delimiter = `\r\n--${boundary}\r\n`; + const closeDelimiter = `\r\n--${boundary}--`; + + const metadataStr = + delimiter + + 'Content-Type: application/json; charset=UTF-8\r\n\r\n' + + JSON.stringify(metadata) + + delimiter + + 'Content-Type: application/octet-stream\r\n\r\n'; + + const metadataBlob = new Blob([metadataStr]); + const contentBlob = new Blob([content]); + const closeBlob = new Blob([closeDelimiter]); + + return new Blob([metadataBlob, contentBlob, closeBlob], { + type: `multipart/related; boundary=${boundary}`, + }); +} diff --git a/src/fsKoofr.ts b/src/fsKoofr.ts new file mode 100644 index 0000000..1760118 --- /dev/null +++ b/src/fsKoofr.ts @@ -0,0 +1,445 @@ +/** + * BYOC — Koofr Filesystem Adapter + * Clean-room implementation using Koofr REST API v2. + * OAuth2 authorization code flow with silent token refresh. + */ + +import { request } from "obsidian"; +import { + COMMAND_CALLBACK_KOOFR, + KOOFR_CLIENT_ID, + KOOFR_CLIENT_SECRET, + type Entity, + type KoofrConfig, +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { retryFetch } from "./misc"; + +const KOOFR_AUTH_URL = "https://app.koofr.net/oauth2/auth"; +const KOOFR_TOKEN_URL = "https://app.koofr.net/oauth2/token"; +const REDIRECT_URI = `obsidian://${COMMAND_CALLBACK_KOOFR}`; + +export const DEFAULT_KOOFR_CONFIG: KoofrConfig = { + accessToken: "", + accessTokenExpiresInMs: 0, + accessTokenExpiresAtTimeMs: 0, + refreshToken: "", + remoteBaseDir: "", + credentialsShouldBeDeletedAtTimeMs: 0, + scope: "", + api: "https://app.koofr.net", + mountID: "", + kind: "koofr", +}; + +//////////////////////////////////////////////////////////////////////////////// +// OAuth2 Helpers +//////////////////////////////////////////////////////////////////////////////// + +export function generateAuthUrl(): string { + const params = new URLSearchParams({ + response_type: "code", + client_id: KOOFR_CLIENT_ID, + redirect_uri: REDIRECT_URI, + scope: "", + }); + return `${KOOFR_AUTH_URL}?${params.toString()}`; +} + +export async function sendAuthReq( + code: string, + errorCallBack: (e: any) => Promise +): Promise { + try { + const rsp = await request({ + url: KOOFR_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "authorization_code", + code: code, + client_id: KOOFR_CLIENT_ID, + client_secret: KOOFR_CLIENT_SECRET, + redirect_uri: REDIRECT_URI, + }).toString(), + }); + return JSON.parse(rsp); + } catch (e) { + console.error(e); + await errorCallBack(e); + } +} + +async function refreshAccessToken(refreshToken: string): Promise { + const rsp = await request({ + url: KOOFR_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: KOOFR_CLIENT_ID, + client_secret: KOOFR_CLIENT_SECRET, + }).toString(), + }); + return JSON.parse(rsp); +} + +export async function setConfigBySuccessfullAuthInplace( + config: KoofrConfig, + authRes: any, + saveFunc: () => Promise +): Promise { + config.accessToken = authRes.access_token; + config.refreshToken = authRes.refresh_token || config.refreshToken; + config.accessTokenExpiresInMs = (authRes.expires_in ?? 3600) * 1000; + config.accessTokenExpiresAtTimeMs = + Date.now() + (authRes.expires_in ?? 3600) * 1000 - 300_000; + config.credentialsShouldBeDeletedAtTimeMs = 0; + await saveFunc(); +} + +//////////////////////////////////////////////////////////////////////////////// +// Path Helpers +//////////////////////////////////////////////////////////////////////////////// + +function getRemotePath(key: string, remoteBaseDir: string): string { + if (!key || key === "/") return `/${remoteBaseDir}`; + const clean = key.endsWith("/") ? key.slice(0, -1) : key; + return `/${remoteBaseDir}/${clean}`; +} + +//////////////////////////////////////////////////////////////////////////////// +// The Client +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsKoofr extends FakeFs { + kind = "koofr"; + private config: KoofrConfig; + private vaultName: string; + private saveFunc: () => Promise; + private remoteBaseDir: string; + private apiBase: string; + + constructor( + config: KoofrConfig, + vaultName: string, + saveFunc: () => Promise + ) { + super(); + this.config = config; + this.vaultName = vaultName; + this.saveFunc = saveFunc; + this.remoteBaseDir = config.remoteBaseDir || vaultName || ""; + this.apiBase = config.api || "https://app.koofr.net"; + } + + private async ensureToken(): Promise { + if (!this.config.accessToken) { + throw Error("[BYOC] Koofr: not authorized."); + } + + if ( + this.config.refreshToken && + this.config.accessTokenExpiresAtTimeMs > 0 && + this.config.accessTokenExpiresAtTimeMs < Date.now() + ) { + const res = await refreshAccessToken(this.config.refreshToken); + if (res.error) { + throw Error(`[BYOC] Koofr refresh error: ${res.error_description}`); + } + this.config.accessToken = res.access_token; + if (res.refresh_token) this.config.refreshToken = res.refresh_token; + this.config.accessTokenExpiresAtTimeMs = + Date.now() + (res.expires_in ?? 3600) * 1000 - 300_000; + await this.saveFunc(); + } + + return this.config.accessToken; + } + + private async ensureMountID(): Promise { + if (this.config.mountID) return this.config.mountID; + + // Get the primary mount (default storage) + const mounts = await this._getJson("/api/v2/mounts"); + if (mounts && mounts.mounts && mounts.mounts.length > 0) { + // Find the primary mount + const primary = + mounts.mounts.find((m: any) => m.isPrimary) || mounts.mounts[0]; + this.config.mountID = primary.id; + await this.saveFunc(); + return this.config.mountID; + } + throw Error("[BYOC] Koofr: no mounts found"); + } + + private async _getJson(path: string): Promise { + const token = await this.ensureToken(); + const url = path.startsWith("http") ? path : `${this.apiBase}${path}`; + return JSON.parse( + await request({ + url, + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + Accept: "application/json", + }, + }) + ); + } + + private async _postJson(path: string, body?: any): Promise { + const token = await this.ensureToken(); + const url = path.startsWith("http") ? path : `${this.apiBase}${path}`; + const opts: any = { + url, + method: "POST", + headers: { + Authorization: `Bearer ${token}`, + Accept: "application/json", + }, + }; + if (body) { + opts.contentType = "application/json"; + opts.body = JSON.stringify(body); + } + const rsp = await request(opts); + return rsp ? JSON.parse(rsp) : {}; + } + + private async _delete(path: string): Promise { + const token = await this.ensureToken(); + const url = path.startsWith("http") ? path : `${this.apiBase}${path}`; + await retryFetch(url, { + method: "DELETE", + headers: { Authorization: `Bearer ${token}` }, + }); + } + + private async ensureBaseDir(): Promise { + const mountID = await this.ensureMountID(); + const remotePath = `/${this.remoteBaseDir}`; + + try { + await this._getJson( + `/api/v2/mounts/${mountID}/files/info?path=${encodeURIComponent(remotePath)}` + ); + } catch { + // Create the base directory + await this._postJson( + `/api/v2/mounts/${mountID}/files/folder?path=${encodeURIComponent("/")}`, + { name: this.remoteBaseDir } + ); + } + } + + /** + * Recursively list all files in the Koofr mount under remoteBaseDir. + */ + private async listRecursive( + mountID: string, + remotePath: string, + entities: Entity[] + ): Promise { + const res = await this._getJson( + `/api/v2/mounts/${mountID}/files/list?path=${encodeURIComponent(remotePath)}` + ); + const files = res.files || []; + + for (const item of files) { + const itemPath = `${remotePath === "/" ? "" : remotePath}/${item.name}`; + const basePrefix = `/${this.remoteBaseDir}/`; + let key = ""; + + if (itemPath.startsWith(basePrefix)) { + key = itemPath.slice(basePrefix.length); + } else if (itemPath === `/${this.remoteBaseDir}`) { + continue; + } else { + key = item.name; + } + + const isFolder = item.type === "dir"; + if (isFolder) { + if (!key.endsWith("/")) key = `${key}/`; + entities.push({ + key, keyRaw: key, + mtimeSvr: item.modified ?? 0, + size: 0, sizeRaw: 0, + } as Entity); + // Recurse + await this.listRecursive(mountID, itemPath, entities); + } else { + entities.push({ + key, keyRaw: key, + mtimeSvr: item.modified ?? 0, + mtimeCli: item.modified ?? 0, + size: item.size ?? 0, + sizeRaw: item.size ?? 0, + hash: item.hash, + } as Entity); + } + } + } + + async walk(): Promise { + await this.ensureBaseDir(); + const mountID = await this.ensureMountID(); + const entities: Entity[] = []; + await this.listRecursive(mountID, `/${this.remoteBaseDir}`, entities); + return entities; + } + + async walkPartial(): Promise { + return this.walk(); + } + + async stat(key: string): Promise { + const mountID = await this.ensureMountID(); + const remotePath = getRemotePath(key, this.remoteBaseDir); + const res = await this._getJson( + `/api/v2/mounts/${mountID}/files/info?path=${encodeURIComponent(remotePath)}` + ); + + const isFolder = res.type === "dir"; + return { + key, keyRaw: key, + mtimeSvr: res.modified ?? 0, + mtimeCli: res.modified ?? 0, + size: isFolder ? 0 : (res.size ?? 0), + sizeRaw: isFolder ? 0 : (res.size ?? 0), + hash: res.hash, + } as Entity; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + const mountID = await this.ensureMountID(); + const folderName = key.replace(/\/+$/, "").split("/").pop()!; + const parentKey = key.replace(/\/+$/, ""); + const parentPath = parentKey.includes("/") + ? `/${this.remoteBaseDir}/${parentKey.slice(0, parentKey.lastIndexOf("/"))}` + : `/${this.remoteBaseDir}`; + + try { + await this._postJson( + `/api/v2/mounts/${mountID}/files/folder?path=${encodeURIComponent(parentPath)}`, + { name: folderName } + ); + } catch (e: any) { + // Already exists is fine + if (!String(e).includes("409") && !String(e).includes("AlreadyExists")) { + throw e; + } + } + + return { key, keyRaw: key, size: 0, sizeRaw: 0 } as Entity; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const mountID = await this.ensureMountID(); + const remotePath = getRemotePath(key, this.remoteBaseDir); + const fileName = key.split("/").pop()!; + const parentPath = remotePath.slice(0, remotePath.lastIndexOf("/")) || "/"; + + const token = await this.ensureToken(); + + // Koofr file upload uses multipart form with PUT + const formData = new FormData(); + formData.append("file", new Blob([content]), fileName); + + const resp = await retryFetch( + `${this.apiBase}/api/v2/mounts/${mountID}/files/put?path=${encodeURIComponent(parentPath)}&filename=${encodeURIComponent(fileName)}&autorename=false&overwrite=true`, + { + method: "POST", + headers: { Authorization: `Bearer ${token}` }, + body: formData, + } + ); + + if (!resp.ok) { + throw Error(`[BYOC] Koofr: upload failed for '${key}': ${resp.status}`); + } + + return { + key, keyRaw: key, + mtimeCli: mtime, ctimeCli: ctime, + size: content.byteLength, + sizeRaw: content.byteLength, + } as Entity; + } + + async readFile(key: string): Promise { + const mountID = await this.ensureMountID(); + const remotePath = getRemotePath(key, this.remoteBaseDir); + const token = await this.ensureToken(); + + const resp = await retryFetch( + `${this.apiBase}/api/v2/mounts/${mountID}/files/get?path=${encodeURIComponent(remotePath)}`, + { + headers: { Authorization: `Bearer ${token}` }, + } + ); + + if (!resp.ok) { + throw Error(`[BYOC] Koofr: download failed for '${key}': ${resp.status}`); + } + return resp.arrayBuffer(); + } + + async rename(key1: string, key2: string): Promise { + const mountID = await this.ensureMountID(); + const from = getRemotePath(key1, this.remoteBaseDir); + const to = getRemotePath(key2, this.remoteBaseDir); + const toName = key2.split("/").pop()!; + const toParent = to.slice(0, to.lastIndexOf("/")) || "/"; + + await this._postJson( + `/api/v2/mounts/${mountID}/files/move?path=${encodeURIComponent(from)}`, + { + toMountId: mountID, + toPath: toParent, + toName: toName, + } + ); + } + + async rm(key: string): Promise { + const mountID = await this.ensureMountID(); + const remotePath = getRemotePath(key, this.remoteBaseDir); + + try { + await this._delete( + `${this.apiBase}/api/v2/mounts/${mountID}/files/remove?path=${encodeURIComponent(remotePath)}` + ); + } catch (e: any) { + // 404 = already gone + if (!String(e).includes("404") && !String(e).includes("NotFound")) throw e; + } + } + + async checkConnect(callbackFunc?: any): Promise { + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + const res = await this._getJson("/api/v2/user"); + return `${res.firstName ?? ""} ${res.lastName ?? ""}`.trim() || "Koofr User"; + } + + async revokeAuth(): Promise { + this.config.accessToken = ""; + this.config.refreshToken = ""; + this.config.mountID = ""; + await this.saveFunc(); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsLocal.ts b/src/fsLocal.ts new file mode 100644 index 0000000..ae7be28 --- /dev/null +++ b/src/fsLocal.ts @@ -0,0 +1,199 @@ +import { DEFAULT_DEBUG_FOLDER, type Entity } from "./baseTypes"; +import { FakeFs } from "./fsAll"; + +import { TFile, TFolder, type Vault } from "obsidian"; +import { mkdirpInVault, statFix, unixTimeToStr } from "./misc"; +import { listFilesInObsFolder } from "./obsFolderLister"; +import type { Profiler } from "./profiler"; + +export class FakeFsLocal extends FakeFs { + vault: Vault; + syncConfigDir: boolean; + syncBookmarks: boolean; + configDir: string; + pluginID: string; + profiler: Profiler | undefined; + deleteToWhere: "obsidian" | "system"; + kind: "local"; + constructor( + vault: Vault, + syncConfigDir: boolean, + syncBookmarks: boolean, + configDir: string, + pluginID: string, + profiler: Profiler | undefined, + deleteToWhere: "obsidian" | "system" + ) { + super(); + + this.vault = vault; + this.syncConfigDir = syncConfigDir; + this.syncBookmarks = syncBookmarks; + this.configDir = configDir; + this.pluginID = pluginID; + this.profiler = profiler; + this.deleteToWhere = deleteToWhere; + this.kind = "local"; + } + + async walk(): Promise { + this.profiler?.addIndent(); + this.profiler?.insert("enter walk for local"); + const local: Entity[] = []; + + const localTAbstractFiles = this.vault.getAllLoadedFiles(); + this.profiler?.insert("finish getting walk for local"); + for (const entry of localTAbstractFiles) { + let r: Entity | undefined = undefined; + let key = entry.path; + if (key.startsWith("/")) { + // why? + // just remove leading slash / + key = key.slice(1); + } + + if (entry.path === "/" || entry.path === "") { + // ignore + continue; + } else if (entry instanceof TFile) { + let mtimeLocal: number | undefined = entry.stat.mtime; + if (mtimeLocal <= 0) { + mtimeLocal = entry.stat.ctime; + } + if (mtimeLocal === 0) { + mtimeLocal = undefined; + } + if (mtimeLocal === undefined) { + throw Error( + `Your file has last modified time 0: ${key}, don't know how to deal with it` + ); + } + r = { + key: key, // local always unencrypted + keyRaw: key, + mtimeCli: mtimeLocal, + mtimeSvr: mtimeLocal, + size: entry.stat.size, // local always unencrypted + sizeRaw: entry.stat.size, + }; + } else if (entry instanceof TFolder) { + key = `${key}/`; + r = { + key: key, + keyRaw: key, + size: 0, + sizeRaw: 0, + }; + } else { + throw Error(`unexpected ${entry}`); + } + + if (r.keyRaw.startsWith(DEFAULT_DEBUG_FOLDER)) { + // skip listing the debug folder, + // which should always not involved in sync + // continue; + } else { + local.push(r); + } + } + + this.profiler?.insert("finish transforming walk for local"); + + if (this.syncConfigDir || this.syncBookmarks) { + this.profiler?.insert("into syncConfigDir or syncBookmarks"); + const bookmarksOnly = !this.syncConfigDir; + const syncFiles = await listFilesInObsFolder( + this.configDir, + this.vault, + this.pluginID, + bookmarksOnly + ); + // console.debug(`syncFiles in obs: ${JSON.stringify(syncFiles, null, 2)}`); + for (const f of syncFiles) { + local.push(f); + } + this.profiler?.insert("finish syncConfigDir"); + } + + this.profiler?.insert("finish walk for local"); + this.profiler?.removeIndent(); + return local; + } + + async walkPartial(): Promise { + return await this.walk(); + } + + async stat(key: string): Promise { + const statRes = await statFix(this.vault, key); + if (statRes === undefined || statRes === null) { + throw Error(`${key} does not exist! cannot stat for local`); + } + const isFolder = statRes.type === "folder"; + return { + key: isFolder ? `${key}/` : key, // local always unencrypted + keyRaw: isFolder ? `${key}/` : key, + ctimeCli: statRes.ctime, + mtimeCli: statRes.mtime, + mtimeSvr: statRes.mtime, + ctimeCliFmt: unixTimeToStr(statRes.ctime), + mtimeCliFmt: unixTimeToStr(statRes.mtime), + mtimeSvrFmt: unixTimeToStr(statRes.mtime), + size: statRes.size, // local always unencrypted + sizeRaw: statRes.size, + }; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + // console.debug(`mkdir: ${key}`); + await mkdirpInVault(key, this.vault); + return await this.stat(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + await this.vault.adapter.writeBinary(key, content, { + mtime: mtime, + ctime: ctime, + }); + return await this.stat(key); + } + + async readFile(key: string): Promise { + return await this.vault.adapter.readBinary(key); + } + + async rename(key1: string, key2: string): Promise { + return await this.vault.adapter.rename(key1, key2); + } + + async rm(key: string): Promise { + if (this.deleteToWhere === "obsidian") { + await this.vault.adapter.trashLocal(key); + } else { + // "system" + if (!(await this.vault.adapter.trashSystem(key))) { + await this.vault.adapter.trashLocal(key); + } + } + } + async checkConnect(callbackFunc?: any): Promise { + return true; + } + + async getUserDisplayName(): Promise { + throw new Error("Method not implemented."); + } + + async revokeAuth(): Promise { + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsMock.ts b/src/fsMock.ts new file mode 100644 index 0000000..f23e551 --- /dev/null +++ b/src/fsMock.ts @@ -0,0 +1,64 @@ +import type { Entity } from "./baseTypes"; +import { FakeFs } from "./fsAll"; + +export class FakeFsMock extends FakeFs { + kind: "mock"; + + constructor() { + super(); + this.kind = "mock"; + } + + async walk(): Promise { + throw new Error("Method not implemented."); + } + + async walkPartial(): Promise { + return await this.walk(); + } + + async stat(key: string): Promise { + throw new Error("Method not implemented."); + } + + async mkdir(key: string, mtime: number, ctime: number): Promise { + throw new Error("Method not implemented."); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + throw new Error("Method not implemented."); + } + + async readFile(key: string): Promise { + throw new Error("Method not implemented."); + } + + async rename(key1: string, key2: string): Promise { + throw new Error("Method not implemented."); + } + + async rm(key: string): Promise { + throw new Error("Method not implemented."); + } + + async checkConnect(callbackFunc?: any): Promise { + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + throw new Error("Method not implemented."); + } + + async revokeAuth(): Promise { + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + throw new Error("Method not implemented."); + } +} diff --git a/src/fsOnedrive.ts b/src/fsOnedrive.ts new file mode 100644 index 0000000..0f28da6 --- /dev/null +++ b/src/fsOnedrive.ts @@ -0,0 +1,1132 @@ +import { CryptoProvider, PublicClientApplication } from "@azure/msal-node"; +import type { AuthenticationProvider } from "@microsoft/microsoft-graph-client"; +import type { + DriveItem, + FileSystemInfo, + UploadSession, + User, +} from "@microsoft/microsoft-graph-types"; +import cloneDeep from "lodash/cloneDeep"; +import { request, requestUrl } from "obsidian"; +import { + COMMAND_CALLBACK_ONEDRIVE, + DEFAULT_CONTENT_TYPE, + type Entity, + ONEDRIVE_AUTHORITY, + ONEDRIVE_CLIENT_ID, + type OnedriveConfig, +} from "./baseTypes"; +import { VALID_REQURL } from "./baseTypesObs"; +import { FakeFs } from "./fsAll"; +import { bufferToArrayBuffer, retryFetch } from "./misc"; + +const SCOPES = ["User.Read", "Files.ReadWrite.AppFolder", "offline_access"]; +const REDIRECT_URI = `obsidian://${COMMAND_CALLBACK_ONEDRIVE}`; + +export const DEFAULT_ONEDRIVE_CONFIG: OnedriveConfig = { + accessToken: "", + clientID: ONEDRIVE_CLIENT_ID ?? "", + authority: ONEDRIVE_AUTHORITY ?? "", + refreshToken: "", + accessTokenExpiresInSeconds: 0, + accessTokenExpiresAtTime: 0, + deltaLink: "", + username: "", + credentialsShouldBeDeletedAtTime: 0, + emptyFile: "skip", + kind: "onedrive", +}; + +//////////////////////////////////////////////////////////////////////////////// +// Onedrive authorization using PKCE +//////////////////////////////////////////////////////////////////////////////// + +export async function getAuthUrlAndVerifier( + clientID: string, + authority: string +) { + const cryptoProvider = new CryptoProvider(); + const { verifier, challenge } = await cryptoProvider.generatePkceCodes(); + + const pkceCodes = { + challengeMethod: "S256", // Use SHA256 Algorithm + verifier: verifier, + challenge: challenge, + }; + + const authCodeUrlParams = { + redirectUri: REDIRECT_URI, + scopes: SCOPES, + codeChallenge: pkceCodes.challenge, // PKCE Code Challenge + codeChallengeMethod: pkceCodes.challengeMethod, // PKCE Code Challenge Method + }; + + const pca = new PublicClientApplication({ + auth: { + clientId: clientID, + authority: authority, + }, + }); + const authCodeUrl = await pca.getAuthCodeUrl(authCodeUrlParams); + + return { + authUrl: authCodeUrl, + verifier: verifier, + }; +} + +/** + * Check doc from + * https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow + * https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow + */ +export interface AccessCodeResponseSuccessfulType { + token_type: "Bearer" | "bearer"; + expires_in: number; + ext_expires_in?: number; + scope: string; + access_token: string; + refresh_token?: string; + id_token?: string; +} +export interface AccessCodeResponseFailedType { + error: string; + error_description: string; + error_codes: number[]; + timestamp: string; + trace_id: string; + correlation_id: string; +} + +export const sendAuthReq = async ( + clientID: string, + authority: string, + authCode: string, + verifier: string, + errorCallBack: any +) => { + // // original code snippets for references + // const authResponse = await pca.acquireTokenByCode({ + // redirectUri: REDIRECT_URI, + // scopes: SCOPES, + // code: authCode, + // codeVerifier: verifier, // PKCE Code Verifier + // }); + // console.info('authResponse') + // console.info(authResponse) + // return authResponse; + + // Because of the CORS problem, + // we need to construct raw request using Obsidian request, + // instead of using msal + // https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow + // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online#code-flow + try { + const rsp1 = await request({ + url: `${authority}/oauth2/v2.0/token`, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + tenant: "consumers", + client_id: clientID, + scope: SCOPES.join(" "), + code: authCode, + redirect_uri: REDIRECT_URI, + grant_type: "authorization_code", + code_verifier: verifier, + }).toString(), + }); + + const rsp2 = JSON.parse(rsp1); + // console.info(rsp2); + + if (rsp2.error !== undefined) { + return rsp2 as AccessCodeResponseFailedType; + } else { + return rsp2 as AccessCodeResponseSuccessfulType; + } + } catch (e) { + console.error(e); + await errorCallBack(e); + } +}; + +export const sendRefreshTokenReq = async ( + clientID: string, + authority: string, + refreshToken: string +) => { + // also use Obsidian request to bypass CORS issue. + try { + const rsp1 = await request({ + url: `${authority}/oauth2/v2.0/token`, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + tenant: "consumers", + client_id: clientID, + scope: SCOPES.join(" "), + refresh_token: refreshToken, + grant_type: "refresh_token", + }).toString(), + }); + + const rsp2 = JSON.parse(rsp1); + // console.info(rsp2); + + if (rsp2.error !== undefined) { + return rsp2 as AccessCodeResponseFailedType; + } else { + return rsp2 as AccessCodeResponseSuccessfulType; + } + } catch (e) { + console.error(e); + throw e; + } +}; + +export const setConfigBySuccessfullAuthInplace = async ( + config: OnedriveConfig, + authRes: AccessCodeResponseSuccessfulType, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + console.info("start updating local info of OneDrive token"); + config.accessToken = authRes.access_token; + config.accessTokenExpiresAtTime = + Date.now() + authRes.expires_in - 5 * 60 * 1000; + config.accessTokenExpiresInSeconds = authRes.expires_in; + config.refreshToken = authRes.refresh_token!; + + // BYOC: No forced expiry — credentials persist until revoked by user + config.credentialsShouldBeDeletedAtTime = 0; + + if (saveUpdatedConfigFunc !== undefined) { + await saveUpdatedConfigFunc(); + } + + console.info("finish updating local info of Onedrive token"); +}; + +//////////////////////////////////////////////////////////////////////////////// +// Other usual common methods +//////////////////////////////////////////////////////////////////////////////// + +const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => { + // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online + const prefix = `/drive/special/approot:/${remoteBaseDir}`; + + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + // special + return prefix; + } + if (key.endsWith("/")) { + key = key.slice(0, key.length - 1); + } + + if (key.startsWith("/")) { + console.warn(`why the path ${key} starts with '/'? but we just go on.`); + key = `${prefix}${key}`; + } else { + key = `${prefix}/${key}`; + } + return key; +}; + +const constructFromDriveItemToEntityError = (x: DriveItem) => { + return `parentPath="${ + x.parentReference?.path ?? "(no parentReference or path)" + }", selfName="${x.name}"`; +}; + +const fromDriveItemToEntity = (x: DriveItem, remoteBaseDir: string): Entity => { + let key = ""; + + // possible prefix: + // pure english: /drive/root:/Apps/remotely-save/${remoteBaseDir} + // or localized, e.g.: /drive/root:/应用/remotely-save/${remoteBaseDir} + const FIRST_COMMON_PREFIX_REGEX = /^\/drive\/root:\/[^\/]+\/remotely-save\//g; + + // why?? /drive/root:/Apps/Graph + const FIFTH_COMMON_PREFIX_REGEX = /^\/drive\/root:\/[^\/]+\/Graph\//g; + + // why again?? /drive/root:/Apps/Graph 1 + const SIXTH_COMMON_PREFIX_REGEX = /^\/drive\/root:\/[^\/]+\/Graph 1\//g; + const SIXTH_COMMON_PREFIX_REGEX_V2 = /^\/drive\/root:\/[^\/]+\/Graph%201\//g; + + // or the root is absolute path /Livefolders, + // e.g.: /Livefolders/应用/remotely-save/${remoteBaseDir} + const SECOND_COMMON_PREFIX_REGEX = /^\/Livefolders\/[^\/]+\/remotely-save\//g; + + // another report, why??? + // /drive/root:/something/app/remotely-save/${remoteBaseDir} + const THIRD_COMMON_PREFIX_REGEX = + /^\/drive\/root:\/[^\/]+\/app\/remotely-save\//g; + + // another possibile prefix + const FOURTH_COMMON_PREFIX_RAW = `/drive/items/`; + + // when to use decode? + const remoteBaseDirEncoded = encodeURIComponent(remoteBaseDir); + + if ( + x.parentReference === undefined || + x.parentReference === null || + x.parentReference.path === undefined || + x.parentReference.path === null + ) { + throw Error("x.parentReference.path is undefinded or null"); + } + const fullPathOriginal = `${x.parentReference.path}/${x.name}`; + const matchFirstPrefixRes = fullPathOriginal.match(FIRST_COMMON_PREFIX_REGEX); + const matchFifthPrefixRes = fullPathOriginal.match(FIFTH_COMMON_PREFIX_REGEX); + const matchSixthPrefixRes = fullPathOriginal.match(SIXTH_COMMON_PREFIX_REGEX); + const matchSixthV2PrefixRes = fullPathOriginal.match( + SIXTH_COMMON_PREFIX_REGEX_V2 + ); + + const matchSecondPrefixRes = fullPathOriginal.match( + SECOND_COMMON_PREFIX_REGEX + ); + const matchThirdPrefixRes = fullPathOriginal.match(THIRD_COMMON_PREFIX_REGEX); + + // first + if ( + matchFirstPrefixRes !== null && + fullPathOriginal.startsWith(`${matchFirstPrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchFirstPrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchFirstPrefixRes !== null && + fullPathOriginal.startsWith( + `${matchFirstPrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchFirstPrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // fifth + else if ( + matchFifthPrefixRes !== null && + fullPathOriginal.startsWith(`${matchFifthPrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchFifthPrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchFifthPrefixRes !== null && + fullPathOriginal.startsWith( + `${matchFifthPrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchFifthPrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // sixth + else if ( + matchSixthPrefixRes !== null && + fullPathOriginal.startsWith(`${matchSixthPrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchSixthPrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchSixthPrefixRes !== null && + fullPathOriginal.startsWith( + `${matchSixthPrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchSixthPrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // sixth v2 + else if ( + matchSixthV2PrefixRes !== null && + fullPathOriginal.startsWith(`${matchSixthV2PrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchSixthV2PrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchSixthV2PrefixRes !== null && + fullPathOriginal.startsWith( + `${matchSixthV2PrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchSixthV2PrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // second + else if ( + matchSecondPrefixRes !== null && + fullPathOriginal.startsWith(`${matchSecondPrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchSecondPrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchSecondPrefixRes !== null && + fullPathOriginal.startsWith( + `${matchSecondPrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchSecondPrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // third + else if ( + matchThirdPrefixRes !== null && + fullPathOriginal.startsWith(`${matchThirdPrefixRes[0]}${remoteBaseDir}`) + ) { + const foundPrefix = `${matchThirdPrefixRes[0]}${remoteBaseDir}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } else if ( + matchThirdPrefixRes !== null && + fullPathOriginal.startsWith( + `${matchThirdPrefixRes[0]}${remoteBaseDirEncoded}` + ) + ) { + const foundPrefix = `${matchThirdPrefixRes[0]}${remoteBaseDirEncoded}`; + key = fullPathOriginal.substring(foundPrefix.length + 1); + } + + // fourth + else if (x.parentReference.path.startsWith(FOURTH_COMMON_PREFIX_RAW)) { + // it's something like + // /drive/items/!:/${remoteBaseDir}/ + // with uri encoded! + if (x.name === undefined || x.name === null) { + throw Error( + `OneDrive item no name variable while matching ${FOURTH_COMMON_PREFIX_RAW}` + ); + } + const parPath = decodeURIComponent(x.parentReference.path); + key = parPath.substring(parPath.indexOf(":") + 1); + if (key.startsWith(`/${remoteBaseDir}/`)) { + key = key.substring(`/${remoteBaseDir}/`.length); + key = `${key}/${x.name}`; + } else if (key === `/${remoteBaseDir}`) { + key = x.name; + } else { + throw Error( + `file/folder with /drive/items/, no idea how to deal with it: +fullPathOriginal=${fullPathOriginal} +matchFirstPrefixRes=${matchFirstPrefixRes} +matchFifthPrefixRes=${matchFifthPrefixRes} +matchSixthPrefixRes=${matchSixthPrefixRes} +matchSixthV2PrefixRes=${matchSixthV2PrefixRes} +matchSecondPrefixRes=${matchSecondPrefixRes} +matchThirdPrefixRes=${matchThirdPrefixRes} +${constructFromDriveItemToEntityError(x)}` + ); + } + } + + // others + else { + throw Error( + `file/folder, no idea how to deal with it without known prefix: +fullPathOriginal=${fullPathOriginal} +matchFirstPrefixRes=${matchFirstPrefixRes} +matchFifthPrefixRes=${matchFifthPrefixRes} +matchSixthPrefixRes=${matchSixthPrefixRes} +matchSixthV2PrefixRes=${matchSixthV2PrefixRes} +matchSecondPrefixRes=${matchSecondPrefixRes} +matchThirdPrefixRes=${matchThirdPrefixRes} +${constructFromDriveItemToEntityError(x)}` + ); + } + + const isFolder = "folder" in x; + if (isFolder) { + key = `${key}/`; + } + + const mtimeTry = x?.fileSystemInfo?.lastModifiedDateTime; + if (mtimeTry === undefined || mtimeTry === null) { + throw Error(`onedrive cannot parse mtime: ${JSON.stringify(x, null, 2)}`); + } + + let ctimeTry = x?.fileSystemInfo?.createdDateTime; + if (ctimeTry === undefined || ctimeTry === null) { + // throw Error(`onedrive cannot parse ctime: ${JSON.stringify(x, null, 2)}`); + // it doesn't hurt, we just use mtimeTry to fullfill + ctimeTry = mtimeTry; + } + + const mtimeSvr = Date.parse(mtimeTry); + const mtimeCli = Date.parse(mtimeTry); + const ctimeCli = Date.parse(ctimeTry); + return { + key: key, + keyRaw: key, + mtimeSvr: mtimeSvr, + mtimeCli: mtimeCli, + ctimeCli: ctimeCli, + size: isFolder ? 0 : x.size!, + sizeRaw: isFolder ? 0 : x.size!, + synthesizedFile: false, + // hash: ?? // TODO + }; +}; + +//////////////////////////////////////////////////////////////////////////////// +// The client. +//////////////////////////////////////////////////////////////////////////////// + +// to adapt to the required interface +class MyAuthProvider implements AuthenticationProvider { + onedriveConfig: OnedriveConfig; + saveUpdatedConfigFunc: () => Promise; + constructor( + onedriveConfig: OnedriveConfig, + saveUpdatedConfigFunc: () => Promise + ) { + this.onedriveConfig = onedriveConfig; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + } + + async getAccessToken() { + if ( + this.onedriveConfig.accessToken === "" || + this.onedriveConfig.refreshToken === "" + ) { + throw Error("The user has not manually auth yet."); + } + + const currentTs = Date.now(); + if (this.onedriveConfig.accessTokenExpiresAtTime > currentTs) { + return this.onedriveConfig.accessToken; + } else { + // use refreshToken to refresh + const r = await sendRefreshTokenReq( + this.onedriveConfig.clientID, + this.onedriveConfig.authority, + this.onedriveConfig.refreshToken + ); + if ((r as any).error !== undefined) { + const r2 = r as AccessCodeResponseFailedType; + throw Error( + `Error while refreshing accessToken: ${r2.error}, ${r2.error_codes}: ${r2.error_description}` + ); + } + const r2 = r as AccessCodeResponseSuccessfulType; + this.onedriveConfig.accessToken = r2.access_token; + this.onedriveConfig.refreshToken = r2.refresh_token!; + this.onedriveConfig.accessTokenExpiresInSeconds = r2.expires_in; + this.onedriveConfig.accessTokenExpiresAtTime = + currentTs + r2.expires_in * 1000 - 60 * 2 * 1000; + await this.saveUpdatedConfigFunc(); + console.info("Onedrive accessToken updated"); + return this.onedriveConfig.accessToken; + } + } +} + +/** + * to export the settings in qrcode, + * we want to "trim" or "shrink" the settings + * @param onedriveConfig + */ +export const getShrinkedSettings = (onedriveConfig: OnedriveConfig) => { + const config = cloneDeep(onedriveConfig); + config.accessToken = "x"; + config.accessTokenExpiresInSeconds = 1; + config.accessTokenExpiresAtTime = 1; + return config; +}; + +export class FakeFsOnedrive extends FakeFs { + kind: "onedrive"; + onedriveConfig: OnedriveConfig; + remoteBaseDir: string; + vaultFolderExists: boolean; + authGetter: MyAuthProvider; + saveUpdatedConfigFunc: () => Promise; + foldersCreatedBefore: Set; + + constructor( + onedriveConfig: OnedriveConfig, + vaultName: string, + saveUpdatedConfigFunc: () => Promise + ) { + super(); + this.kind = "onedrive"; + this.onedriveConfig = onedriveConfig; + this.remoteBaseDir = this.onedriveConfig.remoteBaseDir || vaultName || ""; + this.vaultFolderExists = false; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + this.authGetter = new MyAuthProvider(onedriveConfig, saveUpdatedConfigFunc); + this.foldersCreatedBefore = new Set(); + } + + async _init() { + // check token + if ( + this.onedriveConfig.accessToken === "" || + this.onedriveConfig.refreshToken === "" + ) { + throw Error("The user has not manually auth yet."); + } + + // check vault folder + // console.info(`checking remote has folder /${this.remoteBaseDir}`); + if (this.vaultFolderExists) { + // console.info(`already checked, /${this.remoteBaseDir} exist before`) + } else { + const k = await this._getJson("/drive/special/approot/children"); + // console.debug(k); + this.vaultFolderExists = + (k.value as DriveItem[]).filter((x) => x.name === this.remoteBaseDir) + .length > 0; + if (!this.vaultFolderExists) { + console.info(`remote does not have folder /${this.remoteBaseDir}`); + await this._postJson("/drive/special/approot/children", { + name: `${this.remoteBaseDir}`, + folder: {}, + "@microsoft.graph.conflictBehavior": "replace", + }); + console.info(`remote folder /${this.remoteBaseDir} created`); + this.vaultFolderExists = true; + } else { + // console.info(`remote folder /${this.remoteBaseDir} exists`); + } + } + } + + _buildUrl(pathFragOrig: string) { + const API_PREFIX = "https://graph.microsoft.com/v1.0"; + let theUrl = ""; + if ( + pathFragOrig.startsWith("http://") || + pathFragOrig.startsWith("https://") + ) { + theUrl = pathFragOrig; + } else { + const pathFrag = encodeURI(pathFragOrig); + theUrl = `${API_PREFIX}${pathFrag}`; + } + // we want to support file name with hash # + // because every url we construct here do not contain the # symbol + // thus it should be safe to directly replace the character + theUrl = theUrl.replace(/#/g, "%23"); + // console.debug(`building url: [${pathFragOrig}] => [${theUrl}]`) + return theUrl; + } + + async _getJson(pathFragOrig: string) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug(`getJson, theUrl=${theUrl}`); + return JSON.parse( + await request({ + url: theUrl, + method: "GET", + contentType: "application/json", + headers: { + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + "Cache-Control": "no-cache", + }, + }) + ); + } + + async _postJson(pathFragOrig: string, payload: any) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug(`postJson, theUrl=${theUrl}`); + return JSON.parse( + await request({ + url: theUrl, + method: "POST", + contentType: "application/json", + body: JSON.stringify(payload), + headers: { + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }) + ); + } + + async _patchJson(pathFragOrig: string, payload: any) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug(`patchJson, theUrl=${theUrl}`); + return JSON.parse( + await request({ + url: theUrl, + method: "PATCH", + contentType: "application/json", + body: JSON.stringify(payload), + headers: { + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }) + ); + } + + async _deleteJson(pathFragOrig: string) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug(`deleteJson, theUrl=${theUrl}`); + if (VALID_REQURL) { + await requestUrl({ + url: theUrl, + method: "DELETE", + headers: { + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }); + } else { + await retryFetch(theUrl, { + method: "DELETE", + headers: { + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }); + } + } + + async _putArrayBuffer(pathFragOrig: string, payload: ArrayBuffer) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug(`putArrayBuffer, theUrl=${theUrl}`); + // TODO: + // 20220401: On Android, requestUrl has issue that text becomes base64. + // Use fetch everywhere instead! + // biome-ignore lint/correctness/noConstantCondition: hard code + if (false /*VALID_REQURL*/) { + const res = await requestUrl({ + url: theUrl, + method: "PUT", + body: payload, + contentType: DEFAULT_CONTENT_TYPE, + headers: { + "Content-Type": DEFAULT_CONTENT_TYPE, + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }); + return res.json as DriveItem | UploadSession; + } else { + const res = await retryFetch(theUrl, { + method: "PUT", + body: payload, + headers: { + "Content-Type": DEFAULT_CONTENT_TYPE, + Authorization: `Bearer ${await this.authGetter.getAccessToken()}`, + }, + }); + return (await res.json()) as DriveItem | UploadSession; + } + } + + /** + * A specialized function to upload large files by parts + * @param pathFragOrig + * @param payload + * @param rangeMin + * @param rangeEnd the end, exclusive + * @param size + */ + async _putUint8ArrayByRange( + pathFragOrig: string, + payload: Uint8Array, + rangeStart: number, + rangeEnd: number, + size: number + ) { + const theUrl = this._buildUrl(pathFragOrig); + console.debug( + `putUint8ArrayByRange, theUrl=${theUrl}, range=${rangeStart}-${ + rangeEnd - 1 + }, len=${rangeEnd - rangeStart}, size=${size}` + ); + // NO AUTH HEADER here! + // TODO: + // 20220401: On Android, requestUrl has issue that text becomes base64. + // Use fetch everywhere instead! + // biome-ignore lint/correctness/noConstantCondition: hard code + if (false /*VALID_REQURL*/) { + const res = await requestUrl({ + url: theUrl, + method: "PUT", + body: bufferToArrayBuffer(payload.subarray(rangeStart, rangeEnd)), + contentType: DEFAULT_CONTENT_TYPE, + headers: { + // no "Content-Length" allowed here + "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`, + /* "Cache-Control": "no-cache", not allowed here!!! */ + }, + }); + return res.json as DriveItem | UploadSession; + } else { + const res = await retryFetch(theUrl, { + method: "PUT", + body: payload.subarray(rangeStart, rangeEnd), + headers: { + "Content-Length": `${rangeEnd - rangeStart}`, + "Content-Range": `bytes ${rangeStart}-${rangeEnd - 1}/${size}`, + "Content-Type": DEFAULT_CONTENT_TYPE, + /* "Cache-Control": "no-cache", not allowed here!!! */ + }, + }); + return (await res.json()) as DriveItem | UploadSession; + } + } + + /** + * Use delta api to list all files and folders + * https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_delta?view=odsp-graph-online + */ + async walk(): Promise { + await this._init(); + + const NEXT_LINK_KEY = "@odata.nextLink"; + const DELTA_LINK_KEY = "@odata.deltaLink"; + + let res = await this._getJson( + `/drive/special/approot:/${this.remoteBaseDir}:/delta` + ); + const driveItems = res.value as DriveItem[]; + // console.debug(driveItems); + + while (NEXT_LINK_KEY in res) { + res = await this._getJson(res[NEXT_LINK_KEY]); + driveItems.push(...cloneDeep(res.value as DriveItem[])); + } + + // lastly we should have delta link? + if (DELTA_LINK_KEY in res) { + this.onedriveConfig.deltaLink = res[DELTA_LINK_KEY]; + await this.saveUpdatedConfigFunc(); + } + + // unify everything to Entity + const unifiedContents = driveItems + .map((x) => fromDriveItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.key !== "/"); + + return unifiedContents; + } + + async walkPartial(): Promise { + await this._init(); + + const DELTA_LINK_KEY = "@odata.deltaLink"; + + const res = await this._getJson( + `/drive/special/approot:/${this.remoteBaseDir}:/delta` + ); + const driveItems = res.value as DriveItem[]; + // console.debug(driveItems); + + // lastly we should have delta link? + if (DELTA_LINK_KEY in res) { + this.onedriveConfig.deltaLink = res[DELTA_LINK_KEY]; + await this.saveUpdatedConfigFunc(); + } + + // unify everything to Entity + const unifiedContents = driveItems + .map((x) => fromDriveItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.key !== "/"); + + return unifiedContents; + } + + async stat(key: string): Promise { + await this._init(); + return await this._statFromRoot(getOnedrivePath(key, this.remoteBaseDir)); + } + + async _statFromRoot(key: string): Promise { + // console.info(`remotePath=${remotePath}`); + const rsp = await this._getJson( + `${key}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size` + ); + // console.info(rsp); + const driveItem = rsp as DriveItem; + const res = fromDriveItemToEntity(driveItem, this.remoteBaseDir); + // console.info(res); + return res; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!key.endsWith("/")) { + throw Error(`you should not call mkdir on ${key}`); + } + await this._init(); + const uploadFolder = getOnedrivePath(key, this.remoteBaseDir); + console.debug(`mkdir uploadFolder=${uploadFolder}`); + return await this._mkdirFromRoot(uploadFolder, mtime, ctime); + } + + async _mkdirFromRoot( + key: string, + mtime?: number, + ctime?: number + ): Promise { + // console.debug(`foldersCreatedBefore=${Array.from(this.foldersCreatedBefore)}`); + if (this.foldersCreatedBefore.has(key)) { + // created, pass + // console.debug(`folder ${key} created.`) + } else { + // https://stackoverflow.com/questions/56479865/creating-nested-folders-in-one-go-onedrive-api + // use PATCH to create folder recursively!!! + const playload: any = { + folder: {}, + "@microsoft.graph.conflictBehavior": "replace", + }; + const fileSystemInfo: Record = {}; + if (mtime !== undefined && mtime !== 0) { + const mtimeStr = new Date(mtime).toISOString(); + fileSystemInfo["lastModifiedDateTime"] = mtimeStr; + } + if (ctime !== undefined && ctime !== 0) { + const ctimeStr = new Date(ctime).toISOString(); + fileSystemInfo["createdDateTime"] = ctimeStr; + } + if (Object.keys(fileSystemInfo).length > 0) { + playload["fileSystemInfo"] = fileSystemInfo; + } + await this._patchJson(key, playload); + } + const res = await this._statFromRoot(key); + return res; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (key.endsWith("/")) { + throw Error(`you should not call writeFile on ${key}`); + } + await this._init(); + const uploadFile = getOnedrivePath(key, this.remoteBaseDir); + console.debug(`uploadFile=${uploadFile}`); + return await this._writeFileFromRoot( + uploadFile, + content, + mtime, + ctime, + key, + this.onedriveConfig.emptyFile + ); + } + + async _writeFileFromRoot( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string, + emptyFile: "skip" | "error" + ): Promise { + if (content.byteLength === 0) { + 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, + ctimeCli: ctime, + size: 0, + sizeRaw: 0, + synthesizedFile: true, + // hash: ?? // TODO + }; + } + } + + const ctimeStr = new Date(ctime).toISOString(); + const mtimeStr = new Date(mtime).toISOString(); + + // no need to create parent folders firstly, cool! + + // hard code range size + const MIN_UNIT = 327680; // bytes in msft doc, about 0.32768 MB + const RANGE_SIZE = MIN_UNIT * 20; // about 6.5536 MB + const DIRECT_UPLOAD_MAX_SIZE = 1000 * 1000 * 4; // 4 Megabyte + + if (content.byteLength < DIRECT_UPLOAD_MAX_SIZE) { + // directly using put! + await this._putArrayBuffer( + `${key}:/content?${new URLSearchParams({ + "@microsoft.graph.conflictBehavior": "replace", + })}`, + content + ); + if (mtime !== 0 && ctime !== 0) { + await this._patchJson(key, { + fileSystemInfo: { + lastModifiedDateTime: mtimeStr, + createdDateTime: ctimeStr, + } as FileSystemInfo, + }); + } + } else { + // upload large files! + // ref: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online + + // 1. create uploadSession + // uploadFile already starts with /drive/special/approot:/${remoteBaseDir} + let playload: any = { + item: { + "@microsoft.graph.conflictBehavior": "replace", + }, + }; + if (mtime !== 0 && ctime !== 0) { + playload = { + item: { + "@microsoft.graph.conflictBehavior": "replace", + + // this is only possible using uploadSession + fileSystemInfo: { + lastModifiedDateTime: mtimeStr, + createdDateTime: ctimeStr, + } as FileSystemInfo, + }, + }; + } + const s: UploadSession = await this._postJson( + `${key}:/createUploadSession`, + playload + ); + const uploadUrl = s.uploadUrl!; + console.debug("uploadSession = "); + console.debug(s); + + // 2. upload by ranges + // convert to uint8 + const uint8 = new Uint8Array(content); + + // upload the ranges one by one + let rangeStart = 0; + while (rangeStart < uint8.byteLength) { + await this._putUint8ArrayByRange( + uploadUrl, + uint8, + rangeStart, + Math.min(rangeStart + RANGE_SIZE, uint8.byteLength), + uint8.byteLength + ); + rangeStart += RANGE_SIZE; + } + } + + const res = await this._statFromRoot(key); + return res; + } + + async readFile(key: string): Promise { + await this._init(); + if (key.endsWith("/")) { + throw new Error(`you should not call readFile on folder ${key}`); + } + const downloadFile = getOnedrivePath(key, this.remoteBaseDir); + return await this._readFileFromRoot(downloadFile); + } + + async _readFileFromRoot(key: string): Promise { + const rsp = await this._getJson( + `${key}?$select=@microsoft.graph.downloadUrl` + ); + const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"]; + // biome-ignore lint/correctness/noConstantCondition: + if (false /*VALID_REQURL*/) { + const content = ( + await requestUrl({ + url: downloadUrl, + headers: { "Cache-Control": "no-cache" }, + }) + ).arrayBuffer; + return content; + } else { + // so strange, sometimes (!!!) + // we cannot download the file because of CORS + try { + // cannot set no-cache here, will have cors error + const content = await ( + await retryFetch(downloadUrl, { cache: "no-store" }) + ).arrayBuffer(); + return content; + } catch (e) { + // let's try again to bypass the CORS + const content = ( + await requestUrl({ + url: downloadUrl, + headers: { "Cache-Control": "no-cache" }, + }) + ).arrayBuffer; + return content; + } + } + } + + async rename(key1: string, key2: string): Promise { + if (key1 === "" || key1 === "/" || key2 === "" || key2 === "/") { + return; + } + const remoteFileName1 = getOnedrivePath(key1, this.remoteBaseDir); + const remoteFileName2 = getOnedrivePath(key2, this.remoteBaseDir); + await this._init(); + await this._patchJson(remoteFileName1, { + name: remoteFileName2, + }); + } + + async rm(key: string): Promise { + if (key === "" || key === "/") { + return; + } + const remoteFileName = getOnedrivePath(key, this.remoteBaseDir); + + await this._init(); + await this._deleteJson(remoteFileName); + } + + async checkConnect(callbackFunc?: any): Promise { + try { + const k = await this.getUserDisplayName(); + if (k === "") { + throw Error(`unknown display name!`); + } + } catch (err) { + console.debug(err); + callbackFunc?.(err); + return false; + } + + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName() { + await this._init(); + const res: User = await this._getJson("/me?$select=displayName"); + return res.displayName || ""; + } + + /** + * + * https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc#send-a-sign-out-request + * https://docs.microsoft.com/en-us/graph/api/user-revokesigninsessions + * https://docs.microsoft.com/en-us/graph/api/user-invalidateallrefreshtokens + */ + async revokeAuth() { + // await this._init(); + // await this._postJson("/me/revokeSignInSessions", {}); + throw new Error("Method not implemented."); + } + + async getRevokeAddr() { + return "https://account.live.com/consent/Manage"; + } + + allowEmptyFile(): boolean { + return false; + } +} diff --git a/src/fsOnedriveFull.ts b/src/fsOnedriveFull.ts new file mode 100644 index 0000000..79186f3 --- /dev/null +++ b/src/fsOnedriveFull.ts @@ -0,0 +1,697 @@ +/** + * BYOC — OneDrive Full Filesystem Adapter + * Clean-room implementation using Microsoft Graph API with root folder access. + * Unlike the standard OneDrive adapter (App Folder only), this accesses + * the user's full drive root, allowing custom remoteBaseDir placement. + */ + +import { CryptoProvider, PublicClientApplication } from "@azure/msal-node"; +import type { + DriveItem, + FileSystemInfo, + UploadSession, + User, +} from "@microsoft/microsoft-graph-types"; +import cloneDeep from "lodash/cloneDeep"; +import { request, requestUrl } from "obsidian"; +import { + COMMAND_CALLBACK_ONEDRIVEFULL, + DEFAULT_CONTENT_TYPE, + type Entity, + ONEDRIVE_AUTHORITY, + ONEDRIVE_CLIENT_ID, + type OnedriveFullConfig, +} from "./baseTypes"; +import { VALID_REQURL } from "./baseTypesObs"; +import { FakeFs } from "./fsAll"; +import { bufferToArrayBuffer, retryFetch } from "./misc"; + +// Full access scopes — reads/writes anywhere on the drive +const SCOPES = ["User.Read", "Files.ReadWrite.All", "offline_access"]; +const REDIRECT_URI = `obsidian://${COMMAND_CALLBACK_ONEDRIVEFULL}`; + +export const DEFAULT_ONEDRIVEFULL_CONFIG: OnedriveFullConfig = { + accessToken: "", + clientID: ONEDRIVE_CLIENT_ID ?? "", + authority: ONEDRIVE_AUTHORITY ?? "", + refreshToken: "", + accessTokenExpiresInSeconds: 0, + accessTokenExpiresAtTime: 0, + deltaLink: "", + username: "", + credentialsShouldBeDeletedAtTime: 0, + emptyFile: "skip", + kind: "onedrivefull", +}; + +//////////////////////////////////////////////////////////////////////////////// +// OneDrive Full authorization using PKCE +//////////////////////////////////////////////////////////////////////////////// + +export async function getAuthUrlAndVerifier( + clientID: string, + authority: string +) { + const cryptoProvider = new CryptoProvider(); + const { verifier, challenge } = await cryptoProvider.generatePkceCodes(); + + const authCodeUrlParams = { + redirectUri: REDIRECT_URI, + scopes: SCOPES, + codeChallenge: challenge, + codeChallengeMethod: "S256", + }; + + const pca = new PublicClientApplication({ + auth: { clientId: clientID, authority: authority }, + }); + const authCodeUrl = await pca.getAuthCodeUrl(authCodeUrlParams); + + return { authUrl: authCodeUrl, verifier: verifier }; +} + +export interface AccessCodeResponseSuccessfulType { + token_type: "Bearer" | "bearer"; + expires_in: number; + ext_expires_in?: number; + scope: string; + access_token: string; + refresh_token?: string; + id_token?: string; +} + +export interface AccessCodeResponseFailedType { + error: string; + error_description: string; + error_codes: number[]; + timestamp: string; + trace_id: string; + correlation_id: string; +} + +export const sendAuthReq = async ( + clientID: string, + authority: string, + authCode: string, + verifier: string, + errorCallBack: any +) => { + try { + const rsp1 = await request({ + url: `${authority}/oauth2/v2.0/token`, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + tenant: "consumers", + client_id: clientID, + scope: SCOPES.join(" "), + code: authCode, + redirect_uri: REDIRECT_URI, + grant_type: "authorization_code", + code_verifier: verifier, + }).toString(), + }); + + const rsp2 = JSON.parse(rsp1); + if (rsp2.error !== undefined) { + return rsp2 as AccessCodeResponseFailedType; + } + return rsp2 as AccessCodeResponseSuccessfulType; + } catch (e) { + console.error(e); + await errorCallBack(e); + } +}; + +export const sendRefreshTokenReq = async ( + clientID: string, + authority: string, + refreshToken: string +) => { + try { + const rsp1 = await request({ + url: `${authority}/oauth2/v2.0/token`, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + tenant: "consumers", + client_id: clientID, + scope: SCOPES.join(" "), + refresh_token: refreshToken, + grant_type: "refresh_token", + }).toString(), + }); + + const rsp2 = JSON.parse(rsp1); + if (rsp2.error !== undefined) { + return rsp2 as AccessCodeResponseFailedType; + } + return rsp2 as AccessCodeResponseSuccessfulType; + } catch (e) { + console.error(e); + throw e; + } +}; + +export const setConfigBySuccessfullAuthInplace = async ( + config: OnedriveFullConfig, + authRes: AccessCodeResponseSuccessfulType, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + console.info("start updating local info of OneDrive Full token"); + config.accessToken = authRes.access_token; + config.accessTokenExpiresAtTime = + Date.now() + authRes.expires_in * 1000 - 5 * 60 * 1000; + config.accessTokenExpiresInSeconds = authRes.expires_in; + config.refreshToken = authRes.refresh_token!; + // BYOC: No forced expiry + config.credentialsShouldBeDeletedAtTime = 0; + + if (saveUpdatedConfigFunc !== undefined) { + await saveUpdatedConfigFunc(); + } + console.info("finish updating local info of OneDrive Full token"); +}; + +//////////////////////////////////////////////////////////////////////////////// +// Path Helpers — uses drive root instead of approot +//////////////////////////////////////////////////////////////////////////////// + +const getOnedrivePath = (fileOrFolderPath: string, remoteBaseDir: string) => { + const prefix = `/drive/root:/${remoteBaseDir}`; + + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + return prefix; + } + if (key.endsWith("/")) { + key = key.slice(0, -1); + } + if (key.startsWith("/")) { + key = `${prefix}${key}`; + } else { + key = `${prefix}/${key}`; + } + return key; +}; + +const fromDriveItemToEntity = ( + x: DriveItem, + remoteBaseDir: string +): Entity => { + if (!x.parentReference?.path || !x.name) { + throw Error( + `OneDrive Full: item missing parentReference.path or name: ${JSON.stringify(x)}` + ); + } + + const fullPath = `${x.parentReference.path}/${x.name}`; + const remoteBaseDirEncoded = encodeURIComponent(remoteBaseDir); + + // Expected prefix patterns for root drive access: + // /drive/root:/${remoteBaseDir} + // /drive/root:/${remoteBaseDirEncoded} + // /drive/items/:/${remoteBaseDir} + + let key = ""; + + // Pattern 1: /drive/root:/remoteBaseDir/... + const rootPrefix = `/drive/root:/${remoteBaseDir}`; + const rootPrefixEnc = `/drive/root:/${remoteBaseDirEncoded}`; + const itemsPrefix = `/drive/items/`; + + if (fullPath.startsWith(`${rootPrefix}/`)) { + key = fullPath.slice(rootPrefix.length + 1); + } else if (fullPath === rootPrefix) { + key = ""; + } else if (fullPath.startsWith(`${rootPrefixEnc}/`)) { + key = fullPath.slice(rootPrefixEnc.length + 1); + } else if (fullPath === rootPrefixEnc) { + key = ""; + } else if (x.parentReference.path.startsWith(itemsPrefix)) { + // /drive/items/:/${remoteBaseDir}/... + const decodedPath = decodeURIComponent(x.parentReference.path); + const colonIdx = decodedPath.indexOf(":"); + if (colonIdx >= 0) { + const afterColon = decodedPath.slice(colonIdx + 1); + if (afterColon.startsWith(`/${remoteBaseDir}/`)) { + key = `${afterColon.slice(`/${remoteBaseDir}/`.length)}/${x.name}`; + } else if (afterColon === `/${remoteBaseDir}`) { + key = x.name; + } else { + throw Error( + `OneDrive Full: unexpected item path structure: ${fullPath}` + ); + } + } else { + throw Error( + `OneDrive Full: items path has no colon: ${decodedPath}` + ); + } + } else { + throw Error( + `OneDrive Full: cannot extract key from path: ${fullPath}` + ); + } + + const isFolder = "folder" in x; + if (isFolder) { + key = `${key}/`; + } + + const mtimeRaw = x.fileSystemInfo?.lastModifiedDateTime; + if (!mtimeRaw) { + throw Error(`OneDrive Full: no mtime for item: ${JSON.stringify(x)}`); + } + const ctimeRaw = x.fileSystemInfo?.createdDateTime ?? mtimeRaw; + + return { + key: key, + keyRaw: key, + mtimeSvr: Date.parse(mtimeRaw), + mtimeCli: Date.parse(mtimeRaw), + ctimeCli: Date.parse(ctimeRaw), + size: isFolder ? 0 : (x.size ?? 0), + sizeRaw: isFolder ? 0 : (x.size ?? 0), + synthesizedFile: false, + } as Entity; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Auth Provider +//////////////////////////////////////////////////////////////////////////////// + +class OneDriveFullAuthProvider { + config: OnedriveFullConfig; + saveFunc: () => Promise; + + constructor(config: OnedriveFullConfig, saveFunc: () => Promise) { + this.config = config; + this.saveFunc = saveFunc; + } + + async getAccessToken(): Promise { + if (!this.config.accessToken || !this.config.refreshToken) { + throw Error("OneDrive Full: user has not authorized yet."); + } + + if (this.config.accessTokenExpiresAtTime > Date.now()) { + return this.config.accessToken; + } + + // Refresh the token + const r = await sendRefreshTokenReq( + this.config.clientID, + this.config.authority, + this.config.refreshToken + ); + if ((r as any).error !== undefined) { + const err = r as AccessCodeResponseFailedType; + throw Error( + `OneDrive Full refresh error: ${err.error}: ${err.error_description}` + ); + } + const success = r as AccessCodeResponseSuccessfulType; + this.config.accessToken = success.access_token; + this.config.refreshToken = success.refresh_token!; + this.config.accessTokenExpiresInSeconds = success.expires_in; + this.config.accessTokenExpiresAtTime = + Date.now() + success.expires_in * 1000 - 120_000; + await this.saveFunc(); + console.info("OneDrive Full accessToken refreshed"); + return this.config.accessToken; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// The Client +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsOnedriveFull extends FakeFs { + kind: "onedrivefull"; + config: OnedriveFullConfig; + remoteBaseDir: string; + vaultFolderExists: boolean; + auth: OneDriveFullAuthProvider; + saveFunc: () => Promise; + foldersCreated: Set; + + constructor( + config: OnedriveFullConfig, + vaultName: string, + saveFunc: () => Promise + ) { + super(); + this.kind = "onedrivefull"; + this.config = config; + this.remoteBaseDir = config.remoteBaseDir || vaultName || ""; + this.vaultFolderExists = false; + this.saveFunc = saveFunc; + this.auth = new OneDriveFullAuthProvider(config, saveFunc); + this.foldersCreated = new Set(); + } + + private _buildUrl(pathFrag: string): string { + const API = "https://graph.microsoft.com/v1.0"; + let url = pathFrag.startsWith("http") ? pathFrag : `${API}${encodeURI(pathFrag)}`; + url = url.replace(/#/g, "%23"); + return url; + } + + private async _getJson(path: string): Promise { + return JSON.parse( + await request({ + url: this._buildUrl(path), + method: "GET", + contentType: "application/json", + headers: { + Authorization: `Bearer ${await this.auth.getAccessToken()}`, + "Cache-Control": "no-cache", + }, + }) + ); + } + + private async _postJson(path: string, payload: any): Promise { + return JSON.parse( + await request({ + url: this._buildUrl(path), + method: "POST", + contentType: "application/json", + body: JSON.stringify(payload), + headers: { + Authorization: `Bearer ${await this.auth.getAccessToken()}`, + }, + }) + ); + } + + private async _patchJson(path: string, payload: any): Promise { + return JSON.parse( + await request({ + url: this._buildUrl(path), + method: "PATCH", + contentType: "application/json", + body: JSON.stringify(payload), + headers: { + Authorization: `Bearer ${await this.auth.getAccessToken()}`, + }, + }) + ); + } + + private async _deleteJson(path: string): Promise { + const url = this._buildUrl(path); + if (VALID_REQURL) { + await requestUrl({ + url, + method: "DELETE", + headers: { Authorization: `Bearer ${await this.auth.getAccessToken()}` }, + }); + } else { + await retryFetch(url, { + method: "DELETE", + headers: { Authorization: `Bearer ${await this.auth.getAccessToken()}` }, + }); + } + } + + private async _putArrayBuffer( + path: string, + payload: ArrayBuffer + ): Promise { + const url = this._buildUrl(path); + const token = await this.auth.getAccessToken(); + const res = await retryFetch(url, { + method: "PUT", + body: payload, + headers: { + "Content-Type": DEFAULT_CONTENT_TYPE, + Authorization: `Bearer ${token}`, + }, + }); + return (await res.json()) as DriveItem | UploadSession; + } + + private async _putByRange( + url: string, + data: Uint8Array, + start: number, + end: number, + total: number + ): Promise { + // Upload session ranges — NO auth header + const res = await retryFetch(url, { + method: "PUT", + body: data.subarray(start, end), + headers: { + "Content-Length": `${end - start}`, + "Content-Range": `bytes ${start}-${end - 1}/${total}`, + "Content-Type": DEFAULT_CONTENT_TYPE, + }, + }); + return (await res.json()) as DriveItem | UploadSession; + } + + async _init(): Promise { + if (!this.config.accessToken || !this.config.refreshToken) { + throw Error("OneDrive Full: user has not authorized yet."); + } + + if (!this.vaultFolderExists) { + // Check if the base dir exists at the drive root + try { + await this._getJson(`/drive/root:/${this.remoteBaseDir}`); + this.vaultFolderExists = true; + } catch { + // Create it + console.info(`OneDrive Full: creating /${this.remoteBaseDir}`); + await this._postJson("/drive/root/children", { + name: this.remoteBaseDir, + folder: {}, + "@microsoft.graph.conflictBehavior": "replace", + }); + this.vaultFolderExists = true; + } + } + } + + async walk(): Promise { + await this._init(); + + const NEXT_KEY = "@odata.nextLink"; + const DELTA_KEY = "@odata.deltaLink"; + + let res = await this._getJson( + `/drive/root:/${this.remoteBaseDir}:/delta` + ); + const items = res.value as DriveItem[]; + + while (NEXT_KEY in res) { + res = await this._getJson(res[NEXT_KEY]); + items.push(...cloneDeep(res.value as DriveItem[])); + } + + if (DELTA_KEY in res) { + this.config.deltaLink = res[DELTA_KEY]; + await this.saveFunc(); + } + + return items + .map((x) => fromDriveItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.key !== "/" && x.key !== ""); + } + + async walkPartial(): Promise { + await this._init(); + + const DELTA_KEY = "@odata.deltaLink"; + const res = await this._getJson( + `/drive/root:/${this.remoteBaseDir}:/delta` + ); + const items = res.value as DriveItem[]; + + if (DELTA_KEY in res) { + this.config.deltaLink = res[DELTA_KEY]; + await this.saveFunc(); + } + + return items + .map((x) => fromDriveItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.key !== "/" && x.key !== ""); + } + + async stat(key: string): Promise { + await this._init(); + const path = getOnedrivePath(key, this.remoteBaseDir); + const rsp = await this._getJson( + `${path}?$select=cTag,eTag,fileSystemInfo,folder,file,name,parentReference,size` + ); + return fromDriveItemToEntity(rsp as DriveItem, this.remoteBaseDir); + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!key.endsWith("/")) throw Error(`mkdir called on non-folder: ${key}`); + await this._init(); + + const uploadFolder = getOnedrivePath(key, this.remoteBaseDir); + if (!this.foldersCreated.has(uploadFolder)) { + const payload: any = { + folder: {}, + "@microsoft.graph.conflictBehavior": "replace", + }; + const fsi: Record = {}; + if (mtime && mtime !== 0) fsi.lastModifiedDateTime = new Date(mtime).toISOString(); + if (ctime && ctime !== 0) fsi.createdDateTime = new Date(ctime).toISOString(); + if (Object.keys(fsi).length > 0) payload.fileSystemInfo = fsi; + await this._patchJson(uploadFolder, payload); + this.foldersCreated.add(uploadFolder); + } + return this.stat(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (key.endsWith("/")) throw Error(`writeFile called on folder: ${key}`); + await this._init(); + + if (content.byteLength === 0) { + if (this.config.emptyFile === "error") { + throw Error( + `${key}: Empty files not allowed in OneDrive. Please add content.` + ); + } + return { + key, keyRaw: key, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: 0, sizeRaw: 0, synthesizedFile: true, + } as Entity; + } + + const remotePath = getOnedrivePath(key, this.remoteBaseDir); + const mtimeStr = new Date(mtime).toISOString(); + const ctimeStr = new Date(ctime).toISOString(); + + const DIRECT_MAX = 4_000_000; // 4 MB + const RANGE_SIZE = 327680 * 20; // ~6.5 MB chunks + + if (content.byteLength < DIRECT_MAX) { + await this._putArrayBuffer( + `${remotePath}:/content?${new URLSearchParams({ + "@microsoft.graph.conflictBehavior": "replace", + })}`, + content + ); + if (mtime !== 0 && ctime !== 0) { + await this._patchJson(remotePath, { + fileSystemInfo: { + lastModifiedDateTime: mtimeStr, + createdDateTime: ctimeStr, + }, + }); + } + } else { + // Large file upload session + const session: UploadSession = await this._postJson( + `${remotePath}:/createUploadSession`, + { + item: { + "@microsoft.graph.conflictBehavior": "replace", + fileSystemInfo: { + lastModifiedDateTime: mtimeStr, + createdDateTime: ctimeStr, + }, + }, + } + ); + const uploadUrl = session.uploadUrl!; + const uint8 = new Uint8Array(content); + let start = 0; + while (start < uint8.byteLength) { + await this._putByRange( + uploadUrl, + uint8, + start, + Math.min(start + RANGE_SIZE, uint8.byteLength), + uint8.byteLength + ); + start += RANGE_SIZE; + } + } + + return this.stat(key); + } + + async readFile(key: string): Promise { + await this._init(); + if (key.endsWith("/")) throw Error(`readFile called on folder: ${key}`); + + const remotePath = getOnedrivePath(key, this.remoteBaseDir); + const rsp = await this._getJson( + `${remotePath}?$select=@microsoft.graph.downloadUrl` + ); + const downloadUrl: string = rsp["@microsoft.graph.downloadUrl"]; + + try { + return await (await retryFetch(downloadUrl, { cache: "no-store" })).arrayBuffer(); + } catch { + // Fallback for CORS issues + return (await requestUrl({ url: downloadUrl, headers: { "Cache-Control": "no-cache" } })).arrayBuffer; + } + } + + async rename(key1: string, key2: string): Promise { + if (!key1 || key1 === "/" || !key2 || key2 === "/") return; + await this._init(); + const remote1 = getOnedrivePath(key1, this.remoteBaseDir); + const remote2 = getOnedrivePath(key2, this.remoteBaseDir); + await this._patchJson(remote1, { name: remote2 }); + } + + async rm(key: string): Promise { + if (!key || key === "/") return; + await this._init(); + await this._deleteJson(getOnedrivePath(key, this.remoteBaseDir)); + } + + async checkConnect(callbackFunc?: any): Promise { + try { + const name = await this.getUserDisplayName(); + if (name === "") throw Error("unknown display name"); + } catch (err) { + callbackFunc?.(err); + return false; + } + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + await this._init(); + const res: User = await this._getJson("/me?$select=displayName"); + return res.displayName || ""; + } + + async revokeAuth(): Promise { + throw new Error("Visit https://account.live.com/consent/Manage to revoke."); + } + + async getRevokeAddr(): Promise { + return "https://account.live.com/consent/Manage"; + } + + allowEmptyFile(): boolean { + return false; + } +} + +export const getShrinkedSettings = (config: OnedriveFullConfig) => { + const c = cloneDeep(config); + c.accessToken = "x"; + c.accessTokenExpiresInSeconds = 1; + c.accessTokenExpiresAtTime = 1; + return c; +}; diff --git a/src/fsPCloud.ts b/src/fsPCloud.ts new file mode 100644 index 0000000..7bb0c16 --- /dev/null +++ b/src/fsPCloud.ts @@ -0,0 +1,661 @@ +import { nanoid } from "nanoid"; +import { requestUrl } from "obsidian"; +import pcloudSdk from "pcloud-sdk-js"; +import { + type Entity, + PCLOUD_CLIENT_ID, + PCLOUD_CLIENT_SECRET, + type PCloudConfig, + COMMAND_URI +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { getFolderLevels, retryFetch } from "./misc"; + +// NOTE: COMMAND_CALLBACK_PCLOUD is defined in baseTypes.ts. +// The redirect URI stays as remotely-save-cb-pcloud to match the registered pCloud app whitelist. + +export const DEFAULT_PCLOUD_CONFIG: PCloudConfig = { + accessToken: "", + hostname: "eapi.pcloud.com", + locationid: 2, + credentialsShouldBeDeletedAtTimeMs: 0, + emptyFile: "skip", + kind: "pcloud", +}; + +export interface AuthAllowFirstRes { + code: string; + state?: string; + locationid: 1 | 2; + hostname: "api.pcloud.com" | "eapi.pcloud.com"; +} + +/** + * https://docs.pcloud.com/methods/oauth_2.0/authorize.html + */ +export const generateAuthUrl = async (hasCallback: boolean) => { + const clientID = PCLOUD_CLIENT_ID; + const state = nanoid(); + let authUrl = `https://my.pcloud.com/oauth2/authorize?response_type=code&client_id=${clientID}&state=${state}`; + if (hasCallback) { + // HARDCODED OVERRIDE: Freeloading on the original dev's app config + // since the pCloud API portal is unresponsive/locked for new app registration. + authUrl += `&redirect_uri=obsidian://remotely-save-cb-pcloud`; + } + return { + authUrl, + state, + }; +}; + +interface AuthResSucc { + result: number; + access_token: string; + token_type: "bearer"; + uid: number; + locationid: number; +} + +/** + * https://docs.pcloud.com/methods/oauth_2.0/oauth2_token.html + */ +export const sendAuthReq = async ( + hostname: string, + authCode: string, + errorCallBack: any +) => { + const clientID = PCLOUD_CLIENT_ID ?? ""; + const clientSecret = PCLOUD_CLIENT_SECRET ?? ""; + try { + const k = { + code: authCode, + client_id: clientID, + client_secret: clientSecret, + }; + // console.debug(k); + const resp1 = await fetch(`https://${hostname}/oauth2_token`, { + method: "POST", + body: new URLSearchParams(k), + }); + const resp2: AuthResSucc = await resp1.json(); + // console.debug(resp2); + if (resp2?.result !== 0) { + throw Error(`result is not 0 (success) in the end`); + } + if (!("access_token" in resp2)) { + throw Error(`no access_token in the end`); + } + return resp2; + } catch (e) { + console.error(e); + if (errorCallBack !== undefined) { + await errorCallBack(e); + } + } +}; + +export const setConfigBySuccessfullAuthInplace = async ( + config: PCloudConfig, + authAllowFirstRes: AuthAllowFirstRes, + authRes: AuthResSucc | undefined, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + if (authRes === undefined) { + throw Error( + `BYOC pCloud auth failed — no token received. Please try authenticating again.` + ); + } + + config.accessToken = authRes.access_token; + config.hostname = authAllowFirstRes.hostname; + config.locationid = authAllowFirstRes.locationid; + + // BYOC: No credential expiry — tokens persist until manually revoked. + config.credentialsShouldBeDeletedAtTimeMs = 0; + + await saveUpdatedConfigFunc?.(); + + console.info("finish updating local info of pCloud token"); +}; + +interface PCloudEntity extends Entity { + id: number; + parentID: number | undefined; + parentIDPath: string | undefined; + isFolder: boolean; + hashPCloud: number | undefined; +} + +interface File { + contenttype: string; + created: string; + fileid: number; + hash: number; + id: string; // "f" + fileid + isfolder: false; + modified: string; + name: string; + parentfolderid: number; + size: number; +} + +interface Folder { + contents: (Folder | File)[] | undefined; + id: string; // "d"+folderid + folderid: number; + isfolder: true; + created: string; + modified: string; + name: string; + parentfolderid: number; +} + +interface StatRawResponse { + result: number; + fileids: number[]; + metadata: (File | Folder)[]; + checksums: { sha1: string; sha256?: string; md5?: string }[] | undefined; +} + +const fromRawResponseToEntity = ( + item: Folder | File, + parentFolderPath: string | undefined /* for bfs */ +): PCloudEntity => { + if (item.parentfolderid === undefined || item.parentfolderid === 0) { + throw Error( + `parentfolderid=${item.parentfolderid} should not be in fromRawResponseToEntity` + ); + } + + let keyRaw = item.name; + let size = 0; + let hashPCloud: number | undefined = undefined; + let hash: string | undefined = undefined; + let id: number | undefined = undefined; + if ( + parentFolderPath !== undefined && + parentFolderPath !== "" && + parentFolderPath !== "/" + ) { + if (!parentFolderPath.endsWith("/")) { + throw Error( + `parentFolderPath=${parentFolderPath} should not be in fromRawResponseToEntity` + ); + } + keyRaw = `${parentFolderPath}${item.name}`; + } + + if (item.isfolder) { + keyRaw = `${keyRaw}/`; + id = item.folderid; + } else { + size = item.size; + hashPCloud = item.hash; + hash = `${item.hash}`; + id = item.fileid; + } + + const mtime = new Date(item.modified).valueOf(); + + return { + key: keyRaw, + keyRaw: keyRaw, + mtimeCli: mtime, + mtimeSvr: mtime, + id: id, + parentID: item.parentfolderid, + isFolder: item.isfolder, + size: size, + sizeRaw: size, + hash: hash, + hashPCloud: hashPCloud, + parentIDPath: parentFolderPath, + }; +}; + +const fromNestedFolderToEntityListAndCache = ( + root: Folder +): { entities: PCloudEntity[]; key2Entity: Record } => { + // console.debug("root:"); + // console.debug(root); + + const entities: PCloudEntity[] = []; + const key2Entity: Record = {}; + + if (root.contents === undefined || root.contents.length === 0) { + // console.debug(`early return`); + return { + entities, + key2Entity, + }; + } + + let parents: { + folderPath: string; + itself: Folder | File; + }[] = []; + for (const f of root.contents ?? []) { + parents.push({ + folderPath: "", + itself: f, + }); + } + + while (parents.length !== 0) { + const children: typeof parents = []; + for (const { folderPath, itself } of parents) { + if (itself.isfolder && itself.folderid === root.folderid) { + // special, ignore root folder itself + } else { + const entity = fromRawResponseToEntity(itself, folderPath); + entities.push(entity); + key2Entity[entity.keyRaw] = entity; + } + + if ( + itself.isfolder && + itself.contents !== undefined && + itself.contents.length > 0 + ) { + for (const f of itself.contents) { + if (folderPath === "" || folderPath === "/") { + const child = { + itself: f, + folderPath: `${itself.name}/`, + }; + children.push(child); + } else { + const child = { + itself: f, + folderPath: `${folderPath}${itself.name}/`, + }; + children.push(child); + } + } + } + } + parents = children; + } + + // console.debug("entities:"); + // console.debug(entities); + // console.debug("key2Entity:"); + // console.debug(key2Entity); + + return { + entities, + key2Entity, + }; +}; + +const getPCloudPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + return `/${remoteBaseDir}/${fileOrFolderPath}`; +}; + +export class FakeFsPCloud extends FakeFs { + kind: string; + + pCloudConfig: PCloudConfig; + remoteBaseDir: string; + vaultFolderExists: boolean; + saveUpdatedConfigFunc: () => Promise; + + keyToPCloudEntity: Record; + baseDirID: number; + + client: pcloudSdk.Client | null; + + constructor( + pCloudConfig: PCloudConfig, + vaultName: string, + saveUpdatedConfigFunc: () => Promise + ) { + super(); + this.kind = "pcloud"; + this.pCloudConfig = pCloudConfig; + this.remoteBaseDir = this.pCloudConfig.remoteBaseDir || vaultName || ""; + this.vaultFolderExists = false; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + this.keyToPCloudEntity = {}; + this.baseDirID = 0; + + // Lazy: delay SDK client creation until _init(). + // The constructor runs when the settings panel renders, at which point + // accessToken may still be empty string. createClient() throws if token absent. + this.client = null; + } + + async _init() { + // Lazy-create the SDK client on first real use. + if (!this.client) { + const token = this.pCloudConfig.accessToken; + if (!token) { + throw Error("pCloud: no access token — please authenticate first."); + } + // pCloud SDK reads locationid from global scope; coerce string→number + // because URL callback params always arrive as strings. + (global as any).locationid = + typeof this.pCloudConfig.locationid === "string" + ? parseInt(this.pCloudConfig.locationid as any, 10) + : (this.pCloudConfig.locationid ?? 1); + this.client = pcloudSdk.createClient(token); + } + + if (this.vaultFolderExists) { + return; + } else { + const root = (await this.client.listfolder(0, { + recursive: false, + })) as Folder; + + // find? + if (root.contents === undefined) { + throw Error(`cannot listfolder of root!`); + } + const found = root.contents.filter( + (x) => x.isfolder && x.name === this.remoteBaseDir + ); + if (found.length > 0) { + // we find it! + const f = found[0] as Folder; + this.baseDirID = f.folderid; + this.vaultFolderExists = true; + } else { + // not found, let's create it! + const f: Folder = await this.client.createfolder(this.remoteBaseDir, 0); + // console.debug(f); + this.baseDirID = f.folderid; + this.vaultFolderExists = true; + } + } + } + + /** Non-null accessor — only call after _init() has resolved. */ + private get _sdk(): pcloudSdk.Client { + return this.client!; + } + +async _getAccessToken() { + if (this.pCloudConfig.accessToken === "") { + throw Error("The user has not manually auth yet."); + } + + return this.pCloudConfig.accessToken; + + // TODO: no expire date? + // https://docs.pcloud.com/methods/intro/authentication.html + } + + async walk(): Promise { + await this._init(); + const rsp = (await this.client.listfolder(this.baseDirID, { + recursive: true, + })) as Folder; + + const { entities, key2Entity } = fromNestedFolderToEntityListAndCache(rsp); + + this.keyToPCloudEntity = Object.assign(this.keyToPCloudEntity, key2Entity); + + return entities; + } + + async walkPartial(): Promise { + await this._init(); + const rsp = (await this.client.listfolder(this.baseDirID, { + recursive: false, + })) as Folder; + const { entities, key2Entity } = fromNestedFolderToEntityListAndCache(rsp); + + this.keyToPCloudEntity = Object.assign(this.keyToPCloudEntity, key2Entity); + + return entities; + } + + async stat(key: string): Promise { + await this._init(); + + // TODO: we already have a cache, should we call again? + const cachedEntity = this.keyToPCloudEntity[key]; + const fileID = cachedEntity?.id; + if (cachedEntity === undefined || fileID === undefined) { + throw Error(`no fileID found for key=${key}`); + } + + // why? pcloud doesn't have stat api?? + return cachedEntity; + } + + async mkdir( + key: string, + mtime?: number | undefined, + ctime?: number | undefined + ): Promise { + if (!key.endsWith("/")) { + throw Error(`you should not mkdir on key=${key}`); + } + + await this._init(); + + const cachedEntity = this.keyToPCloudEntity[key]; + const fileID = cachedEntity?.id; + if (cachedEntity !== undefined && fileID !== undefined) { + return cachedEntity; + } + + // xxx/ => ["xxx"] + // xxx/yyy/zzz/ => ["xxx", "xxx/yyy", "xxx/yyy/zzz"] + const folderLevels = getFolderLevels(key); + let parentFolderPath: string | undefined = undefined; + let parentID: number | undefined = undefined; + if (folderLevels.length === 0) { + throw Error(`cannot getFolderLevels of ${key}`); + } else if (folderLevels.length === 1) { + parentID = this.baseDirID; + parentFolderPath = ""; // ignore base dir + } else { + // length > 1 + parentFolderPath = `${folderLevels[folderLevels.length - 2]}/`; + if (!(parentFolderPath in this.keyToPCloudEntity)) { + throw Error( + `parent of ${key}: ${parentFolderPath} is not created before??` + ); + } + parentID = this.keyToPCloudEntity[parentFolderPath].id; + } + + // xxx/yyy/zzz/ => ["xxx", "xxx/yyy", "xxx/yyy/zzz"] => "xxx/yyy/zzz" => "zzz" + let folderItselfWithoutSlash = folderLevels[folderLevels.length - 1]; + folderItselfWithoutSlash = folderItselfWithoutSlash.split("/").pop()!; + + const f = await this.client.createfolder( + folderItselfWithoutSlash, + parentID + ); + + const entity = fromRawResponseToEntity(f, parentFolderPath); + // insert into cache + this.keyToPCloudEntity[key] = entity; + return entity; + } + + /** + * https://docs.pcloud.com/methods/file/uploadfile.html + */ + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (key.endsWith("/")) { + throw Error(`should not call writeFile on ${key}`); + } + + await this._init(); + + const prevCachedEntity: PCloudEntity | undefined = + this.keyToPCloudEntity[key]; + const prevFileID: number | undefined = prevCachedEntity?.id; + + let parentID: number | undefined = undefined; + let parentFolderPath: string | undefined = undefined; + + // "xxx" => [] + // "xxx/yyy/zzz.md" => ["xxx", "xxx/yyy"] + const folderLevels = getFolderLevels(key); + if (folderLevels.length === 0) { + // root + parentID = this.baseDirID; + parentFolderPath = ""; + } else { + parentFolderPath = `${folderLevels[folderLevels.length - 1]}/`; + if (!(parentFolderPath in this.keyToPCloudEntity)) { + throw Error( + `parent of ${key}: ${parentFolderPath} is not created before??` + ); + } + parentID = this.keyToPCloudEntity[parentFolderPath].id; + } + + const fileItself = key.split("/").pop()!; + + // no idea how to use the sdk, let's use https here + // https://docs.pcloud.com/methods/file/uploadfile.html + const params = new URLSearchParams({ + access_token: await this._getAccessToken(), + folderid: `${parentID}`, + filename: fileItself, + nopartial: `1`, + renameifexists: `0`, + mtime: `${Math.floor(mtime / 1000.0)}`, + ctime: `${Math.floor(ctime / 1000.0)}`, + }); + const apiUrl = `https://${this.pCloudConfig.hostname}/uploadfile?${params}`; + + if (content.byteLength > 0) { + const rsp = await retryFetch(apiUrl, { + method: "PUT", + body: content, + }); + const f: StatRawResponse = await rsp.json(); + const entity = fromRawResponseToEntity(f.metadata[0], parentFolderPath); + // console.debug(entity); + this.keyToPCloudEntity[key] = entity; + return entity; + } else { + // no idea why pcloud doesn't work for empty files + // it can be uploaded successfully but the call doesn't end + // we abort it and stat it manually. + // console.warn(`uploading empty file ${key}`); + const controller = new AbortController(); + const timeoutMs = 300; // just a random reasonable number + const id = setTimeout(() => controller.abort(), timeoutMs); + try { + const rsp = await fetch(apiUrl, { + method: "PUT", + body: content, + signal: controller.signal, + }); + } catch (e) { + // console.warn(`we abort the request of uploading empty file ${key}:`); + // console.warn(e); + } finally { + clearTimeout(id); + } + + // raw stat here + // https://docs.pcloud.com/methods/file/stat.html + const params = new URLSearchParams({ + access_token: await this._getAccessToken(), + path: getPCloudPath(key, this.remoteBaseDir), + }); + const apiUrlStat = `https://${this.pCloudConfig.hostname}/stat?${params}`; + const rsp2 = await retryFetch(apiUrlStat); + const f = await rsp2.json(); + const entity = fromRawResponseToEntity(f.metadata, parentFolderPath); + // console.warn(entity); + this.keyToPCloudEntity[key] = entity; + return entity; + } + } + + async readFile(key: string): Promise { + await this._init(); + const cachedEntity = this.keyToPCloudEntity[key]; + const fileID = cachedEntity?.id; + if (cachedEntity === undefined || fileID === undefined) { + throw Error(`no fileID found for key=${key}`); + } + + const params = new URLSearchParams({ + access_token: await this._getAccessToken(), + forcedownload: `1`, + fileid: `${fileID}`, + }); + const urlMeta = `https://${this.pCloudConfig.hostname}/getfilelink?${params}`; + + // Referrer is restricted to pcloud.com. + // we need to bypass it + const meta = (await requestUrl(urlMeta)).json; + // console.debug(meta); + const link: string = `https://${meta.hosts[0]}${meta.path}`; + const rsp = await requestUrl(link); + const content = rsp.arrayBuffer; + return content; + } + + async rename(key1: string, key2: string): Promise { + await this._init(); + throw new Error("Method not implemented."); + } + + async rm(key: string): Promise { + await this._init(); + + const cachedEntity = this.keyToPCloudEntity[key]; + const fileID = cachedEntity?.id; + if (cachedEntity === undefined || fileID === undefined) { + throw Error(`no fileID found for key=${key}`); + } + + if (key.endsWith("/")) { + await this.client.deletefolder(fileID); + } else { + await this.client.deletefile(fileID); + } + } + + async checkConnect(callbackFunc?: any): Promise { + // if we can init, we can connect + try { + await this._init(); + } catch (err) { + console.debug(err); + callbackFunc?.(err); + return false; + } + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + await this._init(); + // https://docs.pcloud.com/methods/general/userinfo.html + const params = new URLSearchParams({ + access_token: await this._getAccessToken(), + }); + const url = `https://${this.pCloudConfig.hostname}/userinfo?${params}`; + const rsp = await retryFetch(url); + const info = await rsp.json(); + // pCloud returns `email` as the primary identifier + return (info.email as string) ?? "pCloud user"; + } + + async revokeAuth(): Promise { + await this._init(); + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsS3.ts b/src/fsS3.ts new file mode 100644 index 0000000..f07aeff --- /dev/null +++ b/src/fsS3.ts @@ -0,0 +1,871 @@ +import { Buffer } from "buffer"; +import * as path from "path"; +import { Readable } from "stream"; +import type { PutObjectCommandInput, _Object } from "@aws-sdk/client-s3"; +import { + DeleteObjectCommand, + GetObjectCommand, + HeadObjectCommand, + type HeadObjectCommandOutput, + ListObjectsV2Command, + type ListObjectsV2CommandInput, + PutObjectCommand, + S3Client, +} from "@aws-sdk/client-s3"; +import { Upload } from "@aws-sdk/lib-storage"; +import type { HttpHandlerOptions } from "@aws-sdk/types"; +import { + FetchHttpHandler, + type FetchHttpHandlerOptions, +} from "@smithy/fetch-http-handler"; +// @ts-ignore +import { requestTimeout } from "@smithy/fetch-http-handler/dist-es/request-timeout"; +import { type HttpRequest, HttpResponse } from "@smithy/protocol-http"; +import { buildQueryString } from "@smithy/querystring-builder"; +// biome-ignore lint/suspicious/noShadowRestrictedNames: +import AggregateError from "aggregate-error"; +import * as mime from "mime-types"; +import { Platform, type RequestUrlParam, requestUrl } from "obsidian"; +import PQueue from "p-queue"; +import { DEFAULT_CONTENT_TYPE, type S3Config } from "./baseTypes"; +import { VALID_REQURL } from "./baseTypesObs"; +import { bufferToArrayBuffer, getFolderLevels } from "./misc"; + +import type { Entity } from "./baseTypes"; +import { FakeFs } from "./fsAll"; + +//////////////////////////////////////////////////////////////////////////////// +// special handler using Obsidian requestUrl +//////////////////////////////////////////////////////////////////////////////// + +/** + * This is close to origin implementation of FetchHttpHandler + * https://github.com/aws/aws-sdk-js-v3/blob/main/packages/fetch-http-handler/src/fetch-http-handler.ts + * that is released under Apache 2 License. + * But this uses Obsidian requestUrl instead. + */ +class ObsHttpHandler extends FetchHttpHandler { + requestTimeoutInMs: number | undefined; + reverseProxyNoSignUrl: string | undefined; + constructor( + options?: FetchHttpHandlerOptions, + reverseProxyNoSignUrl?: string + ) { + super(options); + this.requestTimeoutInMs = + options === undefined ? undefined : options.requestTimeout; + this.reverseProxyNoSignUrl = reverseProxyNoSignUrl; + } + async handle( + request: HttpRequest, + { abortSignal }: HttpHandlerOptions = {} + ): Promise<{ response: HttpResponse }> { + if (abortSignal?.aborted) { + const abortError = new Error("Request aborted"); + abortError.name = "AbortError"; + return Promise.reject(abortError); + } + + let path = request.path; + if (request.query) { + const queryString = buildQueryString(request.query); + if (queryString) { + path += `?${queryString}`; + } + } + + const { port, method } = request; + let url = `${request.protocol}//${request.hostname}${ + port ? `:${port}` : "" + }${path}`; + if ( + this.reverseProxyNoSignUrl !== undefined && + this.reverseProxyNoSignUrl !== "" + ) { + const urlObj = new URL(url); + urlObj.host = this.reverseProxyNoSignUrl; + url = urlObj.href; + } + const body = + method === "GET" || method === "HEAD" ? undefined : request.body; + + const transformedHeaders: Record = {}; + for (const key of Object.keys(request.headers)) { + const keyLower = key.toLowerCase(); + if (keyLower === "host" || keyLower === "content-length") { + continue; + } + transformedHeaders[keyLower] = request.headers[key]; + } + + let contentType: string | undefined = undefined; + if (transformedHeaders["content-type"] !== undefined) { + contentType = transformedHeaders["content-type"]; + } + + let transformedBody: any = body; + if (ArrayBuffer.isView(body)) { + transformedBody = bufferToArrayBuffer(body); + } + + const param: RequestUrlParam = { + body: transformedBody, + headers: transformedHeaders, + method: method, + url: url, + contentType: contentType, + }; + + const raceOfPromises = [ + requestUrl(param).then((rsp) => { + const headers = rsp.headers; + const headersLower: Record = {}; + for (const key of Object.keys(headers)) { + headersLower[key.toLowerCase()] = headers[key]; + } + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array(rsp.arrayBuffer)); + controller.close(); + }, + }); + return { + response: new HttpResponse({ + headers: headersLower, + statusCode: rsp.status, + body: stream, + }), + }; + }), + requestTimeout(this.requestTimeoutInMs), + ]; + + if (abortSignal) { + raceOfPromises.push( + new Promise((resolve, reject) => { + abortSignal.onabort = () => { + const abortError = new Error("Request aborted"); + abortError.name = "AbortError"; + reject(abortError); + }; + }) + ); + } + return Promise.race(raceOfPromises); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// other stuffs +//////////////////////////////////////////////////////////////////////////////// + +export const simpleTransRemotePrefix = (x: string) => { + if (x === undefined) { + return ""; + } + let y = path.posix.normalize(x.trim()); + if (y === undefined || y === "" || y === "/" || y === ".") { + return ""; + } + if (y.startsWith("/")) { + y = y.slice(1); + } + if (!y.endsWith("/")) { + y = `${y}/`; + } + return y; +}; + +export const DEFAULT_S3_CONFIG: S3Config = { + s3Endpoint: "", + s3Region: "", + s3AccessKeyID: "", + s3SecretAccessKey: "", + s3BucketName: "", + bypassCorsLocally: true, + partsConcurrency: 20, + forcePathStyle: false, + remotePrefix: "", + useAccurateMTime: false, // it causes money, disable by default + reverseProxyNoSignUrl: "", + generateFolderObject: false, // new version, by default not generate folders +}; + +/** + * The Body of resp of aws GetObject has mix types + * and we want to get ArrayBuffer here. + * See https://github.com/aws/aws-sdk-js-v3/issues/1877 + * @param b The Body of GetObject + * @returns Promise + */ +const getObjectBodyToArrayBuffer = async ( + b: Readable | ReadableStream | Blob | undefined +) => { + if (b === undefined) { + throw Error(`ObjectBody is undefined and don't know how to deal with it`); + } + if (b instanceof Readable) { + return (await new Promise((resolve, reject) => { + const chunks: Uint8Array[] = []; + b.on("data", (chunk) => chunks.push(chunk)); + b.on("error", reject); + b.on("end", () => resolve(bufferToArrayBuffer(Buffer.concat(chunks)))); + })) as ArrayBuffer; + } else if (b instanceof ReadableStream) { + return await new Response(b, {}).arrayBuffer(); + } else if (b instanceof Blob) { + return await b.arrayBuffer(); + } else { + throw TypeError(`The type of ${b} is not one of the supported types`); + } +}; + +const getS3Client = (s3Config: S3Config) => { + let endpoint = s3Config.s3Endpoint; + if (!(endpoint.startsWith("http://") || endpoint.startsWith("https://"))) { + endpoint = `https://${endpoint}`; + } + + let s3Client: S3Client; + if (VALID_REQURL && s3Config.bypassCorsLocally) { + s3Client = new S3Client({ + region: s3Config.s3Region, + endpoint: endpoint, + forcePathStyle: s3Config.forcePathStyle, + credentials: { + accessKeyId: s3Config.s3AccessKeyID, + secretAccessKey: s3Config.s3SecretAccessKey, + }, + requestHandler: new ObsHttpHandler( + undefined, + s3Config.reverseProxyNoSignUrl + ), + }); + } else { + s3Client = new S3Client({ + region: s3Config.s3Region, + endpoint: endpoint, + forcePathStyle: s3Config.forcePathStyle, + credentials: { + accessKeyId: s3Config.s3AccessKeyID, + secretAccessKey: s3Config.s3SecretAccessKey, + }, + }); + } + + s3Client.middlewareStack.add( + (next, context) => (args) => { + (args.request as any).headers["cache-control"] = "no-cache"; + return next(args); + }, + { + step: "build", + } + ); + + return s3Client; +}; + +const getLocalNoPrefixPath = ( + fileOrFolderPathWithRemotePrefix: string, + remotePrefix: string +) => { + if ( + !( + fileOrFolderPathWithRemotePrefix === `${remotePrefix}` || + fileOrFolderPathWithRemotePrefix.startsWith(`${remotePrefix}`) + ) + ) { + throw Error( + `"${fileOrFolderPathWithRemotePrefix}" doesn't starts with "${remotePrefix}"` + ); + } + return fileOrFolderPathWithRemotePrefix.slice(`${remotePrefix}`.length); +}; + +const getRemoteWithPrefixPath = ( + fileOrFolderPath: string, + remotePrefix: string +) => { + if (remotePrefix === undefined || remotePrefix === "") { + return fileOrFolderPath; + } + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + // special + key = remotePrefix; + } + if (!fileOrFolderPath.startsWith("/")) { + key = `${remotePrefix}${fileOrFolderPath}`; + } + return key; +}; + +const fromS3ObjectToEntity = ( + x: _Object, + remotePrefix: string, + mtimeRecords: Record, + ctimeRecords: Record +) => { + // console.debug(`fromS3ObjectToEntity: ${x.Key!}, ${JSON.stringify(x,null,2)}`); + // S3 officially only supports seconds precision!!!!! + if (x.LastModified === undefined) { + throw Error( + `s3 object ${x.Key!} doesn't have LastModified value: ${JSON.stringify( + x + )}` + ); + } + const mtimeSvr = Math.floor(x.LastModified.valueOf() / 1000.0) * 1000; + let mtimeCli = mtimeSvr; + if (x.Key! in mtimeRecords) { + const m2 = mtimeRecords[x.Key!]; + if (m2 !== 0) { + // to be compatible with RClone, we read and store the time in seconds in new version! + if (m2 >= 1000000000000) { + // it's a millsecond, uploaded by old codes.. + mtimeCli = m2; + } else { + // it's a second, uploaded by new codes of the plugin from March 24, 2024 + mtimeCli = m2 * 1000; + } + } + } + const key = getLocalNoPrefixPath(x.Key!, remotePrefix); // we remove prefix here + const r: Entity = { + key: key, // from s3's repsective, the keyRaw is the key, we will change it in decyption + keyRaw: key, + mtimeSvr: mtimeSvr, + mtimeCli: mtimeCli, + sizeRaw: x.Size!, + size: x.Size!, // from s3's repsective, the sizeRaw is the size, we will change it in decyption + etag: x.ETag, + synthesizedFolder: false, + }; + return r; +}; + +const fromS3HeadObjectToEntity = ( + fileOrFolderPathWithRemotePrefix: string, + x: HeadObjectCommandOutput, + remotePrefix: string, + useAccurateMTime: boolean +) => { + // console.debug(`fromS3HeadObjectToEntity: ${fileOrFolderPathWithRemotePrefix}: ${JSON.stringify(x,null,2)}`); + // S3 officially only supports seconds precision!!!!! + if (x.LastModified === undefined) { + throw Error( + `s3 object ${fileOrFolderPathWithRemotePrefix} doesn't have LastModified value: ${JSON.stringify( + x + )}` + ); + } + const mtimeSvr = Math.floor(x.LastModified.valueOf() / 1000.0) * 1000; + let mtimeCli = mtimeSvr; + if (useAccurateMTime && x.Metadata !== undefined) { + const m2 = Math.floor( + Number.parseFloat(x.Metadata.mtime || x.Metadata.MTime || "0") + ); + if (m2 !== 0) { + // to be compatible with RClone, we read and store the time in seconds in new version! + if (m2 >= 1000000000000) { + // it's a millsecond, uploaded by old codes.. + mtimeCli = m2; + } else { + // it's a second, uploaded by new codes of the plugin from March 24, 2024 + mtimeCli = m2 * 1000; + } + } + } + // console.debug( + // `fromS3HeadObjectToEntity, fileOrFolderPathWithRemotePrefix=${fileOrFolderPathWithRemotePrefix}, remotePrefix=${remotePrefix}, x=${JSON.stringify( + // x + // )} ` + // ); + const key = getLocalNoPrefixPath( + fileOrFolderPathWithRemotePrefix, + remotePrefix + ); + // console.debug(`fromS3HeadObjectToEntity, key=${key} after removing prefix`); + return { + key: key, + keyRaw: key, + mtimeSvr: mtimeSvr, + mtimeCli: mtimeCli, + sizeRaw: x.ContentLength, + size: x.ContentLength, + etag: x.ETag, + synthesizedFolder: false, + } as Entity; +}; + +export class FakeFsS3 extends FakeFs { + s3Config: S3Config; + s3Client: S3Client; + kind: "s3"; + synthFoldersCache: Record; + constructor(s3Config: S3Config) { + super(); + this.s3Config = s3Config; + this.s3Client = getS3Client(s3Config); + this.kind = "s3"; + this.synthFoldersCache = {}; + } + + async walk(): Promise { + const res = ( + await this._walkFromRoot(this.s3Config.remotePrefix, false) + ).filter((x) => x.key !== "" && x.key !== "/"); + return res; + } + + async walkPartial(): Promise { + const res = ( + await this._walkFromRoot(this.s3Config.remotePrefix, true) + ).filter((x) => x.key !== "" && x.key !== "/"); + return res; + } + + /** + * the input key contains basedir (prefix), + * but the result doesn't contain it. + */ + async _walkFromRoot(prefixOfRawKeys: string | undefined, partial: boolean) { + const confCmd = { + Bucket: this.s3Config.s3BucketName, + } as ListObjectsV2CommandInput; + if (prefixOfRawKeys !== undefined && prefixOfRawKeys !== "") { + confCmd.Prefix = prefixOfRawKeys; + } + if (partial) { + confCmd.MaxKeys = 10; // no need to list more! + } + + const contents = [] as _Object[]; + const mtimeRecords: Record = {}; + const ctimeRecords: Record = {}; + const partsConcurrency = partial ? 1 : this.s3Config.partsConcurrency; + const queueHead = new PQueue({ + concurrency: partsConcurrency, + autoStart: true, + }); + queueHead.on("error", (error) => { + queueHead.pause(); + queueHead.clear(); + throw error; + }); + + let isTruncated = true; + do { + const rsp = await this.s3Client.send(new ListObjectsV2Command(confCmd)); + + if (rsp.$metadata.httpStatusCode !== 200) { + throw Error("some thing bad while listing remote!"); + } + if (rsp.Contents === undefined) { + break; + } + contents.push(...rsp.Contents); + + if (this.s3Config.useAccurateMTime) { + // head requests of all objects, love it + for (const content of rsp.Contents) { + queueHead.add(async () => { + const rspHead = await this.s3Client.send( + new HeadObjectCommand({ + Bucket: this.s3Config.s3BucketName, + Key: content.Key, + }) + ); + if (rspHead.$metadata.httpStatusCode !== 200) { + throw Error("some thing bad while heading single object!"); + } + if (rspHead.Metadata === undefined) { + // pass + } else { + mtimeRecords[content.Key!] = Math.floor( + Number.parseFloat( + rspHead.Metadata.mtime || rspHead.Metadata.MTime || "0" + ) + ); + ctimeRecords[content.Key!] = Math.floor( + Number.parseFloat( + rspHead.Metadata.ctime || rspHead.Metadata.CTime || "0" + ) + ); + } + }); + } + } + + if (partial) { + // do not loop over + isTruncated = false; + } else { + // loop over + isTruncated = rsp.IsTruncated ?? false; + confCmd.ContinuationToken = rsp.NextContinuationToken; + if ( + isTruncated && + (confCmd.ContinuationToken === undefined || + confCmd.ContinuationToken === "") + ) { + throw Error("isTruncated is true but no continuationToken provided"); + } + } + } while (isTruncated); + + // wait for any head requests + await queueHead.onIdle(); + + // ensemble fake rsp + // in the end, we need to transform the response list + // back to the local contents-alike list + const res: Entity[] = []; + const realEnrities = new Set(); + for (const remoteObj of contents) { + const remoteEntity = fromS3ObjectToEntity( + remoteObj, + this.s3Config.remotePrefix ?? "", + mtimeRecords, + ctimeRecords + ); + realEnrities.add(remoteEntity.key!); + res.push(remoteEntity); + + for (const f of getFolderLevels(remoteEntity.key!, true)) { + if (realEnrities.has(f)) { + delete this.synthFoldersCache[f]; + continue; + } + if ( + !this.synthFoldersCache.hasOwnProperty(f) || + remoteEntity.mtimeSvr! >= this.synthFoldersCache[f].mtimeSvr! + ) { + this.synthFoldersCache[f] = { + key: f, + keyRaw: f, + size: 0, + sizeRaw: 0, + sizeEnc: 0, + mtimeSvr: remoteEntity.mtimeSvr, + mtimeSvrFmt: remoteEntity.mtimeSvrFmt, + mtimeCli: remoteEntity.mtimeCli, + mtimeCliFmt: remoteEntity.mtimeCliFmt, + synthesizedFolder: true, + }; + } + } + } + for (const key of Object.keys(this.synthFoldersCache)) { + res.push(this.synthFoldersCache[key]); + } + return res; + } + + async stat(key: string): Promise { + if (this.synthFoldersCache.hasOwnProperty(key)) { + return this.synthFoldersCache[key]; + } + let keyFullPath = key; + keyFullPath = getRemoteWithPrefixPath( + keyFullPath, + this.s3Config.remotePrefix ?? "" + ); + return await this._statFromRoot(keyFullPath); + } + + /** + * the input key contains basedir (prefix), + * but the result doesn't contain it. + */ + async _statFromRoot(key: string): Promise { + if ( + this.s3Config.remotePrefix !== undefined && + this.s3Config.remotePrefix !== "" && + !key.startsWith(this.s3Config.remotePrefix) + ) { + throw Error(`_statFromRoot should only accept prefix-ed path`); + } + const res = await this.s3Client.send( + new HeadObjectCommand({ + Bucket: this.s3Config.s3BucketName, + Key: key, + }) + ); + + return fromS3HeadObjectToEntity( + key, + res, + this.s3Config.remotePrefix ?? "", + this.s3Config.useAccurateMTime ?? false + ); + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!key.endsWith("/")) { + throw new Error(`You should not call mkdir on ${key}!`); + } + + const generateFolderObject = this.s3Config.generateFolderObject ?? false; + if (!generateFolderObject) { + const synth = { + key: key, + keyRaw: key, + size: 0, + sizeRaw: 0, + sizeEnc: 0, + mtimeSvr: mtime, + mtimeCli: mtime, + synthesizedFolder: true, + }; + this.synthFoldersCache[key] = synth; + return synth; + } + + const uploadFile = getRemoteWithPrefixPath( + key, + this.s3Config.remotePrefix ?? "" + ); + return await this._mkdirFromRoot(uploadFile, mtime, ctime); + } + + async _mkdirFromRoot(key: string, mtime?: number, ctime?: number) { + if ( + this.s3Config.remotePrefix !== undefined && + this.s3Config.remotePrefix !== "" && + !key.startsWith(this.s3Config.remotePrefix) + ) { + throw Error(`_mkdirFromRoot should only accept prefix-ed path`); + } + + const contentType = DEFAULT_CONTENT_TYPE; + const p: PutObjectCommandInput = { + Bucket: this.s3Config.s3BucketName, + Key: key, + Body: "", + ContentType: contentType, + ContentLength: 0, // interesting we need to set this to avoid the warning + }; + const metadata: Record = {}; + if (mtime !== undefined && mtime !== 0) { + metadata["MTime"] = `${mtime / 1000.0}`; + } + if (ctime !== undefined && ctime !== 0) { + metadata["CTime"] = `${ctime / 1000.0}`; + } + if (Object.keys(metadata).length > 0) { + p["Metadata"] = metadata; + } + await this.s3Client.send(new PutObjectCommand(p)); + return await this._statFromRoot(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const uploadFile = getRemoteWithPrefixPath( + key, + this.s3Config.remotePrefix ?? "" + ); + const res = await this._writeFileFromRoot( + uploadFile, + content, + mtime, + ctime + ); + return res; + } + + /** + * the input key contains basedir (prefix), + * but the result doesn't contain it. + */ + async _writeFileFromRoot( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if ( + this.s3Config.remotePrefix !== undefined && + this.s3Config.remotePrefix !== "" && + !key.startsWith(this.s3Config.remotePrefix) + ) { + throw Error(`_writeFileFromRoot should only accept prefix-ed path`); + } + + const bytesIn5MB = 5242880; + const body = new Uint8Array(content); + + let contentType = DEFAULT_CONTENT_TYPE; + contentType = + mime.contentType(mime.lookup(key) || DEFAULT_CONTENT_TYPE) || + DEFAULT_CONTENT_TYPE; + + const upload = new Upload({ + client: this.s3Client, + queueSize: this.s3Config.partsConcurrency, // concurrency + partSize: bytesIn5MB, // minimal 5MB by default + leavePartsOnError: false, + params: { + Bucket: this.s3Config.s3BucketName, + Key: key, + Body: body, + ContentType: contentType, + Metadata: { + MTime: `${mtime / 1000.0}`, + CTime: `${ctime / 1000.0}`, + }, + }, + }); + upload.on("httpUploadProgress", (progress) => { + // console.info(progress); + }); + await upload.done(); + + return await this._statFromRoot(key); + } + + async readFile(key: string): Promise { + if (key.endsWith("/")) { + throw new Error(`you should not call readFile on folder ${key}`); + } + const downloadFile = getRemoteWithPrefixPath( + key, + this.s3Config.remotePrefix ?? "" + ); + + return await this._readFileFromRoot(downloadFile); + } + + async _readFileFromRoot(key: string): Promise { + if ( + this.s3Config.remotePrefix !== undefined && + this.s3Config.remotePrefix !== "" && + !key.startsWith(this.s3Config.remotePrefix) + ) { + throw Error(`_readFileFromRoot should only accept prefix-ed path`); + } + const data = await this.s3Client.send( + new GetObjectCommand({ + Bucket: this.s3Config.s3BucketName, + Key: key, + }) + ); + const bodyContents = await getObjectBodyToArrayBuffer(data.Body); + return bodyContents; + } + + async rename(key1: string, key2: string): Promise { + throw Error(`rename not implemented for s3`); + } + + async rm(key: string): Promise { + if (key === "/") { + return; + } + + if (key.endsWith("/")) { + if (this.synthFoldersCache.hasOwnProperty(key)) { + delete this.synthFoldersCache[key]; + return; + } + + // in s3 the folder may not exist, so we make our best effort. + // do NOT read this.s3Config.generateFolderObject + // because the folder might be generated by previous setting + try { + const remoteFileName = getRemoteWithPrefixPath( + key, + this.s3Config.remotePrefix ?? "" + ); + + await this.s3Client.send( + new DeleteObjectCommand({ + Bucket: this.s3Config.s3BucketName, + Key: remoteFileName, + }) + ); + } catch (e) { + // pass + } + } else { + const remoteFileName = getRemoteWithPrefixPath( + key, + this.s3Config.remotePrefix ?? "" + ); + + await this.s3Client.send( + new DeleteObjectCommand({ + Bucket: this.s3Config.s3BucketName, + Key: remoteFileName, + }) + ); + } + + // TODO: do we need to delete folder recursively? + // maybe we should not + // because the outer sync algorithm should do that + // (await this._walkFromRoot(remoteFileName)).map(...) + } + + async checkConnect(callbackFunc?: any): Promise { + try { + // const results = await this.s3Client.send( + // new HeadBucketCommand({ Bucket: this.s3Config.s3BucketName }) + // ); + // very simplified version of listing objects + const confCmd = { + Bucket: this.s3Config.s3BucketName, + } as ListObjectsV2CommandInput; + const results = await this.s3Client.send( + new ListObjectsV2Command(confCmd) + ); + + if ( + results === undefined || + results.$metadata === undefined || + results.$metadata.httpStatusCode === undefined + ) { + throw Error("results or $metadata or httStatusCode is undefined"); + } + if (results.$metadata.httpStatusCode !== 200) { + throw Error(`not 200 httpStatusCode`); + } + } catch (err: any) { + console.debug(err); + if (callbackFunc !== undefined) { + if (this.s3Config.s3Endpoint.contains(this.s3Config.s3BucketName)) { + const err2 = new AggregateError([ + err, + new Error( + "Maybe you've included the bucket name inside the endpoint setting. Please remove the bucket name and try again." + ), + ]); + callbackFunc(err2); + } else { + callbackFunc(err); + } + } + return false; + } + + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + throw new Error("Method not implemented."); + } + + async revokeAuth() { + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsWebdav.ts b/src/fsWebdav.ts new file mode 100644 index 0000000..ef5db6a --- /dev/null +++ b/src/fsWebdav.ts @@ -0,0 +1,969 @@ +import { Buffer } from "buffer"; +import { Queue } from "@fyears/tsqueue"; +import { getReasonPhrase } from "http-status-codes/build/cjs/utils-functions"; +import chunk from "lodash/chunk"; +import cloneDeep from "lodash/cloneDeep"; +import flatten from "lodash/flatten"; +import { Platform, type RequestUrlParam, requestUrl } from "obsidian"; +import type { + FileStat, + RequestOptionsWithState, + WebDAVClient, + // Response, + // ResponseDataDetailed, +} from "webdav"; +import type { Entity, WebdavConfig } from "./baseTypes"; +import { VALID_REQURL } from "./baseTypesObs"; +import { FakeFs } from "./fsAll"; +import { bufferToArrayBuffer, delay, splitFileSizeToChunkRanges } from "./misc"; + +/** + * https://stackoverflow.com/questions/32850898/how-to-check-if-a-string-has-any-non-iso-8859-1-characters-with-javascript + * @param str + * @returns true if all are iso 8859 1 chars + */ +function onlyAscii(str: string) { + return !/[^\u0000-\u00ff]/g.test(str); +} + +/** + * https://stackoverflow.com/questions/12539574/ + * @param obj + * @returns + */ +function objKeyToLower(obj: Record) { + return Object.fromEntries( + Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v]) + ); +} + +// @ts-ignore +import { getPatcher } from "webdav/dist/web/index.js"; +if (VALID_REQURL) { + getPatcher().patch( + "request", + async (options: RequestOptionsWithState): Promise => { + const transformedHeaders = objKeyToLower({ ...options.headers }); + delete transformedHeaders["host"]; + delete transformedHeaders["content-length"]; + + const reqContentType = + transformedHeaders["accept"] ?? transformedHeaders["content-type"]; + + const retractedHeaders = { ...transformedHeaders }; + if (retractedHeaders.hasOwnProperty("authorization")) { + retractedHeaders["authorization"] = ""; + } + + // console.debug(`before request:`); + // console.debug(`url: ${options.url}`); + // console.debug(`method: ${options.method}`); + // console.debug(`headers: ${JSON.stringify(retractedHeaders, null, 2)}`); + // console.debug(`reqContentType: ${reqContentType}`); + + const p: RequestUrlParam = { + url: options.url, + method: options.method, + body: options.data as string | ArrayBuffer, + headers: transformedHeaders, + contentType: reqContentType, + throw: false, + }; + + let r = await requestUrl(p); + + if ( + r.status === 401 && + Platform.isIosApp && + !options.url.endsWith("/") && + !options.url.endsWith(".md") && + options.method.toUpperCase() === "PROPFIND" + ) { + // don't ask me why, + // some webdav servers have some mysterious behaviours, + // if a folder doesn't exist without slash, the servers return 401 instead of 404 + // here is a dirty hack that works + console.debug(`so we have 401, try appending request url with slash`); + p.url = `${options.url}/`; + r = await requestUrl(p); + } + + // console.debug(`after request:`); + const rspHeaders = objKeyToLower({ ...r.headers }); + // console.debug(`rspHeaders: ${JSON.stringify(rspHeaders, null, 2)}`); + for (const key in rspHeaders) { + if (rspHeaders.hasOwnProperty(key)) { + // avoid the error: + // Failed to read the 'headers' property from 'ResponseInit': String contains non ISO-8859-1 code point. + // const possibleNonAscii = [ + // "Content-Disposition", + // "X-Accel-Redirect", + // "X-Outfilename", + // "X-Sendfile" + // ]; + // for (const p of possibleNonAscii) { + // if (key === p || key === p.toLowerCase()) { + // rspHeaders[key] = encodeURIComponent(rspHeaders[key]); + // } + // } + if (!onlyAscii(rspHeaders[key])) { + // console.debug(`rspHeaders[key] needs encode: ${key}`); + rspHeaders[key] = encodeURIComponent(rspHeaders[key]); + } + } + } + + let r2: Response | undefined = undefined; + const statusText = getReasonPhrase(r.status); + // console.debug(`statusText: ${statusText}`); + if ([101, 103, 204, 205, 304].includes(r.status)) { + // A null body status is a status that is 101, 103, 204, 205, or 304. + // https://fetch.spec.whatwg.org/#statuses + // fix this: Failed to construct 'Response': Response with null body status cannot have body + r2 = new Response(null, { + status: r.status, + statusText: statusText, + headers: rspHeaders, + }); + } else { + r2 = new Response(r.arrayBuffer, { + status: r.status, + statusText: statusText, + headers: rspHeaders, + }); + } + + return r2; + } + ); +} + +// @ts-ignore +// biome-ignore lint: we want to ts-ignore the next line +import { AuthType, BufferLike, createClient } from "webdav/dist/web/index.js"; + +export const DEFAULT_WEBDAV_CONFIG = { + address: "", + username: "", + password: "", + authType: "basic", + manualRecursive: true, + depth: "manual_1", + remoteBaseDir: "", +} as WebdavConfig; + +const getWebdavPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + // special + key = `/${remoteBaseDir}/`; + } else if (fileOrFolderPath.startsWith("/")) { + console.warn( + `why the path ${fileOrFolderPath} starts with '/'? but we just go on.` + ); + key = `/${remoteBaseDir}${fileOrFolderPath}`; + } else { + key = `/${remoteBaseDir}/${fileOrFolderPath}`; + } + return key; +}; + +/** + * sometimes the path startswith /../../...... + * we want to make sure the path is compatible + */ +const stripLeadingPath = (x: string) => { + let y = x; + while (y.startsWith("/..")) { + y = y.slice("/..".length); + } + return y; +}; + +const getNormPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + const strippedFileOrFolderPath = stripLeadingPath(fileOrFolderPath); + if ( + !( + strippedFileOrFolderPath === `/${remoteBaseDir}` || + strippedFileOrFolderPath.startsWith(`/${remoteBaseDir}/`) + ) + ) { + throw Error( + `"${fileOrFolderPath}" after stripping doesn't starts with "/${remoteBaseDir}/"` + ); + } + const result = strippedFileOrFolderPath.slice(`/${remoteBaseDir}/`.length); + return result; +}; + +const fromWebdavItemToEntity = (x: FileStat, remoteBaseDir: string): Entity => { + let key = getNormPath(x.filename, remoteBaseDir); + + if (x.type === "directory" && !key.endsWith("/")) { + key = `${key}/`; + } + const mtimeSvr = Date.parse(x.lastmod).valueOf(); + return { + key: key, + keyRaw: key, + mtimeSvr: mtimeSvr, + mtimeCli: mtimeSvr, // TODO: no universal way to set mtime in webdav + size: x.size, + sizeRaw: x.size, + }; +}; + +const tryEncodeURI = (x: string) => { + if (x.includes("%")) { + // likely encoded before! + return x; + } + return encodeURI(x); +}; + +const tryEncodeUsernamePassword = (x: string) => { + if (onlyAscii(x)) { + return x; + } + return unescape(encodeURIComponent(x)); +}; + +const parseCustomHeaders = (x: string): Record => { + const y = x.trim(); + if (y === "") { + return {}; + } + const z = y.split("\n"); + + const res: Record = {}; + + for (const kv of z) { + if (!kv.includes(":")) { + continue; + } + + const [keyRaw, ...valueArr] = kv.split(":"); + const key = keyRaw.trim(); + const value = valueArr.join(":").trim(); + res[key] = value; + } + + return res; +}; + +export class FakeFsWebdav extends FakeFs { + kind: "webdav"; + + webdavConfig: WebdavConfig; + remoteBaseDir: string; + client!: WebDAVClient; + vaultFolderExists: boolean; + saveUpdatedConfigFunc: () => Promise; + + supportApachePartial: boolean; + supportSabrePartial: boolean; + isNextcloud: boolean; + nextcloudUploadServerAddress: string; + + constructor( + webdavConfig: WebdavConfig, + vaultName: string, + saveUpdatedConfigFunc: () => Promise + ) { + super(); + this.kind = "webdav"; + this.webdavConfig = cloneDeep(webdavConfig); + this.webdavConfig.address = tryEncodeURI(this.webdavConfig.address); + this.remoteBaseDir = this.webdavConfig.remoteBaseDir || vaultName || ""; + this.vaultFolderExists = false; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + + this.supportApachePartial = false; + this.supportSabrePartial = false; + this.isNextcloud = false; + this.nextcloudUploadServerAddress = ""; + } + + async _init() { + // init client if not inited + if (this.client !== undefined) { + return; + } + + const cacheHeader = { + "Cache-Control": "no-cache", + }; + const customHeaders = parseCustomHeaders( + this.webdavConfig.customHeaders ?? "" + ); + const headers = { ...cacheHeader, ...customHeaders }; + + if ( + this.webdavConfig.username !== "" && + this.webdavConfig.password !== "" + ) { + this.client = createClient(this.webdavConfig.address, { + username: tryEncodeUsernamePassword(this.webdavConfig.username), + password: tryEncodeUsernamePassword(this.webdavConfig.password), + headers: headers, + authType: + this.webdavConfig.authType === "digest" + ? AuthType.Digest + : AuthType.Password, + }); + } else { + console.info("no password"); + this.client = createClient(this.webdavConfig.address, { + headers: headers, + }); + } + + // check vault folder + if (this.vaultFolderExists) { + // pass + } else { + const res = await this.client.exists(`/${this.remoteBaseDir}/`); + if (res) { + // console.info("remote vault folder exits!"); + this.vaultFolderExists = true; + } else { + console.info("remote vault folder not exists, creating"); + await this.client.createDirectory(`/${this.remoteBaseDir}/`); + console.info("remote vault folder created!"); + this.vaultFolderExists = true; + } + } + + // adjust depth parameter + if ( + this.webdavConfig.depth === "auto" || + this.webdavConfig.depth === "auto_1" || + this.webdavConfig.depth === "auto_infinity" || + this.webdavConfig.depth === "auto_unknown" + ) { + this.webdavConfig.depth = "manual_1"; + this.webdavConfig.manualRecursive = true; + if (this.saveUpdatedConfigFunc !== undefined) { + await this.saveUpdatedConfigFunc(); + console.info( + `webdav depth="auto_???" is changed to ${this.webdavConfig.depth}` + ); + } + } + + await this._checkPartialSupport(); + } + + /** + * /remote.php/dav/files/ + * => /remote.php/dav/uploads/ + */ + _getnextcloudUploadServerAddress = () => { + let k = this.webdavConfig.address; + if (k.endsWith("/")) { + // no tailing slash + k = k.substring(0, k.length - 1); + } + const s = k.split("/"); + if ( + s.length > 3 && + s[s.length - 3] === "dav" && + s[s.length - 2] === "files" && + s[s.length - 1] !== "" + ) { + s[s.length - 2] = "uploads"; + return s.join("/"); + } + throw Error(`cannot construct upload address for ${s}`); + }; + + async _checkPartialSupport() { + const compliance = await this.client.getDAVCompliance( + `/${this.remoteBaseDir}/` + ); + + for (const c of compliance.compliance) { + // nextcloud AND with an account + if ( + c.toLocaleLowerCase().includes("nextcloud") && + this.webdavConfig.username !== "" && + this.webdavConfig.password !== "" + ) { + // the address is parsable + const s = this.webdavConfig.address.split("/"); + if ( + s.length > 3 && + s[s.length - 3] === "dav" && + s[s.length - 2] === "files" && + s[s.length - 1] !== "" + ) { + this.isNextcloud = true; + this.nextcloudUploadServerAddress = + this._getnextcloudUploadServerAddress(); + console.debug( + `isNextcloud=${this.isNextcloud}, uploadFolder=${this.nextcloudUploadServerAddress}` + ); + return true; + } else { + return false; + } + } + } + + // taken from https://github.com/perry-mitchell/webdav-client/blob/master/source/operations/partialUpdateFileContents.ts + // which is under MIT license + if ( + compliance.server.includes("Apache") && + compliance.compliance.includes("") + ) { + this.supportApachePartial = true; + console.debug( + `supportApachePartial=true, compliance=${JSON.stringify(compliance)}` + ); + return true; + } + + if (compliance.compliance.includes("sabredav-partialupdate")) { + this.supportSabrePartial = true; + console.debug( + `supportSabrePartial=true, compliance=${JSON.stringify(compliance)}` + ); + return true; + } + + return false; + } + + async walk(): Promise { + await this._init(); + + let contents = [] as FileStat[]; + if ( + this.webdavConfig.depth === "auto" || + this.webdavConfig.depth === "auto_unknown" || + this.webdavConfig.depth === "auto_1" || + this.webdavConfig.depth === "auto_infinity" /* don't trust auto now */ || + this.webdavConfig.depth === "manual_1" || + this.webdavConfig.address.includes("jianguoyun.com") || + this.webdavConfig.address.includes("teracloud.jp") + ) { + // the remote doesn't support infinity propfind, + // we need to do a bfs here + const q = new Queue([`/${this.remoteBaseDir}`]); + const CHUNK_SIZE = 10; + while (q.length > 0) { + const itemsToFetch: string[] = []; + while (q.length > 0) { + itemsToFetch.push(q.pop()!); + } + const itemsToFetchChunks = chunk(itemsToFetch, CHUNK_SIZE); + // console.debug(itemsToFetchChunks); + const subContents = [] as FileStat[]; + for (const singleChunk of itemsToFetchChunks) { + const r = singleChunk.map(async (x) => { + let k = (await this.client.getDirectoryContents(x, { + deep: false, + details: false /* no need for verbose details here */, + // TODO: to support .obsidian, + // we need to load all files including dot, + // anyway to reduce the resources? + // glob: "/**" /* avoid dot files by using glob */, + })) as FileStat[]; + k = k.filter((sub) => stripLeadingPath(sub.filename) !== x); + return k; + }); + const r3 = await Promise.all(r); + for (const r4 of r3) { + if ( + this.webdavConfig.address.includes("jianguoyun.com") && + r4.length >= 749 + ) { + // https://help.jianguoyun.com/?p=2064 + // no more than 750 per request + throw Error( + `出错:坚果云 api 有限制,文件列表加载不全。终止同步!` + ); + } + } + const r2 = flatten(r3); + subContents.push(...r2); + } + for (let i = 0; i < subContents.length; ++i) { + const f = subContents[i]; + contents.push(f); + if (f.type === "directory") { + q.push(stripLeadingPath(f.filename)); + } + } + } + } else { + // the remote supports infinity propfind + contents = (await this.client.getDirectoryContents( + `/${this.remoteBaseDir}`, + { + deep: true, + details: false /* no need for verbose details here */, + // TODO: to support .obsidian, + // we need to load all files including dot, + // anyway to reduce the resources? + // glob: "/**" /* avoid dot files by using glob */, + } + )) as FileStat[]; + } + + const result = contents + .map((x) => fromWebdavItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.keyRaw !== "/"); + return result; + } + + async walkPartial(): Promise { + await this._init(); + + const contents = (await this.client.getDirectoryContents( + `/${this.remoteBaseDir}`, + { + deep: false, // partial, no need to recursive here + details: false /* no need for verbose details here */, + } + )) as FileStat[]; + return contents + .map((x) => fromWebdavItemToEntity(x, this.remoteBaseDir)) + .filter((x) => x.keyRaw !== "/"); + } + + async stat(key: string): Promise { + await this._init(); + const fullPath = getWebdavPath(key, this.remoteBaseDir); + return await this._statFromRoot(fullPath); + } + + async _statFromRoot(key: string): Promise { + const res = (await this.client.stat(key, { + details: false, + })) as FileStat; + return fromWebdavItemToEntity(res, this.remoteBaseDir); + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + if (!key.endsWith("/")) { + throw Error(`you should not call mkdir on ${key}`); + } + await this._init(); + const uploadFile = getWebdavPath(key, this.remoteBaseDir); + return await this._mkdirFromRoot(uploadFile, mtime, ctime); + } + + async _mkdirFromRoot( + key: string, + mtime?: number, + ctime?: number + ): Promise { + // the sync algorithm should do recursive manually already. + // if we set recursive: true here, Digest auth will return some error inside the PROPFIND + await this.client.createDirectory(key, { + recursive: false, + }); + return await this._statFromRoot(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + if (key.endsWith("/")) { + throw Error(`you should not call writeFile on ${key}`); + } + await this._init(); + const uploadFile = getWebdavPath(key, this.remoteBaseDir); + return await this._writeFileFromRoot( + uploadFile, + content, + mtime, + ctime, + key + ); + } + + async _writeFileFromRoot( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + // less than 10 MB + if (content.byteLength <= 10 * 1024 * 1024) { + return await this._writeFileFromRootFull( + key, + content, + mtime, + ctime, + origKey + ); + } + + // larger than 10 MB + if ( + !this.isNextcloud && + !this.supportApachePartial && + !this.supportSabrePartial + ) { + // give up and upload by whole, and directly return + return await this._writeFileFromRootFull( + key, + content, + mtime, + ctime, + origKey + ); + } + + // try to upload by chunks + try { + if (this.isNextcloud) { + return await this._writeFileFromRootNextcloud( + key, + content, + mtime, + ctime, + origKey + ); + } else if (this.supportApachePartial) { + return await this._writeFileFromRootApachePartial( + key, + content, + mtime, + ctime, + origKey + ); + } else if (this.supportSabrePartial) { + return await this._writeFileFromRootSabrePartial( + key, + content, + mtime, + ctime, + origKey + ); + } + throw Error(`Error: partial upload / update method is not implemented??`); + } catch (e) { + console.error( + `we fail to write file partially for nextcloud or apache or sabre/dav, stop!` + ); + console.error(e); + throw e; + // this.isNextcloud = false; + // this.supportApachePartial = false; + // return await this._writeFileFromRootFull( + // key, + // content, + // mtime, + // ctime, + // origKey + // ); + } + } + + async _writeFileFromRootFull( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + // console.debug(`start _writeFileFromRootFull`); + await this.client.putFileContents(key, content, { + overwrite: true, + onUploadProgress: (progress: any) => { + console.info(`Uploaded ${progress.loaded} bytes of ${progress.total}`); + }, + }); + const k = await this._statFromRoot(key); + // console.debug(`end _writeFileFromRootFull`); + return k; + } + + /** + * https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/chunking.html + * @param key + * @param content + * @param mtime + * @param ctime + * @returns + */ + async _writeFileFromRootNextcloud( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + if (key.endsWith("/")) { + throw Error( + `key=${key} should not have tailing slash in _writeFileFromRootNextcloud` + ); + } + const destUrl = `${this.webdavConfig.address}/${encodeURI(key)}`; + console.debug(`destUrl=${destUrl}`); + + const getTmpFolder = (x: string) => { + if (x.endsWith("/")) { + throw Error(`file to upload by chunk should not ends with /`); + } + const y = x.split("/"); + const z = encodeURI(`${y[y.length - 1]}`); + return z; + }; + + const uploadServerAddress = this.nextcloudUploadServerAddress; + console.debug(`uploadServerAddress=${uploadServerAddress}`); + const tmpFolderName = getTmpFolder(key); + console.debug(`tmpFolderName=${tmpFolderName}`); + + const clientForUpload = createClient(uploadServerAddress, { + username: tryEncodeUsernamePassword(this.webdavConfig.username), + password: tryEncodeUsernamePassword(this.webdavConfig.password), + headers: { + "Cache-Control": "no-cache", + }, + authType: + this.webdavConfig.authType === "digest" + ? AuthType.Digest + : AuthType.Password, + }); + + // create folder + await clientForUpload.createDirectory(tmpFolderName, { + method: "MKCOL", + headers: { + Destination: destUrl, + }, + }); + console.debug(`finish creating folder`); + + // upload by chunks + const sizePerChunk = 5 * 1024 * 1024; // 5 mb + const chunkRanges = splitFileSizeToChunkRanges( + content.byteLength, + sizePerChunk + ); + for (let i = 0; i < chunkRanges.length; ++i) { + const { start, end } = chunkRanges[i]; + const tmpFileName = `${i + 1}`.padStart(5, "0"); + const tmpFileNameWithFolder = `${tmpFolderName}/${tmpFileName}`; + console.debug( + `start to upload chunk ${ + i + 1 + } to ${tmpFileNameWithFolder} with startInclusive=${start}, endInclusive=${end}` + ); + await clientForUpload.putFileContents( + tmpFileNameWithFolder, + content.slice(start, end + 1), + { + headers: { + Destination: destUrl, + "OC-Total-Length": `${content.byteLength}`, + }, + } + ); + } + console.debug(`finish upload all chunks`); + + // move to assemble + const fakeFileToMoveUrl = `${tmpFolderName}/.file`; + console.debug(`fakeFileToMoveUrl=${fakeFileToMoveUrl}`); + await clientForUpload.customRequest(fakeFileToMoveUrl, { + method: "MOVE", + headers: { + Destination: destUrl, + "OC-Total-Length": `${content.byteLength}`, + }, + }); + console.debug(`finish moving file`); + // TODO: setting X-OC-Mtime + + // stat + console.debug(`before stat origKey=${origKey}`); + const k = await this.stat(origKey); + console.debug(`after stat`); + if (k.sizeRaw !== content.byteLength) { + // we failed! + this.isNextcloud = false; // give up next time! + const err = `unable to upload file ${key} by chunks to nextcloud`; + console.error(err); + throw Error(err); + } + console.debug(`after stat, k=${JSON.stringify(k, null, 2)}`); + + return k; + } + + async _writeFileFromRootApachePartial( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + // firstly upload a 0-byte data + await this._writeFileFromRootFull( + key, + new ArrayBuffer(0), + mtime, + ctime, + origKey + ); + + // then "update" by chunks + const sizePerChunk = 5 * 1024 * 1024; // 5 mb + const chunkRanges = splitFileSizeToChunkRanges( + content.byteLength, + sizePerChunk + ); + + // TODO: parallel + for (let i = 0; i < chunkRanges.length; ++i) { + const { start, end } = chunkRanges[i]; + await this.client.partialUpdateFileContents( + key, + start, + end, + content.slice(start, end + 1) + ); + } + + // lastly return + return await this.stat(origKey); + } + + async _writeFileFromRootSabrePartial( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number, + origKey: string + ): Promise { + // firstly upload a 0-byte data + await this._writeFileFromRootFull( + key, + new ArrayBuffer(0), + mtime, + ctime, + origKey + ); + + // then "update" by chunks + const sizePerChunk = 5 * 1024 * 1024; // 5 mb + const chunkRanges = splitFileSizeToChunkRanges( + content.byteLength, + sizePerChunk + ); + + // diff from apachePartial: we use "append" header here for dufs... + // we cannot parallel here + for (let i = 0; i < chunkRanges.length; ++i) { + const { start, end } = chunkRanges[i]; + await this.client.customRequest(key, { + method: "PATCH", + headers: { + "X-Update-Range": "append", + "Content-Type": "application/x-sabredav-partialupdate", + }, + data: content.slice(start, end + 1), + }); + } + + // lastly return + return await this.stat(origKey); + } + + async readFile(key: string): Promise { + if (key.endsWith("/")) { + throw Error(`you should not call readFile on ${key}`); + } + await this._init(); + const downloadFile = getWebdavPath(key, this.remoteBaseDir); + return await this._readFileFromRoot(downloadFile); + } + + async _readFileFromRoot(key: string): Promise { + const buff = (await this.client.getFileContents(key)) as BufferLike; + if (buff instanceof ArrayBuffer) { + return buff; + } else if (buff instanceof Buffer) { + return bufferToArrayBuffer(buff); + } + throw Error(`unexpected file content result with type ${typeof buff}`); + } + + async rename(key1: string, key2: string): Promise { + if (key1 === "/" || key2 === "/") { + return; + } + const remoteFileName1 = getWebdavPath(key1, this.remoteBaseDir); + const remoteFileName2 = getWebdavPath(key2, this.remoteBaseDir); + await this._init(); + await this.client.moveFile(remoteFileName1, remoteFileName2); + } + + async rm(key: string): Promise { + if (key === "/") { + return; + } + await this._init(); + try { + const remoteFileName = getWebdavPath(key, this.remoteBaseDir); + await this.client.deleteFile(remoteFileName); + // console.info(`delete ${remoteFileName} succeeded`); + } catch (err) { + console.error("some error while deleting"); + console.error(err); + } + } + + async checkConnect(callbackFunc?: any): Promise { + if ( + !( + this.webdavConfig.address.startsWith("http://") || + this.webdavConfig.address.startsWith("https://") + ) + ) { + const err = + "Error: the url should start with http(s):// but it does not!"; + console.error(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } + return false; + } + + try { + await this._init(); + const results = await this._statFromRoot(`/${this.remoteBaseDir}/`); + if (results === undefined) { + throw Error("cannot stat root vault folder!"); + } + } catch (err) { + console.error(err); + callbackFunc?.(err); + return false; + } + + return await this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + throw new Error("Method not implemented."); + } + + async revokeAuth() { + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsWebdis.ts b/src/fsWebdis.ts new file mode 100644 index 0000000..9ab69e5 --- /dev/null +++ b/src/fsWebdis.ts @@ -0,0 +1,278 @@ +import { isEqual } from "lodash"; +import { + DEFAULT_CONTENT_TYPE, + type Entity, + type WebdisConfig, +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; + +export const DEFAULT_WEBDIS_CONFIG: WebdisConfig = { + address: "", + username: "", + password: "", + remoteBaseDir: "", +}; + +const getWebdisPath = (fileOrFolderPath: string, remoteBaseDir: string) => { + let key = fileOrFolderPath; + if (fileOrFolderPath === "/" || fileOrFolderPath === "") { + // special + key = `${remoteBaseDir}`; + } else if (fileOrFolderPath.startsWith("/")) { + console.warn( + `why the path ${fileOrFolderPath} starts with '/'? but we just go on.` + ); + key = `${remoteBaseDir}${fileOrFolderPath}`; + } else { + key = `${remoteBaseDir}/${fileOrFolderPath}`; + } + return `rs:fs:v1:${encodeURIComponent(key)}`; // we should encode them!!!! +}; + +export const getOrigPath = (fullKey: string, remoteBaseDir: string) => { + const fullKeyDecoded = decodeURIComponent(fullKey); + const prefix = `rs:fs:v1:${remoteBaseDir}/`; + // console.debug(`prefix=${prefix}`); + const suffix1 = ":meta"; + const suffix2 = ":content"; + if (!fullKeyDecoded.startsWith(prefix)) { + throw Error(`you should not call getOrigEntity on ${fullKey}`); + } + let realKey = fullKeyDecoded.slice(prefix.length); + // console.debug(`realKey=${realKey}`); + if (realKey.endsWith(suffix1)) { + realKey = realKey.slice(0, -suffix1.length); + // console.debug(`realKey=${realKey}`); + } else if (realKey.endsWith(suffix2)) { + realKey = realKey.slice(0, -suffix2.length); + // console.debug(`realKey=${realKey}`); + } + // console.debug(`fullKey=${fullKey}, realKey=${realKey}`); + return realKey; +}; + +export class FakeFsWebdis extends FakeFs { + kind: "webdis"; + webdisConfig: WebdisConfig; + remoteBaseDir: string; + saveUpdatedConfigFunc: () => Promise; + + constructor( + webdisConfig: WebdisConfig, + vaultName: string, + saveUpdatedConfigFunc: () => Promise + ) { + super(); + this.kind = "webdis"; + this.webdisConfig = webdisConfig; + this.remoteBaseDir = this.webdisConfig.remoteBaseDir || vaultName || ""; + this.saveUpdatedConfigFunc = saveUpdatedConfigFunc; + } + + async _fetchCommand( + method: "GET" | "POST" | "PUT", + urlPath: string, + content?: ArrayBuffer + ) { + const address = this.webdisConfig.address; + if (!address.startsWith(`https://`) && !address.startsWith(`http://`)) { + throw Error( + `your webdis server address should start with https:// or http://` + ); + } + if (address.endsWith("/")) { + throw Error(`your webdis server should not ends with /`); + } + + if (content !== undefined && method !== "PUT") { + throw Error(`you can only "POST" ArrayBuffer, not using other methods`); + } + + const fullUrl = `${address}/${urlPath}`; + // console.debug(`fullUrl=${fullUrl}`) + + const username = this.webdisConfig.username ?? ""; + const password = this.webdisConfig.password ?? ""; + if (username !== "" && password !== "") { + return await fetch(fullUrl, { + method: method, + headers: { + Authorization: "Basic " + btoa(username + ":" + password), + }, + body: content, + }); + } else if (username === "" && password === "") { + return await fetch(fullUrl, { + method: method, + body: content, + }); + } else { + throw Error( + `your username and password should both be empty or not empty!` + ); + } + } + + async walk(): Promise { + let cursor = "0"; + const res: Entity[] = []; + do { + const command = `SCAN/${cursor}/MATCH/rs:fs:v1:${encodeURIComponent(this.remoteBaseDir + "/")}*:meta/COUNT/1000`; + const rsp = (await (await this._fetchCommand("GET", command)).json())[ + "SCAN" + ]; + // console.debug(rsp); + cursor = rsp[0]; + for (const fullKeyWithMeta of rsp[1]) { + const realKey = getOrigPath(fullKeyWithMeta, this.remoteBaseDir); + res.push(await this.stat(realKey)); + } + } while (cursor !== "0"); + // console.debug(`walk res:`); + // console.debug(res); + return res; + } + + async walkPartial(): Promise { + let cursor = "0"; + const res: Entity[] = []; + const command = `SCAN/${cursor}/MATCH/rs:fs:v1:${encodeURIComponent(this.remoteBaseDir + "/")}*:meta/COUNT/10`; // fewer keys + const rsp = (await (await this._fetchCommand("GET", command)).json())[ + "SCAN" + ]; + // console.debug(rsp); + cursor = rsp[0]; + for (const fullKeyWithMeta of rsp[1]) { + const realKey = getOrigPath(fullKeyWithMeta, this.remoteBaseDir); + res.push(await this.stat(realKey)); + } + // no need to loop over cursor + // console.debug(`walk res:`); + // console.debug(res); + return res; + } + + async stat(key: string): Promise { + const fullKey = getWebdisPath(key, this.remoteBaseDir); + return await this._statFromRaw(fullKey); + } + + async _statFromRaw(key: string): Promise { + // console.debug(`_statFromRaw on ${key}`); + const command = `HGETALL/${key}:meta`; + const rsp = (await (await this._fetchCommand("GET", command)).json())[ + "HGETALL" + ]; + // console.debug(`rsp: ${JSON.stringify(rsp, null, 2)}`); + if (isEqual(rsp, {})) { + // empty! + throw Error(`key ${key} doesn't exist!`); + } + const realKey = getOrigPath(key, this.remoteBaseDir); + return { + key: realKey, + keyRaw: realKey, + mtimeCli: Number.parseInt(rsp["mtime"]), + mtimeSvr: Number.parseInt(rsp["mtime"]), + size: Number.parseInt(rsp["size"]), + sizeRaw: Number.parseInt(rsp["size"]), + }; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + let command = `HSET/${getWebdisPath(key, this.remoteBaseDir)}:meta/size/0`; + if (mtime !== undefined && mtime !== 0) { + command = `${command}/mtime/${mtime}`; + } + if (ctime !== undefined && ctime !== 0) { + command = `${command}/ctime/${ctime}`; + } + const rsp = (await (await this._fetchCommand("GET", command)).json())[ + "HSET" + ]; + return await this.stat(key); + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const fullKey = getWebdisPath(key, this.remoteBaseDir); + + // meta + let command1 = `HSET/${fullKey}:meta/size/${content.byteLength}`; + if (mtime !== undefined && mtime !== 0) { + command1 = `${command1}/mtime/${mtime}`; + } + if (ctime !== undefined && ctime !== 0) { + command1 = `${command1}/ctime/${ctime}`; + } + const rsp1 = (await (await this._fetchCommand("GET", command1)).json())[ + "HSET" + ]; + + // content + const command2 = `SET/${fullKey}:content`; + const rsp2 = ( + await (await this._fetchCommand("PUT", command2, content)).json() + )["SET"]; + + // fetch meta + return await this.stat(key); + } + + async readFile(key: string): Promise { + const fullKey = getWebdisPath(key, this.remoteBaseDir); + const command = `GET/${fullKey}:content?type=${DEFAULT_CONTENT_TYPE}`; + const rsp = await (await this._fetchCommand("GET", command)).arrayBuffer(); + return rsp; + } + + async rename(key1: string, key2: string): Promise { + const fullKey1 = getWebdisPath(key1, this.remoteBaseDir); + const fullKey2 = getWebdisPath(key2, this.remoteBaseDir); + const commandContent = `RENAME/${fullKey1}:content/${fullKey2}:content`; + await this._fetchCommand("POST", commandContent); + const commandMeta = `RENAME/${fullKey1}:meta/${fullKey2}:meta`; + await this._fetchCommand("POST", commandMeta); + } + + async rm(key: string): Promise { + const fullKey = getWebdisPath(key, this.remoteBaseDir); + const command = `DEL/${fullKey}:meta/${fullKey}:content`; + const rsp = (await (await this._fetchCommand("PUT", command)).json())[ + "DEL" + ]; + } + + async checkConnect(callbackFunc?: any): Promise { + try { + const k = await ( + await this._fetchCommand("GET", "PING/helloworld") + ).json(); + if (!isEqual(k, { PING: "helloworld" })) { + throw Error(`no correct ping response`); + } + } catch (err: any) { + console.error(err); + callbackFunc?.(err); + return false; + } + return await this.checkConnectCommonOps(callbackFunc); + // return true; + } + + async getUserDisplayName(): Promise { + return this.webdisConfig.username || ""; + } + + async revokeAuth(): Promise { + throw new Error("Method not implemented."); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/fsYandexDisk.ts b/src/fsYandexDisk.ts new file mode 100644 index 0000000..66de054 --- /dev/null +++ b/src/fsYandexDisk.ts @@ -0,0 +1,450 @@ +/** + * BYOC — Yandex Disk Filesystem Adapter + * Clean-room implementation using Yandex Disk REST API v1. + * OAuth2 authorization code flow with silent token refresh. + */ + +import { request } from "obsidian"; +import { + COMMAND_CALLBACK_YANDEXDISK, + YANDEXDISK_CLIENT_ID, + YANDEXDISK_CLIENT_SECRET, + type Entity, + type YandexDiskConfig, +} from "./baseTypes"; +import { FakeFs } from "./fsAll"; +import { retryFetch } from "./misc"; + +const YANDEX_API = "https://cloud-api.yandex.net/v1/disk"; +const YANDEX_AUTH_URL = "https://oauth.yandex.com/authorize"; +const YANDEX_TOKEN_URL = "https://oauth.yandex.com/token"; +const REDIRECT_URI = `obsidian://${COMMAND_CALLBACK_YANDEXDISK}`; + +export const DEFAULT_YANDEXDISK_CONFIG: YandexDiskConfig = { + accessToken: "", + accessTokenExpiresInMs: 0, + accessTokenExpiresAtTimeMs: 0, + refreshToken: "", + remoteBaseDir: "", + credentialsShouldBeDeletedAtTimeMs: 0, + scope: "cloud_api:disk.app_folder cloud_api:disk.read cloud_api:disk.write", + kind: "yandexdisk", +}; + +//////////////////////////////////////////////////////////////////////////////// +// OAuth2 Helpers +//////////////////////////////////////////////////////////////////////////////// + +export function generateAuthUrl(): string { + const params = new URLSearchParams({ + response_type: "code", + client_id: YANDEXDISK_CLIENT_ID, + redirect_uri: REDIRECT_URI, + force_confirm: "yes", + }); + return `${YANDEX_AUTH_URL}?${params.toString()}`; +} + +export async function sendAuthReq( + code: string, + errorCallBack: (e: any) => Promise +): Promise { + try { + const rsp = await request({ + url: YANDEX_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "authorization_code", + code: code, + client_id: YANDEXDISK_CLIENT_ID, + client_secret: YANDEXDISK_CLIENT_SECRET, + redirect_uri: REDIRECT_URI, + }).toString(), + }); + return JSON.parse(rsp); + } catch (e) { + console.error(e); + await errorCallBack(e); + } +} + +async function refreshAccessToken(refreshToken: string): Promise { + const rsp = await request({ + url: YANDEX_TOKEN_URL, + method: "POST", + contentType: "application/x-www-form-urlencoded", + body: new URLSearchParams({ + grant_type: "refresh_token", + refresh_token: refreshToken, + client_id: YANDEXDISK_CLIENT_ID, + client_secret: YANDEXDISK_CLIENT_SECRET, + }).toString(), + }); + return JSON.parse(rsp); +} + +export async function setConfigBySuccessfullAuthInplace( + config: YandexDiskConfig, + authRes: any, + saveFunc: () => Promise +): Promise { + config.accessToken = authRes.access_token; + config.refreshToken = authRes.refresh_token || config.refreshToken; + config.accessTokenExpiresInMs = (authRes.expires_in ?? 31536000) * 1000; + config.accessTokenExpiresAtTimeMs = + Date.now() + (authRes.expires_in ?? 31536000) * 1000 - 300_000; + config.credentialsShouldBeDeletedAtTimeMs = 0; + await saveFunc(); +} + +//////////////////////////////////////////////////////////////////////////////// +// Path Helpers +//////////////////////////////////////////////////////////////////////////////// + +function getRemotePath(key: string, remoteBaseDir: string): string { + if (!key || key === "/") return `disk:/${remoteBaseDir}`; + const clean = key.endsWith("/") ? key.slice(0, -1) : key; + return `disk:/${remoteBaseDir}/${clean}`; +} + +function normalizeKey(path: string, remoteBaseDir: string): string { + const prefix = `disk:/${remoteBaseDir}/`; + if (path.startsWith(prefix)) { + return path.slice(prefix.length); + } + const prefix2 = `disk:/${remoteBaseDir}`; + if (path === prefix2) return ""; + return path; +} + +//////////////////////////////////////////////////////////////////////////////// +// The Client +//////////////////////////////////////////////////////////////////////////////// + +export class FakeFsYandexDisk extends FakeFs { + kind = "yandexdisk"; + private config: YandexDiskConfig; + private vaultName: string; + private saveFunc: () => Promise; + private remoteBaseDir: string; + + constructor( + config: YandexDiskConfig, + vaultName: string, + saveFunc: () => Promise + ) { + super(); + this.config = config; + this.vaultName = vaultName; + this.saveFunc = saveFunc; + this.remoteBaseDir = config.remoteBaseDir || vaultName || ""; + } + + private async ensureToken(): Promise { + if (!this.config.accessToken) { + throw Error("[BYOC] Yandex Disk: not authorized."); + } + + // Yandex tokens last a very long time (up to 1 year) + // but we still refresh if we have a refresh token and it's expiring + if ( + this.config.refreshToken && + this.config.accessTokenExpiresAtTimeMs > 0 && + this.config.accessTokenExpiresAtTimeMs < Date.now() + ) { + const res = await refreshAccessToken(this.config.refreshToken); + if (res.error) { + throw Error(`[BYOC] Yandex refresh error: ${res.error_description}`); + } + this.config.accessToken = res.access_token; + if (res.refresh_token) this.config.refreshToken = res.refresh_token; + this.config.accessTokenExpiresAtTimeMs = + Date.now() + (res.expires_in ?? 31536000) * 1000 - 300_000; + await this.saveFunc(); + } + + return this.config.accessToken; + } + + private async _getJson(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${YANDEX_API}${url}`; + return JSON.parse( + await request({ + url: fullUrl, + method: "GET", + headers: { Authorization: `OAuth ${token}` }, + }) + ); + } + + private async _put(url: string, body?: any): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${YANDEX_API}${url}`; + const opts: any = { + url: fullUrl, + method: "PUT", + headers: { Authorization: `OAuth ${token}` }, + }; + if (body) { + opts.contentType = "application/json"; + opts.body = JSON.stringify(body); + } + const rsp = await request(opts); + return rsp ? JSON.parse(rsp) : {}; + } + + private async _delete(url: string): Promise { + const token = await this.ensureToken(); + const fullUrl = url.startsWith("http") ? url : `${YANDEX_API}${url}`; + await request({ + url: fullUrl, + method: "DELETE", + headers: { Authorization: `OAuth ${token}` }, + }); + } + + private async ensureBaseDir(): Promise { + try { + await this._getJson( + `/resources?path=${encodeURIComponent(`disk:/${this.remoteBaseDir}`)}` + ); + } catch { + // Create base dir + await this._put( + `/resources?path=${encodeURIComponent(`disk:/${this.remoteBaseDir}`)}` + ); + } + } + + async walk(): Promise { + await this.ensureBaseDir(); + + const entities: Entity[] = []; + let offset = 0; + const limit = 1000; + + while (true) { + const path = encodeURIComponent(`disk:/${this.remoteBaseDir}`); + const res = await this._getJson( + `/resources?path=${path}&limit=${limit}&offset=${offset}&fields=_embedded.items.path,_embedded.items.type,_embedded.items.size,_embedded.items.modified,_embedded.items.created,_embedded.total` + ); + + // Yandex Disk /resources returns embedded items for folders + // We need to use /resources/files for a flat listing + break; // We'll use the flat files endpoint instead + } + + // Use flat file listing — much more efficient + let flatOffset = 0; + while (true) { + const res = await this._getJson( + `/resources/files?limit=${limit}&offset=${flatOffset}&fields=items.path,items.type,items.size,items.modified,items.created` + ); + const items = res.items || []; + + for (const item of items) { + let key = normalizeKey(item.path, this.remoteBaseDir); + if (!key) continue; + // Filter out items not under our base dir + if ( + !item.path.startsWith(`disk:/${this.remoteBaseDir}/`) && + item.path !== `disk:/${this.remoteBaseDir}` + ) { + continue; + } + + const isFolder = item.type === "dir"; + if (isFolder && !key.endsWith("/")) key = `${key}/`; + + const mtime = item.modified ? new Date(item.modified).getTime() : 0; + const ctime = item.created ? new Date(item.created).getTime() : mtime; + + entities.push({ + key, keyRaw: key, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: isFolder ? 0 : (item.size ?? 0), + sizeRaw: isFolder ? 0 : (item.size ?? 0), + } as Entity); + } + + flatOffset += items.length; + if (items.length < limit) break; + } + + // Also list folders recursively + await this.walkFolders( + `disk:/${this.remoteBaseDir}`, + entities + ); + + return entities; + } + + private async walkFolders( + folderPath: string, + entities: Entity[] + ): Promise { + const res = await this._getJson( + `/resources?path=${encodeURIComponent(folderPath)}&limit=1000&fields=_embedded.items.path,_embedded.items.type,_embedded.items.size,_embedded.items.modified,_embedded.items.created` + ); + + const items = res._embedded?.items || []; + for (const item of items) { + if (item.type === "dir") { + let key = normalizeKey(item.path, this.remoteBaseDir); + if (key && !key.endsWith("/")) key = `${key}/`; + if (key) { + const mtime = item.modified ? new Date(item.modified).getTime() : 0; + entities.push({ + key, keyRaw: key, + mtimeSvr: mtime, size: 0, sizeRaw: 0, + } as Entity); + } + // Recurse into subfolder + await this.walkFolders(item.path, entities); + } + } + } + + async walkPartial(): Promise { + return this.walk(); + } + + async stat(key: string): Promise { + const remotePath = getRemotePath(key, this.remoteBaseDir); + const res = await this._getJson( + `/resources?path=${encodeURIComponent(remotePath)}&fields=path,type,size,modified,created` + ); + + const isFolder = res.type === "dir"; + let normalKey = normalizeKey(res.path, this.remoteBaseDir); + if (isFolder && !normalKey.endsWith("/")) normalKey = `${normalKey}/`; + + const mtime = res.modified ? new Date(res.modified).getTime() : 0; + const ctime = res.created ? new Date(res.created).getTime() : mtime; + + return { + key: normalKey, keyRaw: normalKey, + mtimeSvr: mtime, mtimeCli: mtime, ctimeCli: ctime, + size: isFolder ? 0 : (res.size ?? 0), + sizeRaw: isFolder ? 0 : (res.size ?? 0), + } as Entity; + } + + async mkdir(key: string, mtime?: number, ctime?: number): Promise { + const remotePath = getRemotePath(key, this.remoteBaseDir); + try { + await this._put( + `/resources?path=${encodeURIComponent(remotePath)}` + ); + } catch (e: any) { + // 409 = already exists, that's fine + if (!String(e).includes("409")) throw e; + } + return { key, keyRaw: key, size: 0, sizeRaw: 0 } as Entity; + } + + async writeFile( + key: string, + content: ArrayBuffer, + mtime: number, + ctime: number + ): Promise { + const remotePath = getRemotePath(key, this.remoteBaseDir); + + // Step 1: Get upload URL from Yandex + const uploadInfo = await this._getJson( + `/resources/upload?path=${encodeURIComponent(remotePath)}&overwrite=true` + ); + + if (!uploadInfo.href) { + throw Error(`[BYOC] Yandex: no upload URL returned for '${key}'`); + } + + // Step 2: PUT the content to the upload URL (no auth header needed) + const resp = await retryFetch(uploadInfo.href, { + method: "PUT", + body: content, + headers: { "Content-Type": "application/octet-stream" }, + }); + + if (!resp.ok) { + throw Error(`[BYOC] Yandex: upload failed for '${key}': ${resp.status}`); + } + + return { + key, keyRaw: key, + mtimeCli: mtime, ctimeCli: ctime, + size: content.byteLength, + sizeRaw: content.byteLength, + } as Entity; + } + + async readFile(key: string): Promise { + const remotePath = getRemotePath(key, this.remoteBaseDir); + + // Step 1: Get download URL + const downloadInfo = await this._getJson( + `/resources/download?path=${encodeURIComponent(remotePath)}` + ); + + if (!downloadInfo.href) { + throw Error(`[BYOC] Yandex: no download URL returned for '${key}'`); + } + + // Step 2: Download the content + const resp = await retryFetch(downloadInfo.href); + if (!resp.ok) { + throw Error(`[BYOC] Yandex: download failed for '${key}': ${resp.status}`); + } + return resp.arrayBuffer(); + } + + async rename(key1: string, key2: string): Promise { + const from = getRemotePath(key1, this.remoteBaseDir); + const to = getRemotePath(key2, this.remoteBaseDir); + await this._postMove(from, to); + } + + private async _postMove(from: string, to: string): Promise { + const token = await this.ensureToken(); + await request({ + url: `${YANDEX_API}/resources/move?from=${encodeURIComponent(from)}&path=${encodeURIComponent(to)}&overwrite=true`, + method: "POST", + headers: { Authorization: `OAuth ${token}` }, + }); + } + + async rm(key: string): Promise { + const remotePath = getRemotePath(key, this.remoteBaseDir); + try { + await this._delete( + `/resources?path=${encodeURIComponent(remotePath)}&permanently=true` + ); + } catch (e: any) { + // 404 = already gone + if (!String(e).includes("404")) throw e; + } + } + + async checkConnect(callbackFunc?: any): Promise { + return this.checkConnectCommonOps(callbackFunc); + } + + async getUserDisplayName(): Promise { + const res = await this._getJson(""); + return res.user?.display_name || res.user?.login || "Yandex Disk User"; + } + + async revokeAuth(): Promise { + this.config.accessToken = ""; + this.config.refreshToken = ""; + await this.saveFunc(); + } + + allowEmptyFile(): boolean { + return true; + } +} diff --git a/src/i18n.ts b/src/i18n.ts new file mode 100644 index 0000000..86cfd6a --- /dev/null +++ b/src/i18n.ts @@ -0,0 +1,50 @@ +import merge from "lodash/merge"; +import Mustache from "mustache"; +import { moment } from "obsidian"; + +import { LANGS as LANGS_BASIC } from "./langs"; + +// BYOC: No pro-specific i18n strings. Uses only base langs. +const LANGS = LANGS_BASIC; + + +export type LangType = keyof typeof LANGS; +export type LangTypeAndAuto = LangType | "auto"; +export type TransItemType = keyof (typeof LANGS)["en"]; + +export class I18n { + lang: LangTypeAndAuto; + readonly saveSettingFunc: (tolang: LangTypeAndAuto) => Promise; + constructor( + lang: LangTypeAndAuto, + saveSettingFunc: (tolang: LangTypeAndAuto) => Promise + ) { + this.lang = lang; + this.saveSettingFunc = saveSettingFunc; + } + async changeTo(anotherLang: LangTypeAndAuto) { + this.lang = anotherLang; + await this.saveSettingFunc(anotherLang); + } + + _get(key: TransItemType) { + let realLang = this.lang; + if (this.lang === "auto" && moment.locale().replace("-", "_") in LANGS) { + realLang = moment.locale().replace("-", "_") as LangType; + } else { + realLang = "en"; + } + + const localVal = (LANGS[realLang] as (typeof LANGS)["en"])[key]; + const enVal = LANGS["en"][key]; + const res: string = localVal !== undefined ? localVal : (enVal !== undefined ? enVal : key); + return res; + } + + t(key: TransItemType, vars?: Record) { + if (vars === undefined) { + return this._get(key); + } + return Mustache.render(this._get(key), vars); + } +} diff --git a/src/icons.ts b/src/icons.ts new file mode 100644 index 0000000..ea9d6d2 --- /dev/null +++ b/src/icons.ts @@ -0,0 +1,168 @@ +export const SVG_S3 = `AS3Logo`; +export const SVG_DROPBOX = ` + + +`; +export const SVG_ONEDRIVE = `OfficeCore10_32x_24x_20x_16x_01-22-2019`; +export const SVG_GDRIVE = ``; +export const SVG_BOX = ` + + + + + + + + + +`; +export const SVG_PCLOUD = ` + + + + + + + + + + + + + + +`; +export const SVG_YANDEX = ` + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; +export const SVG_KOOFR = ` + + + + + + Etiquette Icons + + + + hash + + computer + icons + folder + + + + + Andy Fitzsimon + + + + + Andy Fitzsimon + + + + + Andy Fitzsimon + + + + image/svg+xml + + + en + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; +export const SVG_AZURE = ``; +export const SVG_WEBDAV = ``; +export const SVG_WEBDIS = ``; diff --git a/src/importExport.ts b/src/importExport.ts new file mode 100644 index 0000000..508d8fd --- /dev/null +++ b/src/importExport.ts @@ -0,0 +1,144 @@ +import cloneDeep from "lodash/cloneDeep"; +import QRCode from "qrcode"; +// BYOC: onedrivefull shrinker — stub until provider is implemented in Batch 3 +const getShrinkedSettingsOnedriveFull = (config: any) => cloneDeep(config); +import { + COMMAND_URI, + type QRExportType, + type RemotelySavePluginSettings, + type UriParams, +} from "./baseTypes"; +import { getShrinkedSettings as getShrinkedSettingsOnedrive } from "./fsOnedrive"; + +export const exportQrCodeUri = async ( + settings: RemotelySavePluginSettings, + currentVaultName: string, + pluginVersion: string, + exportFields: QRExportType +) => { + let settings2: Partial = {}; + + if (exportFields === "basic_and_advanced") { + settings2 = cloneDeep(settings); + delete settings2.s3; + delete settings2.dropbox; + delete settings2.onedrive; + delete settings2.onedrivefull; + delete settings2.webdav; + delete settings2.webdis; + delete settings2.googledrive; + delete settings2.box; + delete settings2.pcloud; + delete settings2.yandexdisk; + delete settings2.koofr; + delete settings2.azureblobstorage; + delete settings2.pro; + } else if (exportFields === "s3") { + settings2 = { s3: cloneDeep(settings.s3) }; + } else if (exportFields === "dropbox") { + settings2 = { dropbox: cloneDeep(settings.dropbox) }; + } else if (exportFields === "onedrive") { + settings2 = { onedrive: getShrinkedSettingsOnedrive(settings.onedrive) }; + } else if (exportFields === "onedrivefull") { + settings2 = { + onedrivefull: getShrinkedSettingsOnedriveFull(settings.onedrivefull), + }; + } else if (exportFields === "webdav") { + settings2 = { webdav: cloneDeep(settings.webdav) }; + } else if (exportFields === "webdis") { + settings2 = { webdis: cloneDeep(settings.webdis) }; + } else if (exportFields === "googledrive") { + settings2 = { googledrive: cloneDeep(settings.googledrive) }; + } else if (exportFields === "box") { + settings2 = { box: cloneDeep(settings.box) }; + } else if (exportFields === "pcloud") { + settings2 = { pcloud: cloneDeep(settings.pcloud) }; + } else if (exportFields === "yandexdisk") { + settings2 = { yandexdisk: cloneDeep(settings.yandexdisk) }; + } else if (exportFields === "koofr") { + settings2 = { koofr: cloneDeep(settings.koofr) }; + } else if (exportFields === "azureblobstorage") { + settings2 = { azureblobstorage: cloneDeep(settings.azureblobstorage) }; + } + + delete settings2.vaultRandomID; + const data = encodeURIComponent(JSON.stringify(settings2)); + const vault = encodeURIComponent(currentVaultName); + const version = encodeURIComponent(pluginVersion); + const rawUri = `obsidian://${COMMAND_URI}?func=settings&version=${version}&vault=${vault}&data=${data}`; + // console.info(uri) + const imgUri = await QRCode.toDataURL(rawUri); + return { + rawUri, + imgUri, + }; +}; + +export interface ProcessQrCodeResultType { + status: "error" | "ok"; + message: string; + result?: RemotelySavePluginSettings; +} + +/** + * we also support directly parse the uri, instead of relying on web browser + * @param input + */ +export const parseUriByHand = (input: string) => { + if ( + !input.startsWith(`obsidian://${COMMAND_URI}?func=settings&`) && + !input.startsWith(`obsidian://${COMMAND_URI_LEGACY}?func=settings&`) + ) { + throw Error(`not valid string`); + } + + const k = new URL(input); + const output = Object.fromEntries(k.searchParams); + return output; +}; + +export const importQrCodeUri = ( + inputParams: any, + currentVaultName: string +): ProcessQrCodeResultType => { + const params = inputParams as UriParams; + if ( + params.func === undefined || + params.func !== "settings" || + params.vault === undefined || + params.data === undefined + ) { + return { + status: "error", + message: `the uri is not for exporting/importing settings: ${JSON.stringify( + inputParams + )}`, + }; + } + + if (params.vault !== currentVaultName) { + return { + status: "error", + message: `the target vault is ${ + params.vault + } but you are currently in ${currentVaultName}: ${JSON.stringify( + inputParams + )}`, + }; + } + + let settings = {} as RemotelySavePluginSettings; + try { + settings = JSON.parse(params.data); + } catch (e) { + return { + status: "error", + message: `errors while parsing settings: ${JSON.stringify(inputParams)}`, + }; + } + return { + status: "ok", + message: "ok", + result: settings, + }; +}; diff --git a/src/langs/LICENSE b/src/langs/LICENSE new file mode 100644 index 0000000..e72929e --- /dev/null +++ b/src/langs/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + \ No newline at end of file diff --git a/src/langs/README.md b/src/langs/README.md new file mode 100644 index 0000000..934ff40 --- /dev/null +++ b/src/langs/README.md @@ -0,0 +1,14 @@ +# Translations for Remotely Save + +## How To Add A Language? + +1. Copy `en.json` to a new json file named `.json`, and translate all the items inside. The language should match what is available in Obsidian app. +2. Modify the `index.ts` file to include the new language file. + +~~## Why Seperated Repo?~~ + +~~For better pull requests management.~~ + +## No more `lang` submodule from 20240106 + +It's actually harder to manage pull requests of submodule for me. The submodule is merged back to the main repo on 20240106. Any further improved translations (and/or pull requestes) should be dealt with in the main repo. diff --git a/src/langs/en.json b/src/langs/en.json new file mode 100644 index 0000000..0b6af11 --- /dev/null +++ b/src/langs/en.json @@ -0,0 +1,554 @@ +{ + "confirm": "Confirm", + "disable": "Disable", + "enable": "Enable", + "goback": "Go Back", + "submit": "Submit", + "sometext": "Here are some texts.", + "syncrun_alreadyrunning": "New command {{newTriggerSource}} stops because {{pluginName}} is already running in stage {{syncStatus}}!", + "syncrun_syncingribbon": "{{pluginName}}: syncing from {{triggerSource}}", + "syncrun_step0": "0/8 BYOC is running in dry mode, thus not actual file changes would happen.", + "syncrun_step1": "1/8 BYOC is preparing ({{serviceType}})", + "syncrun_step2": "2/8 Starting to fetch remote meta data and check password.", + "syncrun_passworderr": "Something goes wrong while checking password.", + "syncrun_step3": "3/8 Starting to process remote data.", + "syncrun_step4": "4/8 Starting to fetch local meta data.", + "syncrun_step5": "5/8 Starting to fetch local prev sync data.", + "syncrun_step6": "6/8 Starting to generate sync plan.", + "syncrun_step7": "7/8 BYOC Sync data is exchanging!", + "syncrun_step7skip": "7/8 BYOC real sync is skipped in dry run mode.", + "syncrun_step8": "8/8 BYOC finished!", + "syncrun_shortstep0": "0/2 BYOC is running in dry mode, not actual file changes would happen.", + "syncrun_shortstep1": "1/2 BYOC starts running ({{serviceType}})", + "syncrun_shortstep2skip": "2/2 BYOC real sync is skipped in dry run mode.", + "syncrun_shortstep2": "2/2 BYOC finished!", + "syncrun_abort": "{{manifestID}}-{{theDate}}: abort sync, triggerSource={{triggerSource}}, error while {{syncStatus}}", + "syncrun_abort_protectmodifypercentage": "Abort! you set changing files \u003e= {{protectModifyPercentage}}% is not allowed but {{realModifyDeleteCount}}/{{allFilesCount}}={{percent}}% is going to be modified or deleted! If you are sure you want this sync, please adjust the allowed ratio in the settings.", + "protocol_saveqr": "New settings for {{manifestName}} is imported and saved. Reopen the plugin settings to make it effective.", + "protocol_callbacknotsupported": "Your uri calls a callback that\u0027s not supported yet: {{params}}", + "protocol_dropbox_connecting": "Connecting to Dropbox...\nPlease DO NOT close this modal.", + "protocol_dropbox_connect_succ": "Connected to Dropbox as {{username}}!", + "protocol_dropbox_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_dropbox_connect_fail": "Something went wrong from response from Dropbox. Maybe the network connection is not good. Maybe you rejected the auth?", + "protocol_dropbox_connect_unknown": "Do not know how to deal with the callback: {{params}}", + "protocol_dropbox_no_modal": "You are not starting Dropbox connection from the settings page. Abort.", + "protocol_onedrive_connecting": "Connecting to OneDrive...\nPlease DO NOT close this modal.", + "protocol_onedrive_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_onedrive_connect_fail": "Something went wrong from response from OneDrive. Maybe you rejected the auth?", + "protocol_onedrive_connect_unknown": "Do not know how to deal with the callback: {{params}}", + "command_startsync": "start sync", + "command_drynrun": "start sync (dry run only)", + "command_exportsyncplans_1_only_change": "export sync plans (latest 1) (change part)", + "command_exportsyncplans_1": "export sync plans (latest 1)", + "command_exportsyncplans_5": "export sync plans (latest 5)", + "command_exportsyncplans_all": "export sync plans (all)", + "command_exportlogsindb": "export logs saved in db", + "statusbar_sync_source_manual": "Manual: ", + "statusbar_sync_source_dry": "Dry: ", + "statusbar_sync_source_auto": "Auto: ", + "statusbar_sync_source_auto_once_init": "Auto (init): ", + "statusbar_sync_source_auto_sync_on_save": "Auto (save): ", + "statusbar_sync_status_prefix_success": "Successfully synced ", + "statusbar_sync_status_prefix_failed": "Failed to sync ", + "statusbar_time_years": "{{time}} years ago", + "statusbar_time_months": "{{time}} months ago", + "statusbar_time_weeks": "{{time}} weeks ago", + "statusbar_time_days": "{{time}} days ago", + "statusbar_time_hours": "{{time}} hours ago", + "statusbar_time_minutes": "{{time}} minutes ago", + "statusbar_time_lessminute": "last minute ago", + "statusbar_time_now": "just now", + "statusbar_syncing": "Syncing...", + "statusbar_lastsync_label": "on {{date}}", + "statusbar_lastsync_never": "Never Synced", + "statusbar_lastsync_never_label": "Never Synced before", + "modal_password_title": "Hold on and PLEASE READ ON...", + "modal_password_shortdesc": "If the field is not empty, files would be encrypted locally before being uploaded.\nIf the field is empty, then files would be uploaded without encryption.", + "modal_password_attn1": "The password itself is stored LOCALLY in PLAIN TEXT. The vault name, remote directory names, and object sizes are NOT encrypted. The remote store must be completely clean before applying or changing encryption formats.", + "modal_password_secondconfirm": "The Second Confirm to change password.", + "modal_password_notice": "New password saved!", + "modal_encryptionmethod_title": "Hold on and PLEASE READ ON...", + "modal_encryptionmethod_shortdesc": "You are changing the encrpytion method but you have set the password before.\nAfter switching the method, you need to \u003cb\u003emanually\u003c/b\u003e and \u003cb\u003efully\u003c/b\u003e delete every encrypted vault files in the remote and re-sync (so that re-upload) the newly encrypted files again.", + "modal_remotebasedir_title": "You are changing the remote base directory config", + "modal_remotebasedir_shortdesc": "1. The plugin would NOT automatically move the content from the old directory to the new one directly on the remote. Everything syncs from the beginning again.\n2. If you set the string to the empty, the config would be reset to use the vault folder name (the default config).\n3. The remote directory name itself would not be encrypted even you\u0027ve set an E2E password.\n4. Some special char like \u0027?\u0027, \u0027/\u0027, \u0027\\\u0027 are not allowed. Spaces in the beginning or in the end are also trimmed.", + "modal_remotebasedir_invaliddirhint": "Your input contains special characters like \u0027?\u0027, \u0027/\u0027, \u0027\\\u0027 which are not allowed.", + "modal_remotebasedir_secondconfirm_vaultname": "Reset To The Default Vault Folder Name", + "modal_remotebasedir_secondconfirm_change": "Confirm To Change", + "modal_remotebasedir_notice": "New remote base directory config saved!", + "modal_remoteprefix_s3_title": "You are changing the remote prefix config", + "modal_remoteprefix_s3_shortdesc": "1. The plugin would NOT automatically move the content from the old directory to the new one directly on the remote. Everything syncs from the beginning again.\n2. If you set the string to the empty, the prefix will be empty and the files will be saved at the root of the bucket.\n3. The remote directory name itself would not be encrypted even you\u0027ve set an E2E password.\n4. Some special char like \u0027?\u0027, \u0027/\u0027, \u0027\\\u0027 are not allowed. Spaces in the beginning or in the end are also trimmed.", + "modal_remoteprefix_s3_invaliddirhint": "Your input contains special characters like \u0027?\u0027, \u0027/\u0027, \u0027\\\u0027 which are not allowed.", + "modal_remoteprefix_s3_tosave": "The prefix to save is \"{{{prefix}}}\"", + "modal_remoteprefix_s3_secondconfirm_empty": "The prefix is empty and the files will be saved at the root of the bucket.", + "modal_remoteprefix_s3_secondconfirm_change": "Confirm To Change", + "modal_remoteprefix_s3_notice": "New remote prefix config saved!", + "modal_dropboxauth_manualsteps": "Step 1: Visit the address in a browser, and follow the steps.\nStep 2: In the end of the web flow, you obtain a long code. Paste it here then click \"Submit\".", + "modal_dropboxauth_autosteps": "Visit the address in a browser, and follow the steps.\nFinally you should be redirected to Obsidian.", + "modal_dropboxauth_copybutton": "Click to copy the auth url", + "modal_dropboxauth_copynotice": "The auth url is copied to the clipboard!", + "modal_dropboxauth_maualinput": "Auth Code from web page", + "modal_dropboxauth_maualinput_desc": "You need to click \"Confirm\".", + "modal_dropboxauth_maualinput_notice": "Trying to connect to Dropbox", + "modal_dropboxauth_maualinput_conn_succ": "Connected to Dropbox as {{username}}!", + "modal_dropboxauth_maualinput_conn_succ_revoke": "Connected as {{username}} — click to disconnect.", + "modal_dropboxauth_maualinput_conn_fail": "Something goes wrong while connecting to Dropbox.", + "modal_onedriveauth_shortdesc": "Currently only OneDrive for personal is supported. OneDrive for Business is NOT supported (yet).\nVisit the address in a browser, and follow the steps.\nFinally you should be redirected to Obsidian.", + "modal_onedriveauth_shortdesc_linux": "It seems that you are using Obsidian on Linux, and you might not be able to jump back here properly. Please consider \u003ca href=\"https://github.com/remotely-save/remotely-save/issues/415\"\u003eusing\u003c/a\u003e the flatpack version of Obsidian, or creating an \u003ca href=\"https://github.com/remotely-save/remotely-save/blob/master/docs/linux.md\"\u003e\u003ccode\u003eobsidian.desktop\u003c/code\u003e file\u003c/a\u003e.", + "modal_onedriveauth_copybutton": "Click to copy the auth url", + "modal_onedriveauth_copynotice": "The auth url is copied to the clipboard!", + "modal_onedriverevokeauth_step1": "Step 1: Go to the following address, click the \"Edit\" button for the plugin, then click \"Remove these permissions\" button on the page.", + "modal_onedriverevokeauth_step2": "Step 2: Click the button below, to clean the locally-saved login credentials.", + "modal_onedriverevokeauth_clean": "Clean Locally-Saved Login Credentials", + "modal_onedriverevokeauth_clean_desc": "You need to click the button.", + "modal_onedriverevokeauth_clean_button": "Clean", + "modal_onedriverevokeauth_clean_notice": "Cleaned!", + "modal_onedriverevokeauth_clean_fail": "Something goes wrong while revoking.", + "modal_syncconfig_attn": "Attention 1/2: This only syncs (copies) the whole Obsidian config dir, not other starting-with-dot folders or files. Except for ignoring folders .git and node_modules, it also doesn\u0027t understand the meaning of sub-files and sub-folders inside the config dir.\nAttention 2/2: After the config dir is synced, plugins settings might be corrupted, and Obsidian might need to be restarted to load the new settings.\nIf you are agreed to take your own risk, please click the following second confirm button.", + "modal_syncconfig_secondconfirm": "The Second Confirm To Enable.", + "modal_syncconfig_notice": "You\u0027ve enabled syncing config folder!", + "modal_qr_shortdesc": "This exports (partial) settings.\nYou can use another device to scan this qrcode.\nOr, you can click the button to copy the special uri and paste it into another device\u0027s web browser or BYOC Import Setting.", + "modal_qr_button": "Click to copy the special URI", + "modal_qr_button_notice": "The special uri is copied to the clipboard!", + "modal_sizesconflict_title": "BYOC: Some conflict were found while skipping large files", + "modal_sizesconflict_desc": "You\u0027ve set skipping files larger than {{thresholdMB}} MB ({{thresholdBytes}} bytes).\nBut the following files have sizes larger than the threshold on one side, and sizes smaller than the threshold on the other side.\nTo avoid unexpected overwriting or deleting, the plugin stops, and you have to manually deal with at least one side of the files.", + "modal_sizesconflict_copybutton": "Click to copy all the below sizes conflicts info", + "modal_sizesconflict_copynotice": "All the sizes conflicts info have been copied to the clipboard!", + "settings_basic": "Basic Settings", + "settings_password": "Encryption Password", + "settings_password_desc": "Require an End-to-End encryption password. Leave blank for no encryption. Note: Passwords are saved locally in plain text. If you change your password later, you must manually delete all files from the remote location and re-upload.", + "settings_encryptionmethod": "Encryption Method", + "settings_encryptionmethod_desc": "Select your E2E encryption format. The RClone Crypt format is heavily recommended. Note: Switching algorithms requires manual deletion of all remote encrypted files prior to re-uploading.", + "settings_encryptionmethod_rclone": "RClone Crypt (recommended)", + "settings_encryptionmethod_openssl": "OpenSSL enc (legacy)", + "settings_autorun": "Schedule For Auto Run", + "settings_autorun_desc": "Triggers an automatic background sync based on the defined interval.", + "settings_autorun_notset": "(not set)", + "settings_autorun_1min": "every 1 minute", + "settings_autorun_5min": "every 5 minutes", + "settings_autorun_10min": "every 10 minutes", + "settings_autorun_30min": "every 30 minutes", + "settings_runoncestartup": "Run Once On Start Up Automatically", + "settings_runoncestartup_desc": "Triggers a single background sync shortly after Obsidian starts. Operates independently from the standard interval timer.", + "settings_runoncestartup_notset": "(not set)", + "settings_runoncestartup_1sec": "sync once after 1 second of start up", + "settings_runoncestartup_10sec": "sync once after 10 seconds of start up", + "settings_runoncestartup_30sec": "sync once after 30 seconds of start up", + "settings_synconsave": "Sync On Save (experimental)", + "settings_synconsave_desc": "Trigger a sync shortly after a file is modified.", + "settings_synconsave_disable": "Disable (default)", + "settings_synconsave_enable": "Enable", + "settings_skiplargefiles": "Skip Large Files", + "settings_skiplargefiles_desc": "Skip files above this size. 1 MB = 10⁶ bytes.", + "settings_skiplargefiles_notset": "(not set)", + "settings_ignorepaths": "Regex Of Paths To Ignore", + "settings_ignorepaths_desc": "Regex of paths of folders or files to ignore. One regex per line. The path is relative to the vault root without leading slash. Non-empty allow paths and non-empty ignore paths are effective together.", + "settings_onlyallowpaths": "Regex Of Paths To Allow", + "settings_onlyallowpaths_desc": "Regex of paths of folders or files to allow to sync. One regex per line. The path is relative to the vault root without leading slash. If this setting is not empty, all paths you want to sync including config dirs must be explicitly set here. Non-empty allow paths and non-empty ignore paths are effective together.", + "settings_enablestatusbar_info": "Show Last Successful Sync In Status Bar", + "settings_enablestatusbar_info_desc": "Displays the timestamp of your last successful sync operation at the bottom of the window.", + "settings_enablestatusbar_reloadrequired_notice": "Reload the plugin for the changes to take effect.", + "settings_resetstatusbar_time": "Reset Last Successful Sync Time", + "settings_resetstatusbar_time_desc": "", + "settings_resetstatusbar_button": "Reset", + "settings_resetstatusbar_notice": "Reset done!", + "settings_checkonnectivity": "Check Connectivity", + "settings_checkonnectivity_desc": "", + "settings_checkonnectivity_button": "Check", + "settings_checkonnectivity_checking": "Checking...", + "settings_remotebasedir": "Change The Remote Base Directory (experimental)", + "settings_remotebasedir_desc": "Folder name on remote. Defaults to vault name. Clear field to reset.", + "settings_remoteprefix_s3": "Change The Remote Prefix (experimental)", + "settings_remoteprefix_s3_desc": "By default in s3 the files are saved at the root of the bucket. You can change the remote prefix here, or keep the input field empty to reset to the default. You need to click \"Confirm\".", + "settings_s3": "Amazon S3 / Compatible", + "settings_s3_disclaimer1": "Disclaimer: This plugin is NOT an official Amazon product.", + "settings_s3_disclaimer2": "Disclaimer: The information is stored locally. Other malicious/harmful/faulty plugins could read the info. If you see any unintentional access to your bucket, please immediately delete the access key on your AWS (or other S3-service provider) settings.", + "settings_s3_cors": "You need to configure CORS to allow requests from origin app://obsidian.md and capacitor://localhost and http://localhost, and add ETag into exposed headers.", + "settings_s3_prod": "Some Amazon S3 official docs for references:", + "settings_s3_prod1": "Endpoint and region info", + "settings_s3_prod2": "Access Key ID and Secret Access Key info", + "settings_s3_prod3": "Configuring CORS", + "settings_s3_endpoint": "Endpoint", + "settings_s3_region": "Region", + "settings_s3_region_desc": "If unsure, try \u0027us-east-1\u0027.", + "settings_s3_accesskeyid": "Access Key ID", + "settings_s3_accesskeyid_desc": "Your assigned Access Key ID.", + "settings_s3_secretaccesskey": "Secret Access Key", + "settings_s3_secretaccesskey_desc": "Your assigned Secret Access Key.", + "settings_s3_bucketname": "Bucket Name", + "settings_s3_bypasscorslocally": "Bypass CORS Issue Locally", + "settings_s3_bypasscorslocally_desc": "Allows skipping server CORS configurations on modern Obsidian builds. If you encounter sync failures, disable this and manually configure CORS on your server.", + "settings_s3_parts": "Parts Concurrency", + "settings_s3_parts_desc": "Large files are split into small parts to upload in S3. How many parts do you want to upload in parallel at most?", + "settings_s3_accuratemtime": "Use Accurate MTime", + "settings_s3_accuratemtime_desc": "Read the uploaded accurate last modified time for better sync algorithm. But it causes extra api requests / time / money to the S3 endpoint.", + "settings_s3_urlstyle": "S3 URL style", + "settings_s3_urlstyle_desc": "Whether to force path-style URLs for S3 objects (e.g., https://s3.amazonaws.com/*/ instead of https://*.s3.amazonaws.com/).", + "settings_s3_reverse_proxy_no_sign_url": "S3 Reverse Proxy (No Sign) Url (experimental)", + "settings_s3_reverse_proxy_no_sign_url_desc": "S3 reverse proxy url without signature. This is useful if you use a revers proxy but do not change the original credential signature. No http(s):// prefix. Leave it blank if you don\u0027t know what it is.", + "settings_s3_generatefolderobject": "Generate Folder Object Or Not", + "settings_s3_generatefolderobject_desc": "S3 does not utilize actual folders. Enabling this uploads a zero-byte object ending with \u0027/\u0027 to emulate folder structure. Disabled by default.", + "settings_s3_generatefolderobject_notgenerate": "Not generate (default)", + "settings_s3_generatefolderobject_generate": "Generate", + "settings_s3_connect_succ": "Great! The bucket can be accessed.", + "settings_s3_connect_fail": "The S3 bucket cannot be reached.", + "settings_dropbox": "Dropbox", + "settings_dropbox_disclaimer1": "Disclaimer: This app is NOT an official Dropbox product.", + "settings_dropbox_disclaimer2": "Credentials are stored locally. Disconnect at dropbox.com/account/connected_apps if needed.", + "settings_dropbox_folder": "We will create and sync inside the folder /Apps/{{pluginID}}/{{remoteBaseDir}} on your Dropbox.", + "settings_dropbox_revoke": "Revoke Auth", + "settings_dropbox_revoke_desc": "Connected as {{username}}", + "settings_dropbox_revoke_button": "Revoke Auth", + "settings_dropbox_revoke_notice": "Revoked!", + "settings_dropbox_revoke_noticeerr": "Something goes wrong while revoking.", + "settings_dropbox_clearlocal": "Clear Locally Saved Credentials", + "settings_dropbox_clearlocal_desc": "You can forcefully clear the locally saved Dropbox login credentials, but not send revoke auth request to the server.", + "settings_dropbox_clearlocal_button": "Clear", + "settings_dropbox_clearlocal_notice": "Cleared!", + "settings_dropbox_auth": "Auth", + "settings_dropbox_auth_desc": "", + "settings_dropbox_auth_button": "Auth", + "settings_dropbox_connect_succ": "Great! We can connect to Dropbox!", + "settings_dropbox_connect_fail": "We cannot connect to Dropbox.", + "settings_onedrive": "OneDrive (App Folder)", + "settings_onedrive_disclaimer1": "Disclaimer: This app is NOT an official Microsoft / OneDrive product.", + "settings_onedrive_disclaimer2": "Credentials are stored locally. Disconnect at microsoft.com/consent if needed.", + "settings_onedrive_folder": "We will create and sync inside the folder /Apps/{{pluginID}}/{{remoteBaseDir}} on your OneDrive.", + "settings_onedrive_nobiz": "Currently only OneDrive for personal is supported. OneDrive for Business is NOT supported (yet).", + "settings_onedrive_revoke": "Revoke Auth", + "settings_onedrive_revoke_desc": "Connected as {{username}}", + "settings_onedrive_revoke_button": "Revoke Auth", + "settings_onedrive_auth": "Auth", + "settings_onedrive_auth_desc": "", + "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_emptyfile": "Empty File Handling", + "settings_onedrive_emptyfile_desc": "OneDrive doesn\u0027t 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": "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\u003e=0.13.25 or iOS\u003e=1.1.1 or Android\u003e=1.2.1 supports bypassing CORS locally. But you are using an old version, and you\u0027re suggested to upgrade Obsidian.", + "settings_webdav_cors": "You need to configure CORS to allow requests from origin app://obsidian.md and capacitor://localhost and http://localhost", + "settings_webdav_folder": "We will create and sync inside the folder /{{remoteBaseDir}} on your server.", + "settings_webdav_addr": "Server Address", + "settings_webdav_addr_desc": "", + "settings_webdav_user": "Username", + "settings_webdav_user_desc": "WebDAV username.", + "settings_webdav_password": "Password", + "settings_webdav_password_desc": "WebDAV password.", + "settings_webdav_auth": "Auth Type", + "settings_webdav_auth_desc": "If no password is provided, this option will be ignored.", + "settings_webdav_depth": "Depth Header Sent To Servers", + "settings_webdav_depth_desc": "Webdav servers should be configured to allow requests with header Depth being \u00271\u0027 or \u0027Infinity\u0027. If you are not sure what\u0027s this, choose \"depth=\u00271\u0027\". If you are sure your server supports depth=\u0027infinity\u0027, please choose that to get way better performance.", + "settings_webdav_depth_1": "only supports depth=\u00271\u0027", + "settings_webdav_depth_inf": "supports depth=\u0027infinity\u0027", + "settings_webdav_customheaders": "Custom Headers Sent To Servers", + "settings_webdav_customheaders_desc": "Some users may want to set custom headers. If you don\u0027t know what it is, just leave it empty. Input them like: \u003ccode\u003eX-Header1:value1\u003c/code\u003e in each line. All spaces around header keys and values will be removed.", + "settings_webdav_connect_succ": "Great! The webdav server can be accessed.", + "settings_webdav_connect_fail": "The webdav server cannot be reached (possible to be any of address/username/password/authtype errors).", + "settings_webdav_connect_fail_withcors": "The webdav server cannot be reached (possible to be any of address/username/password/authtype/CORS errors).", + "settings_webdis": "Webdis", + "settings_webdis_disclaimer1": "Disclaimer: This app is NOT an official Redis® Ltd / Redis® OSS / Webdis product. Redis is a registered trademark of Redis Ltd.", + "settings_webdis_disclaimer2": "Disclaimer: The information is stored locally. Other malicious/harmful/faulty plugins may read the info. If you see any unintentional access to your Webdis server, please immediately change the username and password.", + "settings_webdis_folder": "We will store the value with keys prefixed by :{{remoteBaseDir}} on your server.", + "settings_webdis_addr": "Server Address", + "settings_webdis_addr_desc": "", + "settings_webdis_user": "Username", + "settings_webdis_user_desc": "Webdis username.", + "settings_webdis_password": "Password", + "settings_webdis_password_desc": "Webdis password.", + "settings_webdis_connect_succ": "Great! The Webdis server can be accessed.", + "settings_webdis_connect_fail": "The Webdis server cannot be reached (possible to be any of address/username/password errors).", + "settings_shortdesc": "Configure your personal cloud sync. Choose a provider, authenticate, and your vault will stay synchronized across all your devices.", + "settings_chooseservice": "Choose A Remote Service", + "settings_chooseservice_desc": "Which cloud to sync with?", + "settings_chooseservice_s3": "Amazon S3 / Compatible", + "settings_chooseservice_dropbox": "Dropbox", + "settings_chooseservice_webdav": "WebDAV", + "settings_chooseservice_onedrive": "OneDrive (App Folder)", + "settings_chooseservice_webdis": "Webdis", + "settings_adv": "Advanced Settings", + "settings_concurrency": "Concurrency", + "settings_concurrency_desc": "Max parallel file transfers. Reduce if you hit rate limits.", + "settings_syncunderscore": "Sync _ Files Or Folders", + "settings_syncunderscore_desc": "Sync files or folders starting with _ (\"underscore\") or not", + "settings_configdir": "Sync Config Dir (experimental)", + "settings_configdir_desc": "Syncs the configuration directory. Internal folders like .git and node_modules are ignored. Note: Syncing may impact all active plugins and require an Obsidian restart.", + "settings_bookmarks": "Sync Bookmarks (experimental)", + "settings_bookmarks_desc": "Sync {{configDir}}/bookmarks.json or not. If you sync config dir, this option will be ignore and treated as \"enabled\".", + "settings_deletetowhere": "Deletion Destination", + "settings_deletetowhere_desc": "Which trash should the plugin put the files into while deleting?", + "settings_deletetowhere_system_trash": "system trash (default)", + "settings_deletetowhere_obsidian_trash": "Obsidian .trash folder", + "settings_conflictaction": "Action For Conflict", + "settings_conflictaction_desc": "\u003cp\u003eIf a file is created or modified on both side since last update, it\u0027s a conflict event. How to deal with it? This only works for bidirectional sync.\u003c/p\u003e", + "settings_conflictaction_keep_newer": "newer version survives (default)", + "settings_conflictaction_keep_larger": "larger size version survives", + "settings_cleanemptyfolder": "Action For Empty Folders", + "settings_cleanemptyfolder_desc": "The sync algorithm majorly deals with files, so you need to specify how to deal with empty folders.", + "settings_cleanemptyfolder_skip": "leave them as is (default)", + "settings_cleanemptyfolder_clean_both": "delete local and remote", + "settings_protectmodifypercentage": "Abort Sync If Modification Above Percentage", + "settings_protectmodifypercentage_desc": "Abort sync if more than N% of files would be changed. Protects against accidental mass-delete.", + "settings_protectmodifypercentage_000_desc": "0 (always block)", + "settings_protectmodifypercentage_050_desc": "50 (default)", + "settings_protectmodifypercentage_100_desc": "100 (disable the protection)", + "settings_protectmodifypercentage_custom_desc": "custom", + "settings_protectmodifypercentage_customfield": "Custom Abort Sync If Modification Above Percentage", + "settings_protectmodifypercentage_customfield_desc": "You need to enter a number between 0 (inclusive) and 100 (inclusive). Float number is also allowed.", + "setting_syncdirection": "Sync Direction (experimental)", + "setting_syncdirection_desc": "\u003cp\u003eWhich direction should the plugin sync to? Please be aware that only CHANGED files (based on time and size) are synced regardless any option.\u003c/p\u003e\u003col\u003e\u003cli\u003eBidirectional: Changes on both side are synced to the other side.\u003c/li\u003e\u003cli\u003eIncremental Push: Locally created or modifed files are copied to remote.\u003c/li\u003e\u003cli\u003eIncremental Pull: Remotely created or modifed files are copied to local.\u003c/li\u003e\u003cli\u003eIncremental Push And Delete: Locally created or modifed files are copied to remote, \u003cstrong\u003eand local deletion operations are run on remote too.\u003c/strong\u003e\u003c/li\u003e\u003cli\u003eIncremental Pull And Delete: Remotely created or modifed files are copied to local, \u003cstrong\u003eand remote deletion operations are run on local too.\u003c/strong\u003e\u003c/li\u003e\u003c/ol\u003e", + "setting_syncdirection_bidirectional_desc": "Bidirectional (default)", + "setting_syncdirection_incremental_push_only_desc": "Incremental Push (aka backup mode)", + "setting_syncdirection_incremental_pull_only_desc": "Incremental Pull", + "setting_syncdirection_incremental_push_and_delete_only_desc": "Incremental Push And Delete", + "setting_syncdirection_incremental_pull_and_delete_only_desc": "Incremental Pull And Delete", + "settings_enablemobilestatusbar": "Mobile Status Bar (experimental)", + "settings_enablemobilestatusbar_desc": "By default Obsidian mobile hides status bar. But some users want to show it up. So here is a hack.", + "settings_importexport": "Import and Export Partial Settings", + "settings_export": "Export", + "settings_export_desc": "Export settings by generating a QR code or URI.", + "settings_export_basic_and_advanced_button": "Export Basic And Advanced Part", + "settings_export_s3_button": "Export S3 Part", + "settings_export_dropbox_button": "Export Dropbox Part", + "settings_export_onedrive_button": "Export OneDrive (App Folder) Part", + "settings_export_webdav_button": "Export Webdav Part", + "settings_export_webdis_button": "Export Webdis Part", + "settings_import": "Import", + "settings_import_desc": "Paste the exported URI into here and click \"Import\". Or, you can open a camera or scan-qrcode app to scan the QR code.", + "settings_import_button": "Import", + "settings_import_error_notice": "Your URI string is empty or not correct!", + "settings_debug": "Debug", + "settings_debuglevel": "Alter Notice Level", + "settings_debuglevel_desc": "By default the notice level is \"info\". You can change to \"debug\" to get verbose information while syncing.", + "settings_outputsettingsconsole": "Output Current Settings From Disk To Console", + "settings_outputsettingsconsole_desc": "Outputs decoded settings to the developer console.", + "settings_outputsettingsconsole_button": "Output", + "settings_outputsettingsconsole_notice": "Finished outputing in console.", + "settings_obfuscatesettingfile": "Obfuscate The Setting File Or Not", + "settings_obfuscatesettingfile_desc": "Recommended: encodes data.json to obscure stored credentials.", + "settings_viewconsolelog": "View Console Log", + "settings_viewconsolelog_desc": "On desktop, please press \"ctrl+shift+i\" or \"cmd+option+i\" to view the log. On mobile, please install the third-party plugin \u003ca href=\u0027https://obsidian.md/plugins?search=Logstravaganza\u0027\u003eLogstravaganza\u003c/a\u003e to export the console log to a note.", + "settings_syncplans": "Export Sync Plans", + "settings_syncplans_desc": "Sync plans are created every time after you trigger sync and before the actual sync. Useful to know what would actually happen in those sync. Click the button to export sync plans.", + "settings_syncplans_button_1_only_change": "Export latest 1 (change part)", + "settings_syncplans_button_5_only_change": "Export latest 5 (change part)", + "settings_syncplans_button_1": "Export latest 1", + "settings_syncplans_button_5": "Export latest 5", + "settings_syncplans_button_all": "Export All", + "settings_syncplans_notice": "Sync plans history exported.", + "settings_delsyncplans": "Delete Sync Plans History In DB", + "settings_delsyncplans_desc": "Delete sync plans history in DB.", + "settings_delsyncplans_button": "Delete Sync Plans History", + "settings_delsyncplans_notice": "Sync plans history (in DB) deleted.", + "settings_delprevsync": "Delete Prev Sync Details In DB", + "settings_delprevsync_desc": "The sync algorithm keeps the previous successful sync information in DB to determine the file changes. If you want to ignore them so that all files are treated newly created, you can delete the prev sync info here.", + "settings_delprevsync_button": "Delete Prev Sync Details", + "settings_delprevsync_notice": "Previous sync history (in local DB) deleted", + "settings_profiler_results": "Export Profiler Results", + "settings_profiler_results_desc": "The plugin records the time cost of each steps. Here you can export them to know which step is slow.", + "settings_profiler_results_notice": "Profiler results exported.", + "settings_profiler_results_button_all": "Export All", + "settings_profiler_enableprofiler": "Enable Profiler", + "settings_profiler_enableprofiler_desc": "Collect performance data or not?", + "settings_profiler_enabledebugprint": "Enable Profiler Printing", + "settings_profiler_enabledebugprint_desc": "Print profiler result in each insertion to console or not?", + "settings_profiler_recordsize": "Enable Profiler Recording Size", + "settings_profiler_recordsize_desc": "Let profiler record object sizes or not?", + "settings_outputbasepathvaultid": "Output Vault Base Path And Randomly Assigned ID", + "settings_outputbasepathvaultid_desc": "For debugging purposes.", + "settings_outputbasepathvaultid_button": "Output", + "settings_resetcache": "Reset Local Internal Cache/Databases", + "settings_resetcache_desc": "Reset local internal caches/databases (for debugging purposes). You would want to reload the plugin after resetting this. This option will not empty the {s3, password...} settings.", + "settings_resetcache_button": "Reset", + "settings_resetcache_notice": "Local internal cache/databases deleted. Please manually reload the plugin.", + "syncalgov3_title": "BYOC has HUGE updates on the sync algorithm", + "syncalgov3_texts": "Welcome to use BYOC!\nFrom this version, a new algorithm has been developed:\n\u003cul\u003e\u003cli\u003eMore robust deletion sync,\u003c/li\u003e\u003cli\u003eminimal conflict handling,\u003c/li\u003e\u003cli\u003eno meta data uploaded any more,\u003c/li\u003e\u003cli\u003edeletion / modification protection,\u003c/li\u003e\u003cli\u003ebackup mode\u003c/li\u003e\u003cli\u003enew encryption method\u003c/li\u003e\u003cli\u003e...\u003c/li\u003e\u003c/ul\u003e\nStay tune for more! A full introduction is in the \u003ca href=\u0027https://github.com/remotely-save/remotely-save/tree/master/docs/sync_algorithm/v3/intro.md\u0027\u003edoc website\u003c/a\u003e.\nIf you agree to use this, please read and check two checkboxes then click the \"Agree\" button, and enjoy the plugin!\nIf you do not agree, please click the \"Do Not Agree\" button, the plugin will unload itself.\nAlso, please consider \u003ca href=\u0027https://github.com/remotely-save/remotely-save\u0027\u003evisit the GitHub repo and star ⭐ it\u003c/a\u003e! Or even \u003ca href=\u0027https://github.com/remotely-save/donation\u0027\u003ebuy me a coffee\u003c/a\u003e. Your support is very important to me! Thanks!", + "syncalgov3_checkbox_manual_backup": "I will backup my vault manually firstly.", + "syncalgov3_checkbox_requiremultidevupdate": "I understand I need to update the plugin ACROSS ALL DEVICES to make them work properly.", + "syncalgov3_button_agree": "Agree", + "syncalgov3_button_disagree": "Do Not Agree", + "menu_check_file_stat": "Check file stats", + "settings_chooseservice_googledrive": "Google Drive", + "settings_chooseservice_box": "Box", + "settings_chooseservice_pcloud": "pCloud", + "settings_chooseservice_yandexdisk": "Yandex Disk", + "settings_chooseservice_koofr": "Koofr", + "settings_chooseservice_azureblobstorage": "Azure Blob Storage", + "settings_chooseservice_onedrivefull": "OneDrive (Full Access)", + "settings_export_googledrive_button": "Export Google Drive Part", + "settings_export_box_button": "Export Box Part", + "settings_export_pcloud_button": "Export pCloud Part", + "settings_export_yandexdisk_button": "Export Yandex Disk Part", + "settings_export_koofr_button": "Export Koofr Part", + "settings_export_azureblobstorage_button": "Export Azure Blob Storage Part", + "settings_export_onedrivefull_button": "Export OneDrive (Full Access) Part", + "settings_conflictaction_smart_conflict": "Smart conflict (keep both)", + "settings_conflictaction_smart_conflict_desc": "Keep both versions with timestamped suffixes on conflict.", + "protocol_onedrivefull_connecting": "Connecting to OneDrive (Full Access)...\nPlease DO NOT close this modal.", + "protocol_onedrivefull_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_onedrivefull_connect_fail": "Something went wrong connecting to OneDrive (Full Access).", + "protocol_onedrivefull_connect_unknown": "Do not know how to deal with the callback: {{params}}", + "protocol_box_connecting": "Connecting to Box...\nPlease DO NOT close this modal.", + "protocol_box_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_box_connect_fail": "Something went wrong connecting to Box.", + "protocol_pcloud_connecting": "Connecting to pCloud...\nPlease DO NOT close this modal.", + "protocol_pcloud_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_pcloud_connect_fail": "Something went wrong connecting to pCloud.", + "protocol_yandexdisk_connecting": "Connecting to Yandex Disk...\nPlease DO NOT close this modal.", + "protocol_yandexdisk_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_yandexdisk_connect_fail": "Something went wrong connecting to Yandex Disk.", + "protocol_koofr_connecting": "Connecting to Koofr...\nPlease DO NOT close this modal.", + "protocol_koofr_connect_succ_revoke": "Connected as {{username}} — click to disconnect.", + "protocol_koofr_connect_fail": "Something went wrong connecting to Koofr.", + "settings_pcloud": "pCloud", + "settings_pcloud_folder": "Base folder in pCloud: /{{remoteBaseDir}}", + "settings_pcloud_auth": "Authorize", + "settings_pcloud_auth_desc": "", + "settings_pcloud_auth_button": "Authorize", + "settings_pcloud_revoke": "Revoke Authorization", + "settings_pcloud_revoke_desc": "", + "settings_pcloud_revoke_button": "Revoke", + "modal_pcloudauth_tutorial": "Step 1: Click the link below to securely authorize obsidian-byoc. Step 2: The app will automatically capture the token.", + "modal_pcloudauth_copybutton": "Copy Link", + "modal_pcloudauth_copynotice": "Copied to clipboard.", + "modal_pcloudrevokeauth_step1": "To fully revoke access, visit pCloud Apps and remove obsidian-byoc.", + "modal_pcloudrevokeauth_step2": "Click clean to wipe tokens locally.", + "modal_pcloudrevokeauth_clean": "Clean up local records", + "modal_pcloudrevokeauth_clean_desc": "Deletes local pCloud access tokens.", + "modal_pcloudrevokeauth_clean_button": "Clean", + "modal_pcloudrevokeauth_clean_notice": "Local records cleaned.", + "modal_pcloudrevokeauth_clean_fail": "Failed to clean local records.", + "settings_pcloud_connect_succ": "pCloud Check: Success!", + "settings_pcloud_connect_fail": "pCloud Check: Failed!", + "settings_box": "Box", + "settings_box_folder": "Base folder in Box: /{{remoteBaseDir}}", + "settings_box_auth": "Authorize Box", + "settings_box_auth_desc": "", + "settings_box_auth_button": "Authorize", + "settings_box_revoke": "Revoke Authorization", + "settings_box_revoke_desc": "", + "settings_box_revoke_button": "Revoke", + "settings_box_connect_succ": "Box Check: Success!", + "settings_box_connect_fail": "Box Check: Failed!", + "modal_boxauth_tutorial": "Click the link or copy the URL and open in a browser. After authorizing, you will be redirected back to Obsidian automatically.", + "modal_boxauth_copybutton": "Copy Auth URL", + "modal_boxauth_copynotice": "Auth URL copied to clipboard.", + "modal_boxrevokeauth_step1": "To fully revoke access, visit box.com and remove this app under Connected Apps in your account settings.", + "modal_boxrevokeauth_step2": "Click below to clear locally saved credentials.", + "modal_boxrevokeauth_clean": "Clean Local Credentials", + "modal_boxrevokeauth_clean_desc": "Deletes locally saved Box tokens.", + "modal_boxrevokeauth_clean_button": "Clean", + "modal_boxrevokeauth_clean_notice": "Local credentials cleared.", + "modal_boxrevokeauth_clean_fail": "Failed to clear local credentials.", + "settings_yandexdisk": "Yandex Disk", + "settings_yandexdisk_folder": "Base folder in Yandex Disk: /{{remoteBaseDir}}", + "settings_yandexdisk_auth": "Authorize Yandex Disk", + "settings_yandexdisk_auth_desc": "", + "settings_yandexdisk_auth_button": "Authorize", + "settings_yandexdisk_revoke": "Revoke Authorization", + "settings_yandexdisk_revoke_desc": "", + "settings_yandexdisk_revoke_button": "Revoke", + "settings_yandexdisk_connect_succ": "Yandex Disk Check: Success!", + "settings_yandexdisk_connect_fail": "Yandex Disk Check: Failed!", + "modal_yandexdiskauth_tutorial": "Click the link or copy the URL and open in a browser. After authorizing, you will be redirected back to Obsidian automatically.", + "modal_yandexdiskauth_copybutton": "Copy Auth URL", + "modal_yandexdiskauth_copynotice": "Auth URL copied to clipboard.", + "modal_yandexdiskrevokeauth_step1": "To fully revoke access, visit your Yandex account security settings and remove this app.", + "modal_yandexdiskrevokeauth_step2": "Click below to clear locally saved credentials.", + "modal_yandexdiskrevokeauth_clean": "Clean Local Credentials", + "modal_yandexdiskrevokeauth_clean_desc": "Deletes locally saved Yandex Disk tokens.", + "modal_yandexdiskrevokeauth_clean_button": "Clean", + "modal_yandexdiskrevokeauth_clean_notice": "Local credentials cleared.", + "modal_yandexdiskrevokeauth_clean_fail": "Failed to clear local credentials.", + "settings_koofr": "Koofr", + "settings_koofr_folder": "Base folder in Koofr: /{{remoteBaseDir}}", + "settings_koofr_auth": "Authorize Koofr", + "settings_koofr_auth_desc": "", + "settings_koofr_auth_button": "Authorize", + "settings_koofr_revoke": "Revoke Authorization", + "settings_koofr_revoke_desc": "", + "settings_koofr_revoke_button": "Revoke", + "settings_koofr_connect_succ": "Koofr Check: Success!", + "settings_koofr_connect_fail": "Koofr Check: Failed!", + "modal_koofrauth_tutorial": "Click the link or copy the URL and open in a browser. After authorizing, you will be redirected back to Obsidian automatically.", + "modal_koofrauth_copybutton": "Copy Auth URL", + "modal_koofrauth_copynotice": "Auth URL copied to clipboard.", + "modal_koofrrevokeauth_step1": "To fully revoke access, visit Koofr account settings and remove this app under Linked Apps.", + "modal_koofrrevokeauth_step2": "Click below to clear locally saved credentials.", + "modal_koofrrevokeauth_clean": "Clean Local Credentials", + "modal_koofrrevokeauth_clean_desc": "Deletes locally saved Koofr tokens.", + "modal_koofrrevokeauth_clean_button": "Clean", + "modal_koofrrevokeauth_clean_notice": "Local credentials cleared.", + "modal_koofrrevokeauth_clean_fail": "Failed to clear local credentials.", + "settings_onedrivefull": "OneDrive (Full Access)", + "settings_onedrivefull_folder": "Base folder in OneDrive: /{{remoteBaseDir}}", + "settings_onedrivefull_auth": "Authorize OneDrive (Full Access)", + "settings_onedrivefull_auth_desc": "", + "settings_onedrivefull_auth_button": "Authorize", + "settings_onedrivefull_revoke": "Revoke Authorization", + "settings_onedrivefull_revoke_desc": "", + "settings_onedrivefull_revoke_button": "Revoke", + "settings_onedrivefull_connect_succ": "OneDrive (Full Access) Check: Success!", + "settings_onedrivefull_connect_fail": "OneDrive (Full Access) Check: Failed!", + "modal_onedrivefullauth_tutorial": "Click the link or copy the URL and open in a browser. After authorizing, you will be redirected back to Obsidian automatically.", + "modal_onedrivefullauth_copybutton": "Copy Auth URL", + "modal_onedrivefullauth_copynotice": "Auth URL copied to clipboard.", + "modal_onedrivefullrevokeauth_step1": "To fully revoke access, visit https://microsoft.com/consent and remove this app.", + "modal_onedrivefullrevokeauth_step2": "Click below to clear locally saved credentials.", + "modal_onedrivefullrevokeauth_clean": "Clean Local Credentials", + "modal_onedrivefullrevokeauth_clean_desc": "Deletes locally saved OneDrive (Full Access) tokens.", + "modal_onedrivefullrevokeauth_clean_button": "Clean", + "modal_onedrivefullrevokeauth_clean_notice": "Local credentials cleared.", + "modal_onedrivefullrevokeauth_clean_fail": "Failed to clear local credentials.", + "settings_googledrive": "Google Drive", + "settings_googledrive_folder": "Base folder in Google Drive: /{{remoteBaseDir}}", + "settings_googledrive_auth": "Authorize Google Drive", + "settings_googledrive_auth_desc": "", + "settings_googledrive_auth_button": "Authorize", + "settings_googledrive_revoke": "Revoke Authorization", + "settings_googledrive_revoke_desc": "", + "settings_googledrive_revoke_button": "Revoke", + "settings_googledrive_connect_succ": "Google Drive Check: Success!", + "settings_googledrive_connect_fail": "Google Drive Check: Failed!", + "modal_googledriveauth_tutorial": "Step 1: Click the link or copy the URL and open in a browser.\nStep 2: Sign in and authorize the app — Google will display a code.\nStep 3: Copy that code, paste it below, and click Submit.", + "modal_googledriveauth_copybutton": "Copy Auth URL", + "modal_googledriveauth_copynotice": "Auth URL copied to clipboard.", + "modal_googledriveauth_codeinput": "Authorization Code", + "modal_googledriveauth_codeinput_desc": "Paste the code from the browser here, then click Submit.", + "modal_googledriveauth_codeinput_placeholder": "Paste code here", + "modal_googledriveauth_codeinput_confirm": "Submit", + "modal_googledriveauth_codeinput_notice": "Connecting to Google Drive...", + "modal_googledriveauth_codeinput_conn_succ": "Google Drive connected successfully!", + "modal_googledriveauth_codeinput_conn_fail": "Failed to connect to Google Drive.", + "modal_googledriverevokeauth_step1": "To fully revoke access, visit your Google account permissions and remove this app.", + "modal_googledriverevokeauth_step2": "Click below to clear locally saved credentials.", + "modal_googledriverevokeauth_clean": "Clean Local Credentials", + "modal_googledriverevokeauth_clean_desc": "Deletes locally saved Google Drive tokens.", + "modal_googledriverevokeauth_clean_button": "Clean", + "modal_googledriverevokeauth_clean_notice": "Local credentials cleared.", + "modal_googledriverevokeauth_clean_fail": "Failed to clear local credentials.", + "settings_azureblobstorage": "Azure Blob Storage", + "settings_azureblobstorage_disclaimer1": "Disclaimer: This app is NOT an official Microsoft / Azure product. The SAS URL and other info are stored locally.", + "settings_azureblobstorage_sasurl": "Container SAS URL", + "settings_azureblobstorage_sasurl_desc": "The full SAS URL for your Azure Blob Storage container (starts with https://). Generate it in Azure Portal under your container \u003e Shared Access Signature.", + "settings_azureblobstorage_prefix": "Remote Prefix", + "settings_azureblobstorage_prefix_desc": "Prefix for all objects in the container. Leave blank to use the vault name as the prefix.", + "settings_azureblobstorage_generatefolderobject": "Generate Folder Objects", + "settings_azureblobstorage_generatefolderobject_desc": "Upload zero-byte objects ending with \u0027/\u0027 to represent folders. Disable for cleaner storage.", + "settings_azureblobstorage_generatefolderobject_generate": "Generate", + "settings_azureblobstorage_generatefolderobject_notgenerate": "Not generate (default)", + "settings_azureblobstorage_parts": "Parts Concurrency", + "settings_azureblobstorage_parts_desc": "How many parts to upload in parallel at most? Default is 5.", + "settings_azureblobstorage_connect_succ": "Azure Blob Storage Check: Success!", + "settings_azureblobstorage_connect_fail": "Azure Blob Storage Check: Failed!", + "settings_s3_disclaimer": "This plugin is not an official Amazon product. Credentials, including your secret access key, are stored locally in plain text. Please protect your device.", + "settings_dropbox_disclaimer": "This plugin is not an official Dropbox product. Credentials are stored locally in plain text. You can revoke access at dropbox.com/account/connected_apps.", + "settings_onedrive_disclaimer": "This plugin is not an official Microsoft product. Credentials are stored locally in plain text. You can revoke access at microsoft.com/consent.", + "settings_webdav_disclaimer": "Credentials are stored locally in plain text. Please protect your device.", + "settings_webdis_disclaimer": "This plugin is not an official Redis product. Credentials are stored locally in plain text. Please protect your device.", + "settings_azureblobstorage_disclaimer": "This plugin is not an official Microsoft product. The SAS URL and connection strings are stored locally in plain text.", + "settings_s3_endpoint_desc": "", + "settings_s3_bucketname_desc": "" +} diff --git a/src/langs/index.ts b/src/langs/index.ts new file mode 100644 index 0000000..5c091b4 --- /dev/null +++ b/src/langs/index.ts @@ -0,0 +1,9 @@ +import en from "./en.json"; +import zh_cn from "./zh_cn.json"; +import zh_tw from "./zh_tw.json"; + +export const LANGS = { + en: en, + zh_cn: zh_cn, + zh_tw: zh_tw, +}; diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json new file mode 100644 index 0000000..b6c769f --- /dev/null +++ b/src/langs/zh_cn.json @@ -0,0 +1,397 @@ +{ + "confirm": "确认", + "disable": "关闭", + "enable": "开启", + "goback": "返回", + "submit": "提交", + "sometext": "这里有一段文字。", + "syncrun_alreadyrunning": "{{pluginName}} 正处于此阶段:{{syncStatus}}!中断触发 {{newTriggerSource}}。", + "syncrun_syncingribbon": "{{pluginName}}:正在由 {{triggerSource}} 触发运行", + "syncrun_step0": "0/8 BYOC 在空跑(dry run)模式,不会发生实际的文件交换。", + "syncrun_step1": "1/8 BYOC 准备同步({{serviceType}})", + "syncrun_step2": "2/8 正在获取远端的元数据和检查密码。", + "syncrun_passworderr": "检查密码时候出错。", + "syncrun_step3": "3/8 正在处理远端的元数据。", + "syncrun_step4": "4/8 正在获取本地的元数据。", + "syncrun_step5": "5/8 正在获取本地上一次同步的元数据。", + "syncrun_step6": "6/8 正在生成同步计划。", + "syncrun_step7": "7/8 BYOC 开始发生数据交换!", + "syncrun_step7skip": "7/8 BYOC 在空跑模式,跳过实际数据交换步骤。", + "syncrun_step8": "8/8 BYOC 已完成同步!", + "syncrun_shortstep0": "0/2 BYOC 在空跑(dry run)模式,不会发生实际的文件交换。", + "syncrun_shortstep1": "1/2 BYOC 开始同步({{serviceType}})", + "syncrun_shortstep2skip": "2/2 BYOC 在空跑模式,跳过实际数据交换步骤。", + "syncrun_shortstep2": "2/2 BYOC 已完成同步!", + "syncrun_abort": "{{manifestID}}-{{theDate}}:中断同步,同步来源={{triggerSource}},出错阶段={{syncStatus}}", + "syncrun_abort_protectmodifypercentage": "中断同步!您设置了不允许 >= {{protectModifyPercentage}}% 的变更,但是现在 {{realModifyDeleteCount}}/{{allFilesCount}}={{percent}}% 的文件会被修改或删除!如果您确认这次同步是您想要的,那么请在设置里修改允许比例。", + "protocol_saveqr": " {{manifestName}} 的新设置导入完成。请重启插件设置页使之生效。", + "protocol_callbacknotsupported": "您的 uri callback 暂不支持: {{params}}", + "protocol_dropbox_connecting": "正在连接 Dropbox……\n请不要关闭此弹窗。", + "protocol_dropbox_connect_succ": "好!我们作为用户 {{username}} 连接上了 Dropbox!", + "protocol_dropbox_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", + "protocol_dropbox_connect_fail": "Dropbox 的回调请求有点异常。您是否网络不顺畅?您是否拒绝了鉴权?", + "protocol_dropbox_connect_unknown": "不知道如何处理此 callback:{{params}}", + "protocol_dropbox_no_modal": "您不是在设置页的弹窗里触发 Dropbox 的。中止操作。", + "protocol_onedrive_connecting": "正在连接 OneDrive……\n请不要关闭此弹窗。", + "protocol_onedrive_connect_succ_revoke": "您已作为用户 {{username}} 连接上了。如果您想取消连接,请点击此按钮。", + "protocol_onedrive_connect_fail": "OneDrive 的回调请求有点异常。您是否拒绝了鉴权?", + "protocol_onedrive_connect_unknown": "不知道如何处理此 callback:{{params}}", + "command_startsync": "开始同步(start sync)", + "command_drynrun": "开始同步(空跑模式)(start sync (dry run only))", + "command_exportsyncplans_json": "导出同步计划为 json 格式(export sync plans in json format)", + "command_exportsyncplans_1_only_change": "导出同步计划(最近 1 次)(仅修改部分)(export sync plans (latest 1) (change part))", + "command_exportsyncplans_1": "导出同步计划(最近 1 次)(export sync plans (latest 1))", + "command_exportsyncplans_5": "导出同步计划(最近 5 次)(export sync plans (latest 5))", + "command_exportsyncplans_all": "导出同步计划(所有)(export sync plans (all))", + "command_exportlogsindb": "从数据库导出终端日志(export logs saved in db)", + "statusbar_sync_source_manual": "手动:", + "statusbar_sync_source_dry": "空跑:", + "statusbar_sync_source_auto": "自动:", + "statusbar_sync_source_auto_once_init": "启动时同步:", + "statusbar_sync_source_auto_sync_on_save": "保存时同步:", + "statusbar_sync_status_prefix_success": "同步成功:", + "statusbar_sync_status_prefix_failed": "同步失败:", + "statusbar_time_years": "{{time}} 年前", + "statusbar_time_months": "{{time}} 月前", + "statusbar_time_weeks": "{{time}} 周前", + "statusbar_time_days": "{{time}} 天前", + "statusbar_time_hours": "{{time}} 小时前", + "statusbar_time_minutes": "{{time}} 分钟前", + "statusbar_time_lessminute": "1 分钟内", + "statusbar_time_now": "刚刚", + "statusbar_syncing": "正在同步", + "statusbar_lastsync_label": "日期:{{date}}", + "statusbar_lastsync_never": "没触发过同步", + "statusbar_lastsync_never_label": "没触发过同步", + "modal_password_title": "稍等一下,请阅读下文:", + "modal_password_shortdesc": "如果密码不是空的,那么文件会在上传之前,在本地先用此密码加密。\n如果密码是空的,那么文件会被非加密地上传。", + "modal_password_attn1": "注意 1/5:库(Vault)名字是不会加密的!本插件会在一些远程存储里创建一个和库名字有着同名的文件夹。", + "modal_password_attn2": "注意 2/5:密码在本地不加密地保存!", + "modal_password_attn3": "注意 3/5:一些元信息是没有被加密的,或者可以被较容易地猜测出来。(文件大小很接近于没加密的时候,且文件夹可能会被存储为大小为 0 byte 的对象。)", + "modal_password_attn4": "注意 4/5:您需要确保远程存储是“空”的,或者已有文件也是被同一个新密码加密的;从而避免冲突。", + "modal_password_attn5": "注意 5/5:密码越长越好。", + "modal_password_secondconfirm": "再次确认保存新密码", + "modal_password_notice": "新密码已保存!", + "modal_encryptionmethod_title": "稍等一下,请阅读下文:", + "modal_encryptionmethod_shortdesc": "您正在修改加密方式,但是您已经设置了密码。\n修改加密方式之后,您需要手动完全删除在远端的之前加密过的库文件,然后重新同步(从而重新上传)新的加密文件。", + "modal_remotebasedir_title": "您正在修改远端基文件夹设置", + "modal_remotebasedir_shortdesc": "1. 本插件并不会自动在远端把内容从旧文件夹移动到新文件夹。所有内容都会重新同步。\n2. 如果你使得文本输入框为空,那么本设置会被重设回库的文件夹名(默认设置)。\n3. 即使您设置了端对端加密的密码,远端文件夹名称本身也不会被加密。\n4. 某些特殊字符,如“?”、“/”、“\\”是不允许的。文本前后的空格也会被自动删去。", + "modal_remotebasedir_invaliddirhint": "您所输入的内容含有某些特殊字符,如“?”、“/”、“\\”,它们是不允许的。", + "modal_remotebasedir_secondconfirm_vaultname": "重设回默认的库文件夹名", + "modal_remotebasedir_secondconfirm_change": "确认修改", + "modal_remotebasedir_notice": "新的远端基文件夹设置已保存!", + "modal_remoteprefix_s3_title": "您正在修改远端路径前缀设置", + "modal_remoteprefix_s3_shortdesc": "1. 本插件并不会自动在远端把内容从旧文件夹移动到新文件夹。所有内容都会重新同步。\n2. 如果你使得文本输入框为空,那么本设置为保存为空,文件将会被存储在桶(Bucket)的根目录。\n3. 即使您设置了端对端加密的密码,远端文件夹名称本身也不会被加密。\n4. 某些特殊字符,如“?”、“/”、“\\”是不允许的。文本前后的空格也会被自动删去。", + "modal_remoteprefix_s3_invaliddirhint": "您所输入的内容含有某些特殊字符,如“?”、“/”、“\\”,它们是不允许的。", + "modal_remoteprefix_s3_tosave": "您设定的新前缀为:“{{{prefix}}}”", + "modal_remoteprefix_s3_secondconfirm_empty": "前缀为空,文件会保存在根目录", + "modal_remoteprefix_s3_secondconfirm_change": "确认修改", + "modal_remoteprefix_s3_notice": "新的远端路径前缀设置已保存!", + "modal_dropboxauth_manualsteps": "第 1 步:在浏览器中访问以下地址,然后按照网页提示操作。\n到了最后,您应该会获得一串很长的代码文本,请复制粘贴到下方,并点击“提交”", + "modal_dropboxauth_autosteps": "在浏览器中访问以下地址,然后按照网页提示操作。\n到了最后,您应该会被自动重定向回来 Obsidian。", + "modal_dropboxauth_copybutton": "点击此按钮从而复制鉴权 url", + "modal_dropboxauth_copynotice": "鉴权 url 已复制到剪贴板!", + "modal_dropboxauth_maualinput": "来自网页的授权码", + "modal_dropboxauth_maualinput_desc": "您需要点击“确认”", + "modal_dropboxauth_maualinput_notice": "正在尝试连接 Dropbox", + "modal_dropboxauth_maualinput_conn_succ": "很好!我们作为用户 {{username}} 连接到 Dropbox 了!", + "modal_dropboxauth_maualinput_conn_succ_revoke": "您已作为用户 {{username}} 连接到 Dropbox。如果您想断开连接,点击此按钮。", + "modal_dropboxauth_maualinput_conn_fail": "连接 Dropbox 途中出错了。", + "modal_onedriveauth_shortdesc": "现在只支持个人版 OneDrive,(暂)不支持企业版。\n在浏览器中访问以下地址,然后按照网页提示操作。\n到了最后,您应该会被自动重定向回来 Obsidian。", + "modal_onedriveauth_shortdesc_linux": "您正在用 Linux,有可能无法跳转回来。请考虑使用 flatpack 版本的 Obsidian,或创建 obsidian.desktop 文件。", + "modal_onedriveauth_copybutton": "点击此按钮从而复制鉴权 url", + "modal_onedriveauth_copynotice": "鉴权 url 已复制到剪贴板!", + "modal_onedriverevokeauth_step1": "第 1 步:用浏览器打开以下地址,点击本插件对应的“Edit”按钮,点击“Remove these permissions”按钮。", + "modal_onedriverevokeauth_step2": "第 2 步:点击以下按钮,清理本地保存的登录信息。", + "modal_onedriverevokeauth_clean": "清理本地保存的登录信息", + "modal_onedriverevokeauth_clean_desc": "您需要点击“清理”按钮。", + "modal_onedriverevokeauth_clean_button": "清理", + "modal_onedriverevokeauth_clean_notice": "已清理!", + "modal_onedriverevokeauth_clean_fail": "鉴权途中出错了。", + "modal_syncconfig_attn": "注意 1/2:此设置只同步(复制)整个 Obsidian 的配置文件夹,但是不会同步其它 . 开头的文件夹或文件。除了会忽略 .git 和 node_modules 文件夹之外,它也并不理解配置文件夹的里各个子文件或子文件夹的含义。\n注意 2/2:配置文件夹被同步之后,各插件的设置或许会出错,且 Obsidian 或许需要重启来重载各插件的新配置。\n如果您同意自行承受以上风险,您可以点击以下再次确认按钮。", + "modal_syncconfig_secondconfirm": "再次确认开启", + "modal_syncconfig_notice": "您已开启配置文件夹的同步!", + "modal_qr_shortdesc": "这里可导出(部分)设置。\n您可以使用另一个设备来扫描此 QR 码。\n又或者,您可以点击以下按钮复制此特殊 URI,然后粘贴到另一台设备的网络浏览器或 BYOC 设置里的导入部分。", + "modal_qr_button": "点击此按钮复制特殊 URI", + "modal_qr_button_notice": "特殊 URI 已被复制到剪贴板!", + "modal_sizesconflict_title": "BYOC:跳过大文件的时候出现了一些冲突", + "modal_sizesconflict_desc": "您设置了跳过同步大于 {{thresholdMB}} MB({{thresholdBytes}} bytes)的文件。\n但是以下文件的大小,在一端大于阈值,在另一端则小于阈值。\n为了避免意外的覆盖或删除,插件停止了运作,您需要手动处理至少一端的文件。", + "modal_sizesconflict_copybutton": "点击以复制以下所有文件大小冲突信息", + "modal_sizesconflict_copynotice": "所有的文件大小冲突信息,已被复制到剪贴板!", + "settings_basic": "基本设置", + "settings_password": "密码", + "settings_password_desc": "端到端加密的密码。不填写则代表没密码。您需要点击“确认”来修改。注意:密码和其它信息都会在本地保存。如果您修改了密码,您需要手动删除远端的所有文件,重新同步(从而上传)加密文件。", + "settings_encryptionmethod": "加密方法", + "settings_encryptionmethod_desc": "端到端加密的方法。推荐选用 RClone Crypt 方法,但是它没有加密文件路径结构。OpenSSL enc 是本插件一开始就支持的方式。两种方法都和 RClone、OpenSSL 官方产品和社区无利益相关。如果您修改了加密方法,您需要手动删除远端的所有文件,重新同步(从而上传)加密文件。更多详细说明见在线文档。", + "settings_encryptionmethod_rclone": "RClone Crypt(推荐)", + "settings_encryptionmethod_openssl": "OpenSSL enc(旧方法)", + "settings_autorun": "自动运行", + "settings_autorun_desc": "每隔一段时间,此插件尝试自动同步。会影响到电池用量。", + "settings_autorun_notset": "(不设置)", + "settings_autorun_1min": "每 1 分钟", + "settings_autorun_5min": "每 5 分钟", + "settings_autorun_10min": "每 10 分钟", + "settings_autorun_30min": "每 30 分钟", + "settings_runoncestartup": "启动后自动运行一次", + "settings_runoncestartup_desc": "本设置使得插件在程序启动后自动触发一次同步。修改后,会在下一次启动生效。本设置和“自动运行”是不同的,那个设置是指“每隔一段时间”都自动同步一次。", + "settings_runoncestartup_notset": "(不设置)", + "settings_runoncestartup_1sec": "启动后第 1 秒运行一次", + "settings_runoncestartup_10sec": "启动后第 10 秒运行一次", + "settings_runoncestartup_30sec": "启动后第 30 秒运行一次", + "settings_synconsave": "保存时同步(实验性质)", + "settings_synconsave_desc": "插件如果检查到当前文件在最近一段时间有修改保存过,则尝试同步。请注意,同步是一个很重的操作,因此会影响到耗电量。(修改设置后可能需要重载插件或重启。)", + "settings_synconsave_disable": "关闭(默认)", + "settings_synconsave_enable": "开启", + "settings_skiplargefiles": "跳过大文件", + "settings_skiplargefiles_desc": "跳过大于某一个阈值的文件。这里 1 MB = 10^6 bytes。", + "settings_skiplargefiles_notset": "(不设置)", + "settings_ignorepaths": "忽略的文件或文件夹的正则表达式", + "settings_ignorepaths_desc": "忽略的文件或文件夹的正则表达式。每行一条。路径是相对于库(Vault)根目录的,没有前置 / 符号。非空的忽略列表和非空的允许列表会一起生效。", + "settings_onlyallowpaths": "允许同步的文件或文件夹的正则表达式", + "settings_onlyallowpaths_desc": "允许同步的文件或文件夹的正则表达式。每行一条。路径是相对于库(Vault)根目录的,没有前置 / 符号。如果这个设置非空,那么所有您想同步的路径(包括隐藏的设置文件夹)都要明确地列出到这里。非空的忽略列表和非空的允许列表会一起生效。", + "settings_enablestatusbar_info": "在状态栏显示上一次成功的同步", + "settings_enablestatusbar_info_desc": "在状态栏显示上一次成功的同步的时间。", + "settings_enablestatusbar_reloadrequired_notice": "需要重新载入此插件才生效。", + "settings_resetstatusbar_time": "重设上一次成功同步的时间记录", + "settings_resetstatusbar_time_desc": "重设上一次成功同步的时间记录。", + "settings_resetstatusbar_button": "重设", + "settings_resetstatusbar_notice": "重设完毕!", + "settings_checkonnectivity": "检查可否连接", + "settings_checkonnectivity_desc": "检查可否连接。", + "settings_checkonnectivity_button": "检查", + "settings_checkonnectivity_checking": "正在检查……", + "settings_remotebasedir": "修改远端基文件夹(实验性质)", + "settings_remotebasedir_desc": "默认设定,内容会被同步到远端的和资料库同名的文件夹下。您可以在此修改远端文件夹名,或删除输入框文本从而重设到默认值。您需要点击“确认”。", + "settings_remoteprefix_s3": "修改远端前缀路径(实验性质)", + "settings_remoteprefix_s3_desc": "默认设定 s3 保存在存储桶(Bucket)的根目录。您可以在这里修改路径前缀,或者保持为空保持默认设置。您需要点击“确认”。", + "settings_s3": "S3 或兼容 S3 的服务的设置", + "settings_s3_disclaimer1": "声明:本插件不是 Amazon 的官方产品。", + "settings_s3_disclaimer2": "声明:您所输入的信息存储于本地。其它有害的或者出错的插件,是有可能读取到这些信息的。如果您发现了存储桶有不符合预期的访问,请立刻从 AWS(或其它 S3 服务商)删除记录于此的 access key。", + "settings_s3_cors": "您需要设置 CORS,从而允许这些源的请求访问:app://obsidian.md 和 capacitor://localhost 和 http://localhost 且增加 ETag 到暴露 headers 里。", + "settings_s3_prod": "一些可参考的 Amazon S3 官方文档:", + "settings_s3_prod1": "服务地址(Endpoint)和区域(Region)信息", + "settings_s3_prod2": "Access Key ID 和 Secret Access Key 信息", + "settings_s3_prod3": "设置 CORS", + "settings_s3_endpoint": "服务地址(Endpoint)", + "settings_s3_region": "区域(Region)", + "settings_s3_region_desc": "如果您不知道这个值填什么,可以尝试填写:us-east-1。", + "settings_s3_accesskeyid": "Access Key ID", + "settings_s3_accesskeyid_desc": "Access key ID。注意:access key ID 和其它信息都会在本地保存。", + "settings_s3_secretaccesskey": "Secret Access Key", + "settings_s3_secretaccesskey_desc": "Secret access key。注意:secret access key 和其它信息都会在本地保存。", + "settings_s3_bucketname": "存储桶(Bucket)的名字", + "settings_s3_bypasscorslocally": "本地绕过 CORS 问题", + "settings_s3_bypasscorslocally_desc": "对于 Obsidian 新版本(桌面版>=0.13.25 或 iOS>=1.1.1 或 Android>=1.2.1),本插件可以跳过服务器设置 CORS 的步骤。如果您遇到任意问题,可以关闭此设定,并在服务端设置 CORS(允许来自 app://obsidian.md 和 capacitor://localhost 和 http://localhost 的请求且增加 ETag 到暴露 headers 里)。", + "settings_s3_parts": "分块并行度", + "settings_s3_parts_desc": "在 S3 里,大文件会被分块上传。您希望同一时间最多有多少个分块被上传?", + "settings_s3_accuratemtime": "使用准确的文件修改时间", + "settings_s3_accuratemtime_desc": "读取(已上传的)准确的文件修改时间,有助于同步算法更加准确和稳定。但是它也会导致额外的 api 请求、时间、金钱花费。", + "settings_s3_urlstyle": "S3 URL style", + "settings_s3_urlstyle_desc": "是否对 S3 对象强制使用 path style URL(例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。", + "settings_s3_reverse_proxy_no_sign_url": "S3 反向代理(不签名)地址(实验性质)", + "settings_s3_reverse_proxy_no_sign_url_desc": "不会参与到签名的 S3 反向代理地址。如果您有一个反向代理,但是不想修改原始鉴权签名,这里就可以填写。没有 http(s):// 前缀。如果您不知道这是什么,留空即可。", + "settings_s3_generatefolderobject": "是否生成文件夹 Object", + "settings_s3_generatefolderobject_desc": "S3 不存在“真正”的文件夹。如果您设置了“生成”(或用了旧版本),那么插件会上传 0 字节的以“/”结尾的 Object 来代表文件夹。新版本插件会默认跳过生成这种文件夹 Object。", + "settings_s3_generatefolderobject_notgenerate": "不生成(默认)", + "settings_s3_generatefolderobject_generate": "生成", + "settings_s3_connect_succ": "很好!可以访问到对应存储桶。", + "settings_s3_connect_fail": "无法访问到对应存储桶。", + "settings_dropbox": "Dropbox 设置", + "settings_dropbox_disclaimer1": "声明:此插件不是 Dropbox 官方产品。", + "settings_dropbox_disclaimer2": "声明:您所输入的信息存储于本地。其它有害的或者出错的插件,是有可能读取到这些信息的。如果您发现了 Dropbox 有不符合预期的访问,请立刻从 https://www.dropbox.com/account/connected_apps 删除记录于此插件的连接鉴权。", + "settings_dropbox_folder": "我们会在您的 Dropbox 上创建此文件夹并在里面同步:/Apps/{{pluginID}}/{{remoteBaseDir}}。", + "settings_dropbox_revoke": "撤回鉴权", + "settings_dropbox_revoke_desc": "您现在使用了用户名 {{username}} 来连接。如果想取消连接,请点击此按钮。", + "settings_dropbox_revoke_button": "撤回鉴权", + "settings_dropbox_revoke_notice": "鉴权已被撤回!", + "settings_dropbox_revoke_noticeerr": "撤回鉴权出错。", + "settings_dropbox_clearlocal": "清除本地保存的鉴权信息", + "settings_dropbox_clearlocal_desc": "您可以强制清除本地已保存的 Dropbox 的登录鉴权信息,但不向服务器发送撤回鉴权的请求。", + "settings_dropbox_clearlocal_button": "清除", + "settings_dropbox_clearlocal_notice": "已清除!", + "settings_dropbox_auth": "鉴权", + "settings_dropbox_auth_desc": "鉴权。", + "settings_dropbox_auth_button": "鉴权", + "settings_dropbox_connect_succ": "很好!我们可连接上 Dropbox!", + "settings_dropbox_connect_fail": "我们未能连接上 Dropbox。", + "settings_onedrive": "Onedrive(个人版)(App Folder)设置", + "settings_onedrive_disclaimer1": "声明:此插件不是微软或 OneDrive 的官方产品。", + "settings_onedrive_disclaimer2": "声明:您所输入的信息存储于本地。其它有害的或者出错的插件,是有可能读取到这些信息的。如果您发现了 OneDrive 有不符合预期的访问,请立刻从 https://microsoft.com/consent 删除记录于此插件的连接鉴权。", + "settings_onedrive_folder": "我们会在您的 OneDrive 上创建此文件夹并在里面同步:/Apps/{{pluginID}}/{{remoteBaseDir}}。", + "settings_onedrive_nobiz": "现在只支持个人版 OneDrive。(暂时)并不支持企业版 OneDrive。", + "settings_onedrive_revoke": "撤回鉴权", + "settings_onedrive_revoke_desc": "您现在使用了用户名 {{username}} 来连接。如果想取消连接,请点击此按钮。", + "settings_onedrive_revoke_button": "撤回鉴权", + "settings_onedrive_auth": "鉴权", + "settings_onedrive_auth_desc": "鉴权。", + "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 设置。但您正在使用旧版,建议升级。", + "settings_webdav_cors": "您需要设置 CORS,允许来自这些源的请求:app://obsidian.md 和 capacitor://localhost 和 http://localhost。", + "settings_webdav_folder": "我们会在您的服务器上创建此文件夹并在里面同步:/{{remoteBaseDir}}", + "settings_webdav_addr": "服务器地址", + "settings_webdav_addr_desc": "服务器地址。", + "settings_webdav_user": "用户名", + "settings_webdav_user_desc": "用户名。注意:用户名和其它信息都会保存在本地。", + "settings_webdav_password": "密码", + "settings_webdav_password_desc": "密码。注意:密码和其它信息都会保存在本地。", + "settings_webdav_auth": "鉴权类型", + "settings_webdav_auth_desc": "如果不填写密码,本设置会被忽略。", + "settings_webdav_depth": "发送到服务器的 Depth header", + "settings_webdav_depth_desc": "Webdav 服务器会被设为接收 Depth header 为“1”或“Infinity”请求。如果您不清楚如何设置,可以选择 depth='1'。如果那你确认服务器支持 depth='infinity',请选择它,会获得更加好的性能。", + "settings_webdav_depth_1": "只支持 depth='1'", + "settings_webdav_depth_inf": "支持 depth='infinity'", + "settings_webdav_customheaders": "发送到服务器的自定义 Header", + "settings_webdav_customheaders_desc": "有些用户需要设置自定义 header。如果您不知道是什么,可以直接留空不填。输入格式:每行形如 X-Header1:value1。header 的 key 和 value 之间的空格会被删除。", + "settings_webdav_connect_succ": "很好!可以连接上 Webdav 服务器。", + "settings_webdav_connect_fail": "无法连接上 Webdav 服务器。(可能是地址/账号/密码/鉴权类型等错误。)", + "settings_webdav_connect_fail_withcors": "无法连接上 Webdav 服务器。(可能是地址/账号/密码/鉴权类型/CORS 等错误。)", + "settings_webdis": "Webdis 设置", + "settings_webdis_disclaimer1": "声明:此插件不是 Redis® Ltd 或 Redis® 软件或 Wedis 的官方产品。Redis 是 Redis Ltd 的注册商标。", + "settings_webdis_disclaimer2": "声明:您所输入的信息存储于本地。其它有害的或者出错的插件,是有可能读取到这些信息的。如果您发现了 Webdis 服务器有不符合预期的访问,请立刻修改用户名和密码。", + "settings_webdis_folder": "我们会在您的服务器上创建带有此前缀的 key 并在里面同步::{{remoteBaseDir}}。", + "settings_webdis_addr": "服务器地址", + "settings_webdis_addr_desc": "服务器地址", + "settings_webdis_user": "用户名", + "settings_webdis_user_desc": "用户名。注意:用户名和其它信息都会保存在本地。", + "settings_webdis_password": "密码", + "settings_webdis_password_desc": "密码。注意:密码和其它信息都会保存在本地。", + "settings_webdis_connect_succ": "很好!可以连接上 Webdis 服务器。", + "settings_webdis_connect_fail": "无法连接上 Webdis 服务器。(可能是地址/账号/密码/鉴权类型等错误。)", + "settings_chooseservice": "选择远程服务", + "settings_chooseservice_desc": "从这里开始设置。您想连接到哪一个服务?S3、Dropbox、Webdav、OneDrive(个人版)、Webdis?", + "settings_chooseservice_s3": "S3 或兼容 S3 的服务", + "settings_chooseservice_dropbox": "Dropbox", + "settings_chooseservice_webdav": "Webdav", + "settings_chooseservice_onedrive": "OneDrive(个人版)", + "settings_chooseservice_webdis": "Webdis (an HTTP interface for Redis)", + "settings_adv": "进阶设置", + "settings_concurrency": "并行度", + "settings_concurrency_desc": "您希望同时最多有多少个文件被上传和下载?默认值是 5。如果您遇到了一些问题(如访问频率限制),您可以减少并行度。", + "settings_syncunderscore": "同步 _ 文件或文件夹", + "settings_syncunderscore_desc": "是否同步“_”(下划线)开始的文件或文件夹。", + "settings_configdir": "同步配置文件夹(实验性质)", + "settings_configdir_desc": "是否同步 Obsidian 的配置文件夹 {{configDir}}(其中的 .git 和 node_modules 依然会被忽略)。请注意,此设置可能会影响到您 Obsidian 或者所有的插件设置,也可能需要您同步完毕后重启 Obsidian。如果您同意自行承受其风险,则可以开启本设置。", + "settings_bookmarks": "同步书签(实验性质)", + "settings_bookmarks_desc": "是否同步书签 {{configDir}}/bookmarks.json 。如果您设置了同步配置文件夹,此选项会被忽略并设为“开启”。", + "settings_deletetowhere": "删除文件到哪里", + "settings_deletetowhere_desc": "插件触发删除操作时候,删除到哪里?", + "settings_deletetowhere_system_trash": "系统回收站(默认)", + "settings_deletetowhere_obsidian_trash": "Obsidian .trash 文件夹", + "settings_conflictaction": "处理冲突", + "settings_conflictaction_desc": "如果一个文件,在本地和服务器都被创建或者修改了,那么这就是一个“冲突”情况。如何处理?这个设置只在双向同步时候生效。", + "settings_conflictaction_keep_newer": "保留最后修改的版本(默认)", + "settings_conflictaction_keep_larger": "保留文件体积较大的版本", + "settings_cleanemptyfolder": "处理空文件夹", + "settings_cleanemptyfolder_desc": "同步算法主要是针对文件处理的,您需要手动指定空文件夹如何处理。", + "settings_cleanemptyfolder_skip": "跳过处理空文件夹(默认)", + "settings_cleanemptyfolder_clean_both": "删除本地和服务器的空文件夹", + "settings_protectmodifypercentage": "如果修改超过百分比则中止同步", + "settings_protectmodifypercentage_desc": "如果算法检测到超过 n% 的文件会被修改或删除,则中止同步。从而可以保护用户的文件免受预料之外的修改。您可以设置为 100 而去除此保护,也可以设置为 0 总是强制中止所有同步。", + "settings_protectmodifypercentage_000_desc": "0(总是强制中止)", + "settings_protectmodifypercentage_050_desc": "50(默认值)", + "settings_protectmodifypercentage_100_desc": "100(去除此保护)", + "settings_protectmodifypercentage_custom_desc": "自定义", + "settings_protectmodifypercentage_customfield": "如果修改超过自定义百分比则中止同步", + "settings_protectmodifypercentage_customfield_desc": "您需要输入 0(含)~ 100(含)的数字。小数也是可以的。", + "setting_syncdirection": "同步方向(实验性质)", + "setting_syncdirection_desc": "

W插件应该向哪里同步?注意每个选项都是只有修改了的文件(基于修改时间和大小判断)才会触发同步动作。

  1. 双向同步:一边的变动会同步到另一边。
  2. 增量推送:本地创建或修改的文件会复制到远端。
  3. 增量拉取:远端创建或修改的文件会复制到本地。
  4. 增量推送带删除:本地创建或修改的文件会复制到远端,本地的删除也会在远端运行。
  5. 增量拉取带删除:远端创建或修改的文件会复制到本地,远端的删除也会在本地运行。
", + "setting_syncdirection_bidirectional_desc": "双向同步(默认)", + "setting_syncdirection_incremental_push_only_desc": "增量推送(也即:备份模式)", + "setting_syncdirection_incremental_pull_only_desc": "增量拉取", + "setting_syncdirection_incremental_push_and_delete_only_desc": "增量推送带删除", + "setting_syncdirection_incremental_pull_and_delete_only_desc": "增量拉取带删除", + "settings_enablemobilestatusbar": "手机的状态栏(实验性质)", + "settings_enablemobilestatusbar_desc": "Obsidian 手机版默认隐藏了状态栏。有些用户希望展示它。这里提供了设置选项。", + "settings_importexport": "导入导出部分设置", + "settings_export": "导出", + "settings_export_desc": "用 QR 码或 URI 导出设置信息。", + "settings_export_basic_and_advanced_button": "导出基本或进阶设置", + "settings_export_s3_button": "导出 S3 部分", + "settings_export_dropbox_button": "导出 Dropbox 部分", + "settings_export_onedrive_button": "导出 OneDrive (App Folder) 部分", + "settings_export_webdav_button": "导出 Webdav 部分", + "settings_export_webdis_button": "导出 Webdis 部分", + "settings_import": "导入", + "settings_import_desc": "粘贴之前导出的 URI 到这里然后点击“导入”。或,使用拍摄 app 或者扫描 QR 码的 app,来扫描对应的 QR 码。", + "settings_import_button": "导入", + "settings_import_error_notice": "您输入的 URI 是空的或者不准确的!", + "settings_debug": "调试", + "settings_debuglevel": "修改同步提示信息", + "settings_debuglevel_desc": "默认值为 \"info\"。您可以改为 \"debug\" 从而在同步时候里获取更多信息。", + "settings_outputsettingsconsole": "读取硬盘上的设置文件输出到终端", + "settings_outputsettingsconsole_desc": "硬盘上的设置文件是编码过的,点击这里从而解码并输出到终端。", + "settings_outputsettingsconsole_button": "输出", + "settings_outputsettingsconsole_notice": "已输出到终端", + "settings_obfuscatesettingfile": "是否混淆保存设置文件", + "settings_obfuscatesettingfile_desc": "设置文件(data.json)含有敏感信息。强烈建议混淆后保存它,从而避免出乎意料的读取和修改。如果您确认要手动查看和修改它,可以关闭混淆保存。", + "settings_viewconsolelog": "查看终端输出", + "settings_viewconsolelog_desc": "电脑上,输入“ctrl+shift+i”或“cmd+shift+i”来查看终端输出。手机上,安装第三方插件 Logstravaganza 来导出终端输出到一篇笔记上。", + "settings_syncplans": "导出同步计划", + "settings_syncplans_desc": "每次您启动同步,并在实际上传下载前,插件会生成同步计划。它可以使您知道每次同步发生了什么。点击按钮可以导出同步计划。", + "settings_syncplans_button_1_only_change": "导出最近 1 次(仅修改部分)", + "settings_syncplans_button_5_only_change": "导出最近 5 次(仅修改部分)", + "settings_syncplans_button_1": "导出最近 1 次", + "settings_syncplans_button_5": "导出最近 5 次", + "settings_syncplans_button_all": "导出所有", + "settings_syncplans_notice": "同步计划已导出", + "settings_delsyncplans": "删除数据库里的同步计划历史", + "settings_delsyncplans_desc": "删除数据库里的同步计划历史。", + "settings_delsyncplans_button": "删除同步计划历史", + "settings_delsyncplans_notice": "(数据库里的)同步计划已被删除。", + "settings_delprevsync": "删除数据库里的上次同步明细", + "settings_delprevsync_desc": "同步算法需要上次成功同步的信息来决定文件变更,这个信息保存在本地的数据库里。如果您想忽略这些信息从而所有文件都被视为新创建的话,可以在此删除之前的信息。", + "settings_delprevsync_button": "删除上次同步明细", + "settings_delprevsync_notice": "(本地数据库里的)上次同步明细已被删除。", + "settings_profiler_results": "导出性能数据记录", + "settings_profiler_results_desc": "插件记录了每次同步每一步的耗时。这里可以导出记录得知哪一步最慢。", + "settings_profiler_results_notice": "性能数据已导出", + "settings_profiler_results_button_all": "导出所有", + "settings_profiler_enableprofiler": "性能收集", + "settings_profiler_enableprofiler_desc": "是否开启性能收集功能?", + "settings_profiler_enabledebugprint": "性能收集输出", + "settings_profiler_enabledebugprint_desc": "是否直接输出性能收集结果到终端里?", + "settings_profiler_recordsize": "性能收集统计对象大小", + "settings_profiler_recordsize_desc": "是否收集对象的大小?", + "settings_outputbasepathvaultid": "输出资料库对应的位置和随机分配的 ID", + "settings_outputbasepathvaultid_desc": "用于调试。", + "settings_outputbasepathvaultid_button": "输出", + "settings_resetcache": "重设本地缓存和数据库", + "settings_resetcache_desc": "(出于调试原因)重设本地缓存和数据库。您需要在重设之后重新载入此插件。本重设不会删除 s3,密码……等设定。", + "settings_resetcache_button": "重设", + "settings_resetcache_notice": "本地同步缓存和数据库已被删除。请手动重新载入此插件。", + "syncalgov3_title": "BYOC 的同步算法有重大更新", + "syncalgov3_texts": "欢迎使用 BYOC!\n从这个版本开始,插件更新了同步算法:\n
  • 更稳健的删除同步
  • 引入冲突处理
  • 避免上传元数据
  • 修改删除保护
  • 备份模式
  • 新的加密方式
  • ……
\n敬请期待更多更新!详细介绍请参阅文档网站。\n如果您同意使用新版本,请阅读和勾选两个勾选框,然后点击“同意”按钮,开始使用插件吧!\n如果您不同意,请点击“不同意”按钮,插件将自动停止运行(unload)。\n此外,请考虑访问 GitHub 页面然后点赞 ⭐!您的支持对我十分重要!谢谢!", + "syncalgov3_checkbox_manual_backup": "我将会首先手动备份我的库(Vault)。", + "syncalgov3_checkbox_requiremultidevupdate": "我理解,我需要在所有设备上都更新此插件使之正常运行。", + "syncalgov3_button_agree": "同意", + "syncalgov3_button_disagree": "不同意", + "menu_check_file_stat": "查看文件属性", + "settings_pcloud": "pCloud", + "settings_pcloud_folder": "Base folder in pCloud: /{{remoteBaseDir}}", + "settings_pcloud_auth": "Authorize", + "settings_pcloud_auth_desc": "Authorize this plugin to access your pCloud account.", + "settings_pcloud_auth_button": "Authorize", + "settings_pcloud_revoke": "Revoke Authorization", + "settings_pcloud_revoke_desc": "Revoke authorization if you want to switch accounts or reset your connection.", + "settings_pcloud_revoke_button": "Revoke", + "modal_pcloudauth_tutorial": "Step 1: Click the link below to securely authorize obsidian-byoc. Step 2: The app will automatically capture the token.", + "modal_pcloudauth_copybutton": "Copy Link", + "modal_pcloudauth_copynotice": "Copied to clipboard.", + "modal_pcloudrevokeauth_step1": "To fully revoke access, visit pCloud Apps and remove obsidian-byoc.", + "modal_pcloudrevokeauth_step2": "Click clean to wipe tokens locally.", + "modal_pcloudrevokeauth_clean": "Clean up local records", + "modal_pcloudrevokeauth_clean_desc": "Deletes local pCloud access tokens.", + "modal_pcloudrevokeauth_clean_button": "Clean", + "modal_pcloudrevokeauth_clean_notice": "Local records cleaned.", + "modal_pcloudrevokeauth_clean_fail": "Failed to clean local records.", + "settings_pcloud_connect_succ": "pCloud Check: Success!", + "settings_pcloud_connect_fail": "pCloud Check: Failed!" +} \ No newline at end of file diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json new file mode 100644 index 0000000..d4f0801 --- /dev/null +++ b/src/langs/zh_tw.json @@ -0,0 +1,396 @@ +{ + "confirm": "確認", + "disable": "關閉", + "enable": "開啟", + "goback": "返回", + "submit": "提交", + "sometext": "這裡有一段文字。", + "syncrun_alreadyrunning": "{{pluginName}} 正處於此階段:{{syncStatus}}! 中斷觸發 {{newTriggerSource}}。", + "syncrun_syncingribbon": "{{pluginName}}:正在由 {{triggerSource}} 觸發執行", + "syncrun_step0": "0/8 BYOC 在空跑(dry run)模式,不會發生實際的檔案交換。", + "syncrun_step1": "1/8 BYOC 準備同步({{serviceType}})", + "syncrun_step2": "2/8 正在獲取遠端的元數據和檢查密碼。", + "syncrun_passworderr": "檢查密碼時候出錯。", + "syncrun_step3": "3/8 正在處理遠端的元數據。", + "syncrun_step4": "4/8 正在獲取本地的元資料。", + "syncrun_step5": "5/8 正在獲取本地上一次同步的元資料。", + "syncrun_step6": "6/8 正在生成同步計劃。", + "syncrun_step7": "7/8 BYOC 開始發生資料交換!", + "syncrun_step7skip": "7/8 BYOC 在空跑模式,跳過實際資料交換步驟。", + "syncrun_step8": "8/8 BYOC 已完成同步!", + "syncrun_shortstep0": "0/2 BYOC 在空跑(dry run)模式,不會發生實際的檔案交換。", + "syncrun_shortstep1": "1/2 BYOC 開始同步({{serviceType}})", + "syncrun_shortstep2skip": "2/2 BYOC 在空跑模式,跳過實際資料交換步驟。", + "syncrun_shortstep2": "2/2 BYOC 已完成同步!", + "syncrun_abort": "{{manifestID}}-{{theDate}}:中斷同步,同步來源={{triggerSource}},出錯階段={{syncStatus}}", + "syncrun_abort_protectmodifypercentage": "中斷同步!您設定了不允許 >= {{protectModifyPercentage}}% 的變更,但是現在 {{realModifyDeleteCount}}/{{allFilesCount}}={{percent}}% 的檔案會被修改或刪除!如果您確認這次同步是您想要的,那麼請在設定裡修改允許比例。", + "protocol_saveqr": " {{manifestName}} 的新設定匯入完成。請重啟外掛設定頁使之生效。", + "protocol_callbacknotsupported": "您的 uri callback 暫不支援: {{params}}", + "protocol_dropbox_connecting": "正在連線 Dropbox……\n請不要關閉此彈窗。", + "protocol_dropbox_connect_succ": "好!我們作為使用者 {{username}} 連線上了 Dropbox!", + "protocol_dropbox_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", + "protocol_dropbox_connect_fail": "Dropbox 的回撥請求有點異常。您是否網路不順暢?您是否拒絕了鑑權?", + "protocol_dropbox_connect_unknown": "不知道如何處理此 callback:{{params}}", + "protocol_dropbox_no_modal": "您不是在設定頁的彈窗裡觸發 Dropbox 的。中止操作。", + "protocol_onedrive_connecting": "正在連線 OneDrive……\n請不要關閉此彈窗。", + "protocol_onedrive_connect_succ_revoke": "您已作為使用者 {{username}} 連線上了。如果您想取消連線,請點選此按鈕。", + "protocol_onedrive_connect_fail": "OneDrive 的回撥請求有點異常。您是否拒絕了鑑權?", + "protocol_onedrive_connect_unknown": "不知道如何處理此 callback:{{params}}", + "command_startsync": "開始同步(start sync)", + "command_drynrun": "開始同步(空跑模式)(start sync (dry run only))", + "command_exportsyncplans_1_only_change": "匯出同步計劃(最近 1 次)(僅修改部分)(export sync plans (latest 1) (change part))", + "command_exportsyncplans_1": "匯出同步計劃(最近 1 次)(export sync plans (latest 1))", + "command_exportsyncplans_5": "匯出同步計劃(最近 5 次)(export sync plans (latest 5))", + "command_exportsyncplans_all": "匯出同步計劃(所有)(export sync plans (all))", + "command_exportlogsindb": "從資料庫匯出終端日誌(export logs saved in db)", + "statusbar_sync_source_manual": "手動:", + "statusbar_sync_source_dry": "空跑:", + "statusbar_sync_source_auto": "自動:", + "statusbar_sync_source_auto_once_init": "啟動時同步:", + "statusbar_sync_source_auto_sync_on_save": "儲存時同步:", + "statusbar_sync_status_prefix_success": "同步成功:", + "statusbar_sync_status_prefix_failed": "同步失敗:", + "statusbar_time_years": "{{time}} 年前", + "statusbar_time_months": "{{time}} 月前", + "statusbar_time_weeks": "{{time}} 周前", + "statusbar_time_days": "{{time}} 天前", + "statusbar_time_hours": "{{time}} 小時前", + "statusbar_time_minutes": "{{time}} 分鐘前", + "statusbar_time_lessminute": "1 分鐘內", + "statusbar_time_now": "剛剛", + "statusbar_syncing": "正在同步", + "statusbar_lastsync_label": "日期:{{date}}", + "statusbar_lastsync_never": "沒觸發過同步", + "statusbar_lastsync_never_label": "沒觸發過同步", + "modal_password_title": "稍等一下,請閱讀下文:", + "modal_password_shortdesc": "如果密碼不是空的,那麼檔案會在上傳之前,在本地先用此密碼加密。\n如果密碼是空的,那麼檔案會被非加密地上傳。", + "modal_password_attn1": "注意 1/5:儲存庫(Vault)名字是不會加密的!本外掛會在一些遠端儲存裡建立一個和庫名字有著同名的資料夾。", + "modal_password_attn2": "注意 2/5:密碼在本地不加密地儲存!", + "modal_password_attn3": "注意 3/5:一些元資訊是沒有被加密的,或者可以被較容易地猜測出來。(檔案大小很接近於沒加密的時候,且資料夾可能會被儲存為大小為 0 byte 的物件。)", + "modal_password_attn4": "注意 4/5:您需要確保遠端儲存是“空”的,或者已有檔案也是被同一個新密碼加密的;從而避免衝突。", + "modal_password_attn5": "注意 5/5:密碼越長越好。", + "modal_password_secondconfirm": "再次確認儲存新密碼", + "modal_password_notice": "新密碼已儲存!", + "modal_encryptionmethod_title": "稍等一下,請閱讀下文:", + "modal_encryptionmethod_shortdesc": "您正在修改加密方式,但是您已經設定了密碼。\n修改加密方式之後,您需要手動完全刪除在遠端的之前加密過的庫檔案,然後重新同步(從而重新上傳)新的加密檔案。", + "modal_remotebasedir_title": "您正在修改遠端基資料夾設定", + "modal_remotebasedir_shortdesc": "1. 本外掛並不會自動在遠端把內容從舊資料夾移動到新資料夾。所有內容都會重新同步。\n2. 如果你使得文字輸入框為空,那麼本設定會被重設回庫的資料夾名(預設設定)。\n3. 即使您設定了端對端加密的密碼,遠端資料夾名稱本身也不會被加密。\n4. 某些特殊字元,如“?”、“/”、“\\”是不允許的。文字前後的空格也會被自動刪去。", + "modal_remotebasedir_invaliddirhint": "您所輸入的內容含有某些特殊字元,如“?”、“/”、“\\”,它們是不允許的。", + "modal_remotebasedir_secondconfirm_vaultname": "重設回預設的庫資料夾名", + "modal_remotebasedir_secondconfirm_change": "確認修改", + "modal_remotebasedir_notice": "新的遠端基資料夾設定已儲存!", + "modal_remoteprefix_s3_title": "您正在修改遠端路徑字首設定", + "modal_remoteprefix_s3_shortdesc": "1. 本外掛並不會自動在遠端把內容從舊資料夾移動到新資料夾。所有內容都會重新同步。\n2. 如果你使得文字輸入框為空,那麼本設定為儲存為空,檔案將會被儲存在桶(Bucket)的根目錄。\n3. 即使您設定了端對端加密的密碼,遠端資料夾名稱本身也不會被加密。\n4. 某些特殊字元,如“?”、“/”、“\\”是不允許的。文字前後的空格也會被自動刪去。", + "modal_remoteprefix_s3_invaliddirhint": "您所輸入的內容含有某些特殊字元,如“?”、“/”、“\\”,它們是不允許的。", + "modal_remoteprefix_s3_tosave": "您設定的新字首為:“{{{prefix}}}”", + "modal_remoteprefix_s3_secondconfirm_empty": "字首為空,檔案會儲存在根目錄", + "modal_remoteprefix_s3_secondconfirm_change": "確認修改", + "modal_remoteprefix_s3_notice": "新的遠端路徑字首設定已儲存!", + "modal_dropboxauth_manualsteps": "第 1 步:在瀏覽器中訪問以下地址,然後按照網頁提示操作。\n到了最後,您應該會獲得一串很長的程式碼文字,請複製貼上到下方,並點選“提交”", + "modal_dropboxauth_autosteps": "在瀏覽器中訪問以下地址,然後按照網頁提示操作。\n到了最後,您應該會被自動重定向回來 Obsidian。", + "modal_dropboxauth_copybutton": "點選此按鈕從而複製鑑權 url", + "modal_dropboxauth_copynotice": "鑑權 url 已複製到剪貼簿!", + "modal_dropboxauth_maualinput": "來自網頁的授權碼", + "modal_dropboxauth_maualinput_desc": "您需要點選“確認”", + "modal_dropboxauth_maualinput_notice": "正在嘗試連線 Dropbox", + "modal_dropboxauth_maualinput_conn_succ": "很好!我們作為使用者 {{username}} 連線到 Dropbox 了!", + "modal_dropboxauth_maualinput_conn_succ_revoke": "您已作為使用者 {{username}} 連線到 Dropbox。如果您想斷開連線,點選此按鈕。", + "modal_dropboxauth_maualinput_conn_fail": "連線 Dropbox 途中出錯了。", + "modal_onedriveauth_shortdesc": "現在只支援個人版 OneDrive,(暫)不支援企業版。\n在瀏覽器中訪問以下地址,然後按照網頁提示操作。\n到了最後,您應該會被自動重定向回來 Obsidian。", + "modal_onedriveauth_shortdesc_linux": "您正在用 Linux,有可能無法跳轉回來。請考慮使用 flatpack 版本的 Obsidian,或建立 obsidian.desktop 檔案。", + "modal_onedriveauth_copybutton": "點選此按鈕從而複製鑑權 url", + "modal_onedriveauth_copynotice": "鑑權 url 已複製到剪貼簿!", + "modal_onedriverevokeauth_step1": "第 1 步:用瀏覽器開啟以下地址,點選本外掛對應的“Edit”按鈕,點選“Remove these permissions”按鈕。", + "modal_onedriverevokeauth_step2": "第 2 步:點選以下按鈕,清理本地儲存的登入資訊。", + "modal_onedriverevokeauth_clean": "清理本地儲存的登入資訊", + "modal_onedriverevokeauth_clean_desc": "您需要點選“清理”按鈕。", + "modal_onedriverevokeauth_clean_button": "清理", + "modal_onedriverevokeauth_clean_notice": "已清理!", + "modal_onedriverevokeauth_clean_fail": "鑑權途中出錯了。", + "modal_syncconfig_attn": "注意 1/2:此設定只同步(複製)整個 Obsidian 的配置資料夾,但是不會同步其它 . 開頭的資料夾或檔案。除了會忽略 .git 和 node_modules 資料夾之外,它也並不理解配置資料夾的裡各個子檔案或子資料夾的含義。\n注意 2/2:配置資料夾被同步之後,各外掛的設定或許會出錯,且 Obsidian 或許需要重啟來過載各外掛的新配置。\n如果您同意自行承受以上風險,您可以點選以下再次確認按鈕。", + "modal_syncconfig_secondconfirm": "再次確認開啟", + "modal_syncconfig_notice": "您已開啟配置資料夾的同步!", + "modal_qr_shortdesc": "這裡可匯出(部分)設定。\n您可以使用另一個裝置來掃描此 QR 碼。\n又或者,您可以點選以下按鈕複製此特殊 URI,然後貼上到另一臺裝置的網路瀏覽器或 BYOC 設定裡的匯入部分。", + "modal_qr_button": "點選此按鈕複製特殊 URI", + "modal_qr_button_notice": "特殊 URI 已被複制到剪貼簿!", + "modal_sizesconflict_title": "BYOC:跳過大檔案的時候出現了一些衝突", + "modal_sizesconflict_desc": "您設定了跳過同步大於 {{thresholdMB}} MB({{thresholdBytes}} bytes)的檔案。\n但是以下檔案的大小,在一端大於閾值,在另一端則小於閾值。\n為了避免意外的覆蓋或刪除,外掛停止了運作,您需要手動處理至少一端的檔案。", + "modal_sizesconflict_copybutton": "點選以複製以下所有檔案大小衝突資訊", + "modal_sizesconflict_copynotice": "所有的檔案大小衝突資訊,已被複制到剪貼簿!", + "settings_basic": "基本設定", + "settings_password": "密碼", + "settings_password_desc": "端到端加密的密碼。不填寫則代表沒密碼。您需要點選“確認”來修改。注意:密碼和其它資訊都會在本地儲存。如果您修改了密碼,您需要手動刪除遠端的所有檔案,重新同步(從而上傳)加密檔案。", + "settings_encryptionmethod": "加密方法", + "settings_encryptionmethod_desc": "端到端加密的方法。推薦選用 RClone Crypt 方法,但是它沒有加密檔案路徑結構。OpenSSL enc 是本外掛一開始就支援的方式。兩種方法都和 RClone、OpenSSL 官方產品和社群無利益相關。如果您修改了加密方法,您需要手動刪除遠端的所有檔案,重新同步(從而上傳)加密檔案。更多詳細說明見線上文件。", + "settings_encryptionmethod_rclone": "RClone Crypt(推薦)", + "settings_encryptionmethod_openssl": "OpenSSL enc(舊方法)", + "settings_autorun": "自動執行", + "settings_autorun_desc": "每隔一段時間,此外掛嘗試自動同步。會影響到電池用量。", + "settings_autorun_notset": "(不設定)", + "settings_autorun_1min": "每 1 分鐘", + "settings_autorun_5min": "每 5 分鐘", + "settings_autorun_10min": "每 10 分鐘", + "settings_autorun_30min": "每 30 分鐘", + "settings_runoncestartup": "啟動後自動執行一次", + "settings_runoncestartup_desc": "本設定使得外掛在程式啟動後自動觸發一次同步。修改後,會在下一次啟動生效。本設定和“自動執行”是不同的,那個設定是指“每隔一段時間”都自動同步一次。", + "settings_runoncestartup_notset": "(不設定)", + "settings_runoncestartup_1sec": "啟動後第 1 秒執行一次", + "settings_runoncestartup_10sec": "啟動後第 10 秒執行一次", + "settings_runoncestartup_30sec": "啟動後第 30 秒執行一次", + "settings_synconsave": "儲存時同步(實驗性質)", + "settings_synconsave_desc": "外掛如果檢查到當前檔案在最近一段時間有修改儲存過,則嘗試同步。請注意,同步是一個很重的操作,因此會影響到耗電量。(修改設定後可能需要過載外掛或重啟。)", + "settings_synconsave_disable": "關閉(預設)", + "settings_synconsave_enable": "開啟", + "settings_skiplargefiles": "跳過大檔案", + "settings_skiplargefiles_desc": "跳過大於某一個閾值的檔案。這裡 1 MB = 10^6 bytes。", + "settings_skiplargefiles_notset": "(不設定)", + "settings_ignorepaths": "忽略的檔案或資料夾的正則表示式", + "settings_ignorepaths_desc": "忽略的檔案或資料夾的正則表示式。每行一條。路徑是相對於庫(Vault)根目錄的,沒有前置 / 符號。非空的忽略列表和非空的允許列表會一起生效。", + "settings_onlyallowpaths": "允許同步的檔案或資料夾的正則表示式", + "settings_onlyallowpaths_desc": "允許同步的檔案或資料夾的正則表示式。每行一條。路徑是相對於庫(Vault)根目錄的,沒有前置 / 符號。如果這個設定非空,那麼所有您想同步的路徑(包括隱藏的設定資料夾)都要明確地列出到這裡。非空的忽略列表和非空的允許列表會一起生效。", + "settings_enablestatusbar_info": "在狀態列顯示上一次成功的同步", + "settings_enablestatusbar_info_desc": "在狀態列顯示上一次成功的同步的時間。", + "settings_enablestatusbar_reloadrequired_notice": "需要重新載入此外掛才生效。", + "settings_resetstatusbar_time": "重設上一次成功同步的時間記錄", + "settings_resetstatusbar_time_desc": "重設上一次成功同步的時間記錄。", + "settings_resetstatusbar_button": "重設", + "settings_resetstatusbar_notice": "重設完畢!", + "settings_checkonnectivity": "檢查可否連線", + "settings_checkonnectivity_desc": "檢查可否連線。", + "settings_checkonnectivity_button": "檢查", + "settings_checkonnectivity_checking": "正在檢查……", + "settings_remotebasedir": "修改遠端基資料夾(實驗性質)", + "settings_remotebasedir_desc": "預設設定,內容會被同步到遠端的和資料庫同名的資料夾下。您可以在此修改遠端資料夾名,或刪除輸入框文字從而重設到預設值。您需要點選“確認”。", + "settings_remoteprefix_s3": "修改遠端字首路徑(實驗性質)", + "settings_remoteprefix_s3_desc": "預設設定 s3 儲存在儲存桶(Bucket)的根目錄。您可以在這裡修改路徑字首,或者保持為空保持預設設定。您需要點選“確認”。", + "settings_s3": "S3 或相容 S3 的服務的設定", + "settings_s3_disclaimer1": "宣告:本外掛不是 Amazon 的官方產品。", + "settings_s3_disclaimer2": "宣告:您所輸入的資訊儲存於本地。其它有害的或者出錯的外掛,是有可能讀取到這些資訊的。如果您發現了儲存桶有不符合預期的訪問,請立刻從 AWS(或其它 S3 服務商)刪除記錄於此的 access key。", + "settings_s3_cors": "您需要設定 CORS,從而允許這些源的請求訪問:app://obsidian.md 和 capacitor://localhost 和 http://localhost 且增加 ETag 到暴露 headers 裡。", + "settings_s3_prod": "一些可參考的 Amazon S3 官方文件:", + "settings_s3_prod1": "服務地址(Endpoint)和區域(Region)資訊", + "settings_s3_prod2": "Access Key ID 和 Secret Access Key 資訊", + "settings_s3_prod3": "設定 CORS", + "settings_s3_endpoint": "服務地址(Endpoint)", + "settings_s3_region": "區域(Region)", + "settings_s3_region_desc": "如果您不知道這個值填什麼,可以嘗試填寫:us-east-1。", + "settings_s3_accesskeyid": "Access Key ID", + "settings_s3_accesskeyid_desc": "Access key ID。注意:access key ID 和其它資訊都會在本地儲存。", + "settings_s3_secretaccesskey": "Secret Access Key", + "settings_s3_secretaccesskey_desc": "Secret access key。注意:secret access key 和其它資訊都會在本地儲存。", + "settings_s3_bucketname": "儲存桶(Bucket)的名字", + "settings_s3_bypasscorslocally": "本地繞過 CORS 問題", + "settings_s3_bypasscorslocally_desc": "對於 Obsidian 新版本(桌面版>=0.13.25 或 iOS>=1.1.1 或 Android>=1.2.1),本外掛可以跳過伺服器設定 CORS 的步驟。如果您遇到任意問題,可以關閉此設定,並在服務端設定 CORS(允許來自 app://obsidian.md 和 capacitor://localhost 和 http://localhost 的請求且增加 ETag 到暴露 headers 裡)。", + "settings_s3_parts": "分塊並行度", + "settings_s3_parts_desc": "在 S3 裡,大檔案會被分塊上傳。您希望同一時間最多有多少個分塊被上傳?", + "settings_s3_accuratemtime": "使用準確的檔案修改時間", + "settings_s3_accuratemtime_desc": "讀取(已上傳的)準確的檔案修改時間,有助於同步演算法更加準確和穩定。但是它也會導致額外的 api 請求、時間、金錢花費。", + "settings_s3_urlstyle": "S3 URL style", + "settings_s3_urlstyle_desc": "是否對 S3 物件強制使用 path style URL(例如使用 https://s3.amazonaws.com/*/ 而不是 https://*.s3.amazonaws.com/)。", + "settings_s3_reverse_proxy_no_sign_url": "S3 反向代理(不簽名)地址(實驗性質)", + "settings_s3_reverse_proxy_no_sign_url_desc": "不會參與到簽名的 S3 反向代理地址。如果您有一個反向代理,但是不想修改原始鑑權簽名,這裡就可以填寫。沒有 http(s):// 字首。如果您不知道這是什麼,留空即可。", + "settings_s3_generatefolderobject": "是否生成文件夾 Object", + "settings_s3_generatefolderobject_desc": "S3 不存在“真正”的文件夾。如果您設置了“生成”(或用了舊版本),那麼插件會上傳 0 字節的以“/”結尾的 Object 來代表文件夾。新版本插件會默認跳過生成這種文件夾 Object。", + "settings_s3_generatefolderobject_notgenerate": "不生成(默認)", + "settings_s3_generatefolderobject_generate": "生成", + "settings_s3_connect_succ": "很好!可以訪問到對應儲存桶。", + "settings_s3_connect_fail": "無法訪問到對應儲存桶。", + "settings_dropbox": "Dropbox 設定", + "settings_dropbox_disclaimer1": "宣告:此外掛不是 Dropbox 官方產品。", + "settings_dropbox_disclaimer2": "宣告:您所輸入的資訊儲存於本地。其它有害的或者出錯的外掛,是有可能讀取到這些資訊的。如果您發現了 Dropbox 有不符合預期的訪問,請立刻從 https://www.dropbox.com/account/connected_apps 刪除記錄於此外掛的連線鑑權。", + "settings_dropbox_folder": "我們會在您的 Dropbox 上建立此資料夾並在裡面同步:/Apps/{{pluginID}}/{{remoteBaseDir}}。", + "settings_dropbox_revoke": "撤回鑑權", + "settings_dropbox_revoke_desc": "您現在使用了使用者名稱 {{username}} 來連線。如果想取消連線,請點選此按鈕。", + "settings_dropbox_revoke_button": "撤回鑑權", + "settings_dropbox_revoke_notice": "鑑權已被撤回!", + "settings_dropbox_revoke_noticeerr": "撤回鑑權出錯。", + "settings_dropbox_clearlocal": "清除本地儲存的鑑權資訊", + "settings_dropbox_clearlocal_desc": "您可以強制清除本地已儲存的 Dropbox 的登入鑑權資訊,但不向伺服器傳送撤回鑑權的請求。", + "settings_dropbox_clearlocal_button": "清除", + "settings_dropbox_clearlocal_notice": "已清除!", + "settings_dropbox_auth": "鑑權", + "settings_dropbox_auth_desc": "鑑權。", + "settings_dropbox_auth_button": "鑑權", + "settings_dropbox_connect_succ": "很好!我們可連線上 Dropbox!", + "settings_dropbox_connect_fail": "我們未能連線上 Dropbox。", + "settings_onedrive": "Onedrive(個人版)(App Folder)設定", + "settings_onedrive_disclaimer1": "宣告:此外掛不是微軟或 OneDrive 的官方產品。", + "settings_onedrive_disclaimer2": "宣告:您所輸入的資訊儲存於本地。其它有害的或者出錯的外掛,是有可能讀取到這些資訊的。如果您發現了 OneDrive 有不符合預期的訪問,請立刻從 https://microsoft.com/consent 刪除記錄於此外掛的連線鑑權。", + "settings_onedrive_folder": "我們會在您的 OneDrive 上建立此資料夾並在裡面同步:/Apps/{{pluginID}}/{{remoteBaseDir}}。", + "settings_onedrive_nobiz": "現在只支援個人版 OneDrive。(暫時)並不支援企業版 OneDrive。", + "settings_onedrive_revoke": "撤回鑑權", + "settings_onedrive_revoke_desc": "您現在使用了使用者名稱 {{username}} 來連線。如果想取消連線,請點選此按鈕。", + "settings_onedrive_revoke_button": "撤回鑑權", + "settings_onedrive_auth": "鑑權", + "settings_onedrive_auth_desc": "鑑權。", + "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 設定。但您正在使用舊版,建議升級。", + "settings_webdav_cors": "您需要設定 CORS,允許來自這些源的請求:app://obsidian.md 和 capacitor://localhost 和 http://localhost。", + "settings_webdav_folder": "我們會在您的伺服器上建立此資料夾並在裡面同步:/{{remoteBaseDir}}", + "settings_webdav_addr": "伺服器地址", + "settings_webdav_addr_desc": "伺服器地址。", + "settings_webdav_user": "使用者名稱", + "settings_webdav_user_desc": "使用者名稱。注意:使用者名稱和其它資訊都會儲存在本地。", + "settings_webdav_password": "密碼", + "settings_webdav_password_desc": "密碼。注意:密碼和其它資訊都會儲存在本地。", + "settings_webdav_auth": "鑑權型別", + "settings_webdav_auth_desc": "如果不填寫密碼,本設定會被忽略。", + "settings_webdav_depth": "傳送到伺服器的 Depth header", + "settings_webdav_depth_desc": "Webdav 伺服器會被設為接收 Depth header 為“1”或“Infinity”請求。如果您不清楚如何設定,可以選擇 depth='1'。如果那你確認伺服器支援 depth='infinity',請選擇它,會獲得更加好的效能。", + "settings_webdav_depth_1": "只支援 depth='1'", + "settings_webdav_depth_inf": "支援 depth='infinity'", + "settings_webdav_customheaders": "傳送到伺服器的自定義 Header", + "settings_webdav_customheaders_desc": "有些使用者需要設定自定義 header。如果您不知道是什麼,可以直接留空不填。輸入格式:每行形如 X-Header1:value1。header 的 key 和 value 之間的空格會被刪除。", + "settings_webdav_connect_succ": "很好!可以連線上 Webdav 伺服器。", + "settings_webdav_connect_fail": "無法連線上 Webdav 伺服器。(可能是地址/賬號/密碼/鑑權型別等錯誤。)", + "settings_webdav_connect_fail_withcors": "無法連線上 Webdav 伺服器。(可能是地址/賬號/密碼/鑑權型別/CORS 等錯誤。)", + "settings_webdis": "Webdis 設置", + "settings_webdis_disclaimer1": "聲明:此插件不是 Redis® Ltd 或 Redis® 軟件或 Wedis 的官方產品。Redis 是 Redis Ltd 的註冊商標。", + "settings_webdis_disclaimer2": "聲明:您所輸入的信息存儲於本地。其它有害的或者出錯的插件,是有可能讀取到這些信息的。如果您發現了 Webdis 服務器有不符合預期的訪問,請立刻修改用戶名和密碼。", + "settings_webdis_folder": "我們會在您的服務器上創建帶有此前綴的 key 並在裡面同步::{{remoteBaseDir}}。", + "settings_webdis_addr": "服務器地址", + "settings_webdis_addr_desc": "服務器地址", + "settings_webdis_user": "用戶名", + "settings_webdis_user_desc": "用戶名。注意:用戶名和其它信息都會保存在本地。", + "settings_webdis_password": "密碼", + "settings_webdis_password_desc": "密碼。注意:密碼和其它信息都會保存在本地。", + "settings_webdis_connect_succ": "很好!可以連接上 Webdis 服務器。", + "settings_webdis_connect_fail": "無法連接上 Webdis 服務器。(可能是地址/賬號/密碼/鑑權類型等錯誤。)", + "settings_chooseservice": "選擇遠端服務", + "settings_chooseservice_desc": "從這裡開始設定。您想連線到哪一個服務?S3、Dropbox、Webdav、OneDrive(個人版)、Webdis?", + "settings_chooseservice_s3": "S3 或相容 S3 的服務", + "settings_chooseservice_dropbox": "Dropbox", + "settings_chooseservice_webdav": "Webdav", + "settings_chooseservice_onedrive": "OneDrive(個人版)", + "settings_chooseservice_webdis": "Webdis (an HTTP interface for Redis®)", + "settings_adv": "進階設定", + "settings_concurrency": "並行度", + "settings_concurrency_desc": "您希望同時最多有多少個檔案被上傳和下載?預設值是 5。如果您遇到了一些問題(如訪問頻率限制),您可以減少並行度。", + "settings_syncunderscore": "同步 _ 檔案或資料夾", + "settings_syncunderscore_desc": "是否同步“_”(下劃線)開始的檔案或資料夾。", + "settings_configdir": "同步配置資料夾(實驗性質)", + "settings_configdir_desc": "是否同步 Obsidian 的配置資料夾 {{configDir}}(其中的 .git 和 node_modules 依然會被忽略)。請注意,此設定可能會影響到您 Obsidian 或者所有的外掛設定,也可能需要您同步完畢後重啟 Obsidian。如果您同意自行承受其風險,則可以開啟本設定。", + "settings_bookmarks": "同步書籤(實驗性質)", + "settings_bookmarks_desc": "是否同步書籤 {{configDir}}/bookmarks.json 。如果您設定了同步配置資料夾,此選項會被忽略並設為“開啟”。", + "settings_deletetowhere": "刪除檔案到哪裡", + "settings_deletetowhere_desc": "外掛觸發刪除操作時候,刪除到哪裡?", + "settings_deletetowhere_system_trash": "系統回收站(預設)", + "settings_deletetowhere_obsidian_trash": "Obsidian .trash 資料夾", + "settings_conflictaction": "處理衝突", + "settings_conflictaction_desc": "如果一個檔案,在本地和伺服器都被建立或者修改了,那麼這就是一個“衝突”情況。如何處理?這個設定只在雙向同步時候生效。", + "settings_conflictaction_keep_newer": "保留最後修改的版本(預設)", + "settings_conflictaction_keep_larger": "保留檔案體積較大的版本", + "settings_cleanemptyfolder": "處理空資料夾", + "settings_cleanemptyfolder_desc": "同步演算法主要是針對檔案處理的,您需要手動指定空資料夾如何處理。", + "settings_cleanemptyfolder_skip": "跳過處理空資料夾(預設)", + "settings_cleanemptyfolder_clean_both": "刪除本地和伺服器的空資料夾", + "settings_protectmodifypercentage": "如果修改超過百分比則中止同步", + "settings_protectmodifypercentage_desc": "如果演算法檢測到超過 n% 的檔案會被修改或刪除,則中止同步。從而可以保護使用者的檔案免受預料之外的修改。您可以設定為 100 而去除此保護,也可以設定為 0 總是強制中止所有同步。", + "settings_protectmodifypercentage_000_desc": "0(總是強制中止)", + "settings_protectmodifypercentage_050_desc": "50(預設值)", + "settings_protectmodifypercentage_100_desc": "100(去除此保護)", + "settings_protectmodifypercentage_custom_desc": "自定義", + "settings_protectmodifypercentage_customfield": "如果修改超過自定義百分比則中止同步", + "settings_protectmodifypercentage_customfield_desc": "您需要輸入 0(含)~ 100(含)的數字。小數也是可以的。", + "setting_syncdirection": "同步方向(實驗性質)", + "setting_syncdirection_desc": "

W外掛應該向哪裡同步?注意每個選項都是隻有修改了的檔案(基於修改時間和大小判斷)才會觸發同步動作。

  1. 雙向同步:一邊的變動會同步到另一邊。
  2. 增量推送:本地建立或修改的檔案會複製到遠端。
  3. 增量拉取:遠端建立或修改的檔案會複製到本地。
  4. 增量推送帶刪除:本地建立或修改的檔案會複製到遠端,本地的刪除也會在遠端執行。
  5. 增量拉取帶刪除:遠端建立或修改的檔案會複製到本地,遠端的刪除也會在本地執行。
", + "setting_syncdirection_bidirectional_desc": "雙向同步(預設)", + "setting_syncdirection_incremental_push_only_desc": "增量推送(也即:備份模式)", + "setting_syncdirection_incremental_pull_only_desc": "增量拉取", + "setting_syncdirection_incremental_push_and_delete_only_desc": "增量推送帶刪除", + "setting_syncdirection_incremental_pull_and_delete_only_desc": "增量拉取帶刪除", + "settings_enablemobilestatusbar": "手機的狀態列(實驗性質)", + "settings_enablemobilestatusbar_desc": "Obsidian 手機版預設隱藏了狀態列。有些使用者希望展示它。這裡提供了設定選項。", + "settings_importexport": "匯入匯出部分設定", + "settings_export": "匯出", + "settings_export_desc": "用 QR 碼或 URI 匯出設定資訊。", + "settings_export_basic_and_advanced_button": "匯出基本或進階設定", + "settings_export_s3_button": "匯出 S3 部分", + "settings_export_dropbox_button": "匯出 Dropbox 部分", + "settings_export_onedrive_button": "匯出 OneDrive (App Folder) 部分", + "settings_export_webdav_button": "匯出 Webdav 部分", + "settings_export_webdis_button": "匯出 Webdis 部分", + "settings_import": "匯入", + "settings_import_desc": "貼上之前匯出的 URI 到這裡然後點選“匯入”。或,使用拍攝 app 或者掃描 QR 碼的 app,來掃描對應的 QR 碼。", + "settings_import_button": "匯入", + "settings_import_error_notice": "您輸入的 URI 是空的或者不準確的!", + "settings_debug": "除錯", + "settings_debuglevel": "修改同步提示資訊", + "settings_debuglevel_desc": "預設值為 \"info\"。您可以改為 \"debug\" 從而在同步時候裡獲取更多資訊。", + "settings_outputsettingsconsole": "讀取硬碟上的設定檔案輸出到終端", + "settings_outputsettingsconsole_desc": "硬碟上的設定檔案是編碼過的,點選這裡從而解碼並輸出到終端。", + "settings_outputsettingsconsole_button": "輸出", + "settings_outputsettingsconsole_notice": "已輸出到終端", + "settings_obfuscatesettingfile": "是否混淆儲存設定檔案", + "settings_obfuscatesettingfile_desc": "設定檔案(data.json)含有敏感資訊。強烈建議混淆後儲存它,從而避免出乎意料的讀取和修改。如果您確認要手動檢視和修改它,可以關閉混淆儲存。", + "settings_viewconsolelog": "檢視終端輸出", + "settings_viewconsolelog_desc": "電腦上,輸入“ctrl+shift+i”或“cmd+shift+i”來檢視終端輸出。手機上,安裝第三方外掛 Logstravaganza 來匯出終端輸出到一篇筆記上。", + "settings_syncplans": "匯出同步計劃", + "settings_syncplans_desc": "每次您啟動同步,並在實際上傳下載前,外掛會生成同步計劃。它可以使您知道每次同步發生了什麼。點選按鈕可以匯出同步計劃。", + "settings_syncplans_button_1_only_change": "匯出最近 1 次(僅修改部分)", + "settings_syncplans_button_5_only_change": "匯出最近 5 次(僅修改部分)", + "settings_syncplans_button_1": "匯出最近 1 次", + "settings_syncplans_button_5": "匯出最近 5 次", + "settings_syncplans_button_all": "匯出所有", + "settings_syncplans_notice": "同步計劃已匯出", + "settings_delsyncplans": "刪除資料庫裡的同步計劃歷史", + "settings_delsyncplans_desc": "刪除資料庫裡的同步計劃歷史。", + "settings_delsyncplans_button": "刪除同步計劃歷史", + "settings_delsyncplans_notice": "(資料庫裡的)同步計劃已被刪除。", + "settings_delprevsync": "刪除資料庫裡的上次同步明細", + "settings_delprevsync_desc": "同步演算法需要上次成功同步的資訊來決定檔案變更,這個資訊儲存在本地的資料庫裡。如果您想忽略這些資訊從而所有檔案都被視為新建立的話,可以在此刪除之前的資訊。", + "settings_delprevsync_button": "刪除上次同步明細", + "settings_delprevsync_notice": "(本地資料庫裡的)上次同步明細已被刪除。", + "settings_profiler_results": "匯出效能資料記錄", + "settings_profiler_results_desc": "外掛記錄了每次同步每一步的耗時。這裡可以匯出記錄得知哪一步最慢。", + "settings_profiler_results_notice": "效能資料已匯出", + "settings_profiler_results_button_all": "匯出所有", + "settings_profiler_enableprofiler": "效能收集", + "settings_profiler_enableprofiler_desc": "是否開啟效能收集功能?", + "settings_profiler_enabledebugprint": "效能收集輸出", + "settings_profiler_enabledebugprint_desc": "是否直接輸出效能收集結果到終端裡?", + "settings_profiler_recordsize": "效能收集統計物件大小", + "settings_profiler_recordsize_desc": "是否收集物件的大小?", + "settings_outputbasepathvaultid": "輸出資料庫對應的位置和隨機分配的 ID", + "settings_outputbasepathvaultid_desc": "用於除錯。", + "settings_outputbasepathvaultid_button": "輸出", + "settings_resetcache": "重設本地快取和資料庫", + "settings_resetcache_desc": "(出於除錯原因)重設本地快取和資料庫。您需要在重設之後重新載入此外掛。本重設不會刪除 s3,密碼……等設定。", + "settings_resetcache_button": "重設", + "settings_resetcache_notice": "本地同步快取和資料庫已被刪除。請手動重新載入此外掛。", + "syncalgov3_title": "BYOC 的同步演算法有重大更新", + "syncalgov3_texts": "歡迎使用 BYOC!\n從這個版本開始,外掛更新了同步演算法:\n
  • 更穩健的刪除同步
  • 引入衝突處理
  • 避免上傳元資料
  • 修改刪除保護
  • 備份模式
  • 新的加密方式
  • ……
\n敬請期待更多更新!詳細介紹請參閱文件網站。\n如果您同意使用新版本,請閱讀和勾選兩個勾選框,然後點選“同意”按鈕,開始使用外掛吧!\n如果您不同意,請點選“不同意”按鈕,外掛將自動停止執行(unload)。\n此外,請考慮訪問 GitHub 頁面然後點贊 ⭐!您的支援對我十分重要!謝謝!", + "syncalgov3_checkbox_manual_backup": "我將會首先手動備份我的庫(Vault)。", + "syncalgov3_checkbox_requiremultidevupdate": "我理解,我需要在所有裝置上都更新此外掛使之正常執行。", + "syncalgov3_button_agree": "同意", + "syncalgov3_button_disagree": "不同意", + "menu_check_file_stat": "檢視檔案屬性", + "settings_pcloud": "pCloud", + "settings_pcloud_folder": "Base folder in pCloud: /{{remoteBaseDir}}", + "settings_pcloud_auth": "Authorize", + "settings_pcloud_auth_desc": "Authorize this plugin to access your pCloud account.", + "settings_pcloud_auth_button": "Authorize", + "settings_pcloud_revoke": "Revoke Authorization", + "settings_pcloud_revoke_desc": "Revoke authorization if you want to switch accounts or reset your connection.", + "settings_pcloud_revoke_button": "Revoke", + "modal_pcloudauth_tutorial": "Step 1: Click the link below to securely authorize obsidian-byoc. Step 2: The app will automatically capture the token.", + "modal_pcloudauth_copybutton": "Copy Link", + "modal_pcloudauth_copynotice": "Copied to clipboard.", + "modal_pcloudrevokeauth_step1": "To fully revoke access, visit pCloud Apps and remove obsidian-byoc.", + "modal_pcloudrevokeauth_step2": "Click clean to wipe tokens locally.", + "modal_pcloudrevokeauth_clean": "Clean up local records", + "modal_pcloudrevokeauth_clean_desc": "Deletes local pCloud access tokens.", + "modal_pcloudrevokeauth_clean_button": "Clean", + "modal_pcloudrevokeauth_clean_notice": "Local records cleaned.", + "modal_pcloudrevokeauth_clean_fail": "Failed to clean local records.", + "settings_pcloud_connect_succ": "pCloud Check: Success!", + "settings_pcloud_connect_fail": "pCloud Check: Failed!" +} \ No newline at end of file diff --git a/src/localdb.ts b/src/localdb.ts new file mode 100644 index 0000000..1322f7d --- /dev/null +++ b/src/localdb.ts @@ -0,0 +1,608 @@ +import localforage from "localforage"; +import { extendPrototype as ep1 } from "localforage-getitems"; +import { extendPrototype as ep2 } from "localforage-removeitems"; +ep1(localforage); +ep2(localforage); +export type LocalForage = typeof localforage; +import { nanoid } from "nanoid"; + +/** SyncPlanType — local stub; full type defined in Batch 2 sync engine */ +type SyncPlanType = Record; +import type { Entity, SUPPORTED_SERVICES_TYPE } from "./baseTypes"; +import { unixTimeToStr } from "./misc"; + +const DB_VERSION_NUMBER_IN_HISTORY = [20211114, 20220108, 20220326, 20240220]; +export const DEFAULT_DB_VERSION_NUMBER: number = 20240220; +export const DEFAULT_DB_NAME = "remotelysavedb"; +export const DEFAULT_TBL_VERSION = "schemaversion"; +export const DEFAULT_SYNC_PLANS_HISTORY = "syncplanshistory"; +export const DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING = "vaultrandomidmapping"; +export const DEFAULT_TBL_LOGGER_OUTPUT = "loggeroutput"; +export const DEFAULT_TBL_SIMPLE_KV_FOR_MISC = "simplekvformisc"; +export const DEFAULT_TBL_PREV_SYNC_RECORDS = "prevsyncrecords"; +export const DEFAULT_TBL_PROFILER_RESULTS = "profilerresults"; +export const DEFAULT_TBL_FILE_CONTENT_HISTORY = "filecontenthistory"; + +/** + * @deprecated + */ +export const DEFAULT_TBL_FILE_HISTORY = "filefolderoperationhistory"; +/** + * @deprecated + */ +export const DEFAULT_TBL_SYNC_MAPPING = "syncmetadatahistory"; + +/** + * @deprecated + * But we cannot remove it. Because we want to migrate the old data. + */ +interface SyncMetaMappingRecord { + localKey: string; + remoteKey: string; + localSize: number; + remoteSize: number; + localMtime: number; + remoteMtime: number; + remoteExtraKey: string; + remoteType: SUPPORTED_SERVICES_TYPE; + keyType: "folder" | "file"; + vaultRandomID: string; +} + +interface SyncPlanRecord { + ts: number; + remoteType: string; + syncPlan: string; + vaultRandomID: string; +} + +export interface InternalDBs { + versionTbl: LocalForage; + syncPlansTbl: LocalForage; + vaultRandomIDMappingTbl: LocalForage; + loggerOutputTbl: LocalForage; + simpleKVForMiscTbl: LocalForage; + prevSyncRecordsTbl: LocalForage; + profilerResultsTbl: LocalForage; + fileContentHistoryTbl: LocalForage; + + /** + * @deprecated + * But we cannot remove it. Because we want to migrate the old data. + */ + fileHistoryTbl: LocalForage; + + /** + * @deprecated + * But we cannot remove it. Because we want to migrate the old data. + */ + syncMappingTbl: LocalForage; +} + +/** + * TODO + * @param syncMappings + * @returns + */ +const fromSyncMappingsToPrevSyncRecords = ( + oldSyncMappings: SyncMetaMappingRecord[] +): Entity[] => { + const res: Entity[] = []; + for (const oldMapping of oldSyncMappings) { + const newEntity: Entity = { + key: oldMapping.localKey, + keyEnc: oldMapping.remoteKey, + keyRaw: + oldMapping.remoteKey !== undefined && oldMapping.remoteKey !== "" + ? oldMapping.remoteKey + : oldMapping.localKey, + mtimeCli: oldMapping.localMtime, + mtimeSvr: oldMapping.remoteMtime, + size: oldMapping.localSize, + sizeEnc: oldMapping.remoteSize, + sizeRaw: + oldMapping.remoteKey !== undefined && oldMapping.remoteKey !== "" + ? oldMapping.remoteSize + : oldMapping.localSize, + etag: oldMapping.remoteExtraKey, + }; + + res.push(newEntity); + } + return res; +}; + +/** + * + * @param db + * @param vaultRandomID + * Migrate the sync mapping record to sync Entity. + */ +const migrateDBsFrom20220326To20240220 = async ( + db: InternalDBs, + vaultRandomID: string, + profileID: string +) => { + const oldVer = 20220326; + const newVer = 20240220; + console.debug(`start upgrading internal db from ${oldVer} to ${newVer}`); + + // from sync mapping to prev sync + const syncMappings = await getAllSyncMetaMappingByVault(db, vaultRandomID); + const prevSyncRecords = fromSyncMappingsToPrevSyncRecords(syncMappings); + for (const prevSyncRecord of prevSyncRecords) { + await upsertPrevSyncRecordByVaultAndProfile( + db, + vaultRandomID, + profileID, + prevSyncRecord + ); + } + + // // clear not used data + // // as of 20240220, we don't call them, + // // for the opportunity for users to downgrade + // await clearFileHistoryOfEverythingByVault(db, vaultRandomID); + // await clearAllSyncMetaMappingByVault(db, vaultRandomID); + + await db.versionTbl.setItem(`${vaultRandomID}\tversion`, newVer); + console.debug(`finish upgrading internal db from ${oldVer} to ${newVer}`); +}; + +const migrateDBs = async ( + db: InternalDBs, + oldVer: number, + newVer: number, + vaultRandomID: string, + profileID: string +) => { + if (oldVer === newVer) { + return; + } + + // as of 20240220, we assume everyone is using 20220326 already + // drop any old code to reduce the verbose + if (oldVer < 20220326) { + throw Error( + "You are using a very old version of BYOC (or Remotely Save). No way to auto update internal DB. Please install and enable 0.3.40 of Remotely Save firstly, then install BYOC." + ); + } + + if (oldVer === 20220326 && newVer === 20240220) { + return await migrateDBsFrom20220326To20240220(db, vaultRandomID, profileID); + } + + if (newVer < oldVer) { + throw Error( + "You've installed a new version, but then downgrade to an old version. Stop working!" + ); + } + // not implemented + throw Error(`not supported internal db changes from ${oldVer} to ${newVer}`); +}; + +export const prepareDBs = async ( + vaultBasePath: string, + vaultRandomIDFromOldConfigFile: string, + profileID: string +) => { + const db = { + versionTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_VERSION, + }), + syncPlansTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_SYNC_PLANS_HISTORY, + }), + vaultRandomIDMappingTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_VAULT_RANDOM_ID_MAPPING, + }), + loggerOutputTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_LOGGER_OUTPUT, + }), + simpleKVForMiscTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_SIMPLE_KV_FOR_MISC, + }), + prevSyncRecordsTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_PREV_SYNC_RECORDS, + }), + profilerResultsTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_PROFILER_RESULTS, + }), + + fileHistoryTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_FILE_HISTORY, + }), + syncMappingTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_SYNC_MAPPING, + }), + + fileContentHistoryTbl: localforage.createInstance({ + name: DEFAULT_DB_NAME, + storeName: DEFAULT_TBL_FILE_CONTENT_HISTORY, + }), + } as InternalDBs; + + // try to get vaultRandomID firstly + let vaultRandomID = ""; + const vaultRandomIDInDB: string | null = + await db.vaultRandomIDMappingTbl.getItem(`path2id\t${vaultBasePath}`); + if (vaultRandomIDInDB === null) { + if (vaultRandomIDFromOldConfigFile !== "") { + // reuse the old config id + vaultRandomID = vaultRandomIDFromOldConfigFile; + } else { + // no old config id, we create a random one + vaultRandomID = nanoid(); + } + // save the id back + await db.vaultRandomIDMappingTbl.setItem( + `path2id\t${vaultBasePath}`, + vaultRandomID + ); + await db.vaultRandomIDMappingTbl.setItem( + `id2path\t${vaultRandomID}`, + vaultBasePath + ); + } else { + vaultRandomID = vaultRandomIDInDB; + } + + if (vaultRandomID === "") { + throw Error("no vaultRandomID found or generated"); + } + + // as of 20240220, we set the version per vault, instead of global "version" + const originalVersion: number | null = + (await db.versionTbl.getItem(`${vaultRandomID}\tversion`)) ?? + (await db.versionTbl.getItem("version")); + if (originalVersion === null) { + console.debug( + `no internal db version, setting it to ${DEFAULT_DB_VERSION_NUMBER}` + ); + // as of 20240220, we set the version per vault, instead of global "version" + await db.versionTbl.setItem( + `${vaultRandomID}\tversion`, + DEFAULT_DB_VERSION_NUMBER + ); + } else if (originalVersion === DEFAULT_DB_VERSION_NUMBER) { + // do nothing + } else { + console.debug( + `trying to upgrade db version from ${originalVersion} to ${DEFAULT_DB_VERSION_NUMBER}` + ); + await migrateDBs( + db, + originalVersion, + DEFAULT_DB_VERSION_NUMBER, + vaultRandomID, + profileID + ); + } + + console.info("db connected"); + return { + db: db, + vaultRandomID: vaultRandomID, + }; +}; + +export const destroyDBs = async () => { + // await localforage.dropInstance({ + // name: DEFAULT_DB_NAME, + // }); + // console.info("db deleted"); + const req = indexedDB.deleteDatabase(DEFAULT_DB_NAME); + req.onsuccess = (event) => { + console.info("db deleted"); + }; + req.onblocked = (event) => { + console.warn("trying to delete db but it was blocked"); + }; + req.onerror = (event) => { + console.error("tried to delete db but something goes wrong!"); + console.error(event); + }; +}; + +export const clearFileHistoryOfEverythingByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + const keys = (await db.fileHistoryTbl.keys()).filter((x) => + x.startsWith(`${vaultRandomID}\t`) + ); + await db.fileHistoryTbl.removeItems(keys); + // for (const key of keys) { + // if (key.startsWith(`${vaultRandomID}\t`)) { + // await db.fileHistoryTbl.removeItem(key); + // } + // } +}; + +/** + * @deprecated But we cannot remove it. Because we want to migrate the old data. + * @param db + * @param vaultRandomID + * @returns + */ +export const getAllSyncMetaMappingByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + return await Promise.all( + ((await db.syncMappingTbl.keys()) ?? []) + .filter((key) => key.startsWith(`${vaultRandomID}\t`)) + .map( + async (key) => + (await db.syncMappingTbl.getItem(key)) as SyncMetaMappingRecord + ) + ); +}; + +export const clearAllSyncMetaMappingByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + const keys = (await db.syncMappingTbl.keys()).filter((x) => + x.startsWith(`${vaultRandomID}\t`) + ); + await db.syncMappingTbl.removeItems(keys); + // for (const key of keys) { + // if (key.startsWith(`${vaultRandomID}\t`)) { + // await db.syncMappingTbl.removeItem(key); + // } + // } +}; + +export const insertSyncPlanRecordByVault = async ( + db: InternalDBs, + syncPlan: SyncPlanType, + vaultRandomID: string, + remoteType: SUPPORTED_SERVICES_TYPE +) => { + const now = Date.now(); + const record = { + ts: now, + tsFmt: unixTimeToStr(now), + vaultRandomID: vaultRandomID, + remoteType: remoteType, + syncPlan: JSON.stringify(syncPlan /* directly stringify */, null, 2), + } as SyncPlanRecord; + await db.syncPlansTbl.setItem(`${vaultRandomID}\t${now}`, record); +}; + +export const clearAllSyncPlanRecords = async (db: InternalDBs) => { + await db.syncPlansTbl.clear(); +}; + +export const readAllSyncPlanRecordTextsByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + const records = [] as SyncPlanRecord[]; + await db.syncPlansTbl.iterate((value, key, iterationNumber) => { + if (key.startsWith(`${vaultRandomID}\t`)) { + records.push(value as SyncPlanRecord); + } + }); + records.sort((a, b) => -(a.ts - b.ts)); // descending + + if (records === undefined) { + return [] as string[]; + } else { + return records.map((x) => x.syncPlan); + } +}; + +/** + * We remove records that are older than 1 days or 20 records. + * It's a heavy operation, so we shall not place it in the start up. + * @param db + */ +export const clearExpiredSyncPlanRecords = async (db: InternalDBs) => { + const MILLISECONDS_OLD = 1000 * 60 * 60 * 24 * 1; // 1 days + const COUNT_TO_MANY = 20; + + const currTs = Date.now(); + const expiredTs = currTs - MILLISECONDS_OLD; + + let records = (await db.syncPlansTbl.keys()).map((key) => { + const ts = Number.parseInt(key.split("\t")[1]); + const expired = ts <= expiredTs; + return { + ts: ts, + key: key, + expired: expired, + }; + }); + + const keysToRemove = new Set( + records.filter((x) => x.expired).map((x) => x.key) + ); + + if (records.length - keysToRemove.size > COUNT_TO_MANY) { + // we need to find out records beyond 100 records + records = records.filter((x) => !x.expired); // shrink the array + records.sort((a, b) => -(a.ts - b.ts)); // descending + records.slice(COUNT_TO_MANY).forEach((element) => { + keysToRemove.add(element.key); + }); + } + + // const ps = [] as Promise[]; + // keysToRemove.forEach((element) => { + // ps.push(db.syncPlansTbl.removeItem(element)); + // }); + // await Promise.all(ps); + await db.syncPlansTbl.removeItems(Array.from(keysToRemove)); +}; + +export const getAllPrevSyncRecordsByVaultAndProfile = async ( + db: InternalDBs, + vaultRandomID: string, + profileID: string +) => { + const res: Entity[] = []; + const kv: Record = + await db.prevSyncRecordsTbl.getItems(); + for (const key of Object.getOwnPropertyNames(kv)) { + if (key.startsWith(`${vaultRandomID}\t${profileID}\t`)) { + const val = kv[key]; + if (val !== null) { + res.push(val); + } + } + } + return res; +}; + +export const upsertPrevSyncRecordByVaultAndProfile = async ( + db: InternalDBs, + vaultRandomID: string, + profileID: string, + prevSync: Entity +) => { + await db.prevSyncRecordsTbl.setItem( + `${vaultRandomID}\t${profileID}\t${prevSync.key}`, + prevSync + ); +}; + +export const clearPrevSyncRecordByVaultAndProfile = async ( + db: InternalDBs, + vaultRandomID: string, + profileID: string, + key: string +) => { + await db.prevSyncRecordsTbl.removeItem( + `${vaultRandomID}\t${profileID}\t${key}` + ); +}; + +export const clearAllPrevSyncRecordByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + const keys = (await db.prevSyncRecordsTbl.keys()).filter((x) => + x.startsWith(`${vaultRandomID}\t`) + ); + await db.prevSyncRecordsTbl.removeItems(keys); +}; + +export const clearAllLoggerOutputRecords = async (db: InternalDBs) => { + await db.loggerOutputTbl.clear(); + console.debug(`successfully clearAllLoggerOutputRecords`); +}; + +export const upsertLastSuccessSyncTimeByVault = async ( + db: InternalDBs, + vaultRandomID: string, + millis: number +) => { + await db.simpleKVForMiscTbl.setItem( + `${vaultRandomID}-lastSuccessSyncMillis`, + millis + ); +}; + +export const getLastSuccessSyncTimeByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + return (await db.simpleKVForMiscTbl.getItem( + `${vaultRandomID}-lastSuccessSyncMillis` + )) as number | null | undefined; +}; + +export const upsertLastFailedSyncTimeByVault = async ( + db: InternalDBs, + vaultRandomID: string, + millis: number +) => { + await db.simpleKVForMiscTbl.setItem( + `${vaultRandomID}-lastFailedSyncMillis`, + millis + ); +}; + +export const getLastFailedSyncTimeByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + return (await db.simpleKVForMiscTbl.getItem( + `${vaultRandomID}-lastFailedSyncMillis` + )) as number | null | undefined; +}; + +export const upsertPluginVersionByVault = async ( + db: InternalDBs, + vaultRandomID: string, + newVersion: string +) => { + let oldVersion: string | null = await db.simpleKVForMiscTbl.getItem( + `${vaultRandomID}-pluginversion` + ); + if (oldVersion === null) { + oldVersion = "0.0.0"; + } + await db.simpleKVForMiscTbl.setItem( + `${vaultRandomID}-pluginversion`, + newVersion + ); + + return { + oldVersion: oldVersion, + newVersion: newVersion, + }; +}; + +export const insertProfilerResultByVault = async ( + db: InternalDBs, + profilerStr: string, + vaultRandomID: string, + remoteType: SUPPORTED_SERVICES_TYPE +) => { + const now = Date.now(); + await db.profilerResultsTbl.setItem(`${vaultRandomID}\t${now}`, profilerStr); + + // clear older one while writing + const records = (await db.profilerResultsTbl.keys()) + .filter((x) => x.startsWith(`${vaultRandomID}\t`)) + .map((x) => Number.parseInt(x.split("\t")[1])); + records.sort((a, b) => -(a - b)); // descending + while (records.length > 5) { + const ts = records.pop()!; + await db.profilerResultsTbl.removeItem(`${vaultRandomID}\t${ts}`); + } +}; + +export const readAllProfilerResultsByVault = async ( + db: InternalDBs, + vaultRandomID: string +) => { + const records = [] as { val: string; ts: number }[]; + await db.profilerResultsTbl.iterate((value, key, iterationNumber) => { + if (key.startsWith(`${vaultRandomID}\t`)) { + records.push({ + val: value as string, + ts: Number.parseInt(key.split("\t")[1]), + }); + } + }); + records.sort((a, b) => -(a.ts - b.ts)); // descending + + if (records === undefined) { + return [] as string[]; + } else { + return records.map((x) => x.val); + } +}; diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..c1b3832 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,1112 @@ +// biome-ignore lint/suspicious/noShadowRestrictedNames: +import AggregateError from "aggregate-error"; +import cloneDeep from "lodash/cloneDeep"; +import throttle from "lodash/throttle"; +import { FileText, RefreshCcw, RotateCcw, createElement } from "lucide"; +import { + Events, + FileSystemAdapter, + type Modal, + Notice, + Platform, + Plugin, + type Setting, + TFolder, + addIcon, + requireApiVersion, + setIcon, +} from "obsidian"; +import type { + BYOCPluginSettings, + SyncTriggerSourceType, +} from "./baseTypes"; +import { + COMMAND_CALLBACK, + COMMAND_CALLBACK_DROPBOX, + COMMAND_CALLBACK_ONEDRIVE, + COMMAND_CALLBACK_ONEDRIVEFULL, + COMMAND_CALLBACK_BOX, + COMMAND_CALLBACK_PCLOUD, + COMMAND_CALLBACK_YANDEXDISK, + COMMAND_CALLBACK_KOOFR, + COMMAND_URI, + COMMAND_URI_LEGACY, +} from "./baseTypes"; +import { API_VER_ENSURE_REQURL_OK } from "./baseTypesObs"; +import { messyConfigToNormal, normalConfigToMessy } from "./configPersist"; +import { exportVaultSyncPlansToFiles } from "./debugMode"; +import { + DEFAULT_DROPBOX_CONFIG, + sendAuthReq as sendAuthReqDropbox, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceDropbox, +} from "./fsDropbox"; +import { FakeFsEncrypt } from "./fsEncrypt"; +import { getClient } from "./fsGetter"; +import { FakeFsLocal } from "./fsLocal"; +import { + type AccessCodeResponseSuccessfulType as AccessCodeResponseSuccessfulTypeOnedrive, + DEFAULT_ONEDRIVE_CONFIG, + sendAuthReq as sendAuthReqOnedrive, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceOnedrive, +} from "./fsOnedrive"; +import { DEFAULT_S3_CONFIG } from "./fsS3"; +import { DEFAULT_WEBDAV_CONFIG } from "./fsWebdav"; +import { DEFAULT_WEBDIS_CONFIG } from "./fsWebdis"; + +// --- BYOC Local Provider Imports (no pro/) --- +import { + DEFAULT_GOOGLEDRIVE_CONFIG, +} from "./fsGoogleDrive"; +import { + DEFAULT_BOX_CONFIG, + sendAuthReq as sendAuthReqBox, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceBox, +} from "./fsBox"; +import { + DEFAULT_PCLOUD_CONFIG, + type AuthAllowFirstRes as AuthAllowFirstResPCloud, + generateAuthUrl as generateAuthUrlPCloud, + sendAuthReq as sendAuthReqPCloud, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplacePCloud, +} from "./fsPCloud"; +import { + DEFAULT_YANDEXDISK_CONFIG, + sendAuthReq as sendAuthReqYandexDisk, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceYandexDisk, +} from "./fsYandexDisk"; +import { + DEFAULT_KOOFR_CONFIG, + sendAuthReq as sendAuthReqKoofr, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceKoofr, +} from "./fsKoofr"; +import { DEFAULT_AZUREBLOBSTORAGE_CONFIG } from "./fsAzureBlobStorage"; +import { + type AccessCodeResponseSuccessfulType as AccessCodeResponseSuccessfulTypeOnedriveFull, + DEFAULT_ONEDRIVEFULL_CONFIG, + sendAuthReq as sendAuthReqOnedriveFull, + setConfigBySuccessfullAuthInplace as setConfigBySuccessfullAuthInplaceOnedriveFull, +} from "./fsOnedriveFull"; + +// --- Sync Engine (stub in Batch 0, real implementation in Batch 2) --- +import { syncer } from "./sync/syncer"; + +// --- Data Migration (Batch 4) --- +import { runMigration, CURRENT_MIGRATION_VERSION } from "./migration"; + +import { I18n } from "./i18n"; +import type { LangTypeAndAuto, TransItemType } from "./i18n"; +import { importQrCodeUri } from "./importExport"; +import { + type InternalDBs, + clearAllLoggerOutputRecords, + clearExpiredSyncPlanRecords, + getLastFailedSyncTimeByVault, + getLastSuccessSyncTimeByVault, + prepareDBs, + upsertLastFailedSyncTimeByVault, + upsertLastSuccessSyncTimeByVault, + upsertPluginVersionByVault, +} from "./localdb"; +import { changeMobileStatusBar } from "./misc"; +import { DEFAULT_PROFILER_CONFIG, Profiler } from "./profiler"; +import { BYOCSettingTab } from "./settings"; + +// ─── Default Settings ───────────────────────────────────────────────────────── + +const DEFAULT_SETTINGS: BYOCPluginSettings = { + s3: DEFAULT_S3_CONFIG, + webdav: DEFAULT_WEBDAV_CONFIG, + dropbox: DEFAULT_DROPBOX_CONFIG, + onedrive: DEFAULT_ONEDRIVE_CONFIG, + onedrivefull: DEFAULT_ONEDRIVEFULL_CONFIG, + webdis: DEFAULT_WEBDIS_CONFIG, + googledrive: DEFAULT_GOOGLEDRIVE_CONFIG, + box: DEFAULT_BOX_CONFIG, + pcloud: DEFAULT_PCLOUD_CONFIG, + yandexdisk: DEFAULT_YANDEXDISK_CONFIG, + koofr: DEFAULT_KOOFR_CONFIG, + azureblobstorage: DEFAULT_AZUREBLOBSTORAGE_CONFIG, + password: "", + serviceType: "s3", + currLogLevel: "info", + autoRunEveryMilliseconds: -1, + initRunAfterMilliseconds: -1, + syncOnSaveAfterMilliseconds: -1, + agreeToUploadExtraMetadata: true, + concurrency: 5, + syncConfigDir: false, + syncBookmarks: false, + syncUnderscoreItems: false, + lang: "auto", + logToDB: false, + skipSizeLargerThan: -1, + ignorePaths: [], + onlyAllowPaths: [], + enableStatusBarInfo: true, + deleteToWhere: "system", + // BYOC: All users auto-agree to sync v3 — no modal shown + agreeToUseSyncV3: true, + conflictAction: "keep_newer", + howToCleanEmptyFolder: "clean_both", + protectModifyPercentage: 50, + syncDirection: "bidirectional", + obfuscateSettingFile: true, + enableMobileStatusBar: false, + encryptionMethod: "unknown", + profiler: DEFAULT_PROFILER_CONFIG, + migrationVersion: 0, +}; + +// ─── Icons ──────────────────────────────────────────────────────────────────── + +interface OAuth2Info { + verifier?: string; + helperModal?: Modal; + authDiv?: HTMLElement; + revokeDiv?: HTMLElement; + revokeAuthSetting?: Setting; +} + +const iconNameSyncWait = `byoc-sync-wait`; +const iconNameSyncRunning = `byoc-sync-running`; +const iconNameLogs = `byoc-logs`; + +const getIconSvg = () => { + const iconSvgSyncWait = createElement(RotateCcw); + iconSvgSyncWait.setAttribute("width", "100"); + iconSvgSyncWait.setAttribute("height", "100"); + const iconSvgSyncRunning = createElement(RefreshCcw); + iconSvgSyncRunning.setAttribute("width", "100"); + iconSvgSyncRunning.setAttribute("height", "100"); + const iconSvgLogs = createElement(FileText); + iconSvgLogs.setAttribute("width", "100"); + iconSvgLogs.setAttribute("height", "100"); + const res = { + iconSvgSyncWait: iconSvgSyncWait.outerHTML, + iconSvgSyncRunning: iconSvgSyncRunning.outerHTML, + iconSvgLogs: iconSvgLogs.outerHTML, + }; + iconSvgSyncWait.empty(); + iconSvgSyncRunning.empty(); + iconSvgLogs.empty(); + return res; +}; + +const getStatusBarShortMsgFromSyncSource = ( + t: (x: TransItemType, vars?: any) => string, + s: SyncTriggerSourceType | undefined +) => { + if (s === undefined) return ""; + switch (s) { + case "manual": return t("statusbar_sync_source_manual"); + case "dry": return t("statusbar_sync_source_dry"); + case "auto": return t("statusbar_sync_source_auto"); + case "auto_once_init": return t("statusbar_sync_source_auto_once_init"); + case "auto_sync_on_save": return t("statusbar_sync_source_auto_sync_on_save"); + default: throw new Error(`no translate for ${s}`); + } +}; + +// ─── Plugin Class ───────────────────────────────────────────────────────────── + +export default class BYOCPlugin extends Plugin { + settings!: BYOCPluginSettings; + db!: InternalDBs; + isSyncing!: boolean; + hasPendingSyncOnSave!: boolean; + statusBarElement!: HTMLSpanElement; + oauth2Info!: OAuth2Info; + currLogLevel!: string; + currSyncMsg?: string; + syncRibbon?: HTMLElement; + autoRunIntervalID?: number; + syncOnSaveIntervalID?: number; + i18n!: I18n; + vaultRandomID!: string; + debugServerTemp?: string; + syncEvent?: Events; + appContainerObserver?: MutationObserver; + + async syncRun(triggerSource: SyncTriggerSourceType = "manual") { + let profiler: Profiler | undefined = undefined; + if (this.settings.profiler?.enable ?? false) { + profiler = new Profiler( + undefined, + this.settings.profiler?.enablePrinting ?? false, + this.settings.profiler?.recordSize ?? false + ); + } + const fsLocal = new FakeFsLocal( + this.app.vault, + this.settings.syncConfigDir ?? false, + this.settings.syncBookmarks ?? false, + this.app.vault.configDir, + this.manifest.id, + profiler, + this.settings.deleteToWhere ?? "system" + ); + const fsRemote = getClient( + this.settings, + this.app.vault.getName(), + async () => await this.saveSettings() + ); + const fsEncrypt = new FakeFsEncrypt( + fsRemote, + this.settings.password ?? "", + this.settings.encryptionMethod ?? "rclone-base64" + ); + + const t = (x: TransItemType, vars?: any) => this.i18n.t(x, vars); + const profileID = this.getCurrProfileID(); + + const getProtectError = ( + protectModifyPercentage: number, + realModifyDeleteCount: number, + allFilesCount: number + ) => { + const percentNum = (100 * realModifyDeleteCount) / allFilesCount; + if (percentNum < protectModifyPercentage) { + return ""; + } + const percent = percentNum.toFixed(1); + return t("syncrun_abort_protectmodifypercentage", { + protectModifyPercentage, + realModifyDeleteCount, + allFilesCount, + percent, + }); + }; + + const getNotice = (s: SyncTriggerSourceType, msg: string, timeout?: number) => { + if (s === "manual" || s === "dry") new Notice(msg, timeout); + }; + + const notifyFunc = async (s: SyncTriggerSourceType, step: number) => { + switch (step) { + case 0: + if (s === "dry") getNotice(s, this.settings.currLogLevel === "info" ? t("syncrun_shortstep0") : t("syncrun_step0")); + break; + case 1: + getNotice(s, this.settings.currLogLevel === "info" + ? t("syncrun_shortstep1", { serviceType: this.settings.serviceType }) + : t("syncrun_step1", { serviceType: this.settings.serviceType })); + break; + case 2: if (this.settings.currLogLevel !== "info") getNotice(s, t("syncrun_step2")); break; + case 3: if (this.settings.currLogLevel !== "info") getNotice(s, t("syncrun_step3")); break; + case 4: if (this.settings.currLogLevel !== "info") getNotice(s, t("syncrun_step4")); break; + case 5: if (this.settings.currLogLevel !== "info") getNotice(s, t("syncrun_step5")); break; + case 6: if (this.settings.currLogLevel !== "info") getNotice(s, t("syncrun_step6")); break; + case 7: + if (s === "dry") { + getNotice(s, this.settings.currLogLevel === "info" ? t("syncrun_shortstep2skip") : t("syncrun_step7skip")); + } else if (this.settings.currLogLevel !== "info") { + getNotice(s, t("syncrun_step7")); + } + break; + case 8: + getNotice(s, this.settings.currLogLevel === "info" ? t("syncrun_shortstep2") : t("syncrun_step8")); + break; + default: + throw new Error(`unknown step=${step} for showing notice`); + } + }; + + const errNotifyFunc = async (s: SyncTriggerSourceType, error: Error) => { + console.error(error); + if (error instanceof AggregateError) { + for (const e of error.errors) getNotice(s, e.message, 10 * 1000); + } else { + getNotice(s, error?.message ?? "error while sync", 10 * 1000); + } + }; + + const ribbonFunc = async (s: SyncTriggerSourceType, step: number) => { + if (step === 1 && this.syncRibbon !== undefined) { + setIcon(this.syncRibbon, iconNameSyncRunning); + this.syncRibbon.setAttribute("aria-label", + t("syncrun_syncingribbon", { pluginName: this.manifest.name, triggerSource: s })); + } else if (step === 8 && this.syncRibbon !== undefined) { + setIcon(this.syncRibbon, iconNameSyncWait); + this.syncRibbon.setAttribute("aria-label", `${this.manifest.name}`); + } + }; + + const statusBarFunc = async (s: SyncTriggerSourceType, step: number, everythingOk: boolean) => { + if (step === 1) { + this.updateLastSyncMsg(s, "syncing", -1, -1); + } else if (step === 8 && everythingOk) { + const ts = Date.now(); + await upsertLastSuccessSyncTimeByVault(this.db, this.vaultRandomID, ts); + this.updateLastSyncMsg(s, "not_syncing", ts, null); + } else if (!everythingOk) { + const ts = Date.now(); + await upsertLastFailedSyncTimeByVault(this.db, this.vaultRandomID, ts); + this.updateLastSyncMsg(s, "not_syncing", null, ts); + } + }; + + const markIsSyncingFunc = async (isSyncing: boolean) => { + this.isSyncing = isSyncing; + }; + + const callbackSyncProcess = async ( + s: SyncTriggerSourceType, + realCounter: number, + realTotalCount: number, + pathName: string, + decision: string + ) => { + this.setCurrSyncMsg(t, s, realCounter, realTotalCount, pathName, decision, triggerSource); + }; + + if (this.isSyncing) { + getNotice(triggerSource, t("syncrun_alreadyrunning", { + pluginName: this.manifest.name, + syncStatus: "running", + newTriggerSource: triggerSource, + })); + if (this.currSyncMsg) getNotice(triggerSource, this.currSyncMsg); + return; + } + + const configSaver = async () => await this.saveSettings(); + + await syncer( + fsLocal, + fsRemote, + fsEncrypt, + profiler, + this.db, + triggerSource, + profileID, + this.vaultRandomID, + this.app.vault.configDir, + this.settings, + this.manifest.version, + configSaver, + getProtectError, + markIsSyncingFunc, + notifyFunc, + errNotifyFunc, + ribbonFunc, + statusBarFunc, + callbackSyncProcess + ); + + fsEncrypt.closeResources(); + (profiler as Profiler | undefined)?.clear(); + this.syncEvent?.trigger("SYNC_DONE"); + } + + async onload() { + console.info(`loading plugin ${this.manifest.id} (BYOC)`); + + const { iconSvgSyncWait, iconSvgSyncRunning, iconSvgLogs } = getIconSvg(); + addIcon(iconNameSyncWait, iconSvgSyncWait); + addIcon(iconNameSyncRunning, iconSvgSyncRunning); + addIcon(iconNameLogs, iconSvgLogs); + + this.oauth2Info = { + verifier: "", + helperModal: undefined, + authDiv: undefined, + revokeDiv: undefined, + revokeAuthSetting: undefined, + }; + + this.currSyncMsg = ""; + this.isSyncing = false; + this.hasPendingSyncOnSave = false; + this.syncEvent = new Events(); + + await this.loadSettings(); + + // ─── Batch 4: Data Migration ──────────────────────────────────────────── + // Auto-detect legacy remotely-save configs and migrate on first run. + // Backs up data.json before any changes, strips credential expiry timers, + // and promotes pro-gated provider configs to top-level. + const migrationRan = await runMigration(this, this.settings); + if (migrationRan) { + await this.saveSettings(); + console.info(`[BYOC] Migration complete. Version: ${this.settings.migrationVersion}`); + } + + const profileID: string = this.getCurrProfileID(); + + this.i18n = new I18n(this.settings.lang!, async (lang: LangTypeAndAuto) => { + this.settings.lang = lang; + await this.saveSettings(); + }); + const t = (x: TransItemType, vars?: any) => this.i18n.t(x, vars); + + // NOTE: Credential expiry check removed in BYOC. + // Credentials persist until manually revoked. + // Token refresh is handled per-provider during sync. + + const vaultRandomIDFromOldConfigFile = await this.getVaultRandomIDFromOldConfigFile(); + this.tryToAddIgnoreFile(); + + const vaultBasePath = this.getVaultBasePath(); + + try { + await this.prepareDBAndVaultRandomID(vaultBasePath, vaultRandomIDFromOldConfigFile, profileID); + } catch (err: any) { + new Notice(err?.message ?? "error of prepareDBAndVaultRandomID", 10 * 1000); + throw err; + } + + this.enableAutoClearOutputToDBHistIfSet(); + this.enableAutoClearSyncPlanHist(); + + // Primary BYOC protocol handler + this.registerObsidianProtocolHandler(COMMAND_URI, async (inputParams) => { + const parsed = importQrCodeUri(inputParams, this.app.vault.getName()); + if (parsed.status === "error") { + new Notice(parsed.message); + } else { + const copied = cloneDeep(parsed.result); + this.settings = Object.assign({}, this.settings, copied); + this.saveSettings(); + new Notice(t("protocol_saveqr", { manifestName: this.manifest.name })); + } + }); + + // Legacy remotely-save:// URI support (for migration compatibility) + try { + this.registerObsidianProtocolHandler(COMMAND_URI_LEGACY, async (inputParams) => { + new Notice( + `[BYOC] Legacy remotely-save:// URI detected. Please update links to use bring-your-own-cloud:// instead.` + ); + }); + } catch (e) { + console.warn("[BYOC] Legacy protocol 'remotely-save' already registered (likely another plugin is active). Setup continuing."); + } + + this.registerObsidianProtocolHandler(COMMAND_CALLBACK, async (inputParams) => { + new Notice(t("protocol_callbacknotsupported", { params: JSON.stringify(inputParams) })); + }); + + // ─── Dropbox OAuth Callback ─────────────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_DROPBOX, async (inputParams) => { + if (inputParams.code !== undefined && this.oauth2Info?.verifier !== undefined) { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_dropbox_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } else { + new Notice(t("protocol_dropbox_no_modal")); + return; + } + + const authRes = await sendAuthReqDropbox( + this.settings.dropbox.clientID, + this.oauth2Info.verifier, + inputParams.code, + async (e: any) => { new Notice(t("protocol_dropbox_connect_fail")); new Notice(`${e}`); throw e; } + ); + + const self = this; + setConfigBySuccessfullAuthInplaceDropbox(this.settings.dropbox, authRes!, () => self.saveSettings()); + const client = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + const username = await client.getUserDisplayName(); + this.settings.dropbox.username = username; + await this.saveSettings(); + new Notice(t("protocol_dropbox_connect_succ", { username })); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("dropbox-auth-button-hide", this.settings.dropbox.username !== ""); + this.oauth2Info.authDiv = undefined; + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_dropbox_connect_succ_revoke", { username: this.settings.dropbox.username })); + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("dropbox-revoke-auth-button-hide", this.settings.dropbox.username === ""); + this.oauth2Info.revokeDiv = undefined; + } else { + new Notice(t("protocol_dropbox_connect_fail")); + throw new Error(t("protocol_dropbox_connect_unknown", { params: JSON.stringify(inputParams) })); + } + }); + + // ─── OneDrive AppFolder OAuth Callback ─────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_ONEDRIVE, async (inputParams) => { + if (inputParams.code !== undefined && this.oauth2Info?.verifier !== undefined) { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_onedrive_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const rsp = await sendAuthReqOnedrive( + this.settings.onedrive.clientID, + this.settings.onedrive.authority, + inputParams.code, + this.oauth2Info.verifier, + async (e: any) => { new Notice(t("protocol_onedrive_connect_fail")); new Notice(`${e}`); return; } + ); + if ((rsp as any).error !== undefined) { new Notice(`${JSON.stringify(rsp)}`); throw new Error(`${JSON.stringify(rsp)}`); } + const self = this; + setConfigBySuccessfullAuthInplaceOnedrive(this.settings.onedrive, rsp as AccessCodeResponseSuccessfulTypeOnedrive, () => self.saveSettings()); + const client = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + this.settings.onedrive.username = await client.getUserDisplayName(); + await this.saveSettings(); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("onedrive-auth-button-hide", this.settings.onedrive.username !== ""); + this.oauth2Info.authDiv = undefined; + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_onedrive_connect_succ_revoke", { username: this.settings.onedrive.username })); + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("onedrive-revoke-auth-button-hide", this.settings.onedrive.username === ""); + this.oauth2Info.revokeDiv = undefined; + } else { + new Notice(t("protocol_onedrive_connect_fail")); + throw new Error(t("protocol_onedrive_connect_unknown", { params: JSON.stringify(inputParams) })); + } + }); + + // ─── OneDrive Full OAuth Callback ───────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_ONEDRIVEFULL, async (inputParams) => { + if (inputParams.code !== undefined && this.oauth2Info?.verifier !== undefined) { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_onedrivefull_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const rsp = await sendAuthReqOnedriveFull( + this.settings.onedrivefull.clientID, + this.settings.onedrivefull.authority, + inputParams.code, + this.oauth2Info.verifier, + async (e: any) => { new Notice(t("protocol_onedrivefull_connect_fail")); new Notice(`${e}`); return; } + ); + if ((rsp as any).error !== undefined) { new Notice(`${JSON.stringify(rsp)}`); throw new Error(`${JSON.stringify(rsp)}`); } + const self = this; + setConfigBySuccessfullAuthInplaceOnedriveFull(this.settings.onedrivefull, rsp as AccessCodeResponseSuccessfulTypeOnedriveFull, () => self.saveSettings()); + const client = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + this.settings.onedrivefull.username = await client.getUserDisplayName(); + await this.saveSettings(); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("onedrivefull-auth-button-hide", this.settings.onedrivefull.username !== ""); + this.oauth2Info.authDiv = undefined; + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_onedrivefull_connect_succ_revoke", { username: this.settings.onedrivefull.username })); + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("onedrivefull-revoke-auth-button-hide", this.settings.onedrivefull.username === ""); + this.oauth2Info.revokeDiv = undefined; + } else { + new Notice(t("protocol_onedrivefull_connect_fail")); + throw new Error(t("protocol_onedrivefull_connect_unknown", { params: JSON.stringify(inputParams) })); + } + }); + + // ─── Box OAuth Callback ─────────────────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_BOX, async (inputParams) => { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_box_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const authRes = await sendAuthReqBox( + inputParams.code, + async (e: any) => { new Notice(t("protocol_box_connect_fail")); new Notice(`${e}`); throw e; } + ); + const self = this; + await setConfigBySuccessfullAuthInplaceBox(this.settings.box!, authRes, () => self.saveSettings()); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("box-auth-button-hide", this.settings.box?.refreshToken !== ""); + this.oauth2Info.authDiv = undefined; + try { + const boxClient = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + const username = await boxClient.getUserDisplayName(); + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_box_connect_succ_revoke", { username })); + } catch (_) { + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_box_connect_succ_revoke", { username: "Box user" })); + } + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("box-revoke-auth-button-hide", this.settings.box?.refreshToken === ""); + this.oauth2Info.revokeDiv = undefined; + }); + + // ─── pCloud OAuth Callback ──────────────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_PCLOUD, async (inputParams) => { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_pcloud_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const authRes = await sendAuthReqPCloud( + inputParams.hostname, + inputParams.code, + async (e: any) => { new Notice(t("protocol_pcloud_connect_fail")); new Notice(`${e}`); throw e; } + ); + const self = this; + await setConfigBySuccessfullAuthInplacePCloud( + this.settings.pcloud!, + inputParams as unknown as AuthAllowFirstResPCloud, + authRes, + () => self.saveSettings() + ); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("pcloud-auth-button-hide", this.settings.pcloud?.accessToken !== ""); + this.oauth2Info.authDiv = undefined; + // Fetch display name and inject "Logged in as" row into the revoke div + try { + const pcloudClient = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + const username = await pcloudClient.getUserDisplayName(); + this.settings.pcloud!.username = username; + await this.saveSettings(); + // Prepend a read-only "Logged in as" row at the top of the revoke div + if (this.oauth2Info.revokeDiv) { + const loggedInSetting = new Setting(this.oauth2Info.revokeDiv); + loggedInSetting.setName("Logged in as").setDesc(username); + // Move it before the revoke button + this.oauth2Info.revokeDiv.prepend(loggedInSetting.settingEl); + } + } catch (_) { /* username display is non-critical */ } + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("pcloud-revoke-auth-button-hide", this.settings.pcloud?.accessToken === ""); + this.oauth2Info.revokeDiv = undefined; + }); + + // ─── Yandex Disk OAuth Callback ─────────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_YANDEXDISK, async (inputParams) => { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_yandexdisk_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const authRes = await sendAuthReqYandexDisk( + inputParams.code, + async (e: any) => { new Notice(t("protocol_yandexdisk_connect_fail")); new Notice(`${e}`); throw e; } + ); + const self = this; + await setConfigBySuccessfullAuthInplaceYandexDisk(this.settings.yandexdisk!, authRes, () => self.saveSettings()); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("yandexdisk-auth-button-hide", this.settings.yandexdisk?.refreshToken !== ""); + this.oauth2Info.authDiv = undefined; + try { + const yandexClient = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + const username = await yandexClient.getUserDisplayName(); + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_yandexdisk_connect_succ_revoke", { username })); + } catch (_) { + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_yandexdisk_connect_succ_revoke", { username: "Yandex user" })); + } + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("yandexdisk-revoke-auth-button-hide", this.settings.yandexdisk?.refreshToken === ""); + this.oauth2Info.revokeDiv = undefined; + }); + + // ─── Koofr OAuth Callback ───────────────────────────────────────────────── + this.registerObsidianProtocolHandler(COMMAND_CALLBACK_KOOFR, async (inputParams) => { + if (this.oauth2Info.helperModal !== undefined) { + const k = this.oauth2Info.helperModal.contentEl; + k.empty(); + t("protocol_koofr_connecting").split("\n").forEach((val) => k.createEl("p", { text: val })); + } + const authRes = await sendAuthReqKoofr( + this.settings.koofr.api, + inputParams.code, + async (e: any) => { new Notice(t("protocol_koofr_connect_fail")); new Notice(`${e}`); throw e; }, + true + ); + const self = this; + await setConfigBySuccessfullAuthInplaceKoofr(this.settings.koofr!, authRes!, () => self.saveSettings()); + this.oauth2Info.verifier = ""; + this.oauth2Info.helperModal?.close(); + this.oauth2Info.helperModal = undefined; + this.oauth2Info.authDiv?.toggleClass("koofr-auth-button-hide", this.settings.koofr?.refreshToken !== ""); + this.oauth2Info.authDiv = undefined; + try { + const koofrClient = getClient(this.settings, this.app.vault.getName(), () => self.saveSettings()); + const username = await koofrClient.getUserDisplayName(); + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_koofr_connect_succ_revoke", { username })); + } catch (_) { + this.oauth2Info.revokeAuthSetting?.setDesc(t("protocol_koofr_connect_succ_revoke", { username: "Koofr user" })); + } + this.oauth2Info.revokeAuthSetting = undefined; + this.oauth2Info.revokeDiv?.toggleClass("koofr-revoke-auth-button-hide", this.settings.koofr?.refreshToken === ""); + this.oauth2Info.revokeDiv = undefined; + }); + + // ─── UI ─────────────────────────────────────────────────────────────────── + + this.syncRibbon = this.addRibbonIcon( + iconNameSyncWait, + `${this.manifest.name}`, + async () => this.syncRun("manual") + ); + + this.enableMobileStatusBarIfSet(); + + if ( + (!Platform.isMobile || (Platform.isMobile && this.settings.enableMobileStatusBar)) && + this.settings.enableStatusBarInfo === true + ) { + const statusBarItem = this.addStatusBarItem(); + this.statusBarElement = statusBarItem.createEl("span"); + this.statusBarElement.setAttribute("data-tooltip-position", "top"); + + if (!this.isSyncing) { + this.updateLastSyncMsg( + undefined, + "not_syncing", + await getLastSuccessSyncTimeByVault(this.db, this.vaultRandomID), + await getLastFailedSyncTimeByVault(this.db, this.vaultRandomID) + ); + } + + this.registerInterval( + window.setInterval(async () => { + if (!this.isSyncing) { + this.updateLastSyncMsg( + undefined, + "not_syncing", + await getLastSuccessSyncTimeByVault(this.db, this.vaultRandomID), + await getLastFailedSyncTimeByVault(this.db, this.vaultRandomID) + ); + } + }, 1000 * 30) + ); + } + + this.addCommand({ id: "start-sync", name: t("command_startsync"), icon: iconNameSyncWait, callback: async () => this.syncRun("manual") }); + this.addCommand({ id: "start-sync-dry-run", name: t("command_drynrun"), icon: iconNameSyncWait, callback: async () => this.syncRun("dry") }); + this.addCommand({ id: "export-sync-plans-1-only-change", name: t("command_exportsyncplans_1_only_change"), icon: iconNameLogs, callback: async () => { await exportVaultSyncPlansToFiles(this.db, this.app.vault, this.vaultRandomID, 1, true); new Notice(t("settings_syncplans_notice")); } }); + this.addCommand({ id: "export-sync-plans-1", name: t("command_exportsyncplans_1"), icon: iconNameLogs, callback: async () => { await exportVaultSyncPlansToFiles(this.db, this.app.vault, this.vaultRandomID, 1, false); new Notice(t("settings_syncplans_notice")); } }); + this.addCommand({ id: "export-sync-plans-5", name: t("command_exportsyncplans_5"), icon: iconNameLogs, callback: async () => { await exportVaultSyncPlansToFiles(this.db, this.app.vault, this.vaultRandomID, 5, false); new Notice(t("settings_syncplans_notice")); } }); + this.addCommand({ id: "export-sync-plans-all", name: t("command_exportsyncplans_all"), icon: iconNameLogs, callback: async () => { await exportVaultSyncPlansToFiles(this.db, this.app.vault, this.vaultRandomID, -1, false); new Notice(t("settings_syncplans_notice")); } }); + + this.addSettingTab(new BYOCSettingTab(this.app, this)); + + this.enableCheckingFileStat(); + + // BYOC: All users use v3 sync algorithm — no consent modal shown + this.enableAutoSyncIfSet(); + this.enableInitSyncIfSet(); + this.toggleSyncOnSaveIfSet(); + + const { oldVersion } = await upsertPluginVersionByVault(this.db, this.vaultRandomID, this.manifest.version); + } + + async onunload() { + console.info(`unloading plugin ${this.manifest.id}`); + this.syncRibbon = undefined; + if (this.appContainerObserver !== undefined) { + this.appContainerObserver.disconnect(); + this.appContainerObserver = undefined; + } + if (this.oauth2Info !== undefined) { + this.oauth2Info.helperModal = undefined; + this.oauth2Info = { verifier: "", helperModal: undefined, authDiv: undefined, revokeDiv: undefined, revokeAuthSetting: undefined }; + } + } + + async loadSettings() { + this.settings = Object.assign( + {}, + cloneDeep(DEFAULT_SETTINGS), + messyConfigToNormal(await this.loadData()) + ); + + // Ensure all provider configs exist + if (!this.settings.googledrive) this.settings.googledrive = DEFAULT_GOOGLEDRIVE_CONFIG; + if (!this.settings.box) this.settings.box = DEFAULT_BOX_CONFIG; + if (!this.settings.pcloud) this.settings.pcloud = DEFAULT_PCLOUD_CONFIG; + if (!this.settings.yandexdisk) this.settings.yandexdisk = DEFAULT_YANDEXDISK_CONFIG; + if (!this.settings.koofr) this.settings.koofr = DEFAULT_KOOFR_CONFIG; + if (!this.settings.azureblobstorage) this.settings.azureblobstorage = DEFAULT_AZUREBLOBSTORAGE_CONFIG; + if (!this.settings.onedrivefull) this.settings.onedrivefull = DEFAULT_ONEDRIVEFULL_CONFIG; + + // Dropbox defaults + if (this.settings.dropbox.clientID === "") this.settings.dropbox.clientID = DEFAULT_SETTINGS.dropbox.clientID; + if (this.settings.dropbox.remoteBaseDir === undefined) this.settings.dropbox.remoteBaseDir = ""; + + // OneDrive defaults + if (this.settings.onedrive.clientID === "") this.settings.onedrive.clientID = DEFAULT_SETTINGS.onedrive.clientID; + if (this.settings.onedrive.authority === "") this.settings.onedrive.authority = DEFAULT_SETTINGS.onedrive.authority; + if (this.settings.onedrive.remoteBaseDir === undefined) this.settings.onedrive.remoteBaseDir = ""; + if (this.settings.onedrive.emptyFile === undefined) this.settings.onedrive.emptyFile = "skip"; + if (this.settings.onedrive.kind === undefined) this.settings.onedrive.kind = "onedrive"; + + // WebDAV defaults + if (this.settings.webdav.manualRecursive === undefined) this.settings.webdav.manualRecursive = true; + if (this.settings.webdav.depth === undefined || ["auto","auto_1","auto_infinity","auto_unknown"].includes(this.settings.webdav.depth)) { + this.settings.webdav.depth = "manual_1"; + this.settings.webdav.manualRecursive = true; + } + if (this.settings.webdav.remoteBaseDir === undefined) this.settings.webdav.remoteBaseDir = ""; + if (this.settings.webdav.customHeaders === undefined) this.settings.webdav.customHeaders = ""; + + // S3 defaults + if (this.settings.s3.partsConcurrency === undefined) this.settings.s3.partsConcurrency = 20; + if (this.settings.s3.forcePathStyle === undefined) this.settings.s3.forcePathStyle = false; + if (this.settings.s3.remotePrefix === undefined) this.settings.s3.remotePrefix = ""; + if (this.settings.s3.useAccurateMTime === undefined) this.settings.s3.useAccurateMTime = false; + if (this.settings.s3.generateFolderObject === undefined) this.settings.s3.generateFolderObject = false; + + // General defaults + if (this.settings.ignorePaths === undefined) this.settings.ignorePaths = []; + if (this.settings.onlyAllowPaths === undefined) this.settings.onlyAllowPaths = []; + if (this.settings.enableStatusBarInfo === undefined) this.settings.enableStatusBarInfo = true; + if (this.settings.syncOnSaveAfterMilliseconds === undefined) this.settings.syncOnSaveAfterMilliseconds = -1; + if (this.settings.deleteToWhere === undefined) this.settings.deleteToWhere = "system"; + if (this.settings.syncBookmarks === undefined) this.settings.syncBookmarks = false; + this.settings.logToDB = false; // deprecated + if (requireApiVersion(API_VER_ENSURE_REQURL_OK)) this.settings.s3.bypassCorsLocally = true; + if (this.settings.agreeToUseSyncV3 === undefined) this.settings.agreeToUseSyncV3 = true; // BYOC: always true + if (this.settings.conflictAction === undefined) this.settings.conflictAction = "keep_newer"; + if (this.settings.howToCleanEmptyFolder === undefined) this.settings.howToCleanEmptyFolder = "clean_both"; + if (this.settings.protectModifyPercentage === undefined) this.settings.protectModifyPercentage = 50; + if (this.settings.syncDirection === undefined) this.settings.syncDirection = "bidirectional"; + if (this.settings.obfuscateSettingFile === undefined) this.settings.obfuscateSettingFile = true; + if (this.settings.enableMobileStatusBar === undefined) this.settings.enableMobileStatusBar = false; + + if (this.settings.encryptionMethod === undefined || this.settings.encryptionMethod === "unknown") { + this.settings.encryptionMethod = (!this.settings.password) ? "rclone-base64" : "openssl-base64"; + } + + if (this.settings.profiler === undefined) this.settings.profiler = DEFAULT_PROFILER_CONFIG; + if (this.settings.profiler.enable === undefined) this.settings.profiler.enable = false; + if (this.settings.profiler.enablePrinting === undefined) this.settings.profiler.enablePrinting = false; + if (this.settings.profiler.recordSize === undefined) this.settings.profiler.recordSize = false; + + // Remove pro field if it survived migration + if ("pro" in this.settings) delete (this.settings as any).pro; + + await this.saveSettings(); + } + + async saveSettings() { + if (this.settings.obfuscateSettingFile) { + await this.saveData(normalConfigToMessy(this.settings)); + } else { + await this.saveData(this.settings); + } + } + + getCurrProfileID() { + if (this.settings.serviceType !== undefined) { + return `${this.settings.serviceType}-default-1`; + } + throw new Error("unknown serviceType in the setting!"); + } + + async getVaultRandomIDFromOldConfigFile() { + let vaultRandomID = ""; + if (this.settings.vaultRandomID !== undefined) { + if (this.settings.vaultRandomID !== "") vaultRandomID = this.settings.vaultRandomID; + console.debug("vaultRandomID is no longer saved in data.json"); + delete this.settings.vaultRandomID; + await this.saveSettings(); + } + return vaultRandomID; + } + + async trash(x: string) { + if (this.settings.deleteToWhere === "obsidian") { + await this.app.vault.adapter.trashLocal(x); + } else { + if (!(await this.app.vault.adapter.trashSystem(x))) { + await this.app.vault.adapter.trashLocal(x); + } + } + } + + getVaultBasePath() { + if (this.app.vault.adapter instanceof FileSystemAdapter) { + return this.app.vault.adapter.getBasePath().split("?")[0]; + } else { + return this.app.vault.adapter.getResourcePath("").split("?")[0]; + } + } + + async prepareDBAndVaultRandomID(vaultBasePath: string, vaultRandomIDFromOldConfigFile: string, profileID: string) { + const { db, vaultRandomID } = await prepareDBs(vaultBasePath, vaultRandomIDFromOldConfigFile, profileID); + this.db = db; + this.vaultRandomID = vaultRandomID; + } + + enableAutoSyncIfSet() { + if (this.settings.autoRunEveryMilliseconds != null && this.settings.autoRunEveryMilliseconds > 0) { + this.app.workspace.onLayoutReady(() => { + const intervalID = window.setInterval(() => this.syncRun("auto"), this.settings.autoRunEveryMilliseconds); + this.autoRunIntervalID = intervalID; + this.registerInterval(intervalID); + }); + } + } + + enableInitSyncIfSet() { + if (this.settings.initRunAfterMilliseconds != null && this.settings.initRunAfterMilliseconds > 0) { + this.app.workspace.onLayoutReady(() => { + window.setTimeout(() => this.syncRun("auto_once_init"), this.settings.initRunAfterMilliseconds); + }); + } + } + + async _checkCurrFileModified(caller: "SYNC" | "FILE_CHANGES") { + const currentFile = this.app.workspace.getActiveFile(); + if (currentFile) { + const lastModified = currentFile.stat.mtime; + const lastSuccessSyncMillis = await getLastSuccessSyncTimeByVault(this.db, this.vaultRandomID); + if (caller === "SYNC" || (caller === "FILE_CHANGES" && lastModified > (lastSuccessSyncMillis ?? 1))) { + if (this.isSyncing) { this.hasPendingSyncOnSave = true; return; } + else if (this.hasPendingSyncOnSave || caller === "FILE_CHANGES") { + this.hasPendingSyncOnSave = false; + await this.syncRun("auto_sync_on_save"); + } + } + } + } + + _syncOnSaveEvent1 = () => { this._checkCurrFileModified("SYNC"); }; + _syncOnSaveEvent2 = throttle(async () => { await this._checkCurrFileModified("FILE_CHANGES"); }, 1000 * 3, { leading: false, trailing: true }); + + toggleSyncOnSaveIfSet() { + if (this.settings.syncOnSaveAfterMilliseconds != null && this.settings.syncOnSaveAfterMilliseconds > 0) { + this.app.workspace.onLayoutReady(() => { + this.registerEvent(this.syncEvent?.on("SYNC_DONE", this._syncOnSaveEvent1)!); + this.registerEvent(this.app.vault.on("modify", this._syncOnSaveEvent2)); + this.registerEvent(this.app.vault.on("create", this._syncOnSaveEvent2)); + this.registerEvent(this.app.vault.on("delete", this._syncOnSaveEvent2)); + this.registerEvent(this.app.vault.on("rename", this._syncOnSaveEvent2)); + }); + } else { + this.syncEvent?.off("SYNC_DONE", this._syncOnSaveEvent1); + this.app.vault.off("modify", this._syncOnSaveEvent2); + this.app.vault.off("create", this._syncOnSaveEvent2); + this.app.vault.off("delete", this._syncOnSaveEvent2); + this.app.vault.off("rename", this._syncOnSaveEvent2); + } + } + + enableMobileStatusBarIfSet() { + this.app.workspace.onLayoutReady(() => { + if (Platform.isMobile && this.settings.enableMobileStatusBar) { + this.appContainerObserver = changeMobileStatusBar("enable"); + } + }); + } + + enableCheckingFileStat() { + this.app.workspace.onLayoutReady(() => { + const t = (x: TransItemType, vars?: any) => this.i18n.t(x, vars); + this.registerEvent( + this.app.workspace.on("file-menu", (menu, file) => { + if (file instanceof TFolder) return; + menu.addItem((item) => { + item.setTitle(t("menu_check_file_stat")).setIcon("file-cog").onClick(async () => { + const fsLocal = new FakeFsLocal( + this.app.vault, this.settings.syncConfigDir ?? false, this.settings.syncBookmarks ?? false, + this.app.vault.configDir, this.manifest.id, undefined, this.settings.deleteToWhere ?? "system" + ); + const s = await fsLocal.stat(file.path); + new Notice(JSON.stringify(s, null, 2), 10000); + }); + }); + }) + ); + }); + } + + async saveAgreeToUseNewSyncAlgorithm() { + this.settings.agreeToUseSyncV3 = true; + await this.saveSettings(); + } + + setCurrSyncMsg( + t: (x: TransItemType, vars?: any) => string, + s: SyncTriggerSourceType, + i: number, + totalCount: number, + pathName: string, + decision: string, + triggerSource: SyncTriggerSourceType + ) { + const L = `${totalCount}`.length; + const iStr = `${i}`.padStart(L, "0"); + const prefix = getStatusBarShortMsgFromSyncSource(t, s); + const shortMsg = prefix + `Syncing ${iStr}/${totalCount}`; + const longMsg = prefix + `Syncing progress=${iStr}/${totalCount},decision=${decision},path=${pathName},source=${triggerSource}`; + this.currSyncMsg = longMsg; + if (this.statusBarElement !== undefined) { + this.statusBarElement.setText(shortMsg); + this.statusBarElement.setAttribute("aria-label", longMsg); + } + } + + updateLastSyncMsg( + s: SyncTriggerSourceType | undefined, + syncStatus: "not_syncing" | "syncing", + lastSuccessSyncMillis: number | null | undefined, + lastFailedSyncMillis: number | null | undefined + ) { + if (this.statusBarElement === undefined) return; + const t = (x: TransItemType, vars?: any) => this.i18n.t(x, vars); + + let lastSyncMsg = t("statusbar_lastsync_never"); + let lastSyncLabelMsg = t("statusbar_lastsync_never_label"); + + const inputTs = Math.max(lastSuccessSyncMillis ?? -999, lastFailedSyncMillis ?? -999); + const isSuccess = (lastSuccessSyncMillis ?? -999) >= (lastFailedSyncMillis ?? -999); + + if (syncStatus === "syncing") { + lastSyncMsg = getStatusBarShortMsgFromSyncSource(t, s!) + t("statusbar_syncing"); + } else if (inputTs > 0) { + const prefix = isSuccess ? t("statusbar_sync_status_prefix_success") : t("statusbar_sync_status_prefix_failed"); + const deltaTime = Date.now() - inputTs; + const years = Math.floor(deltaTime / 31556952000); + const months = Math.floor(deltaTime / 2629746000); + const weeks = Math.floor(deltaTime / 604800000); + const days = Math.floor(deltaTime / 86400000); + const hours = Math.floor(deltaTime / 3600000); + const minutes = Math.floor(deltaTime / 60000); + const seconds = Math.floor(deltaTime / 1000); + let timeText = years > 0 ? t("statusbar_time_years", { time: years }) : + months > 0 ? t("statusbar_time_months", { time: months }) : + weeks > 0 ? t("statusbar_time_weeks", { time: weeks }) : + days > 0 ? t("statusbar_time_days", { time: days }) : + hours > 0 ? t("statusbar_time_hours", { time: hours }) : + minutes > 0 ? t("statusbar_time_minutes", { time: minutes }) : + seconds > 30 ? t("statusbar_time_lessminute") : + t("statusbar_time_now"); + const dateText = new Date(inputTs).toLocaleTimeString(navigator.language, { weekday: "long", year: "numeric", month: "long", day: "numeric" }); + lastSyncMsg = prefix + timeText; + lastSyncLabelMsg = prefix + t("statusbar_lastsync_label", { date: dateText }); + } + + this.statusBarElement.setText(lastSyncMsg); + this.statusBarElement.setAttribute("aria-label", lastSyncLabelMsg); + } + + async tryToAddIgnoreFile() { + const pluginConfigDir = this.manifest.dir || `${this.app.vault.configDir}/plugins/${this.manifest.id}`; + const pluginConfigDirExists = await this.app.vault.adapter.exists(pluginConfigDir); + if (!pluginConfigDirExists) return; + const ignoreFile = `${pluginConfigDir}/.gitignore`; + const ignoreFileExists = await this.app.vault.adapter.exists(ignoreFile); + if (!ignoreFileExists) { + try { this.app.vault.adapter.write(ignoreFile, "data.json\n"); } catch (_) {} + } + } + + enableAutoClearOutputToDBHistIfSet() { + this.app.workspace.onLayoutReady(() => { + window.setTimeout(() => clearAllLoggerOutputRecords(this.db), 1000 * 30); + }); + } + + enableAutoClearSyncPlanHist() { + this.app.workspace.onLayoutReady(() => { + window.setTimeout(() => clearExpiredSyncPlanRecords(this.db), 1000 * 45); + const intervalID = window.setInterval(() => clearExpiredSyncPlanRecords(this.db), 1000 * 60 * 5); + this.registerInterval(intervalID); + }); + } +} diff --git a/src/metadataOnRemote.ts b/src/metadataOnRemote.ts new file mode 100644 index 0000000..b47a883 --- /dev/null +++ b/src/metadataOnRemote.ts @@ -0,0 +1,112 @@ +import isEqual from "lodash/isEqual"; +import { base64url } from "rfc4648"; +import { reverseString } from "./misc"; + +const DEFAULT_README_FOR_METADATAONREMOTE = + "Do NOT edit or delete the file manually. This file is for the plugin remotely-save to store some necessary meta data on the remote services. Its content is slightly obfuscated."; + +const DEFAULT_VERSION_FOR_METADATAONREMOTE = "20220220"; + +export const DEFAULT_FILE_NAME_FOR_METADATAONREMOTE = + "_remotely-save-metadata-on-remote.json"; + +export const DEFAULT_FILE_NAME_FOR_METADATAONREMOTE2 = + "_remotely-save-metadata-on-remote.bin"; + +export interface DeletionOnRemote { + key: string; + actionWhen: number; +} + +export interface MetadataOnRemote { + version?: string; + generatedWhen?: number; + deletions?: DeletionOnRemote[]; +} + +export const isEqualMetadataOnRemote = ( + a: MetadataOnRemote | undefined, + b: MetadataOnRemote | undefined +) => { + const m1 = a === undefined ? { deletions: [] } : a; + const m2 = b === undefined ? { deletions: [] } : b; + + // we only need to compare deletions + const d1 = m1.deletions === undefined ? [] : m1.deletions; + const d2 = m2.deletions === undefined ? [] : m2.deletions; + return isEqual(d1, d2); +}; + +export const serializeMetadataOnRemote = (x: MetadataOnRemote) => { + const y = x; + + if (y["version"] === undefined) { + y["version"] === DEFAULT_VERSION_FOR_METADATAONREMOTE; + } + if (y["generatedWhen"] === undefined) { + y["generatedWhen"] = Date.now(); + } + if (y["deletions"] === undefined) { + y["deletions"] = []; + } + + const z = { + readme: DEFAULT_README_FOR_METADATAONREMOTE, + d: reverseString( + base64url.stringify(Buffer.from(JSON.stringify(x), "utf-8"), { + pad: false, + }) + ), + }; + + return JSON.stringify(z, null, 2); +}; + +export const deserializeMetadataOnRemote = (x: string | ArrayBuffer) => { + let y1 = ""; + if (typeof x === "string") { + y1 = x; + } else { + y1 = new TextDecoder().decode(x); + } + + let y2: any; + try { + y2 = JSON.parse(y1); + } catch (e) { + throw new Error( + `invalid remote meta data file with first few chars: ${y1.slice(0, 5)}` + ); + } + + if (!("readme" in y2 && "d" in y2)) { + throw new Error( + 'invalid remote meta data file (no "readme" or "d" fields)!' + ); + } + + let y3: string; + try { + y3 = ( + base64url.parse(reverseString(y2["d"]), { + out: Buffer.allocUnsafe as any, + loose: true, + }) as Buffer + ).toString("utf-8"); + } catch (e) { + throw new Error('invalid remote meta data file (invalid "d" field)!'); + } + + let y4: MetadataOnRemote; + try { + y4 = JSON.parse(y3) as MetadataOnRemote; + } catch (e) { + throw new Error( + `invalid remote meta data file with \"d\" field with first few chars: ${y3.slice( + 0, + 5 + )}` + ); + } + return y4; +}; diff --git a/src/migration.ts b/src/migration.ts new file mode 100644 index 0000000..b35b613 --- /dev/null +++ b/src/migration.ts @@ -0,0 +1,337 @@ +/** + * BYOC — Data Migration Module + * + * Handles seamless migration from `remotely-save` plugin configs + * to the BYOC format. Runs once on first load, backs up original + * data, and strips all credential expiry timers. + * + * Migration version history: + * 0 → 1: Initial migration from remotely-save format + */ + +import cloneDeep from "lodash/cloneDeep"; +import { Notice, type Plugin } from "obsidian"; +import type { BYOCPluginSettings } from "./baseTypes"; +import { messyConfigToNormal } from "./configPersist"; + +/** Current migration version — bump when adding new migrations */ +export const CURRENT_MIGRATION_VERSION = 1; + +/** The old plugin's data directory name */ +const LEGACY_PLUGIN_ID = "remotely-save"; + +/** + * Checks if a legacy `remotely-save` plugin folder exists in the vault, + * reads its data.json, decodes it, and returns the parsed settings. + * Returns null if no legacy data found or if it can't be parsed. + */ +async function readLegacyConfig( + plugin: Plugin +): Promise | null> { + const legacyDataPath = `${plugin.app.vault.configDir}/plugins/${LEGACY_PLUGIN_ID}/data.json`; + + try { + const exists = await plugin.app.vault.adapter.exists(legacyDataPath); + if (!exists) { + console.debug("[BYOC Migration] No legacy remotely-save data.json found."); + return null; + } + + const raw = await plugin.app.vault.adapter.read(legacyDataPath); + const parsed = JSON.parse(raw); + + // The legacy plugin uses the same messy encoding (base64url reversed) + const decoded = messyConfigToNormal(parsed); + if (!decoded) { + console.warn("[BYOC Migration] Legacy data.json decoded to null."); + return null; + } + + console.info("[BYOC Migration] Successfully read legacy remotely-save config."); + return decoded as Record; + } catch (err) { + console.warn("[BYOC Migration] Failed to read legacy config:", err); + return null; + } +} + +/** + * Creates a backup of the current BYOC data.json before migration. + * The backup is written to data.json.byoc-backup in the BYOC plugin dir. + */ +async function backupCurrentConfig(plugin: Plugin): Promise { + const pluginDir = + plugin.manifest.dir || + `${plugin.app.vault.configDir}/plugins/${plugin.manifest.id}`; + const dataPath = `${pluginDir}/data.json`; + const backupPath = `${pluginDir}/data.json.byoc-backup`; + + try { + const exists = await plugin.app.vault.adapter.exists(dataPath); + if (exists) { + const raw = await plugin.app.vault.adapter.read(dataPath); + await plugin.app.vault.adapter.write(backupPath, raw); + console.info(`[BYOC Migration] Backed up data.json → ${backupPath}`); + } + } catch (err) { + console.warn("[BYOC Migration] Failed to create backup:", err); + } +} + +/** + * Strips all credential expiry timers from provider configs. + * The old plugin had an 80-day nuke that force-deleted tokens. + * BYOC keeps credentials indefinitely until manually revoked. + */ +function stripCredentialExpiry(settings: BYOCPluginSettings): void { + // Dropbox + if (settings.dropbox?.credentialsShouldBeDeletedAtTime) { + delete settings.dropbox.credentialsShouldBeDeletedAtTime; + } + + // OneDrive + if (settings.onedrive?.credentialsShouldBeDeletedAtTime) { + delete settings.onedrive.credentialsShouldBeDeletedAtTime; + } + + // OneDrive Full + if (settings.onedrivefull?.credentialsShouldBeDeletedAtTime) { + delete settings.onedrivefull.credentialsShouldBeDeletedAtTime; + } + + // Google Drive + if (settings.googledrive?.credentialsShouldBeDeletedAtTimeMs) { + delete settings.googledrive.credentialsShouldBeDeletedAtTimeMs; + } + + // Box + if (settings.box?.credentialsShouldBeDeletedAtTimeMs) { + delete settings.box.credentialsShouldBeDeletedAtTimeMs; + } + + // pCloud + if (settings.pcloud?.credentialsShouldBeDeletedAtTimeMs) { + delete settings.pcloud.credentialsShouldBeDeletedAtTimeMs; + } + + // Yandex Disk + if (settings.yandexdisk?.credentialsShouldBeDeletedAtTimeMs) { + delete settings.yandexdisk.credentialsShouldBeDeletedAtTimeMs; + } + + // Koofr + if (settings.koofr?.credentialsShouldBeDeletedAtTimeMs) { + delete settings.koofr.credentialsShouldBeDeletedAtTimeMs; + } +} + +/** + * Extracts provider configs that lived under the `pro` key in the + * legacy plugin and promotes them to top-level fields in BYOC settings. + */ +function extractProConfigs( + legacyConfig: Record, + settings: BYOCPluginSettings +): void { + const pro = legacyConfig.pro; + if (!pro || typeof pro !== "object") return; + + // The pro object could contain nested provider configs + // Map known pro fields to their BYOC top-level equivalents + const providerMap: Record = { + googledrive: "googledrive", + box: "box", + pcloud: "pcloud", + yandexdisk: "yandexdisk", + koofr: "koofr", + azureblobstorage: "azureblobstorage", + onedrivefull: "onedrivefull", + }; + + for (const [proKey, settingsKey] of Object.entries(providerMap)) { + if (pro[proKey] && typeof pro[proKey] === "object") { + // Only merge if the current config is empty/default + const current = settings[settingsKey] as Record | undefined; + const incoming = pro[proKey] as Record; + + // Check if incoming has real data (e.g., a non-empty accessToken) + const hasRealData = + incoming.accessToken || + incoming.refreshToken || + incoming.containerSasUrl; + + if (hasRealData) { + (settings as any)[settingsKey] = { + ...((current as any) || {}), + ...cloneDeep(incoming), + }; + console.info( + `[BYOC Migration] Promoted pro.${proKey} config to top-level.` + ); + } + } + } + + // Also check if pro had feature flags we can learn from + if (pro.enabledProFeatures && Array.isArray(pro.enabledProFeatures)) { + console.info( + `[BYOC Migration] Legacy pro features detected: ${pro.enabledProFeatures.join(", ")}. All features enabled in BYOC by default.` + ); + } +} + +/** + * Merges legacy remotely-save settings into the current BYOC settings. + * Preserves existing BYOC values — only fills in fields that are + * empty/default in BYOC but have real data in the legacy config. + */ +function mergeLegacyIntoSettings( + legacyConfig: Record, + settings: BYOCPluginSettings +): void { + // Core provider configs that exist at top level in both old and new + const directProviders = [ + "s3", + "webdav", + "dropbox", + "onedrive", + "webdis", + ] as const; + + for (const provider of directProviders) { + const legacy = legacyConfig[provider]; + if (!legacy || typeof legacy !== "object") continue; + + const current = settings[provider] as Record; + if (!current) continue; + + // Check if legacy has meaningful credentials + const hasCredentials = + legacy.accessToken || + legacy.refreshToken || + legacy.s3AccessKeyID || + legacy.address || + legacy.password; + + if (hasCredentials) { + // Merge legacy into current, legacy values fill gaps + for (const [key, value] of Object.entries(legacy)) { + if ( + value !== undefined && + value !== null && + value !== "" && + (current[key] === undefined || + current[key] === null || + current[key] === "") + ) { + current[key] = cloneDeep(value); + } + } + console.info( + `[BYOC Migration] Merged legacy ${provider} credentials.` + ); + } + } + + // Scalar settings — only copy if BYOC has the default value + const scalarFields: Array<{ + key: keyof BYOCPluginSettings; + defaultVal: any; + }> = [ + { key: "serviceType", defaultVal: "s3" }, + { key: "password", defaultVal: "" }, + { key: "currLogLevel", defaultVal: "info" }, + { key: "autoRunEveryMilliseconds", defaultVal: -1 }, + { key: "initRunAfterMilliseconds", defaultVal: -1 }, + { key: "syncOnSaveAfterMilliseconds", defaultVal: -1 }, + { key: "concurrency", defaultVal: 5 }, + { key: "syncConfigDir", defaultVal: false }, + { key: "syncBookmarks", defaultVal: false }, + { key: "skipSizeLargerThan", defaultVal: -1 }, + { key: "ignorePaths", defaultVal: [] }, + { key: "onlyAllowPaths", defaultVal: [] }, + { key: "deleteToWhere", defaultVal: "system" }, + { key: "conflictAction", defaultVal: "keep_newer" }, + { key: "protectModifyPercentage", defaultVal: 50 }, + { key: "syncDirection", defaultVal: "bidirectional" }, + { key: "encryptionMethod", defaultVal: "unknown" }, + ]; + + for (const { key, defaultVal } of scalarFields) { + const legacyVal = legacyConfig[key]; + const currentVal = settings[key]; + + if (legacyVal !== undefined && legacyVal !== null) { + // Only overwrite if BYOC still has the default + const isDefault = + Array.isArray(defaultVal) + ? Array.isArray(currentVal) && (currentVal as any[]).length === 0 + : currentVal === defaultVal; + + if (isDefault) { + (settings as any)[key] = cloneDeep(legacyVal); + } + } + } + + // Extract pro-gated provider configs + extractProConfigs(legacyConfig, settings); +} + +/** + * Main migration entry point. Called from plugin.onload() AFTER + * loadSettings() has already run. This means settings already has + * defaults applied — we're layering legacy data on top. + * + * Returns true if migration was performed. + */ +export async function runMigration( + plugin: Plugin, + settings: BYOCPluginSettings +): Promise { + const currentVersion = settings.migrationVersion ?? 0; + + if (currentVersion >= CURRENT_MIGRATION_VERSION) { + // Already migrated, nothing to do + return false; + } + + console.info( + `[BYOC Migration] Running migration v${currentVersion} → v${CURRENT_MIGRATION_VERSION}` + ); + + // Step 1: Backup existing BYOC data.json + await backupCurrentConfig(plugin); + + // Step 2: Read legacy remotely-save config + const legacyConfig = await readLegacyConfig(plugin); + + if (legacyConfig) { + // Step 3: Merge legacy data into current settings + mergeLegacyIntoSettings(legacyConfig, settings); + + // Step 4: Strip all credential expiry timers + stripCredentialExpiry(settings); + + // Step 5: Clean up deprecated pro field + if ("pro" in settings) { + delete (settings as any).pro; + } + + new Notice( + "[BYOC] Migrated settings from Remotely Save. Your old config is backed up.", + 8000 + ); + console.info("[BYOC Migration] Migration from remotely-save complete."); + } else { + // No legacy config found, but still strip expiry timers + // in case user manually copied settings + stripCredentialExpiry(settings); + console.info("[BYOC Migration] No legacy config found. Applied defaults."); + } + + // Step 6: Stamp migration version + settings.migrationVersion = CURRENT_MIGRATION_VERSION; + + return true; +} diff --git a/src/misc.ts b/src/misc.ts new file mode 100644 index 0000000..8477317 --- /dev/null +++ b/src/misc.ts @@ -0,0 +1,886 @@ +import * as path from "path"; +import type { Vault } from "obsidian"; + +import emojiRegex from "emoji-regex"; +import { base32 } from "rfc4648"; +import XRegExp from "xregexp"; + +declare global { + interface Window { + moment: (...data: any) => any; + } +} + +/** + * If any part of the file starts with '.' or '_' then it's a hidden file. + * @param item + * @param dot + * @param underscore + * @returns + */ +export const isHiddenPath = (item: string, dot = true, underscore = true) => { + if (!(dot || underscore)) { + throw Error("parameter error for isHiddenPath"); + } + const k = path.posix.normalize(item); // TODO: only unix path now + const k2 = k.split("/"); // TODO: only unix path now + // console.info(k2) + for (const singlePart of k2) { + if (singlePart === "." || singlePart === ".." || singlePart === "") { + continue; + } + if (dot && singlePart[0] === ".") { + return true; + } + if (underscore && singlePart[0] === "_") { + return true; + } + } + return false; +}; + +/** + * Util func for mkdir -p based on the "path" of original file or folder + * "a/b/c/" => ["a", "a/b", "a/b/c"] + * "a/b/c/d/e.txt" => ["a", "a/b", "a/b/c", "a/b/c/d"] + * @param x string + * @returns string[] might be empty + */ +export const getFolderLevels = (x: string, addEndingSlash = false) => { + const res: string[] = []; + + if (x === "" || x === "/") { + return res; + } + + const y1 = x.split("/"); + const i = 0; + for (let index = 0; index + 1 < y1.length; index++) { + let k = y1.slice(0, index + 1).join("/"); + if (k === "" || k === "/") { + continue; + } + if (addEndingSlash) { + k = `${k}/`; + } + res.push(k); + } + return res; +}; + +export const mkdirpInVault = async (thePath: string, vault: Vault) => { + // console.info(thePath); + const foldersToBuild = getFolderLevels(thePath); + // console.info(foldersToBuild); + for (const folder of foldersToBuild) { + const r = await vault.adapter.exists(folder); + // console.info(r); + if (!r) { + console.info(`mkdir ${folder}`); + await vault.adapter.mkdir(folder); + } + } +}; + +/** + * https://stackoverflow.com/questions/8609289 + * @param b Buffer + * @returns ArrayBuffer + */ +export const bufferToArrayBuffer = ( + b: Buffer | Uint8Array | ArrayBufferView +) => { + return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength); +}; + +/** + * Simple func. + * @param b + * @returns + */ +export const arrayBufferToBuffer = (b: ArrayBuffer) => { + return Buffer.from(b); +}; + +export const arrayBufferToBase64 = (b: ArrayBuffer) => { + return arrayBufferToBuffer(b).toString("base64"); +}; + +export const arrayBufferToHex = (b: ArrayBuffer) => { + return arrayBufferToBuffer(b).toString("hex"); +}; + +export const base64ToArrayBuffer = (b64text: string) => { + return bufferToArrayBuffer(Buffer.from(b64text, "base64")); +}; + +export const copyArrayBuffer = (src: ArrayBuffer) => { + const dst = new ArrayBuffer(src.byteLength); + new Uint8Array(dst).set(new Uint8Array(src)); + return dst; +}; + +/** + * https://stackoverflow.com/questions/43131242 + * @param hex + * @returns + */ +export const hexStringToTypedArray = (hex: string) => { + const f = hex.match(/[\da-f]{2}/gi); + if (f === null) { + throw Error(`input ${hex} is not hex, no way to transform`); + } + return new Uint8Array(f.map((h) => Number.parseInt(h, 16))); +}; + +export const base64ToBase32 = (a: string) => { + return base32.stringify(Buffer.from(a, "base64")); +}; + +export const base64ToBase64url = (a: string, pad = false) => { + let b = a.replace(/\+/g, "-").replace(/\//g, "_"); + if (!pad) { + b = b.replace(/=/g, ""); + } + return b; +}; + +/** + * iOS Safari could decrypt string with invalid password! + * So we need an extra way to test the decrypted result. + * One simple way is testing the result are "valid", printable chars or not. + * + * https://stackoverflow.com/questions/6198986 + * https://www.regular-expressions.info/unicode.html + * Manual test shows that emojis like '🍎' match '\\p{Cs}', + * so we need to write the regrex in a form that \p{C} minus \p{Cs} + * @param a + */ +export const isVaildText = (a: string) => { + if (a === undefined) { + return false; + } + // If the regex matches, the string is invalid. + return !XRegExp("\\p{Cc}|\\p{Cf}|\\p{Co}|\\p{Cn}|\\p{Zl}|\\p{Zp}", "A").test( + a + ); +}; + +/** + * Use regex to detect a text contains emoji or not. + * @param a + * @returns + */ +export const hasEmojiInText = (a: string) => { + const regex = emojiRegex(); + return regex.test(a); +}; + +/** + * Convert the headers to a normal object. + * @param h + * @param toLower + * @returns + */ +export const headersToRecord = (h: Headers, toLower = true) => { + const res: Record = {}; + h.forEach((v, k) => { + if (toLower) { + res[k.toLowerCase()] = v; + } else { + res[k] = v; + } + }); + return res; +}; + +/** + * If input is already a folder, returns it as is; + * And if input is a file, returns its direname. + * @param a + * @returns + */ +export const getPathFolder = (a: string) => { + if (a.endsWith("/")) { + return a; + } + const b = path.posix.dirname(a); + return b.endsWith("/") ? b : `${b}/`; +}; + +/** + * If input is already a folder, returns its folder; + * And if input is a file, returns its direname. + * @param a + * @returns + */ +export const getParentFolder = (a: string) => { + const b = path.posix.dirname(a); + if (b === "." || b === "/") { + // the root + return "/"; + } + if (b.endsWith("/")) { + return b; + } + return `${b}/`; +}; + +/** + * https://stackoverflow.com/questions/54511144 + * @param a + * @param delimiter + * @returns + */ +export const setToString = (a: Set, delimiter = ",") => { + return [...a].join(delimiter); +}; + +export const extractSvgSub = (x: string, subEl = "rect") => { + const parser = new window.DOMParser(); + const dom = parser.parseFromString(x, "image/svg+xml"); + const svg = dom.querySelector("svg")!; + svg.setAttribute("viewbox", "0 0 10 10"); + return svg.innerHTML; +}; + +/** + * https://stackoverflow.com/questions/18230217 + * @param min + * @param max + * @returns + */ +export const getRandomIntInclusive = (min: number, max: number) => { + const randomBuffer = new Uint32Array(1); + window.crypto.getRandomValues(randomBuffer); + const randomNumber = randomBuffer[0] / (0xffffffff + 1); + const min2 = Math.ceil(min); + const max2 = Math.floor(max); + return Math.floor(randomNumber * (max2 - min2 + 1)) + min2; +}; + +/** + * Random buffer + * @param byteLength + * @returns + */ +export const getRandomArrayBuffer = (byteLength: number) => { + const k = window.crypto.getRandomValues(new Uint8Array(byteLength)); + return bufferToArrayBuffer(k); +}; + +/** + * https://stackoverflow.com/questions/958908 + * @param x + * @returns + */ +export const reverseString = (x: string) => { + return [...x].reverse().join(""); +}; + +export interface SplitRange { + partNum: number; // starting from 1 + start: number; + end: number; // exclusive +} +export const getSplitRanges = (bytesTotal: number, bytesEachPart: number) => { + const res: SplitRange[] = []; + if (bytesEachPart >= bytesTotal) { + res.push({ + partNum: 1, + start: 0, + end: bytesTotal, + }); + return res; + } + const remainder = bytesTotal % bytesEachPart; + const howMany = + Math.floor(bytesTotal / bytesEachPart) + (remainder === 0 ? 0 : 1); + for (let i = 0; i < howMany; ++i) { + res.push({ + partNum: i + 1, + start: bytesEachPart * i, + end: Math.min(bytesEachPart * (i + 1), bytesTotal), + }); + } + return res; +}; + +/** + * https://stackoverflow.com/questions/332422 + * @param obj anything + * @returns string of the name of the object + */ +export const getTypeName = (obj: any) => { + return Object.prototype.toString.call(obj).slice(8, -1); +}; + +/** + * starting from 1 + * @param x + * @returns + */ +export const atWhichLevel = (x: string | undefined) => { + if ( + x === undefined || + x === "" || + x === "." || + x === ".." || + x.startsWith("/") + ) { + throw Error(`do not know which level for ${x}`); + } + let y = x; + if (x.endsWith("/")) { + y = x.slice(0, -1); + } + return y.split("/").length; +}; + +export const checkHasSpecialCharForDir = (x: string) => { + return /[?/\\]/.test(x); +}; + +export const unixTimeToStr = (x: number | undefined | null, hasMs = false) => { + if (x === undefined || x === null || Number.isNaN(x)) { + return undefined; + } + if (hasMs) { + // 1716712162574 => '2024-05-26T16:29:22.574+08:00' + return window.moment(x).toISOString(true); + } else { + // 1716712162574 => '2024-05-26T16:29:22+08:00' + return window.moment(x).format() as string; + } +}; + +/** + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#examples + * @returns + */ +const getCircularReplacer = () => { + const seen = new WeakSet(); + return (key: any, value: any) => { + if (typeof value === "object" && value !== null) { + if (seen.has(value)) { + return; + } + seen.add(value); + } + return value; + }; +}; + +/** + * Convert "any" value to string. + * @param x + * @returns + */ +export const toText = (x: any) => { + if (x === undefined || x === null) { + return `${x}`; + } + if (typeof x === "string") { + return x; + } + if ( + x instanceof String || + x instanceof Date || + typeof x === "number" || + typeof x === "bigint" || + typeof x === "boolean" + ) { + return `${x}`; + } + + if ( + x instanceof Error || + (x?.stack && + x?.message && + typeof x.stack === "string" && + typeof x.message === "string") + ) { + return `ERROR! MESSAGE: ${x.message}, STACK: ${x.stack}`; + } + + try { + const y = JSON.stringify(x, getCircularReplacer(), 2); + if (y !== undefined) { + return y; + } + throw new Error("not jsonable"); + } catch { + return `${x}`; + } +}; + +/** + * On Android the stat has bugs for folders. So we need a fixed version. + * @param vault + * @param path + */ +export const statFix = async (vault: Vault, path: string) => { + const s = await vault.adapter.stat(path); + if (s === undefined || s === null) { + throw Error(`${path} doesn't exist cannot run stat`); + } + if (s.ctime === undefined || s.ctime === null || Number.isNaN(s.ctime)) { + s.ctime = undefined as any; // force assignment + } + if (s.mtime === undefined || s.mtime === null || Number.isNaN(s.mtime)) { + s.mtime = undefined as any; // force assignment + } + if ( + (s.size === undefined || s.size === null || Number.isNaN(s.size)) && + s.type === "folder" + ) { + s.size = 0; + } + return s; +}; + +export const isSpecialFolderNameToSkip = ( + x: string, + more: string[] | undefined +) => { + const specialFolders = [ + ".git", + ".github", + ".gitlab", + ".svn", + "node_modules", + ".DS_Store", + "__MACOSX ", + "Icon\r", // https://superuser.com/questions/298785/icon-file-on-os-x-desktop + "desktop.ini", + "Desktop.ini", + "thumbs.db", + "Thumbs.db", + ].concat(more !== undefined ? more : []); + for (const iterator of specialFolders) { + if ( + x === iterator || + x === `${iterator}/` || + x.endsWith(`/${iterator}`) || + x.endsWith(`/${iterator}/`) + ) { + return true; + } + } + + // microsoft tmp files... + const p = x.split("/"); + if (p.length > 0) { + const f = p[p.length - 1]; // file name + if (f.startsWith("~$")) { + const suffixList = ["doc", "docx", "ppt", "pptx", "xls", "xlsx"]; + for (const suffix of suffixList) { + if (f.endsWith(`.${suffix}`)) { + return true; + } + } + } + } + + return false; +}; + +/** + * + * @param x versionX + * @param y versionY + * @returns 1(x>y), 0(x==y), -1(x { + if (x === undefined || x === null) { + return -1; + } + if (y === undefined || y === null) { + return 1; + } + if (x === y) { + return 0; + } + const [x1, x2, x3] = x.split(".").map((k) => Number(k)); + const [y1, y2, y3] = y.split(".").map((k) => Number(k)); + if ( + x1 > y1 || + (x1 === y1 && x2 > y2) || + (x1 === y1 && x2 === y2 && x3 > y3) + ) { + return 1; + } + return -1; +}; + +/** + * https://stackoverflow.com/questions/19929641/how-to-append-an-html-string-to-a-documentfragment + * To introduce some advanced html fragments. + * @param string + * @returns + */ +export const stringToFragment = (string: string) => { + const wrapper = document.createElement("template"); + wrapper.innerHTML = string; + return wrapper.content; +}; + +/** + * https://stackoverflow.com/questions/39538473/using-settimeout-on-promise-chain + * @param ms + * @returns + */ +export const delay = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); + +/** + * https://forum.obsidian.md/t/css-to-show-status-bar-on-mobile-devices/77185 + * @param op + */ +export const changeMobileStatusBar = ( + op: "enable" | "disable", + oldAppContainerObserver?: MutationObserver +) => { + const appContainer = document.getElementsByClassName("app-container")[0] as + | HTMLElement + | undefined; + + const statusbar = document.querySelector( + ".is-mobile .app-container .status-bar" + ) as HTMLElement | undefined; + + if (appContainer === undefined || statusbar === undefined) { + // give up, exit + console.warn(`give up watching appContainer for statusbar`); + console.warn(`appContainer=${appContainer}, statusbar=${statusbar}`); + return undefined; + } + + if (op === "enable") { + const callback = async ( + mutationList: MutationRecord[], + observer: MutationObserver + ) => { + for (const mutation of mutationList) { + // console.debug(mutation); + if (mutation.type === "childList" && mutation.addedNodes.length > 0) { + const k = mutation.addedNodes[0] as Element; + if ( + k.className.contains("mobile-navbar") || + k.className.contains("mobile-toolbar") + ) { + // have to wait, otherwise the height is not correct?? + await delay(300); + const height = window + .getComputedStyle(k as Element) + .getPropertyValue("height"); + + statusbar.style.setProperty("display", "flex"); + statusbar.style.setProperty("margin-bottom", height); + } + } + } + }; + const observer = new MutationObserver(callback); + observer.observe(appContainer, { + attributes: false, + childList: true, + characterData: false, + subtree: false, + }); + + try { + // init, manual call + const navBar = document.getElementsByClassName( + "mobile-navbar" + )[0] as HTMLElement; + // thanks to community's solution + const height = window.getComputedStyle(navBar).getPropertyValue("height"); + statusbar.style.setProperty("display", "flex"); + statusbar.style.setProperty("margin-bottom", height); + } catch (e) { + // skip + } + + return observer; + } else { + if (oldAppContainerObserver !== undefined) { + console.debug(`disconnect oldAppContainerObserver`); + oldAppContainerObserver.disconnect(); + // biome-ignore lint/style/noParameterAssign: we want gc + oldAppContainerObserver = undefined; + } + statusbar.style.removeProperty("display"); + statusbar.style.removeProperty("margin-bottom"); + return undefined; + } +}; + +/** + * https://github.com/remotely-save/remotely-save/issues/567 + * https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Case-Sensitivity-in-API-2/td-p/191279 + * @param entities + */ +export const fixEntityListCasesInplace = (entities: { keyRaw: string }[]) => { + entities.sort((a, b) => a.keyRaw.length - b.keyRaw.length); + // console.log(JSON.stringify(entities,null,2)); + + const caseMapping: Record = { "": "" }; + for (const e of entities) { + // console.log(`looking for: ${JSON.stringify(e, null, 2)}`); + + let parentFolder = getParentFolder(e.keyRaw); + if (parentFolder === "/") { + parentFolder = ""; + } + const parentFolderLower = parentFolder.toLocaleLowerCase(); + const segs = e.keyRaw.split("/"); + if (e.keyRaw.endsWith("/")) { + // folder + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKeyRaw = `${caseMapping[parentFolderLower]}${segs + .slice(-2) + .join("/")}`; + caseMapping[newKeyRaw.toLocaleLowerCase()] = newKeyRaw; + e.keyRaw = newKeyRaw; + // console.log(JSON.stringify(caseMapping,null,2)); + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } else { + // file + if (caseMapping.hasOwnProperty(parentFolderLower)) { + const newKeyRaw = `${caseMapping[parentFolderLower]}${segs + .slice(-1) + .join("/")}`; + e.keyRaw = newKeyRaw; + } else { + throw Error(`${parentFolder} doesn't have cases record??`); + } + } + } + + return entities; +}; + +/** + * https://stackoverflow.com/questions/1248302/how-to-get-the-size-of-a-javascript-object + * @param object + * @returns bytes + */ +export const roughSizeOfObject = (object: any) => { + const objectList: any[] = []; + const stack = [object]; + let bytes = 0; + + while (stack.length) { + const value = stack.pop(); + + switch (typeof value) { + case "boolean": + bytes += 4; + break; + case "string": + bytes += value.length * 2; + break; + case "number": + bytes += 8; + break; + case "object": + if (!objectList.includes(value)) { + objectList.push(value); + for (const prop in value) { + if (value.hasOwnProperty(prop)) { + stack.push(value[prop]); + } + } + } + break; + } + } + return bytes; +}; + +export const splitFileSizeToChunkRanges = ( + totalSize: number, + chunkSize: number +) => { + if (totalSize < 0) { + throw Error(`totalSize should not be negative`); + } + if (chunkSize <= 0) { + throw Error(`chunkSize should not be negative or zero`); + } + + if (totalSize === 0) { + return []; + } + if (totalSize <= chunkSize) { + return [{ start: 0, end: totalSize - 1 }]; + } + + const res: { start: number; end: number }[] = []; + + const blocksCount = Math.ceil((totalSize * 1.0) / chunkSize); + + for (let i = 0; i < blocksCount; ++i) { + res.push({ + start: i * chunkSize, + end: Math.min((i + 1) * chunkSize - 1, totalSize - 1), + }); + } + return res; +}; + +export const getSha1 = async (x: ArrayBuffer, stringify: "base64" | "hex") => { + const y = await window.crypto.subtle.digest("SHA-1", x); + + if (stringify === "base64") { + return arrayBufferToBase64(y); + } else if (stringify === "hex") { + return arrayBufferToHex(y); + } + throw Error(`not supported stringify option = ${stringify}`); +}; + +/** + * https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions + * https://support.microsoft.com/en-us/office/restrictions-and-limitations-in-onedrive-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa#invalidcharacters + */ +export const checkValidName = (x: string) => { + if (x === undefined || x === "") { + // what?? + return { + reason: "empty", + result: false, + }; + } + + // The following reserved characters: + const invalidChars = '*"<>:|?'.split(""); + for (const c of invalidChars) { + if (x.includes(c)) { + return { + reason: `reserved character: ${c}`, + result: false, + }; + } + } + + // directory component + for (const c of [".", ".."]) { + if ( + x === c || + x.endsWith(`/${c}`) || + x.startsWith(`${c}/`) || + x.includes(`/${c}/`) + ) { + return { + reason: `directory being ${c}`, + result: false, + }; + } + } + + // reserved file names + const reservedNames = [ + "CON", + "PRN", + "AUX", + "NUL", + "COM0", + "COM1", + "COM2", + "COM3", + "COM4", + "COM5", + "COM6", + "COM7", + "COM8", + "COM9", + "COM¹", + "COM²", + "COM³", + "LPT0", + "LPT1", + "LPT2", + "LPT3", + "LPT4", + "LPT5", + "LPT6", + "LPT7", + "LPT8", + "LPT9", + "LPT¹", + "LPT²", + "LPT³", + ]; + for (const f of reservedNames) { + if ( + x === f || + x.startsWith(`${f}.`) || + x.startsWith(`${f}/`) || + x.includes(`/${f}/`) || + x.endsWith(`/${f}`) || + x.includes(`/${f}.`) + ) { + return { + reason: `reserved folder/file name: ${f}`, + result: false, + }; + } + } + + // Do not end a file or directory name with a space or a period. + if ( + x.endsWith(" ") || + x.endsWith(".") || + x.includes(" /") || + x.includes("./") + ) { + return { + reason: `folder/file name ending with a space or a period`, + result: false, + }; + } + + return { + reason: "ok", + result: true, + }; +}; + +/** + * Drop-in replacement for `fetch()` with exponential backoff on HTTP 429. + * Respects the server's `Retry-After` header when present. + */ +export const retryFetch = async ( + input: RequestInfo | URL, + init?: RequestInit, + extraHint = "" +): Promise => { + const waitSeconds = [1, 2, 4, 8]; + const prefix = extraHint ? `${extraHint}: ` : ""; + for (let idx = 0; idx < waitSeconds.length; idx++) { + if (idx !== 0) { + console.warn(`${prefix}retry attempt ${idx + 1} at ${Date.now()}`); + } + const resp = await fetch(input, init); + if (resp.status !== 429 || idx === waitSeconds.length - 1) { + if (resp.status === 429 && idx === waitSeconds.length - 1) { + throw new Error(`${prefix}429 too many requests after ${idx + 1} retries`); + } + return resp; + } + const svrSec = Number.parseInt(resp.headers.get("retry-after") || "0") || 0; + const fallbackSec = waitSeconds[idx]; + const secMin = Math.max(svrSec, fallbackSec); + const secMax = Math.max(secMin * 1.8, 2); + console.warn( + `${prefix}429 on attempt ${idx + 1}, waiting ${secMin}–${secMax}s before retry` + ); + await delay(secMin * 1000 + Math.random() * (secMax - secMin) * 1000); + } + // unreachable, but TypeScript needs it + throw new Error(`${prefix}retryFetch exhausted all retries`); +}; diff --git a/src/obsFolderLister.ts b/src/obsFolderLister.ts new file mode 100644 index 0000000..42ead91 --- /dev/null +++ b/src/obsFolderLister.ts @@ -0,0 +1,141 @@ +import type { ListedFiles, Vault } from "obsidian"; +import type { Entity } from "./baseTypes"; + +import { Queue } from "@fyears/tsqueue"; +import chunk from "lodash/chunk"; +import flatten from "lodash/flatten"; +import { isSpecialFolderNameToSkip, statFix } from "./misc"; + +const isPluginDirItself = (x: string, pluginId: string) => { + return ( + x === pluginId || + x === `${pluginId}/` || + x.endsWith(`/${pluginId}`) || + x.endsWith(`/${pluginId}/`) + ); +}; + +const isLikelyPluginSubFiles = (x: string) => { + const reqFiles = [ + "data.json", + "main.js", + "manifest.json", + ".gitignore", + "styles.css", + ]; + for (const iterator of reqFiles) { + if (x === iterator || x.endsWith(`/${iterator}`)) { + return true; + } + } + return false; +}; + +export const listFilesInObsFolder = async ( + configDir: string, + vault: Vault, + pluginId: string, + bookmarksOnly: boolean +): Promise => { + const q = new Queue([configDir]); + const CHUNK_SIZE = 10; + let contents: Entity[] = []; + + let iterRound = 0; + + while (q.length > 0) { + const itemsToFetch: string[] = []; + while (q.length > 0) { + itemsToFetch.push(q.pop()!); + } + + const itemsToFetchChunks = chunk(itemsToFetch, CHUNK_SIZE); + for (const singleChunk of itemsToFetchChunks) { + const r = singleChunk.map(async (x) => { + const statRes = await statFix(vault, x); + + if (statRes === undefined || statRes === null) { + throw Error("something goes wrong while listing hidden folder"); + } + const isFolder = statRes.type === "folder"; + let children: ListedFiles | undefined = undefined; + if (isFolder) { + children = await vault.adapter.list(x); + } + + if ( + !isFolder && + (statRes.mtime === undefined || + statRes.mtime === null || + statRes.mtime === 0) + ) { + throw Error( + `File in Obsidian ${configDir} has last modified time 0: ${x}, don't know how to deal with it.` + ); + } + + return { + itself: { + key: isFolder ? `${x}/` : x, // local always unencrypted + keyRaw: isFolder ? `${x}/` : x, + mtimeCli: statRes.mtime, + mtimeSvr: statRes.mtime, + size: statRes.size, // local always unencrypted + sizeRaw: statRes.size, + }, + children: children, + }; + }); + const r2 = flatten(await Promise.all(r)); + + for (const iter of r2) { + contents.push(iter.itself); + const isInsideSelfPlugin = isPluginDirItself(iter.itself.key, pluginId); + if (iter.children !== undefined) { + for (const iter2 of iter.children.folders) { + if ( + isSpecialFolderNameToSkip(iter2, ["workspace", "workspace.json"]) + ) { + continue; + } + if (isInsideSelfPlugin && !isLikelyPluginSubFiles(iter2)) { + // special treatment for remotely-save folder + continue; + } + q.push(iter2); + } + for (const iter2 of iter.children.files) { + if ( + isSpecialFolderNameToSkip(iter2, ["workspace", "workspace.json"]) + ) { + continue; + } + if (isInsideSelfPlugin && !isLikelyPluginSubFiles(iter2)) { + // special treatment for remotely-save folder + continue; + } + q.push(iter2); + } + } + } + } + + if (bookmarksOnly && iterRound > 1) { + // list until bookmarks.json is found or next level is arrived. + break; + } + + iterRound += 1; + } + + // console.debug(`contents in obs config: ${JSON.stringify(contents)}`); + + if (bookmarksOnly) { + contents = contents.filter( + (e) => + e.key === `${configDir}/` || e.key === `${configDir}/bookmarks.json` + ); + } + + return contents; +}; diff --git a/src/profiler.ts b/src/profiler.ts new file mode 100644 index 0000000..8073bb1 --- /dev/null +++ b/src/profiler.ts @@ -0,0 +1,153 @@ +import type { ProfilerConfig, SUPPORTED_SERVICES_TYPE } from "./baseTypes"; +import { type InternalDBs, insertProfilerResultByVault } from "./localdb"; +import { roughSizeOfObject, unixTimeToStr } from "./misc"; + +interface BreakPoint { + label: string; + fakeTimeMilli: number; // it's NOT a unix timestamp + indent: number; + size?: number; +} + +export const DEFAULT_PROFILER_CONFIG: ProfilerConfig = { + enable: false, + enablePrinting: false, + recordSize: false, +}; + +export class Profiler { + startTime: number; + breakPoints: BreakPoint[]; + indent: number; + enablePrinting: boolean; + recordSize: boolean; + constructor(label?: string, enablePrinting?: boolean, recordSize?: boolean) { + this.breakPoints = []; + this.indent = 0; + this.startTime = 0; + this.enablePrinting = enablePrinting ?? false; + this.recordSize = recordSize ?? false; + + if (label !== undefined) { + this.startTime = Date.now(); + const p = { + label: label, + fakeTimeMilli: performance.now(), + indent: this.indent, + }; + this.breakPoints.push(p); + if (this.enablePrinting) { + console.debug(this.toString(-1)); + } + } + } + + insert(label: string) { + if (this.breakPoints.length === 0) { + this.startTime = Date.now(); + } + const p = { + label: label, + fakeTimeMilli: performance.now(), + indent: this.indent, + }; + this.breakPoints.push(p); + if (this.enablePrinting) { + console.debug(this.toString(-1)); + } + + return this; + } + + insertSize(label: string, obj: any) { + if (!this.recordSize) { + return; + } + if (this.breakPoints.length === 0) { + this.startTime = Date.now(); + } + const p = { + label: label, + fakeTimeMilli: performance.now(), + indent: this.indent, + size: roughSizeOfObject(obj), + }; + this.breakPoints.push(p); + if (this.enablePrinting) { + console.debug(this.toString(-1)); + } + + return this; + } + + addIndent() { + this.indent += 2; + } + removeIndent() { + this.indent -= 2; + if (this.indent < 0) { + this.indent = 0; + } + } + + clear() { + this.breakPoints = []; + this.indent = 0; + this.startTime = 0; + return this; + } + + toString(idx?: number) { + if (idx !== undefined) { + let i = idx; + if (idx < 0) { + i = this.breakPoints.length + idx; + } + const label = this.breakPoints?.[i]["label"]; + const indent = this.breakPoints?.[i]["indent"]; + let millsec = 0; + if (i >= 1) { + millsec = + Math.round( + (this.breakPoints?.[i]["fakeTimeMilli"] - + this.breakPoints?.[i - 1]["fakeTimeMilli"]) * + 10 + ) / 10.0; + } + let res = `${" ".repeat(indent)}[${label}]: ${millsec}ms`; + if (this.breakPoints[i].hasOwnProperty("size")) { + const size = this.breakPoints[i].size as number; + res += `, size=${size}`; + } + return res; + } + + if (this.breakPoints.length === 0) { + return "nothing in profiler"; + } + + let res = `[startTime]: ${unixTimeToStr(this.startTime)}`; + for (let i = 0; i < this.breakPoints.length; ++i) { + if (i === 0) { + res += `\n[${this.breakPoints[i]["label"]}]: start`; + } else { + res += `\n${this.toString(i)}`; + } + } + + return res; + } + + async save( + db: InternalDBs, + vaultRandomID: string, + remoteType: SUPPORTED_SERVICES_TYPE + ) { + await insertProfilerResultByVault( + db, + this.toString(), + vaultRandomID, + remoteType + ); + } +} diff --git a/src/settings.ts b/src/settings.ts new file mode 100644 index 0000000..0e5f010 --- /dev/null +++ b/src/settings.ts @@ -0,0 +1,2703 @@ +import { SVG_DROPBOX, SVG_ONEDRIVE, SVG_PCLOUD, SVG_S3, SVG_WEBDAV, SVG_WEBDIS } from './icons'; +import { Eye, EyeOff, createElement } from "lucide"; +import { + type App, + Modal, + Notice, + Platform, + PluginSettingTab, + Setting, + requireApiVersion, +} from "obsidian"; +import type { TextComponent } from "obsidian"; +import type { + CipherMethodType, + ConflictActionType, + EmptyFolderCleanType, + QRExportType, + SUPPORTED_SERVICES_TYPE, + SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR, + SyncDirectionType, + WebdavAuthType, +} from "./baseTypes"; + +import cloneDeep from "lodash/cloneDeep"; +import { generateAzureBlobStorageSettingsPart } from "./settingsAzureBlobStorage"; +import { generateBoxSettingsPart } from "./settingsBox"; +// import { generateClearDupFilesSettingsPart } from "./settingsClearDupFiles"; +import { generateGoogleDriveSettingsPart } from "./settingsGoogleDrive"; +import { generateKoofrSettingsPart } from "./settingsKoofr"; +import { generateOnedriveFullSettingsPart } from "./settingsOnedriveFull"; +import { generatePCloudSettingsPart } from "./settingsPCloud"; +// import { generateProSettingsPart } from "./settingsPro"; +import { generateYandexDiskSettingsPart } from "./settingsYandexDisk"; +import { API_VER_ENSURE_REQURL_OK, VALID_REQURL } from "./baseTypesObs"; +import { messyConfigToNormal } from "./configPersist"; +import { + exportVaultProfilerResultsToFiles, + exportVaultSyncPlansToFiles, +} from "./debugMode"; +import { + DEFAULT_DROPBOX_CONFIG, + getAuthUrlAndVerifier as getAuthUrlAndVerifierDropbox, + sendAuthReq as sendAuthReqDropbox, + setConfigBySuccessfullAuthInplace, +} from "./fsDropbox"; +import { getClient } from "./fsGetter"; +import { + DEFAULT_ONEDRIVE_CONFIG, + getAuthUrlAndVerifier as getAuthUrlAndVerifierOnedrive, +} from "./fsOnedrive"; +import { simpleTransRemotePrefix } from "./fsS3"; +import type { TransItemType } from "./i18n"; +import { + exportQrCodeUri, + importQrCodeUri, + parseUriByHand, +} from "./importExport"; +import { + clearAllPrevSyncRecordByVault, + clearAllSyncPlanRecords, + destroyDBs, + upsertLastFailedSyncTimeByVault, + upsertLastSuccessSyncTimeByVault, +} from "./localdb"; +import type RemotelySavePlugin from "./main"; // unavoidable +import { + changeMobileStatusBar, + checkHasSpecialCharForDir, + stringToFragment, +} from "./misc"; +import { DEFAULT_PROFILER_CONFIG } from "./profiler"; + +class PasswordModal extends Modal { + plugin: RemotelySavePlugin; + newPassword: string; + encryptionMethodSetting: Setting; + constructor( + app: App, + plugin: RemotelySavePlugin, + newPassword: string, + encryptionMethodSetting: Setting + ) { + super(app); + this.plugin = plugin; + this.newPassword = newPassword; + this.encryptionMethodSetting = encryptionMethodSetting; + } + + onOpen() { + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + // contentEl.setText("Add Or change password."); + contentEl.createEl("h2", { text: t("modal_password_title") }); + t("modal_password_shortdesc") + .split("\n") + .forEach((val, idx) => { + contentEl.createEl("p", { + text: val, + }); + }); + + [ + t("modal_password_attn1"), + t("modal_password_attn2"), + t("modal_password_attn3"), + t("modal_password_attn4"), + t("modal_password_attn5"), + ].forEach((val, idx) => { + if (idx < 3) { + contentEl.createEl("p", { + text: val, + cls: "password-disclaimer", + }); + } else { + contentEl.createEl("p", { + text: val, + }); + } + }); + + new Setting(contentEl) + .addButton((button) => { + button.setButtonText(t("modal_password_secondconfirm")); + button.onClick(async () => { + this.plugin.settings.password = this.newPassword; + if (this.newPassword !== "") { + this.encryptionMethodSetting.settingEl.removeClass( + "settings-encryption-method-hide" + ); + } else { + this.encryptionMethodSetting.settingEl.addClass( + "settings-encryption-method-hide" + ); + } + + await this.plugin.saveSettings(); + new Notice(t("modal_password_notice")); + this.close(); + }); + button.setClass("password-second-confirm"); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class EncryptionMethodModal extends Modal { + plugin: RemotelySavePlugin; + constructor(app: App, plugin: RemotelySavePlugin) { + super(app); + this.plugin = plugin; + } + + onOpen() { + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + // contentEl.setText("Add Or change password."); + contentEl.createEl("h2", { text: t("modal_encryptionmethod_title") }); + t("modal_encryptionmethod_shortdesc") + .split("\n") + .forEach((val, idx) => { + contentEl.createEl("p", { + text: stringToFragment(val), + }); + }); + + new Setting(contentEl).addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(async () => { + this.close(); + }); + button.setClass("encryptionmethod-second-confirm"); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +export class ChangeRemoteBaseDirModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly newRemoteBaseDir: string; + readonly service: SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR; + constructor( + app: App, + plugin: RemotelySavePlugin, + newRemoteBaseDir: string, + service: SUPPORTED_SERVICES_TYPE_WITH_REMOTE_BASE_DIR + ) { + super(app); + this.plugin = plugin; + this.newRemoteBaseDir = newRemoteBaseDir; + this.service = service; + } + + onOpen() { + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + contentEl.createEl("h2", { text: t("modal_remotebasedir_title") }); + t("modal_remotebasedir_shortdesc") + .split("\n") + .forEach((val, idx) => { + contentEl.createEl("p", { + text: val, + }); + }); + + if ( + this.newRemoteBaseDir === "" || + this.newRemoteBaseDir === this.app.vault.getName() + ) { + new Setting(contentEl) + .addButton((button) => { + button.setButtonText( + t("modal_remotebasedir_secondconfirm_vaultname") + ); + button.onClick(async () => { + // in the settings, the value is reset to the special case "" + this.plugin.settings[this.service].remoteBaseDir = ""; + await this.plugin.saveSettings(); + new Notice(t("modal_remotebasedir_notice")); + this.close(); + }); + button.setClass("remotebasedir-second-confirm"); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } else if (checkHasSpecialCharForDir(this.newRemoteBaseDir)) { + contentEl.createEl("p", { + text: t("modal_remotebasedir_invaliddirhint"), + }); + new Setting(contentEl).addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } else { + new Setting(contentEl) + .addButton((button) => { + button.setButtonText(t("modal_remotebasedir_secondconfirm_change")); + button.onClick(async () => { + this.plugin.settings[this.service].remoteBaseDir = + this.newRemoteBaseDir; + await this.plugin.saveSettings(); + new Notice(t("modal_remotebasedir_notice")); + this.close(); + }); + button.setClass("remotebasedir-second-confirm"); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +/** + * s3 is special and do not necessarily the same as others + * thus a new Modal here + */ +class ChangeS3RemotePrefixModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly newRemotePrefix: string; + constructor(app: App, plugin: RemotelySavePlugin, newRemotePrefix: string) { + super(app); + this.plugin = plugin; + this.newRemotePrefix = newRemotePrefix; + } + + onOpen() { + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + contentEl.createEl("h2", { text: t("modal_remoteprefix_s3_title") }); + t("modal_remoteprefix_s3_shortdesc") + .split("\n") + .forEach((val, idx) => { + contentEl.createEl("p", { + text: val, + }); + }); + + contentEl.createEl("p", { + text: t("modal_remoteprefix_s3_tosave", { prefix: this.newRemotePrefix }), + }); + + if ( + this.newRemotePrefix === "" || + this.newRemotePrefix === this.app.vault.getName() + ) { + new Setting(contentEl) + .addButton((button) => { + button.setButtonText(t("modal_remoteprefix_s3_secondconfirm_empty")); + button.onClick(async () => { + // in the settings, the value is reset to the special case "" + this.plugin.settings.s3.remotePrefix = ""; + await this.plugin.saveSettings(); + new Notice(t("modal_remoteprefix_s3_notice")); + this.close(); + }); + button.setClass("remoteprefix-second-confirm"); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } else { + new Setting(contentEl) + .addButton((button) => { + button.setButtonText(t("modal_remoteprefix_s3_secondconfirm_change")); + button.onClick(async () => { + this.plugin.settings.s3.remotePrefix = this.newRemotePrefix; + await this.plugin.saveSettings(); + new Notice(t("modal_remoteprefix_s3_notice")); + this.close(); + }); + button.setClass("remoteprefix-s3-second-confirm"); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class DropboxAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + } + + async onOpen() { + this.modalEl.addClass("byoc-auth-modal"); + this.titleEl.innerHTML = `${SVG_DROPBOX} Connect Dropbox Account`; + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + let needManualPatse = false; + const userAgent = window.navigator.userAgent.toLocaleLowerCase() || ""; + // some users report that, + // the Linux would open another instance Obsidian if jumping back, + // so fallback to manual paste on Linux + if ( + Platform.isDesktopApp && + !Platform.isMacOS && + (/linux/.test(userAgent) || + /ubuntu/.test(userAgent) || + /debian/.test(userAgent) || + /fedora/.test(userAgent) || + /centos/.test(userAgent)) + ) { + needManualPatse = true; + } + + const { authUrl, verifier } = await getAuthUrlAndVerifierDropbox( + this.plugin.settings.dropbox.clientID, + needManualPatse + ); + + if (needManualPatse) { + t("modal_dropboxauth_manualsteps") + .split("\n") + .forEach((val) => { + contentEl.createEl("p", { + text: val, + }); + }); + } else { + this.plugin.oauth2Info.verifier = verifier; + + t("modal_dropboxauth_autosteps") + .split("\n") + .forEach((val) => { + contentEl.createEl("p", { + text: val, + }); + }); + } + + const div2 = contentEl.createDiv(); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + + + contentEl.createEl("p").createEl("a", { + href: authUrl, + text: authUrl, + }); + + if (needManualPatse) { + let authCode = ""; + new Setting(contentEl) + .setName(t("modal_dropboxauth_maualinput")) + .setDesc(t("modal_dropboxauth_maualinput_desc")) + .addText((text) => + text + .setPlaceholder("") + .setValue("") + .onChange((val) => { + authCode = val.trim(); + }) + ) + .addButton(async (button) => { + button.setButtonText(t("submit")); + button.onClick(async () => { + new Notice(t("modal_dropboxauth_maualinput_notice")); + try { + const authRes = await sendAuthReqDropbox( + this.plugin.settings.dropbox.clientID, + verifier, + authCode, + async (e: any) => { + new Notice(t("protocol_dropbox_connect_fail")); + new Notice(`${e}`); + throw e; + } + ); + const self = this; + setConfigBySuccessfullAuthInplace( + this.plugin.settings.dropbox, + authRes!, + () => self.plugin.saveSettings() + ); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + const username = await client.getUserDisplayName(); + this.plugin.settings.dropbox.username = username; + await this.plugin.saveSettings(); + new Notice( + t("modal_dropboxauth_maualinput_conn_succ", { + username: username, + }) + ); + this.authDiv.toggleClass( + "dropbox-auth-button-hide", + this.plugin.settings.dropbox.username !== "" + ); + this.revokeAuthDiv.toggleClass( + "dropbox-revoke-auth-button-hide", + this.plugin.settings.dropbox.username === "" + ); + this.revokeAuthSetting.setDesc( + t("modal_dropboxauth_maualinput_conn_succ_revoke", { + username: this.plugin.settings.dropbox.username, + }) + ); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_dropboxauth_maualinput_conn_fail")); + } + }); + }); + } + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +export class OnedriveAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + } + + async onOpen() { + this.modalEl.addClass("byoc-auth-modal"); + this.titleEl.innerHTML = `${SVG_ONEDRIVE} Connect OneDrive Account`; + const { contentEl } = this; + + const { authUrl, verifier } = await getAuthUrlAndVerifierOnedrive( + this.plugin.settings.onedrive.clientID, + this.plugin.settings.onedrive.authority + ); + this.plugin.oauth2Info.verifier = verifier; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + t("modal_onedriveauth_shortdesc") + .split("\n") + .forEach((val) => { + contentEl.createEl("p", { + text: val, + }); + }); + if (Platform.isLinux) { + t("modal_onedriveauth_shortdesc_linux") + .split("\n") + .forEach((val) => { + contentEl.createEl("p", { + text: stringToFragment(val), + }); + }); + } + const div2 = contentEl.createDiv(); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +export class OnedriveRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + } + + async onOpen() { + this.modalEl.addClass("byoc-auth-modal"); + this.titleEl.innerHTML = `${SVG_ONEDRIVE} Revoke OneDrive Account`; + const { contentEl } = this; + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + contentEl.createEl("p", { + text: t("modal_onedriverevokeauth_step1"), + }); + const consentUrl = "https://microsoft.com/consent"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + + contentEl.createEl("p", { + text: t("modal_onedriverevokeauth_step2"), + }); + + new Setting(contentEl) + .setName(t("modal_onedriverevokeauth_clean")) + .setDesc(t("modal_onedriverevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_onedriverevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.onedrive = JSON.parse( + JSON.stringify(DEFAULT_ONEDRIVE_CONFIG) + ); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "onedrive-auth-button-hide", + this.plugin.settings.onedrive.username !== "" + ); + this.revokeAuthDiv.toggleClass( + "onedrive-revoke-auth-button-hide", + this.plugin.settings.onedrive.username === "" + ); + new Notice(t("modal_onedriverevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_onedriverevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class SyncConfigDirModal extends Modal { + plugin: RemotelySavePlugin; + saveDropdownFunc: () => void; + constructor( + app: App, + plugin: RemotelySavePlugin, + saveDropdownFunc: () => void + ) { + super(app); + this.plugin = plugin; + this.saveDropdownFunc = saveDropdownFunc; + } + + async onOpen() { + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + t("modal_syncconfig_attn") + .split("\n") + .forEach((val) => { + contentEl.createEl("p", { + text: val, + }); + }); + + new Setting(contentEl) + .addButton((button) => { + button.setButtonText(t("modal_syncconfig_secondconfirm")); + button.onClick(async () => { + this.plugin.settings.syncConfigDir = true; + await this.plugin.saveSettings(); + this.saveDropdownFunc(); + new Notice(t("modal_syncconfig_notice")); + this.close(); + }); + }) + .addButton((button) => { + button.setButtonText(t("goback")); + button.onClick(() => { + this.close(); + }); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class ExportSettingsQrCodeModal extends Modal { + plugin: RemotelySavePlugin; + exportType: QRExportType; + constructor(app: App, plugin: RemotelySavePlugin, exportType: QRExportType) { + super(app); + this.plugin = plugin; + this.exportType = exportType; + } + + async onOpen() { + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + const { rawUri, imgUri } = await exportQrCodeUri( + this.plugin.settings, + this.app.vault.getName(), + this.plugin.manifest.version, + this.exportType + ); + + const div1 = contentEl.createDiv(); + t("modal_qr_shortdesc") + .split("\n") + .forEach((val) => { + div1.createEl("p", { + text: val, + }); + }); + + const div2 = contentEl.createDiv(); + div2.createEl( + "button", + { + text: t("modal_qr_button"), + }, + (el) => { + el.onclick = async () => { + await navigator.clipboard.writeText(rawUri); + new Notice(t("modal_qr_button_notice")); + }; + } + ); + + const div3 = contentEl.createDiv(); + div3.createEl( + "img", + { + cls: "qrcode-img", + }, + async (el) => { + el.src = imgUri; + } + ); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +const getEyesElements = () => { + const eyeEl = createElement(Eye); + const eyeOffEl = createElement(EyeOff); + return { + eye: eyeEl.outerHTML, + eyeOff: eyeOffEl.outerHTML, + }; +}; + +export const wrapTextWithPasswordHide = (text: TextComponent) => { + const { eye, eyeOff } = getEyesElements(); + const hider = text.inputEl.insertAdjacentElement("afterend", createSpan())!; + // the init type of hider is "hidden" === eyeOff === password + hider.innerHTML = eyeOff; + hider.addEventListener("click", (e) => { + const isText = text.inputEl.getAttribute("type") === "text"; + hider.innerHTML = isText ? eyeOff : eye; + text.inputEl.setAttribute("type", isText ? "password" : "text"); + text.inputEl.focus(); + }); + + // the init type of text el is password + text.inputEl.setAttribute("type", "password"); + return text; +}; + +export class BYOCSettingTab extends PluginSettingTab { + readonly plugin: RemotelySavePlugin; + + constructor(app: App, plugin: RemotelySavePlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + containerEl.style.setProperty("overflow-wrap", "break-word"); + + containerEl.empty(); + containerEl.addClass("byoc-settings"); + + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + const introCard = containerEl.createDiv({ cls: "byoc-intro-card" }); + introCard.createEl("p", { text: t("settings_shortdesc") }); + + + + ////////////////////////////////////////////////// + // below for service chooser (part 1/2) + ////////////////////////////////////////////////// + + // we need to create the div in advance of any other service divs + const serviceChooserDiv = containerEl.createDiv({ cls: "byoc-provider-chooser" }); + + + ////////////////////////////////////////////////// + // below for s3 + ////////////////////////////////////////////////// + + const s3Div = containerEl.createEl("div", { cls: "s3-hide" }); + s3Div.toggleClass("s3-hide", this.plugin.settings.serviceType !== "s3"); + s3Div.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_S3} ${t("settings_s3")}`; + s3Div.createEl("p", { text: t("settings_s3_disclaimer"), cls: "s3-disclaimer" }); + + new Setting(s3Div) + .setName(t("settings_s3_endpoint")) + + .addText((text) => + text + .setPlaceholder("") + .setValue(this.plugin.settings.s3.s3Endpoint) + .onChange(async (value) => { + this.plugin.settings.s3.s3Endpoint = value.trim(); + await this.plugin.saveSettings(); + }) + ); + + new Setting(s3Div) + .setName(t("settings_s3_region")) + .setDesc(t("settings_s3_region_desc")) + .addText((text) => + text + .setPlaceholder("") + .setValue(`${this.plugin.settings.s3.s3Region}`) + .onChange(async (value) => { + this.plugin.settings.s3.s3Region = value.trim(); + await this.plugin.saveSettings(); + }) + ); + + new Setting(s3Div) + .setName(t("settings_s3_accesskeyid")) + .setDesc(t("settings_s3_accesskeyid_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(`${this.plugin.settings.s3.s3AccessKeyID}`) + .onChange(async (value) => { + this.plugin.settings.s3.s3AccessKeyID = value.trim(); + await this.plugin.saveSettings(); + }); + }); + + new Setting(s3Div) + .setName(t("settings_s3_secretaccesskey")) + .setDesc(t("settings_s3_secretaccesskey_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(`${this.plugin.settings.s3.s3SecretAccessKey}`) + .onChange(async (value) => { + this.plugin.settings.s3.s3SecretAccessKey = value.trim(); + await this.plugin.saveSettings(); + }); + }); + + new Setting(s3Div) + .setName(t("settings_s3_bucketname")) + + .addText((text) => + text + .setPlaceholder("") + .setValue(`${this.plugin.settings.s3.s3BucketName}`) + .onChange(async (value) => { + this.plugin.settings.s3.s3BucketName = value.trim(); + await this.plugin.saveSettings(); + }) + ); + + new Setting(s3Div) + .setName(t("settings_s3_urlstyle")) + .setDesc(t("settings_s3_urlstyle_desc")) + .addDropdown((dropdown) => { + dropdown.addOption( + "virtualHostedStyle", + "Virtual Hosted-Style (default)" + ); + dropdown.addOption("pathStyle", "Path-Style"); + dropdown + .setValue( + this.plugin.settings.s3.forcePathStyle + ? "pathStyle" + : "virtualHostedStyle" + ) + .onChange(async (val: string) => { + this.plugin.settings.s3.forcePathStyle = val === "pathStyle"; + await this.plugin.saveSettings(); + }); + }); + + if (VALID_REQURL && !requireApiVersion(API_VER_ENSURE_REQURL_OK)) { + new Setting(s3Div) + .setName(t("settings_s3_bypasscorslocally")) + .setDesc(t("settings_s3_bypasscorslocally_desc")) + .addDropdown((dropdown) => { + dropdown + .addOption("disable", t("disable")) + .addOption("enable", t("enable")); + + dropdown + .setValue( + `${ + this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable" + }` + ) + .onChange(async (value) => { + if (value === "enable") { + this.plugin.settings.s3.bypassCorsLocally = true; + } else { + this.plugin.settings.s3.bypassCorsLocally = false; + } + await this.plugin.saveSettings(); + }); + }); + } + + new Setting(s3Div) + .setName(t("settings_s3_parts")) + .setDesc(t("settings_s3_parts_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("1", "1"); + dropdown.addOption("2", "2"); + dropdown.addOption("3", "3"); + dropdown.addOption("5", "5"); + dropdown.addOption("10", "10"); + dropdown.addOption("15", "15"); + dropdown.addOption("20", "20 (default)"); + + dropdown + .setValue(`${this.plugin.settings.s3.partsConcurrency}`) + .onChange(async (val) => { + const realVal = Number.parseInt(val); + this.plugin.settings.s3.partsConcurrency = realVal; + await this.plugin.saveSettings(); + }); + }); + + new Setting(s3Div) + .setName(t("settings_s3_accuratemtime")) + .setDesc(t("settings_s3_accuratemtime_desc")) + .addDropdown((dropdown) => { + dropdown + .addOption("disable", t("disable")) + .addOption("enable", t("enable")); + + dropdown + .setValue( + `${this.plugin.settings.s3.useAccurateMTime ? "enable" : "disable"}` + ) + .onChange(async (val) => { + if (val === "enable") { + this.plugin.settings.s3.useAccurateMTime = true; + } else { + this.plugin.settings.s3.useAccurateMTime = false; + } + await this.plugin.saveSettings(); + }); + }); + + let newS3RemotePrefix = this.plugin.settings.s3.remotePrefix || ""; + new Setting(s3Div) + .setName(t("settings_remoteprefix_s3")) + .setDesc(t("settings_remoteprefix_s3_desc")) + .addText((text) => + text + .setPlaceholder("") + .setValue(newS3RemotePrefix) + .onChange((value) => { + newS3RemotePrefix = simpleTransRemotePrefix(value.trim()); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeS3RemotePrefixModal( + this.app, + this.plugin, + simpleTransRemotePrefix(newS3RemotePrefix.trim()) + ).open(); + }); + }); + new Setting(s3Div) + .setName(t("settings_s3_reverse_proxy_no_sign_url")) + .setDesc(t("settings_s3_reverse_proxy_no_sign_url_desc")) + .addText((text) => + text + .setPlaceholder("") + .setValue(this.plugin.settings.s3.reverseProxyNoSignUrl ?? "") + .onChange(async (value) => { + this.plugin.settings.s3.reverseProxyNoSignUrl = value.trim(); + await this.plugin.saveSettings(); + }) + ); + + new Setting(s3Div) + .setName(t("settings_s3_generatefolderobject")) + .setDesc(t("settings_s3_generatefolderobject_desc")) + .addDropdown((dropdown) => { + dropdown + .addOption( + "notgenerate", + t("settings_s3_generatefolderobject_notgenerate") + ) + .addOption( + "generate", + t("settings_s3_generatefolderobject_generate") + ); + + dropdown + .setValue( + `${ + this.plugin.settings.s3.generateFolderObject + ? "generate" + : "notgenerate" + }` + ) + .onChange(async (val) => { + if (val === "generate") { + this.plugin.settings.s3.generateFolderObject = true; + } else { + this.plugin.settings.s3.generateFolderObject = false; + } + await this.plugin.saveSettings(); + }); + }); + + new Setting(s3Div) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = err; + }); + if (res) { + new Notice(t("settings_s3_connect_succ")); + } else { + new Notice(t("settings_s3_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + ////////////////////////////////////////////////// + // below for dropbpx + ////////////////////////////////////////////////// + + const dropboxDiv = containerEl.createEl("div", { cls: "dropbox-hide" }); + dropboxDiv.toggleClass( + "dropbox-hide", + this.plugin.settings.serviceType !== "dropbox" + ); + dropboxDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_DROPBOX} ${t("settings_dropbox")}`; + dropboxDiv.createEl("p", { text: t("settings_dropbox_disclaimer"), cls: "dropbox-disclaimer" }); + + let newDropboxRemoteBaseDir = this.plugin.settings.dropbox.remoteBaseDir || ""; + new Setting(dropboxDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(this.app.vault.getName()) + .setValue(newDropboxRemoteBaseDir) + .onChange((value) => { + newDropboxRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + this.app, + this.plugin, + newDropboxRemoteBaseDir, + "dropbox" + ).open(); + }); + }); + + new Setting(dropboxDiv) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_dropbox_connect_succ")); + } else { + new Notice(t("settings_dropbox_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + ////////////////////////////////////////////////// + // below for onedrive + ////////////////////////////////////////////////// + + const onedriveDiv = containerEl.createEl("div", { cls: "onedrive-hide" }); + onedriveDiv.toggleClass( + "onedrive-hide", + this.plugin.settings.serviceType !== "onedrive" + ); + onedriveDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_ONEDRIVE} ${t("settings_onedrive")}`; + onedriveDiv.createEl("p", { text: t("settings_onedrive_disclaimer"), cls: "onedrive-disclaimer" }); + + let newOnedriveRemoteBaseDir = this.plugin.settings.onedrive.remoteBaseDir || ""; + new Setting(onedriveDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(this.app.vault.getName()) + .setValue(newOnedriveRemoteBaseDir) + .onChange((value) => { + newOnedriveRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + this.app, + this.plugin, + newOnedriveRemoteBaseDir, + "onedrive" + ).open(); + }); + }); + + 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")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_onedrive_connect_succ")); + } else { + new Notice(t("settings_onedrive_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + ////////////////////////////////////////////////// + // below for webdav + ////////////////////////////////////////////////// + + const webdavDiv = containerEl.createEl("div", { cls: "webdav-hide" }); + webdavDiv.toggleClass( + "webdav-hide", + this.plugin.settings.serviceType !== "webdav" + ); + + webdavDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_WEBDAV} ${t("settings_webdav")}`; + webdavDiv.createEl("p", { text: t("settings_webdav_disclaimer"), cls: "webdav-disclaimer" }); + + new Setting(webdavDiv) + .setName(t("settings_webdav_addr")) + .addText((text) => + text + .setPlaceholder("") + .setValue(this.plugin.settings.webdav.address) + .onChange(async (value) => { + this.plugin.settings.webdav.address = value.trim(); + // deprecate auto on 20240116, force to manual_1 + if ( + this.plugin.settings.webdav.depth === "auto" || + this.plugin.settings.webdav.depth === "auto_1" || + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" + ) { + this.plugin.settings.webdav.depth = "manual_1"; + } + + // normally saved + await this.plugin.saveSettings(); + }) + ); + + new Setting(webdavDiv) + .setName(t("settings_webdav_user")) + .setDesc(t("settings_webdav_user_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(this.plugin.settings.webdav.username) + .onChange(async (value) => { + this.plugin.settings.webdav.username = value.trim(); + // deprecate auto on 20240116, force to manual_1 + if ( + this.plugin.settings.webdav.depth === "auto" || + this.plugin.settings.webdav.depth === "auto_1" || + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" + ) { + this.plugin.settings.webdav.depth = "manual_1"; + } + await this.plugin.saveSettings(); + }); + }); + + new Setting(webdavDiv) + .setName(t("settings_webdav_password")) + .setDesc(t("settings_webdav_password_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(this.plugin.settings.webdav.password) + .onChange(async (value) => { + this.plugin.settings.webdav.password = value.trim(); + // deprecate auto on 20240116, force to manual_1 + if ( + this.plugin.settings.webdav.depth === "auto" || + this.plugin.settings.webdav.depth === "auto_1" || + this.plugin.settings.webdav.depth === "auto_infinity" || + this.plugin.settings.webdav.depth === "auto_unknown" + ) { + this.plugin.settings.webdav.depth = "manual_1"; + } + await this.plugin.saveSettings(); + }); + }); + + new Setting(webdavDiv) + .setName(t("settings_webdav_auth")) + .setDesc(t("settings_webdav_auth_desc")) + .addDropdown(async (dropdown) => { + dropdown.addOption("basic", "basic"); + if (VALID_REQURL) { + dropdown.addOption("digest", "digest"); + } + + // new version config, copied to old version, we need to reset it + if (!VALID_REQURL && this.plugin.settings.webdav.authType !== "basic") { + this.plugin.settings.webdav.authType = "basic"; + await this.plugin.saveSettings(); + } + + dropdown + .setValue(this.plugin.settings.webdav.authType) + .onChange(async (val) => { + this.plugin.settings.webdav.authType = val as WebdavAuthType; + await this.plugin.saveSettings(); + }); + }); + + new Setting(webdavDiv) + .setName(t("settings_webdav_depth")) + .setDesc(t("settings_webdav_depth_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("manual_1", t("settings_webdav_depth_1")); + dropdown.addOption("manual_infinity", t("settings_webdav_depth_inf")); + + dropdown + .setValue(this.plugin.settings.webdav.depth || "manual_1") + .onChange(async (val) => { + if (val === "manual_1") { + this.plugin.settings.webdav.depth = "manual_1"; + this.plugin.settings.webdav.manualRecursive = true; + } else if (val === "manual_infinity") { + this.plugin.settings.webdav.depth = "manual_infinity"; + this.plugin.settings.webdav.manualRecursive = false; + } + + // normally save + await this.plugin.saveSettings(); + }); + }); + + new Setting(webdavDiv) + .setName(t("settings_webdav_customheaders")) + .setDesc(stringToFragment(t("settings_webdav_customheaders_desc"))) + .addTextArea((textArea) => { + textArea + .setPlaceholder(`X-Header1: Value1\nX-Header2: Value2`) + .setValue(`${this.plugin.settings.webdav.customHeaders ?? ""}`) + .onChange(async (value) => { + this.plugin.settings.webdav.customHeaders = value + .trim() + .split("\n") + .filter((x) => x.trim() !== "") + .join("\n"); + await this.plugin.saveSettings(); + }); + textArea.inputEl.rows = 10; + textArea.inputEl.cols = 30; + + textArea.inputEl.addClass("webdav-customheaders-textarea"); + }); + + let newWebdavRemoteBaseDir = + this.plugin.settings.webdav.remoteBaseDir || ""; + new Setting(webdavDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(this.app.vault.getName()) + .setValue(newWebdavRemoteBaseDir) + .onChange((value) => { + newWebdavRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + this.app, + this.plugin, + newWebdavRemoteBaseDir, + "webdav" + ).open(); + }); + }); + + new Setting(webdavDiv) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_webdav_connect_succ")); + } else { + if (VALID_REQURL) { + new Notice(t("settings_webdav_connect_fail")); + } else { + new Notice(t("settings_webdav_connect_fail_withcors")); + } + new Notice(errors.msg); + } + }); + }); + + ////////////////////////////////////////////////// + // below for webdis + ////////////////////////////////////////////////// + + const webdisDiv = containerEl.createEl("div", { cls: "webdis-hide" }); + webdisDiv.toggleClass( + "webdis-hide", + this.plugin.settings.serviceType !== "webdis" + ); + + webdisDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_WEBDIS} ${t("settings_webdis")}`; + webdisDiv.createEl("p", { text: t("settings_webdis_disclaimer"), cls: "webdis-disclaimer" }); + + new Setting(webdisDiv) + .setName(t("settings_webdis_addr")) + .addText((text) => + text + .setPlaceholder("https://") + .setValue(this.plugin.settings.webdis.address) + .onChange(async (value) => { + this.plugin.settings.webdis.address = value.trim(); + // normally saved + await this.plugin.saveSettings(); + }) + ); + + new Setting(webdisDiv) + .setName(t("settings_webdis_user")) + .setDesc(t("settings_webdis_user_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(this.plugin.settings.webdis.username ?? "") + .onChange(async (value) => { + this.plugin.settings.webdis.username = (value ?? "").trim(); + await this.plugin.saveSettings(); + }); + }); + + new Setting(webdisDiv) + .setName(t("settings_webdis_password")) + .setDesc(t("settings_webdis_password_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(this.plugin.settings.webdis.password ?? "") + .onChange(async (value) => { + this.plugin.settings.webdis.password = (value ?? "").trim(); + await this.plugin.saveSettings(); + }); + }); + + let newWebdisRemoteBaseDir = + this.plugin.settings.webdis.remoteBaseDir || ""; + new Setting(webdisDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(this.app.vault.getName()) + .setValue(newWebdisRemoteBaseDir) + .onChange((value) => { + newWebdisRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + this.app, + this.plugin, + newWebdisRemoteBaseDir, + "webdis" + ).open(); + }); + }); + + new Setting(webdisDiv) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient( + this.plugin.settings, + this.app.vault.getName(), + () => this.plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_webdis_connect_succ")); + } else { + new Notice(t("settings_webdis_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + ////////////////////////////////////////////////// + // below for Onedrive (Full) + ////////////////////////////////////////////////// + + const { + onedriveFullDiv, + onedriveFullAllowedToUsedDiv, + onedriveFullNotShowUpHintSetting, + } = generateOnedriveFullSettingsPart( + containerEl, + t, + this.app, + this.plugin, + () => this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for googledrive + ////////////////////////////////////////////////// + + const { + googleDriveDiv, + googleDriveAllowedToUsedDiv, + googleDriveNotShowUpHintSetting, + } = generateGoogleDriveSettingsPart( + containerEl, + t, + this.app, + this.plugin, + () => this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for box + ////////////////////////////////////////////////// + + const { boxDiv, boxAllowedToUsedDiv, boxNotShowUpHintSetting } = + generateBoxSettingsPart(containerEl, t, this.app, this.plugin, () => + this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for pcloud + ////////////////////////////////////////////////// + + const { pCloudDiv, pCloudAllowedToUsedDiv, pCloudNotShowUpHintSetting } = + generatePCloudSettingsPart(containerEl, t, this.app, this.plugin, () => + this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for yandexdisk + ////////////////////////////////////////////////// + + const { + yandexDiskDiv, + yandexDiskAllowedToUsedDiv, + yandexDiskNotShowUpHintSetting, + } = generateYandexDiskSettingsPart( + containerEl, + t, + this.app, + this.plugin, + () => this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for koofr + ////////////////////////////////////////////////// + + const { koofrDiv, koofrAllowedToUsedDiv, koofrNotShowUpHintSetting } = + generateKoofrSettingsPart(containerEl, t, this.app, this.plugin, () => + this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for Azure Blob Storage + ////////////////////////////////////////////////// + + const { + azureBlobStorageDiv, + azureBlobStorageAllowedToUsedDiv, + azureBlobStorageNotShowUpHintSetting, + } = generateAzureBlobStorageSettingsPart( + containerEl, + t, + this.app, + this.plugin, + () => this.plugin.saveSettings() + ); + + ////////////////////////////////////////////////// + // below for general chooser (part 2/2) + ////////////////////////////////////////////////// + + // we need to create chooser + // after all service-div-s being created + new Setting(serviceChooserDiv) + .setName(t("settings_chooseservice")) + .setDesc(t("settings_chooseservice_desc")) + .addDropdown(async (dropdown) => { + dropdown.addOption("s3", t("settings_chooseservice_s3")); + dropdown.addOption("dropbox", t("settings_chooseservice_dropbox")); + dropdown.addOption("webdav", t("settings_chooseservice_webdav")); + dropdown.addOption("onedrive", t("settings_chooseservice_onedrive")); + dropdown.addOption("webdis", t("settings_chooseservice_webdis")); + + dropdown.addOption("separator line", "-----"); + (dropdown.selectEl.lastChild as HTMLElement).setAttribute( + "disabled", + "disabled" + ); + + dropdown.addOption( + "googledrive", + t("settings_chooseservice_googledrive") + ); + dropdown.addOption( + "onedrivefull", + t("settings_chooseservice_onedrivefull") + ); + dropdown.addOption("box", t("settings_chooseservice_box")); + dropdown.addOption("pcloud", t("settings_chooseservice_pcloud")); + dropdown.addOption( + "yandexdisk", + t("settings_chooseservice_yandexdisk") + ); + dropdown.addOption("koofr", t("settings_chooseservice_koofr")); + dropdown.addOption( + "azureblobstorage", + t("settings_chooseservice_azureblobstorage") + ); + + dropdown + .setValue(this.plugin.settings.serviceType) + .onChange(async (val) => { + this.plugin.settings.serviceType = val as SUPPORTED_SERVICES_TYPE; + s3Div.toggleClass( + "s3-hide", + this.plugin.settings.serviceType !== "s3" + ); + dropboxDiv.toggleClass( + "dropbox-hide", + this.plugin.settings.serviceType !== "dropbox" + ); + onedriveDiv.toggleClass( + "onedrive-hide", + this.plugin.settings.serviceType !== "onedrive" + ); + onedriveFullDiv.toggleClass( + "onedrivefull-hide", + this.plugin.settings.serviceType !== "onedrivefull" + ); + webdavDiv.toggleClass( + "webdav-hide", + this.plugin.settings.serviceType !== "webdav" + ); + webdisDiv.toggleClass( + "webdis-hide", + this.plugin.settings.serviceType !== "webdis" + ); + googleDriveDiv.toggleClass( + "googledrive-hide", + this.plugin.settings.serviceType !== "googledrive" + ); + boxDiv.toggleClass( + "box-hide", + this.plugin.settings.serviceType !== "box" + ); + pCloudDiv.toggleClass( + "pcloud-hide", + this.plugin.settings.serviceType !== "pcloud" + ); + yandexDiskDiv.toggleClass( + "yandexdisk-hide", + this.plugin.settings.serviceType !== "yandexdisk" + ); + koofrDiv.toggleClass( + "koofr-hide", + this.plugin.settings.serviceType !== "koofr" + ); + azureBlobStorageDiv.toggleClass( + "azureblobstorage-hide", + this.plugin.settings.serviceType !== "azureblobstorage" + ); + + await this.plugin.saveSettings(); + }); + }); + + ////////////////////////////////////////////////// + // below for basic settings + ////////////////////////////////////////////////// + + const basicDiv = containerEl.createEl("div"); + basicDiv.createEl("h2", { text: t("settings_basic"), cls: "byoc-section-heading" }); + + const passwordSetting = new Setting(basicDiv); + const encryptionMethodSetting = new Setting(basicDiv); + + let newPassword = `${this.plugin.settings.password}`; + passwordSetting + .setName(t("settings_password")) + .setDesc(t("settings_password_desc")) + .addText((text) => { + wrapTextWithPasswordHide(text); + text + .setPlaceholder("") + .setValue(`${this.plugin.settings.password}`) + .onChange(async (value) => { + newPassword = value.trim(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("confirm")); + button.onClick(async () => { + new PasswordModal( + this.app, + this.plugin, + newPassword, + encryptionMethodSetting + ).open(); + }); + }); + + if (this.plugin.settings.password === "") { + encryptionMethodSetting.settingEl.addClass( + "settings-encryption-method-hide" + ); + } + encryptionMethodSetting + .setName(t("settings_encryptionmethod")) + .setDesc(stringToFragment(t("settings_encryptionmethod_desc"))) + .addDropdown((dropdown) => { + dropdown + .addOption("rclone-base64", t("settings_encryptionmethod_rclone")) + .addOption("openssl-base64", t("settings_encryptionmethod_openssl")) + .setValue(this.plugin.settings.encryptionMethod ?? "rclone-base64") + .onChange(async (val: string) => { + this.plugin.settings.encryptionMethod = val as CipherMethodType; + await this.plugin.saveSettings(); + if (this.plugin.settings.password !== "") { + new EncryptionMethodModal(this.app, this.plugin).open(); + } + }); + }); + + new Setting(basicDiv) + .setName(t("settings_autorun")) + .setDesc(t("settings_autorun_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("-1", t("settings_autorun_notset")); + dropdown.addOption(`${1000 * 60 * 1}`, t("settings_autorun_1min")); + dropdown.addOption(`${1000 * 60 * 5}`, t("settings_autorun_5min")); + dropdown.addOption(`${1000 * 60 * 10}`, t("settings_autorun_10min")); + dropdown.addOption(`${1000 * 60 * 30}`, t("settings_autorun_30min")); + + dropdown + .setValue(`${this.plugin.settings.autoRunEveryMilliseconds}`) + .onChange(async (val: string) => { + const realVal = Number.parseInt(val); + this.plugin.settings.autoRunEveryMilliseconds = realVal; + await this.plugin.saveSettings(); + if ( + (realVal === undefined || realVal === null || realVal <= 0) && + this.plugin.autoRunIntervalID !== undefined + ) { + // clear + window.clearInterval(this.plugin.autoRunIntervalID); + this.plugin.autoRunIntervalID = undefined; + } else if ( + realVal !== undefined && + realVal !== null && + realVal > 0 + ) { + const intervalID = window.setInterval(() => { + console.info("auto run from settings.ts"); + this.plugin.syncRun("auto"); + }, realVal); + this.plugin.autoRunIntervalID = intervalID; + this.plugin.registerInterval(intervalID); + } + }); + }); + + new Setting(basicDiv) + .setName(t("settings_runoncestartup")) + .setDesc(t("settings_runoncestartup_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("-1", t("settings_runoncestartup_notset")); + dropdown.addOption( + `${1000 * 1 * 1}`, + t("settings_runoncestartup_1sec") + ); + dropdown.addOption( + `${1000 * 10 * 1}`, + t("settings_runoncestartup_10sec") + ); + dropdown.addOption( + `${1000 * 30 * 1}`, + t("settings_runoncestartup_30sec") + ); + dropdown + .setValue(`${this.plugin.settings.initRunAfterMilliseconds}`) + .onChange(async (val: string) => { + const realVal = Number.parseInt(val); + this.plugin.settings.initRunAfterMilliseconds = realVal; + await this.plugin.saveSettings(); + }); + }); + + new Setting(basicDiv) + .setName(t("settings_synconsave")) + .setDesc(t("settings_synconsave_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("-1", t("settings_synconsave_disable")); + dropdown.addOption("1000", t("settings_synconsave_enable")); + // for backward compatibility, we need to use a number representing seconds + let syncOnSaveEnabled = false; + if ((this.plugin.settings.syncOnSaveAfterMilliseconds ?? -1) > 0) { + syncOnSaveEnabled = true; + } + dropdown + .setValue(`${syncOnSaveEnabled ? "1000" : "-1"}`) + .onChange(async (val: string) => { + this.plugin.settings.syncOnSaveAfterMilliseconds = + Number.parseInt(val); + await this.plugin.saveSettings(); + this.plugin.toggleSyncOnSaveIfSet(); + }); + }); + + new Setting(basicDiv) + .setName(t("settings_skiplargefiles")) + .setDesc(t("settings_skiplargefiles_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("-1", t("settings_skiplargefiles_notset")); + + const mbs = [1, 5, 10, 20, 50, 100, 200, 500, 1000]; + for (const mb of mbs) { + dropdown.addOption(`${mb * 1000 * 1000}`, `${mb} MB`); + } + dropdown + .setValue(`${this.plugin.settings.skipSizeLargerThan}`) + .onChange(async (val) => { + this.plugin.settings.skipSizeLargerThan = Number.parseInt(val); + await this.plugin.saveSettings(); + }); + }); + + // custom status bar items is not supported on mobile + if (!Platform.isMobileApp) { + new Setting(basicDiv) + .setName(t("settings_enablestatusbar_info")) + .setDesc(t("settings_enablestatusbar_info_desc")) + .addToggle((toggle) => { + toggle + .setValue(this.plugin.settings.enableStatusBarInfo ?? false) + .onChange(async (val) => { + this.plugin.settings.enableStatusBarInfo = val; + await this.plugin.saveSettings(); + new Notice(t("settings_enablestatusbar_reloadrequired_notice")); + }); + }); + + new Setting(basicDiv) + .setName(t("settings_resetstatusbar_time")) + .setDesc(t("settings_resetstatusbar_time_desc")) + .addButton((button) => { + button.setButtonText(t("settings_resetstatusbar_button")); + button.onClick(async () => { + // reset last sync time + await upsertLastSuccessSyncTimeByVault( + this.plugin.db, + this.plugin.vaultRandomID, + -1 + ); + await upsertLastFailedSyncTimeByVault( + this.plugin.db, + this.plugin.vaultRandomID, + -1 + ); + this.plugin.updateLastSyncMsg(undefined, "not_syncing", null, null); + new Notice(t("settings_resetstatusbar_notice")); + }); + }); + } + + new Setting(basicDiv) + .setName(t("settings_ignorepaths")) + .setDesc(t("settings_ignorepaths_desc")) + .setClass("ignorepaths-settings") + + .addTextArea((textArea) => { + textArea + .setValue(`${(this.plugin.settings.ignorePaths ?? []).join("\n")}`) + .onChange(async (value) => { + this.plugin.settings.ignorePaths = value + .trim() + .split("\n") + .filter((x) => x.trim() !== ""); + await this.plugin.saveSettings(); + }); + textArea.inputEl.rows = 10; + textArea.inputEl.cols = 30; + + textArea.inputEl.addClass("ignorepaths-textarea"); + }); + + new Setting(basicDiv) + .setName(t("settings_onlyallowpaths")) + .setDesc(t("settings_onlyallowpaths_desc")) + .setClass("onlyallowpaths-settings") + + .addTextArea((textArea) => { + textArea + .setValue(`${(this.plugin.settings.onlyAllowPaths ?? []).join("\n")}`) + .onChange(async (value) => { + this.plugin.settings.onlyAllowPaths = value + .trim() + .split("\n") + .filter((x) => x.trim() !== ""); + await this.plugin.saveSettings(); + }); + textArea.inputEl.rows = 10; + textArea.inputEl.cols = 30; + + textArea.inputEl.addClass("onlyallowpaths-textarea"); + }); + + ////////////////////////////////////////////////// + // below for advanced settings + ////////////////////////////////////////////////// + const advDiv = containerEl.createEl("div"); + advDiv.createEl("h2", { + text: t("settings_adv"), cls: "byoc-section-heading" + }); + + new Setting(advDiv) + .setName(t("settings_concurrency")) + .setDesc(t("settings_concurrency_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("1", "1"); + dropdown.addOption("2", "2"); + dropdown.addOption("3", "3"); + dropdown.addOption("5", "5 (default)"); + dropdown.addOption("10", "10"); + dropdown.addOption("15", "15"); + dropdown.addOption("20", "20"); + + dropdown + .setValue(`${this.plugin.settings.concurrency}`) + .onChange(async (val) => { + const realVal = Number.parseInt(val); + this.plugin.settings.concurrency = realVal; + await this.plugin.saveSettings(); + }); + }); + + new Setting(advDiv) + .setName(t("settings_syncunderscore")) + .setDesc(t("settings_syncunderscore_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("disable", t("disable")); + dropdown.addOption("enable", t("enable")); + dropdown + .setValue( + `${this.plugin.settings.syncUnderscoreItems ? "enable" : "disable"}` + ) + .onChange(async (val) => { + this.plugin.settings.syncUnderscoreItems = val === "enable"; + await this.plugin.saveSettings(); + }); + }); + + new Setting(advDiv) + .setName(t("settings_configdir")) + .setDesc( + t("settings_configdir_desc", { + configDir: this.app.vault.configDir, + }) + ) + .addDropdown((dropdown) => { + dropdown.addOption("disable", t("disable")); + dropdown.addOption("enable", t("enable")); + + const bridge = { + secondConfirm: false, + }; + dropdown + .setValue( + `${this.plugin.settings.syncConfigDir ? "enable" : "disable"}` + ) + .onChange(async (val) => { + if (val === "enable" && !bridge.secondConfirm) { + dropdown.setValue("disable"); + new SyncConfigDirModal(this.app, this.plugin, () => { + bridge.secondConfirm = true; + dropdown.setValue("enable"); + }).open(); + } else { + bridge.secondConfirm = false; + this.plugin.settings.syncConfigDir = false; + await this.plugin.saveSettings(); + } + }); + }); + + new Setting(advDiv) + .setName(t("settings_bookmarks")) + .setDesc( + t("settings_bookmarks_desc", { + configDir: this.app.vault.configDir, + }) + ) + .addDropdown((dropdown) => { + dropdown.addOption("disable", t("disable")); + dropdown.addOption("enable", t("enable")); + + dropdown + .setValue( + `${this.plugin.settings.syncBookmarks ? "enable" : "disable"}` + ) + .onChange(async (val) => { + this.plugin.settings.syncBookmarks = val === "enable"; + await this.plugin.saveSettings(); + }); + }); + + new Setting(advDiv) + .setName(t("settings_deletetowhere")) + .setDesc(t("settings_deletetowhere_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("system", t("settings_deletetowhere_system_trash")); + dropdown.addOption( + "obsidian", + t("settings_deletetowhere_obsidian_trash") + ); + dropdown + .setValue(this.plugin.settings.deleteToWhere ?? "system") + .onChange(async (val) => { + this.plugin.settings.deleteToWhere = val as "system" | "obsidian"; + await this.plugin.saveSettings(); + }); + }); + + let conflictActionSettingOrigDesc = t("settings_conflictaction_desc"); + if ( + (this.plugin.settings.conflictAction ?? "keep_newer") === "smart_conflict" + ) { + conflictActionSettingOrigDesc += t( + "settings_conflictaction_smart_conflict_desc" + ); + } + const conflictActionSetting = new Setting(advDiv) + .setName(t("settings_conflictaction")) + .setDesc(stringToFragment(conflictActionSettingOrigDesc)); + conflictActionSetting.addDropdown((dropdown) => { + dropdown + .addOption("keep_newer", t("settings_conflictaction_keep_newer")) + .addOption("keep_larger", t("settings_conflictaction_keep_larger")) + .addOption( + "smart_conflict", + t("settings_conflictaction_smart_conflict") + ) + .setValue(this.plugin.settings.conflictAction ?? "keep_newer") + .onChange(async (val) => { + this.plugin.settings.conflictAction = val as ConflictActionType; + await this.plugin.saveSettings(); + + conflictActionSettingOrigDesc = t("settings_conflictaction_desc"); + if ( + (this.plugin.settings.conflictAction ?? "keep_newer") === + "smart_conflict" + ) { + conflictActionSettingOrigDesc += t( + "settings_conflictaction_smart_conflict_desc" + ); + } + conflictActionSetting.setDesc( + stringToFragment(conflictActionSettingOrigDesc) + ); + }); + }); + + + const percentage1 = new Setting(advDiv) + .setName(t("settings_protectmodifypercentage")) + .setDesc(t("settings_protectmodifypercentage_desc")); + + const percentage2 = new Setting(advDiv) + .setName(t("settings_protectmodifypercentage_customfield")) + .setDesc(t("settings_protectmodifypercentage_customfield_desc")); + if ((this.plugin.settings.protectModifyPercentage ?? 50) % 10 === 0) { + percentage2.settingEl.addClass("settings-percentage-custom-hide"); + } + let percentage2Text: TextComponent | undefined = undefined; + percentage2.addText((text) => { + text.inputEl.type = "number"; + percentage2Text = text; + text + .setPlaceholder("0 ~ 100") + .setValue(`${this.plugin.settings.protectModifyPercentage ?? 50}`) + .onChange(async (val) => { + let k = Number.parseFloat(val.trim()); + if (Number.isNaN(k)) { + // do nothing! + } else { + if (k < 0) { + k = 0; + } else if (k > 100) { + k = 100; + } + this.plugin.settings.protectModifyPercentage = k; + await this.plugin.saveSettings(); + } + }); + }); + + percentage1.addDropdown((dropdown) => { + for (const i of Array.from({ length: 11 }, (x, i) => i * 10)) { + let desc = `${i}`; + if (i === 0) { + desc = t("settings_protectmodifypercentage_000_desc"); + } else if (i === 50) { + desc = t("settings_protectmodifypercentage_050_desc"); + } else if (i === 100) { + desc = t("settings_protectmodifypercentage_100_desc"); + } + dropdown.addOption(`${i}`, desc); + } + dropdown.addOption( + "custom", + t("settings_protectmodifypercentage_custom_desc") + ); + + const p = this.plugin.settings.protectModifyPercentage ?? 50; + let initVal = "custom"; + if (p % 10 === 0) { + initVal = `${p}`; + } else { + // show custom + percentage2.settingEl.removeClass("settings-percentage-custom"); + } + dropdown.setValue(initVal).onChange(async (val) => { + const k = Number.parseInt(val); + if (val === "custom" || Number.isNaN(k)) { + // do nothing until user changes something in custom field + percentage2.settingEl.removeClass("settings-percentage-custom-hide"); + } else { + this.plugin.settings.protectModifyPercentage = k; + percentage2.settingEl.addClass("settings-percentage-custom-hide"); + percentage2Text?.setValue(`${k}`); + await this.plugin.saveSettings(); + } + }); + }); + + new Setting(advDiv) + .setName(t("setting_syncdirection")) + .setDesc(stringToFragment(t("setting_syncdirection_desc"))) + .addDropdown((dropdown) => { + dropdown.addOption( + "bidirectional", + t("setting_syncdirection_bidirectional_desc") + ); + dropdown.addOption( + "incremental_push_only", + t("setting_syncdirection_incremental_push_only_desc") + ); + dropdown.addOption( + "incremental_pull_only", + t("setting_syncdirection_incremental_pull_only_desc") + ); + dropdown.addOption( + "incremental_push_and_delete_only", + t("setting_syncdirection_incremental_push_and_delete_only_desc") + ); + dropdown.addOption( + "incremental_pull_and_delete_only", + t("setting_syncdirection_incremental_pull_and_delete_only_desc") + ); + + dropdown + .setValue(this.plugin.settings.syncDirection ?? "bidirectional") + .onChange(async (val) => { + this.plugin.settings.syncDirection = val as SyncDirectionType; + await this.plugin.saveSettings(); + }); + }); + + if (Platform.isMobile) { + new Setting(advDiv) + .setName(t("settings_enablemobilestatusbar")) + .setDesc(t("settings_enablemobilestatusbar_desc")) + .addDropdown(async (dropdown) => { + dropdown + .addOption("enable", t("enable")) + .addOption("disable", t("disable")); + + dropdown + .setValue( + `${ + this.plugin.settings.enableMobileStatusBar + ? "enable" + : "disable" + }` + ) + .onChange(async (val) => { + if (val === "enable") { + this.plugin.settings.enableMobileStatusBar = true; + this.plugin.appContainerObserver = + changeMobileStatusBar("enable"); + } else { + this.plugin.settings.enableMobileStatusBar = false; + changeMobileStatusBar( + "disable", + this.plugin.appContainerObserver + ); + this.plugin.appContainerObserver?.disconnect(); + this.plugin.appContainerObserver = undefined; + } + await this.plugin.saveSettings(); + }); + }); + } + + ////////////////////////////////////////////////// + // below for import and export functions + ////////////////////////////////////////////////// + + // import and export + const importExportDiv = containerEl.createEl("div"); + importExportDiv.createEl("h2", { + text: t("settings_importexport"), cls: "byoc-section-heading" + }); + + const importExportDivSetting1 = new Setting(importExportDiv) + .setName(t("settings_export")) + .setDesc(t("settings_export_desc")); + importExportDivSetting1.settingEl.addClass("setting-need-wrapping"); + importExportDivSetting1 + .addButton(async (button) => { + button.setButtonText(t("settings_export_basic_and_advanced_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "basic_and_advanced" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_s3_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "s3").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_dropbox_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "dropbox" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_onedrive_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "onedrive" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_onedrivefull_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "onedrivefull" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_webdav_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "webdav").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_webdis_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "webdis").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_googledrive_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "googledrive" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_box_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "box").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_pcloud_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "pcloud").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_yandexdisk_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "yandexdisk" + ).open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_koofr_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal(this.app, this.plugin, "koofr").open(); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_export_azureblobstorage_button")); + button.onClick(async () => { + new ExportSettingsQrCodeModal( + this.app, + this.plugin, + "azureblobstorage" + ).open(); + }); + }); + + let importSettingVal = ""; + new Setting(importExportDiv) + .setName(t("settings_import")) + .setDesc(t("settings_import_desc")) + .addText((text) => + text + .setPlaceholder("obsidian://remotely-save?func=settings&...") + .setValue("") + .onChange((val) => { + importSettingVal = val; + }) + ) + .addButton(async (button) => { + button.setButtonText(t("confirm")); + button.onClick(async () => { + if (importSettingVal !== "") { + // console.debug(importSettingVal); + try { + const inputParams = parseUriByHand(importSettingVal); + const parsed = importQrCodeUri( + inputParams, + this.app.vault.getName() + ); + if (parsed.status === "error") { + new Notice(parsed.message); + } else { + const copied = cloneDeep(parsed.result); + // new Notice(JSON.stringify(copied)) + this.plugin.settings = Object.assign( + {}, + this.plugin.settings, + copied + ); + this.plugin.saveSettings(); + new Notice( + t("protocol_saveqr", { + manifestName: this.plugin.manifest.name, + }) + ); + } + } catch (e) { + new Notice(`${e}`); + } + + importSettingVal = ""; + } else { + new Notice(t("settings_import_error_notice")); + importSettingVal = ""; + } + }); + }); + + ////////////////////////////////////////////////// + // below for pro + ////////////////////////////////////////////////// + + const proDiv = containerEl.createEl("div"); + + + ////////////////////////////////////////////////// + // below for debug + ////////////////////////////////////////////////// + + const debugDiv = containerEl.createEl("div"); + debugDiv.createEl("h2", { text: t("settings_debug"), cls: "byoc-section-heading" }); + + new Setting(debugDiv) + .setName(t("settings_debuglevel")) + .setDesc(t("settings_debuglevel_desc")) + .addDropdown(async (dropdown) => { + dropdown.addOption("info", "info"); + dropdown.addOption("debug", "debug"); + dropdown + .setValue(this.plugin.settings.currLogLevel ?? "info") + .onChange(async (val: string) => { + this.plugin.settings.currLogLevel = val; + await this.plugin.saveSettings(); + console.info(`the log level is changed to ${val}`); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_outputsettingsconsole")) + .setDesc(t("settings_outputsettingsconsole_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_outputsettingsconsole_button")); + button.onClick(async () => { + const c = messyConfigToNormal(await this.plugin.loadData()); + console.info(c); + new Notice(t("settings_outputsettingsconsole_notice")); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_obfuscatesettingfile")) + .setDesc(t("settings_obfuscatesettingfile_desc")) + .addDropdown(async (dropdown) => { + dropdown + .addOption("enable", t("enable")) + .addOption("disable", t("disable")); + + dropdown + .setValue( + `${ + this.plugin.settings.obfuscateSettingFile ? "enable" : "disable" + }` + ) + .onChange(async (val) => { + if (val === "enable") { + this.plugin.settings.obfuscateSettingFile = true; + } else { + this.plugin.settings.obfuscateSettingFile = false; + } + await this.plugin.saveSettings(); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_viewconsolelog")) + .setDesc(stringToFragment(t("settings_viewconsolelog_desc"))); + + const debugDivExportSyncPlans = new Setting(debugDiv) + .setName(t("settings_syncplans")) + .setDesc(t("settings_syncplans_desc")); + debugDivExportSyncPlans.settingEl.addClass("setting-need-wrapping"); + debugDivExportSyncPlans + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_1_only_change")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + 1, + true + ); + new Notice(t("settings_syncplans_notice")); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_5_only_change")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + 5, + true + ); + new Notice(t("settings_syncplans_notice")); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_1")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + 1, + false + ); + new Notice(t("settings_syncplans_notice")); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_5")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + 5, + false + ); + new Notice(t("settings_syncplans_notice")); + }); + }) + .addButton(async (button) => { + button.setButtonText(t("settings_syncplans_button_all")); + button.onClick(async () => { + await exportVaultSyncPlansToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID, + -1, + false + ); + new Notice(t("settings_syncplans_notice")); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_delsyncplans")) + .setDesc(t("settings_delsyncplans_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_delsyncplans_button")); + button.onClick(async () => { + await clearAllSyncPlanRecords(this.plugin.db); + new Notice(t("settings_delsyncplans_notice")); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_delprevsync")) + .setDesc(t("settings_delprevsync_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_delprevsync_button")); + button.onClick(async () => { + await clearAllPrevSyncRecordByVault( + this.plugin.db, + this.plugin.vaultRandomID + ); + new Notice(t("settings_delprevsync_notice")); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_profiler_results")) + .setDesc(t("settings_profiler_results_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_profiler_results_button_all")); + button.onClick(async () => { + await exportVaultProfilerResultsToFiles( + this.plugin.db, + this.app.vault, + this.plugin.vaultRandomID + ); + new Notice(t("settings_profiler_results_notice")); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_profiler_enableprofiler")) + .setDesc(t("settings_profiler_enableprofiler_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("enable", t("enable")); + dropdown.addOption("disable", t("disable")); + dropdown + .setValue( + this.plugin.settings.profiler?.enable ? "enable" : "disable" + ) + .onChange(async (val: string) => { + if (this.plugin.settings.profiler === undefined) { + this.plugin.settings.profiler = DEFAULT_PROFILER_CONFIG; + } + this.plugin.settings.profiler.enable = val === "enable"; + await this.plugin.saveSettings(); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_profiler_enabledebugprint")) + .setDesc(t("settings_profiler_enabledebugprint_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("enable", t("enable")); + dropdown.addOption("disable", t("disable")); + dropdown + .setValue( + this.plugin.settings.profiler?.enablePrinting ? "enable" : "disable" + ) + .onChange(async (val: string) => { + if (this.plugin.settings.profiler === undefined) { + this.plugin.settings.profiler = DEFAULT_PROFILER_CONFIG; + } + this.plugin.settings.profiler.enablePrinting = val === "enable"; + await this.plugin.saveSettings(); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_profiler_recordsize")) + .setDesc(t("settings_profiler_recordsize_desc")) + .addDropdown((dropdown) => { + dropdown.addOption("enable", t("enable")); + dropdown.addOption("disable", t("disable")); + dropdown + .setValue( + this.plugin.settings.profiler?.recordSize ? "enable" : "disable" + ) + .onChange(async (val: string) => { + if (this.plugin.settings.profiler === undefined) { + this.plugin.settings.profiler = DEFAULT_PROFILER_CONFIG; + } + this.plugin.settings.profiler.recordSize = val === "enable"; + await this.plugin.saveSettings(); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_outputbasepathvaultid")) + .setDesc(t("settings_outputbasepathvaultid_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_outputbasepathvaultid_button")); + button.onClick(async () => { + new Notice(this.plugin.getVaultBasePath()); + new Notice(this.plugin.vaultRandomID); + }); + }); + + new Setting(debugDiv) + .setName(t("settings_resetcache")) + .setDesc(t("settings_resetcache_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_resetcache_button")); + button.onClick(async () => { + await destroyDBs(); + new Notice(t("settings_resetcache_notice")); + this.plugin.unload(); + }); + }); + } + + hide() { + const { containerEl } = this; + containerEl.empty(); + super.hide(); + } +} diff --git a/src/settingsAzureBlobStorage.ts b/src/settingsAzureBlobStorage.ts new file mode 100644 index 0000000..cd4cdde --- /dev/null +++ b/src/settingsAzureBlobStorage.ts @@ -0,0 +1,135 @@ +import { SVG_AZURE } from './icons'; +import { type App, Notice, Setting } from "obsidian"; +import { DEFAULT_AZUREBLOBSTORAGE_CONFIG } from "./fsAzureBlobStorage"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; + +export const generateAzureBlobStorageSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const azureBlobStorageDiv = containerEl.createEl("div", { + cls: "azureblobstorage-hide", + }); + azureBlobStorageDiv.toggleClass( + "azureblobstorage-hide", + plugin.settings.serviceType !== "azureblobstorage" + ); + azureBlobStorageDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_AZURE} ${t("settings_azureblobstorage")}`; + + const azureDescDiv = azureBlobStorageDiv.createEl("div", { + cls: "settings-long-desc", + }); + azureDescDiv.createEl("p", { + text: t("settings_azureblobstorage_disclaimer"), + cls: "azureblobstorage-disclaimer" + }); + + const azureBlobStorageNotShowUpHintSetting = new Setting(azureBlobStorageDiv); + azureBlobStorageNotShowUpHintSetting.settingEl.addClass( + "azureblobstorage-allow-to-use-hide" + ); + + const azureBlobStorageAllowedToUsedDiv = azureBlobStorageDiv.createDiv(); + + new Setting(azureBlobStorageAllowedToUsedDiv) + .setName(t("settings_azureblobstorage_sasurl")) + .setDesc(t("settings_azureblobstorage_sasurl_desc")) + .addText((text) => { + text + .setPlaceholder("https://account.blob.core.windows.net/container?...") + .setValue(plugin.settings.azureblobstorage.containerSasUrl) + .onChange(async (val) => { + plugin.settings.azureblobstorage.containerSasUrl = val.trim(); + await saveUpdatedConfigFunc?.(); + }); + text.inputEl.type = "password"; + }); + + new Setting(azureBlobStorageAllowedToUsedDiv) + .setName(t("settings_azureblobstorage_prefix")) + .setDesc(t("settings_azureblobstorage_prefix_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(plugin.settings.azureblobstorage.remotePrefix) + .onChange(async (val) => { + plugin.settings.azureblobstorage.remotePrefix = val.trim(); + await saveUpdatedConfigFunc?.(); + }) + ); + + new Setting(azureBlobStorageAllowedToUsedDiv) + .setName(t("settings_azureblobstorage_generatefolderobject")) + .setDesc(t("settings_azureblobstorage_generatefolderobject_desc")) + .addDropdown((dropdown) => + dropdown + .addOption( + "false", + t("settings_azureblobstorage_generatefolderobject_notgenerate") + ) + .addOption( + "true", + t("settings_azureblobstorage_generatefolderobject_generate") + ) + .setValue( + plugin.settings.azureblobstorage.generateFolderObject + ? "true" + : "false" + ) + .onChange(async (val) => { + plugin.settings.azureblobstorage.generateFolderObject = val === "true"; + await saveUpdatedConfigFunc?.(); + }) + ); + + new Setting(azureBlobStorageAllowedToUsedDiv) + .setName(t("settings_azureblobstorage_parts")) + .setDesc(t("settings_azureblobstorage_parts_desc")) + .addText((text) => + text + .setPlaceholder("5") + .setValue( + `${plugin.settings.azureblobstorage.partsConcurrency ?? 5}` + ) + .onChange(async (val) => { + const n = parseInt(val); + if (!isNaN(n) && n > 0) { + plugin.settings.azureblobstorage.partsConcurrency = n; + await saveUpdatedConfigFunc?.(); + } + }) + ); + + new Setting(azureBlobStorageAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_azureblobstorage_connect_succ")); + } else { + new Notice(t("settings_azureblobstorage_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { + azureBlobStorageDiv, + azureBlobStorageAllowedToUsedDiv, + azureBlobStorageNotShowUpHintSetting, + }; +}; diff --git a/src/settingsBox.ts b/src/settingsBox.ts new file mode 100644 index 0000000..f1dcba8 --- /dev/null +++ b/src/settingsBox.ts @@ -0,0 +1,226 @@ +import { SVG_BOX } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { generateAuthUrl, DEFAULT_BOX_CONFIG } from "./fsBox"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal } from "./settings"; + +class BoxAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_BOX} Connect Box Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + const authUrl = generateAuthUrl(); + + const div2 = contentEl.createDiv(); + t("modal_boxauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +} + + onClose() { + this.contentEl.empty(); + } +} + +class BoxRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_BOX} Revoke Box Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_boxrevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = "https://app.box.com/account/security"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + t("modal_boxrevokeauth_step2").split("\n").forEach((val) => { contentEl.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_boxrevokeauth_clean")) + .setDesc(t("modal_boxrevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_boxrevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.box = cloneDeep(DEFAULT_BOX_CONFIG); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "box-auth-button-hide", + this.plugin.settings.box.refreshToken !== "" + ); + this.revokeAuthDiv.toggleClass( + "box-revoke-auth-button-hide", + this.plugin.settings.box.refreshToken === "" + ); + new Notice(t("modal_boxrevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_boxrevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +export const generateBoxSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const boxDiv = containerEl.createEl("div", { cls: "box-hide" }); + boxDiv.toggleClass("box-hide", plugin.settings.serviceType !== "box"); + boxDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_BOX} ${t("settings_box")}`; + + const boxLongDescDiv = boxDiv.createEl("div", { cls: "settings-long-desc" }); + boxLongDescDiv.createEl("p", { + text: t("settings_box_folder", { + remoteBaseDir: plugin.settings.box.remoteBaseDir || app.vault.getName(), + }), + }); + + const boxNotShowUpHintSetting = new Setting(boxDiv); + boxNotShowUpHintSetting.settingEl.addClass("box-allow-to-use-hide"); + + const boxAllowedToUsedDiv = boxDiv.createDiv(); + + const boxSelectAuthDiv = boxAllowedToUsedDiv.createDiv(); + const boxAuthDiv = boxSelectAuthDiv.createDiv({ + cls: "box-auth-button-hide settings-auth-related", + }); + const boxRevokeAuthDiv = boxSelectAuthDiv.createDiv({ + cls: "box-revoke-auth-button-hide settings-auth-related", + }); + + const boxRevokeAuthSetting = new Setting(boxRevokeAuthDiv) + .setName(t("settings_box_revoke")) + .setDesc(t("settings_box_revoke_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_box_revoke_button")); + button.onClick(async () => { + new BoxRevokeAuthModal(app, plugin, boxAuthDiv, boxRevokeAuthDiv, t).open(); + }); + }); + + new Setting(boxAuthDiv) + .setName(t("settings_box_auth")) + .setDesc(t("settings_box_auth_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_box_auth_button")); + button.onClick(async () => { + const modal = new BoxAuthModal( + app, + plugin, + boxAuthDiv, + boxRevokeAuthDiv, + boxRevokeAuthSetting, + t + ); + plugin.oauth2Info.helperModal = modal; + plugin.oauth2Info.authDiv = boxAuthDiv; + plugin.oauth2Info.revokeDiv = boxRevokeAuthDiv; + plugin.oauth2Info.revokeAuthSetting = boxRevokeAuthSetting; + modal.open(); + }); + }); + + const isConnected = !!plugin.settings.box?.refreshToken; + boxAuthDiv.toggleClass("box-auth-button-hide", isConnected); + boxRevokeAuthDiv.toggleClass("box-revoke-auth-button-hide", !isConnected); + + let newBoxRemoteBaseDir = plugin.settings.box.remoteBaseDir || ""; + new Setting(boxAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newBoxRemoteBaseDir) + .onChange((value) => { + newBoxRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal(app, plugin, newBoxRemoteBaseDir, "box").open(); + }); + }); + + new Setting(boxAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .setDesc(t("settings_checkonnectivity_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_box_connect_succ")); + } else { + new Notice(t("settings_box_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { boxDiv, boxAllowedToUsedDiv, boxNotShowUpHintSetting }; +}; diff --git a/src/settingsGoogleDrive.ts b/src/settingsGoogleDrive.ts new file mode 100644 index 0000000..c574fc1 --- /dev/null +++ b/src/settingsGoogleDrive.ts @@ -0,0 +1,311 @@ +import { SVG_GDRIVE } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { + DEFAULT_GOOGLEDRIVE_CONFIG, + generateAuthUrl, + sendAuthReq, + setConfigBySuccessfullAuthInplace, +} from "./fsGoogleDrive"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal } from "./settings"; + +// Google uses urn:ietf:wg:oauth:2.0:oob — copy-paste flow, no obsidian:// callback +class GoogleDriveAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_GDRIVE} Connect Google Drive Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + const authUrl = generateAuthUrl(); + + const div2 = contentEl.createDiv(); + t("modal_googledriveauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +let authCode = ""; + new Setting(contentEl) + .setName(t("modal_googledriveauth_codeinput")) + .setDesc(t("modal_googledriveauth_codeinput_desc")) + .addText((text) => + text + .setPlaceholder(t("modal_googledriveauth_codeinput_placeholder")) + .onChange((val) => { + authCode = val.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("modal_googledriveauth_codeinput_confirm")); + button.setCta(); + button.onClick(async () => { + if (!authCode) return; + new Notice(t("modal_googledriveauth_codeinput_notice")); + try { + const authRes = await sendAuthReq( + authCode, + "", + async (e: any) => { + new Notice(t("modal_googledriveauth_codeinput_conn_fail")); + new Notice(`${e}`); + } + ); + if (!authRes || authRes.error) { + new Notice(t("modal_googledriveauth_codeinput_conn_fail")); + return; + } + const self = this; + await setConfigBySuccessfullAuthInplace( + this.plugin.settings.googledrive, + authRes, + () => self.plugin.saveSettings() + ); + const isConnected = + this.plugin.settings.googledrive.refreshToken !== ""; + this.authDiv.toggleClass( + "googledrive-auth-button-hide", + isConnected + ); + this.revokeAuthDiv.toggleClass( + "googledrive-revoke-auth-button-hide", + !isConnected + ); + new Notice(t("modal_googledriveauth_codeinput_conn_succ")); + this.close(); + } catch (e) { + console.error(e); + new Notice(t("modal_googledriveauth_codeinput_conn_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +class GoogleDriveRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_GDRIVE} Revoke Google Drive Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_googledriverevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = + "https://myaccount.google.com/permissions"; + t("modal_googledriverevokeauth_step2").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_googledriverevokeauth_clean")) + .setDesc(t("modal_googledriverevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_googledriverevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.googledrive = cloneDeep( + DEFAULT_GOOGLEDRIVE_CONFIG + ); + await this.plugin.saveSettings(); + const isConnected = + this.plugin.settings.googledrive.refreshToken !== ""; + this.authDiv.toggleClass( + "googledrive-auth-button-hide", + isConnected + ); + this.revokeAuthDiv.toggleClass( + "googledrive-revoke-auth-button-hide", + !isConnected + ); + new Notice(t("modal_googledriverevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_googledriverevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +export const generateGoogleDriveSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const googleDriveDiv = containerEl.createEl("div", { + cls: "googledrive-hide", + }); + googleDriveDiv.toggleClass( + "googledrive-hide", + plugin.settings.serviceType !== "googledrive" + ); + googleDriveDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_GDRIVE} ${t("settings_googledrive")}`; + + const googleDriveLongDescDiv = googleDriveDiv.createEl("div", { + cls: "settings-long-desc", + }); + googleDriveLongDescDiv.createEl("p", { + text: t("settings_googledrive_folder", { + remoteBaseDir: + plugin.settings.googledrive.remoteBaseDir || app.vault.getName(), + }), + }); + + const googleDriveNotShowUpHintSetting = new Setting(googleDriveDiv); + googleDriveNotShowUpHintSetting.settingEl.addClass( + "googledrive-allow-to-use-hide" + ); + + const googleDriveAllowedToUsedDiv = googleDriveDiv.createDiv(); + + const googleDriveSelectAuthDiv = googleDriveAllowedToUsedDiv.createDiv(); + const googleDriveAuthDiv = googleDriveSelectAuthDiv.createDiv({ + cls: "googledrive-auth-button-hide settings-auth-related", + }); + const googleDriveRevokeAuthDiv = googleDriveSelectAuthDiv.createDiv({ + cls: "googledrive-revoke-auth-button-hide settings-auth-related", + }); + + const googleDriveRevokeAuthSetting = new Setting(googleDriveRevokeAuthDiv) + .setName(t("settings_googledrive_revoke")) + .setDesc(t("settings_googledrive_revoke_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_googledrive_revoke_button")); + button.onClick(async () => { + new GoogleDriveRevokeAuthModal( + app, + plugin, + googleDriveAuthDiv, + googleDriveRevokeAuthDiv, + t + ).open(); + }); + }); + + new Setting(googleDriveAuthDiv) + .setName(t("settings_googledrive_auth")) + .setDesc(t("settings_googledrive_auth_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_googledrive_auth_button")); + button.onClick(async () => { + new GoogleDriveAuthModal( + app, + plugin, + googleDriveAuthDiv, + googleDriveRevokeAuthDiv, + googleDriveRevokeAuthSetting, + t + ).open(); + }); + }); + + const isConnected = !!plugin.settings.googledrive?.refreshToken; + googleDriveAuthDiv.toggleClass("googledrive-auth-button-hide", isConnected); + googleDriveRevokeAuthDiv.toggleClass( + "googledrive-revoke-auth-button-hide", + !isConnected + ); + + let newGoogleDriveRemoteBaseDir = + plugin.settings.googledrive.remoteBaseDir || ""; + new Setting(googleDriveAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newGoogleDriveRemoteBaseDir) + .onChange((value) => { + newGoogleDriveRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + app, + plugin, + newGoogleDriveRemoteBaseDir, + "googledrive" + ).open(); + }); + }); + + new Setting(googleDriveAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .setDesc(t("settings_checkonnectivity_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_googledrive_connect_succ")); + } else { + new Notice(t("settings_googledrive_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { + googleDriveDiv, + googleDriveAllowedToUsedDiv, + googleDriveNotShowUpHintSetting, + }; +}; diff --git a/src/settingsKoofr.ts b/src/settingsKoofr.ts new file mode 100644 index 0000000..f9781ea --- /dev/null +++ b/src/settingsKoofr.ts @@ -0,0 +1,239 @@ +import { SVG_KOOFR } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { generateAuthUrl, DEFAULT_KOOFR_CONFIG } from "./fsKoofr"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal } from "./settings"; + +class KoofrAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_KOOFR} Connect Koofr Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + const authUrl = generateAuthUrl(); + + const div2 = contentEl.createDiv(); + t("modal_koofrauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +} + + onClose() { + this.contentEl.empty(); + } +} + +class KoofrRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_KOOFR} Revoke Koofr Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_koofrrevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = "https://app.koofr.net/app/admin/linked-apps"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + t("modal_koofrrevokeauth_step2").split("\n").forEach((val) => { contentEl.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_koofrrevokeauth_clean")) + .setDesc(t("modal_koofrrevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_koofrrevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.koofr = cloneDeep(DEFAULT_KOOFR_CONFIG); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "koofr-auth-button-hide", + this.plugin.settings.koofr.refreshToken !== "" + ); + this.revokeAuthDiv.toggleClass( + "koofr-revoke-auth-button-hide", + this.plugin.settings.koofr.refreshToken === "" + ); + new Notice(t("modal_koofrrevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_koofrrevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +export const generateKoofrSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const koofrDiv = containerEl.createEl("div", { cls: "koofr-hide" }); + koofrDiv.toggleClass("koofr-hide", plugin.settings.serviceType !== "koofr"); + koofrDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_KOOFR} ${t("settings_koofr")}`; + + const koofrLongDescDiv = koofrDiv.createEl("div", { + cls: "settings-long-desc", + }); + koofrLongDescDiv.createEl("p", { + text: t("settings_koofr_folder", { + remoteBaseDir: plugin.settings.koofr.remoteBaseDir || app.vault.getName(), + }), + }); + + const koofrNotShowUpHintSetting = new Setting(koofrDiv); + koofrNotShowUpHintSetting.settingEl.addClass("koofr-allow-to-use-hide"); + + const koofrAllowedToUsedDiv = koofrDiv.createDiv(); + + const koofrSelectAuthDiv = koofrAllowedToUsedDiv.createDiv(); + const koofrAuthDiv = koofrSelectAuthDiv.createDiv({ + cls: "koofr-auth-button-hide settings-auth-related", + }); + const koofrRevokeAuthDiv = koofrSelectAuthDiv.createDiv({ + cls: "koofr-revoke-auth-button-hide settings-auth-related", + }); + + const koofrRevokeAuthSetting = new Setting(koofrRevokeAuthDiv) + .setName(t("settings_koofr_revoke")) + .setDesc(t("settings_koofr_revoke_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_koofr_revoke_button")); + button.onClick(async () => { + new KoofrRevokeAuthModal( + app, + plugin, + koofrAuthDiv, + koofrRevokeAuthDiv, + t + ).open(); + }); + }); + + new Setting(koofrAuthDiv) + .setName(t("settings_koofr_auth")) + .setDesc(t("settings_koofr_auth_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_koofr_auth_button")); + button.onClick(async () => { + const modal = new KoofrAuthModal( + app, + plugin, + koofrAuthDiv, + koofrRevokeAuthDiv, + koofrRevokeAuthSetting, + t + ); + plugin.oauth2Info.helperModal = modal; + plugin.oauth2Info.authDiv = koofrAuthDiv; + plugin.oauth2Info.revokeDiv = koofrRevokeAuthDiv; + plugin.oauth2Info.revokeAuthSetting = koofrRevokeAuthSetting; + modal.open(); + }); + }); + + const isConnected = !!plugin.settings.koofr?.refreshToken; + koofrAuthDiv.toggleClass("koofr-auth-button-hide", isConnected); + koofrRevokeAuthDiv.toggleClass("koofr-revoke-auth-button-hide", !isConnected); + + let newKoofrRemoteBaseDir = plugin.settings.koofr.remoteBaseDir || ""; + new Setting(koofrAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newKoofrRemoteBaseDir) + .onChange((value) => { + newKoofrRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + app, + plugin, + newKoofrRemoteBaseDir, + "koofr" + ).open(); + }); + }); + + new Setting(koofrAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .setDesc(t("settings_checkonnectivity_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_koofr_connect_succ")); + } else { + new Notice(t("settings_koofr_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { koofrDiv, koofrAllowedToUsedDiv, koofrNotShowUpHintSetting }; +}; diff --git a/src/settingsOnedriveFull.ts b/src/settingsOnedriveFull.ts new file mode 100644 index 0000000..62c230e --- /dev/null +++ b/src/settingsOnedriveFull.ts @@ -0,0 +1,276 @@ +import { SVG_ONEDRIVE } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { + DEFAULT_ONEDRIVEFULL_CONFIG, + getAuthUrlAndVerifier, +} from "./fsOnedriveFull"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal } from "./settings"; + +class OnedrivefullAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_ONEDRIVE} Connect OneDrive (Full) Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + + t("modal_onedrivefullauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + + try { + const { authUrl, verifier } = await getAuthUrlAndVerifier( + this.plugin.settings.onedrivefull.clientID, + this.plugin.settings.onedrivefull.authority + ); + + this.plugin.oauth2Info.verifier = verifier; + + const div2 = contentEl.createDiv(); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +} catch (e) { + console.error(e); + contentEl.createEl("p", { + text: t("protocol_onedrivefull_connect_fail"), + }); + } + } + + onClose() { + this.contentEl.empty(); + } +} + +class OnedrivefullRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_ONEDRIVE} Revoke OneDrive (Full) Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_onedrivefullrevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = "https://microsoft.com/consent"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + t("modal_onedrivefullrevokeauth_step2").split("\n").forEach((val) => { contentEl.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_onedrivefullrevokeauth_clean")) + .setDesc(t("modal_onedrivefullrevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_onedrivefullrevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.onedrivefull = cloneDeep(DEFAULT_ONEDRIVEFULL_CONFIG); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "onedrivefull-auth-button-hide", + this.plugin.settings.onedrivefull.username !== "" + ); + this.revokeAuthDiv.toggleClass( + "onedrivefull-revoke-auth-button-hide", + this.plugin.settings.onedrivefull.username === "" + ); + new Notice(t("modal_onedrivefullrevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_onedrivefullrevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +export const generateOnedriveFullSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const onedriveFullDiv = containerEl.createEl("div", { + cls: "onedrivefull-hide", + }); + onedriveFullDiv.toggleClass( + "onedrivefull-hide", + plugin.settings.serviceType !== "onedrivefull" + ); + onedriveFullDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_ONEDRIVE} ${t("settings_onedrivefull")}`; + + const onedriveFullLongDescDiv = onedriveFullDiv.createEl("div", { + cls: "settings-long-desc", + }); + onedriveFullLongDescDiv.createEl("p", { + text: t("settings_onedrivefull_folder", { + remoteBaseDir: + plugin.settings.onedrivefull.remoteBaseDir || app.vault.getName(), + }), + }); + + const onedriveFullNotShowUpHintSetting = new Setting(onedriveFullDiv); + onedriveFullNotShowUpHintSetting.settingEl.addClass( + "onedrivefull-allow-to-use-hide" + ); + + const onedriveFullAllowedToUsedDiv = onedriveFullDiv.createDiv(); + + const onedriveFullSelectAuthDiv = onedriveFullAllowedToUsedDiv.createDiv(); + const onedriveFullAuthDiv = onedriveFullSelectAuthDiv.createDiv({ + cls: "onedrivefull-auth-button-hide settings-auth-related", + }); + const onedriveFullRevokeAuthDiv = onedriveFullSelectAuthDiv.createDiv({ + cls: "onedrivefull-revoke-auth-button-hide settings-auth-related", + }); + + const onedriveFullRevokeAuthSetting = new Setting(onedriveFullRevokeAuthDiv) + .setName(t("settings_onedrivefull_revoke")) + .setDesc( + t("protocol_onedrivefull_connect_succ_revoke", { + username: plugin.settings.onedrivefull.username || "", + }) + ) + .addButton(async (button) => { + button.setButtonText(t("settings_onedrivefull_revoke_button")); + button.onClick(async () => { + new OnedrivefullRevokeAuthModal( + app, + plugin, + onedriveFullAuthDiv, + onedriveFullRevokeAuthDiv, + t + ).open(); + }); + }); + + new Setting(onedriveFullAuthDiv) + .setName(t("settings_onedrivefull_auth")) + .setDesc(t("settings_onedrivefull_auth_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_onedrivefull_auth_button")); + button.onClick(async () => { + const modal = new OnedrivefullAuthModal( + app, + plugin, + onedriveFullAuthDiv, + onedriveFullRevokeAuthDiv, + onedriveFullRevokeAuthSetting, + t + ); + plugin.oauth2Info.helperModal = modal; + plugin.oauth2Info.authDiv = onedriveFullAuthDiv; + plugin.oauth2Info.revokeDiv = onedriveFullRevokeAuthDiv; + plugin.oauth2Info.revokeAuthSetting = onedriveFullRevokeAuthSetting; + modal.open(); + }); + }); + + const isConnected = !!plugin.settings.onedrivefull?.username; + onedriveFullAuthDiv.toggleClass("onedrivefull-auth-button-hide", isConnected); + onedriveFullRevokeAuthDiv.toggleClass( + "onedrivefull-revoke-auth-button-hide", + !isConnected + ); + + let newOnedriveFullRemoteBaseDir = + plugin.settings.onedrivefull.remoteBaseDir || ""; + new Setting(onedriveFullAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newOnedriveFullRemoteBaseDir) + .onChange((value) => { + newOnedriveFullRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + app, + plugin, + newOnedriveFullRemoteBaseDir, + "onedrivefull" + ).open(); + }); + }); + + new Setting(onedriveFullAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .setDesc(t("settings_checkonnectivity_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_onedrivefull_connect_succ")); + } else { + new Notice(t("settings_onedrivefull_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { + onedriveFullDiv, + onedriveFullAllowedToUsedDiv, + onedriveFullNotShowUpHintSetting, + }; +}; diff --git a/src/settingsPCloud.ts b/src/settingsPCloud.ts new file mode 100644 index 0000000..25a592e --- /dev/null +++ b/src/settingsPCloud.ts @@ -0,0 +1,249 @@ +import { SVG_PCLOUD } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal, ExportSettingsQrCodeModal } from "./settings"; +import { DEFAULT_PCLOUD_CONFIG, generateAuthUrl } from "./fsPCloud"; + +class PCloudAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_PCLOUD} Connect pCloud Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + + const { authUrl } = await generateAuthUrl(true); + const div2 = contentEl.createDiv(); + t("modal_pcloudauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +} + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +class PCloudRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_PCLOUD} Revoke pCloud Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_pcloudrevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = "https://my.pcloud.com/#page=settings&settings=tab-apps"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + t("modal_pcloudrevokeauth_step2").split("\n").forEach((val) => { contentEl.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_pcloudrevokeauth_clean")) + .setDesc(t("modal_pcloudrevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_pcloudrevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.pcloud = cloneDeep(DEFAULT_PCLOUD_CONFIG); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "pcloud-auth-button-hide", + this.plugin.settings.pcloud.accessToken !== "" + ); + this.revokeAuthDiv.toggleClass( + "pcloud-revoke-auth-button-hide", + this.plugin.settings.pcloud.accessToken === "" + ); + new Notice(t("modal_pcloudrevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_pcloudrevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + } +} + +export const generatePCloudSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const pCloudDiv = containerEl.createEl("div", { cls: "pcloud-hide" }); + pCloudDiv.toggleClass("pcloud-hide", plugin.settings.serviceType !== "pcloud"); + pCloudDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_PCLOUD} ${t("settings_pcloud")}`; + + // Remote folder info + pCloudDiv.createEl("p", { + cls: "settings-long-desc", + text: t("settings_pcloud_folder", { + remoteBaseDir: plugin.settings.pcloud.remoteBaseDir || app.vault.getName(), + }), + }); + + const pCloudNotShowUpHintSetting = new Setting(pCloudDiv); + pCloudNotShowUpHintSetting.settingEl.addClass("pcloud-allow-to-use-hide"); + + const pCloudAllowedToUsedDiv = pCloudDiv.createDiv(); + const pcloudSelectAuthDiv = pCloudAllowedToUsedDiv.createDiv(); + + // ── Auth button div (shown when NOT connected) ────────────────────────── + const pcloudAuthDiv = pcloudSelectAuthDiv.createDiv({ + cls: "pcloud-auth-button-hide settings-auth-related", + }); + + // ── Revoke / connected div (shown when connected) ─────────────────────── + const pcloudRevokeAuthDiv = pcloudSelectAuthDiv.createDiv({ + cls: "pcloud-revoke-auth-button-hide settings-auth-related", + }); + + const hasAccessToken = !!plugin.settings.pcloud?.accessToken; + const savedUsername = plugin.settings.pcloud?.username; + + // "Logged in as" display — only rendered if we have a stored username + if (savedUsername) { + new Setting(pcloudRevokeAuthDiv) + .setName("Logged in as") + .setDesc(savedUsername); + } + + // Revoke button row + const pcloudRevokeAuthSetting = new Setting(pcloudRevokeAuthDiv) + .setName(t("settings_pcloud_revoke")) + .addButton(async (button) => { + button.setButtonText(t("settings_pcloud_revoke_button")); + button.setWarning(); + button.onClick(async () => { + new PCloudRevokeAuthModal( + app, + plugin, + pcloudAuthDiv, + pcloudRevokeAuthDiv, + t + ).open(); + }); + }); + + // Auth button row + new Setting(pcloudAuthDiv) + .setName(t("settings_pcloud_auth")) + .addButton(async (button) => { + button.setButtonText(t("settings_pcloud_auth_button")); + button.setCta(); + button.onClick(async () => { + const modal = new PCloudAuthModal( + app, + plugin, + pcloudAuthDiv, + pcloudRevokeAuthDiv, + pcloudRevokeAuthSetting, + t + ); + plugin.oauth2Info.helperModal = modal; + plugin.oauth2Info.authDiv = pcloudAuthDiv; + plugin.oauth2Info.revokeDiv = pcloudRevokeAuthDiv; + plugin.oauth2Info.revokeAuthSetting = pcloudRevokeAuthSetting; + modal.open(); + }); + }); + + pcloudAuthDiv.toggleClass("pcloud-auth-button-hide", hasAccessToken); + pcloudRevokeAuthDiv.toggleClass("pcloud-revoke-auth-button-hide", !hasAccessToken); + + // Remote base dir + let newpcloudRemoteBaseDir = plugin.settings.pcloud.remoteBaseDir || ""; + new Setting(pCloudAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newpcloudRemoteBaseDir) + .onChange((value) => { newpcloudRemoteBaseDir = value.trim(); }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal(app, plugin, newpcloudRemoteBaseDir, "pcloud").open(); + }); + }); + + // Check connectivity + new Setting(pCloudAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { errors.msg = `${err}`; }); + if (res) { + new Notice(t("settings_pcloud_connect_succ")); + } else { + new Notice(t("settings_pcloud_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { + pCloudDiv, + pCloudAllowedToUsedDiv, + pCloudNotShowUpHintSetting, + }; +}; diff --git a/src/settingsProviderStubs.ts b/src/settingsProviderStubs.ts new file mode 100644 index 0000000..6d1414c --- /dev/null +++ b/src/settingsProviderStubs.ts @@ -0,0 +1,8 @@ +// Stubs for features not yet implemented +export function generateClearDupFilesSettingsPart(...args: any[]): any { + return {}; +} + +export function generateProSettingsPart(...args: any[]): any { + return {}; +} diff --git a/src/settingsYandexDisk.ts b/src/settingsYandexDisk.ts new file mode 100644 index 0000000..4b42ca5 --- /dev/null +++ b/src/settingsYandexDisk.ts @@ -0,0 +1,248 @@ +import { SVG_YANDEX } from './icons'; +import cloneDeep from "lodash/cloneDeep"; +import { type App, Modal, Notice, Setting } from "obsidian"; +import { generateAuthUrl, DEFAULT_YANDEXDISK_CONFIG } from "./fsYandexDisk"; +import { getClient } from "./fsGetter"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; +import { stringToFragment } from "./misc"; +import { ChangeRemoteBaseDirModal } from "./settings"; + +class YandexDiskAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly revokeAuthSetting: Setting; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + revokeAuthSetting: Setting, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.revokeAuthSetting = revokeAuthSetting; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_YANDEX} Connect Yandex Disk Account`; + this.modalEl.addClass("byoc-auth-modal"); + const { contentEl } = this; + const t = this.t; + const authUrl = generateAuthUrl(); + + const div2 = contentEl.createDiv(); + t("modal_yandexdiskauth_tutorial").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + contentEl.createEl("button", { text: "Open Authorization in Browser" }, (el) => { el.onclick = () => window.open(authUrl); }); + +} + + onClose() { + this.contentEl.empty(); + } +} + +class YandexDiskRevokeAuthModal extends Modal { + readonly plugin: RemotelySavePlugin; + readonly authDiv: HTMLDivElement; + readonly revokeAuthDiv: HTMLDivElement; + readonly t: (x: TransItemType, vars?: any) => string; + constructor( + app: App, + plugin: RemotelySavePlugin, + authDiv: HTMLDivElement, + revokeAuthDiv: HTMLDivElement, + t: (x: TransItemType, vars?: any) => string + ) { + super(app); + this.plugin = plugin; + this.authDiv = authDiv; + this.revokeAuthDiv = revokeAuthDiv; + this.t = t; + } + + async onOpen() { + this.titleEl.innerHTML = `${SVG_YANDEX} Revoke Yandex Disk Account`; + this.modalEl.addClass("byoc-auth-modal"); + const t = this.t; + const { contentEl } = this; + + t("modal_yandexdiskrevokeauth_step1").split("\n").forEach((val) => { div2.createEl("p", { text: val }); }); + const consentUrl = "https://id.yandex.com/security/apps"; + contentEl.createEl("p").createEl("a", { + href: consentUrl, + text: consentUrl, + }); + t("modal_yandexdiskrevokeauth_step2").split("\n").forEach((val) => { contentEl.createEl("p", { text: val }); }); + + new Setting(contentEl) + .setName(t("modal_yandexdiskrevokeauth_clean")) + .setDesc(t("modal_yandexdiskrevokeauth_clean_desc")) + .addButton(async (button) => { + button.setButtonText(t("modal_yandexdiskrevokeauth_clean_button")); + button.onClick(async () => { + try { + this.plugin.settings.yandexdisk = cloneDeep(DEFAULT_YANDEXDISK_CONFIG); + await this.plugin.saveSettings(); + this.authDiv.toggleClass( + "yandexdisk-auth-button-hide", + this.plugin.settings.yandexdisk.refreshToken !== "" + ); + this.revokeAuthDiv.toggleClass( + "yandexdisk-revoke-auth-button-hide", + this.plugin.settings.yandexdisk.refreshToken === "" + ); + new Notice(t("modal_yandexdiskrevokeauth_clean_notice")); + this.close(); + } catch (err) { + console.error(err); + new Notice(t("modal_yandexdiskrevokeauth_clean_fail")); + } + }); + }); + } + + onClose() { + this.contentEl.empty(); + } +} + +export const generateYandexDiskSettingsPart = ( + containerEl: HTMLElement, + t: (x: TransItemType, vars?: any) => string, + app: App, + plugin: RemotelySavePlugin, + saveUpdatedConfigFunc: () => Promise | undefined +) => { + const yandexDiskDiv = containerEl.createEl("div", { cls: "yandexdisk-hide" }); + yandexDiskDiv.toggleClass( + "yandexdisk-hide", + plugin.settings.serviceType !== "yandexdisk" + ); + yandexDiskDiv.createEl("h2", { cls: "byoc-provider-heading" }).innerHTML = `${SVG_YANDEX} ${t("settings_yandexdisk")}`; + + const yandexDiskLongDescDiv = yandexDiskDiv.createEl("div", { + cls: "settings-long-desc", + }); + yandexDiskLongDescDiv.createEl("p", { + text: t("settings_yandexdisk_folder", { + remoteBaseDir: + plugin.settings.yandexdisk.remoteBaseDir || app.vault.getName(), + }), + }); + + const yandexDiskNotShowUpHintSetting = new Setting(yandexDiskDiv); + yandexDiskNotShowUpHintSetting.settingEl.addClass("yandexdisk-allow-to-use-hide"); + + const yandexDiskAllowedToUsedDiv = yandexDiskDiv.createDiv(); + + const yandexDiskSelectAuthDiv = yandexDiskAllowedToUsedDiv.createDiv(); + const yandexDiskAuthDiv = yandexDiskSelectAuthDiv.createDiv({ + cls: "yandexdisk-auth-button-hide settings-auth-related", + }); + const yandexDiskRevokeAuthDiv = yandexDiskSelectAuthDiv.createDiv({ + cls: "yandexdisk-revoke-auth-button-hide settings-auth-related", + }); + + const yandexDiskRevokeAuthSetting = new Setting(yandexDiskRevokeAuthDiv) + .setName(t("settings_yandexdisk_revoke")) + .setDesc(t("settings_yandexdisk_revoke_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_yandexdisk_revoke_button")); + button.onClick(async () => { + new YandexDiskRevokeAuthModal( + app, + plugin, + yandexDiskAuthDiv, + yandexDiskRevokeAuthDiv, + t + ).open(); + }); + }); + + new Setting(yandexDiskAuthDiv) + .setName(t("settings_yandexdisk_auth")) + .setDesc(t("settings_yandexdisk_auth_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_yandexdisk_auth_button")); + button.onClick(async () => { + const modal = new YandexDiskAuthModal( + app, + plugin, + yandexDiskAuthDiv, + yandexDiskRevokeAuthDiv, + yandexDiskRevokeAuthSetting, + t + ); + plugin.oauth2Info.helperModal = modal; + plugin.oauth2Info.authDiv = yandexDiskAuthDiv; + plugin.oauth2Info.revokeDiv = yandexDiskRevokeAuthDiv; + plugin.oauth2Info.revokeAuthSetting = yandexDiskRevokeAuthSetting; + modal.open(); + }); + }); + + const isConnected = !!plugin.settings.yandexdisk?.refreshToken; + yandexDiskAuthDiv.toggleClass("yandexdisk-auth-button-hide", isConnected); + yandexDiskRevokeAuthDiv.toggleClass("yandexdisk-revoke-auth-button-hide", !isConnected); + + let newYandexDiskRemoteBaseDir = + plugin.settings.yandexdisk.remoteBaseDir || ""; + new Setting(yandexDiskAllowedToUsedDiv) + .setName(t("settings_remotebasedir")) + .setDesc(t("settings_remotebasedir_desc")) + .addText((text) => + text + .setPlaceholder(app.vault.getName()) + .setValue(newYandexDiskRemoteBaseDir) + .onChange((value) => { + newYandexDiskRemoteBaseDir = value.trim(); + }) + ) + .addButton((button) => { + button.setButtonText(t("confirm")); + button.onClick(() => { + new ChangeRemoteBaseDirModal( + app, + plugin, + newYandexDiskRemoteBaseDir, + "yandexdisk" + ).open(); + }); + }); + + new Setting(yandexDiskAllowedToUsedDiv) + .setName(t("settings_checkonnectivity")) + .setDesc(t("settings_checkonnectivity_desc")) + .addButton(async (button) => { + button.setButtonText(t("settings_checkonnectivity_button")); + button.onClick(async () => { + new Notice(t("settings_checkonnectivity_checking")); + const client = getClient(plugin.settings, app.vault.getName(), () => + plugin.saveSettings() + ); + const errors = { msg: "" }; + const res = await client.checkConnect((err: any) => { + errors.msg = `${err}`; + }); + if (res) { + new Notice(t("settings_yandexdisk_connect_succ")); + } else { + new Notice(t("settings_yandexdisk_connect_fail")); + new Notice(errors.msg); + } + }); + }); + + return { + yandexDiskDiv, + yandexDiskAllowedToUsedDiv, + yandexDiskNotShowUpHintSetting, + }; +}; diff --git a/src/sync/conflict.ts b/src/sync/conflict.ts new file mode 100644 index 0000000..1b06a87 --- /dev/null +++ b/src/sync/conflict.ts @@ -0,0 +1,20 @@ +/** + * Generates a sync conflict file name. + * Format: [original-name] (sync-conflict-[timestamp])[.ext] + * @param filepath The original file path + * @returns The conflict file path + */ +export const generateConflictFileName = (filepath: string): string => { + const parts = filepath.split("."); + const now = new Date(); + + // Create a minimal timestamp exactly as BYOC requires + const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}${String(now.getSeconds()).padStart(2, "0")}`; + + if (parts.length > 1 && !filepath.startsWith(".")) { + const ext = parts.pop(); + const base = parts.join("."); + return `${base} (sync-conflict-${timestamp}).${ext}`; + } + return `${filepath} (sync-conflict-${timestamp})`; +}; diff --git a/src/sync/planner.ts b/src/sync/planner.ts new file mode 100644 index 0000000..a6f7596 --- /dev/null +++ b/src/sync/planner.ts @@ -0,0 +1,168 @@ +import type { MixedEntity, ConflictActionType, DecisionTypeForMixedEntity } from "../baseTypes"; + +// Two timestamps are considered equal if within 2 seconds of each other. +// Necessary because providers and local FS have different mtime precision. +const MTIME_TOLERANCE_MS = 2000; + +const mtimeChanged = (current?: number, baseline?: number): boolean => { + if (current === undefined || baseline === undefined) return false; + return Math.abs(current - baseline) > MTIME_TOLERANCE_MS; +}; + +const newerSide = ( + local?: number, + remote?: number +): "local" | "remote" | "equal" => { + if (local === undefined && remote === undefined) return "equal"; + if (local === undefined) return "remote"; + if (remote === undefined) return "local"; + if (Math.abs(local - remote) <= MTIME_TOLERANCE_MS) return "equal"; + return local > remote ? "local" : "remote"; +}; + +const largerSide = ( + localSize?: number, + remoteSize?: number +): "local" | "remote" | "equal" => { + if (localSize === undefined && remoteSize === undefined) return "equal"; + if (localSize === undefined) return "remote"; + if (remoteSize === undefined) return "local"; + if (localSize === remoteSize) return "equal"; + return localSize > remoteSize ? "local" : "remote"; +}; + +/** + * determineSyncDecision computes the 3-way merge result for a single entity. + * + * State matrix: + * local | remote | prevSync → decision + * ──────┼────────┼────────────────────────────── + * ✗ | ✗ | any → only_history (already gone both sides) + * ✗ | ✓ | ✗ → remote_is_created_then_pull + * ✓ | ✗ | ✗ → local_is_created_then_push + * ✓ | ✓ | ✗ → conflict_created (both added independently) + * ✗ | ✓ | ✓ → check remote vs prevSync: + * remote unchanged → local deleted → delete remote + * remote changed → conflict (delete vs modify) + * ✓ | ✗ | ✓ → check local vs prevSync: + * local unchanged → remote deleted → delete local + * local changed → conflict (modify vs delete) + * ✓ | ✓ | ✓ → compare each side to baseline → push/pull/conflict/equal + */ +export const determineSyncDecision = ( + node: MixedEntity, + conflictAction: ConflictActionType +): DecisionTypeForMixedEntity => { + const { local, remote, prevSync } = node; + + const hasLocal = local !== undefined; + const hasRemote = remote !== undefined; + const hasPrev = prevSync !== undefined; + + // ── Both missing ───────────────────────────────────────────────────────── + if (!hasLocal && !hasRemote) { + return "only_history"; + } + + // ── One side present, no history ───────────────────────────────────────── + if (hasLocal && !hasRemote && !hasPrev) { + return "local_is_created_then_push"; + } + + if (!hasLocal && hasRemote && !hasPrev) { + return "remote_is_created_then_pull"; + } + + // ── Both present, no history → created independently on both sides ─────── + if (hasLocal && hasRemote && !hasPrev) { + return resolveCreatedConflict(conflictAction, local, remote); + } + + // ── Local missing, prev exists → local was deleted ─────────────────────── + if (!hasLocal && hasRemote && hasPrev) { + const remoteChanged = + mtimeChanged(remote.mtimeSvr, prevSync.mtimeSvr) || + mtimeChanged(remote.mtimeCli, prevSync.mtimeCli) || + remote.sizeRaw !== prevSync.sizeRaw; + + if (remoteChanged) { + // Remote was modified after our last sync — treat as conflict + return resolveModifiedConflict(conflictAction, local, remote); + } + return "local_is_deleted_thus_also_delete_remote"; + } + + // ── Remote missing, prev exists → remote was deleted ───────────────────── + if (hasLocal && !hasRemote && hasPrev) { + const localChanged = + mtimeChanged(local.mtimeCli, prevSync.mtimeCli) || + local.sizeRaw !== prevSync.sizeRaw; + + if (localChanged) { + // Local was modified after our last sync — treat as conflict + return resolveModifiedConflict(conflictAction, local, remote); + } + return "remote_is_deleted_thus_also_delete_local"; + } + + // ── Both present with history → compare each side to baseline ──────────── + if (hasLocal && hasRemote && hasPrev) { + const localChanged = + mtimeChanged(local.mtimeCli, prevSync.mtimeCli) || + local.sizeRaw !== prevSync.sizeRaw; + + const remoteChanged = + mtimeChanged(remote.mtimeSvr, prevSync.mtimeSvr) || + mtimeChanged(remote.mtimeCli, prevSync.mtimeCli) || + remote.sizeRaw !== prevSync.sizeRaw; + + if (!localChanged && !remoteChanged) return "equal"; + if (localChanged && !remoteChanged) return "local_is_modified_then_push"; + if (!localChanged && remoteChanged) return "remote_is_modified_then_pull"; + + // Both changed → true conflict + return resolveModifiedConflict(conflictAction, local, remote); + } + + return "equal"; +}; + +const resolveCreatedConflict = ( + action: ConflictActionType, + local: any, + remote: any +): DecisionTypeForMixedEntity => { + if (action === "keep_newer") { + const side = newerSide(local?.mtimeCli, remote?.mtimeCli ?? remote?.mtimeSvr); + return side === "local" + ? "conflict_created_then_keep_local" + : "conflict_created_then_keep_remote"; + } + if (action === "keep_larger") { + const side = largerSide(local?.sizeRaw, remote?.sizeRaw); + return side === "local" + ? "conflict_created_then_keep_local" + : "conflict_created_then_keep_remote"; + } + return "conflict_created_then_smart_conflict"; +}; + +const resolveModifiedConflict = ( + action: ConflictActionType, + local: any, + remote: any +): DecisionTypeForMixedEntity => { + if (action === "keep_newer") { + const side = newerSide(local?.mtimeCli, remote?.mtimeCli ?? remote?.mtimeSvr); + return side === "local" + ? "conflict_modified_then_keep_local" + : "conflict_modified_then_keep_remote"; + } + if (action === "keep_larger") { + const side = largerSide(local?.sizeRaw, remote?.sizeRaw); + return side === "local" + ? "conflict_modified_then_keep_local" + : "conflict_modified_then_keep_remote"; + } + return "conflict_modified_then_smart_conflict"; +}; diff --git a/src/sync/syncer.ts b/src/sync/syncer.ts new file mode 100644 index 0000000..a64141f --- /dev/null +++ b/src/sync/syncer.ts @@ -0,0 +1,244 @@ +import type { FakeFs } from "../fsAll"; +import type { FakeFsEncrypt } from "../fsEncrypt"; +import type { FakeFsLocal } from "../fsLocal"; +import type { InternalDBs } from "../localdb"; +import type { Profiler } from "../profiler"; +import type { BYOCPluginSettings, SyncTriggerSourceType, Entity, MixedEntity } from "../baseTypes"; +import { determineSyncDecision } from "./planner"; +import { generateConflictFileName } from "./conflict"; +import { copyFileOrFolder } from "../copyLogic"; + +// ─── Folder-aware sorter ─────────────────────────────────────────────────────── +// Correct execution order to prevent parent-before-child violations: +// 1. Folders to CREATE — ascending depth (parents first) +// 2. Files — any order +// 3. Folders to DELETE — descending depth (children first) +const folderDepth = (key: string) => key.split("/").length - 1; + +function sortSyncActions(actions: MixedEntity[]): MixedEntity[] { + const foldersToCreate: MixedEntity[] = []; + const files: MixedEntity[] = []; + const foldersToDelete: MixedEntity[] = []; + const rest: MixedEntity[] = []; + + for (const node of actions) { + const isFolder = node.key.endsWith("/"); + const d = node.decision; + + if (isFolder) { + const isCreate = + d === "folder_to_be_created" || + d === "folder_existed_local_then_also_create_remote" || + d === "folder_existed_remote_then_also_create_local" || + d === "local_is_created_then_push" || + d === "remote_is_created_then_pull"; + + const isDelete = + d === "folder_to_be_deleted_on_both" || + d === "folder_to_be_deleted_on_remote" || + d === "folder_to_be_deleted_on_local" || + d === "remote_is_deleted_thus_also_delete_local" || + d === "local_is_deleted_thus_also_delete_remote"; + + if (isCreate) { + foldersToCreate.push(node); + } else if (isDelete) { + foldersToDelete.push(node); + } else { + rest.push(node); + } + } else { + files.push(node); + } + } + + foldersToCreate.sort((a, b) => folderDepth(a.key) - folderDepth(b.key)); + foldersToDelete.sort((a, b) => folderDepth(b.key) - folderDepth(a.key)); + + return [...foldersToCreate, ...files, ...foldersToDelete, ...rest]; +} + +/** + * BYOC — Sync Engine orchestrator. (Clean-Room Implementation) + */ +export async function syncer( + fsLocal: FakeFsLocal, + fsRemote: FakeFs, + fsEncrypt: FakeFsEncrypt, + profiler: Profiler | undefined, + db: InternalDBs, + triggerSource: SyncTriggerSourceType, + profileID: string, + vaultRandomID: string, + configDir: string, + settings: BYOCPluginSettings, + pluginVersion: string, + configSaver: () => Promise, + getProtectError: ( + protectModifyPercentage: number, + realModifyDeleteCount: number, + allFilesCount: number + ) => string, + markIsSyncingFunc: (isSyncing: boolean) => Promise, + notifyFunc: (s: SyncTriggerSourceType, step: number) => Promise, + errNotifyFunc: (s: SyncTriggerSourceType, error: Error) => Promise, + ribbonFunc: (s: SyncTriggerSourceType, step: number) => Promise, + statusBarFunc: (s: SyncTriggerSourceType, step: number, everythingOk: boolean) => Promise, + callbackSyncProcess: ( + s: SyncTriggerSourceType, + realCounter: number, + realTotalCount: number, + pathName: string, + decision: string + ) => Promise +): Promise { + await markIsSyncingFunc(true); + try { + // Phase 1: Fetching + await statusBarFunc(triggerSource, 1, true); // Prepare + const remoteFsTarget = settings.password !== "" ? fsEncrypt : fsRemote; + + const [localWalk, remoteWalk, prevSyncItemsRaw] = await Promise.all([ + fsLocal.walk(), + remoteFsTarget.walk(), + db.prevSyncRecordsTbl.getItem(profileID) + ]); + + const prevSyncItems = prevSyncItemsRaw || []; + + // Matrix Assembly + const nodes = new Map(); + + // Seed missing history + for (const p of prevSyncItems) { + if (!nodes.has(p.keyRaw)) { + nodes.set(p.keyRaw, { key: p.keyRaw, prevSync: p }); + } else { + nodes.get(p.keyRaw)!.prevSync = p; + } + } + + for (const l of localWalk) { + if (!nodes.has(l.keyRaw)) { + nodes.set(l.keyRaw, { key: l.keyRaw, local: l }); + } else { + nodes.get(l.keyRaw)!.local = l; + } + } + + for (const r of remoteWalk) { + if (!nodes.has(r.keyRaw)) { + nodes.set(r.keyRaw, { key: r.keyRaw, remote: r }); + } else { + nodes.get(r.keyRaw)!.remote = r; + } + } + + // Phase 2: Planner + const unsortedActions = Array.from(nodes.values()).map(node => { + node.decision = determineSyncDecision(node, settings.conflictAction || "smart_conflict"); + return node; + }); + + // M1: Enforce folder-before-file creation order, file-before-folder delete order. + const syncActions = sortSyncActions(unsortedActions); + + // M2: Skip protection check on empty vault (avoids division by zero/NaN). + if (localWalk.length > 0) { + let deleteModifyCount = 0; + for (const action of syncActions) { + if (action.decision?.includes("delete") || action.decision?.includes("pull")) { + deleteModifyCount++; + } + } + + const protectErr = getProtectError(settings.protectModifyPercentage || 50, deleteModifyCount, localWalk.length); + if (protectErr !== "") { + throw Error(`Protection Triggered: ${protectErr}`); + } + } + + // Phase 3: Execution Engine + await statusBarFunc(triggerSource, 7, true); // Exchanging data + + const successfulCommits: Entity[] = []; + let counter = 0; + + for (const node of syncActions) { + const decision = node.decision; + if (decision === "equal" || decision === "only_history") { + // M3: Commit the freshest known entity, not the stale prevSync snapshot. + // Using stale prevSync would cause false-change detection on next sync + // if provider clocks have drifted slightly. + const commitEntity = node.local ?? node.remote ?? node.prevSync; + if (commitEntity) successfulCommits.push(commitEntity); + continue; + } + + await callbackSyncProcess(triggerSource, ++counter, syncActions.length, node.key, decision || "unknown"); + + try { + if (decision === "local_is_created_then_push" || decision === "local_is_modified_then_push") { + const res = await copyFileOrFolder(node.key, fsLocal, remoteFsTarget); + successfulCommits.push({ ...node.local!, keyRaw: node.key, mtimeSvr: res.entity.mtimeSvr }); + } + else if (decision === "remote_is_created_then_pull" || decision === "remote_is_modified_then_pull") { + const res = await copyFileOrFolder(node.key, remoteFsTarget, fsLocal); + successfulCommits.push({ ...node.remote!, keyRaw: node.key, mtimeCli: res.entity.mtimeCli }); + } + else if (decision === "remote_is_deleted_thus_also_delete_local") { + await fsLocal.rm(node.key); + // Omit from successfulCommits — clears baseline so next sync won't re-examine + } + else if (decision === "local_is_deleted_thus_also_delete_remote") { + await remoteFsTarget.rm(node.key); + // Omit from successfulCommits — clears baseline + } + else if (decision?.includes("conflict") && decision?.includes("smart_conflict")) { + // Smart conflict: preserve local as a timestamped backup, then pull remote. + const cName = generateConflictFileName(node.key); + if (!node.key.endsWith("/")) { + // Read the current local content and write it to the conflict-named path. + const localContent = await fsLocal.readFile(node.key); + const localStat = await fsLocal.stat(node.key); + await fsLocal.writeFile( + cName, + localContent, + localStat.mtimeCli ?? Date.now(), + localStat.ctimeCli ?? localStat.mtimeCli ?? Date.now() + ); + } + // Pull remote over the actual local file — now safe because local is backed up. + const res = await copyFileOrFolder(node.key, remoteFsTarget, fsLocal); + successfulCommits.push({ ...node.remote!, keyRaw: node.key, mtimeCli: res.entity.mtimeCli }); + } + else if (decision?.includes("keep_local")) { + const res = await copyFileOrFolder(node.key, fsLocal, remoteFsTarget); + successfulCommits.push({ ...node.local!, keyRaw: node.key, mtimeSvr: res.entity.mtimeSvr }); + } + else if (decision?.includes("keep_remote")) { + const res = await copyFileOrFolder(node.key, remoteFsTarget, fsLocal); + successfulCommits.push({ ...node.remote!, keyRaw: node.key, mtimeCli: res.entity.mtimeCli }); + } + } catch (e) { + // If an operation fails, bypass pushing to successfulCommits. + // The file stays in broken state — next sync will retry it. + console.error(`BYOC Engine failed handling file ${node.key}:`, e); + } + } + + // Phase 4: Committing Baseline + // Atomically save mtimes of all successfully synced files for 3-way diff next run. + await db.prevSyncRecordsTbl.setItem(profileID, successfulCommits); + + await notifyFunc(triggerSource, 8); // finish + await statusBarFunc(triggerSource, 8, true); + + } catch (err: any) { + console.error("BYOC Sync Error: ", err); + await errNotifyFunc(triggerSource, err); + await statusBarFunc(triggerSource, 8, false); + } finally { + await markIsSyncingFunc(false); + } +} diff --git a/src/syncAlgoV3Notice.ts b/src/syncAlgoV3Notice.ts new file mode 100644 index 0000000..a4dd03c --- /dev/null +++ b/src/syncAlgoV3Notice.ts @@ -0,0 +1,128 @@ +import { type App, Modal } from "obsidian"; +import type { TransItemType } from "./i18n"; +import type RemotelySavePlugin from "./main"; // unavoidable + +import { stringToFragment } from "./misc"; + +export class SyncAlgoV3Modal extends Modal { + agree: boolean; + manualBackup: boolean; + requireUpdateAllDev: boolean; + readonly plugin: RemotelySavePlugin; + constructor(app: App, plugin: RemotelySavePlugin) { + super(app); + this.plugin = plugin; + this.agree = false; + this.manualBackup = false; + this.requireUpdateAllDev = false; + } + onOpen() { + const { contentEl } = this; + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + contentEl.createEl("h2", { + text: t("syncalgov3_title"), + }); + + const ul = contentEl.createEl("ul"); + t("syncalgov3_texts") + .split("\n") + .forEach((val) => { + ul.createEl("li", { + text: stringToFragment(val), + }); + }); + + // code modified partially from BART released under MIT License + contentEl.createDiv("modal-button-container", (buttonContainerEl) => { + let agreeBtn: HTMLButtonElement | undefined = undefined; + + buttonContainerEl.createEl( + "label", + { + cls: "mod-checkbox", + }, + (labelEl) => { + const checkboxEl = labelEl.createEl("input", { + attr: { tabindex: -1 }, + type: "checkbox", + }); + checkboxEl.checked = this.manualBackup; + checkboxEl.addEventListener("click", () => { + this.manualBackup = checkboxEl.checked; + if (agreeBtn !== undefined) { + if (this.manualBackup && this.requireUpdateAllDev) { + agreeBtn.removeAttribute("disabled"); + } else { + agreeBtn.setAttr("disabled", true); + } + } + }); + labelEl.appendText(t("syncalgov3_checkbox_manual_backup")); + } + ); + + buttonContainerEl.createEl( + "label", + { + cls: "mod-checkbox", + }, + (labelEl) => { + const checkboxEl = labelEl.createEl("input", { + attr: { tabindex: -1 }, + type: "checkbox", + }); + checkboxEl.checked = this.requireUpdateAllDev; + checkboxEl.addEventListener("click", () => { + this.requireUpdateAllDev = checkboxEl.checked; + if (agreeBtn !== undefined) { + if (this.manualBackup && this.requireUpdateAllDev) { + agreeBtn.removeAttribute("disabled"); + } else { + agreeBtn.setAttr("disabled", true); + } + } + }); + labelEl.appendText(t("syncalgov3_checkbox_requiremultidevupdate")); + } + ); + + agreeBtn = buttonContainerEl.createEl("button", { + attr: { type: "button" }, + cls: "mod-cta", + text: t("syncalgov3_button_agree"), + }); + agreeBtn.setAttr("disabled", true); + agreeBtn.addEventListener("click", () => { + this.agree = true; + this.close(); + }); + + buttonContainerEl + .createEl("button", { + attr: { type: "submit" }, + text: t("syncalgov3_button_disagree"), + }) + .addEventListener("click", () => { + this.close(); + }); + }); + } + + onClose() { + const { contentEl } = this; + contentEl.empty(); + if (this.agree) { + console.info("agree to use the new algorithm"); + this.plugin.saveAgreeToUseNewSyncAlgorithm(); + this.plugin.enableAutoSyncIfSet(); + this.plugin.enableInitSyncIfSet(); + this.plugin.toggleSyncOnSaveIfSet(); + } else { + console.info("do not agree to use the new algorithm"); + this.plugin.unload(); + } + } +} diff --git a/src/worker.d.ts b/src/worker.d.ts new file mode 100644 index 0000000..13ad2cf --- /dev/null +++ b/src/worker.d.ts @@ -0,0 +1,6 @@ +declare module "*.worker.ts" { + class WebpackWorker extends Worker { + constructor(); + } + export default WebpackWorker; +} diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..cfc2cd9 --- /dev/null +++ b/styles.css @@ -0,0 +1,376 @@ +/* ═══════════════════════════════════════════════════════════════════════════ + BYOC — Bring Your Own Cloud + Plugin Stylesheet — Frosted Glass / Acrylic Design Language + Scoped to .byoc-settings to prevent bleed into other plugins or Obsidian core + ═══════════════════════════════════════════════════════════════════════════ */ + + +/* ──── 1. Visibility toggles — functional only, no visual treatment ────────────────────── */ + +.settings-percentage-custom-hide, +.settings-encryption-method-hide, +.s3-hide, .dropbox-hide, .onedrive-hide, .onedrivefull-hide, +.webdav-hide, .webdis-hide, .googledrive-hide, .box-hide, +.pcloud-hide, .yandexdisk-hide, .koofr-hide, .azureblobstorage-hide, +.pro-hide, +.dropbox-auth-button-hide, .dropbox-revoke-auth-button-hide, +.onedrive-auth-button-hide, .onedrive-revoke-auth-button-hide, +.onedrivefull-auth-button-hide, .onedrivefull-revoke-auth-button-hide, +.onedrivefull-allow-to-use-hide, +.googledrive-auth-button-hide, .googledrive-revoke-auth-button-hide, +.googledrive-allow-to-use-hide, +.box-auth-button-hide, .box-revoke-auth-button-hide, .box-allow-to-use-hide, +.pcloud-auth-button-hide, .pcloud-revoke-auth-button-hide, .pcloud-allow-to-use-hide, +.yandexdisk-auth-button-hide, .yandexdisk-revoke-auth-button-hide, .yandexdisk-allow-to-use-hide, +.koofr-auth-button-hide, .koofr-revoke-auth-button-hide, .koofr-allow-to-use-hide, +.azureblobstorage-allow-to-use-hide, +.pro-auth-button-hide, .pro-revoke-auth-button-hide { + display: none; +} + + +/* ──── 2. Settings panel root ──────────────────────────────────────────────────────────────────────────────────────────────────── */ + +.byoc-settings { + --byoc-radius: 8px; + --byoc-radius-sm: 5px; + --byoc-transition: 160ms ease; + --byoc-accent: var(--interactive-accent); + --byoc-accent-hover: var(--interactive-accent-hover); + --byoc-surface: rgba(var(--mono-100-rgb, 128, 128, 128), 0.05); + --byoc-border: var(--background-modifier-border); + --byoc-warn-bg: rgba(var(--color-yellow-rgb, 230, 175, 45), 0.1); + --byoc-warn-border: rgba(var(--color-yellow-rgb, 230, 175, 45), 0.4); + --byoc-warn-text: var(--text-warning, var(--text-muted)); +} + + +/* ──── 3. Page heading ────────────────────────────────────────────────────────────────────────────────────────────────────────────── */ + +.byoc-settings > h1 { + font-size: 1.35em; + font-weight: 700; + letter-spacing: -0.01em; + color: var(--text-normal); + padding-bottom: 0.5em; + margin-bottom: 0.25em; + border-bottom: 2px solid var(--byoc-accent); + display: flex; + align-items: center; + gap: 0.5em; +} + +.byoc-settings > h1::before { + content: "☁"; + font-size: 0.85em; + opacity: 0.7; +} + + +/* ──── 4. Auth section card ──────────────────────────────────────────────────────────────────────────────────────────────────── */ +/* .settings-auth-related wraps the Authorize / Revoke setting items */ + +.byoc-settings .settings-auth-related { + margin-top: 1rem; + padding: 1rem 1rem 0.5rem; + border-radius: var(--byoc-radius); + border: 1px solid var(--byoc-border); + background: var(--byoc-surface); + box-shadow: + 0 1px 3px rgba(0, 0, 0, 0.06), + inset 0 1px 0 rgba(255, 255, 255, 0.04); + position: relative; + overflow: hidden; +} + +/* Accent left strip */ +.byoc-settings .settings-auth-related::before { + content: ""; + position: absolute; + left: 0; + top: 0; + bottom: 0; + width: 3px; + background: var(--byoc-accent); + border-radius: var(--byoc-radius) 0 0 var(--byoc-radius); + opacity: 0.8; +} + + +/* ──── 5. Disclaimer callout boxes ────────────────────────────────────────────────────────────────────────────────────── */ +/* Replaces the plain font-weight:bold with a proper callout treatment */ + +.byoc-settings .s3-disclaimer, +.byoc-settings .dropbox-disclaimer, +.byoc-settings .onedrive-disclaimer, +.byoc-settings .onedrivefull-disclaimer, +.byoc-settings .webdav-disclaimer, +.byoc-settings .webdis-disclaimer, +.byoc-settings .googledrive-disclaimer, +.byoc-settings .box-disclaimer, +.byoc-settings .pcloud-disclaimer, +.byoc-settings .yandexdisk-disclaimer, +.byoc-settings .koofr-disclaimer, +.byoc-settings .azureblobstorage-disclaimer, +.byoc-settings .pro-disclaimer, +.byoc-settings .password-disclaimer, +.byoc-settings .s3-disclaimer, +.byoc-settings .password-second-confirm, +.byoc-settings .encryptionmethod-second-confirm { + font-weight: 500; + font-size: 0.82em; + color: var(--byoc-warn-text); + background: var(--byoc-warn-bg); + border: 1px solid var(--byoc-warn-border); + border-radius: var(--byoc-radius-sm); + padding: 0.45em 0.65em 0.45em 2em; + margin: 0.25em 0; + line-height: 1.5; + position: relative; +} + +.byoc-settings .s3-disclaimer::before, +.byoc-settings .dropbox-disclaimer::before, +.byoc-settings .onedrive-disclaimer::before, +.byoc-settings .onedrivefull-disclaimer::before, +.byoc-settings .webdav-disclaimer::before, +.byoc-settings .webdis-disclaimer::before, +.byoc-settings .googledrive-disclaimer::before, +.byoc-settings .box-disclaimer::before, +.byoc-settings .pcloud-disclaimer::before, +.byoc-settings .yandexdisk-disclaimer::before, +.byoc-settings .koofr-disclaimer::before, +.byoc-settings .azureblobstorage-disclaimer::before, +.byoc-settings .pro-disclaimer::before, +.byoc-settings .password-disclaimer::before, +.byoc-settings .password-second-confirm::before, +.byoc-settings .encryptionmethod-second-confirm::before { + content: "âš "; + position: absolute; + left: 0.6em; + top: 50%; + transform: translateY(-50%); + font-size: 0.9em; + opacity: 0.75; +} + + +/* ──── 6. Monospace textareas ──────────────────────────────────────────────────────────────────────────────────────────────── */ + +.webdav-customheaders-textarea, +.ignorepaths-textarea, +.onlyallowpaths-textarea { + font-family: var(--font-monospace); + font-size: 0.85em; + border-radius: var(--byoc-radius-sm); + background: var(--background-secondary); + border-color: var(--byoc-border); + transition: border-color var(--byoc-transition); + resize: vertical; + min-height: 5em; +} + +.webdav-customheaders-textarea:focus, +.ignorepaths-textarea:focus, +.onlyallowpaths-textarea:focus { + border-color: var(--byoc-accent); + outline: none; + box-shadow: 0 0 0 2px rgba(var(--interactive-accent-rgb, 100, 130, 255), 0.15); +} + + +/* ──── 7. QR code export ────────────────────────────────────────────────────────────────────────────────────────────────────────── */ + +.qrcode-img { + width: 260px; + height: 260px; + border-radius: var(--byoc-radius); + border: 1px solid var(--byoc-border); + display: block; + margin: 0.75em auto; +} + + +/* ──── 8. HTTP server warning (log to http) ──────────────────────────────────────────────────────────────────── */ + +.logtohttpserver-warning { + color: var(--text-error); + font-weight: 600; + font-size: 0.82em; + padding: 0.3em 0.5em; + border-radius: var(--byoc-radius-sm); + background: rgba(var(--color-red-rgb, 220, 60, 60), 0.08); + border: 1px solid rgba(var(--color-red-rgb, 220, 60, 60), 0.25); +} + + +/* ──── 9. Grid control wrapping ──────────────────────────────────────────────────────────────────────────────────────────── */ + +.setting-need-wrapping .setting-item-control { + display: grid; +} + + +/* ──── 10. Auth modal content ──────────────────────────────────────────────────────────────────────────────────────────────── */ +/* Targets the contentEl inside BYOC's auth modals */ + +.byoc-auth-modal .modal-title { + font-size: 1.1em; + font-weight: 600; + letter-spacing: -0.01em; +} + +.byoc-auth-modal .modal-content { + gap: 0.75em; + display: flex; + flex-direction: column; +} + +/* Step description paragraphs inside auth modals */ +.byoc-auth-modal p { + font-size: 0.9em; + color: var(--text-muted); + line-height: 1.6; + margin: 0; +} + +/* Auth link button inside modal */ +.byoc-auth-modal a { + display: block; + text-align: center; + margin-top: 0.25em; + padding: 0.5em 0; + color: var(--byoc-accent); + font-weight: 500; + text-underline-offset: 2px; + transition: opacity var(--byoc-transition); +} + +.byoc-auth-modal a:hover { + opacity: 0.8; +} + +/* ──── 12. Auth modal buttons ──── */ +.byoc-auth-modal button { + background-color: var(--byoc-accent); + color: var(--text-on-accent, #ffffff); + padding: 0.7em 1.2em; + margin: 1em 0; + font-weight: 600; + border: none; + cursor: pointer; + width: 100%; + justify-content: center; + border-radius: var(--byoc-radius-sm); + font-size: 0.85em; + transition: + background var(--byoc-transition), + transform var(--byoc-transition); +} + +.byoc-auth-modal button:active { + transform: scale(0.98); +} + +.byoc-settings .setting-item.setting-item-heading { + border-top: 1px solid var(--byoc-border); + margin-top: 0.5rem; + padding-top: 0.75rem; +} + +.byoc-settings .setting-item.setting-item-heading .setting-item-name { + font-weight: 600; + color: var(--text-accent, var(--interactive-accent)); + text-transform: uppercase; + font-size: 0.78em; + letter-spacing: 0.06em; +} + +/* ──── 13. Service Chooser Dropdown Card ──── */ +.byoc-provider-chooser { + margin: 1.5rem 0; + padding: 0.5rem 1rem; + background: rgba(var(--mono-100-rgb, 128, 128, 128), 0.08); + border: 1px solid var(--byoc-border); + border-radius: var(--byoc-radius); + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); +} + +.byoc-provider-chooser .setting-item { + border: none; + background: transparent; + padding: 0.5rem 0; +} + +.byoc-provider-chooser .setting-item-name { + font-weight: 600; + color: var(--text-normal); +} + +/* ──── 14. Active Provider Sub-header ──── */ +h2.byoc-provider-heading { + margin-top: 2rem !important; + padding-bottom: 0.5em; + font-size: 1.2em; + color: var(--byoc-accent); + border-bottom: 1px solid var(--byoc-border); + display: flex; + align-items: center; + gap: 0.4em; +} + +h2.byoc-provider-heading svg { + width: 1.25em; + height: 1.25em; + flex-shrink: 0; + display: inline-block; + vertical-align: middle; +} + +/* ──── 15. General Section Headers ──── */ +h2.byoc-section-heading { + margin-top: 2.5rem !important; + padding-bottom: 0.5em; + font-size: 1.15em; + color: var(--text-normal); + border-bottom: 1px solid var(--byoc-border); + display: flex; + align-items: center; + gap: 0.4em; + opacity: 0.9; +} +h2.byoc-section-heading::before { + content: "♦"; + color: var(--text-muted); + font-size: 0.7em; + opacity: 0.6; +} + +/* ──── 16. Setting Item Hover ──── */ +.byoc-settings .setting-item { + border-radius: var(--byoc-radius-sm); + transition: background var(--byoc-transition); +} + +.byoc-settings .setting-item:hover { + background: rgba(var(--mono-100-rgb, 128, 128, 128), 0.04); +} + + +/* Added BYOC Intro Card */ +.byoc-intro-card { + background: rgba(100, 130, 255, 0.05); + border-left: 3px solid var(--byoc-accent); + padding: 1rem 1.2rem; + border-radius: var(--byoc-radius-sm); + margin-bottom: 2rem; + color: var(--text-normal); + font-size: 0.95em; +} + +.byoc-auth-modal .modal-title svg { + width: 1.5em; + height: 1.5em; + vertical-align: middle; + margin-right: 8px; +} diff --git a/tests/configPersist.test.ts b/tests/configPersist.test.ts new file mode 100644 index 0000000..f15ffe1 --- /dev/null +++ b/tests/configPersist.test.ts @@ -0,0 +1,57 @@ +import { strict as assert } from "assert"; + +import type { RemotelySavePluginSettings } from "../src/baseTypes"; +import { messyConfigToNormal, normalConfigToMessy } from "../src/configPersist"; + +const DEFAULT_SETTINGS: RemotelySavePluginSettings = { + s3: { + s3AccessKeyID: "acc", + } as any, + webdav: { + address: "addr", + } as any, + dropbox: { + username: "测试中文", + } as any, + onedrive: { + username: "test 🍎 emoji", + } as any, + onedrivefull: { + username: "test 🍎 emoji", + } as any, + webdis: { + address: "addr", + } as any, + googledrive: { + refreshToken: "xxx", + } as any, + box: { + refreshToken: "xxx", + } as any, + pcloud: { + accessToken: "xxx", + } as any, + yandexdisk: { + refreshToken: "xxx", + } as any, + koofr: { + refreshToken: "xxx", + } as any, + azureblobstorage: { + containerSasUrl: "http://127.0.0.1", + } as any, + password: "password", + serviceType: "s3", + currLogLevel: "info", + ignorePaths: ["somefoldertoignore"], + enableStatusBarInfo: true, +}; + +describe("Config Persist tests", () => { + it("should encrypt go back and forth conrrectly", async () => { + const k = DEFAULT_SETTINGS; + const k2 = normalConfigToMessy(k); + const k3 = messyConfigToNormal(k2); + assert.deepEqual(k3, k); + }); +}); diff --git a/tests/encryptOpenSSL.test.ts b/tests/encryptOpenSSL.test.ts new file mode 100644 index 0000000..c372b5b --- /dev/null +++ b/tests/encryptOpenSSL.test.ts @@ -0,0 +1,163 @@ +import { strict as assert } from "assert"; +import * as fs from "fs"; +import * as path from "path"; +import { + decryptArrayBuffer, + decryptBase32ToString, + encryptArrayBuffer, + encryptStringToBase32, + encryptStringToBase64url, + getSizeFromEncToOrig, + getSizeFromOrigToEnc, +} from "../src/encryptOpenSSL"; +import { base64ToBase64url, bufferToArrayBuffer } from "../src/misc"; + +describe("Encryption OpenSSL tests", () => { + beforeEach(() => { + global.window = { + crypto: require("crypto").webcrypto, + } as any; + }); + + it("should encrypt string", async () => { + const k = "dkjdhkfhdkjgsdklxxd"; + const password = "hey"; + assert.notEqual(await encryptStringToBase32(k, password), k); + }); + + it("should encrypt string and return different results each time", async () => { + const k = "dkjdhkfhdkjgsdklxxd"; + const password = "hey"; + const res1 = await encryptStringToBase32(k, password); + const res2 = await encryptStringToBase32(k, password); + assert.notEqual(res1, res2); + }); + + it("should raise error using different password", async () => { + const k = "secret text"; + const password = "hey"; + const password2 = "hey2"; + const enc = await encryptStringToBase32(k, password); + await assert.rejects(decryptBase32ToString(enc, password2)); + }); + + it("should encrypt and decrypt string and get the same result returned", async () => { + const k = "jfkkjkjbce7983ycdeknkkjckooAIUHIDIBIE((*BII)njD/d/dd/d/sjxhux"; + const password = "hfiuibec989###oiu982bj1`"; + const enc = await encryptStringToBase32(k, password); + // console.log(enc); + const dec = await decryptBase32ToString(enc, password); + // console.log(dec); + assert.equal(dec, k); + }); + + it("should encrypt text file and get the same result as openssl", async () => { + const fileContent = ( + await fs.readFileSync( + path.join(__dirname, "static_assets", "sometext.txt") + ) + ).toString("utf-8"); + const password = "somepassword"; + const saltHex = "8302F586FAB491EC"; + const enc = await encryptStringToBase64url( + fileContent, + password, + undefined, + saltHex + ); + + // two command returns same result: + // cat ./sometext.txt | openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword + // openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -base64 -pass pass:somepassword -in ./sometext.txt + const opensslBase64Res = + "U2FsdGVkX1+DAvWG+rSR7BPXMnlvSSVGMdjsx7kE1CTH+28P+yAZRdDGgFWMGkMd"; + // we output base32, so we need some transformation + const opensslBase64urlRes = base64ToBase64url(opensslBase64Res); + + assert.equal(enc, opensslBase64urlRes); + }); + + it("should encrypt binary file and get the same result as openssl", async () => { + const testFolder = path.join(__dirname, "static_assets", "mona_lisa"); + const testFileName = + "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"; + const fileArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName)) + ); + const password = "somepassword"; + const saltHex = "8302F586FAB491EC"; + const enc = await encryptArrayBuffer( + fileArrBuf, + password, + undefined, + saltHex + ); + const opensslArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName + ".enc")) + ); + + // openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 20000 -pass pass:somepassword -in mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg -out mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc + + assert.ok(Buffer.from(enc).equals(Buffer.from(opensslArrBuf))); + }); + + it("should encrypt binary file not deterministically", async () => { + const testFolder = path.join(__dirname, "static_assets", "mona_lisa"); + const testFileName = + "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"; + const fileArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName)) + ); + const password = "somepassword"; + const res1 = await encryptArrayBuffer(fileArrBuf, password); + const res2 = await encryptArrayBuffer(fileArrBuf, password); + + assert.ok(!Buffer.from(res1).equals(Buffer.from(res2))); + }); + + it("should decrypt binary file and get the same result as openssl", async () => { + const testFolder = path.join(__dirname, "static_assets", "mona_lisa"); + const testFileName = + "1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"; + const fileArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName + ".enc")) + ); + const password = "somepassword"; + const dec = await decryptArrayBuffer(fileArrBuf, password); + const opensslArrBuf = bufferToArrayBuffer( + await fs.readFileSync(path.join(testFolder, testFileName)) + ); + + assert.deepEqual(Buffer.from(dec), Buffer.from(opensslArrBuf)); + }); + + it("should get size from origin to encrypted correctly", () => { + assert.throws(() => getSizeFromOrigToEnc(-1)); + assert.throws(() => getSizeFromOrigToEnc(0.5)); + assert.equal(getSizeFromOrigToEnc(0), 32); + assert.equal(getSizeFromOrigToEnc(15), 32); + assert.equal(getSizeFromOrigToEnc(16), 48); + assert.equal(getSizeFromOrigToEnc(31), 48); + assert.equal(getSizeFromOrigToEnc(32), 64); + assert.equal(getSizeFromOrigToEnc(14787203), 14787232); + }); + + it("should get size from encrypted to origin correctly", () => { + assert.throws(() => getSizeFromEncToOrig(-1)); + assert.throws(() => getSizeFromEncToOrig(30)); + + assert.deepEqual(getSizeFromEncToOrig(32), { + minSize: 0, + maxSize: 15, + }); + assert.deepEqual(getSizeFromEncToOrig(48), { + minSize: 16, + maxSize: 31, + }); + + assert.throws(() => getSizeFromEncToOrig(14787231)); + + const { minSize, maxSize } = getSizeFromEncToOrig(14787232); + assert.ok(minSize <= 14787203 && 14787203 <= maxSize); + }); +}); diff --git a/tests/fsWebdis.test.ts b/tests/fsWebdis.test.ts new file mode 100644 index 0000000..15a388e --- /dev/null +++ b/tests/fsWebdis.test.ts @@ -0,0 +1,12 @@ +import { strict as assert } from "assert"; +import { getOrigPath } from "../src/fsWebdis"; + +describe("Webdis operations tests", () => { + it("should get orig keys correctly", () => { + const input = "rs:fs:v1:库名字/something dev.md:meta"; + const output = getOrigPath(input, "库名字"); + const expected = "something dev.md"; + + assert.equal(output, expected); + }); +}); diff --git a/tests/metadataOnRemote.test.ts b/tests/metadataOnRemote.test.ts new file mode 100644 index 0000000..7ac6462 --- /dev/null +++ b/tests/metadataOnRemote.test.ts @@ -0,0 +1,81 @@ +import { strict as assert } from "assert"; +import { + type MetadataOnRemote, + isEqualMetadataOnRemote, +} from "../src/metadataOnRemote"; + +describe("Metadata operations tests", () => { + it("should compare objects deeply", async () => { + const a: MetadataOnRemote = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + }; + const b: MetadataOnRemote = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + }; + + assert.ok(isEqualMetadataOnRemote(a, b)); + }); + + it("should find diff", async () => { + const a: MetadataOnRemote = { + deletions: [ + { key: "xxxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + }; + const b: MetadataOnRemote = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + }; + + assert.ok(!isEqualMetadataOnRemote(a, b)); + }); + + it("should treat undefined correctly", async () => { + const a: MetadataOnRemote | undefined = undefined; + let b: MetadataOnRemote | undefined = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + }; + + assert.ok(!isEqualMetadataOnRemote(a, b)); + + b = { deletions: [] }; + assert.ok(isEqualMetadataOnRemote(a, b)); + + b = { deletions: undefined }; + assert.ok(isEqualMetadataOnRemote(a, b)); + + b = undefined; + assert.ok(isEqualMetadataOnRemote(a, b)); + }); + + it("should ignore generated at fields", async () => { + const a: MetadataOnRemote = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + generatedWhen: 1, + }; + const b: MetadataOnRemote = { + deletions: [ + { key: "xxx", actionWhen: 1 }, + { key: "yyy", actionWhen: 2 }, + ], + generatedWhen: 2, + }; + + assert.ok(isEqualMetadataOnRemote(a, b)); + }); +}); diff --git a/tests/misc.test.ts b/tests/misc.test.ts new file mode 100644 index 0000000..9e2a564 --- /dev/null +++ b/tests/misc.test.ts @@ -0,0 +1,493 @@ +import { strict as assert } from "assert"; +import { JSDOM } from "jsdom"; +import * as misc from "../src/misc"; + +describe("Misc: hidden file", () => { + it("should find hidden file correctly", () => { + let item = ""; + assert.ok(!misc.isHiddenPath(item)); + + item = "."; + assert.ok(!misc.isHiddenPath(item)); + + item = ".."; + assert.ok(!misc.isHiddenPath(item)); + + item = "/x/y/z/../././../a/b/c"; + assert.ok(!misc.isHiddenPath(item)); + + item = ".hidden"; + assert.ok(misc.isHiddenPath(item)); + + item = "_hidden_loose"; + assert.ok(misc.isHiddenPath(item)); + assert.ok(!misc.isHiddenPath(item, true, false)); + + item = "/sdd/_hidden_loose"; + assert.ok(misc.isHiddenPath(item)); + + item = "what/../_hidden_loose/what/what/what"; + assert.ok(misc.isHiddenPath(item)); + + item = "what/../_hidden_loose/what/what/what"; + assert.ok(!misc.isHiddenPath(item, true, false)); + + item = "what/../_hidden_loose/../.hidden/what/what/what"; + assert.ok(misc.isHiddenPath(item, true, false)); + + item = "what/../_hidden_loose/../.hidden/what/what/what"; + assert.ok(!misc.isHiddenPath(item, false, true)); + + item = "what/_hidden_loose/what/what/what"; + assert.ok(misc.isHiddenPath(item, false, true)); + assert.ok(!misc.isHiddenPath(item, true, false)); + + item = "what/.hidden/what/what/what"; + assert.ok(!misc.isHiddenPath(item, false, true)); + assert.ok(misc.isHiddenPath(item, true, false)); + }); +}); + +describe("Misc: get folder levels", () => { + it("should ignore empty path", () => { + const item = ""; + assert.equal(misc.getFolderLevels(item).length, 0); + }); + + it("should ignore single file", () => { + const item = "xxx"; + assert.equal(misc.getFolderLevels(item).length, 0); + }); + + it("should detect path ending with /", () => { + const item = "xxx/"; + const res = ["xxx"]; + assert.deepEqual(misc.getFolderLevels(item), res); + }); + + it("should correctly split folders and files", () => { + const item = "xxx/yyy/zzz.md"; + const res = ["xxx", "xxx/yyy"]; + assert.deepEqual(misc.getFolderLevels(item), res); + + const item2 = "xxx/yyy/zzz"; + const res2 = ["xxx", "xxx/yyy"]; + assert.deepEqual(misc.getFolderLevels(item2), res2); + + const item3 = "xxx/yyy/zzz/"; + const res3 = ["xxx", "xxx/yyy", "xxx/yyy/zzz"]; + assert.deepEqual(misc.getFolderLevels(item3), res3); + }); + + it("should correctly add ending slash if required", () => { + const item = "xxx/yyy/zzz.md"; + const res = ["xxx/", "xxx/yyy/"]; + assert.deepEqual(misc.getFolderLevels(item, true), res); + + const item2 = "xxx/yyy/zzz"; + const res2 = ["xxx/", "xxx/yyy/"]; + assert.deepEqual(misc.getFolderLevels(item2, true), res2); + + const item3 = "xxx/yyy/zzz/"; + const res3 = ["xxx/", "xxx/yyy/", "xxx/yyy/zzz/"]; + assert.deepEqual(misc.getFolderLevels(item3, true), res3); + }); + + it("should treat path starting with / correctly", () => { + const item = "/xxx/yyy/zzz.md"; + const res = ["/xxx", "/xxx/yyy"]; + assert.deepEqual(misc.getFolderLevels(item), res); + + const item2 = "/xxx/yyy/zzz"; + const res2 = ["/xxx", "/xxx/yyy"]; + assert.deepEqual(misc.getFolderLevels(item2), res2); + + const item3 = "/xxx/yyy/zzz/"; + const res3 = ["/xxx", "/xxx/yyy", "/xxx/yyy/zzz"]; + assert.deepEqual(misc.getFolderLevels(item3), res3); + + const item4 = "/xxx"; + const res4 = [] as string[]; + assert.deepEqual(misc.getFolderLevels(item4), res4); + + const item5 = "/"; + const res5 = [] as string[]; + assert.deepEqual(misc.getFolderLevels(item5), res5); + }); +}); + +describe("Misc: get parent folder", () => { + it("should treat empty path correctly", () => { + const item = ""; + assert.equal(misc.getParentFolder(item), "/"); + }); + + it("should treat one level path correctly", () => { + let item = "abc/"; + assert.equal(misc.getParentFolder(item), "/"); + item = "/efg/"; + assert.equal(misc.getParentFolder(item), "/"); + }); + + it("should treat more levels path correctly", () => { + let item = "abc/efg"; + assert.equal(misc.getParentFolder(item), "abc/"); + item = "/hij/klm/"; + assert.equal(misc.getParentFolder(item), "/hij/"); + }); +}); + +describe("Misc: vaild file name tests", () => { + it("should treat no ascii correctly", async () => { + const x = misc.isVaildText("😄🍎 apple 苹果"); + // console.log(x) + assert.ok(x); + }); + + it("should find not-printable chars correctly", async () => { + const x = misc.isVaildText("😄🍎 apple 苹果\u0000"); + // console.log(x) + assert.ok(!x); + }); + + it("should allow spaces/slashes/...", async () => { + const x = misc.isVaildText("😄🍎 apple 苹果/-_=/\\*%^&@#$`"); + assert.ok(x); + }); +}); + +describe("Misc: get dirname", () => { + it("should return itself for folder", async () => { + const x = misc.getPathFolder("ssss/"); + // console.log(x) + assert.equal(x, "ssss/"); + }); + + it("should return folder for file", async () => { + const x = misc.getPathFolder("sss/yyy"); + // console.log(x) + assert.equal(x, "sss/"); + }); + + it("should treat / specially", async () => { + const x = misc.getPathFolder("/"); + assert.equal(x, "/"); + + const y = misc.getPathFolder("/abc"); + assert.equal(y, "/"); + }); +}); + +describe("Misc: extract svg", () => { + beforeEach(() => { + const fakeBrowser = new JSDOM(""); + global.window = fakeBrowser.window as any; + }); + + it("should extract rect from svg correctly", () => { + const x = ""; + const y = misc.extractSvgSub(x); + // console.log(x) + assert.equal(y, ""); + }); +}); + +describe("Misc: get split ranges", () => { + it("should deal with big parts", () => { + const k = misc.getSplitRanges(10, 20); + const k2: misc.SplitRange[] = [ + { + partNum: 1, + start: 0, + end: 10, + }, + ]; + assert.deepEqual(k, k2); + }); + + it("should deal with 0 remainder", () => { + const k = misc.getSplitRanges(20, 10); + const k2: misc.SplitRange[] = [ + { + partNum: 1, + start: 0, + end: 10, + }, + { + partNum: 2, + start: 10, + end: 20, + }, + ]; + assert.deepEqual(k, k2); + }); + + it("should deal with not-0 remainder", () => { + const k = misc.getSplitRanges(25, 10); + const k2: misc.SplitRange[] = [ + { + partNum: 1, + start: 0, + end: 10, + }, + { + partNum: 2, + start: 10, + end: 20, + }, + { + partNum: 3, + start: 20, + end: 25, + }, + ]; + assert.deepEqual(k, k2); + }); +}); + +describe("Misc: at which level", () => { + it("should throw error on some parameters", () => { + assert.throws(() => misc.atWhichLevel(undefined)); + assert.throws(() => misc.atWhichLevel("")); + assert.throws(() => misc.atWhichLevel("..")); + assert.throws(() => misc.atWhichLevel(".")); + assert.throws(() => misc.atWhichLevel("/")); + assert.throws(() => misc.atWhichLevel("/xxyy")); + }); + + it("should treat folders correctly", () => { + assert.equal(misc.atWhichLevel("x/"), 1); + assert.equal(misc.atWhichLevel("x/y/"), 2); + }); + + it("should treat files correctly", () => { + assert.equal(misc.atWhichLevel("x.md"), 1); + assert.equal(misc.atWhichLevel("x/y.md"), 2); + assert.equal(misc.atWhichLevel("x/y/z.md"), 3); + }); +}); + +describe("Misc: special char for dir", () => { + it("should return false for normal string", () => { + assert.ok(!misc.checkHasSpecialCharForDir("")); + assert.ok(!misc.checkHasSpecialCharForDir("xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy_xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy.xxx")); + assert.ok(!misc.checkHasSpecialCharForDir("yyy?xxx")); + }); + + it("should return true for special cases", () => { + assert.ok(misc.checkHasSpecialCharForDir("?")); + assert.ok(misc.checkHasSpecialCharForDir("/")); + assert.ok(misc.checkHasSpecialCharForDir("\\")); + assert.ok(misc.checkHasSpecialCharForDir("xxx/yyy")); + assert.ok(misc.checkHasSpecialCharForDir("xxx\\yyy")); + assert.ok(misc.checkHasSpecialCharForDir("xxx?yyy")); + }); +}); + +describe("Misc: split chunk ranges", () => { + it("should fail on negative numner", () => { + assert.throws(() => misc.splitFileSizeToChunkRanges(-1, 2)); + assert.throws(() => misc.splitFileSizeToChunkRanges(1, -1)); + assert.throws(() => misc.splitFileSizeToChunkRanges(1, 0)); + }); + + it("should return nothing for 0 input", () => { + let input: [number, number] = [0, 1]; + let output: any = []; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [0, 100]; + output = []; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + }); + + it("should return single item for 1 input", () => { + let input: [number, number] = [1, 1]; + let output = [{ start: 0, end: 0 }]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [1, 100]; + output = [{ start: 0, end: 0 }]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + }); + + it("should return single item for larger or equal input", () => { + let input: [number, number] = [10, 10]; + let output = [{ start: 0, end: 9 }]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [10, 21]; + output = [{ start: 0, end: 9 }]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + }); + + it("should return correct items for normal input", () => { + let input: [number, number] = [10, 9]; + let output = [ + { start: 0, end: 8 }, + { start: 9, end: 9 }, + ]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [10, 5]; + output = [ + { start: 0, end: 4 }, + { start: 5, end: 9 }, + ]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [3, 1]; + output = [ + { start: 0, end: 0 }, + { start: 1, end: 1 }, + { start: 2, end: 2 }, + ]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [15, 5]; + output = [ + { start: 0, end: 4 }, + { start: 5, end: 9 }, + { start: 10, end: 14 }, + ]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + + input = [1024, 578]; + output = [ + { start: 0, end: 577 }, + { start: 578, end: 1023 }, + ]; + assert.deepStrictEqual(output, misc.splitFileSizeToChunkRanges(...input)); + }); +}); + +describe("Misc: check valid file names", () => { + it("should be ok for normal file nmes", () => { + let item = "what/.hidden/what/what/what"; + assert.ok(misc.checkValidName(item).result); + + item = "ssss/%%%^^^$xxxx.md"; + assert.ok(misc.checkValidName(item).result); + }); + + it("should be not ok for reserved characters", () => { + let item = "a**"; + assert.ok(!misc.checkValidName(item).result); + + item = "a?*"; + assert.ok(!misc.checkValidName(item).result); + + item = "<>:"; + assert.ok(!misc.checkValidName(item).result); + + item = "<>:/ssss"; + assert.ok(!misc.checkValidName(item).result); + }); + + it("should be not ok for reserved names", () => { + let item = "CON"; + assert.ok(!misc.checkValidName(item).result); + + item = "CON.txt"; + assert.ok(!misc.checkValidName(item).result); + + item = "CON.md"; + assert.ok(!misc.checkValidName(item).result); + + item = "con"; // lower case is ok + assert.ok(misc.checkValidName(item).result); + + item = "CON/"; + assert.ok(!misc.checkValidName(item).result); + + item = "CON.dir/"; + assert.ok(!misc.checkValidName(item).result); + + item = "CON.dir.folder/"; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx/CON"; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx/CON.txt"; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx/CON.txt.md"; + assert.ok(!misc.checkValidName(item).result); + }); + + it("should be not ok for invalid endings", () => { + let item = "xxx "; + assert.ok(!misc.checkValidName(item).result); + + item = "/xxx "; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx.yyy."; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx.yyy."; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx /"; + assert.ok(!misc.checkValidName(item).result); + + item = "xxx.yyy./"; + assert.ok(!misc.checkValidName(item).result); + }); +}); + +describe("Misc: Dropbox: should fix the folder name cases", () => { + it("should do nothing on empty folders", () => { + const input: any[] = []; + assert.equal(misc.fixEntityListCasesInplace(input).length, 0); + }); + + it("should sort folders by length by side effect", () => { + const input = [ + { keyRaw: "aaaa/" }, + { keyRaw: "bbb/" }, + { keyRaw: "c/" }, + { keyRaw: "dd/" }, + ]; + + const output = [ + { keyRaw: "c/" }, + { keyRaw: "dd/" }, + { keyRaw: "bbb/" }, + { keyRaw: "aaaa/" }, + ]; + assert.deepEqual(misc.fixEntityListCasesInplace(input), output); + }); + + it("should fix folder names", () => { + const input = [ + { keyRaw: "AAA/" }, + { keyRaw: "aaa/bbb/CCC.md" }, + { keyRaw: "aaa/BBB/" }, + + { keyRaw: "ddd/" }, + { keyRaw: "DDD/EEE/fff.md" }, + { keyRaw: "DDD/eee/" }, + + { keyRaw: "Ggg/" }, + { keyRaw: "ggG/hHH你好/Fff世界.md" }, + { keyRaw: "ggG/Hhh你好/" }, + ]; + + const output = [ + { keyRaw: "AAA/" }, + { keyRaw: "ddd/" }, + { keyRaw: "Ggg/" }, + { keyRaw: "AAA/BBB/" }, + { keyRaw: "ddd/eee/" }, + { keyRaw: "Ggg/Hhh你好/" }, + { keyRaw: "AAA/BBB/CCC.md" }, + { keyRaw: "ddd/eee/fff.md" }, + { keyRaw: "Ggg/Hhh你好/Fff世界.md" }, + ]; + assert.deepEqual(misc.fixEntityListCasesInplace(input), output); + }); +}); diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt new file mode 100644 index 0000000..b6d34e7 --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.LICENSE.txt @@ -0,0 +1,3 @@ +The file 1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg is downloaded from Wikimedia Commons: https://commons.wikimedia.org/wiki/File:Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg . + +Quoted from the web address: ... This photographic reproduction is therefore also considered to be in the public domain in the United States. In other jurisdictions, re-use of this content may be restricted ... diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg new file mode 100644 index 0000000..87baf8b --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b332c298be98be9968715952822431a051f9d07e67460cdcf91a5a3a305b5df1 +size 1141550 diff --git a/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc new file mode 100644 index 0000000..1728efa --- /dev/null +++ b/tests/static_assets/mona_lisa/1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7687eaf60ed93f754b107dc86a437aa97fad7bedd40432171b08c22cded7b559 +size 1141568 diff --git a/tests/static_assets/mona_lisa/openssl_command.sh b/tests/static_assets/mona_lisa/openssl_command.sh new file mode 100644 index 0000000..4e0df02 --- /dev/null +++ b/tests/static_assets/mona_lisa/openssl_command.sh @@ -0,0 +1,3 @@ +# The encryption file is produced by the following command. +# A salt is explictly provided because we need reproducible output in tests. +openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 10000 -pass pass:somepassword -in '1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg' -out 1374px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg.enc diff --git a/tests/static_assets/sometext.txt b/tests/static_assets/sometext.txt new file mode 100644 index 0000000..1d53ee3 --- /dev/null +++ b/tests/static_assets/sometext.txt @@ -0,0 +1 @@ +A secret text 你好世界 diff --git a/tests/syncConflict.test.ts b/tests/syncConflict.test.ts new file mode 100644 index 0000000..48cd696 --- /dev/null +++ b/tests/syncConflict.test.ts @@ -0,0 +1,21 @@ +import { strict as assert } from "assert"; +import { generateConflictFileName } from "../src/sync/conflict"; + +describe("Sync Conflict Resolver", () => { + it("should generate a sync conflict filename preserving extensions", () => { + const original = "my notes/ideas.md"; + const conflictName = generateConflictFileName(original); + + // e.g., 'ideas (sync conflict).md' or similar prefix/suffix + assert.ok(conflictName.startsWith("my notes/ideas")); + assert.ok(conflictName.endsWith(".md")); + assert.ok(conflictName.includes("sync-conflict-")); + }); + + it("should handle files without extensions correctly", () => { + const original = "my notes/ideas"; + const conflictName = generateConflictFileName(original); + assert.ok(conflictName.startsWith("my notes/ideas")); + assert.ok(conflictName.includes("sync-conflict-")); + }); +}); diff --git a/tests/syncPlanner.test.ts b/tests/syncPlanner.test.ts new file mode 100644 index 0000000..61b3271 --- /dev/null +++ b/tests/syncPlanner.test.ts @@ -0,0 +1,196 @@ +import { strict as assert } from "assert"; +import type { Entity, MixedEntity } from "../src/baseTypes"; +import { determineSyncDecision } from "../src/sync/planner"; + +function makeEntity(fields: Partial & { sizeRaw?: number }): Entity { + return { keyRaw: "test.md", sizeRaw: 0, ...fields } as Entity; +} + +function node( + local?: Partial, + remote?: Partial, + prevSync?: Partial +): MixedEntity { + return { + key: "test.md", + local: local ? makeEntity(local) : undefined, + remote: remote ? makeEntity(remote) : undefined, + prevSync: prevSync ? makeEntity(prevSync) : undefined, + }; +} + +const T = 100_000; // base timestamp +const D = 10_000; // big delta (clearly changed) +const S = 500; // small delta (within 2s tolerance — should NOT count as changed) + +describe("Sync Planner — 3-Way Merge Decision Matrix", () => { + + // ── Additions ─────────────────────────────────────────────────────────── + it("local created → push", () => { + assert.equal( + determineSyncDecision(node({ mtimeCli: T }), "smart_conflict"), + "local_is_created_then_push" + ); + }); + + it("remote created → pull", () => { + assert.equal( + determineSyncDecision(node(undefined, { mtimeSvr: T }), "smart_conflict"), + "remote_is_created_then_pull" + ); + }); + + // ── Equal (no change) ──────────────────────────────────────────────────── + it("both equal to prevSync → equal", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T }, { sizeRaw: 10, mtimeSvr: T }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "equal" + ); + }); + + it("timestamps within tolerance → equal (no spurious conflict)", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T + S }, { sizeRaw: 10, mtimeSvr: T }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "equal" + ); + }); + + // ── Modifications ──────────────────────────────────────────────────────── + it("local modified, remote unchanged → push", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 20, mtimeCli: T + D }, { sizeRaw: 10, mtimeSvr: T }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "local_is_modified_then_push" + ); + }); + + it("remote modified, local unchanged → pull", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T }, { sizeRaw: 20, mtimeSvr: T + D }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "remote_is_modified_then_pull" + ); + }); + + // ── Deletions ──────────────────────────────────────────────────────────── + it("local deleted, remote unchanged → delete remote", () => { + assert.equal( + determineSyncDecision( + node(undefined, { sizeRaw: 10, mtimeSvr: T }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "local_is_deleted_thus_also_delete_remote" + ); + }); + + it("remote deleted, local unchanged → delete local", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T }, undefined, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "remote_is_deleted_thus_also_delete_local" + ); + }); + + it("both missing with prevSync → only_history", () => { + assert.equal( + determineSyncDecision( + node(undefined, undefined, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "only_history" + ); + }); + + // ── Delete-vs-Modify conflicts (new edge cases) ────────────────────────── + it("local deleted BUT remote was modified → smart_conflict (not blind delete)", () => { + assert.equal( + determineSyncDecision( + node(undefined, { sizeRaw: 20, mtimeSvr: T + D }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "conflict_modified_then_smart_conflict" + ); + }); + + it("remote deleted BUT local was modified → smart_conflict (not blind delete)", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 20, mtimeCli: T + D }, undefined, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "conflict_modified_then_smart_conflict" + ); + }); + + // ── True conflicts ─────────────────────────────────────────────────────── + it("both modified → smart_conflict", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 20, mtimeCli: T + D }, { sizeRaw: 30, mtimeSvr: T + D }, { sizeRaw: 10, mtimeCli: T, mtimeSvr: T }), + "smart_conflict" + ), + "conflict_modified_then_smart_conflict" + ); + }); + + it("created on both sides → smart_conflict", () => { + assert.equal( + determineSyncDecision(node({ sizeRaw: 10, mtimeCli: T }, { sizeRaw: 20, mtimeSvr: T + D }), "smart_conflict"), + "conflict_created_then_smart_conflict" + ); + }); + + // ── keep_newer ─────────────────────────────────────────────────────────── + it("keep_newer: local is newer → keep_local", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T + D }, { sizeRaw: 20, mtimeSvr: T }, { sizeRaw: 5, mtimeCli: T - D, mtimeSvr: T - D }), + "keep_newer" + ), + "conflict_modified_then_keep_local" + ); + }); + + it("keep_newer: remote is newer → keep_remote", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T }, { sizeRaw: 20, mtimeSvr: T + D }, { sizeRaw: 5, mtimeCli: T - D, mtimeSvr: T - D }), + "keep_newer" + ), + "conflict_modified_then_keep_remote" + ); + }); + + // ── keep_larger ────────────────────────────────────────────────────────── + it("keep_larger: local is larger → keep_local", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 500, mtimeCli: T + D }, { sizeRaw: 20, mtimeSvr: T + D }, { sizeRaw: 5, mtimeCli: T - D, mtimeSvr: T - D }), + "keep_larger" + ), + "conflict_modified_then_keep_local" + ); + }); + + it("keep_larger: remote is larger → keep_remote", () => { + assert.equal( + determineSyncDecision( + node({ sizeRaw: 10, mtimeCli: T + D }, { sizeRaw: 500, mtimeSvr: T + D }, { sizeRaw: 5, mtimeCli: T - D, mtimeSvr: T - D }), + "keep_larger" + ), + "conflict_modified_then_keep_remote" + ); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..87d21a5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "es6", + "strict": true, + "allowJs": true, + "noImplicitAny": true, + "moduleResolution": "node", + // "allowSyntheticDefaultImports": true, + "resolveJsonModule": true, + "esModuleInterop": true, + "importHelpers": true, + "isolatedModules": true, + "skipLibCheck": true, + "noEmitOnError": false, + "lib": ["dom", "es5", "scripthost", "es2015", "webworker"] + }, + "include": ["src/global.d.ts", "src/**/*.ts"], + "exclude": ["tests/**", "pro/**", "node_modules/**"] +} diff --git a/upgrade_modals.js b/upgrade_modals.js new file mode 100644 index 0000000..7e4928e --- /dev/null +++ b/upgrade_modals.js @@ -0,0 +1,40 @@ +const fs = require('fs'); + +const svgMap = { + 'Dropbox': 'SVG_DROPBOX', + 'Onedrive': 'SVG_ONEDRIVE', + 'Onedrivefull': 'SVG_ONEDRIVE', + 'GoogleDrive': 'SVG_GDRIVE', + 'Box': 'SVG_BOX', + 'PCloud': 'SVG_PCLOUD', + 'Koofr': 'SVG_KOOFR', + 'YandexDisk': 'SVG_YANDEX' +}; + +const files = fs.readdirSync('src').filter(f => f.startsWith('settings') && f.endsWith('.ts')).map(f => 'src/' + f); + +files.forEach(file => { + let content = fs.readFileSync(file, 'utf8'); + + // 1. Upgrade titles to include SVG + content = content.replace(/this\.titleEl\.setText\(\"(Connect|Revoke) ([A-Za-z0-9_]+) Account\"\);/g, (match, action, provider) => { + const svgVar = svgMap[provider] || 'SVG_PCLOUD'; + return 'this.titleEl.innerHTML = \\ ' + action + ' ' + provider + ' Account\;'; + }); + + // 2. Eradicate Copy Button & A Tag, replace with unified Browser Button + // We use a robust regex that matches div2.createEl("button", { text: t("..._copybutton") }, ... ); + + // NOTE: In settingsBox, settingsKoofr, etc., the text is ext: t("modal_boxauth_copybutton") + content = content.replace(/div2\.createEl\(\s*\"button\"[\s\S]*?_copybutton\"[\s\S]*?^\s*\}\);\s*/gm, 'contentEl.createEl(\"button\", { text: \"Open Authorization in Browser\" }, (el) => { el.onclick = () => window.open(authUrl); });\n\n'); + + // Actually, some might not match uniquely. Let's just match any div2.createEl("button" that writes to clipboard! + content = content.replace(/div2\.createEl\(\s*\"button\"[\s\S]*?navigator\.clipboard\.writeText[\s\S]*?^\s*\}\);\s*/gm, 'contentEl.createEl(\"button\", { text: \"Open Authorization in Browser\" }, (el) => { el.onclick = () => window.open(authUrl); });\n\n'); + + // Handle a tags: contentEl.createEl("p").createEl("a", ...); + content = content.replace(/contentEl\.createEl\(\"p\"\)\.createEl\(\"a\"[\s\S]*?^\s*\}\);\s*/gm, ''); + + fs.writeFileSync(file, content, 'utf8'); +}); + +console.log('Successfully upgraded all modals'); diff --git a/versions.json b/versions.json new file mode 100644 index 0000000..af9a39e --- /dev/null +++ b/versions.json @@ -0,0 +1,3 @@ +{ + "1.0.0": "0.15.0" +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..66483fa --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,154 @@ +require("dotenv").config(); +const path = require("path"); +const webpack = require("webpack"); +const TerserPlugin = require("terser-webpack-plugin"); + +const DEFAULT_DROPBOX_APP_KEY = process.env.DROPBOX_APP_KEY || ""; +const DEFAULT_ONEDRIVE_CLIENT_ID = process.env.ONEDRIVE_CLIENT_ID || ""; +const DEFAULT_ONEDRIVE_AUTHORITY = process.env.ONEDRIVE_AUTHORITY || ""; +const DEFAULT_GOOGLEDRIVE_CLIENT_ID = process.env.GOOGLEDRIVE_CLIENT_ID || ""; +const DEFAULT_GOOGLEDRIVE_CLIENT_SECRET = + process.env.GOOGLEDRIVE_CLIENT_SECRET || ""; +const DEFAULT_BOX_CLIENT_ID = process.env.BOX_CLIENT_ID || ""; +const DEFAULT_BOX_CLIENT_SECRET = process.env.BOX_CLIENT_SECRET || ""; +const DEFAULT_PCLOUD_CLIENT_ID = process.env.PCLOUD_CLIENT_ID || ""; +const DEFAULT_PCLOUD_CLIENT_SECRET = process.env.PCLOUD_CLIENT_SECRET || ""; +const DEFAULT_YANDEXDISK_CLIENT_ID = process.env.YANDEXDISK_CLIENT_ID || ""; +const DEFAULT_YANDEXDISK_CLIENT_SECRET = + process.env.YANDEXDISK_CLIENT_SECRET || ""; +const DEFAULT_KOOFR_CLIENT_ID = process.env.KOOFR_CLIENT_ID || ""; +const DEFAULT_KOOFR_CLIENT_SECRET = process.env.KOOFR_CLIENT_SECRET || ""; + +module.exports = { + entry: "./src/main.ts", + target: "web", + output: { + filename: "main.js", + path: __dirname, + publicPath: "", + libraryTarget: "commonjs", + globalObject: "this", + }, + plugins: [ + new webpack.DefinePlugin({ + "global.DEFAULT_DROPBOX_APP_KEY": `"${DEFAULT_DROPBOX_APP_KEY}"`, + "global.DEFAULT_ONEDRIVE_CLIENT_ID": `"${DEFAULT_ONEDRIVE_CLIENT_ID}"`, + "global.DEFAULT_ONEDRIVE_AUTHORITY": `"${DEFAULT_ONEDRIVE_AUTHORITY}"`, + "global.DEFAULT_GOOGLEDRIVE_CLIENT_ID": `"${DEFAULT_GOOGLEDRIVE_CLIENT_ID}"`, + "global.DEFAULT_GOOGLEDRIVE_CLIENT_SECRET": `"${DEFAULT_GOOGLEDRIVE_CLIENT_SECRET}"`, + "global.DEFAULT_BOX_CLIENT_ID": `"${DEFAULT_BOX_CLIENT_ID}"`, + "global.DEFAULT_BOX_CLIENT_SECRET": `"${DEFAULT_BOX_CLIENT_SECRET}"`, + "global.DEFAULT_PCLOUD_CLIENT_ID": `"${DEFAULT_PCLOUD_CLIENT_ID}"`, + "global.DEFAULT_PCLOUD_CLIENT_SECRET": `"${DEFAULT_PCLOUD_CLIENT_SECRET}"`, + "global.DEFAULT_YANDEXDISK_CLIENT_ID": `"${DEFAULT_YANDEXDISK_CLIENT_ID}"`, + "global.DEFAULT_YANDEXDISK_CLIENT_SECRET": `"${DEFAULT_YANDEXDISK_CLIENT_SECRET}"`, + "global.DEFAULT_KOOFR_CLIENT_ID": `"${DEFAULT_KOOFR_CLIENT_ID}"`, + "global.DEFAULT_KOOFR_CLIENT_SECRET": `"${DEFAULT_KOOFR_CLIENT_SECRET}"`, + + "process.env.NODE_DEBUG": `undefined`, // ugly fix + "process.env.DEBUG": `undefined`, // ugly fix + // "process.version": `"v20.10.0"`, // who's using this? + // "process":`undefined`, + // "global.process":`undefined`, + + // make azure blob storage happy + // https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-util/src/checkEnvironment.ts + "globalThis.process.versions": `undefined`, + }), + // Work around for Buffer is undefined: + // https://github.com/webpack/changelog-v5/issues/10 + new webpack.ProvidePlugin({ + Buffer: ["buffer", "Buffer"], + }), + new webpack.ProvidePlugin({ + process: "process/browser", + }), + // Resolve node:xxx protocol imports (webpack 5 compatibility) + new webpack.NormalModuleReplacementPlugin(/^node:/, (resource) => { + resource.request = resource.request.replace(/^node:/, ""); + }), + ], + module: { + rules: [ + { + test: /\.worker\.ts$/, + loader: "worker-loader", + options: { + inline: "no-fallback", + }, + }, + { + test: /\.tsx?$/, + use: { + loader: "ts-loader", + options: { + // Skip type checking in webpack — handled separately by tsc. + // This allows main.js to emit despite pre-existing upstream type errors. + transpileOnly: true, + }, + }, + exclude: /node_modules/, + }, + { + test: /\.svg?$/, + type: "asset/source", + }, + { + test: /\.m?js$/, + resolve: { + fullySpecified: false, // process/browser returns some errors before + }, + }, + ], + }, + resolve: { + extensions: [".tsx", ".ts", ".js"], + mainFields: ["browser", "module", "main"], + // Map node:xxx protocol imports to browser polyfills + alias: { + "node:url": require.resolve("url/"), + // aggregate-error uses node:url (fileURLToPath) — not browser-safe. + // Replace with our minimal browser-compatible stub. + "aggregate-error": path.resolve(__dirname, "src/aggregateErrorStub.ts"), + }, + fallback: { + // assert: require.resolve("assert"), + // buffer: require.resolve("buffer/"), + // console: require.resolve("console-browserify"), + // constants: require.resolve("constants-browserify"), + crypto: require.resolve("crypto-browserify"), + // crypto: false, + // domain: require.resolve("domain-browser"), + // events: require.resolve("events"), + fs: false, + http: false, + // http: require.resolve("stream-http"), + https: false, + // https: require.resolve("https-browserify"), + net: false, + // os: require.resolve("os-browserify/browser"), + path: require.resolve("path-browserify"), + // punycode: require.resolve("punycode"), + process: require.resolve("process/browser"), + // querystring: require.resolve("querystring-es3"), + stream: require.resolve("stream-browserify"), + // string_decoder: require.resolve("string_decoder"), + // sys: require.resolve("util"), + // timers: require.resolve("timers-browserify"), + tls: false, + // tty: require.resolve("tty-browserify"), + url: require.resolve("url/"), + // util: require.resolve("util"), + // vm: require.resolve("vm-browserify"), + vm: false, + // zlib: require.resolve("browserify-zlib"), + }, + }, + externals: { + obsidian: "commonjs2 obsidian", + }, + optimization: { + minimize: true, + minimizer: [new TerserPlugin({ extractComments: false })], + }, +};