【第三章】BCMS中应用的文件系统
因为我的mac 上mysql 的问题,造成我选择file和mongodb来做数据的持久化操作。
mongodb的话,写了一小部分,已经可是可以使用,但是存在问题,所以决定先写file来进行数据操作。
之前我和小伙伴思考和讨论过这个问题,用小文件和数据库储存文件哪个会更快,数据库保存数据的方式应该和文件系统的方式一致,至于效率有待验证。
既然要使用文件系统去做数据的存储,基础的文件读取修改等方法应该写的通用,我现在还在修改这部分,因为已经在应用中发现了问题,这里先贴出来。
这里是主文件file.robot.js
const fs = require('fs')
const fileUtil = require('./utils')
const getRootPath = fileUtil.getRootPath
const fileSet = require('./file.config')
const dataPath = fileSet.dataPath
const fileEndfix = fileSet.fileEndfix
const getFilename = function(filename) {
if(filename.indexOf(fileEndfix) >= 0) {
return filename
}
return getRootPath() + dataPath + '/' + filename + fileEndfix
}
const create = function(filename, data, callback) {
console.log('create file start')
data = data || ""
checkAndMkdir(filename, function() {
fs.writeFile(filename, data, function(err) {
if(err) {
console.log('sorry, still have mistake in create function')
}
callback && callback()
})
})
console.log('there is something wrong in the create file function')
}
const deleteFile = function(file) {
if(fs.existsSync(file)) {
fs.unlinkSync(file)
}
}
const clearContent = function(filename, callback) {
const filepath = getFilename(filename)
if(fs.existsSync(filepath)) {
fs.writeFile(filepath, '', function(err) {
if(err) {
console.log('Sorry, you make a mistake when you clear the file content : ' + filepath, err)
}
callback && callback()
})
}
}
const modify = function(filename, data, callback) {
console.log('modify file ', filename )
const filepath = getFilename(filename)
const oldContent = getFileContent(filepath)
const content = mergeContent(data, oldContent)
fs.writeFile(filepath, content, function(err) {
if(err) {
console.log('Sorry, you have made a mistake when you modify file: ' + filepath, err)
}
callback && callback()
})
}
const readFile = function(filename) {
const file_full_name = getFilename(filename)
const isExist = fs.existsSync(file_full_name)
if(!isExist) {
create(file_full_name)
} else {
return getFileContent(filename)
}
}
const getFileContent = function(filename) {
return fs.readFileSync(getFilename(filename)).toString()
}
function getParentPath(pathname) {
if(pathname.indexOf('/')<0) {
return pathname
}
return pathname.split('/').slice(0, -1).join('/')
}
function checkAndMkdir(pathname, callback) {
const dirname = getParentPath(pathname)
fs.mkdir(dirname, function(err) {
if(err) {
const parentDir = getParentPath(pathname)
checkAndMkdir(parentDir, callback)
}
callback && callback()
})
}
/**
*
*
*/
function mergeContent(newContent, oldContent) {
// console.log(' newContent ', newContent, oldContent.toString())
if(!oldContent) {
return newContent
}
const data = JSON.parse(oldContent)
const type = Object.prototype.toString.call(data)
if(typeof newContent !== 'string') {
console.log('Sorry, your merge data must be string')
return null
}
if(type === '[object Array]') {
data.push(newContent)
}
if(type === '[object Object]') {
data[newContent] = newContent
}
return data
}
module.exports = {
create: create,
deleteFile: deleteFile,
clearContent: clearContent,
modify: modify,
readFile: readFile,
getFileContent: getFileContent,
// readFiles: readFiles,
}
接着是file用到帮助函数,写在utils中:
var fs = require('fs') var rootPath = require('./file.config').rootPath var pathStore = {} Object.defineProperty(pathStore, 'rootPath', { value: rootPath, writable: false, enumerable: false, configurable: false, }) var getPath = function(key) { return pathStore[key] } var setPath = function(key, value) { pathStore } const getRootPath = function() { return getPath('rootPath') } const setRootPath = function() { setPath('rootPath', rootPath) } setRootPath() module.exports = { getPath: getPath, setPath: setPath, getRootPath: getRootPath, }
当然,这里有一个配置文件,去配置根目录和文件后缀,比如你想存成.txt, 后缀就写成'.txt', 想存成js,就把后缀换成'.js':
//files store path, use absolute path const path = require('path') const rootPath = path.join(__dirname) const dataPath = '/data' const fileEndfix = '.txt' const superAdmin = { name: 'admin', pass: 'admin', role: 'super' } module.exports = { rootPath: rootPath, dataPath: dataPath, fileEndfix: fileEndfix, superAdmin: superAdmin, }
superAdmin是初始化超级管理员的账户信息,可以自己去随意更改,然后添加新的管理员之后,这个账户可以禁用。
基本的file储存方法已经可以使用(尚在修改中),下次将写如何从前端初始化一个用户的操作。
浙公网安备 33010602011771号