Skip to content
This repository was archived by the owner on Mar 27, 2020. It is now read-only.

Commit abedd70

Browse files
committed
Merge branch 'release/0.0.3'
2 parents d9eb736 + 40a857a commit abedd70

11 files changed

+539
-100
lines changed

.circleci/config.yml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
version: 2
2+
3+
anchors:
4+
- &image_default circleci/node:10.1.0
5+
- &working_directory ~/gdrive-lib
6+
jobs:
7+
build:
8+
docker:
9+
- image: *image_default
10+
working_directory: *working_directory
11+
steps:
12+
- checkout
13+
- restore_cache:
14+
key: gdrive-lib-dependency-cache-{{ checksum "package.json" }}
15+
- run:
16+
name: Running Build setup
17+
command: npm run build
18+
- save_cache:
19+
key: gdrive-lib-dependency-cache-{{ checksum "package.json" }}
20+
paths:
21+
- node_modules
22+
- persist_to_workspace:
23+
root: ~/gdrive-lib
24+
paths:
25+
- .
26+
test-vuln:
27+
docker:
28+
- image: *image_default
29+
working_directory: *working_directory
30+
steps:
31+
- attach_workspace:
32+
at: *working_directory
33+
- run:
34+
name: Running Vuln Tests
35+
command: npm run test:vuln
36+
test-mocha:
37+
docker:
38+
- image: *image_default
39+
working_directory: *working_directory
40+
steps:
41+
- attach_workspace:
42+
at: *working_directory
43+
- run:
44+
name: Running mocha tests
45+
command: npm run test:mocha
46+
test-eslint:
47+
docker:
48+
- image: *image_default
49+
working_directory: *working_directory
50+
steps:
51+
- attach_workspace:
52+
at: *working_directory
53+
- run:
54+
name: Running linter
55+
command: npm run test:lint
56+
57+
workflows:
58+
version: 2
59+
gdrive-lib-all:
60+
jobs:
61+
- build
62+
- test-vuln:
63+
requires:
64+
- build
65+
- test-mocha:
66+
requires:
67+
- build
68+
- test-eslint:
69+
requires:
70+
- build

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
sandbox.js

.eslintrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
"ignoreReadBeforeAssign": false
1818
}
1919
]
20+
},
21+
"env": {
22+
"node": true,
23+
"browser": true,
24+
"jest": true
2025
}
2126
}

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# gdrive-lib
22
Library for common Google Drive functionality.
33

4+
# Get Started
5+
6+
```
7+
yarn add @knotel/gdrive-lib --save
8+
```
49

510
# Google Folders
611

@@ -17,6 +22,9 @@ Library for common Google Drive functionality.
1722

1823
1. Specify credentials.json.
1924

25+
## Google Drive REST API Reference
26+
27+
* https://developers.google.com/drive/api/v3/reference/
2028

2129
## Supported MIME Types
2230

@@ -26,7 +34,7 @@ Library for common Google Drive functionality.
2634
## Getting Started
2735

2836
```
29-
let gdrive = new GDrive()
37+
const GDrive = require('@knotel/gdrive-lib')
3038
gdrive.init() // set auth within internal state of gdrive instance
3139
gdrive.getAll({
3240
rootFolderId: 'XXX'

build/gdrive.js

+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
'use strict'
2+
3+
var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault')
4+
5+
Object.defineProperty(exports, '__esModule', {
6+
value: true,
7+
})
8+
exports.default = void 0
9+
10+
var _regenerator = _interopRequireDefault(require('@babel/runtime/regenerator'))
11+
12+
var _asyncToGenerator2 = _interopRequireDefault(require('@babel/runtime/helpers/asyncToGenerator'))
13+
14+
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread'))
15+
16+
var _construct2 = _interopRequireDefault(require('@babel/runtime/helpers/construct'))
17+
18+
var _toConsumableArray2 = _interopRequireDefault(require('@babel/runtime/helpers/toConsumableArray'))
19+
20+
var _classCallCheck2 = _interopRequireDefault(require('@babel/runtime/helpers/classCallCheck'))
21+
22+
var _createClass2 = _interopRequireDefault(require('@babel/runtime/helpers/createClass'))
23+
24+
var _require = require('googleapis')
25+
var google = _require.google
26+
27+
require('dotenv').config()
28+
29+
var GDrive =
30+
/* #__PURE__ */
31+
(function () {
32+
function GDrive () {
33+
(0, _classCallCheck2.default)(this, GDrive)
34+
this.driveOptions = {
35+
pageSize: 200,
36+
corpora: 'teamDrive',
37+
supportsTeamDrives: true,
38+
includeTeamDriveItems: true,
39+
teamDriveId: process.env.TEAM_DRIVE,
40+
fields: 'files(id, name, mimeType, parents, trashed)',
41+
}
42+
this.authCredentials = [process.env.CLIENT_EMAIL, null, process.env.PRIVATE_KEY, ['https://www.googleapis.com/auth/drive'], process.env.USER_EMAIL]
43+
}
44+
/**
45+
* Initializes the Google Drive connection by creating the authorization token.
46+
*
47+
* @since 0.0.1
48+
*
49+
*/
50+
51+
(0, _createClass2.default)(GDrive, [{
52+
key: 'init',
53+
value: function init () {
54+
var _this = this
55+
56+
return new Promise(function (resolve, reject) {
57+
var auth = (0, _construct2.default)(google.auth.JWT, (0, _toConsumableArray2.default)(_this.authCredentials))
58+
_this.drive = google.drive({
59+
version: 'v3',
60+
auth: auth,
61+
})
62+
auth.authorize(function (err, tokens) {
63+
if (err) reject(err); else resolve(auth)
64+
})
65+
})
66+
},
67+
}, {
68+
key: 'get',
69+
value: function get (fileId) {
70+
var _this2 = this
71+
72+
return new Promise(function (resolve, reject) {
73+
_this2.drive.files.get({
74+
fileId: fileId,
75+
}).then(function (res) {
76+
resolve(res.data)
77+
}, function (err) {
78+
reject(err)
79+
})
80+
})
81+
},
82+
/**
83+
* Returns file by ID. If folder, will return children up to pageSize limit.
84+
*
85+
* @since 0.0.1
86+
* @param {Object} options Specific configurations for how to get files from Google Drive.
87+
* @param {string } fileId Unique Google Drive file ID.
88+
* @returns {Array} Returns direct descendents of root folder specified with names, MIME Types and select metadata.
89+
*
90+
*/
91+
92+
}, {
93+
key: 'getAll',
94+
value: function getAll (options) {
95+
var _this3 = this
96+
97+
var rootFolderId = options.rootFolderId
98+
return new Promise(function (resolve, reject) {
99+
_this3.drive.files.list((0, _objectSpread2.default)({}, _this3.driveOptions, {
100+
q: "'".concat(rootFolderId, "' in parents and trashed = false"),
101+
})).then(function (res) {
102+
resolve(res.data.files)
103+
}, function (err) {
104+
reject(err)
105+
})
106+
})
107+
},
108+
}, {
109+
key: 'upsert',
110+
value: function upsert (options, directorySchema) {
111+
var _this4 = this
112+
113+
var rootFolderId = options.rootFolderId
114+
return new Promise(function (resolve, reject) {
115+
return (0, _asyncToGenerator2.default)(
116+
/* #__PURE__ */
117+
_regenerator.default.mark(function _callee () {
118+
return _regenerator.default.wrap(function _callee$ (_context) {
119+
while (1) {
120+
switch (_context.prev = _context.next) {
121+
case 0:
122+
_context.next = 2
123+
return _this4._buildDirectory(_this4.drive, directorySchema, rootFolderId)
124+
125+
case 2:
126+
resolve(directorySchema)
127+
128+
case 3:
129+
case 'end':
130+
return _context.stop()
131+
}
132+
}
133+
}, _callee, this)
134+
}))()
135+
})
136+
},
137+
}, {
138+
key: 'update',
139+
value: function update () {},
140+
}, {
141+
key: 'destroy',
142+
value: function destroy () {},
143+
}, {
144+
key: '_upsert',
145+
value: function _upsert (drive, parentFolderId, fileMetaData) {
146+
var _this5 = this
147+
148+
return new Promise(function (resolve, reject) {
149+
drive.files.list((0, _objectSpread2.default)({}, _this5.driveOptions, {
150+
q: "'".concat(parentFolderId, "' in parents and trashed = false"),
151+
})).then(function (res) {
152+
var file = res.data.files.find(function (file) {
153+
return file.name === fileMetaData.resource['name']
154+
})
155+
156+
if (!file) {
157+
drive.files.create(fileMetaData).then(function (res) {
158+
console.log('Created '.concat(fileMetaData.resource['mimeType'], ': ').concat(fileMetaData.resource['name']))
159+
resolve(res.data)
160+
}, function (err) {
161+
reject(err)
162+
})
163+
} else {
164+
// TODO: Get count of all non-folder files directly within
165+
// IF SPACE ALREADY EXISTS, CHECK 'SPACE PHOTOS' FOR COUNT OF IMAGE FILES
166+
// if mimetype == folder
167+
// check folder for sizes
168+
console.log('File already exists! '.concat(fileMetaData.resource['mimeType'], ': ').concat(fileMetaData.resource['name']))
169+
resolve(file)
170+
}
171+
}, function (err) {
172+
reject(err)
173+
})
174+
})
175+
},
176+
}, {
177+
key: '_buildDirectory',
178+
value: (function () {
179+
var _buildDirectory2 = (0, _asyncToGenerator2.default)(
180+
/* #__PURE__ */
181+
_regenerator.default.mark(function _callee2 (drive, fileStructArray, parentFolderId) {
182+
var i, fileStruct, file
183+
return _regenerator.default.wrap(function _callee2$ (_context2) {
184+
while (1) {
185+
switch (_context2.prev = _context2.next) {
186+
case 0:
187+
i = 0
188+
189+
case 1:
190+
if (!(i < fileStructArray.length)) {
191+
_context2.next = 13
192+
break
193+
}
194+
195+
fileStruct = fileStructArray[i]
196+
_context2.next = 5
197+
return this._upsert(drive, parentFolderId, this._fileMetaData(fileStruct, parentFolderId))
198+
199+
case 5:
200+
file = _context2.sent
201+
// TODO: check for error
202+
fileStruct.id = file.id
203+
204+
if (!fileStruct.children) {
205+
_context2.next = 10
206+
break
207+
}
208+
209+
_context2.next = 10
210+
return this._buildDirectory(drive, fileStruct.children, fileStruct.id)
211+
212+
case 10:
213+
i++
214+
_context2.next = 1
215+
break
216+
217+
case 13:
218+
case 'end':
219+
return _context2.stop()
220+
}
221+
}
222+
}, _callee2, this)
223+
}))
224+
225+
function _buildDirectory (_x, _x2, _x3) {
226+
return _buildDirectory2.apply(this, arguments)
227+
}
228+
229+
return _buildDirectory
230+
}()),
231+
}, {
232+
key: '_fileMetaData',
233+
value: function _fileMetaData (fileStruct, parentFolderId) {
234+
// TODO: Handle for files created from template.
235+
// TODO: Handle for dynamic folders created using spaces.
236+
var resource = {
237+
'name': fileStruct.name,
238+
'parents': [parentFolderId],
239+
'mimeType': fileStruct.mimeType,
240+
'teamDriveId': this.driveOptions.teamDriveId,
241+
'fields': 'id',
242+
}
243+
return {
244+
resource: resource,
245+
supportsTeamDrives: true,
246+
fields: 'id, parents',
247+
}
248+
},
249+
}])
250+
return GDrive
251+
}())
252+
253+
var _default = GDrive
254+
exports.default = _default

example.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"mimeType": "application/vnd.google-apps.folder",
55
"children": [
66
{ "name": "Archive", "mimeType": "application/vnd.google-apps.folder" },
7-
{ "name": "Archive 2", "mimeType": "application/vnd.google-apps.folder",
7+
{ "name": "Archive 2", "id": "1MATkffMyyhAq6Mc__zyVIk_AIzSo09mf", "mimeType": "application/vnd.google-apps.folder",
88
"children": [
99
{ "name": "sub archive", "mimeType": "application/vnd.google-apps.folder" },
1010
{ "name": "sub archive 2",

0 commit comments

Comments
 (0)