From 1cabb14bcc5b7329ca29abf10e5b9c0a9df7447f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 12 Apr 2017 15:58:11 +0300 Subject: [PATCH] feature(upload-file) add wraptile --- client/dom/upload-files.js | 76 +++++++++++++++++++------------------- package.json | 1 + 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/client/dom/upload-files.js b/client/dom/upload-files.js index affa2449..9c003b3d 100644 --- a/client/dom/upload-files.js +++ b/client/dom/upload-files.js @@ -2,7 +2,8 @@ /* global CloudCmd */ -const exec = require('execon'); +const {eachSeries} = require('execon'); +const wraptile = require('wraptile/legacy'); const DOM = require('.'); const load = require('./load'); @@ -11,9 +12,10 @@ const Images = require('./images'); const {FS} = require('../../common/cloudfunc'); const {CurrentInfo} = DOM; +const onEnd = wraptile(_onEnd); +const loadFile = wraptile(_loadFile); + module.exports = (dir, files) => { - let i = 0; - if (!files) { files = dir; dir = CurrentInfo.dirPath; @@ -27,39 +29,39 @@ module.exports = (dir, files) => { const array = [...files]; const {name} = files[0]; - exec.eachSeries(array, loadFile, func(name)); - - function func(name) { - return () => { - CloudCmd.refresh(null, () => { - DOM.setCurrentByName(name); - }); - }; - } - - function loadFile(file, callback) { - const name = file.name; - const path = dir + name; - const {PREFIX_URL} = CloudCmd; - const api = PREFIX_URL + FS; - - const percent = (i, n, per = 100) => { - return Math.round(i * per / n); - }; - - const step = (n) => 100 / n; - - ++i; - - load.put(api + path, file) - .on('end', callback) - .on('progress', (count) => { - const max = step(n); - const value = (i - 1) * max + percent(count, 100, max); - - Images.show.load('top'); - Images.setProgress(Math.round(value)); - }); - } + eachSeries(array, loadFile(dir, n), onEnd(name)); }; +function _onEnd(name) { + CloudCmd.refresh(null, () => { + DOM.setCurrentByName(name); + }); +} + +function _loadFile(dir, n, file, callback) { + let i = 0; + + const name = file.name; + const path = dir + name; + const {PREFIX_URL} = CloudCmd; + const api = PREFIX_URL + FS; + + const percent = (i, n, per = 100) => { + return Math.round(i * per / n); + }; + + const step = (n) => 100 / n; + + ++i; + + load.put(api + path, file) + .on('end', callback) + .on('progress', (count) => { + const max = step(n); + const value = (i - 1) * max + percent(count, 100, max); + + Images.show.load('top'); + Images.setProgress(Math.round(value)); + }); +} + diff --git a/package.json b/package.json index f3c3e55e..4bed6b60 100644 --- a/package.json +++ b/package.json @@ -189,6 +189,7 @@ "tar-stream": "^1.5.2", "version-io": "^1.2.5", "webpack": "^2.2.1", + "wraptile": "^1.0.0", "yaspeller": "^3.0.0" }, "engines": {