]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-import.ts
Add import http enabled configuration
[github/Chocobozzz/PeerTube.git] / server / models / video / video-import.ts
CommitLineData
fbad87b0 1import {
ed31c059 2 AfterUpdate,
fbad87b0
C
3 AllowNull,
4 BelongsTo,
5 Column,
6 CreatedAt,
7 DataType,
8 Default,
9 DefaultScope,
10 ForeignKey,
11 Is,
12 Model,
13 Table,
14 UpdatedAt
15} from 'sequelize-typescript'
ed31c059
C
16import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers'
17import { getSort, throwIfNotValid } from '../utils'
fbad87b0
C
18import { VideoModel } from './video'
19import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports'
20import { VideoImport, VideoImportState } from '../../../shared'
21import { VideoChannelModel } from './video-channel'
22import { AccountModel } from '../account/account'
ed31c059 23import { TagModel } from './tag'
fbad87b0
C
24
25@DefaultScope({
26 include: [
27 {
28 model: () => VideoModel,
d7f83948 29 required: false,
fbad87b0
C
30 include: [
31 {
32 model: () => VideoChannelModel,
33 required: true,
34 include: [
35 {
36 model: () => AccountModel,
37 required: true
38 }
39 ]
ed31c059
C
40 },
41 {
42 model: () => TagModel,
43 required: false
fbad87b0
C
44 }
45 ]
46 }
47 ]
48})
49
50@Table({
51 tableName: 'videoImport',
52 indexes: [
53 {
54 fields: [ 'videoId' ],
55 unique: true
56 }
57 ]
58})
59export class VideoImportModel extends Model<VideoImportModel> {
60 @CreatedAt
61 createdAt: Date
62
63 @UpdatedAt
64 updatedAt: Date
65
66 @AllowNull(false)
67 @Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl'))
68 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
69 targetUrl: string
70
71 @AllowNull(false)
72 @Default(null)
73 @Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
74 @Column
75 state: VideoImportState
76
77 @AllowNull(true)
78 @Default(null)
79 @Column(DataType.TEXT)
80 error: string
81
82 @ForeignKey(() => VideoModel)
83 @Column
84 videoId: number
85
86 @BelongsTo(() => VideoModel, {
87 foreignKey: {
ed31c059 88 allowNull: true
fbad87b0 89 },
ed31c059 90 onDelete: 'set null'
fbad87b0
C
91 })
92 Video: VideoModel
93
ed31c059
C
94 @AfterUpdate
95 static deleteVideoIfFailed (instance: VideoImportModel, options) {
96 if (instance.state === VideoImportState.FAILED) {
97 return instance.Video.destroy({ transaction: options.transaction })
98 }
99
100 return undefined
101 }
102
fbad87b0
C
103 static loadAndPopulateVideo (id: number) {
104 return VideoImportModel.findById(id)
105 }
106
ed31c059
C
107 static listUserVideoImportsForApi (accountId: number, start: number, count: number, sort: string) {
108 const query = {
109 offset: start,
110 limit: count,
111 order: getSort(sort),
112 include: [
113 {
114 model: VideoModel,
d7f83948 115 required: false,
ed31c059
C
116 include: [
117 {
118 model: VideoChannelModel,
119 required: true,
120 include: [
121 {
122 model: AccountModel,
123 required: true,
124 where: {
125 id: accountId
126 }
127 }
128 ]
129 },
130 {
131 model: TagModel,
132 required: false
133 }
134 ]
135 }
136 ]
137 }
138
139 return VideoImportModel.unscoped()
140 .findAndCountAll(query)
141 .then(({ rows, count }) => {
142 return {
143 data: rows,
144 total: count
145 }
146 })
147 }
148
fbad87b0
C
149 toFormattedJSON (): VideoImport {
150 const videoFormatOptions = {
151 additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
152 }
ed31c059
C
153 const video = this.Video
154 ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), {
155 tags: this.Video.Tags.map(t => t.name)
156 })
157 : undefined
fbad87b0
C
158
159 return {
d7f83948 160 id: this.id,
fbad87b0 161 targetUrl: this.targetUrl,
ed31c059
C
162 state: {
163 id: this.state,
164 label: VideoImportModel.getStateLabel(this.state)
165 },
d7f83948 166 error: this.error,
ed31c059
C
167 updatedAt: this.updatedAt.toISOString(),
168 createdAt: this.createdAt.toISOString(),
fbad87b0
C
169 video
170 }
171 }
ed31c059
C
172 private static getStateLabel (id: number) {
173 return VIDEO_IMPORT_STATES[id] || 'Unknown'
174 }
fbad87b0 175}