X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Fcaptions.ts;h=2b511a39834c8380b59d043f9b2a2ad9b087f59a;hb=bd911b54b555b11df7e9849cf92d358bccfecf6e;hp=3ba9181891e008013c5d2e2d7901f63fa2c2c077;hpb=be0f59b4eec3c2c4dcd151e2b174be39dff1568e;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts index 3ba918189..2b511a398 100644 --- a/server/controllers/api/videos/captions.ts +++ b/server/controllers/api/videos/captions.ts @@ -1,22 +1,19 @@ -import * as express from 'express' -import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' -import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' +import express from 'express' +import { Hooks } from '@server/lib/plugins/hooks' +import { MVideoCaption } from '@server/types/models' +import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' +import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' import { createReqFiles } from '../../../helpers/express-utils' -import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers' +import { logger } from '../../../helpers/logger' import { getFormattedObjects } from '../../../helpers/utils' +import { MIMETYPES } from '../../../initializers/constants' +import { sequelizeTypescript } from '../../../initializers/database' +import { federateVideoIfNeeded } from '../../../lib/activitypub/videos' +import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' +import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' import { VideoCaptionModel } from '../../../models/video/video-caption' -import { VideoModel } from '../../../models/video/video' -import { logger } from '../../../helpers/logger' -import { federateVideoIfNeeded } from '../../../lib/activitypub' -import { moveAndProcessCaptionFile } from '../../../helpers/captions-utils' -const reqVideoCaptionAdd = createReqFiles( - [ 'captionfile' ], - VIDEO_CAPTIONS_MIMETYPE_EXT, - { - captionfile: CONFIG.STORAGE.CAPTIONS_DIR - } -) +const reqVideoCaptionAdd = createReqFiles([ 'captionfile' ], MIMETYPES.VIDEO_CAPTIONS.MIMETYPE_EXT) const videoCaptionsRouter = express.Router() @@ -45,37 +42,41 @@ export { // --------------------------------------------------------------------------- async function listVideoCaptions (req: express.Request, res: express.Response) { - const data = await VideoCaptionModel.listVideoCaptions(res.locals.video.id) + const data = await VideoCaptionModel.listVideoCaptions(res.locals.onlyVideo.id) return res.json(getFormattedObjects(data, data.length)) } async function addVideoCaption (req: express.Request, res: express.Response) { const videoCaptionPhysicalFile = req.files['captionfile'][0] - const video = res.locals.video as VideoModel + const video = res.locals.videoAll + + const captionLanguage = req.params.captionLanguage const videoCaption = new VideoCaptionModel({ videoId: video.id, - language: req.params.captionLanguage - }) - videoCaption.Video = video + filename: VideoCaptionModel.generateCaptionName(captionLanguage), + language: captionLanguage + }) as MVideoCaption // Move physical file await moveAndProcessCaptionFile(videoCaptionPhysicalFile, videoCaption) await sequelizeTypescript.transaction(async t => { - await VideoCaptionModel.insertOrReplaceLanguage(video.id, req.params.captionLanguage, t) + await VideoCaptionModel.insertOrReplaceLanguage(videoCaption, t) // Update video update await federateVideoIfNeeded(video, false, t) }) - return res.status(204).end() + Hooks.runAction('action:api.video-caption.created', { caption: videoCaption, req, res }) + + return res.status(HttpStatusCode.NO_CONTENT_204).end() } async function deleteVideoCaption (req: express.Request, res: express.Response) { - const video = res.locals.video as VideoModel - const videoCaption = res.locals.videoCaption as VideoCaptionModel + const video = res.locals.videoAll + const videoCaption = res.locals.videoCaption await sequelizeTypescript.transaction(async t => { await videoCaption.destroy({ transaction: t }) @@ -86,5 +87,7 @@ async function deleteVideoCaption (req: express.Request, res: express.Response) logger.info('Video caption %s of video %s deleted.', videoCaption.language, video.uuid) - return res.type('json').status(204).end() + Hooks.runAction('action:api.video-caption.deleted', { caption: videoCaption, req, res }) + + return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end() }