aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/update.ts34
-rw-r--r--server/controllers/api/videos/upload.ts64
2 files changed, 47 insertions, 51 deletions
diff --git a/server/controllers/api/videos/update.ts b/server/controllers/api/videos/update.ts
index 15899307d..2cf8a5883 100644
--- a/server/controllers/api/videos/update.ts
+++ b/server/controllers/api/videos/update.ts
@@ -1,12 +1,12 @@
1import express from 'express' 1import express from 'express'
2import { Transaction } from 'sequelize/types' 2import { Transaction } from 'sequelize/types'
3import { updateTorrentMetadata } from '@server/helpers/webtorrent'
4import { changeVideoChannelShare } from '@server/lib/activitypub/share' 3import { changeVideoChannelShare } from '@server/lib/activitypub/share'
4import { JobQueue } from '@server/lib/job-queue'
5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' 5import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
6import { openapiOperationDoc } from '@server/middlewares/doc' 6import { openapiOperationDoc } from '@server/middlewares/doc'
7import { FilteredModelAttributes } from '@server/types' 7import { FilteredModelAttributes } from '@server/types'
8import { MVideoFullLight } from '@server/types/models' 8import { MVideoFullLight } from '@server/types/models'
9import { HttpStatusCode, VideoUpdate } from '@shared/models' 9import { HttpStatusCode, ManageVideoTorrentPayload, VideoUpdate } from '@shared/models'
10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
11import { resetSequelizeInstance } from '../../../helpers/database-utils' 11import { resetSequelizeInstance } from '../../../helpers/database-utils'
12import { createReqFiles } from '../../../helpers/express-utils' 12import { createReqFiles } from '../../../helpers/express-utils'
@@ -139,15 +139,13 @@ async function updateVideo (req: express.Request, res: express.Response) {
139 return { videoInstanceUpdated, isNewVideo } 139 return { videoInstanceUpdated, isNewVideo }
140 }) 140 })
141 141
142 if (videoInstanceUpdated.isLive !== true && videoInfoToUpdate.name) { 142 const refreshedVideo = await updateTorrentsMetadataIfNeeded(videoInstanceUpdated, videoInfoToUpdate)
143 await updateTorrentsMetadata(videoInstanceUpdated)
144 }
145 143
146 await sequelizeTypescript.transaction(t => federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)) 144 await sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, isNewVideo, t))
147 145
148 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) 146 if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo)
149 147
150 Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res }) 148 Hooks.runAction('action:api.video.updated', { video: refreshedVideo, body: req.body, req, res })
151 } catch (err) { 149 } catch (err) {
152 // Force fields we want to update 150 // Force fields we want to update
153 // If the transaction is retried, sequelize will think the object has not changed 151 // If the transaction is retried, sequelize will think the object has not changed
@@ -194,19 +192,25 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
194 } 192 }
195} 193}
196 194
197async function updateTorrentsMetadata (video: MVideoFullLight) { 195async function updateTorrentsMetadataIfNeeded (video: MVideoFullLight, videoInfoToUpdate: VideoUpdate) {
196 if (video.isLive || !videoInfoToUpdate.name) return video
197
198 for (const file of (video.VideoFiles || [])) { 198 for (const file of (video.VideoFiles || [])) {
199 await updateTorrentMetadata(video, file) 199 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', videoId: video.id, videoFileId: file.id }
200 200
201 await file.save() 201 const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
202 await job.finished()
202 } 203 }
203 204
204 const hls = video.getHLSPlaylist() 205 const hls = video.getHLSPlaylist()
205 if (!hls) return
206 206
207 for (const file of (hls.VideoFiles || [])) { 207 for (const file of (hls?.VideoFiles || [])) {
208 await updateTorrentMetadata(hls, file) 208 const payload: ManageVideoTorrentPayload = { action: 'update-metadata', streamingPlaylistId: hls.id, videoFileId: file.id }
209 209
210 await file.save() 210 const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
211 await job.finished()
211 } 212 }
213
214 // Refresh video since files have changed
215 return VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
212} 216}
diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts
index dd69cf238..14ae9d920 100644
--- a/server/controllers/api/videos/upload.ts
+++ b/server/controllers/api/videos/upload.ts
@@ -2,8 +2,8 @@ import express from 'express'
2import { move } from 'fs-extra' 2import { move } from 'fs-extra'
3import { basename } from 'path' 3import { basename } from 'path'
4import { getResumableUploadPath } from '@server/helpers/upload' 4import { getResumableUploadPath } from '@server/helpers/upload'
5import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
6import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' 5import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6import { JobQueue } from '@server/lib/job-queue'
7import { generateWebTorrentVideoFilename } from '@server/lib/paths' 7import { generateWebTorrentVideoFilename } from '@server/lib/paths'
8import { Redis } from '@server/lib/redis' 8import { Redis } from '@server/lib/redis'
9import { uploadx } from '@server/lib/uploadx' 9import { uploadx } from '@server/lib/uploadx'
@@ -17,10 +17,10 @@ import {
17import { VideoPathManager } from '@server/lib/video-path-manager' 17import { VideoPathManager } from '@server/lib/video-path-manager'
18import { buildNextVideoState } from '@server/lib/video-state' 18import { buildNextVideoState } from '@server/lib/video-state'
19import { openapiOperationDoc } from '@server/middlewares/doc' 19import { openapiOperationDoc } from '@server/middlewares/doc'
20import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' 20import { MVideoFile, MVideoFullLight } from '@server/types/models'
21import { getLowercaseExtension } from '@shared/core-utils' 21import { getLowercaseExtension } from '@shared/core-utils'
22import { isAudioFile, uuidToShort } from '@shared/extra-utils' 22import { isAudioFile, uuidToShort } from '@shared/extra-utils'
23import { HttpStatusCode, VideoCreate, VideoResolution, VideoState } from '@shared/models' 23import { HttpStatusCode, ManageVideoTorrentPayload, VideoCreate, VideoResolution, VideoState } from '@shared/models'
24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' 24import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
25import { retryTransactionWrapper } from '../../../helpers/database-utils' 25import { retryTransactionWrapper } from '../../../helpers/database-utils'
26import { createReqFiles } from '../../../helpers/express-utils' 26import { createReqFiles } from '../../../helpers/express-utils'
@@ -209,17 +209,22 @@ async function addVideo (options: {
209 // Channel has a new content, set as updated 209 // Channel has a new content, set as updated
210 await videoCreated.VideoChannel.setAsUpdated() 210 await videoCreated.VideoChannel.setAsUpdated()
211 211
212 createTorrentFederate(video, videoFile) 212 createTorrentFederate(videoCreated, videoFile)
213 .then(() => { 213 .catch(err => {
214 if (video.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) { 214 logger.error('Cannot create torrent or federate video for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) })
215 return addMoveToObjectStorageJob(video) 215
216 return videoCreated
217 }).then(refreshedVideo => {
218 if (!refreshedVideo) return
219
220 if (refreshedVideo.state === VideoState.TO_MOVE_TO_EXTERNAL_STORAGE) {
221 return addMoveToObjectStorageJob(refreshedVideo)
216 } 222 }
217 223
218 if (video.state === VideoState.TO_TRANSCODE) { 224 if (refreshedVideo.state === VideoState.TO_TRANSCODE) {
219 return addOptimizeOrMergeAudioJob(videoCreated, videoFile, user) 225 return addOptimizeOrMergeAudioJob(refreshedVideo, videoFile, user)
220 } 226 }
221 }) 227 }).catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) }))
222 .catch(err => logger.error('Cannot add optimize/merge audio job for %s.', videoCreated.uuid, { err, ...lTags(videoCreated.uuid) }))
223 228
224 Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res }) 229 Hooks.runAction('action:api.video.uploaded', { video: videoCreated, req, res })
225 230
@@ -254,36 +259,23 @@ async function buildNewFile (videoPhysicalFile: express.VideoUploadFile) {
254 return videoFile 259 return videoFile
255} 260}
256 261
257async function createTorrentAndSetInfoHashAsync (video: MVideo, fileArg: MVideoFile) { 262async function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) {
258 await createTorrentAndSetInfoHash(video, fileArg) 263 const payload: ManageVideoTorrentPayload = { videoId: video.id, videoFileId: videoFile.id, action: 'create' }
259 264
260 // Refresh videoFile because the createTorrentAndSetInfoHash could be long 265 const job = await JobQueue.Instance.createJobWithPromise({ type: 'manage-video-torrent', payload })
261 const refreshedFile = await VideoFileModel.loadWithVideo(fileArg.id) 266 await job.finished()
262 // File does not exist anymore, remove the generated torrent
263 if (!refreshedFile) return fileArg.removeTorrent()
264 267
265 refreshedFile.infoHash = fileArg.infoHash 268 const refreshedVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
266 refreshedFile.torrentFilename = fileArg.torrentFilename 269 if (!refreshedVideo) return
267 270
268 return refreshedFile.save() 271 // Only federate and notify after the torrent creation
269} 272 Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo)
270 273
271function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) { 274 await retryTransactionWrapper(() => {
272 // Create the torrent file in async way because it could be long 275 return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t))
273 return createTorrentAndSetInfoHashAsync(video, videoFile) 276 })
274 .catch(err => logger.error('Cannot create torrent file for video %s', video.url, { err, ...lTags(video.uuid) }))
275 .then(() => VideoModel.loadAndPopulateAccountAndServerAndTags(video.id))
276 .then(refreshedVideo => {
277 if (!refreshedVideo) return
278
279 // Only federate and notify after the torrent creation
280 Notifier.Instance.notifyOnNewVideoIfNeeded(refreshedVideo)
281 277
282 return retryTransactionWrapper(() => { 278 return refreshedVideo
283 return sequelizeTypescript.transaction(t => federateVideoIfNeeded(refreshedVideo, true, t))
284 })
285 })
286 .catch(err => logger.error('Cannot federate or notify video creation %s', video.url, { err, ...lTags(video.uuid) }))
287} 279}
288 280
289async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) { 281async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) {