2024-03-15 15:47:18 +01:00
|
|
|
import { buildAspectRatio } from '@peertube/peertube-core-utils'
|
2024-03-26 14:05:19 +01:00
|
|
|
import { HttpStatusCode, UserRight, VideoState } from '@peertube/peertube-models'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
|
|
|
import { CreateJobArgument, CreateJobOptions, JobQueue } from '@server/lib/job-queue/index.js'
|
|
|
|
import { Hooks } from '@server/lib/plugins/hooks.js'
|
|
|
|
import { regenerateMiniaturesIfNeeded } from '@server/lib/thumbnail.js'
|
2024-02-14 10:20:02 +01:00
|
|
|
import { setupUploadResumableRoutes } from '@server/lib/uploadx.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist.js'
|
2024-03-15 15:47:18 +01:00
|
|
|
import { buildNewFile, createVideoSource } from '@server/lib/video-file.js'
|
|
|
|
import { buildMoveJob, buildStoryboardJobIfNeeded } from '@server/lib/video-jobs.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { VideoPathManager } from '@server/lib/video-path-manager.js'
|
|
|
|
import { buildNextVideoState } from '@server/lib/video-state.js'
|
|
|
|
import { openapiOperationDoc } from '@server/middlewares/doc.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { MStreamingPlaylistFiles, MVideo, MVideoFile, MVideoFullLight } from '@server/types/models/index.js'
|
2024-03-15 15:47:18 +01:00
|
|
|
import express from 'express'
|
|
|
|
import { move } from 'fs-extra/esm'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { logger, loggerTagsFactory } from '../../../helpers/logger.js'
|
2023-07-19 16:02:49 +02:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
2024-03-26 14:05:19 +01:00
|
|
|
ensureUserHasRight,
|
2023-07-19 16:02:49 +02:00
|
|
|
replaceVideoSourceResumableInitValidator,
|
|
|
|
replaceVideoSourceResumableValidator,
|
|
|
|
videoSourceGetLatestValidator
|
2023-07-31 14:34:36 +02:00
|
|
|
} from '../../../middlewares/index.js'
|
2023-07-19 16:02:49 +02:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('api', 'video')
|
|
|
|
|
|
|
|
const videoSourceRouter = express.Router()
|
|
|
|
|
|
|
|
videoSourceRouter.get('/:id/source',
|
|
|
|
openapiOperationDoc({ operationId: 'getVideoSource' }),
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(videoSourceGetLatestValidator),
|
|
|
|
getVideoLatestSource
|
|
|
|
)
|
|
|
|
|
2024-03-26 14:05:19 +01:00
|
|
|
videoSourceRouter.delete('/:id/source/file',
|
|
|
|
openapiOperationDoc({ operationId: 'deleteVideoSourceFile' }),
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_VIDEO_FILES),
|
|
|
|
asyncMiddleware(videoSourceGetLatestValidator),
|
|
|
|
asyncMiddleware(deleteVideoLatestSourceFile)
|
|
|
|
)
|
|
|
|
|
2024-02-14 10:20:02 +01:00
|
|
|
setupUploadResumableRoutes({
|
|
|
|
routePath: '/:id/source/replace-resumable',
|
|
|
|
router: videoSourceRouter,
|
2023-07-19 16:02:49 +02:00
|
|
|
|
2024-02-14 10:20:02 +01:00
|
|
|
uploadInitAfterMiddlewares: [ asyncMiddleware(replaceVideoSourceResumableInitValidator) ],
|
|
|
|
uploadedMiddlewares: [ asyncMiddleware(replaceVideoSourceResumableValidator) ],
|
|
|
|
uploadedController: asyncMiddleware(replaceVideoSourceResumable)
|
|
|
|
})
|
2023-07-19 16:02:49 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoSourceRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-03-26 14:05:19 +01:00
|
|
|
async function deleteVideoLatestSourceFile (req: express.Request, res: express.Response) {
|
|
|
|
const videoSource = res.locals.videoSource
|
|
|
|
const video = res.locals.videoAll
|
|
|
|
|
|
|
|
await video.removeOriginalFile(videoSource)
|
|
|
|
|
|
|
|
videoSource.keptOriginalFilename = null
|
2024-04-22 11:48:05 +02:00
|
|
|
videoSource.storage = null
|
|
|
|
|
2024-03-26 14:05:19 +01:00
|
|
|
await videoSource.save()
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:02:49 +02:00
|
|
|
function getVideoLatestSource (req: express.Request, res: express.Response) {
|
|
|
|
return res.json(res.locals.videoSource.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async function replaceVideoSourceResumable (req: express.Request, res: express.Response) {
|
|
|
|
const videoPhysicalFile = res.locals.updateVideoFileResumable
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
const videoFile = await buildNewFile({ path: videoPhysicalFile.path, mode: 'web-video', ffprobe: res.locals.ffprobe })
|
2023-07-19 16:02:49 +02:00
|
|
|
const originalFilename = videoPhysicalFile.originalname
|
|
|
|
|
|
|
|
const videoFileMutexReleaser = await VideoPathManager.Instance.lockFiles(res.locals.videoAll.uuid)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const destination = VideoPathManager.Instance.getFSVideoFileOutputPath(res.locals.videoAll, videoFile)
|
|
|
|
await move(videoPhysicalFile.path, destination)
|
|
|
|
|
|
|
|
let oldWebVideoFiles: MVideoFile[] = []
|
|
|
|
let oldStreamingPlaylists: MStreamingPlaylistFiles[] = []
|
|
|
|
|
|
|
|
const inputFileUpdatedAt = new Date()
|
|
|
|
|
|
|
|
const video = await sequelizeTypescript.transaction(async transaction => {
|
|
|
|
const video = await VideoModel.loadFull(res.locals.videoAll.id, transaction)
|
|
|
|
|
|
|
|
oldWebVideoFiles = video.VideoFiles
|
|
|
|
oldStreamingPlaylists = video.VideoStreamingPlaylists
|
|
|
|
|
|
|
|
for (const file of video.VideoFiles) {
|
|
|
|
await file.destroy({ transaction })
|
|
|
|
}
|
|
|
|
for (const playlist of oldStreamingPlaylists) {
|
|
|
|
await playlist.destroy({ transaction })
|
|
|
|
}
|
|
|
|
|
|
|
|
videoFile.videoId = video.id
|
|
|
|
await videoFile.save({ transaction })
|
|
|
|
|
|
|
|
video.VideoFiles = [ videoFile ]
|
|
|
|
video.VideoStreamingPlaylists = []
|
|
|
|
|
|
|
|
video.state = buildNextVideoState()
|
|
|
|
video.duration = videoPhysicalFile.duration
|
|
|
|
video.inputFileUpdatedAt = inputFileUpdatedAt
|
2024-02-27 11:18:56 +01:00
|
|
|
video.aspectRatio = buildAspectRatio({ width: videoFile.width, height: videoFile.height })
|
2023-07-19 16:02:49 +02:00
|
|
|
await video.save({ transaction })
|
|
|
|
|
|
|
|
await autoBlacklistVideoIfNeeded({
|
|
|
|
video,
|
|
|
|
user,
|
|
|
|
isRemote: false,
|
|
|
|
isNew: false,
|
|
|
|
isNewFile: true,
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
return video
|
|
|
|
})
|
|
|
|
|
|
|
|
await removeOldFiles({ video, files: oldWebVideoFiles, playlists: oldStreamingPlaylists })
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
const source = await createVideoSource({
|
|
|
|
inputFilename: originalFilename,
|
|
|
|
inputProbe: res.locals.ffprobe,
|
|
|
|
inputPath: destination,
|
|
|
|
video,
|
2023-07-19 16:02:49 +02:00
|
|
|
createdAt: inputFileUpdatedAt
|
|
|
|
})
|
|
|
|
|
2024-03-15 15:47:18 +01:00
|
|
|
await regenerateMiniaturesIfNeeded(video, res.locals.ffprobe)
|
2023-07-19 16:02:49 +02:00
|
|
|
await video.VideoChannel.setAsUpdated()
|
|
|
|
await addVideoJobsAfterUpload(video, video.getMaxQualityFile())
|
|
|
|
|
|
|
|
logger.info('Replaced video file of video %s with uuid %s.', video.name, video.uuid, lTags(video.uuid))
|
|
|
|
|
|
|
|
Hooks.runAction('action:api.video.file-updated', { video, req, res })
|
|
|
|
|
2023-07-21 17:46:37 +02:00
|
|
|
return res.json(source.toFormattedJSON())
|
2023-07-19 16:02:49 +02:00
|
|
|
} finally {
|
|
|
|
videoFileMutexReleaser()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoJobsAfterUpload (video: MVideoFullLight, videoFile: MVideoFile) {
|
|
|
|
const jobs: (CreateJobArgument & CreateJobOptions)[] = [
|
|
|
|
{
|
|
|
|
type: 'manage-video-torrent' as 'manage-video-torrent',
|
|
|
|
payload: {
|
|
|
|
videoId: video.id,
|
|
|
|
videoFileId: videoFile.id,
|
|
|
|
action: 'create'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-12-27 10:39:09 +01:00
|
|
|
buildStoryboardJobIfNeeded({ video, federate: false }),
|
2023-07-19 16:02:49 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
type: 'federate-video' as 'federate-video',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
2023-12-15 14:52:12 +01:00
|
|
|
isNewVideoForFederation: false
|
2023-07-19 16:02:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
|
2023-10-31 12:15:40 +01:00
|
|
|
jobs.push(await buildMoveJob({ video, isNewVideo: false, previousVideoState: undefined, type: 'move-to-object-storage' }))
|
2023-07-19 16:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (video.state === VideoState.TO_TRANSCODE) {
|
|
|
|
jobs.push({
|
|
|
|
type: 'transcoding-job-builder' as 'transcoding-job-builder',
|
|
|
|
payload: {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
optimizeJob: {
|
|
|
|
isNewVideo: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return JobQueue.Instance.createSequentialJobFlow(...jobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeOldFiles (options: {
|
|
|
|
video: MVideo
|
|
|
|
files: MVideoFile[]
|
|
|
|
playlists: MStreamingPlaylistFiles[]
|
|
|
|
}) {
|
|
|
|
const { video, files, playlists } = options
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
await video.removeWebVideoFile(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const playlist of playlists) {
|
|
|
|
await video.removeStreamingPlaylistFiles(playlist)
|
|
|
|
}
|
|
|
|
}
|