-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathupload.js
103 lines (92 loc) · 3.12 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
93
94
95
96
97
98
99
100
101
102
103
/* 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 progress = require('cli-progress');
const BoxCLIError = require('../../cli-error');
const CHUNKED_UPLOAD_FILE_SIZE = 1024 * 1024 * 100; // 100 MiB
class FilesUploadCommand extends BoxCommand {
async run() {
const { flags, args } = await this.parse(FilesUploadCommand);
let size = fs.statSync(args.path).size;
let folderID = flags['parent-id'];
let stream;
try {
stream = fs.createReadStream(args.path);
} catch (ex) {
throw new BoxCLIError(`Could not open file ${args.path}`, ex);
}
let fileAttributes = {};
let name;
if (flags.name) {
name = flags.name;
} else {
name = path.basename(args.path);
}
if (flags['content-created-at']) {
fileAttributes.content_created_at = flags['content-created-at'];
}
if (flags['content-modified-at']) {
fileAttributes.content_modified_at = flags['content-modified-at'];
}
// @TODO(2018-08-24): Consider adding --preserve-timestamps flag
let file;
if (size < CHUNKED_UPLOAD_FILE_SIZE) {
file = await this.client.files.uploadFile(folderID, name, stream, fileAttributes);
} else {
let progressBar = new progress.Bar({
format: '[{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total} | Speed: {speed} MB/s',
stopOnComplete: true,
});
let uploader = await this.client.files.getChunkedUploader(folderID, size, name, stream, { fileAttributes });
let bytesUploaded = 0;
let startTime = Date.now();
progressBar.start(size, 0, { speed: 'N/A' });
uploader.on('chunkUploaded', chunk => {
bytesUploaded += chunk.part.size;
progressBar.update(bytesUploaded, {
speed: Math.floor(bytesUploaded / (Date.now() - startTime) / 1000),
});
});
file = await uploader.start();
}
await this.output(file.entries[0]);
}
}
FilesUploadCommand.description = 'Upload a file';
FilesUploadCommand.examples = ['box files:upload /path/to/file.pdf --parent-id 22222'];
FilesUploadCommand._endpoint = 'post_files_content';
FilesUploadCommand.flags = {
...BoxCommand.flags,
'parent-id': Flags.string({
char: 'p',
description: 'ID of the parent folder to upload the file to; defaults to the root folder',
default: '0'
}),
name: Flags.string({
char: 'n',
description: 'Provide different name for uploaded file'
}),
'content-created-at': Flags.string({
description: 'The creation date of the file content. Use a timestamp or shorthand syntax 0t, like 5w for 5 weeks',
parse: input => BoxCommand.normalizeDateString(input),
}),
'content-modified-at': Flags.string({
description: 'The modification date of the file content. Use a timestamp or shorthand syntax 0t, like 5w for 5 weeks',
parse: input => BoxCommand.normalizeDateString(input),
}),
'id-only': Flags.boolean({
description: 'Return only an ID to output from this command',
}),
};
FilesUploadCommand.args = {
path: Args.string({
name: 'path',
required: true,
hidden: false,
description: 'Path to the file to be uploaded',
}),
};
module.exports = FilesUploadCommand;