-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (96 loc) · 3.29 KB
/
index.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
'use strict';
const { google } = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = 'token.json';
const fs = require('fs');
const path = require('path');
const mime = require('mime-types')
module.exports = class gdrive {
constructor() {
if (fs.existsSync('credentials.json')) {
this._initialized = false;
var credentials = fs.readFileSync('credentials.json');
if (credentials != "") {
this._initialized = this.authorize(JSON.parse(credentials));
}
}
this._log = console.log;
}
setLogFunction(func) {
this._log = func;
}
initDriveInstance(auth) {
this._drive = google.drive({ version: 'v3', auth });
}
authorize(credentials) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
this._oAuth = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
// Check if we have previously stored a token.
if (fs.existsSync(TOKEN_PATH)) {
const token = fs.readFileSync(TOKEN_PATH);
this._oAuth.setCredentials(JSON.parse(token));
this.initDriveInstance(this._oAuth);
return true;
}
return false;
}
generateAuthUrl() {
return this._oAuth.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
}
authWithSecret(secret) {
this._oAuth.getToken(secret, (err, token) => {
if (err)
return this._log(err);
this._oAuth.setCredentials(token);
this._driveInitialized = true;
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return this._log(err);
this._log("TOKEN: " + JSON.stringify(token));
});
});
}
findFile(fileName) {
if (!this._initialized)
return Promise.reject("gdrive not initialized.")
return this._drive.files.list({
pageSize: 10,
q: "name = '" + fileName + "' and mimeType = 'application/vnd.google-apps.folder'",
fields: 'nextPageToken, files(id, name)',
}).then((res) => {
const files = res.data.files;
if (files.length == 1)
return Promise.resolve(files[0].id);
else
return Promise.reject("File not found");
}).catch((err) => {
return Promise.reject(err);
});
}
uploadFile(filename, description, folderId) {
if (!this._initialized)
return Promise.reject("gdrive not initialized.");
const name = path.basename(filename);
var fileMetadata = {
'parents': [folderId],
'name': name,
'description' : description
};
var media = {
mimeType: mime.lookup(filename),
body: fs.createReadStream(filename)
};
return this._drive.files.create({
resource: fileMetadata,
media: media,
fields: 'description, id, name, mimeType, starred'
}).then((file) => {
return Promise.resolve(file.data.id);
}).catch((err) => {
return Promise.reject(err);
});
}
}