-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
file-api-driver-onedrive.js
300 lines (245 loc) · 8.48 KB
/
file-api-driver-onedrive.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const moment = require('moment');
const { dirname, basename } = require('lib/path-utils.js');
class FileApiDriverOneDrive {
constructor(api) {
this.api_ = api;
this.pathCache_ = {};
}
api() {
return this.api_;
}
itemFilter_() {
return {
select: 'name,file,folder,fileSystemInfo,parentReference',
};
}
makePath_(path) {
return path;
}
makeItems_(odItems) {
let output = [];
for (let i = 0; i < odItems.length; i++) {
output.push(this.makeItem_(odItems[i]));
}
return output;
}
makeItem_(odItem) {
let output = {
path: odItem.name,
isDir: 'folder' in odItem,
};
if ('deleted' in odItem) {
output.isDeleted = true;
} else {
// output.created_time = moment(odItem.fileSystemInfo.createdDateTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('x');
output.updated_time = moment(odItem.fileSystemInfo.lastModifiedDateTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('x');
}
return output;
}
async statRaw_(path) {
let item = null;
try {
item = await this.api_.execJson('GET', this.makePath_(path), this.itemFilter_());
} catch (error) {
if (error.code == 'itemNotFound') return null;
throw error;
}
return item;
}
async stat(path) {
let item = await this.statRaw_(path);
if (!item) return null;
return this.makeItem_(item);
}
async setTimestamp(path, timestamp) {
let body = {
fileSystemInfo: {
lastModifiedDateTime:
`${moment
.unix(timestamp / 1000)
.utc()
.format('YYYY-MM-DDTHH:mm:ss.SSS')}Z`,
},
};
let item = await this.api_.execJson('PATCH', this.makePath_(path), null, body);
return this.makeItem_(item);
}
async list(path, options = null) {
let query = this.itemFilter_();
let url = `${this.makePath_(path)}:/children`;
if (options.context) {
query = null;
url = options.context;
}
let r = await this.api_.execJson('GET', url, query);
return {
hasMore: !!r['@odata.nextLink'],
items: this.makeItems_(r.value),
context: r['@odata.nextLink'],
};
}
async get(path, options = null) {
if (!options) options = {};
try {
if (options.target == 'file') {
let response = await this.api_.exec('GET', `${this.makePath_(path)}:/content`, null, null, options);
return response;
} else {
let content = await this.api_.execText('GET', `${this.makePath_(path)}:/content`);
return content;
}
} catch (error) {
if (error.code == 'itemNotFound') return null;
throw error;
}
}
async mkdir(path) {
let item = await this.stat(path);
if (item) return item;
let parentPath = dirname(path);
item = await this.api_.execJson('POST', `${this.makePath_(parentPath)}:/children`, this.itemFilter_(), {
name: basename(path),
folder: {},
});
return this.makeItem_(item);
}
async put(path, content, options = null) {
if (!options) options = {};
let response = null;
try {
if (options.source == 'file') {
response = await this.api_.exec('PUT', `${this.makePath_(path)}:/content`, null, null, options);
} else {
options.headers = { 'Content-Type': 'text/plain' };
response = await this.api_.exec('PUT', `${this.makePath_(path)}:/content`, null, content, options);
}
} catch (error) {
if (error && error.code === 'BadRequest' && error.message === 'Maximum request length exceeded.') {
error.code = 'rejectedByTarget';
error.message = 'Resource exceeds OneDrive max file size (4MB)';
}
throw error;
}
return response;
}
delete(path) {
return this.api_.exec('DELETE', this.makePath_(path));
}
async move() {
// Cannot work in an atomic way because if newPath already exist, the OneDrive API throw an error
// "An item with the same name already exists under the parent". Some posts suggest to use
// @name.conflictBehavior [0]but that doesn't seem to work. So until Microsoft fixes this
// it's not possible to do an atomic move.
//
// [0] https://stackoverflow.com/questions/29191091/onedrive-api-overwrite-on-move
throw new Error('NOT WORKING');
// let previousItem = await this.statRaw_(oldPath);
// let newDir = dirname(newPath);
// let newName = basename(newPath);
// // We don't want the modification date to change when we move the file so retrieve it
// // now set it in the PATCH operation.
// let item = await this.api_.execJson('PATCH', this.makePath_(oldPath), this.itemFilter_(), {
// name: newName,
// parentReference: { path: newDir },
// fileSystemInfo: {
// lastModifiedDateTime: previousItem.fileSystemInfo.lastModifiedDateTime,
// },
// });
// return this.makeItem_(item);
}
format() {
throw new Error('Not implemented');
}
async pathDetails_(path) {
if (this.pathCache_[path]) return this.pathCache_[path];
let output = await this.api_.execJson('GET', path);
this.pathCache_[path] = output;
return this.pathCache_[path];
}
clearRoot() {
throw new Error('Not implemented');
}
async delta(path, options = null) {
let output = {
hasMore: false,
context: {},
items: [],
};
const freshStartDelta = () => {
const url = `${this.makePath_(path)}:/delta`;
const query = this.itemFilter_();
query.select += ',deleted';
return { url: url, query: query };
};
const pathDetails = await this.pathDetails_(path);
const pathId = pathDetails.id;
let context = options ? options.context : null;
let url = context ? context.nextLink : null;
let query = null;
if (!url) {
const info = freshStartDelta();
url = info.url;
query = info.query;
}
let response = null;
try {
response = await this.api_.execJson('GET', url, query);
} catch (error) {
if (error.code === 'resyncRequired') {
// Error: Resync required. Replace any local items with the server's version (including deletes) if you're sure that the service was up to date with your local changes when you last sync'd. Upload any local changes that the server doesn't know about.
// Code: resyncRequired
// Request: GET https://graph.microsoft.com/v1.0/drive/root:/Apps/JoplinDev:/delta?select=...
// The delta token has expired or is invalid and so a full resync is required. This happens for example when all the items
// on the OneDrive App folder are manually deleted. In this case, instead of sending the list of deleted items in the delta
// call, OneDrive simply request the client to re-sync everything.
// OneDrive provides a URL to resume syncing from but it does not appear to work so below we simply start over from
// the beginning. The synchronizer will ensure that no duplicate are created and conflicts will be resolved.
// More info there: https://stackoverflow.com/q/46941371/561309
const info = freshStartDelta();
url = info.url;
query = info.query;
response = await this.api_.execJson('GET', url, query);
} else {
throw error;
}
}
let items = [];
// The delta API might return things that happen in subdirectories of the root and we don't want to
// deal with these since all the files we're interested in are at the root (The .resource dir
// is special since it's managed directly by the clients and resources never change - only the
// associated .md file at the root is synced). So in the loop below we check that the parent is
// indeed the root, otherwise the item is skipped.
// (Not sure but it's possible the delta API also returns events for files that are copied outside
// of the app directory and later deleted or modified. We also don't want to deal with
// these files during sync).
for (let i = 0; i < response.value.length; i++) {
const v = response.value[i];
if (v.parentReference.id !== pathId) continue;
items.push(this.makeItem_(v));
}
output.items = output.items.concat(items);
let nextLink = null;
if (response['@odata.nextLink']) {
nextLink = response['@odata.nextLink'];
output.hasMore = true;
} else {
if (!response['@odata.deltaLink']) throw new Error(`Delta link missing: ${JSON.stringify(response)}`);
nextLink = response['@odata.deltaLink'];
}
output.context = { nextLink: nextLink };
// https://dev.onedrive.com/items/view_delta.htm
// The same item may appear more than once in a delta feed, for various reasons. You should use the last occurrence you see.
// So remove any duplicate item from the array.
let temp = [];
let seenPaths = [];
for (let i = output.items.length - 1; i >= 0; i--) {
let item = output.items[i];
if (seenPaths.indexOf(item.path) >= 0) continue;
temp.splice(0, 0, item);
seenPaths.push(item.path);
}
output.items = temp;
return output;
}
}
module.exports = { FileApiDriverOneDrive };