mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
implemented endpoints for uploading and showing team pictures
This commit is contained in:
69
traque-back/photo.js
Normal file
69
traque-back/photo.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import { app, game } from "./index.js";
|
||||
import multer from "multer";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
const UPLOAD_DIR = "uploads/"
|
||||
const ALLOWED_MIME = [
|
||||
"image/png",
|
||||
"image/jpeg",
|
||||
"image/gif"
|
||||
]
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, callback) {
|
||||
callback(null, UPLOAD_DIR);
|
||||
},
|
||||
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,
|
||||
fileFilter: function (req, file, callback) {
|
||||
if ( ALLOWED_MIME.indexOf(file.mimetype) == -1) {
|
||||
callback(null, false);
|
||||
} else if (!game.getTeam(Number(req.query.team))) {
|
||||
callback(null, false);
|
||||
} else {
|
||||
callback(null, true);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
export function initPhotoUpload() {
|
||||
clean();
|
||||
app.post("/upload", upload.single('file'), (req, res) => {
|
||||
console.log("upload", req.query)
|
||||
res.send("")
|
||||
})
|
||||
app.get("/photo/my", (req,res) => {
|
||||
let team = game.getTeam(Number(req.query.team));
|
||||
if(team) {
|
||||
res.set("Content-Type","image/png")
|
||||
res.sendFile(process.cwd() + "/" + UPLOAD_DIR + "/" + team.id);
|
||||
}else {
|
||||
res.send(400,"Team not found")
|
||||
}
|
||||
})
|
||||
app.get("/photo/enemy", (req,res) => {
|
||||
let team = game.getTeam(Number(req.query.team));
|
||||
if(team) {
|
||||
res.set("Content-Type","image/png")
|
||||
res.sendFile(process.cwd() + "/" + UPLOAD_DIR + "/" + team.chasing);
|
||||
}else {
|
||||
res.send(400,"Team not found")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user