Skip to content

Commit

Permalink
worker's google drive support upload & delete
Browse files Browse the repository at this point in the history
  • Loading branch information
maple3142 committed Oct 6, 2019
1 parent 0b79cc7 commit 7e17f1b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions worker/googleDrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7e17f1b

Please sign in to comment.