improved documentation

This commit is contained in:
2024-06-10 22:14:24 +00:00
parent 9e595baf4b
commit 303cc83c54
8 changed files with 172 additions and 21 deletions

View File

@@ -1,3 +1,6 @@
/*
This module manages the handler for uploading photos, as well as serving the correct file on requests based on the team ID and current game state
*/
import { app } from "./index.js";
import multer from "multer";
import fs from "fs";
@@ -10,27 +13,21 @@ const ALLOWED_MIME = [
"image/gif"
]
//Setup multer (the file upload middleware)
const storage = multer.diskStorage({
// Save the file in the uploads directory
destination: function (req, file, callback) {
callback(null, UPLOAD_DIR);
},
// Save the file with the team ID as the filename
filename: function (req, file, callback) {
callback(null, req.query.team);
}
});
function clean() {
const files = fs.readdirSync(UPLOAD_DIR);
for (const file of files) {
const filePath = path.join(UPLOAD_DIR, file);
fs.unlinkSync(filePath);
}
}
const upload = multer({
storage,
//Only upload the file if it is a valid mime type and the team POST parameter is a valid team
fileFilter: function (req, file, callback) {
if (ALLOWED_MIME.indexOf(file.mimetype) == -1) {
callback(null, false);
@@ -43,13 +40,25 @@ const upload = multer({
})
//Clean the uploads directory
function clean() {
const files = fs.readdirSync(UPLOAD_DIR);
for (const file of files) {
const filePath = path.join(UPLOAD_DIR, file);
fs.unlinkSync(filePath);
}
}
export function initPhotoUpload() {
clean();
//App handler for uploading a photo and saving it to a file
app.post("/upload", upload.single('file'), (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
console.log("upload", req.query)
res.send("")
})
//App handler for serving the photo of a team given its secret ID
app.get("/photo/my", (req, res) => {
let team = game.getTeam(Number(req.query.team));
if (team) {
@@ -60,6 +69,7 @@ export function initPhotoUpload() {
res.send(400, "Team not found")
}
})
//App handler for serving the photo of the team chased by the team given by its secret ID
app.get("/photo/enemy", (req, res) => {
let team = game.getTeam(Number(req.query.team));
if (team) {