UNPKG

rocket-cli

Version:

Pure command line tool to replace Baidu('百毒') NetDisk or other NetDisk with this COOL shit!

87 lines (82 loc) 2.63 kB
var Promise = require('promise'); var db = require('../db/index'); var ora = require('ora'); var qiniuWrapper = require('../provider/Qiniu'); var moment = require('moment'); var util = require('../util'); var path = require('path'); var thunk = require('thunks')(); var thunkWorkers = require('thunk-workers'); var workshop; function excuteUploadTasks(item) { workshop(function () { var startTime = moment().format('lll'); var spinner = ora('[PUSH] ' + 'Start: ' + startTime + ' | ' + item.path + ' | ' + (item.meta.size / (1024 * 1024)).toFixed(2) + 'Mb').start(); return new Promise(function (resolve, reject) { qiniuWrapper.upload(path.join(item.query.dir, item.file), item.path) .then(function (ret) { item.cloud = { key: ret.key, hash: ret.hash, upload_at: moment().format('lll') }; spinner.succeed('[PUSH] ' + 'Start: ' + startTime + ' | ' + 'End: ' + moment().format('lll') + ' | ' + item.path + ' | ' + +(item.meta.size / (1024 * 1024)).toFixed(2) + 'Mb'); return resolve(item); }) .catch(function (err) { spinner.fail('[PUSH] ' + 'Start: ' + startTime + ' | ' + 'End: ' + moment().format('lll') + ' | ' + item.path + ' | ' + (item.meta.size / (1024 * 1024)).toFixed(2) + 'Mb'); return reject(err); }); }); })(function (err, retItem) { if (!err) { syncBack2Index(retItem); } }) } module.exports = { handle: function (options) { return new Promise(function (resolve, reject) { workshop = thunkWorkers(options.maxWorkers || 5); db.getInstance().find({ path: {$exists: true}, file: {$exists: true}, "cloud.hash": "", "cloud.key": "" }, function (err, docs) { if (docs && docs.length > 0) { qiniuWrapper.init() .then(function () { docs.forEach(function (item) { excuteUploadTasks(item); }); }) .catch(function (err) { return reject(err); }); } else { return reject('Noting needed to push! \n'); } }); }); } }; function syncBack2Index(item) { var spinner = ora('[UPDATE INDEX] ' + item.path + ' ').start(); db.getInstance().update({ _id: item._id }, { $set: { 'cloud.key': item.cloud.key, 'cloud.hash': item.cloud.hash, 'cloud.upload_at': item.cloud.upload_at } }, {}, function (err, numReplaced) { if (err) { util.logError(err); } }); spinner.succeed(); }