异步读文件优化性能

This commit is contained in:
结发受长生 2019-05-15 14:24:41 +08:00
parent 58090f0ef2
commit 9c9ad8eac9
4 changed files with 63 additions and 44 deletions

View File

@ -55,11 +55,11 @@ class ImageSynchronizer {
item.type = 'new'
return true
}
// else if (storageItems[index].eTag !== item.md5) {
// // 文件名存在, 但是hash值不同, 代表有变化
// item.type = 'change'
// return true
// }
else if (storageItems[index].eTag !== item.md5) {
// 文件名存在, 但是hash值不同, 代表有变化
item.type = 'change'
return true
}
return false
});
// 处理待上传的文件
@ -85,12 +85,12 @@ class ImageSynchronizer {
}).then(result => {
// eTag是上传后远端校验的md5值, 用于和本地进行比对
let eTag = result.eTag.replace(/"/g,'')
// if(filesList[index].md5 === eTag) {
if(filesList[index].md5 === eTag) {
console.log(`${filesList[index].name} 上传成功, md5:${eTag} 类型: ${filesList[index].type}`)
// } else {
// console.warn(`${filesList[index].name} 上传出错, md5值不一致`)
// console.warn(`===> 本地文件: ${filesList[index].md5}, 接口返回: ${eTag}`)
// }
} else {
console.warn(`${filesList[index].name} 上传出错, md5值不一致`)
console.warn(`===> 本地文件: ${filesList[index].md5}, 接口返回: ${eTag}`)
}
this._uploadObject(filesList, ++index)
})
}

View File

@ -2,40 +2,51 @@ const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
function sortName(item1, item2) {
if(item1.name > item2.name) {
return 1
} else if(item1.name < item2.name) {
return -1
}
return 0
}
/**
* 递归遍历目录中的所有文件
* @param {String} imageFolderPath 文件夹路径
* @param {Array} images 图片列表
* @param {String} rootPath 根路径
*/
function readDirSync(imageFolderPath, images, rootPath){
function readDirSync(imageFolderPath, images, rootPath, callback, count={fileCount:0, finishCount:0}){
var files = fs.readdirSync(imageFolderPath);
files.forEach(item => {
var fileInfo = fs.statSync(`${imageFolderPath}/${item}`)
if(fileInfo.isDirectory()){
// 该文件是一个目录, 则遍历该目录内容
readDirSync(`${imageFolderPath}/${item}`, images, rootPath)
}else{
//读取一个Buffer
// let buffer = fs.readFileSync(`${imageFolderPath}/${item}`)
// let fsHash = crypto.createHash('md5')
// fsHash.update(buffer)
images.push({
name: `${imageFolderPath}/${item}`.replace(rootPath, ''),
// md5: fsHash.digest('hex')
readDirSync(`${imageFolderPath}/${item}`, images, rootPath, callback, count)
} else {
count.fileCount ++
var stream = fs.createReadStream(`${imageFolderPath}/${item}`)
var fsHash = crypto.createHash('md5')
stream.on('data', data => {
fsHash.update(data)
})
stream.on('end', () => {
count.finishCount ++
images.push({
name: `${imageFolderPath}/${item}`.replace(rootPath, ''),
md5: fsHash.digest('hex')
})
if(count.fileCount === count.finishCount && typeof callback === 'function') {
callback(images.sort(sortName))
}
})
}
})
return images
}
module.exports = function (rootPath, imageFloder) {
return readDirSync(path.resolve(rootPath, imageFloder), [], rootPath).sort(function(item1, item2){
if(item1.name > item2.name) {
return 1
} else if(item1.name < item2.name) {
return -1
}
return 0
})
module.exports = function (rootPath, imageFloder, callback) {
return readDirSync(path.resolve(rootPath, imageFloder), [], rootPath, callback)
}

View File

@ -6,7 +6,7 @@ const gulp = require('gulp'),
// 程序执行的传参
const argv = require('optimist')
.demand(['accessKey', 'accessSecret', 'deployPath'])
// .demand(['accessKey', 'accessSecret', 'deployPath'])
.describe('accessKey', '网易云对象存储key')
.describe('accessSecret', '网易云对象存储secret')
.describe('deployPath', '静态化后发布的目录')
@ -21,8 +21,7 @@ gulp.task('generate', async function() {
await hexo.call('clean')
await hexo.call('generate', { watch: false })
return hexo.exit()
}
catch (err) {
} catch (err) {
return hexo.exit(err)
}
})
@ -54,20 +53,29 @@ gulp.task('compressHtml', () => {
// 同步图片到对象存储仓库
gulp.task('syncImages', () => {
const listImages = require('./deploy_utils/list_images')
// 当前本地存在的所有图片
const imagesList = listImages(`${process.cwd()}/source/`, 'images/')
const ImageSynchronizer = require('./deploy_utils/image_synchronize')
const nosSetting = {
defaultBucket: 'blog-cdn',
endpoint: 'http://nos-eastchina1.126.net',
accessKey: argv.accessKey,
accessSecret: argv.accessSecret
if(!argv.accessKey || !argv.accessSecret) {
return Promise.resolve('未获得accessKey以及accessSecret, 跳过图片同步').then(console.log)
}
const imageSynchronizer = new ImageSynchronizer(nosSetting, imagesList, `${process.cwd()}/source/`)
return imageSynchronizer.synchronize('images/')
// 同步当前本地存在的所有图片
return new Promise((resolve)=>{
listImages(`${process.cwd()}/source/`, 'images/', resolve)
}).then(imagesList => {
const ImageSynchronizer = require('./deploy_utils/image_synchronize')
const nosSetting = {
defaultBucket: 'blog-cdn',
endpoint: 'http://nos-eastchina1.126.net',
accessKey: argv.accessKey,
accessSecret: argv.accessSecret
}
const imageSynchronizer = new ImageSynchronizer(nosSetting, imagesList, `${process.cwd()}/source/`)
return imageSynchronizer.synchronize('images/')
})
})
gulp.task('deploy', () => {
if(!argv.deployPath) {
return Promise.resolve('未获得deployPath, 跳过发布').then(console.log)
}
const deploy = require('./deploy_utils/deploy')
return deploy.exec('./public', argv.deployPath, false)
})

View File

@ -1,6 +1,6 @@
{
"mouseover": {
"waifu": ["是…是不小心碰到了吧", "萝莉控是什么呀", "你看到我的小熊了吗", "再摸的话我可要报警了!⌇●﹏●⌇", "110吗这里有个变态一直在摸我(ó﹏ò。)"],
"waifu": ["是…是不小心碰到了吧", "三年起步,最高死刑呦(๑>◡<๑)", "你看到我的小熊了吗", "再摸的话我可要报警了!⌇●﹏●⌇", "110吗这里有个变态一直在摸我(ó﹏ò。)"],
"home": "点击前往首页,想回到上一页可以使用浏览器的后退功能哦",
"innerArchive": "所有文章都在这里,支持搜索喔",
"friends": "想认识一下我的朋友们吗",