partey_workadventure/back/src/Controller/FileController.ts

59 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-09-23 18:28:12 +02:00
const multer = require('multer');
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 fs from "fs";
const upload = multer({ dest: 'dist/files/' });
2020-09-20 17:12:27 +02:00
2020-09-23 18:28:12 +02:00
class FileUpload{
path: string
constructor(path : string) {
this.path = path;
}
}
interface RequestFileHandlerInterface extends Request{
file: FileUpload
}
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();
2020-09-23 18:28:12 +02:00
fs.copyFileSync((req as RequestFileHandlerInterface).file.path, `dist/files/${audioMessageId}`);
fs.unlinkSync((req as RequestFileHandlerInterface).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);
});
}
}