feat: added videos controller

This commit is contained in:
Sergey Yarkov 2022-01-02 22:19:08 +03:00
parent cd4eb2513c
commit b44f3262b7
7 changed files with 70 additions and 28 deletions

View File

@ -11,7 +11,6 @@ import LessonService from 'App/Services/LessonService';
* Models * Models
*/ */
import LessonMaterial from 'App/Models/LessonMaterial'; import LessonMaterial from 'App/Models/LessonMaterial';
import LessonVideo from 'App/Models/LessonVideo';
/** /**
* Validators * Validators
@ -170,28 +169,6 @@ export default class LessonsController extends BaseController {
return this.sendResponse(ctx, data, message, status); 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); new Ioc().make(LessonsController);

View File

@ -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);

View File

@ -123,7 +123,7 @@ export default class LessonRepository {
clientName: data.video.clientName, clientName: data.video.clientName,
ext: data.video.extname, ext: data.video.extname,
size: data.video.size, size: data.video.size,
url: `lessons/video/${videoName}`, url: `/video/${videoName}`,
}); });
} }

View File

@ -96,7 +96,7 @@ export default class LessonService {
/** /**
* Load lesson with materials * 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 { return {
success: true, success: true,

View File

@ -15,9 +15,9 @@ Route.group(() => {
Route.get('/materials/:fileName', 'Api/v1/LessonsController.getMaterial') Route.get('/materials/:fileName', 'Api/v1/LessonsController.getMaterial')
.middleware('role:admin,teacher,student') .middleware('role:admin,teacher,student')
.as('lessons.get-material'); .as('lessons.get-material');
Route.get('/video/:fileName', 'Api/v1/LessonsController.getVideo') // Route.get('/video/:fileName', 'Api/v1/LessonsController.getVideo')
.middleware('role:admin,teacher,student') // .middleware('role:admin,teacher,student')
.as('lessons.get-video'); // .as('lessons.get-video');
Route.get('/:id/progress', 'Api/v1/LessonsController.getVideoProgress') Route.get('/:id/progress', 'Api/v1/LessonsController.getVideoProgress')
.middleware('role:admin,teacher,student') .middleware('role:admin,teacher,student')
.as('lessons.get-video-progress'); .as('lessons.get-video-progress');

View File

@ -26,4 +26,9 @@ Route.group(() => {
* Api routes * Api routes
*/ */
require('./api'); require('./api');
/**
* Other routes
*/
require('./videos');
}); });

7
routes/videos.ts Normal file
View File

@ -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');