mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
/* global CloudCmd */
|
|
const philip = require('philip');
|
|
|
|
const Images = require('./images');
|
|
const {FS} = require('../../common/cloudfunc.mjs');
|
|
const DOM = require('.');
|
|
const Dialog = require('./dialog');
|
|
|
|
const {getCurrentDirPath: getPathWhenRootEmpty} = DOM;
|
|
|
|
module.exports = (items) => {
|
|
if (items.length)
|
|
Images.show('top');
|
|
|
|
const entries = Array
|
|
.from(items)
|
|
.map((item) => item.webkitGetAsEntry());
|
|
|
|
const dirPath = getPathWhenRootEmpty();
|
|
const path = dirPath.replace(/\/$/, '');
|
|
|
|
const progress = Dialog.progress('Uploading...');
|
|
|
|
progress.catch(() => {
|
|
Dialog.alert('Upload aborted');
|
|
uploader.abort();
|
|
});
|
|
|
|
const uploader = philip(entries, (type, name, data, i, n, callback) => {
|
|
const {prefixURL} = CloudCmd;
|
|
const full = prefixURL + FS + path + name;
|
|
|
|
let upload;
|
|
switch(type) {
|
|
case 'file':
|
|
upload = uploadFile(full, data);
|
|
break;
|
|
|
|
case 'directory':
|
|
upload = uploadDir(full);
|
|
break;
|
|
}
|
|
|
|
upload.on('end', callback);
|
|
|
|
upload.on('progress', (count) => {
|
|
const current = percent(i, n);
|
|
const next = percent(i + 1, n);
|
|
const max = next - current;
|
|
const value = current + percent(count, 100, max);
|
|
|
|
progress.setProgress(value);
|
|
});
|
|
});
|
|
|
|
uploader.on('error', (error) => {
|
|
Dialog.alert(error);
|
|
uploader.abort();
|
|
});
|
|
|
|
uploader.on('end', CloudCmd.refresh);
|
|
};
|
|
|
|
const percent = (i, n, per = 100) => Math.round(i * per / n);
|
|
|
|
const uploadFile = (url, data) => DOM.load.put(url, data);
|
|
|
|
const uploadDir = (url) => DOM.load.put(`${url}?dir`);
|