feat: add file lib to routes, abstract out file upload functionality
This commit is contained in:
parent
a680ff9120
commit
19a9d63d6c
2 changed files with 38 additions and 31 deletions
67
index.js
67
index.js
|
|
@ -54,6 +54,31 @@ webPush.setVapidDetails(
|
|||
vapidPrivateKey,
|
||||
);
|
||||
|
||||
async function saveFile(structureId, req, uploadedFile, asset=false) {
|
||||
const name = uploadedFile.name;
|
||||
const [mime_type, mime_subtype] = uploadedFile.mimetype.split("/");
|
||||
const uploadPath = path.join(__dirname, "public", structureId);
|
||||
const storedPath = path.join(structureId, uploadedFile.name);
|
||||
|
||||
fs.mkdirSync(uploadPath, { recursive: true });
|
||||
|
||||
await uploadedFile.mv(path.join(uploadPath, name));
|
||||
let id = model.createFile(
|
||||
db,
|
||||
structureId,
|
||||
name,
|
||||
storedPath,
|
||||
mime_type,
|
||||
mime_subtype,
|
||||
asset,
|
||||
);
|
||||
|
||||
let file = model.getFile(db, id);
|
||||
file.url = prefixUrlWithHost(req, file.path);
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
function bootstrapContext(db, structureId, routeId, initContext) {
|
||||
const allDbInstances = {};
|
||||
function getDb(alias) {
|
||||
|
|
@ -64,7 +89,7 @@ function bootstrapContext(db, structureId, routeId, initContext) {
|
|||
// for now just designing with component approach
|
||||
const eta = model.getTemplater(structureId);
|
||||
|
||||
const libs = { eta, db: getDb, push: webPush };
|
||||
const libs = { eta, db: getDb, push: webPush, files: { saveFile: (...args) => saveFile(structureId, ...args) } };
|
||||
|
||||
let dbs = model.getDbsForStructure(db, structureId);
|
||||
let context = vm.createContext({
|
||||
|
|
@ -693,44 +718,24 @@ app.get("/workshop/:structure_id/route/:route_id/logs", (req, res) => {
|
|||
});
|
||||
|
||||
// POST route to handle file upload
|
||||
app.post("/workshop/:structure_id/files", (req, res) => {
|
||||
app.post("/workshop/:structure_id/files", async (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);
|
||||
const targetFile = req.files.file;
|
||||
|
||||
fs.mkdirSync(uploadPath, { recursive: true });
|
||||
|
||||
file.mv(path.join(uploadPath, name), (err) => {
|
||||
try {
|
||||
if (err) {
|
||||
return res.status(500).send(err);
|
||||
}
|
||||
|
||||
let id = model.createFile(
|
||||
db,
|
||||
structure_id,
|
||||
name,
|
||||
storedPath,
|
||||
mime_type,
|
||||
mime_subtype,
|
||||
);
|
||||
|
||||
let file = model.getFile(db, id);
|
||||
file.url = prefixUrlWithHost(req, file.path);
|
||||
|
||||
return res.send(eta.render("workshop/file_detail", { file: file }));
|
||||
} catch (e) {
|
||||
try {
|
||||
const uploadedFile = await saveFile(structure_id, req, targetFile)
|
||||
return res.send(eta.render("workshop/file_detail", { file: uploadedFile }));
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e)
|
||||
res.status(500);
|
||||
return res.send(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
function prefixUrlWithHost(req, path) {
|
||||
|
|
|
|||
2
migrations/003_add_asset_field_to_file.sql
Normal file
2
migrations/003_add_asset_field_to_file.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE files
|
||||
ADD asset INTEGER NOT NULL DEFAULT false;
|
||||
Loading…
Add table
Add a link
Reference in a new issue