-
Notifications
You must be signed in to change notification settings - Fork 1
/
gDrive.js
168 lines (112 loc) · 3 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
var async = require('async');
var fs = require('fs');
var readline = require('readline');
var googleapis = require('googleapis');
var SCOPE = 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file';
var OAuthClient;
var credential;
var CLIENT_ID;
var CLIENT_SECRET;
var REDIRECT_URL;
var isAuthed;
exports.auth = function (filename, done) {
credential = require(filename);
CLIENT_ID = credential.installed.client_id;
CLIENT_SECRET = credential.installed.client_secret;
REDIRECT_URL = credential.installed.redirect_uris[0];
OAuthClient = new googleapis.OAuth2Client(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL)
async.waterfall([
// Ask for Code
function (cb) {
if (credential.tokens) {
cb(null, credential.tokens);
return;
}
var url = OAuthClient.generateAuthUrl({
access_type: 'offline', // will return a refresh token
scope: SCOPE
});
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Please Visit:\n' + url + '\n And paste the result here:\n', function (code) {
OAuthClient.getToken(code, function(err, tokens) {
cb(err, tokens);
});
});
},
// Update the tokens
function (tokens, cb) {
OAuthClient.setCredentials(tokens);
credential.tokens = tokens;
fs.writeFile(filename, JSON.stringify(credential), cb);
},
], function (err) {
if (err) {
return done(err);
}
isAuthed = true;
done(null);
});
};
exports.upload = function (title, file, folderId, done) {
if (!isAuthed) {
throw new Error('Googleapis: Unauthorized');
}
async.waterfall([
// Get client
function (cb) {
googleapis.discover('drive', 'v2').execute(cb);
},
// Upload
function (client, cb) {
console.log('Uploading ' + title);
client.drive.files.insert({
title: title+'.html',
mimeType: 'text/html',
convert: true
})
.withMedia('text/html', file)
.withAuthClient(OAuthClient)
.execute(function (err, fileResponse) {
if (err) {
cb(err);
return;
}
cb(null, client, fileResponse);
});
},
// Rename & Move to new folder
function (client, fileResponse, cb) {
var fileId = fileResponse.id;
var originalFolderId = fileResponse.parents[0].id;
var ops = {
fileId: fileId
};
console.log('Rename file as ' + title);
if (folderId) {
console.log('Move file from ' + originalFolderId + ' to folder ' + folderId);
ops.addParents = folderId;
ops.removeParents = originalFolderId;
}
client.drive.files.patch(ops, {
title: title
}).withAuthClient(OAuthClient)
.execute(function (err, fileResponse) {
cb(err, fileResponse);
});
},
// Get link
function (fileResponse, cb) {
var link = fileResponse.alternateLink;
cb(null, link);
}
], function (err, link) {
var realLink = link.replace(/\?usp.*/, '');
done(null, realLink);
});
};