Last active
April 10, 2016 09:31
-
-
Save cloudrain21/11a43fd3313c93139b5f3a4727bc9d44 to your computer and use it in GitHub Desktop.
Simple file stat infomation module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
util = require('util'), | |
imgsize = require('image-size'); | |
module.exports = (function(){ | |
var supportExt = ["jpg", "jpeg", "png"]; // supported extension array | |
var dirPath = ""; // directory path | |
var fileList = []; // file name array | |
var fileInfo = [{}]; // array of file info object | |
// get filename list array length | |
var getFileCount = function() { | |
return fileList.length; | |
}; | |
// set directory path for getting file list and infos | |
var setDirPath = function(path) { | |
dirPath = path; | |
}; | |
// currently set directory path | |
var getDirPath = function() { | |
return dirPath; | |
}; | |
// add supported file extension | |
var addFileExt = function(extension) { | |
supportExt.push(extension); | |
}; | |
// get filename list from given directory path | |
var getFileListWithPath = function(path) { | |
fileList = fs.readdirSync(path); | |
if( fileList == null ) { | |
console.error("get file list error : " + error.errno); | |
return null; | |
} | |
return fileList; | |
}; | |
// return currently maintaining filename list | |
var getFileList = function() { | |
fileList = fs.readdirSync(dirPath); | |
if( fileList == null ) { | |
console.error("get file list error : " + error.errno); | |
return null; | |
} | |
return fileList; | |
}; | |
// read file stat from storage file and keep,return them | |
var getFileInfo = function(path,callback) { | |
if( path != null ) { | |
setDirPath(path); | |
} | |
getFileList(); | |
// John Resig (MIT Licensed) | |
Array.prototype.remove = function(from, to) { | |
var rest = this.slice((to || from) + 1 || this.length); | |
this.length = from < 0 ? this.length + from : from; | |
return this.push.apply(this, rest); | |
}; | |
for(var i=0; i<fileList.length; i++) { | |
ext = fileList[i].split('.').pop(); | |
if( supportExt.indexOf(ext) == -1 ) { | |
console.log("Unsupported file extension : " + ext); | |
fileList.remove(i); | |
continue; | |
} | |
var filePath = dirPath + "/" + fileList[i]; | |
var fileStat = fs.statSync(filePath); | |
if( ! fileStat.isFile() ) { | |
continue; | |
} | |
var dimen = imgsize(filePath); | |
var portland = dimen.height >= dimen.width ? "port" : "land"; | |
// console.log("File [ " + filePath + "]"); | |
var fileStatStr = util.inspect(fileStat); | |
var fileSize = fileStatStr.replace(/[{}, ]/g, '') | |
.split('\n')[8] | |
.split(':')[1]; | |
var fileObj = { | |
filename: fileList[i], | |
filesize: fileSize, | |
portland: portland | |
}; | |
callback(fileObj); | |
// console.log(JSON.stringify(fileObj)); | |
} | |
}; | |
return { | |
addSupportFileExt: addFileExt, | |
getFileCount: getFileCount, | |
getDirPath: getDirPath, | |
getFileList: getFileList, | |
getFileListWithPath: getFileListWithPath, | |
getFileInfo: getFileInfo | |
}; | |
}()); | |
/* | |
var finfo = require('fileinfo.js'); | |
(function() { | |
var dirPath = "../public/img/laos1"; | |
finfo.getFileInfo(dirPath, function(fileObj) { | |
console.log(JSON.stringify(fileObj); | |
}); | |
}()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment