From effeacf2143b0dff8de82948866518de6be1e04c Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Tue, 29 Jan 2019 00:41:43 +0800 Subject: [PATCH] Initial commit --- .gitignore | 6 +++++ ConfigReader.js | 15 +++++++++++ DatabaseProvider.js | 11 ++++++++ DeveloperNote.md | 12 +++++++++ Readme.md | 5 ++++ SimpleLogger.js | 34 ++++++++++++++++++++++++ app.js | 54 ++++++++++++++++++++++++++++++++++++++ config/config.json.example | 17 ++++++++++++ database_sqlite3.js | 19 ++++++++++++++ package.json | 22 ++++++++++++++++ 10 files changed, 195 insertions(+) create mode 100644 .gitignore create mode 100644 ConfigReader.js create mode 100644 DatabaseProvider.js create mode 100644 DeveloperNote.md create mode 100644 Readme.md create mode 100644 SimpleLogger.js create mode 100644 app.js create mode 100644 config/config.json.example create mode 100644 database_sqlite3.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..753577d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +node_modules/ +test/ +config/*.json +*.log +*.db +*.sql diff --git a/ConfigReader.js b/ConfigReader.js new file mode 100644 index 0000000..f00a2a5 --- /dev/null +++ b/ConfigReader.js @@ -0,0 +1,15 @@ +const fs=require('fs') +const path=require('path') + +console.log("[Start] Reading config...") +let Config=null +try { + Config=JSON.parse(fs.readFileSync(path.join("config","config.json"))) +} catch (e) { + console.log("[Fatal] Failed to read config. Stopped.") + console.log(e.toString()) + throw e +} +console.log("[Done] Config is ready.") + +module.exports=Config \ No newline at end of file diff --git a/DatabaseProvider.js b/DatabaseProvider.js new file mode 100644 index 0000000..9f8302b --- /dev/null +++ b/DatabaseProvider.js @@ -0,0 +1,11 @@ +class DatabaseProvider { + constructor(db) { + this.db=db + } + + makeTables() { + return this.db.makeTables() + } +} + +module.exports=DatabaseProvider diff --git a/DeveloperNote.md b/DeveloperNote.md new file mode 100644 index 0000000..bb1b073 --- /dev/null +++ b/DeveloperNote.md @@ -0,0 +1,12 @@ +# File Store Developer Note + +Deployment & Run + +``` +git clone http://kiritow.com:3000/kiritow/filestore +cd filestore +cp config/config.json.example config/config.json +vim config.json +npm install +node app +``` diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..38512bb --- /dev/null +++ b/Readme.md @@ -0,0 +1,5 @@ +# File Store + +File Store & Management. + +See [Developer Note](DeveloperNote.md) for more information. diff --git a/SimpleLogger.js b/SimpleLogger.js new file mode 100644 index 0000000..2dc9117 --- /dev/null +++ b/SimpleLogger.js @@ -0,0 +1,34 @@ +const fs=require('fs') +const Config=require('./ConfigReader') + +class SimpleLogger { + constructor(invoker,log_file) { + this.invoker=invoker + this.stream=fs.createWriteStream(log_file) + this.stream.write(`Simple Logger Initialized at ${new Date().toLocaleString()}\n`) + } + log(str) { + this.invoker.log(str) + this.stream.write(`${new Date().toLocaleString()} | ${str}\n`) + } +} + +class SimpleLoggerInvoker { + constructor(logger,abbr) { + this.logger=logger + this.abbr=abbr + } + log(str) { + this.logger.log(this.logger,`${this.abbr} | ${str}`) + } +} + +let theLogger=new SimpleLogger(console,Config.log_name) + +module.exports=function(abbr) { + if(abbr) { + return new SimpleLoggerInvoker(theLogger,abbr) + } else { + return theLogger + } +} diff --git a/app.js b/app.js new file mode 100644 index 0000000..50c7549 --- /dev/null +++ b/app.js @@ -0,0 +1,54 @@ +const fs=require('fs') +const path=require('path') +const promisify=require('util').promisify + +const dbprovider=require('./DatabaseProvider') + +const Config=require('./ConfigReader') +const logger=require('./SimpleLogger')() + +logger.log("[Start] Initializing database provider...") +let db=null +try { + let provider=`./database_${Config.db_engine.toLowerCase()}` + logger.log(`[Working] Provider resolved to ${provider}`) + let providerClass=require(provider) + db=new dbprovider(new providerClass) +} catch (e) { + logger.log("[Fatal] Failed to create db provider.") + logger.log(e.toString()) + return +} +logger.log("[Done] Database provider Initialized.") + +async function ScanSingle(retArr,filepath) { + let stat=await promisify(fs.stat)(filepath) + if(stat.isDirectory()) { + return ScanDir(retArr,filepath) + } else if(stat.isFile()) { + retArr.push({path:filepath,mtime:stat.mtime,size:stat.size}) + } +} + +async function ScanDir(retArr,dir) { + logger.log(`Scanning ${dir}...`) + let files=await promisify(fs.readdir)(dir) + let pArr=new Array + files.forEach((file)=>{ + pArr.push(ScanSingle(retArr,path.join(dir,file))) + }) + return Promise.all(pArr) +} + +async function main() { + logger.log(`[Start] About to scan ${Config.scan_path.length} dir${Config.scan_path.length>1?"s":""}...`) + let retArr=new Array + let pArr=new Array + Config.scan_path.forEach((dir)=>{ + pArr.push(ScanDir(retArr,dir)) + }) + await Promise.all(pArr) + logger.log(`[Done] Scan Finished. ${retArr.length} results in set.`) +} + +main() diff --git a/config/config.json.example b/config/config.json.example new file mode 100644 index 0000000..996378b --- /dev/null +++ b/config/config.json.example @@ -0,0 +1,17 @@ +{ + "db_engine":"sqlite3", + "db_info":{ + "username":"db_username_here", + "password":"db_password_here", + "host":"localhost", + "port":3306, + "dbname":"your_dbname_here" + }, + "log_name":"filestore.log", + "scan_path":[], + "ignore_types":[], + "filestore_account":{ + "username":"your_username_here", + "password":"your_password_here" + } +} diff --git a/database_sqlite3.js b/database_sqlite3.js new file mode 100644 index 0000000..1c169ca --- /dev/null +++ b/database_sqlite3.js @@ -0,0 +1,19 @@ +const sqlite3=require('sqlite3') +const Config=require('./ConfigReader') +const logger=require('./SimpleLogger')("[SQLite3 Driver]") + +class DatabaseSQLite3Provider { + constructor() { + logger.log(`Opening database file: ${Config.db_info.dbname}.db`) + this.db=new sqlite3.Database(`${Config.db_info.dbname}.db`,(err)=>{ + if(err) { + logger.log(`[Fatal] ${e.toString()}`) + throw err + } else { + logger.log("Database opened.") + } + }) + } +} + +module.exports=DatabaseSQLite3Provider diff --git a/package.json b/package.json new file mode 100644 index 0000000..b11f424 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "filestore", + "version": "0.0.1", + "description": "File Store & Management", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "http://kiritow.com:3000/Kiritow/FileStore" + }, + "keywords": [ + "Node", + "File" + ], + "author": "Kiritow", + "license": "MIT", + "dependencies": { + "sqlite3": "^4.0.6" + } +}