diff options
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/video/video-import.ts | 82 | ||||
-rw-r--r-- | server/models/video/video.ts | 8 |
2 files changed, 81 insertions, 9 deletions
diff --git a/server/models/video/video-import.ts b/server/models/video/video-import.ts index 89eeafd6a..6b8a16b65 100644 --- a/server/models/video/video-import.ts +++ b/server/models/video/video-import.ts | |||
@@ -1,4 +1,5 @@ | |||
1 | import { | 1 | import { |
2 | AfterUpdate, | ||
2 | AllowNull, | 3 | AllowNull, |
3 | BelongsTo, | 4 | BelongsTo, |
4 | Column, | 5 | Column, |
@@ -12,13 +13,14 @@ import { | |||
12 | Table, | 13 | Table, |
13 | UpdatedAt | 14 | UpdatedAt |
14 | } from 'sequelize-typescript' | 15 | } from 'sequelize-typescript' |
15 | import { CONSTRAINTS_FIELDS } from '../../initializers' | 16 | import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers' |
16 | import { throwIfNotValid } from '../utils' | 17 | import { getSort, throwIfNotValid } from '../utils' |
17 | import { VideoModel } from './video' | 18 | import { VideoModel } from './video' |
18 | import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' | 19 | import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports' |
19 | import { VideoImport, VideoImportState } from '../../../shared' | 20 | import { VideoImport, VideoImportState } from '../../../shared' |
20 | import { VideoChannelModel } from './video-channel' | 21 | import { VideoChannelModel } from './video-channel' |
21 | import { AccountModel } from '../account/account' | 22 | import { AccountModel } from '../account/account' |
23 | import { TagModel } from './tag' | ||
22 | 24 | ||
23 | @DefaultScope({ | 25 | @DefaultScope({ |
24 | include: [ | 26 | include: [ |
@@ -35,6 +37,10 @@ import { AccountModel } from '../account/account' | |||
35 | required: true | 37 | required: true |
36 | } | 38 | } |
37 | ] | 39 | ] |
40 | }, | ||
41 | { | ||
42 | model: () => TagModel, | ||
43 | required: false | ||
38 | } | 44 | } |
39 | ] | 45 | ] |
40 | } | 46 | } |
@@ -79,27 +85,89 @@ export class VideoImportModel extends Model<VideoImportModel> { | |||
79 | 85 | ||
80 | @BelongsTo(() => VideoModel, { | 86 | @BelongsTo(() => VideoModel, { |
81 | foreignKey: { | 87 | foreignKey: { |
82 | allowNull: false | 88 | allowNull: true |
83 | }, | 89 | }, |
84 | onDelete: 'CASCADE' | 90 | onDelete: 'set null' |
85 | }) | 91 | }) |
86 | Video: VideoModel | 92 | Video: VideoModel |
87 | 93 | ||
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 | |||
88 | static loadAndPopulateVideo (id: number) { | 103 | static loadAndPopulateVideo (id: number) { |
89 | return VideoImportModel.findById(id) | 104 | return VideoImportModel.findById(id) |
90 | } | 105 | } |
91 | 106 | ||
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, | ||
115 | required: true, | ||
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 | |||
92 | toFormattedJSON (): VideoImport { | 149 | toFormattedJSON (): VideoImport { |
93 | const videoFormatOptions = { | 150 | const videoFormatOptions = { |
94 | additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } | 151 | additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true } |
95 | } | 152 | } |
96 | const video = Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { | 153 | const video = this.Video |
97 | tags: this.Video.Tags.map(t => t.name) | 154 | ? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { |
98 | }) | 155 | tags: this.Video.Tags.map(t => t.name) |
156 | }) | ||
157 | : undefined | ||
99 | 158 | ||
100 | return { | 159 | return { |
101 | targetUrl: this.targetUrl, | 160 | targetUrl: this.targetUrl, |
161 | state: { | ||
162 | id: this.state, | ||
163 | label: VideoImportModel.getStateLabel(this.state) | ||
164 | }, | ||
165 | updatedAt: this.updatedAt.toISOString(), | ||
166 | createdAt: this.createdAt.toISOString(), | ||
102 | video | 167 | video |
103 | } | 168 | } |
104 | } | 169 | } |
170 | private static getStateLabel (id: number) { | ||
171 | return VIDEO_IMPORT_STATES[id] || 'Unknown' | ||
172 | } | ||
105 | } | 173 | } |
diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 459fcb31e..f32010014 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts | |||
@@ -1569,21 +1569,25 @@ export class VideoModel extends Model<VideoModel> { | |||
1569 | removeThumbnail () { | 1569 | removeThumbnail () { |
1570 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) | 1570 | const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) |
1571 | return unlinkPromise(thumbnailPath) | 1571 | return unlinkPromise(thumbnailPath) |
1572 | .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err })) | ||
1572 | } | 1573 | } |
1573 | 1574 | ||
1574 | removePreview () { | 1575 | removePreview () { |
1575 | // Same name than video thumbnail | 1576 | const previewPath = join(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) |
1576 | return unlinkPromise(CONFIG.STORAGE.PREVIEWS_DIR + this.getPreviewName()) | 1577 | return unlinkPromise(previewPath) |
1578 | .catch(err => logger.warn('Cannot delete preview %s.', previewPath, { err })) | ||
1577 | } | 1579 | } |
1578 | 1580 | ||
1579 | removeFile (videoFile: VideoFileModel) { | 1581 | removeFile (videoFile: VideoFileModel) { |
1580 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) | 1582 | const filePath = join(CONFIG.STORAGE.VIDEOS_DIR, this.getVideoFilename(videoFile)) |
1581 | return unlinkPromise(filePath) | 1583 | return unlinkPromise(filePath) |
1584 | .catch(err => logger.warn('Cannot delete file %s.', filePath, { err })) | ||
1582 | } | 1585 | } |
1583 | 1586 | ||
1584 | removeTorrent (videoFile: VideoFileModel) { | 1587 | removeTorrent (videoFile: VideoFileModel) { |
1585 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) | 1588 | const torrentPath = join(CONFIG.STORAGE.TORRENTS_DIR, this.getTorrentFileName(videoFile)) |
1586 | return unlinkPromise(torrentPath) | 1589 | return unlinkPromise(torrentPath) |
1590 | .catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err })) | ||
1587 | } | 1591 | } |
1588 | 1592 | ||
1589 | getActivityStreamDuration () { | 1593 | getActivityStreamDuration () { |