15 } from 'sequelize-typescript'
16 import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers'
17 import { getSort, throwIfNotValid } from '../utils'
18 import { VideoModel } from './video'
19 import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
20 import { VideoImport, VideoImportState } from '../../../shared'
21 import { VideoChannelModel } from './video-channel'
22 import { AccountModel } from '../account/account'
23 import { TagModel } from './tag'
24 import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos'
29 model: () => VideoModel,
33 model: () => VideoChannelModel,
37 model: () => AccountModel,
51 tableName: 'videoImport',
54 fields: [ 'videoId' ],
59 export class VideoImportModel extends Model<VideoImportModel> {
68 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl'))
69 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
74 @Is('VideoImportMagnetUri', value => throwIfNotValid(value, isVideoMagnetUriValid, 'magnetUri'))
75 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) // Use the same constraints than URLs
80 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max))
85 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
87 state: VideoImportState
91 @Column(DataType.TEXT)
94 @ForeignKey(() => VideoModel)
98 @BelongsTo(() => VideoModel, {
107 static deleteVideoIfFailed (instance: VideoImportModel, options) {
108 if (instance.state === VideoImportState.FAILED) {
109 return instance.Video.destroy({ transaction: options.transaction })
115 static loadAndPopulateVideo (id: number) {
116 return VideoImportModel.findById(id)
119 static listUserVideoImportsForApi (accountId: number, start: number, count: number, sort: string) {
124 order: getSort(sort),
131 model: VideoChannelModel,
152 return VideoImportModel.unscoped()
153 .findAndCountAll(query)
154 .then(({ rows, count }) => {
162 toFormattedJSON (): VideoImport {
163 const videoFormatOptions = {
164 additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
166 const video = this.Video
167 ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), {
168 tags: this.Video.Tags.map(t => t.name)
175 targetUrl: this.targetUrl,
176 magnetUri: this.magnetUri,
177 torrentName: this.torrentName,
181 label: VideoImportModel.getStateLabel(this.state)
184 updatedAt: this.updatedAt.toISOString(),
185 createdAt: this.createdAt.toISOString(),
189 private static getStateLabel (id: number) {
190 return VIDEO_IMPORT_STATES[id] || 'Unknown'