异步读文件优化性能
This commit is contained in:
parent
58090f0ef2
commit
9c9ad8eac9
@ -55,11 +55,11 @@ class ImageSynchronizer {
|
|||||||
item.type = 'new'
|
item.type = 'new'
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// else if (storageItems[index].eTag !== item.md5) {
|
else if (storageItems[index].eTag !== item.md5) {
|
||||||
// // 文件名存在, 但是hash值不同, 代表有变化
|
// 文件名存在, 但是hash值不同, 代表有变化
|
||||||
// item.type = 'change'
|
item.type = 'change'
|
||||||
// return true
|
return true
|
||||||
// }
|
}
|
||||||
return false
|
return false
|
||||||
});
|
});
|
||||||
// 处理待上传的文件
|
// 处理待上传的文件
|
||||||
@ -85,12 +85,12 @@ class ImageSynchronizer {
|
|||||||
}).then(result => {
|
}).then(result => {
|
||||||
// eTag是上传后远端校验的md5值, 用于和本地进行比对
|
// eTag是上传后远端校验的md5值, 用于和本地进行比对
|
||||||
let eTag = result.eTag.replace(/"/g,'')
|
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}`)
|
console.log(`${filesList[index].name} 上传成功, md5:${eTag} 类型: ${filesList[index].type}`)
|
||||||
// } else {
|
} else {
|
||||||
// console.warn(`${filesList[index].name} 上传出错, md5值不一致`)
|
console.warn(`${filesList[index].name} 上传出错, md5值不一致`)
|
||||||
// console.warn(`===> 本地文件: ${filesList[index].md5}, 接口返回: ${eTag}`)
|
console.warn(`===> 本地文件: ${filesList[index].md5}, 接口返回: ${eTag}`)
|
||||||
// }
|
}
|
||||||
this._uploadObject(filesList, ++index)
|
this._uploadObject(filesList, ++index)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,40 +2,51 @@ const fs = require('fs')
|
|||||||
const path = require('path')
|
const path = require('path')
|
||||||
const crypto = require('crypto')
|
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 {String} imageFolderPath 文件夹路径
|
||||||
* @param {Array} images 图片列表
|
* @param {Array} images 图片列表
|
||||||
* @param {String} rootPath 根路径
|
* @param {String} rootPath 根路径
|
||||||
*/
|
*/
|
||||||
function readDirSync(imageFolderPath, images, rootPath){
|
function readDirSync(imageFolderPath, images, rootPath, callback, count={fileCount:0, finishCount:0}){
|
||||||
var files = fs.readdirSync(imageFolderPath);
|
var files = fs.readdirSync(imageFolderPath);
|
||||||
files.forEach(item => {
|
files.forEach(item => {
|
||||||
var fileInfo = fs.statSync(`${imageFolderPath}/${item}`)
|
var fileInfo = fs.statSync(`${imageFolderPath}/${item}`)
|
||||||
if(fileInfo.isDirectory()){
|
if(fileInfo.isDirectory()){
|
||||||
// 该文件是一个目录, 则遍历该目录内容
|
// 该文件是一个目录, 则遍历该目录内容
|
||||||
readDirSync(`${imageFolderPath}/${item}`, images, rootPath)
|
readDirSync(`${imageFolderPath}/${item}`, images, rootPath, callback, count)
|
||||||
}else{
|
} else {
|
||||||
//读取一个Buffer
|
count.fileCount ++
|
||||||
// let buffer = fs.readFileSync(`${imageFolderPath}/${item}`)
|
var stream = fs.createReadStream(`${imageFolderPath}/${item}`)
|
||||||
// let fsHash = crypto.createHash('md5')
|
var fsHash = crypto.createHash('md5')
|
||||||
// fsHash.update(buffer)
|
|
||||||
|
stream.on('data', data => {
|
||||||
|
fsHash.update(data)
|
||||||
|
})
|
||||||
|
stream.on('end', () => {
|
||||||
|
count.finishCount ++
|
||||||
images.push({
|
images.push({
|
||||||
name: `${imageFolderPath}/${item}`.replace(rootPath, ''),
|
name: `${imageFolderPath}/${item}`.replace(rootPath, ''),
|
||||||
// md5: fsHash.digest('hex')
|
md5: fsHash.digest('hex')
|
||||||
|
})
|
||||||
|
if(count.fileCount === count.finishCount && typeof callback === 'function') {
|
||||||
|
callback(images.sort(sortName))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return images
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function (rootPath, imageFloder) {
|
module.exports = function (rootPath, imageFloder, callback) {
|
||||||
return readDirSync(path.resolve(rootPath, imageFloder), [], rootPath).sort(function(item1, item2){
|
return readDirSync(path.resolve(rootPath, imageFloder), [], rootPath, callback)
|
||||||
if(item1.name > item2.name) {
|
|
||||||
return 1
|
|
||||||
} else if(item1.name < item2.name) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
gulpfile.js
18
gulpfile.js
@ -6,7 +6,7 @@ const gulp = require('gulp'),
|
|||||||
|
|
||||||
// 程序执行的传参
|
// 程序执行的传参
|
||||||
const argv = require('optimist')
|
const argv = require('optimist')
|
||||||
.demand(['accessKey', 'accessSecret', 'deployPath'])
|
// .demand(['accessKey', 'accessSecret', 'deployPath'])
|
||||||
.describe('accessKey', '网易云对象存储key')
|
.describe('accessKey', '网易云对象存储key')
|
||||||
.describe('accessSecret', '网易云对象存储secret')
|
.describe('accessSecret', '网易云对象存储secret')
|
||||||
.describe('deployPath', '静态化后发布的目录')
|
.describe('deployPath', '静态化后发布的目录')
|
||||||
@ -21,8 +21,7 @@ gulp.task('generate', async function() {
|
|||||||
await hexo.call('clean')
|
await hexo.call('clean')
|
||||||
await hexo.call('generate', { watch: false })
|
await hexo.call('generate', { watch: false })
|
||||||
return hexo.exit()
|
return hexo.exit()
|
||||||
}
|
} catch (err) {
|
||||||
catch (err) {
|
|
||||||
return hexo.exit(err)
|
return hexo.exit(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -54,8 +53,13 @@ gulp.task('compressHtml', () => {
|
|||||||
// 同步图片到对象存储仓库
|
// 同步图片到对象存储仓库
|
||||||
gulp.task('syncImages', () => {
|
gulp.task('syncImages', () => {
|
||||||
const listImages = require('./deploy_utils/list_images')
|
const listImages = require('./deploy_utils/list_images')
|
||||||
// 当前本地存在的所有图片
|
if(!argv.accessKey || !argv.accessSecret) {
|
||||||
const imagesList = listImages(`${process.cwd()}/source/`, 'images/')
|
return Promise.resolve('未获得accessKey以及accessSecret, 跳过图片同步').then(console.log)
|
||||||
|
}
|
||||||
|
// 同步当前本地存在的所有图片
|
||||||
|
return new Promise((resolve)=>{
|
||||||
|
listImages(`${process.cwd()}/source/`, 'images/', resolve)
|
||||||
|
}).then(imagesList => {
|
||||||
const ImageSynchronizer = require('./deploy_utils/image_synchronize')
|
const ImageSynchronizer = require('./deploy_utils/image_synchronize')
|
||||||
const nosSetting = {
|
const nosSetting = {
|
||||||
defaultBucket: 'blog-cdn',
|
defaultBucket: 'blog-cdn',
|
||||||
@ -65,9 +69,13 @@ gulp.task('syncImages', () => {
|
|||||||
}
|
}
|
||||||
const imageSynchronizer = new ImageSynchronizer(nosSetting, imagesList, `${process.cwd()}/source/`)
|
const imageSynchronizer = new ImageSynchronizer(nosSetting, imagesList, `${process.cwd()}/source/`)
|
||||||
return imageSynchronizer.synchronize('images/')
|
return imageSynchronizer.synchronize('images/')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
gulp.task('deploy', () => {
|
gulp.task('deploy', () => {
|
||||||
|
if(!argv.deployPath) {
|
||||||
|
return Promise.resolve('未获得deployPath, 跳过发布').then(console.log)
|
||||||
|
}
|
||||||
const deploy = require('./deploy_utils/deploy')
|
const deploy = require('./deploy_utils/deploy')
|
||||||
return deploy.exec('./public', argv.deployPath, false)
|
return deploy.exec('./public', argv.deployPath, false)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"mouseover": {
|
"mouseover": {
|
||||||
"waifu": ["是…是不小心碰到了吧", "萝莉控是什么呀", "你看到我的小熊了吗", "再摸的话我可要报警了!⌇●﹏●⌇", "110吗,这里有个变态一直在摸我(ó﹏ò。)"],
|
"waifu": ["是…是不小心碰到了吧", "三年起步,最高死刑呦(๑>◡<๑)", "你看到我的小熊了吗", "再摸的话我可要报警了!⌇●﹏●⌇", "110吗,这里有个变态一直在摸我(ó﹏ò。)"],
|
||||||
"home": "点击前往首页,想回到上一页可以使用浏览器的后退功能哦",
|
"home": "点击前往首页,想回到上一页可以使用浏览器的后退功能哦",
|
||||||
"innerArchive": "所有文章都在这里,支持搜索喔",
|
"innerArchive": "所有文章都在这里,支持搜索喔",
|
||||||
"friends": "想认识一下我的朋友们吗",
|
"friends": "想认识一下我的朋友们吗",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user