2020-09-23 18:28:12 +02:00
|
|
|
const multer = require('multer');
|
2020-09-20 19:01:21 +02:00
|
|
|
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";
|
2020-09-20 19:01:21 +02:00
|
|
|
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(){
|
2020-09-20 19:01:21 +02:00
|
|
|
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-20 19:01:21 +02:00
|
|
|
|
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 19:01:21 +02:00
|
|
|
|
2020-09-20 17:12:27 +02:00
|
|
|
return res.status(OK).send({
|
|
|
|
id: audioMessageId,
|
2020-09-20 19:01:21 +02:00
|
|
|
path: `/download-audio-message/${audioMessageId}`
|
2020-09-20 17:12:27 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadAudioMessage(){
|
2020-09-20 19:01:21 +02:00
|
|
|
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
|
2020-09-20 19:01:21 +02:00
|
|
|
const audiMessageId = req.params.id;
|
2020-09-20 17:12:27 +02:00
|
|
|
|
2020-09-20 19:01:21 +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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|