Files
Kakubovna/app/handlers/uploadHandler.js
2023-06-07 00:14:44 +02:00

158 lines
4.8 KiB
JavaScript

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.num_files = 0;
this.fileCache = {};
}
new(num_files) {
if (!this.loaded) {
if (typeof num_files == "number" && num_files > 0) {
this.num_files = num_files;
} else {
console.error("Invalid number of files");
return false;
}
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;
return 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 count(id) AS "count" FROM "file" WHERE upload_id=?`);
var num_files = sql.get(this.id)["count"];
if (typeof num_files == "number" && num_files > 0) {
this.num_files = num_files;
} else {
console.error("Invalid number of files");
return false;
}
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;
return true;
} else {
console.log("Not Found");
return false;
}
}
}
getNumFiles() {
return this.num_files;
}
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;
}
getFileByIndex(index) {
return this.getFiles()[index];
}
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)]
}
}
}