Initial commit

This commit is contained in:
Kirigaya Kazuto 2019-01-29 00:41:43 +08:00
commit effeacf214
10 changed files with 195 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
node_modules/
test/
config/*.json
*.log
*.db
*.sql

15
ConfigReader.js Normal file
View File

@ -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

11
DatabaseProvider.js Normal file
View File

@ -0,0 +1,11 @@
class DatabaseProvider {
constructor(db) {
this.db=db
}
makeTables() {
return this.db.makeTables()
}
}
module.exports=DatabaseProvider

12
DeveloperNote.md Normal file
View File

@ -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
```

5
Readme.md Normal file
View File

@ -0,0 +1,5 @@
# File Store
File Store & Management.
See [Developer Note](DeveloperNote.md) for more information.

34
SimpleLogger.js Normal file
View File

@ -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
}
}

54
app.js Normal file
View File

@ -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()

View File

@ -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"
}
}

19
database_sqlite3.js Normal file
View File

@ -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

22
package.json Normal file
View File

@ -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"
}
}