Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
Merge branch 'dev' of github.com:Leon-OnlineLearning/Leon-Serverside …
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
ak-seyam committed Apr 21, 2021
2 parents 9c8b358 + 418b8da commit 3cab21d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ todo
localhost.pem
localhost-key.pem
lectures/
ormconfig.json
ormconfig.json
recordings/
13 changes: 13 additions & 0 deletions controller/BusinessLogic/Event/Exam/exam-logic-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import Exam from "@models/Events/Exam";
import UserInputError from "@services/utils/UserInputError";
import { getRepository } from "typeorm";
import ExamsLogic from "./exam-logic";
import { promises } from 'fs'
const mkdir = promises.mkdir
const appendFile = promises.appendFile
import { join } from 'path'

let upload_folder = process.env['UPLOADED_RECORDING_PATH'] || 'recordings';
export default class ExamsLogicImpl implements ExamsLogic {
async getExamById(examId: string): Promise<Exam> {
const res = await getRepository(Exam).findOne(examId)
Expand Down Expand Up @@ -34,4 +39,12 @@ export default class ExamsLogicImpl implements ExamsLogic {
async createExam(exam: Exam): Promise<Exam> {
return await getRepository(Exam).save(exam)
}
async saveRecording(chunk: Buffer, examId: string, userId: string): Promise<void> {
let video_dir = join(upload_folder, examId)

await mkdir(video_dir, { recursive: true })

// TODO make sure the chunk order is right
await appendFile(join(video_dir, `${userId}.webm`), chunk)
}
}
1 change: 1 addition & 0 deletions controller/BusinessLogic/Event/Exam/exam-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default interface ExamsLogic {
updateExam(examId: string, newData: Exam) : Promise<Exam>;
deleteExamById(examId: string) : Promise<void>;
getExamsByYear(year: number): Promise<Exam[]>;
saveRecording(chunk: Buffer, examId: string, userId: string) : Promise<void>;
}
22 changes: 22 additions & 0 deletions services/Routes/Event/Exam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import ExamParser, { ExamRequest } from "@services/Routes/BodyParserMiddleware/E
import ExamsLogic from "@controller/BusinessLogic/Event/Exam/exam-logic"
import ExamsLogicImpl from "@controller/BusinessLogic/Event/Exam/exam-logic-impl"
import simpleFinalMWDecorator from "@services/utils/RequestDecorator"
import multer from "multer"
import { onlyStudents } from "@services/Routes/User/AuthorizationMiddleware"

const router = Router()

Expand All @@ -13,6 +15,26 @@ router.use(passport.authenticate('access-token', { session: false }))

const parser: BodyParserMiddleware = new ExamParser()

var storage = multer.memoryStorage();
var upload = multer({ storage: storage });

/**
* save exam recording
*
* req body must contain
* - usedId
* - examId
* - chunckIndex : number of current chunk
* - chuck : actual recorded chunk in webm format
// TODO add parser to validate the exam info fields
*/
router.put('/record',onlyStudents, upload.single('chuck'), async (req, res) => {
simpleFinalMWDecorator(res, async () => {
const logic: ExamsLogic = new ExamsLogicImpl()
await logic.saveRecording(req.file.buffer, req.body.examId, req.body.userId);
})
})

router.get('/:examId', async (req, res) => {
simpleFinalMWDecorator(res, async () => {
const logic: ExamsLogic = new ExamsLogicImpl()
Expand Down

0 comments on commit 3cab21d

Please sign in to comment.