]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/update.ts
Merge branch 'release/4.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / update.ts
index a0aa13d71cd87d4d0070a71496709be0b7cddefc..15899307de27407f6f43e559da71747079e216c7 100644 (file)
@@ -1,17 +1,16 @@
 import express from 'express'
 import { Transaction } from 'sequelize/types'
+import { updateTorrentMetadata } from '@server/helpers/webtorrent'
 import { changeVideoChannelShare } from '@server/lib/activitypub/share'
 import { buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
 import { openapiOperationDoc } from '@server/middlewares/doc'
 import { FilteredModelAttributes } from '@server/types'
 import { MVideoFullLight } from '@server/types/models'
-import { VideoUpdate } from '../../../../shared'
-import { HttpStatusCode } from '../../../../shared/models'
+import { HttpStatusCode, VideoUpdate } from '@shared/models'
 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
 import { resetSequelizeInstance } from '../../../helpers/database-utils'
 import { createReqFiles } from '../../../helpers/express-utils'
 import { logger, loggerTagsFactory } from '../../../helpers/logger'
-import { CONFIG } from '../../../initializers/config'
 import { MIMETYPES } from '../../../initializers/constants'
 import { sequelizeTypescript } from '../../../initializers/database'
 import { federateVideoIfNeeded } from '../../../lib/activitypub/videos'
@@ -26,14 +25,7 @@ const lTags = loggerTagsFactory('api', 'video')
 const auditLogger = auditLoggerFactory('videos')
 const updateRouter = express.Router()
 
-const reqVideoFileUpdate = createReqFiles(
-  [ 'thumbnailfile', 'previewfile' ],
-  MIMETYPES.IMAGE.MIMETYPE_EXT,
-  {
-    thumbnailfile: CONFIG.STORAGE.TMP_DIR,
-    previewfile: CONFIG.STORAGE.TMP_DIR
-  }
-)
+const reqVideoFileUpdate = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
 
 updateRouter.put('/:id',
   openapiOperationDoc({ operationId: 'putVideo' }),
@@ -51,7 +43,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-export async function updateVideo (req: express.Request, res: express.Response) {
+async function updateVideo (req: express.Request, res: express.Response) {
   const videoFromReq = res.locals.videoAll
   const videoFieldsSave = videoFromReq.toJSON()
   const oldVideoAuditView = new VideoAuditView(videoFromReq.toFormattedDetailsJSON())
@@ -68,7 +60,7 @@ export async function updateVideo (req: express.Request, res: express.Response)
   })
 
   try {
-    const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
+    const { videoInstanceUpdated, isNewVideo } = await sequelizeTypescript.transaction(async t => {
       // Refresh video since thumbnails to prevent concurrent updates
       const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoFromReq.id, t)
 
@@ -137,8 +129,6 @@ export async function updateVideo (req: express.Request, res: express.Response)
         transaction: t
       })
 
-      await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
-
       auditLogger.update(
         getAuditIdFromRes(res),
         new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
@@ -146,14 +136,18 @@ export async function updateVideo (req: express.Request, res: express.Response)
       )
       logger.info('Video with name %s and uuid %s updated.', video.name, video.uuid, lTags(video.uuid))
 
-      return videoInstanceUpdated
+      return { videoInstanceUpdated, isNewVideo }
     })
 
-    if (wasConfidentialVideo) {
-      Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
+    if (videoInstanceUpdated.isLive !== true && videoInfoToUpdate.name) {
+      await updateTorrentsMetadata(videoInstanceUpdated)
     }
 
-    Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body })
+    await sequelizeTypescript.transaction(t => federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t))
+
+    if (wasConfidentialVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated)
+
+    Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body, req, res })
   } catch (err) {
     // Force fields we want to update
     // If the transaction is retried, sequelize will think the object has not changed
@@ -199,3 +193,20 @@ function updateSchedule (videoInstance: MVideoFullLight, videoInfoToUpdate: Vide
     return ScheduleVideoUpdateModel.deleteByVideoId(videoInstance.id, transaction)
   }
 }
+
+async function updateTorrentsMetadata (video: MVideoFullLight) {
+  for (const file of (video.VideoFiles || [])) {
+    await updateTorrentMetadata(video, file)
+
+    await file.save()
+  }
+
+  const hls = video.getHLSPlaylist()
+  if (!hls) return
+
+  for (const file of (hls.VideoFiles || [])) {
+    await updateTorrentMetadata(hls, file)
+
+    await file.save()
+  }
+}