diff --git a/index.js b/index.js index 1949442..4060bec 100644 --- a/index.js +++ b/index.js @@ -311,6 +311,28 @@ function createDb(db, structId, name) { return transaction(); } +function getFilesForStruct(db, structureId) { + let test = db + .prepare("SELECT * FROM files WHERE structure_id = ? ORDER BY id DESC") + .all(structureId); + + console.log(db.prepare("PRAGMA table_info(files)").all()); + + return test; +} + +function getFile(db, fileId) { + return db.prepare("SELECT * FROM files WHERE id = ?").get(fileId); +} + +function createFile(db, structure_id, name, filePath, mime_type, mime_subtype) { + return db + .prepare( + "INSERT INTO files (structure_id, name, path, mime_type, mime_subtype) VALUES (?, ?, ?, ?, ?)" + ) + .run(structure_id, name, filePath, mime_type, mime_subtype).lastInsertRowid; +} + const { LRUCache } = require("lru-cache"); const templateCache = new LRUCache({ max: 100 }); @@ -586,8 +608,68 @@ app.get("/workshop/:structure_id/route/:id", (req, res) => { ); }); -app.get("/workshop/tip", (req, res) => { - res.send("try entering something in the url field above"); +// POST route to handle file upload +app.post("/workshop/:structure_id/files", (req, res) => { + if (!req.files || Object.keys(req.files).length === 0) { + return res.status(400).send("failed to upload that file"); + } + + const { structure_id } = req.params; + const file = req.files.file; + const name = file.name; + const [mime_type, mime_subtype] = file.mimetype.split("/"); + const uploadPath = path.join(__dirname, "public", structure_id); + const storedPath = path.join(structure_id, file.name); + + fs.mkdirSync(uploadPath, { recursive: true }); + + file.mv(path.join(uploadPath, name), (err) => { + try { + if (err) { + return res.status(500).send(err); + } + + let id = createFile( + db, + structure_id, + name, + storedPath, + mime_type, + mime_subtype + ); + + let file = getFile(db, id); + file.url = prefixUrlWithHost(req, file.path); + + return res.send(eta.render("workshop/file_detail", { file: file })); + } catch (e) { + res.status(500); + return res.send(e); + } + }); +}); + +function prefixUrlWithHost(req, path) { + return req.protocol + "://" + req.get("host") + "/" + path; +} + +// POST route to handle file upload +app.get("/workshop/:structure_id/files", (req, res) => { + const { structure_id } = req.params; + + const files = getFilesForStruct(db, structure_id); + files.map((it) => { + it.url = prefixUrlWithHost(req, it.path); + }); + + return res.send( + bootstrapTemplateWithHTMXetc( + eta.render("workshop/files", { + files, + ...sidebarStuff(db, structure_id), + }) + ) + ); }); app.get("/workshop/:structure_id/new_template_modal", (req, res) => { diff --git a/migrations/000_bootstrap_db.sql b/migrations/000_bootstrap_db.sql index bd91527..0b98b26 100644 --- a/migrations/000_bootstrap_db.sql +++ b/migrations/000_bootstrap_db.sql @@ -37,8 +37,7 @@ CREATE TABLE templates ( engine TEXT NOT NULL DEFAULT "eta", created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(structure_id) REFERENCES structures(id) - UNIQUE(name, structure_id) + FOREIGN KEY(structure_id) REFERENCES structures(id) UNIQUE(name, structure_id) ); CREATE TABLE routes ( @@ -57,47 +56,89 @@ CREATE TABLE structure_dbs ( db_id INTEGER, structure_id INTEGER, alias TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(db_id, structure_id), FOREIGN KEY(db_id) REFERENCES dbs(id), FOREIGN KEY(structure_id) REFERENCES structures(id) ); +CREATE TABLE files ( + id INTEGER PRIMARY KEY, + structure_id INTEGER, + name TEXT, + path TEXT, + mime_type TEXT, + mime_subtype TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (structure_id) REFERENCES structures(id), + UNIQUE(path, structure_id) +); -- =========================== -- TRIGGERS -- =========================== - CREATE TRIGGER update_users_updated_at -AFTER UPDATE ON users -FOR EACH ROW -BEGIN - UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +AFTER +UPDATE + ON users FOR EACH ROW BEGIN +UPDATE + users +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = OLD.id; + END; CREATE TRIGGER update_structures_updated_at -AFTER UPDATE ON structures -FOR EACH ROW -BEGIN - UPDATE structures SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +AFTER +UPDATE + ON structures FOR EACH ROW BEGIN +UPDATE + structures +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = OLD.id; + END; CREATE TRIGGER update_dbs_updated_at -AFTER UPDATE ON dbs -FOR EACH ROW -BEGIN - UPDATE dbs SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +AFTER +UPDATE + ON dbs FOR EACH ROW BEGIN +UPDATE + dbs +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = OLD.id; + END; CREATE TRIGGER update_templates_updated_at -AFTER UPDATE ON templates -FOR EACH ROW -BEGIN - UPDATE templates SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; +AFTER +UPDATE + ON templates FOR EACH ROW BEGIN +UPDATE + templates +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = OLD.id; + END; CREATE TRIGGER update_routes_updated_at -AFTER UPDATE ON routes -FOR EACH ROW -BEGIN - UPDATE routes SET updated_at = CURRENT_TIMESTAMP WHERE id = OLD.id; -END; +AFTER +UPDATE + ON routes FOR EACH ROW BEGIN +UPDATE + routes +SET + updated_at = CURRENT_TIMESTAMP +WHERE + id = OLD.id; + +END; \ No newline at end of file diff --git a/views/workshop/file_detail.eta b/views/workshop/file_detail.eta new file mode 100644 index 0000000..33e447a --- /dev/null +++ b/views/workshop/file_detail.eta @@ -0,0 +1 @@ +
  • <%= it.file.name %>    
  • \ No newline at end of file diff --git a/views/workshop/files.eta b/views/workshop/files.eta new file mode 100644 index 0000000..122ba94 --- /dev/null +++ b/views/workshop/files.eta @@ -0,0 +1,42 @@ + + + + + + + + +
    +
    +
    Workshop - <%~ include('/workshop/structure_name', {structure: it.structure}) %> +
    +
    +
    +
    + <%~ include('/workshop/sidebar', it) %> +
    +
    +

    Files

    +
    +
    + + + +
    +
    +
    +
      + <% for (let file of it.files) { %> + <%~ include('/workshop/file_detail', { file }) %> + <% } %> +
    +
    +
    +
    +
    + + + + \ No newline at end of file diff --git a/views/workshop/sidebar.eta b/views/workshop/sidebar.eta index 294abbb..bb97825 100644 --- a/views/workshop/sidebar.eta +++ b/views/workshop/sidebar.eta @@ -56,5 +56,8 @@ +
  • + Files +
  • \ No newline at end of file