This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdrive.js
247 lines (230 loc) · 7.58 KB
/
gdrive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict'
const { google } = require('googleapis')
require('dotenv').config()
const delay = (ms) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms)
})
}
class GDrive {
constructor () {
const buff = Buffer.from(process.env.PRIVATE_KEY, 'base64')
const PRIVATE_KEY = buff.toString()
this.driveOptions = {
pageSize: 200,
corpora: 'teamDrive',
supportsTeamDrives: true,
includeTeamDriveItems: true,
teamDriveId: process.env.TEAM_DRIVE,
fields: 'files(id, name, mimeType, parents, trashed)',
}
this.authCredentials = [
process.env.CLIENT_EMAIL,
null,
PRIVATE_KEY,
['https://www.googleapis.com/auth/drive'],
process.env.USER_EMAIL,
]
}
/**
* Initializes the Google Drive connection by creating the authorization token.
*
* @since 0.0.1
*
*/
init () {
return new Promise((resolve, reject) => {
const auth = new google.auth.JWT(...this.authCredentials)
this.auth = auth
this.drive = google.drive({ version: 'v3', auth })
auth.authorize(function (err, tokens) {
if (err) {
console.error(err)
reject(err)
} else {
resolve(auth)
}
})
})
}
get (fileId) {
return new Promise((resolve, reject) => {
this.drive.files.get({ fileId: fileId })
.then((res) => {
resolve(res.data)
}, (err) => {
console.error(err)
reject(err)
})
})
}
/**
* Fetch all files from Google Drive folder specified. Can recursively traverse folder if specified in `recursive` option.
* Can return folders only with `foldersOnly` option. Can add a non-folder file count with `nonFolderFileCount`.
*
* @since 0.0.1
* @param {Object} options Specific configurations for how to get files from Google Drive.
* @returns {Array} Returns descendents of root folder in either a flat object array or nested object array.
*
*/
getAll ({ rootFolderId, recursive = false, foldersOnly = false, nonFolderFileCount = true } = {}) {
const fileStructure = {
id: rootFolderId,
children: [],
}
return new Promise((resolve, reject) => {
return (async () => {
await this._getDirectory({ recursive, foldersOnly, nonFolderFileCount }, fileStructure, rootFolderId)
resolve(fileStructure)
})()
})
}
async _getDirectory (options, parentFolder, parentFolderId) {
await delay(500)
const files = await this._fetchGoogleFiles(parentFolderId, options.foldersOnly)
parentFolder.children = files // push onto file structure
if (options.nonFolderFileCount) {
parentFolder.nonFolderFileCount = files.filter(file => file.mimeType !== 'application/vnd.google-apps.folder').length
}
if (files.length <= 0) return // base case
if (options.recursive) {
let i = 0
while (i < files.length) {
const file = files[i]
if (file.mimeType === 'application/vnd.google-apps.folder') await this._getDirectory(options, file, file.id)
i++
}
}
}
async _fetchGoogleFiles (parentFolderId, foldersOnly = false) {
await delay(500)
return new Promise((resolve, reject) => {
this.drive.files.list({
...this.driveOptions,
q: `${foldersOnly ? 'mimeType = "application/vnd.google-apps.folder" and ' : ''}'${parentFolderId}' in parents and trashed = false`,
}).then(res => {
resolve(res.data.files)
}, err => {
console.error(err)
reject(err)
})
})
}
/**
* Recursively upsert folders by ID or name specified in schema.
*
* @since 0.0.1
* @param {Object} options Specific configurations for how to get files from Google Drive.
* @param {Array} directorySchema Nested structure of objects and object arrays. All files should have an ID or name.
* @returns {Array} Returns directorySchema in the same format, except missing names and IDs are returned where files are newly created.
*
*/
upsert ({ rootFolderId, rename }, directorySchema) {
return new Promise((resolve, reject) => {
return (async () => {
await this._upsertDirectory(directorySchema, rootFolderId, rename)
resolve(directorySchema)
})()
})
}
async _upsertDirectory (fileStructArray, parentFolderId, rename) {
let i = 0
while (i < fileStructArray.length) {
await delay(500)
const file = await this._upsertFile(parentFolderId, rename, fileStructArray[i])
// TODO: check for error
fileStructArray[i].id = file.id
fileStructArray[i].name = file.name
if (fileStructArray[i].children) await this._upsertDirectory(fileStructArray[i].children, fileStructArray[i].id, rename)
i++
}
}
async _upsertFile (parentFolderId, rename, fileStruct) {
await delay(500)
return new Promise((resolve, reject) => {
this._fetchGoogleFiles(parentFolderId, false).then(files => {
let file = files.find((file) => {
if (rename && fileStruct.id) return file.id === fileStruct.id
})
if (!file) file = files.find(file => file.name === fileStruct.name)
if (!file) {
fileStruct.nonFolderFileCount = 0
this._createGoogleFile(parentFolderId, fileStruct).then(file => {
resolve(file)
}, (err) => {
console.error(err)
reject(err)
})
} else {
this._fetchGoogleFiles(file.id, false).then(files => {
fileStruct.nonFolderFileCount = files.filter(file => file.mimeType !== 'application/vnd.google-apps.folder').length
})
if (rename && file.name !== fileStruct.name) {
this._updateGoogleFile(file, fileStruct).then(file => {
resolve(file)
}, (err) => {
console.error(err)
reject(err)
})
} else {
console.log(`File already exists! ${fileStruct.mimeType}: ${fileStruct.name}`)
resolve(file)
}
}
}, (err) => {
console.error(err)
reject(err)
})
})
}
async _createGoogleFile (parentFolderId, fileStruct) {
await delay(500)
const fileMetadata = this._createGoogleMetadata(fileStruct, parentFolderId)
return new Promise((resolve, reject) => {
this.drive.files.create(fileMetadata).then(res => {
console.log(`Created ${fileStruct.mimeType}: ${fileStruct.name}`)
resolve({ ...res.data, name: fileStruct.name, mimeType: fileStruct.mimeType })
}, err => {
console.error(err)
reject(err)
})
})
}
async _updateGoogleFile (file, fileStruct) {
await delay(500)
return new Promise((resolve, reject) => {
this.drive.files.update({
fileId: fileStruct.id,
supportsTeamDrives: true,
resource: {
'name': fileStruct.name,
},
}).then(res => {
console.log(`Updated ${fileStruct.mimeType}: ${file.name} -> ${fileStruct.name}`)
resolve(res.data)
}, err => {
console.error(err)
reject(err)
})
})
}
_createGoogleMetadata (fileStruct, parentFolderId) {
// TODO: Handle for files created from template.
// TODO: Handle for dynamic folders created using spaces.
const resource = {
'name': fileStruct.name,
'parents': [parentFolderId],
'mimeType': fileStruct.mimeType,
'teamDriveId': this.driveOptions.teamDriveId,
'fields': 'id name',
}
return {
resource: resource,
supportsTeamDrives: true,
fields: 'id, parents',
}
}
update () {}
destroy () {}
}
module.exports = GDrive