From f6763cc851ca36688b0af86cea813f3e0a5e8b4d Mon Sep 17 00:00:00 2001 From: liushuai Date: Tue, 7 Nov 2017 00:50:10 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=83=E7=89=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _config.yml | 12 +- package.json | 3 +- scripts/qiniu.js | 198 ++++++++++++++++++ scripts/redering.js | 40 ++++ themes/zendapi/layout/_partial/head.ejs | 10 +- .../layout/_partial/index/infolist.ejs | 4 +- .../layout/_partial/index/sponsors.ejs | 2 +- 7 files changed, 258 insertions(+), 11 deletions(-) create mode 100644 scripts/qiniu.js create mode 100644 scripts/redering.js diff --git a/_config.yml b/_config.yml index 8fa50e8..ebbaaaa 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://oyp0xh70t.bkt.clouddn.com/ + 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..64b5f60 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "private": true, "hexo": { - "version": "3.3.7" + "version": "3.3.9" }, "dependencies": { "clean-css": "^4.1.9", @@ -19,6 +19,7 @@ "hexo-toc": "^1.0.8", "html-minifier": "^3.5.6", "markdown-it": "^8.3.1", + "qiniu": "^7.1.1", "sax": "^1.2.4", "uglify-es": "^3.1.5", "mkdir-recursive": "^0.3.0", 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..1734a1b --- /dev/null +++ b/scripts/redering.js @@ -0,0 +1,40 @@ +/** + * Created by liushuai on 2017/10/28. + * + */ + +var cdnUrlPrefix = '/statics/'; +var args = process.argv; +var hasQn = args.indexOf('qiniu'); +if (hasQn > -1) { + hexo.config.staticDir = hexo.config.qiniu.cdnUrlPrefix; + cdnUrlPrefix=hexo.config.qiniu.cdnUrlPrefix; +} +function qiniucss(css) { + var ret = ''; + for (var i=0; i'; + } + return ret; +} +function qiniujs(js) { + var ret = ''; + for (var i=0; i'; + } + return ret; +} +function qiniuimg(img, className = '', style='') { + var ret = ''; + if (className != '') { + className = 'class="' + className + '"'; + } + var url = cdnUrlPrefix + img + "?" + Date.parse(new Date()); + ret += ''; + return ret; +} +hexo.extend.helper.register('qiniucss', qiniucss); +hexo.extend.helper.register('qiniujs', qiniujs); +hexo.extend.helper.register('qiniuimg', qiniuimg); diff --git a/themes/zendapi/layout/_partial/head.ejs b/themes/zendapi/layout/_partial/head.ejs index e2e3869..3ad6a9d 100644 --- a/themes/zendapi/layout/_partial/head.ejs +++ b/themes/zendapi/layout/_partial/head.ejs @@ -73,10 +73,10 @@ if (layout == "post") { %> <%}%> - - - +<%- qiniujs('statics/js/jquery/jquery-3.2.1.min.js') %> +<%- qiniujs('statics/js/uikit/js/uikit.min.js') %> +<%- qiniujs('statics/js/uikit/js/uikit-icons.min.js') %> <% if (layout == "manual" || layout == "post") { %> - - +<%- qiniujs('statics/js/highlight/highlight.pack.js') %> +<%- qiniucss('statics/js/highlight/styles/railscasts.css') %> <% } %> diff --git a/themes/zendapi/layout/_partial/index/infolist.ejs b/themes/zendapi/layout/_partial/index/infolist.ejs index 8ea48d6..caa0345 100644 --- a/themes/zendapi/layout/_partial/index/infolist.ejs +++ b/themes/zendapi/layout/_partial/index/infolist.ejs @@ -30,7 +30,7 @@ let manuallist = site.data.recommendmanuallist;
- 推荐阅读
+ <%- qiniuimg("statics/images/index/infolist/manual.svg", 'title-icon') %>推荐阅读
    <% if (manuallist.length > 0) { for(let i in manuallist) { @@ -47,7 +47,7 @@ let manuallist = site.data.recommendmanuallist;
    - 最新博文
    + <%- qiniuimg("statics/images/index/infolist/news.svg", 'title-icon') %>最新博文
      <% if (newslist.length > 0) { for(let i in newslist) { diff --git a/themes/zendapi/layout/_partial/index/sponsors.ejs b/themes/zendapi/layout/_partial/index/sponsors.ejs index 25a173f..77b66d7 100644 --- a/themes/zendapi/layout/_partial/index/sponsors.ejs +++ b/themes/zendapi/layout/_partial/index/sponsors.ejs @@ -1,7 +1,7 @@
      From 17a82f7670d25d828878a8527460ade1cb61d655 Mon Sep 17 00:00:00 2001 From: liushuai Date: Tue, 7 Nov 2017 23:21:46 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=83=E7=89=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/console.js | 19 +++++ scripts/redering.js | 70 +++++++++---------- themes/zendapi/layout/_partial/head.ejs | 10 +-- .../layout/_partial/index/infolist.ejs | 12 ++-- .../layout/_partial/index/sponsors.ejs | 2 +- 5 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 scripts/console.js diff --git a/scripts/console.js b/scripts/console.js new file mode 100644 index 0000000..29e549a --- /dev/null +++ b/scripts/console.js @@ -0,0 +1,19 @@ +/** + * Created by liushuai on 2017/11/7. + * + */ +hexo.extend.console.register('qndeploy', 'deploy project use qiniu', function (args) { + hexo.on('deployAfter', function(){ + console.log('aaaaaaaaaaaaaa啊啊发生地方阿斯顿发'); + 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/redering.js b/scripts/redering.js index 1734a1b..65b548b 100644 --- a/scripts/redering.js +++ b/scripts/redering.js @@ -3,38 +3,38 @@ * */ -var cdnUrlPrefix = '/statics/'; -var args = process.argv; -var hasQn = args.indexOf('qiniu'); -if (hasQn > -1) { - hexo.config.staticDir = hexo.config.qiniu.cdnUrlPrefix; - cdnUrlPrefix=hexo.config.qiniu.cdnUrlPrefix; -} -function qiniucss(css) { - var ret = ''; - for (var i=0; i'; - } - return ret; -} -function qiniujs(js) { - var ret = ''; - for (var i=0; i'; - } - return ret; -} -function qiniuimg(img, className = '', style='') { - var ret = ''; - if (className != '') { - className = 'class="' + className + '"'; - } - var url = cdnUrlPrefix + img + "?" + Date.parse(new Date()); - ret += ''; - return ret; -} -hexo.extend.helper.register('qiniucss', qiniucss); -hexo.extend.helper.register('qiniujs', qiniujs); -hexo.extend.helper.register('qiniuimg', qiniuimg); +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/head.ejs b/themes/zendapi/layout/_partial/head.ejs index 3ad6a9d..e2e3869 100644 --- a/themes/zendapi/layout/_partial/head.ejs +++ b/themes/zendapi/layout/_partial/head.ejs @@ -73,10 +73,10 @@ if (layout == "post") { %> <%}%> -<%- qiniujs('statics/js/jquery/jquery-3.2.1.min.js') %> -<%- qiniujs('statics/js/uikit/js/uikit.min.js') %> -<%- qiniujs('statics/js/uikit/js/uikit-icons.min.js') %> + + + <% if (layout == "manual" || layout == "post") { %> -<%- qiniujs('statics/js/highlight/highlight.pack.js') %> -<%- qiniucss('statics/js/highlight/styles/railscasts.css') %> + + <% } %> diff --git a/themes/zendapi/layout/_partial/index/infolist.ejs b/themes/zendapi/layout/_partial/index/infolist.ejs index caa0345..429041f 100644 --- a/themes/zendapi/layout/_partial/index/infolist.ejs +++ b/themes/zendapi/layout/_partial/index/infolist.ejs @@ -30,7 +30,7 @@ let manuallist = site.data.recommendmanuallist;
      - <%- qiniuimg("statics/images/index/infolist/manual.svg", 'title-icon') %>推荐阅读
      + 推荐阅读
        <% if (manuallist.length > 0) { for(let i in manuallist) { @@ -47,15 +47,15 @@ let manuallist = site.data.recommendmanuallist;
        - <%- qiniuimg("statics/images/index/infolist/news.svg", 'title-icon') %>最新博文
        + 最新博文
          <% 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/layout/_partial/index/sponsors.ejs b/themes/zendapi/layout/_partial/index/sponsors.ejs index 77b66d7..25a173f 100644 --- a/themes/zendapi/layout/_partial/index/sponsors.ejs +++ b/themes/zendapi/layout/_partial/index/sponsors.ejs @@ -1,7 +1,7 @@
        From 7008af4a0527d7d0799ad250a822cbdefb91dffc Mon Sep 17 00:00:00 2001 From: liushuai Date: Wed, 8 Nov 2017 10:55:36 +0800 Subject: [PATCH 3/4] qiniu cdn --- _config.yml | 2 +- package.json | 5 +++-- scripts/console.js | 1 - scripts/redering.js | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/_config.yml b/_config.yml index ebbaaaa..2cca88d 100644 --- a/_config.yml +++ b/_config.yml @@ -87,7 +87,7 @@ cpp_generator: publish_dir: "apidocs/cpp" ## 七牛配置 qiniu: - cdnUrlPrefix: http://oyp0xh70t.bkt.clouddn.com/ + cdnUrlPrefix: http://cdn.zendapi.org/ bucket: zendapi access_key: NWS5w6THL5HaA6P2mZrp1-D6lOYZw4hhYGB3Dgf4 secret_key: i7z_ZvOU3RKTrxQO-_y4_icc09I3OTs0lyatl1ou diff --git a/package.json b/package.json index 64b5f60..476ec40 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "hexo-toc": "^1.0.8", "html-minifier": "^3.5.6", "markdown-it": "^8.3.1", - "qiniu": "^7.1.1", "sax": "^1.2.4", "uglify-es": "^3.1.5", "mkdir-recursive": "^0.3.0", @@ -27,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 index 29e549a..64f604d 100644 --- a/scripts/console.js +++ b/scripts/console.js @@ -4,7 +4,6 @@ */ hexo.extend.console.register('qndeploy', 'deploy project use qiniu', function (args) { hexo.on('deployAfter', function(){ - console.log('aaaaaaaaaaaaaa啊啊发生地方阿斯顿发'); hexo.call('qiniu',{}); }); hexo.config.staticDir = hexo.config.qiniu.cdnUrlPrefix; diff --git a/scripts/redering.js b/scripts/redering.js index 65b548b..c8efdcb 100644 --- a/scripts/redering.js +++ b/scripts/redering.js @@ -9,9 +9,9 @@ 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, ''); + str = str.replace(//gi, ''); + str = str.replace(//gi, ''); + str = str.replace(/<\/script>/gi, ''); return minify(str, { enable: true, exclude: [], From de4e759bed3de925781595cc9f868b7f1a659d22 Mon Sep 17 00:00:00 2001 From: liushuai Date: Wed, 8 Nov 2017 11:06:57 +0800 Subject: [PATCH 4/4] fix --- package.json | 2 +- themes/zendapi/scripts/redering.js | 35 ------------------------------ 2 files changed, 1 insertion(+), 36 deletions(-) delete mode 100644 themes/zendapi/scripts/redering.js diff --git a/package.json b/package.json index 476ec40..c56f57a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "private": true, "hexo": { - "version": "3.3.9" + "version": "3.3.7" }, "dependencies": { "clean-css": "^4.1.9", 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