This repository has been archived on 2024-12-31. You can view files and clone it, but cannot push or open issues or pull requests.
2022-07-19 21:17:59 +02:00

32 lines
1.3 KiB
JavaScript

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const mime = require('mime');
const database = require("./config/database");
const media = require("./models/Media");
const app = express();
database.authenticate()
.then(() => console.log("Connected to " + process.env.MYSQL_HOSTNAME + " with user " + process.env.MYSQL_USERNAME))
.catch(err => console.log("An error occurred while connecting to database: " + err));
app.use(cors());
app.get("/:resourceID", (req, res) => {
const resource = req.params.resourceID.split(".")
if (resource.length < 2)
return res.status(400).json({message: "The specified URL is not in the correct format"});
media.findOne({where: {assetID: resource[0], assetEnding: resource[resource.length - 1]}})
.then(r => {
require('fs').readFile(process.env.CDN_MEDIA_PATH+"/"+r.assetID, (err, data) =>
res.header("Content-Type", mime.lookup(r.assetEnding)).send(data)
);
})
.catch(() => res.status(404).json({message: "The specified file was not found."}));
});
app.get('*', (req, res) => {
res.status(400).json({message: "The specified URL is not in the correct format"});
});
app.listen(process.env.SERVER_PORT || 3000);