异步读文件优化性能
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
+31
-20
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user