-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathupload.js
92 lines (78 loc) · 2.84 KB
/
upload.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
/* eslint-disable no-sync */
'use strict';
const BoxCommand = require('../../box-command');
const { Flags, Args } = require('@oclif/core');
const fs = require('fs');
const path = require('path');
const BoxCLIError = require('../../cli-error');
const utils = require('../../util');
const CHUNKED_UPLOAD_FILE_SIZE = 1024 * 1024 * 100; // 100 MiB
class FoldersUploadCommand extends BoxCommand {
async run() {
const { flags, args } = await this.parse(FoldersUploadCommand);
let folderId = await this.uploadFolder(args.path, flags['parent-folder'], flags['folder-name']);
let folder = await this.client.folders.get(folderId);
await this.output(folder);
}
async uploadFolder(folderPath, parentFolderId, folderName) {
folderName = folderName || path.basename(folderPath);
let folderItems;
try {
folderItems = await utils.readdirAsync(folderPath);
} catch (ex) {
throw new BoxCLIError(`Could not read directory ${folderPath}`, ex);
}
// Filters out files or folders that have a "." as the first character in their name. These won't be uploaded.
folderItems = folderItems.filter(item => item[0] !== '.');
let folder = await this.client.folders.create(parentFolderId, folderName);
let folderId = folder.id;
// @TODO(2018-09-14): Add --preserve-timestamps flag
for (let item of folderItems) {
// @TODO(2018-08-15): Improve performance by queueing async work and performing it in parallel
/* eslint-disable no-await-in-loop */
let itemPath = path.join(folderPath, item);
let itemStat = fs.statSync(itemPath);
if (itemStat.isDirectory()) {
await this.uploadFolder(itemPath, folderId);
} else {
let size = itemStat.size;
try {
let fileStream = fs.createReadStream(itemPath);
if (size < CHUNKED_UPLOAD_FILE_SIZE) {
await this.client.files.uploadFile(folderId, item, fileStream);
} else {
let uploader = await this.client.files.getChunkedUploader(folderId, size, item, fileStream);
await uploader.start();
}
} catch (ex) {
throw new BoxCLIError(`Could not upload file ${itemPath}`, ex);
}
}
/* eslint-enable no-await-in-loop */
}
return folderId;
}
}
FoldersUploadCommand.description = 'Upload a folder';
FoldersUploadCommand.examples = ['box folders:upload /path/to/folder'];
FoldersUploadCommand.flags = {
...BoxCommand.flags,
'folder-name': Flags.string({ description: 'Name to use for folder if not using local folder name' }),
'id-only': Flags.boolean({
description: 'Return only an ID to output from this command',
}),
'parent-folder': Flags.string({
char: 'p',
description: 'Folder to upload this folder into; defaults to the root folder',
default: '0',
})
};
FoldersUploadCommand.args = {
path: Args.string({
name: 'path',
required: true,
hidden: false,
description: 'Local path to the folder to upload',
})
};
module.exports = FoldersUploadCommand;