-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
106 lines (90 loc) · 3.19 KB
/
index.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
104
"use strict";
var path = require('path')
var fs = require('fs')
var crypto = require('crypto')
var util = require('util')
var through2 = require('through2')
var gulpUtil = require('gulp-util')
var QN = require('qn');
var q = require('q')
var Moment = require('moment')
var colors = gulpUtil.colors
var log = gulpUtil.log
var PluginError = gulpUtil.PluginError
var uploadedFiles = 0
var totalFiles = 0
var filesUploaded = {} // 记录所有成功上传的文件。 {filePath:fileKey, ...}
module.exports = function(config, options){
var options = options || {}
var options = extend( {dir: '', versioning: false, recordInFile: null}, options );
var recordFile = options.recordInFile || null; //以json形式在本地文件记录上传内容
var qn = QN.create(config)
var version = Moment().format('YYMMDDHHmm')
return through2.obj(function(file, enc, next){
var that = this;
var filePath = path.relative(file.base, file.path);
if (file._contents === null) return next();
var fileKey = options.dir + ((!options.dir || options.dir[options.dir.length - 1]) === '/' ? '' : '/') + (options.versioning ? version + '/' : '') + filePath;
var fileHash = calcHash(file);
//以同步的方式处理 文件上传
q.nbind(qn.stat, qn)(fileKey).spread(function(stat){
if (stat.hash === fileHash) return false;
return q.nbind(qn.delete, qn)(fileKey)
}, function(){ return true;})
.then(function(isUpload){
totalFiles ++
if (isUpload === false) return false
return q.nbind(qn.upload, qn)(file._contents, {key: fileKey});
})
.then(function (stat){
if( stat === false ){
log('Skip:', colors.grey(filePath));
return
}
uploadedFiles ++
filesUploaded[filePath] = fileKey;
log('Upload:', colors.green(filePath), '→', colors.green(fileKey));
}, function(err){
log('Error', colors.red(filePath));
})
.then(function(){
next();
})
}, function(){ /* flushFunction */
log('Total:', colors.green(uploadedFiles + '/' + totalFiles));
// Check if versioning
if (!options.versioning) return;
log('Version:', colors.green(version));
if ( recordFile ) {
fs.writeFileSync(recordFile, JSON.stringify(filesUploaded))
log('Write version file:', colors.green(recordFile));
}
});
}
function extend(target, source) {
target = target || {};
for (var prop in source) {
if (typeof source[prop] === 'object') {
target[prop] = extend(target[prop], source[prop]);
} else {
target[prop] = source[prop];
}
}
return target;
}
/**
* Calc qiniu etag
*
* @param file
* @returns {*}
*/
function calcHash(file) {
if (file.size > 1 << 22) return false;
var shasum = crypto.createHash('sha1');
shasum.update(file._contents);
var sha1 = shasum.digest();
var hash = new Buffer(1 + sha1.length);
hash[0] = 0x16;
sha1.copy(hash, 1);
return hash.toString('base64').replace('+', '-').replace('/', '_');
}