mirror of
https://github.com/Troplo/Colubrina.git
synced 2024-11-23 03:36:42 +11:00
35 lines
858 B
JavaScript
35 lines
858 B
JavaScript
const express = require("express")
|
|
const router = express.Router()
|
|
const Errors = require("../lib/errors.js")
|
|
const { Attachment } = require("../models")
|
|
const path = require("path")
|
|
|
|
router.get("/:attachment", async (req, res, next) => {
|
|
try {
|
|
const attachment = await Attachment.findOne({
|
|
where: {
|
|
attachment: req.params.attachment
|
|
}
|
|
})
|
|
if (!attachment) {
|
|
throw Errors.attachmentNotFound
|
|
} else {
|
|
if (
|
|
[".jpg", ".jpeg", ".png", ".gif"].includes(
|
|
path.extname(attachment.attachment)
|
|
)
|
|
) {
|
|
res.sendFile(path.resolve("usercontent/" + attachment.attachment))
|
|
} else {
|
|
res.download(
|
|
path.resolve("usercontent/" + attachment.attachment),
|
|
attachment.name
|
|
)
|
|
}
|
|
}
|
|
} catch (e) {
|
|
next(e)
|
|
}
|
|
})
|
|
|
|
module.exports = router
|