Added the cdn v2

This commit is contained in:
2022-07-19 21:17:59 +02:00
parent 28e8171183
commit 0233a2866b
8 changed files with 3897 additions and 0 deletions

32
ContentDeliveryV2/app.js Normal file
View File

@ -0,0 +1,32 @@
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);