partey_workadventure/back/src/Controller/FileController.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

import {Application, Request, RequestHandler, Response} from "express";
2020-09-20 17:12:27 +02:00
import {OK} from "http-status-codes";
import {URL_ROOM_STARTED} from "_Enum/EnvironmentVariable";
import {uuid} from "uuidv4";
import multer from 'multer';
import fs from "fs";
const upload = multer({ dest: 'dist/files/' });
2020-09-20 17:12:27 +02:00
export class FileController {
App : Application;
constructor(App : Application) {
this.App = App;
this.uploadAudioMessage();
this.downloadAudioMessage();
}
uploadAudioMessage(){
this.App.post("/upload-audio-message", (upload.single('file') as RequestHandler), (req: Request, res: Response) => {
2020-09-20 17:12:27 +02:00
//TODO check user connected and admin role
//TODO upload audio message
const audioMessageId = uuid();
fs.copyFileSync(req.file.path, `dist/files/${audioMessageId}`);
fs.unlinkSync(req.file.path);
2020-09-20 17:12:27 +02:00
return res.status(OK).send({
id: audioMessageId,
path: `/download-audio-message/${audioMessageId}`
2020-09-20 17:12:27 +02:00
});
});
}
downloadAudioMessage(){
this.App.get("/download-audio-message/:id", (req: Request, res: Response) => {
2020-09-20 17:12:27 +02:00
//TODO check user connected and admin role
//TODO upload audio message
const audiMessageId = req.params.id;
2020-09-20 17:12:27 +02:00
const fs = require('fs');
const path = `dist/files/${audiMessageId}`;
const file = fs.createReadStream(path);
2020-09-20 17:12:27 +02:00
res.writeHead(200);
file.pipe(res);
});
}
}