video-caption.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import * as Sequelize from 'sequelize'
  2. import {
  3. AllowNull,
  4. BeforeDestroy,
  5. BelongsTo,
  6. Column,
  7. CreatedAt,
  8. ForeignKey,
  9. Is,
  10. Model,
  11. Scopes,
  12. Table,
  13. UpdatedAt
  14. } from 'sequelize-typescript'
  15. import { throwIfNotValid } from '../utils'
  16. import { VideoModel } from './video'
  17. import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions'
  18. import { VideoCaption } from '../../../shared/models/videos/caption/video-caption.model'
  19. import { CONFIG, STATIC_PATHS, VIDEO_LANGUAGES } from '../../initializers'
  20. import { join } from 'path'
  21. import { logger } from '../../helpers/logger'
  22. import { remove } from 'fs-extra'
  23. export enum ScopeNames {
  24. WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
  25. }
  26. @Scopes({
  27. [ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
  28. include: [
  29. {
  30. attributes: [ 'uuid', 'remote' ],
  31. model: () => VideoModel.unscoped(),
  32. required: true
  33. }
  34. ]
  35. }
  36. })
  37. @Table({
  38. tableName: 'videoCaption',
  39. indexes: [
  40. {
  41. fields: [ 'videoId' ]
  42. },
  43. {
  44. fields: [ 'videoId', 'language' ],
  45. unique: true
  46. }
  47. ]
  48. })
  49. export class VideoCaptionModel extends Model<VideoCaptionModel> {
  50. @CreatedAt
  51. createdAt: Date
  52. @UpdatedAt
  53. updatedAt: Date
  54. @AllowNull(false)
  55. @Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
  56. @Column
  57. language: string
  58. @ForeignKey(() => VideoModel)
  59. @Column
  60. videoId: number
  61. @BelongsTo(() => VideoModel, {
  62. foreignKey: {
  63. allowNull: false
  64. },
  65. onDelete: 'CASCADE'
  66. })
  67. Video: VideoModel
  68. @BeforeDestroy
  69. static async removeFiles (instance: VideoCaptionModel) {
  70. if (!instance.Video) {
  71. instance.Video = await instance.$get('Video') as VideoModel
  72. }
  73. if (instance.isOwned()) {
  74. logger.info('Removing captions %s of video %s.', instance.Video.uuid, instance.language)
  75. try {
  76. await instance.removeCaptionFile()
  77. } catch (err) {
  78. logger.error('Cannot remove caption file of video %s.', instance.Video.uuid)
  79. }
  80. }
  81. return undefined
  82. }
  83. static loadByVideoIdAndLanguage (videoId: string | number, language: string) {
  84. const videoInclude = {
  85. model: VideoModel.unscoped(),
  86. attributes: [ 'id', 'remote', 'uuid' ],
  87. where: { }
  88. }
  89. if (typeof videoId === 'string') videoInclude.where['uuid'] = videoId
  90. else videoInclude.where['id'] = videoId
  91. const query = {
  92. where: {
  93. language
  94. },
  95. include: [
  96. videoInclude
  97. ]
  98. }
  99. return VideoCaptionModel.findOne(query)
  100. }
  101. static insertOrReplaceLanguage (videoId: number, language: string, transaction: Sequelize.Transaction) {
  102. const values = {
  103. videoId,
  104. language
  105. }
  106. return VideoCaptionModel.upsert<VideoCaptionModel>(values, { transaction, returning: true })
  107. .then(([ caption ]) => caption)
  108. }
  109. static listVideoCaptions (videoId: number) {
  110. const query = {
  111. order: [ [ 'language', 'ASC' ] ],
  112. where: {
  113. videoId
  114. }
  115. }
  116. return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
  117. }
  118. static getLanguageLabel (language: string) {
  119. return VIDEO_LANGUAGES[language] || 'Unknown'
  120. }
  121. static deleteAllCaptionsOfRemoteVideo (videoId: number, transaction: Sequelize.Transaction) {
  122. const query = {
  123. where: {
  124. videoId
  125. },
  126. transaction
  127. }
  128. return VideoCaptionModel.destroy(query)
  129. }
  130. isOwned () {
  131. return this.Video.remote === false
  132. }
  133. toFormattedJSON (): VideoCaption {
  134. return {
  135. language: {
  136. id: this.language,
  137. label: VideoCaptionModel.getLanguageLabel(this.language)
  138. },
  139. captionPath: this.getCaptionStaticPath()
  140. }
  141. }
  142. getCaptionStaticPath () {
  143. return join(STATIC_PATHS.VIDEO_CAPTIONS, this.getCaptionName())
  144. }
  145. getCaptionName () {
  146. return `${this.Video.uuid}-${this.language}.vtt`
  147. }
  148. removeCaptionFile () {
  149. return remove(CONFIG.STORAGE.CAPTIONS_DIR + this.getCaptionName())
  150. }
  151. }