From 7e17f1bda650a70c1a59cdb289071c88dab1c023 Mon Sep 17 00:00:00 2001 From: maple3142 Date: Sun, 6 Oct 2019 20:35:44 +0800 Subject: [PATCH] worker's google drive support upload & delete --- worker/googleDrive.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/worker/googleDrive.js b/worker/googleDrive.js index 9680b754..79af72a5 100644 --- a/worker/googleDrive.js +++ b/worker/googleDrive.js @@ -118,5 +118,37 @@ class GoogleDrive { this._getIdCache.has(parentId + childName) return resp.files[0].id } + async upload(parentId, name, file) { + await this.initializeClient() + const createResp = await this.client.post('https://www.googleapis.com/upload/drive/v3/files', { + qs: { + uploadType: 'resumable', + supportsAllDrives: true + }, + json: { + name, + parents: [parentId] + } + }) + const putUrl = createResp.headers.get('Location') + return this.client + .put(putUrl, { + body: file + }) + .json() + } + async uploadByPath(path, name, file, rootId = 'root') { + const id = await this.getId(path, rootId) + if (!id) return null + return this.upload(id, name, file) + } + async delete(fileId) { + return this.client.delete(`files/${fileId}`) + } + async deleteByPath(path, rootId = 'root') { + const id = await this.getId(path, rootId) + if (!id) return null + return this.delete(id) + } } export default GoogleDrive