]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-caption.ts
Playlist server API
[github/Chocobozzz/PeerTube.git] / server / models / video / video-caption.ts
index 9920dfc7c42816415bbd031033c6dc4d7dfffffd..b4f17b481c8320dcc1e42ec6b801cf609621e47f 100644 (file)
@@ -15,11 +15,11 @@ import {
 import { throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
 import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
-import { VideoCaption } from '../../../shared/models/videos/video-caption.model'
+import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
 import { CONFIG, STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers'
 import { join } from 'path'
 import { logger } from '../../helpers/logger'
-import { unlinkPromise } from '../../helpers/core-utils'
+import { remove } from 'fs-extra'
 
 export enum ScopeNames {
   WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
@@ -75,14 +75,18 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> {
 
   @BeforeDestroy
   static async removeFiles (instance: VideoCaptionModel) {
+    if (!instance.Video) {
+      instance.Video = await instance.$get('Video') as VideoModel
+    }
 
     if (instance.isOwned()) {
-      if (!instance.Video) {
-        instance.Video = await instance.$get('Video') as VideoModel
-      }
+      logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
 
-      logger.debug('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
-      return instance.removeCaptionFile()
+      try {
+        await instance.removeCaptionFile()
+      } catch (err) {
+        logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
+      }
     }
 
     return undefined
@@ -116,7 +120,8 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> {
       language
     }
 
-    return VideoCaptionModel.upsert(values, { transaction })
+    return VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true })
+      .then(([ caption ]) => caption)
   }
 
   static listVideoCaptions (videoId: number) {
@@ -168,6 +173,6 @@ export class VideoCaptionModel extends Model<VideoCaptionModel> {
   }
 
   removeCaptionFile () {
-    return unlinkPromise(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
+    return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
   }
 }