Colubrina/backend/routes/mediaproxy.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-06-05 22:58:18 +10:00
const express = require("express")
const router = express.Router()
const Errors = require("../lib/errors.js")
const { Message } = require("../models")
const axios = require("axios")
2023-02-12 17:01:08 +11:00
router.get(["/:mid/:index/:securityToken","/:mid/:index/:securityToken.:extension"], async (req, res,next) => {
2022-06-05 22:58:18 +10:00
try {
const message = await Message.findOne({
where: {
id: req.params.mid
}
})
if (!message) {
throw Errors.invalidParameter("message id")
}
const embed = message.embeds.find(
(e) => e.securityToken === req.params.securityToken
)
if (!embed) {
throw Errors.invalidParameter("securityToken")
}
await axios
.get(embed.link, {
headers: {
"user-agent": "Googlebot/2.1 (+http://www.google.com/bot.html)"
},
responseType: "arraybuffer"
})
.then((response) => {
res.setHeader("content-type", response.headers["content-type"])
res.setHeader("cache-control", "public, max-age=604800")
res.end(response.data, "binary")
})
2022-08-14 22:06:56 +10:00
.catch(() => {
res.status(404).end()
})
2022-06-05 22:58:18 +10:00
} catch (e) {
next(e)
}
})
module.exports = router