mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 10:19:20 +03:00
feat: added videos controller
This commit is contained in:
parent
cd4eb2513c
commit
b44f3262b7
@ -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);
|
||||
|
||||
53
app/Controllers/Http/VideosController.ts
Normal file
53
app/Controllers/Http/VideosController.ts
Normal 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);
|
||||
@ -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}`,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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');
|
||||
|
||||
@ -26,4 +26,9 @@ Route.group(() => {
|
||||
* Api routes
|
||||
*/
|
||||
require('./api');
|
||||
|
||||
/**
|
||||
* Other routes
|
||||
*/
|
||||
require('./videos');
|
||||
});
|
||||
|
||||
7
routes/videos.ts
Normal file
7
routes/videos.ts
Normal 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');
|
||||
Loading…
x
Reference in New Issue
Block a user