Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 19 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 检测到文件分享流量超出限制,会自动复制一份到之前的文件夹并返回复制的文件给你。该过程是透明的,因此你无需进行额外处理

注意:请在下载完一段时间后删除复制的文件,否则,随着复制的文件越多,它们会占用更多的空间。另外,复制过程中不会检测是否已有复制的文件,因此多次下载会触发多次复制动作。
19 changes: 19 additions & 0 deletions README.zhtw.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 檢測到文件分享流量超出限制,會自動複製一份到之前的文件夾並返回複製的文件給你。該過程是透明的,因此你無需進行額外處理

注意:請在下載完一段時間後刪除複製的文件,否則,隨著複製的文件越多,它們會佔用更多的空間。另外,複製過程中不會檢測是否已有復制的文件,因此多次下載會觸發多次復制動作。
8 changes: 8 additions & 0 deletions worker/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"useTabs": true,
"tabWidth": 4,
"endOfLine": "lf",
"singleQuote": true,
"semi": false,
"printWidth": 120
}
16 changes: 16 additions & 0 deletions worker/googleDrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 32 additions & 4 deletions worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -102,9 +122,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
}
Expand All @@ -118,6 +143,7 @@ async function onPut(request) {
}
})
}

function unauthorized() {
return new Response('Unauthorized', {
headers: {
Expand All @@ -127,13 +153,15 @@ function unauthorized() {
status: 401
})
}

function parseBasicAuth(auth) {
try {
return atob(auth.split(' ').pop()).split(':')
} catch (e) {
return []
}
}

function doBasicAuth(request) {
const auth = request.headers.get('Authorization')
if (!auth || !/^Basic [A-Za-z0-9._~+/-]+=*$/i.test(auth)) {
Expand Down