]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-caption.ts
Generate a name for caption files
[github/Chocobozzz/PeerTube.git] / server / models / video / video-caption.ts
index e8e883dd012a7a7cfd177b07dcaf139ccec13199..a1553ea156ca6a1b458e8fc444b5b1e1fbe75e10 100644 (file)
@@ -16,7 +16,7 @@ import {
   UpdatedAt
 } from 'sequelize-typescript'
 import { buildRemoteVideoBaseUrl } from '@server/helpers/activitypub'
-import { MVideoAccountLight, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
+import { MVideoAccountLight, MVideoCaption, MVideoCaptionFormattable, MVideoCaptionVideo } from '@server/types/models'
 import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
 import { logger } from '../../helpers/logger'
@@ -24,6 +24,7 @@ import { CONFIG } from '../../initializers/config'
 import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants'
 import { buildWhereIdOrUUID, throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
+import { v4 as uuidv4 } from 'uuid'
 
 export enum ScopeNames {
   WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
@@ -44,6 +45,10 @@ export enum ScopeNames {
 @Table({
   tableName: 'videoCaption',
   indexes: [
+    {
+      fields: [ 'filename' ],
+      unique: true
+    },
     {
       fields: [ 'videoId' ]
     },
@@ -65,6 +70,10 @@ export class VideoCaptionModel extends Model {
   @Column
   language: string
 
+  @AllowNull(false)
+  @Column
+  filename: string
+
   @AllowNull(true)
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
   fileUrl: string
@@ -88,12 +97,12 @@ export class VideoCaptionModel extends Model {
     }
 
     if (instance.isOwned()) {
-      logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
+      logger.info('Removing caption %s.', instance.filename)
 
       try {
         await instance.removeCaptionFile()
       } catch (err) {
-        logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
+        logger.error('Cannot remove caption file %s.', instance.filename)
       }
     }
 
@@ -119,15 +128,28 @@ export class VideoCaptionModel extends Model {
     return VideoCaptionModel.findOne(query)
   }
 
-  static insertOrReplaceLanguage (videoId: number, language: string, fileUrl: string, transaction: Transaction) {
-    const values = {
-      videoId,
-      language,
-      fileUrl
+  static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
+    const query = {
+      where: {
+        filename
+      },
+      include: [
+        {
+          model: VideoModel.unscoped(),
+          attributes: [ 'id', 'remote', 'uuid' ]
+        }
+      ]
     }
 
-    return VideoCaptionModel.upsert(values, { transaction, returning: true })
-      .then(([ caption ]) => caption)
+    return VideoCaptionModel.findOne(query)
+  }
+
+  static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
+    const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language)
+    // Delete existing file
+    if (existing) await existing.destroy({ transaction })
+
+    return caption.save({ transaction })
   }
 
   static listVideoCaptions (videoId: number): Promise<MVideoCaptionVideo[]> {
@@ -156,6 +178,10 @@ export class VideoCaptionModel extends Model {
     return VideoCaptionModel.destroy(query)
   }
 
+  static generateCaptionName (language: string) {
+    return `${uuidv4()}-${language}.vtt`
+  }
+
   isOwned () {
     return this.Video.remote === false
   }
@@ -170,16 +196,12 @@ export class VideoCaptionModel extends Model {
     }
   }
 
-  getCaptionStaticPath (this: MVideoCaptionFormattable) {
-    return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
-  }
-
-  getCaptionName (this: MVideoCaptionFormattable) {
-    return `${this.Video.uuid}-${this.language}.vtt`
+  getCaptionStaticPath (this: MVideoCaption) {
+    return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
   }
 
-  removeCaptionFile (this: MVideoCaptionFormattable) {
-    return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
+  removeCaptionFile (this: MVideoCaption) {
+    return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.filename)
   }
 
   getFileUrl (video: MVideoAccountLight) {