diff --git a/handlers/fileHandler.js b/handlers/fileHandler.js new file mode 100644 index 0000000..e69de29 diff --git a/handlers/sqliteHandler.js b/handlers/sqliteHandler.js new file mode 100644 index 0000000..ee4116d --- /dev/null +++ b/handlers/sqliteHandler.js @@ -0,0 +1,168 @@ +module.exports = { + + UploadObject: class UploadObject { + constructor(db) { + this.db = db; + this.absrc = ['E', 'T', 'X', 'W', 'U', 'R', 'J', 'I', 'N', 'C']; + this.loaded = false; + this.fileCache = {}; + } + + new() { + if (!this.loaded) { + this.hashsalt = this.makeHash(10); + this.adminhash = this.makeHash(10); + var sql = this.db.prepare(`INSERT INTO "upload" (hashsalt, adminhash, upload_unix) VALUES (?, ?, ?)`); + var result = sql.run(this.hashsalt, this.adminhash, Math.floor(Date.now() / 1000)); + this.id = result.lastInsertRowid; + this.hashid = this.id2hashid(result.lastInsertRowid); + this.dir = this.hashid+this.hashsalt; + var fs = require('fs'); + + if (!fs.existsSync("uploads/"+this.dir)){ + fs.mkdirSync("uploads/"+this.dir); + } + + this.loaded = true; + } + } + + load(hash) { + if (!this.loaded) { + var shash = this.splitHash(hash); + this.hashid = shash[0]; + this.id = this.hashid2id(this.hashid) + this.hashsalt = shash[1]; + this.dir = this.hashid+this.hashsalt; + + var sql = this.db.prepare(`SELECT EXISTS(SELECT 1 FROM "upload" WHERE id=? AND hashsalt=? LIMIT 1) AS "exists"`); + var result = sql.get(this.id, this.hashsalt); + if (result["exists"]) { + console.log("LOADED: ", result); + this.loaded = true; + } + } + } + + isLoaded() { + return this.loaded; + } + + getDir() { + return this.dir; + } + + loadFiles() { + this.files = this.db.prepare(`SELECT filename FROM "file" WHERE upload_id=?`).all(this.id); + console.log(this.files); + } + + getFiles() { + this.loadFiles(); + return this.files; + } + + registerFile(filename) { + var sql = this.db.prepare(`INSERT INTO "file" (filename, upload_id) VALUES (?, ?)`); + var result = sql.run(filename, this.id); + return result; + } + + delete() { + + } + + getHash() { + return this.hashid+this.hashsalt; + } + + getHashId() { + return this.hashid; + } + + getAdminHash() { + return this.adminhash; + } + + + id2hashid(id) { + var hashid = ""; + id = id.toString(); + for (var i = 0; i < id.length; i++) { + hashid += this.absrc[id.charAt(i)]; + } + return hashid; + } + + hashid2id(hashid) { + var id = ""; + for (var i = 0; i < hashid.length; i++) { + var index = this.absrc.findIndex((letter) => letter == hashid.charAt(i)); + if (!(index < 0)) { + id = id + index.toString(); + }; + } + return parseInt(id); + } + + // https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript + makeHash(len = 10) { + let result = ''; + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + const charactersLength = characters.length; + let counter = 0; + while (counter < len) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + counter += 1; + } + return result; + } + + splitHash(hash) { + return [hash.slice(0, hash.length - 10), hash.slice(hash.length - 10, hash.length)] + } + + }, + + SqliteHandler: class SqliteHandler { + constructor(path) { + this.loaded = false; + this.path = path; + this.db = require('better-sqlite3')(this.path); + this.db.prepare(`CREATE TABLE IF NOT EXISTS "user" ( + "id" INTEGER NOT NULL UNIQUE, + "name" TEXT NOT NULL UNIQUE, + "password" TEXT NOT NULL, + "email" INTEGER, + "admin" INTEGER, + PRIMARY KEY("id" AUTOINCREMENT) + )`).run(); + + this.db.prepare(`CREATE TABLE IF NOT EXISTS "upload" ( + "id" INTEGER NOT NULL UNIQUE, + "hashsalt" TEXT NOT NULL UNIQUE, + "adminhash" TEXT NOT NULL UNIQUE, + "desc" TEXT, + "password" TEXT, + "owner_id" INTEGER, + "upload_unix" INTEGER, + PRIMARY KEY("id" AUTOINCREMENT), + FOREIGN KEY("owner_id") REFERENCES "user"("id") + )`).run(); + + this.db.prepare(`CREATE TABLE IF NOT EXISTS "file" ( + "id" INTEGER NOT NULL UNIQUE, + "filename" TEXT NOT NULL, + "upload_id" INTEGER NOT NULL, + "state" INTEGER NOT NULL, + PRIMARY KEY("id" AUTOINCREMENT), + FOREIGN KEY("upload_id") REFERENCES "upload"("id") + )`).run(); + } + + getDatabaseObj() { + return this.db; + } + } + +} \ No newline at end of file diff --git a/handlers/userHandler.js b/handlers/userHandler.js new file mode 100644 index 0000000..e69de29