2022-03-11 18:48:40 -08:00
|
|
|
import { Router } from 'express'
|
2022-03-21 17:20:41 -07:00
|
|
|
import secretKey from '../lib/middleware/secret-key';
|
|
|
|
import { File } from '../lib/models/File'
|
2022-03-11 18:48:40 -08:00
|
|
|
|
|
|
|
export const files = Router()
|
|
|
|
|
2022-03-21 17:20:41 -07:00
|
|
|
files.get("/raw/:id", secretKey, async (req, res, next) => {
|
2022-03-11 18:48:40 -08:00
|
|
|
try {
|
|
|
|
const file = await File.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.params.id
|
|
|
|
},
|
|
|
|
attributes: ["title", "content"],
|
|
|
|
})
|
2022-03-21 17:42:37 -07:00
|
|
|
|
|
|
|
// TODO: JWT-checkraw files
|
|
|
|
if (file?.post?.visibility === "private") {
|
|
|
|
// jwt(req as UserJwtRequest, res, () => {
|
|
|
|
// res.json(file);
|
|
|
|
// })
|
|
|
|
res.json(file);
|
|
|
|
} else {
|
|
|
|
res.json(file);
|
|
|
|
}
|
2022-03-11 18:48:40 -08:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
next(e);
|
|
|
|
}
|
|
|
|
});
|
2022-03-23 12:36:29 -07:00
|
|
|
|
|
|
|
|
|
|
|
files.get("/html/:id", async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const file = await File.findOne({
|
|
|
|
where: {
|
|
|
|
id: req.params.id
|
|
|
|
},
|
|
|
|
attributes: ["html"],
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return res.status(404).json({ error: "File not found" })
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', 'text/plain')
|
|
|
|
res.setHeader('Cache-Control', 'public, max-age=4800')
|
|
|
|
res.status(200).write(file.html)
|
|
|
|
res.end()
|
|
|
|
} catch (error) {
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
})
|