This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
file-download-store.es6
527 lines (463 loc) · 15.6 KB
/
file-download-store.es6
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import _ from 'underscore';
import os from 'os';
import fs from 'fs';
import path from 'path';
import {exec} from 'child_process';
import {remote, shell} from 'electron';
import mkdirp from 'mkdirp';
import progress from 'request-progress';
import NylasStore from 'nylas-store';
import Actions from '../actions';
import NylasAPI from '../nylas-api';
import NylasAPIRequest from '../nylas-api-request';
Promise.promisifyAll(fs);
const mkdirpAsync = Promise.promisify(mkdirp);
const State = {
Unstarted: 'unstarted',
Downloading: 'downloading',
Finished: 'finished',
Failed: 'failed',
};
// TODO make this list more exhaustive
const NonPreviewableExtensions = [
'jpg',
'bmp',
'gif',
'png',
'jpeg',
'zip',
'tar',
'gz',
'bz2',
'dmg',
'exe',
'ics',
]
const THUMBNAIL_WIDTH = 320
// Expose the Download class for our tests, and possibly for other things someday
export class Download {
static State = State
constructor({accountId, fileId, targetPath, filename, filesize, progressCallback, retryWithBackoff}) {
this.accountId = accountId;
this.fileId = fileId;
this.targetPath = targetPath;
this.filename = filename;
this.filesize = filesize;
this.progressCallback = progressCallback;
this.retryWithBackoff = retryWithBackoff || false;
this.timeout = 15000;
this.maxTimeout = 2 * 60 * 1000;
this.attempts = 0;
this.maxAttempts = 10;
if (!this.accountId) {
throw new Error("Download.constructor: You must provide a non-empty accountId.");
}
if (!this.filename || this.filename.length === 0) {
throw new Error("Download.constructor: You must provide a non-empty filename.");
}
if (!this.fileId) {
throw new Error("Download.constructor: You must provide a fileID to download.");
}
if (!this.targetPath) {
throw new Error("Download.constructor: You must provide a target path to download.");
}
this.percent = 0;
this.promise = null;
this.state = State.Unstarted;
}
// We need to pass a plain object so we can have fresh references for the
// React views while maintaining the single object with the running
// request.
data() {
return Object.freeze(_.clone({
state: this.state,
fileId: this.fileId,
percent: this.percent,
filename: this.filename,
filesize: this.filesize,
targetPath: this.targetPath,
}));
}
run() {
// If run has already been called, return the existing promise. Never
// initiate multiple downloads for the same file
if (this.promise) { return this.promise; }
// Note: we must resolve or reject with `this`
this.promise = new Promise((resolve, reject) => {
const stream = fs.createWriteStream(this.targetPath);
this.state = State.Downloading;
let startRequest = null;
const onFailed = (err) => {
this.request = null;
stream.end();
if (!this.retryWithBackoff || this.attempts >= this.maxAttempts) {
this.state = State.Failed;
if (fs.existsSync(this.targetPath)) {
fs.unlinkSync(this.targetPath);
}
reject(err);
return;
}
this.timeout = Math.min(this.maxTimeout, this.timeout * 2);
startRequest();
};
const onSuccess = () => {
this.request = null;
stream.end();
this.state = State.Finished;
this.percent = 100;
resolve(this);
};
startRequest = () => {
console.info(`starting download with ${this.timeout}ms timeout`);
const request = new NylasAPIRequest({
api: NylasAPI,
options: {
json: false,
path: `/files/${this.fileId}/download`,
accountId: this.accountId,
encoding: null, // Tell `request` not to parse the response data
timeout: this.timeout,
started: (req) => {
this.attempts += 1;
this.request = req;
return progress(this.request, {throtte: 250})
.on('progress', (prog) => {
this.percent = prog.percent;
this.progressCallback();
})
// This is a /socket/ error event, not an HTTP error event. It fires
// when the conn is dropped, user if offline, but not on HTTP status codes.
// It is sometimes called in place of "end", not before or after.
.on('error', onFailed)
.on('end', () => {
if (this.state === State.Failed) { return; }
const {response} = this.request
const statusCode = response ? response.statusCode : null;
if ([200, 202, 204].includes(statusCode)) {
onSuccess();
} else {
onFailed(new Error(`Server returned a ${statusCode}`));
}
})
.pipe(stream);
},
},
});
request.run()
};
startRequest();
});
return this.promise
}
ensureClosed() {
if (this.request) {
this.request.abort()
}
}
}
class FileDownloadStore extends NylasStore {
constructor() {
super()
this.listenTo(Actions.fetchFile, this._fetch);
this.listenTo(Actions.fetchAndOpenFile, this._fetchAndOpen);
this.listenTo(Actions.fetchAndSaveFile, this._fetchAndSave);
this.listenTo(Actions.fetchAndSaveAllFiles, this._fetchAndSaveAll);
this.listenTo(Actions.abortFetchFile, this._abortFetchFile);
this._downloads = {};
this._filePreviewPaths = {};
this._downloadDirectory = path.join(NylasEnv.getConfigDirPath(), 'downloads');
mkdirp(this._downloadDirectory);
}
// Returns a path on disk for saving the file. Note that we must account
// for files that don't have a name and avoid returning <downloads/dir/"">
// which causes operations to happen on the directory (badness!)
//
pathForFile(file) {
if (!file) { return null; }
return path.join(this._downloadDirectory, file.id, file.safeDisplayName());
}
downloadDataForFile(fileId) {
const download = this._downloads[fileId]
if (!download) { return null; }
return download.data()
}
// Returns a hash of download objects keyed by fileId
//
downloadDataForFiles(fileIds = []) {
const downloadData = {};
fileIds.forEach((fileId) => {
downloadData[fileId] = this.downloadDataForFile(fileId);
});
return downloadData;
}
previewPathsForFiles(fileIds = []) {
const previewPaths = {};
fileIds.forEach((fileId) => {
previewPaths[fileId] = this.previewPathForFile(fileId);
});
return previewPaths;
}
previewPathForFile(fileId) {
return this._filePreviewPaths[fileId];
}
// Returns a promise with a Download object, allowing other actions to be
// daisy-chained to the end of the download operation.
_runDownload(file) {
const targetPath = this.pathForFile(file);
// is there an existing download for this file? If so,
// return that promise so users can chain to the end of it.
let download = this._downloads[file.id];
if (download) { return download.run(); }
// create a new download for this file
download = new Download({
accountId: file.accountId,
fileId: file.id,
filesize: file.size,
filename: file.displayName(),
targetPath,
progressCallback: () => this.trigger(),
retryWithBackoff: true,
});
// Do we actually need to queue and run the download? Queuing a download
// for an already-downloaded file has side-effects, like making the UI
// flicker briefly.
return this._prepareFolder(file).then(() => {
return this._checkForDownloadedFile(file)
.then((alreadyHaveFile) => {
if (alreadyHaveFile) {
// If we have the file, just resolve with a resolved download representing the file.
download.promise = Promise.resolve();
download.state = State.Finished;
return Promise.resolve(download);
}
this._downloads[file.id] = download;
this.trigger();
return download.run().finally(() => {
download.ensureClosed();
if (download.state === State.Failed) {
delete this._downloads[file.id];
}
this.trigger();
});
})
.then(() => this._generatePreview(file))
.then(() => Promise.resolve(download))
});
}
_generatePreview(file) {
if (process.platform !== 'darwin') { return Promise.resolve() }
if (!NylasEnv.config.get('core.attachments.displayFilePreview')) {
return Promise.resolve()
}
if (NonPreviewableExtensions.includes(file.displayExtension())) {
return Promise.resolve()
}
const filePath = this.pathForFile(file)
const previewPath = `${filePath}.png`
return fs.accessAsync(filePath, fs.F_OK)
.then(() => {
fs.accessAsync(previewPath, fs.F_OK)
.then(() => {
// If the preview file already exists, set our state and bail
this._filePreviewPaths[file.id] = previewPath
this.trigger()
})
.catch(() => {
// If the preview file doesn't exist yet, generate it
const fileDir = `"${path.dirname(filePath)}"`
const escapedPath = `"${filePath}"`
return new Promise((resolve) => {
const previewSize = THUMBNAIL_WIDTH * (11 / 8.5)
exec(`qlmanage -t -f ${window.devicePixelRatio} -s ${previewSize} -o ${fileDir} ${escapedPath}`, (error, stdout, stderr) => {
if (error) {
// Ignore errors, we don't really mind if we can't generate a preview
// for a file
NylasEnv.reportError(error)
resolve()
return
}
if (stdout.match(/No thumbnail created/i) || stderr) {
resolve()
return
}
this._filePreviewPaths[file.id] = previewPath
this.trigger()
resolve()
})
})
})
})
// If the file doesn't exist, ignore the error.
.catch(() => Promise.resolve())
}
// Returns a promise that resolves with true or false. True if the file has
// been downloaded, false if it should be downloaded.
//
_checkForDownloadedFile(file) {
return fs.statAsync(this.pathForFile(file))
.then((stats) => {
return Promise.resolve(stats.size >= file.size);
})
.catch(() => {
return Promise.resolve(false);
})
}
// Checks that the folder for the download is ready. Returns a promise that
// resolves when the download directory for the file has been created.
//
_prepareFolder(file) {
const targetFolder = path.join(this._downloadDirectory, file.id);
return fs.statAsync(targetFolder)
.catch(() => {
return mkdirpAsync(targetFolder);
});
}
_fetch = (file) => {
return this._runDownload(file)
.catch(this._catchFSErrors)
// Passively ignore
.catch(() => {});
}
_fetchAndOpen = (file) => {
return this._runDownload(file)
.then((download) => shell.openItem(download.targetPath))
.catch(this._catchFSErrors)
.catch((error) => {
return this._presentError({file, error});
});
}
_saveDownload = (download, savePath) => {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(download.targetPath);
stream.pipe(fs.createWriteStream(savePath));
stream.on('error', err => reject(err));
stream.on('end', () => resolve());
});
}
_fetchAndSave = (file) => {
const defaultPath = this._defaultSavePath(file);
const defaultExtension = path.extname(defaultPath);
NylasEnv.showSaveDialog({defaultPath}, (savePath) => {
if (!savePath) { return; }
const saveExtension = path.extname(savePath);
const newDownloadDirectory = path.dirname(savePath);
const didLoseExtension = defaultExtension !== '' && saveExtension === '';
let actualSavePath = savePath
if (didLoseExtension) {
actualSavePath += defaultExtension;
}
this._runDownload(file)
.then((download) => this._saveDownload(download, actualSavePath))
.then(() => {
if (NylasEnv.savedState.lastDownloadDirectory !== newDownloadDirectory) {
shell.showItemInFolder(actualSavePath);
NylasEnv.savedState.lastDownloadDirectory = newDownloadDirectory;
}
})
.catch(this._catchFSErrors)
.catch(error => {
this._presentError({file, error});
});
});
}
_fetchAndSaveAll = (files) => {
const defaultPath = this._defaultSaveDir();
const options = {
defaultPath,
title: 'Save Into...',
buttonLabel: 'Download All',
properties: ['openDirectory', 'createDirectory'],
};
return new Promise((resolve) => {
NylasEnv.showOpenDialog(options, (selected) => {
if (!selected) { return; }
const dirPath = selected[0];
if (!dirPath) { return; }
NylasEnv.savedState.lastDownloadDirectory = dirPath;
const lastSavePaths = [];
const savePromises = files.map((file) => {
const savePath = path.join(dirPath, file.safeDisplayName());
return this._runDownload(file)
.then((download) => this._saveDownload(download, savePath))
.then(() => lastSavePaths.push(savePath));
});
Promise.all(savePromises)
.then(() => {
if (lastSavePaths.length > 0) {
shell.showItemInFolder(lastSavePaths[0]);
}
return resolve(lastSavePaths);
})
.catch(this._catchFSErrors)
.catch((error) => {
return this._presentError({error});
});
});
});
}
_abortFetchFile = (file) => {
const download = this._downloads[file.id];
if (!download) { return; }
download.ensureClosed();
this.trigger();
const downloadPath = this.pathForFile(file);
fs.exists(downloadPath, (exists) => {
if (exists) {
fs.unlink(downloadPath);
}
});
}
_defaultSaveDir() {
let home = ''
if (process.platform === 'win32') {
home = process.env.USERPROFILE;
} else {
home = process.env.HOME;
}
let downloadDir = path.join(home, 'Downloads');
if (!fs.existsSync(downloadDir)) {
downloadDir = os.tmpdir();
}
if (NylasEnv.savedState.lastDownloadDirectory) {
if (fs.existsSync(NylasEnv.savedState.lastDownloadDirectory)) {
downloadDir = NylasEnv.savedState.lastDownloadDirectory;
}
}
return downloadDir;
}
_defaultSavePath(file) {
const downloadDir = this._defaultSaveDir();
return path.join(downloadDir, file.safeDisplayName());
}
_presentError({file, error} = {}) {
const name = file ? file.displayName() : "one or more files";
const errorString = error ? error.toString() : "";
return remote.dialog.showMessageBox({
type: 'warning',
message: "Download Failed",
detail: `Unable to download ${name}. Check your network connection and try again. ${errorString}`,
buttons: ["OK"],
});
}
_catchFSErrors(error) {
let message = null;
if (['EPERM', 'EMFILE', 'EACCES'].includes(error.code)) {
message = "N1 could not save an attachment. Check that permissions are set correctly and try restarting N1 if the issue persists.";
}
if (['ENOSPC'].includes(error.code)) {
message = "N1 could not save an attachment because you have run out of disk space.";
}
if (message) {
remote.dialog.showMessageBox({
type: 'warning',
message: "Download Failed",
detail: `${message}\n\n${error.message}`,
buttons: ["OK"],
});
return Promise.resolve();
}
return Promise.reject(error);
}
}
export default new FileDownloadStore()