diff --git a/_config.yml b/_config.yml index 8fa50e8..2cca88d 100644 --- a/_config.yml +++ b/_config.yml @@ -80,12 +80,20 @@ theme: zendapi ## zendAPI项目当前的版本号 zapi_version: 0.0.1 - + cpp_generator: repo_url: "https://github.com/qcoreteam/zendapi.git" project_name: "zendapi" publish_dir: "apidocs/cpp" - +## 七牛配置 +qiniu: + cdnUrlPrefix: http://cdn.zendapi.org/ + bucket: zendapi + access_key: NWS5w6THL5HaA6P2mZrp1-D6lOYZw4hhYGB3Dgf4 + secret_key: i7z_ZvOU3RKTrxQO-_y4_icc09I3OTs0lyatl1ou + up_host: http://upload.qiniu.com + update_exist: true +staticDir: / # Deployment ## Docs: https://hexo.io/docs/deployment.html #deploy: diff --git a/package.json b/package.json index 02e3bfd..c56f57a 100644 --- a/package.json +++ b/package.json @@ -26,9 +26,11 @@ }, "devDependencies": { "clone": "^2.1.1", + "colors": "^1.1.2", "hexo-pagination": "^0.1.0", "node-sass": "^4.5.3", "nodegit": "^0.18.3", + "qiniu": "^6.1.13", "xml2js": "^0.4.19" } -} +} \ No newline at end of file diff --git a/scripts/console.js b/scripts/console.js new file mode 100644 index 0000000..64f604d --- /dev/null +++ b/scripts/console.js @@ -0,0 +1,18 @@ +/** + * Created by liushuai on 2017/11/7. + * + */ +hexo.extend.console.register('qndeploy', 'deploy project use qiniu', function (args) { + hexo.on('deployAfter', function(){ + hexo.call('qiniu',{}); + }); + hexo.config.staticDir = hexo.config.qiniu.cdnUrlPrefix; + hexo.call('deploy',{ + g: true + }); +}); + +hexo.extend.console.register('qnserver', 'deploy project use qiniu', function (args) { + hexo.config.staticDir = hexo.config.qiniu.cdnUrlPrefix; + hexo.call('server',{}); +}); \ No newline at end of file diff --git a/scripts/qiniu.js b/scripts/qiniu.js new file mode 100644 index 0000000..2912562 --- /dev/null +++ b/scripts/qiniu.js @@ -0,0 +1,198 @@ +/** + * Created by liushuai on 2017/11/2. + * + */ + +hexo.extend.console.register('qiniu', 'update static file to qiniu', function (args) { + var fs = require('fs'); + var path = require('path'); + var log = hexo.log; + global.colors = require('colors'); + var config = hexo.config.qiniu; + + var qiniu = require('qiniu'); + var local_dir = path.dirname(__dirname); + + var dirPrefix = 'public'; + + var need_upload_nums = 0; + + qiniu.conf.ACCESS_KEY = config.access_key; + qiniu.conf.SECRET_KEY = config.secret_key; + + if(config.up_host){ + qiniu.conf.UP_HOST = config.up_host; + } + + var bucket = config.bucket + + function getEtag(buffer,callback){ + + // 判断传入的参数是buffer还是stream还是filepath + var mode = 'buffer'; + + if(typeof buffer === 'string'){ + buffer = require('fs').createReadStream(buffer); + mode='stream'; + }else if(buffer instanceof require('stream')){ + mode='stream'; + } + + // sha1算法 + var sha1 = function(content){ + var crypto = require('crypto'); + var sha1 = crypto.createHash('sha1'); + sha1.update(content); + return sha1.digest(); + }; + + // 以4M为单位分割 + var blockSize = 4*1024*1024; + var sha1String = []; + var prefix = 0x16; + var blockCount = 0; + + switch(mode){ + case 'buffer': + var bufferSize = buffer.length; + blockCount = Math.ceil(bufferSize / blockSize); + + for(var i=0;i 1){ + prefix = 0x96; + sha1Buffer = sha1(sha1Buffer); + } + + sha1Buffer = Buffer.concat( + [new Buffer([prefix]),sha1Buffer], + sha1Buffer.length + 1 + ); + + return sha1Buffer.toString('base64') + .replace(/\//g,'_').replace(/\+/g,'-'); + + } + + } + +//构造上传函数 + function uploadFile(key, localFile) { + var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key); + var uptoken = putPolicy.token(); + var extra = new qiniu.io.PutExtra(); + log.i(bucket, key, localFile) + qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) { + if(!err) { + // 上传成功, 处理返回值 + //console.log(ret.hash, ret.key, ret.persistentId); + } else { + // 上传失败, 处理返回代码 + console.log(err); + } + }); + } + +//构建bucketmanager对象 + var client = new qiniu.rs.Client(); + + + + /** + * 上传前预先检查 + * file为本地路径(绝对路径或相对路径都可) + * name为远程文件名 + */ + var check_upload = function (file, name) { + //uploadFile(config.bucket, file.replace(/\\/g, '/'), name); + + //获取文件信息 + client.stat(config.bucket, name, function(err, ret) { + + if (!err) { + //console.log(ret.hash, ret.fsize, ret.putTime, ret.mimeType); + getEtag(file, function (hash) { + + if(hash != ret.hash){ + // 不更新已存在的,忽略 + if (!config.update_exist) { + log.i('Don\'t upload exist file: '.yellow + file); + return; + } + + need_upload_nums++; + log.i('Need upload update file: '.yellow + file); + uploadFile(name, file); + } else { + log.i('Don\'t upload unchange file: '.cyan + file); + } + + }); + + } else { + + // 文件不存在 + if(err.code == 612){ + uploadFile(name, file); + }else{ + log.e('get file stat err: '.cyan + name + '\n' + err); + } + } + }); + }; + + /** + * 遍历目录进行上传 + */ + var sync = function (dir) { + if (!dir) { + dir=''; + log.i('Now start qiniu sync.'.yellow); + return; + } + var files = fs.readdirSync(path.join(local_dir, dirPrefix, dir)); + files.forEach(function(file) { + var fname = path.join(local_dir, dirPrefix + '', dir + '', file + ''); + + var stat = fs.lstatSync(fname); + if(stat.isDirectory() == true) { + sync(path.join(dir + '', file + '')); + } else { + var name = path.join(dir, file).replace(/\\/g, '/').replace(/^\//g, ''); + check_upload(fname, name); + } + }) + }; + + sync('statics/css'); + sync('statics/fonts'); + sync('statics/images'); + sync('statics/js'); +}); \ No newline at end of file diff --git a/scripts/redering.js b/scripts/redering.js new file mode 100644 index 0000000..c8efdcb --- /dev/null +++ b/scripts/redering.js @@ -0,0 +1,40 @@ +/** + * Created by liushuai on 2017/10/28. + * + */ + +const minify = require('html-minifier').minify; +const UglifyJS = require('uglify-es'); +var CleanCSS = require('clean-css'); + +hexo.extend.filter.register('after_render:html', function(str, data){ + var timestamp = Date.parse(new Date()); + str = str.replace(//gi, ''); + str = str.replace(//gi, ''); + str = str.replace(/<\/script>/gi, ''); + return minify(str, { + enable: true, + exclude: [], + ignoreCustomComments: [/^\s*more/], + removeComments: true, + removeCommentsFromCDATA: true, + collapseWhitespace: true, + collapseBooleanAttributes: true, + removeEmptyAttributes: true, + minifyJS: true, + minifyCSS: true + }); +}); + +hexo.extend.filter.register('after_render:js', function(str, data){ + var result = UglifyJS.minify(str); + return result.code; +}); + +hexo.extend.filter.register('after_render:css', function (str, data) { + var result = new CleanCSS({ + enable: true, + exclude: ['*.min.css'] + }).minify(str); + return result.styles; +}); \ No newline at end of file diff --git a/themes/zendapi/layout/_partial/index/infolist.ejs b/themes/zendapi/layout/_partial/index/infolist.ejs index 8ea48d6..429041f 100644 --- a/themes/zendapi/layout/_partial/index/infolist.ejs +++ b/themes/zendapi/layout/_partial/index/infolist.ejs @@ -50,12 +50,12 @@ let manuallist = site.data.recommendmanuallist; 最新博文
    <% if (newslist.length > 0) { - for(let i in newslist) { - let item = newslist[i]; + for(let i in newslist) { + let item = newslist[i]; %> -
  • <%- item.title %>
  • +
  • <%- item.title %>
  • <% } - } else { %> + } else { %>
  • 暂无数据
  • <% } %>
diff --git a/themes/zendapi/scripts/redering.js b/themes/zendapi/scripts/redering.js deleted file mode 100644 index 43e8455..0000000 --- a/themes/zendapi/scripts/redering.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Created by liushuai on 2017/10/28. - * - */ -const minify = require('html-minifier').minify; -const UglifyJS = require('uglify-es'); -var CleanCSS = require('clean-css'); - -// hexo.extend.filter.register('after_render:html', function(str, data){ -// return minify(str, { -// enable: true, -// exclude: [], -// ignoreCustomComments: [/^\s*more/], -// removeComments: true, -// removeCommentsFromCDATA: true, -// collapseWhitespace: true, -// collapseBooleanAttributes: true, -// removeEmptyAttributes: true, -// minifyJS: true, -// minifyCSS: true -// }); -// }); - -hexo.extend.filter.register('after_render:js', function(str, data){ - var result = UglifyJS.minify(str); - return result.code; -}); - -hexo.extend.filter.register('after_render:css', function (str, data) { - var result = new CleanCSS({ - enable: true, - exclude: ['*.min.css'] - }).minify(str); - return result.styles; -}); \ No newline at end of file