From 4f04de0c4374a27cd35bd29dd993e52977f4974f Mon Sep 17 00:00:00 2001 From: CodeingBoy Date: Sun, 15 Dec 2019 10:49:59 +0800 Subject: [PATCH 1/4] Add missing .prettierrc and reformat index.js --- worker/.prettierrc | 8 ++++++++ worker/index.js | 14 +++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 worker/.prettierrc diff --git a/worker/.prettierrc b/worker/.prettierrc new file mode 100644 index 00000000..6e50ea95 --- /dev/null +++ b/worker/.prettierrc @@ -0,0 +1,8 @@ +{ + "useTabs": true, + "tabWidth": 4, + "endOfLine": "lf", + "singleQuote": true, + "semi": false, + "printWidth": 120 +} \ No newline at end of file diff --git a/worker/index.js b/worker/index.js index 29433ff4..3303c0d1 100644 --- a/worker/index.js +++ b/worker/index.js @@ -102,9 +102,14 @@ async function onPut(request) { const u = new URL(url) const Referer = u.href const Origin = u.protocol + '//' + u.host - fileBody = (await fetch(url, { - headers: { Referer, Origin } - })).body + fileBody = ( + await fetch(url, { + headers: { + Referer, + Origin + } + }) + ).body } else { fileBody = request.body } @@ -118,6 +123,7 @@ async function onPut(request) { } }) } + function unauthorized() { return new Response('Unauthorized', { headers: { @@ -127,6 +133,7 @@ function unauthorized() { status: 401 }) } + function parseBasicAuth(auth) { try { return atob(auth.split(' ').pop()).split(':') @@ -134,6 +141,7 @@ function parseBasicAuth(auth) { return [] } } + function doBasicAuth(request) { const auth = request.headers.get('Authorization') if (!auth || !/^Basic [A-Za-z0-9._~+/-]+=*$/i.test(auth)) { From 9994381c41f6416eda8033a0befd0f92e4d08f13 Mon Sep 17 00:00:00 2001 From: CodeingBoy Date: Sun, 15 Dec 2019 13:52:33 +0800 Subject: [PATCH 2/4] Add copy API --- worker/googleDrive.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/worker/googleDrive.js b/worker/googleDrive.js index deb5ddab..d6a0d8d8 100644 --- a/worker/googleDrive.js +++ b/worker/googleDrive.js @@ -167,5 +167,21 @@ class GoogleDrive { if (!id) return null return this.delete(id) } + async copy(fileId, parentId) { + this.initializeClient() + if (parentId) { + return this.client + .post(`files/${fileId}/copy`, { + json: { + parents: [parentId] + } + }).json() + } else { + return this.client + .post(`files/${fileId}/copy`, { + json: {} + }).json() + } + } } export default GoogleDrive From c0bec867cafd64a82937638c0477315ce09954b2 Mon Sep 17 00:00:00 2001 From: CodeingBoy Date: Sun, 15 Dec 2019 10:48:00 +0800 Subject: [PATCH 3/4] Introduce file copying on forbidden --- worker/index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/worker/index.js b/worker/index.js index 3303c0d1..41cfbe5f 100644 --- a/worker/index.js +++ b/worker/index.js @@ -41,7 +41,27 @@ async function onGet(request) { } const isGoogleApps = result.mimeType.includes('vnd.google-apps') if (!isGoogleApps) { - const r = await gd.download(result.id, request.headers.get('Range')) + let r + try { + r = await gd.download(result.id, request.headers.get('Range')) + } catch (e) { + if (e.toString().indexOf('Forbidden') !== -1 && self.props.copy_on_forbidden) { + // try copy file + const copiedFile = await gd.copy(result.id, self.props.copy_parent_id) + r = await gd.download(copiedFile.id, request.headers.get('Range')) + } else { + // other error, return it + return new Response( + { + error: e + }, + { + status: r.status + } + ) + } + } + const h = new Headers(r.headers) h.set('Content-Disposition', `inline; filename*=UTF-8''${encodeURIComponent(result.name)}`) return new Response(r.body, { From b7217465b78be992f2832e8f60319210684338db Mon Sep 17 00:00:00 2001 From: CodeingBoy Date: Sun, 15 Dec 2019 13:39:35 +0800 Subject: [PATCH 4/4] Add documentations --- README.md | 19 +++++++++++++++++++ README.zh.md | 19 +++++++++++++++++++ README.zhtw.md | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/README.md b/README.md index fecf3d36..842a682b 100644 --- a/README.md +++ b/README.md @@ -37,3 +37,22 @@ Go [https://gdindex-code-builder.glitch.me/](https://gdindex-code-builder.glitch 5. Copy the content of [worker/dist/worker.js](worker/dist/worker.js) to CloudFlare Workers. 6. Fill `refresh_token`, `root_folder_id` and other options on the top of the script. 7. Deploy! + +### Enabling file copy on forbidden + +Google Drive limited each users' file sharing bandwidth(about 750GB per day). If you try pulling a shared file from who exceed this limit, you will receive a `403 - forbidden` error. Copying file to your may solve this problem, but it hurts because you can only copy a file once a time. + +That's why "Copy on forbidden" comes in. + +1. Create a folder, which will be used to store your copied files, crawl its id from network requests(normally you can get it from Developer Tools) +2. Add following config to your `worker.js`: + +``` + ... + copy_on_forbidden: true, + copy_parent_id: 'YOUR_COPY_FOLDER_ID' // replace YOUR_COPY_FOLDER_ID to your copy folder's ID +``` + +3. Just do normal download, if this file exceed limits, worker will make a copy and return copied one to you. This process is transparent so you won't need to deal other things. + +Note: Be sure to delete all you copied files after a while, as more files get copied, it will consumer more space on you drive. Besides, this feature will NOT detect existing copies, multiple downloads will leads multiple copies. diff --git a/README.zh.md b/README.zh.md index 314e8da6..918447e9 100644 --- a/README.zh.md +++ b/README.zh.md @@ -33,3 +33,22 @@ 5. 复制 [worker/dist/worker.js](worker/dist/worker.js) 的内容到 CloudFlare Workers 6. 在脚本顶端填上 `refresh_token`, `root_folder_id` 以及其他的选项 7. 部署! + +### 启用按需复制 + +Google Drive 限制了每个用户分享文件的流量(一般是每天 750GB)。如果你试图从超出流量限制的用户处下载一个分享文件,会收到 `403 - forbidden` 错误。将文件复制一份可能可以解决此问题,但是在网页端上一次操作只能复制一个文件。 + +因此,我们引入了“按需复制”功能。 + +1. 新建一个用于存放所有复制文件的文件夹。打开“开发者工具”获得这个文件夹的 ID +2. 添加以下配置到你的 `worker.js`: + +``` + ... + copy_on_forbidden: true, + copy_parent_id: 'YOUR_COPY_FOLDER_ID' // 替换 YOUR_COPY_FOLDER_ID 为你的文件夹 ID +``` + +3. 正常开始下载即可,如果 worker 检测到文件分享流量超出限制,会自动复制一份到之前的文件夹并返回复制的文件给你。该过程是透明的,因此你无需进行额外处理 + +注意:请在下载完一段时间后删除复制的文件,否则,随着复制的文件越多,它们会占用更多的空间。另外,复制过程中不会检测是否已有复制的文件,因此多次下载会触发多次复制动作。 diff --git a/README.zhtw.md b/README.zhtw.md index 4fa0325f..2dec52b9 100644 --- a/README.zhtw.md +++ b/README.zhtw.md @@ -33,3 +33,22 @@ 5. 複製 [worker/dist/worker.js](worker/dist/worker.js) 的內容到 CloudFlare Workers 6. 在腳本頂端填上 `refresh_token`, `root_folder_id` 以及其他的選項 7. 部署! + +### 啟用按需複制 + +Google Drive 限制了每個用戶分享文件的流量(一般是每天 750GB)。如果你試圖從超出流量限制的用戶處下載一個分享文件,會收到 `403 - forbidden` 錯誤。將文件複製一份可能可以解決此問題,但是在網頁端上一次操作只能複制一個文件。 + +因此,我們引入了“按需複制”功能。 + +1. 新建一個用於存放所有復製文件的文件夾。打開“開發者工具”獲得這個文件夾的 ID +2. 添加以下配置到你的 `worker.js`: + +``` +  ... +  copy_on_forbidden: true, +  copy_parent_id: 'YOUR_COPY_FOLDER_ID' // 替換 YOUR_COPY_FOLDER_ID 為你的文件夾 ID +``` + +3. 正常開始下載即可,如果 worker 檢測到文件分享流量超出限制,會自動複製一份到之前的文件夾並返回複製的文件給你。該過程是透明的,因此你無需進行額外處理 + +注意:請在下載完一段時間後刪除複製的文件,否則,隨著複製的文件越多,它們會佔用更多的空間。另外,複製過程中不會檢測是否已有復制的文件,因此多次下載會觸發多次復制動作。