X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fvideos%2Findex.ts;h=4e4697ef4cfc1b24c0edbe8abb3c81592a110a38;hb=6040f87d143a5fa01db79867ece8197c3ce7be47;hp=92c6ee697b77cd206001115e952afefff4f5f2e8;hpb=06215f15e0a9fea2ef95b8b49cb2b5868fb64017;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 92c6ee697..4e4697ef4 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -1,11 +1,10 @@ import * as express from 'express' import { extname, join } from 'path' import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared' -import { renamePromise } from '../../../helpers/core-utils' import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' import { processImage } from '../../../helpers/image-utils' import { logger } from '../../../helpers/logger' -import { auditLoggerFactory, VideoAuditView } from '../../../helpers/audit-logger' +import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' import { getFormattedObjects, getServerActor } from '../../../helpers/utils' import { CONFIG, @@ -32,6 +31,7 @@ import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, + checkVideoFollowConstraints, commonVideosFiltersValidator, optionalAuthenticate, paginationValidator, @@ -50,12 +50,15 @@ import { abuseVideoRouter } from './abuse' import { blacklistRouter } from './blacklist' import { videoCommentRouter } from './comment' import { rateVideoRouter } from './rate' +import { ownershipVideoRouter } from './ownership' import { VideoFilter } from '../../../../shared/models/videos/video-query.type' import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' import { videoCaptionsRouter } from './captions' import { videoImportsRouter } from './import' import { resetSequelizeInstance } from '../../../helpers/database-utils' +import { rename } from 'fs-extra' +import { watchingRouter } from './watching' const auditLogger = auditLoggerFactory('videos') const videosRouter = express.Router() @@ -64,17 +67,17 @@ const reqVideoFileAdd = createReqFiles( [ 'videofile', 'thumbnailfile', 'previewfile' ], Object.assign({}, VIDEO_MIMETYPE_EXT, IMAGE_MIMETYPE_EXT), { - videofile: CONFIG.STORAGE.VIDEOS_DIR, - thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR, - previewfile: CONFIG.STORAGE.PREVIEWS_DIR + videofile: CONFIG.STORAGE.TMP_DIR, + thumbnailfile: CONFIG.STORAGE.TMP_DIR, + previewfile: CONFIG.STORAGE.TMP_DIR } ) const reqVideoFileUpdate = createReqFiles( [ 'thumbnailfile', 'previewfile' ], IMAGE_MIMETYPE_EXT, { - thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR, - previewfile: CONFIG.STORAGE.PREVIEWS_DIR + thumbnailfile: CONFIG.STORAGE.TMP_DIR, + previewfile: CONFIG.STORAGE.TMP_DIR } ) @@ -84,6 +87,8 @@ videosRouter.use('/', rateVideoRouter) videosRouter.use('/', videoCommentRouter) videosRouter.use('/', videoCaptionsRouter) videosRouter.use('/', videoImportsRouter) +videosRouter.use('/', ownershipVideoRouter) +videosRouter.use('/', watchingRouter) videosRouter.get('/categories', listVideoCategories) videosRouter.get('/licences', listVideoLicences) @@ -117,7 +122,9 @@ videosRouter.get('/:id/description', asyncMiddleware(getVideoDescription) ) videosRouter.get('/:id', + optionalAuthenticate, asyncMiddleware(videosGetValidator), + asyncMiddleware(checkVideoFollowConstraints), getVideo ) videosRouter.post('/:id/views', @@ -156,6 +163,13 @@ function listVideoPrivacies (req: express.Request, res: express.Response) { } async function addVideo (req: express.Request, res: express.Response) { + // Processing the video could be long + // Set timeout to 10 minutes + req.setTimeout(1000 * 60 * 10, () => { + logger.error('Upload video has timed out.') + return res.sendStatus(408) + }) + const videoPhysicalFile = req.files['videofile'][0] const videoInfo: VideoCreate = req.body @@ -194,7 +208,7 @@ async function addVideo (req: express.Request, res: express.Response) { // Move physical file const videoDir = CONFIG.STORAGE.VIDEOS_DIR const destination = join(videoDir, video.getVideoFilename(videoFile)) - await renamePromise(videoPhysicalFile.path, destination) + await rename(videoPhysicalFile.path, destination) // This is important in case if there is another attempt in the retry process videoPhysicalFile.filename = video.getVideoFilename(videoFile) videoPhysicalFile.path = destination @@ -251,7 +265,7 @@ async function addVideo (req: express.Request, res: express.Response) { await federateVideoIfNeeded(video, true, t) - auditLogger.create(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) + auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) return videoCreated @@ -352,7 +366,7 @@ async function updateVideo (req: express.Request, res: express.Response) { await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) auditLogger.update( - res.locals.oauth.token.User.Account.Actor.getIdentifier(), + getAuditIdFromRes(res), new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), oldVideoAuditView ) @@ -373,6 +387,11 @@ async function updateVideo (req: express.Request, res: express.Response) { function getVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video + if (videoInstance.isOutdated()) { + JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', videoUrl: videoInstance.url } }) + .catch(err => logger.error('Cannot create AP refresher job for video %s.', videoInstance.url, { err })) + } + return res.json(videoInstance.toFormattedDetailsJSON()) } @@ -380,18 +399,19 @@ async function viewVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video const ip = req.ip - const exists = await Redis.Instance.isViewExists(ip, videoInstance.uuid) + const exists = await Redis.Instance.isVideoIPViewExists(ip, videoInstance.uuid) if (exists) { logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid) return res.status(204).end() } - await videoInstance.increment('views') - await Redis.Instance.setView(ip, videoInstance.uuid) - - const serverAccount = await getServerActor() + await Promise.all([ + Redis.Instance.addVideoView(videoInstance.id), + Redis.Instance.setIPVideoView(ip, videoInstance.uuid) + ]) - await sendCreateView(serverAccount, videoInstance, undefined) + const serverActor = await getServerActor() + await sendCreateView(serverActor, videoInstance, undefined) return res.status(204).end() } @@ -409,11 +429,12 @@ async function getVideoDescription (req: express.Request, res: express.Response) return res.json({ description }) } -async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { +async function listVideos (req: express.Request, res: express.Response) { const resultList = await VideoModel.listForApi({ start: req.query.start, count: req.query.count, sort: req.query.sort, + includeLocalVideos: true, categoryOneOf: req.query.categoryOneOf, licenceOneOf: req.query.licenceOneOf, languageOneOf: req.query.languageOneOf, @@ -421,7 +442,8 @@ async function listVideos (req: express.Request, res: express.Response, next: ex tagsAllOf: req.query.tagsAllOf, nsfw: buildNSFWFilter(res, req.query.nsfw), filter: req.query.filter as VideoFilter, - withFiles: false + withFiles: false, + user: res.locals.oauth ? res.locals.oauth.token.User : undefined }) return res.json(getFormattedObjects(resultList.data, resultList.total)) @@ -434,7 +456,7 @@ async function removeVideo (req: express.Request, res: express.Response) { await videoInstance.destroy({ transaction: t }) }) - auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) + auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) return res.type('json').status(204).end()