video-caption.ts 4.8 KB

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