From b44f3262b7180132785aeef5f986d5f2ee741b2e Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Sun, 2 Jan 2022 22:19:08 +0300 Subject: [PATCH] feat: added videos controller --- .../Http/Api/v1/LessonsController.ts | 23 -------- app/Controllers/Http/VideosController.ts | 53 +++++++++++++++++++ app/Repositories/LessonRepository.ts | 2 +- app/Services/LessonService.ts | 2 +- routes/apis/v1/lessons.ts | 6 +-- routes/index.ts | 5 ++ routes/videos.ts | 7 +++ 7 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 app/Controllers/Http/VideosController.ts create mode 100644 routes/videos.ts diff --git a/app/Controllers/Http/Api/v1/LessonsController.ts b/app/Controllers/Http/Api/v1/LessonsController.ts index 63320e4..fff5bf6 100644 --- a/app/Controllers/Http/Api/v1/LessonsController.ts +++ b/app/Controllers/Http/Api/v1/LessonsController.ts @@ -11,7 +11,6 @@ import LessonService from 'App/Services/LessonService'; * Models */ import LessonMaterial from 'App/Models/LessonMaterial'; -import LessonVideo from 'App/Models/LessonVideo'; /** * Validators @@ -170,28 +169,6 @@ export default class LessonsController extends BaseController { return this.sendResponse(ctx, data, message, status); } - - /** - * Get video file of lesson by file name - * GET /lessons/video/:fileName - */ - public async getVideo(ctx: HttpContextContract) { - const { fileName } = ctx.request.params(); - const { data, message, status, success, error } = await this.lessonService.fetchVideoFile(ctx, fileName); - - if (!success && error) { - throw new Exception(message, status, error.code); - } - - /** - * Send file to user - */ - if (data instanceof LessonVideo) { - return this.sendFileFromDrive(`videos/${data.name}`, ctx); - } - - return this.sendResponse(ctx, data, message, status); - } } new Ioc().make(LessonsController); diff --git a/app/Controllers/Http/VideosController.ts b/app/Controllers/Http/VideosController.ts new file mode 100644 index 0000000..d4ab112 --- /dev/null +++ b/app/Controllers/Http/VideosController.ts @@ -0,0 +1,53 @@ +import { Exception, inject, Ioc } from '@adonisjs/core/build/standalone'; +import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; + +/** + * Models + */ +import LessonVideo from 'App/Models/LessonVideo'; + +/** + * Services + */ +import LessonService from 'App/Services/LessonService'; + +import BaseController from './BaseController'; + +@inject() +export default class VideosController extends BaseController { + private lessonService: LessonService; + + constructor(lessonService: LessonService) { + super(); + this.lessonService = lessonService; + } + + /** + * Get video file of lesson by file name + * GET /video/:fileName + */ + public async getVideo(ctx: HttpContextContract) { + const { fileName } = ctx.request.params(); + const { data, message, status, success, error } = await this.lessonService.fetchVideoFile(ctx, fileName); + + if (!success && error) { + throw new Exception(message, status, error.code); + } + + if (data instanceof LessonVideo) { + /** + * Allow user to control video + */ + ctx.response.header('Accept-Ranges', 'bytes'); + + /** + * Send video + */ + return this.sendFileFromDrive(`videos/${data.name}`, ctx); + } + + return this.sendResponse(ctx, data, message, status); + } +} + +new Ioc().make(VideosController); diff --git a/app/Repositories/LessonRepository.ts b/app/Repositories/LessonRepository.ts index 7813711..c837f80 100644 --- a/app/Repositories/LessonRepository.ts +++ b/app/Repositories/LessonRepository.ts @@ -123,7 +123,7 @@ export default class LessonRepository { clientName: data.video.clientName, ext: data.video.extname, size: data.video.size, - url: `lessons/video/${videoName}`, + url: `/video/${videoName}`, }); } diff --git a/app/Services/LessonService.ts b/app/Services/LessonService.ts index 002fb10..cf0fe3b 100644 --- a/app/Services/LessonService.ts +++ b/app/Services/LessonService.ts @@ -96,7 +96,7 @@ export default class LessonService { /** * Load lesson with materials */ - await lesson.load(loader => loader.load('content').load('materials')); + await lesson.load(loader => loader.load('content').load('materials').load('video')); return { success: true, diff --git a/routes/apis/v1/lessons.ts b/routes/apis/v1/lessons.ts index 806ef1b..793cb97 100644 --- a/routes/apis/v1/lessons.ts +++ b/routes/apis/v1/lessons.ts @@ -15,9 +15,9 @@ Route.group(() => { Route.get('/materials/:fileName', 'Api/v1/LessonsController.getMaterial') .middleware('role:admin,teacher,student') .as('lessons.get-material'); - Route.get('/video/:fileName', 'Api/v1/LessonsController.getVideo') - .middleware('role:admin,teacher,student') - .as('lessons.get-video'); + // Route.get('/video/:fileName', 'Api/v1/LessonsController.getVideo') + // .middleware('role:admin,teacher,student') + // .as('lessons.get-video'); Route.get('/:id/progress', 'Api/v1/LessonsController.getVideoProgress') .middleware('role:admin,teacher,student') .as('lessons.get-video-progress'); diff --git a/routes/index.ts b/routes/index.ts index d614d40..cfc24ed 100644 --- a/routes/index.ts +++ b/routes/index.ts @@ -26,4 +26,9 @@ Route.group(() => { * Api routes */ require('./api'); + + /** + * Other routes + */ + require('./videos'); }); diff --git a/routes/videos.ts b/routes/videos.ts new file mode 100644 index 0000000..1121eb0 --- /dev/null +++ b/routes/videos.ts @@ -0,0 +1,7 @@ +import Route from '@ioc:Adonis/Core/Route'; + +Route.group(() => { + Route.get('/video/:fileName', 'VideosController.getVideo') + .middleware('role:admin,teacher,student') + .as('videos.get-video'); +}).middleware('auth');