├── .gitignore ├── README.md ├── package.json ├── LICENSE ├── qetag.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .idea/ 3 | gulpfile.js 4 | tests/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [gulp](http://gulpjs.com)-[qiniu](http://qiniu.com) 2 | 3 | > 上传静态资源到七牛 CDN 4 | 5 | ## Install 6 | 7 | ``` 8 | npm install gulp-qiniu --save-dev 9 | ``` 10 | 11 | ## Usage 12 | 13 | 实例代码: 14 | 15 | ```js 16 | gulp.src('./public/**') 17 | .pipe(qiniu({ 18 | accessKey: "xxxx", 19 | secretKey: "xxxx", 20 | bucket: "bucket", 21 | private: false 22 | }, { 23 | dir: 'assets/', 24 | versioning: true, 25 | versionFile: './cdn.json', 26 | concurrent: 10 27 | })) 28 | ``` 29 | 30 | ## License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-qiniu", 3 | "version": "0.2.4", 4 | "description": "Gulp task for deploying files to qiniu CDN", 5 | "author": "hfcorriez ", 6 | "licenses": [ 7 | { 8 | "type": "MIT", 9 | "url": "https://github.com/hfcorriez/gulp-qiniu/blob/master/LICENSE" 10 | } 11 | ], 12 | "keywords": [ 13 | "gulpplugin", 14 | "gulp", 15 | "qiniu", 16 | "cdn" 17 | ], 18 | "homepage": "http://github.com/hfcorriez/gulp-qiniu", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/hfcorriez/gulp-qiniu.git" 22 | }, 23 | "engines": { 24 | "node": "*" 25 | }, 26 | "dependencies": { 27 | "gulp-util": "~2.2.9", 28 | "minimatch": "^2.0.4", 29 | "moment": "~2.7.0", 30 | "q": "~1.0.1", 31 | "qn": "^1.1.1", 32 | "through2": "~0.4.0" 33 | }, 34 | "devDependencies": {} 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Corrie Zhao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /qetag.js: -------------------------------------------------------------------------------- 1 | // 计算文件的eTag,参数为buffer或者readableStream或者文件路径 2 | function getEtag(buffer, callback) { 3 | 4 | // 判断传入的参数是buffer还是stream还是filepath 5 | var mode = 'buffer'; 6 | 7 | if (typeof buffer === 'string') { 8 | buffer = require('fs').createReadStream(buffer); 9 | mode = 'stream'; 10 | } else if (buffer instanceof require('stream')) { 11 | mode = 'stream'; 12 | } 13 | 14 | // sha1算法 15 | var sha1 = function (content) { 16 | var crypto = require('crypto'); 17 | var sha1 = crypto.createHash('sha1'); 18 | sha1.update(content); 19 | return sha1.digest(); 20 | }; 21 | 22 | // 以4M为单位分割 23 | var blockSize = 4 * 1024 * 1024; 24 | var sha1String = []; 25 | var prefix = 0x16; 26 | var blockCount = 0; 27 | 28 | switch (mode) { 29 | case 'buffer': 30 | var bufferSize = buffer.length; 31 | blockCount = Math.ceil(bufferSize / blockSize); 32 | 33 | for (var i = 0; i < blockCount; i++) { 34 | sha1String.push(sha1(buffer.slice(i * blockSize, (i + 1) * blockSize))); 35 | } 36 | process.nextTick(function () { 37 | callback(null, calcEtag()); 38 | }); 39 | break; 40 | case 'stream': 41 | var stream = buffer; 42 | stream.on('readable', function () { 43 | var chunk; 44 | while (chunk = stream.read(blockSize)) { 45 | sha1String.push(sha1(chunk)); 46 | blockCount++; 47 | } 48 | }); 49 | stream.on('end', function () { 50 | callback(null, calcEtag()); 51 | }); 52 | break; 53 | } 54 | 55 | function calcEtag() { 56 | if (!sha1String.length) { 57 | return 'Fto5o-5ea0sNMlW_75VgGJCv2AcJ'; 58 | } 59 | var sha1Buffer = Buffer.concat(sha1String, blockCount * 20); 60 | 61 | // 如果大于4M,则对各个块的sha1结果再次sha1 62 | if (blockCount > 1) { 63 | prefix = 0x96; 64 | sha1Buffer = sha1(sha1Buffer); 65 | } 66 | 67 | sha1Buffer = Buffer.concat( 68 | [new Buffer([prefix]), sha1Buffer], 69 | sha1Buffer.length + 1 70 | ); 71 | 72 | return sha1Buffer.toString('base64') 73 | .replace(/\//g, '_').replace(/\+/g, '-'); 74 | 75 | } 76 | 77 | } 78 | 79 | module.exports = getEtag; 80 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var through2 = require('through2'); 3 | var PluginError = require('gulp-util').PluginError; 4 | var colors = require('gulp-util').colors; 5 | var log = require('gulp-util').log; 6 | var QN = require('qn'); 7 | var Moment = require('moment'); 8 | var Q = require('q'); 9 | var fs = require('fs') 10 | var crypto = require('crypto') 11 | var minimatch = require('minimatch') 12 | var uploadedFiles = 0; 13 | var getEtag = require('./qetag') 14 | 15 | module.exports = function (qiniu, option) { 16 | option = option || {}; 17 | option = extend({dir: '', versioning: false, versionFile: null, ignore: [], concurrent: 10}, option); 18 | 19 | var qn = QN.create(qiniu) 20 | , version = Moment().format('YYMMDDHHmm') 21 | , qs = [] 22 | , filesIndex = 0 23 | 24 | return through2.obj(function (file, enc, next) { 25 | var that = this; 26 | var isIgnore = false; 27 | var filePath = path.relative(file.base, file.path); 28 | filePath = filePath.split(path.sep).join('/'); 29 | 30 | if (file._contents === null) return next(); 31 | option.ignore.forEach(function (item) { 32 | if (minimatch(filePath, item)) isIgnore = true; 33 | }) 34 | if (isIgnore) return next(); 35 | 36 | filesIndex++ 37 | 38 | var fileKey = option.dir + ((!option.dir || option.dir[option.dir.length - 1]) === '/' ? '' : '/') + (option.versioning ? version + '/' : '') + filePath; 39 | var retries = 0; 40 | var isConcurrent = filesIndex % Math.floor(option.concurrent) !== 0 41 | 42 | var handler = function () { 43 | return Q.nbind(qn.stat, qn)(fileKey) 44 | .spread(function (stat) { 45 | return Q.nfcall(getEtag, file._contents) 46 | .then(function (fileHash) { 47 | // Skip when hash equal 48 | if (stat.hash === fileHash) return false; 49 | 50 | // Start 51 | log('Start →', fileKey); 52 | 53 | // Then delete 54 | return Q.nbind(qn.delete, qn)(fileKey) 55 | }) 56 | }, function () { 57 | // Start 58 | log('Start →', fileKey); 59 | 60 | // Upload when not exists 61 | return true; 62 | }) 63 | .then(function (isUpload) { 64 | if (isUpload === false) return false; 65 | return Q.nbind(qn.upload, qn)(file._contents, {key: fileKey}) 66 | }) 67 | .then(function (stat) { 68 | // No upload 69 | if (stat === false) { 70 | log('Skip →', colors.grey(fileKey)); 71 | !isConcurrent && next() 72 | return; 73 | } 74 | 75 | // Record hash 76 | uploadedFiles++; 77 | 78 | log('Upload →', colors.green((qiniu.origin ? qiniu.origin + '/' : '') + fileKey)); 79 | !isConcurrent && next() 80 | }, function (err) { 81 | log('Error →', colors.red(fileKey), new PluginError('gulp-qiniu', err).message); 82 | that.emit('Error', colors.red(fileKey), new PluginError('gulp-qiniu', err)); 83 | 84 | if (retries++ < 3) { 85 | log('Retry(' + retries + ') →', colors.red(fileKey)); 86 | return handler() 87 | } else { 88 | !isConcurrent && next() 89 | } 90 | }) 91 | } 92 | 93 | qs.push(handler()) 94 | 95 | isConcurrent && next() 96 | }, function () { 97 | Q.all(qs) 98 | .then(function (rets) { 99 | log('Total →', colors.green(uploadedFiles + '/' + rets.length)); 100 | 101 | // Check if versioning 102 | if (!option.versioning) return; 103 | log('Version →', colors.green(version)); 104 | 105 | if (option.versionFile) { 106 | fs.writeFileSync(option.versionFile, JSON.stringify({version: version})) 107 | log('Write version file →', colors.green(option.versionFile)); 108 | } 109 | }, function (err) { 110 | log('Failed upload →', err.message); 111 | }); 112 | }); 113 | 114 | function extend(target, source) { 115 | target = target || {}; 116 | for (var prop in source) { 117 | if (typeof source[prop] === 'object') { 118 | target[prop] = extend(target[prop], source[prop]); 119 | } else { 120 | target[prop] = source[prop]; 121 | } 122 | } 123 | return target; 124 | } 125 | }; 126 | --------------------------------------------------------------------------------