d-drive-cli 1.1.3 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/upload.js +39 -8
- package/package.json +1 -1
- package/src/commands/upload.ts +39 -8
package/dist/commands/upload.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.uploadCommand = uploadCommand;
|
|
7
7
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const stream_1 = require("stream");
|
|
9
10
|
const chalk_1 = __importDefault(require("chalk"));
|
|
10
11
|
const ora_1 = __importDefault(require("ora"));
|
|
11
12
|
const form_data_1 = __importDefault(require("form-data"));
|
|
@@ -61,7 +62,15 @@ async function uploadSingleFile(api, filePath, destination, showProgress) {
|
|
|
61
62
|
console.log(chalk_1.default.cyan(`\nUploading: ${fileName}`));
|
|
62
63
|
console.log(chalk_1.default.gray(`Size: ${formatFileSize(fileSize)}`));
|
|
63
64
|
const formData = new form_data_1.default();
|
|
64
|
-
|
|
65
|
+
// Create a passthrough so we can monitor bytes read from disk
|
|
66
|
+
const fileStream = fs_extra_1.default.createReadStream(filePath);
|
|
67
|
+
const pass = new stream_1.PassThrough();
|
|
68
|
+
// Pipe file stream into pass-through which is appended to form-data
|
|
69
|
+
fileStream.pipe(pass);
|
|
70
|
+
formData.append('file', pass, {
|
|
71
|
+
filename: fileName,
|
|
72
|
+
knownLength: fileSize,
|
|
73
|
+
});
|
|
65
74
|
formData.append('path', destination);
|
|
66
75
|
// Ensure CLI uploads follow frontend behavior and request server-side encryption by default
|
|
67
76
|
formData.append('encrypt', 'true');
|
|
@@ -71,16 +80,38 @@ async function uploadSingleFile(api, filePath, destination, showProgress) {
|
|
|
71
80
|
complete: '█',
|
|
72
81
|
incomplete: '░',
|
|
73
82
|
width: 40,
|
|
74
|
-
total:
|
|
83
|
+
total: 1,
|
|
84
|
+
});
|
|
85
|
+
// Track bytes read from disk and update progress bar as fraction [0..1]
|
|
86
|
+
let uploaded = 0;
|
|
87
|
+
fileStream.on('data', (chunk) => {
|
|
88
|
+
const len = typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
|
|
89
|
+
uploaded += len;
|
|
90
|
+
if (progressBar) {
|
|
91
|
+
const ratio = Math.min(1, uploaded / fileSize);
|
|
92
|
+
progressBar.update(ratio);
|
|
93
|
+
}
|
|
75
94
|
});
|
|
76
95
|
}
|
|
96
|
+
// Ensure Content-Length is set for axios in Node
|
|
97
|
+
const headers = formData.getHeaders();
|
|
98
|
+
try {
|
|
99
|
+
const length = await new Promise((resolve, reject) => {
|
|
100
|
+
formData.getLength((err, len) => {
|
|
101
|
+
if (err)
|
|
102
|
+
return reject(err);
|
|
103
|
+
resolve(len);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
headers['Content-Length'] = String(length);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
// ignore length error
|
|
110
|
+
}
|
|
77
111
|
await api.post('/files/upload', formData, {
|
|
78
|
-
headers
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
progressBar.update(progressEvent.loaded / fileSize);
|
|
82
|
-
}
|
|
83
|
-
},
|
|
112
|
+
headers,
|
|
113
|
+
maxContentLength: Infinity,
|
|
114
|
+
maxBodyLength: Infinity,
|
|
84
115
|
});
|
|
85
116
|
}
|
|
86
117
|
function formatFileSize(bytes) {
|
package/package.json
CHANGED
package/src/commands/upload.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
2
|
import path from 'path';
|
|
3
|
+
import { PassThrough } from 'stream';
|
|
3
4
|
import chalk from 'chalk';
|
|
4
5
|
import ora from 'ora';
|
|
5
6
|
import FormData from 'form-data';
|
|
@@ -85,7 +86,15 @@ async function uploadSingleFile(
|
|
|
85
86
|
console.log(chalk.gray(`Size: ${formatFileSize(fileSize)}`));
|
|
86
87
|
|
|
87
88
|
const formData = new FormData();
|
|
88
|
-
|
|
89
|
+
// Create a passthrough so we can monitor bytes read from disk
|
|
90
|
+
const fileStream = fs.createReadStream(filePath);
|
|
91
|
+
const pass = new PassThrough();
|
|
92
|
+
// Pipe file stream into pass-through which is appended to form-data
|
|
93
|
+
fileStream.pipe(pass);
|
|
94
|
+
formData.append('file', pass, {
|
|
95
|
+
filename: fileName,
|
|
96
|
+
knownLength: fileSize,
|
|
97
|
+
});
|
|
89
98
|
formData.append('path', destination);
|
|
90
99
|
// Ensure CLI uploads follow frontend behavior and request server-side encryption by default
|
|
91
100
|
formData.append('encrypt', 'true');
|
|
@@ -97,17 +106,39 @@ async function uploadSingleFile(
|
|
|
97
106
|
complete: '█',
|
|
98
107
|
incomplete: '░',
|
|
99
108
|
width: 40,
|
|
100
|
-
total:
|
|
109
|
+
total: 1,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Track bytes read from disk and update progress bar as fraction [0..1]
|
|
113
|
+
let uploaded = 0;
|
|
114
|
+
fileStream.on('data', (chunk: Buffer | string) => {
|
|
115
|
+
const len = typeof chunk === 'string' ? Buffer.byteLength(chunk) : chunk.length;
|
|
116
|
+
uploaded += len;
|
|
117
|
+
if (progressBar) {
|
|
118
|
+
const ratio = Math.min(1, uploaded / fileSize);
|
|
119
|
+
progressBar.update(ratio);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Ensure Content-Length is set for axios in Node
|
|
125
|
+
const headers = formData.getHeaders();
|
|
126
|
+
try {
|
|
127
|
+
const length = await new Promise<number>((resolve, reject) => {
|
|
128
|
+
formData.getLength((err: any, len: number) => {
|
|
129
|
+
if (err) return reject(err);
|
|
130
|
+
resolve(len);
|
|
131
|
+
});
|
|
101
132
|
});
|
|
133
|
+
headers['Content-Length'] = String(length);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
// ignore length error
|
|
102
136
|
}
|
|
103
137
|
|
|
104
138
|
await api.post('/files/upload', formData, {
|
|
105
|
-
headers
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
progressBar.update(progressEvent.loaded / fileSize);
|
|
109
|
-
}
|
|
110
|
-
},
|
|
139
|
+
headers,
|
|
140
|
+
maxContentLength: Infinity,
|
|
141
|
+
maxBodyLength: Infinity,
|
|
111
142
|
});
|
|
112
143
|
}
|
|
113
144
|
|