diff options
Diffstat (limited to 'server/models/video/thumbnail.ts')
-rw-r--r-- | server/models/video/thumbnail.ts | 208 |
1 files changed, 0 insertions, 208 deletions
diff --git a/server/models/video/thumbnail.ts b/server/models/video/thumbnail.ts deleted file mode 100644 index 1722acdb4..000000000 --- a/server/models/video/thumbnail.ts +++ /dev/null | |||
@@ -1,208 +0,0 @@ | |||
1 | import { remove } from 'fs-extra' | ||
2 | import { join } from 'path' | ||
3 | import { | ||
4 | AfterDestroy, | ||
5 | AllowNull, | ||
6 | BeforeCreate, | ||
7 | BeforeUpdate, | ||
8 | BelongsTo, | ||
9 | Column, | ||
10 | CreatedAt, | ||
11 | DataType, | ||
12 | Default, | ||
13 | ForeignKey, | ||
14 | Model, | ||
15 | Table, | ||
16 | UpdatedAt | ||
17 | } from 'sequelize-typescript' | ||
18 | import { afterCommitIfTransaction } from '@server/helpers/database-utils' | ||
19 | import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models' | ||
20 | import { AttributesOnly } from '@shared/typescript-utils' | ||
21 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' | ||
22 | import { logger } from '../../helpers/logger' | ||
23 | import { CONFIG } from '../../initializers/config' | ||
24 | import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, WEBSERVER } from '../../initializers/constants' | ||
25 | import { VideoModel } from './video' | ||
26 | import { VideoPlaylistModel } from './video-playlist' | ||
27 | |||
28 | @Table({ | ||
29 | tableName: 'thumbnail', | ||
30 | indexes: [ | ||
31 | { | ||
32 | fields: [ 'videoId' ] | ||
33 | }, | ||
34 | { | ||
35 | fields: [ 'videoPlaylistId' ], | ||
36 | unique: true | ||
37 | }, | ||
38 | { | ||
39 | fields: [ 'filename', 'type' ], | ||
40 | unique: true | ||
41 | } | ||
42 | ] | ||
43 | }) | ||
44 | export class ThumbnailModel extends Model<Partial<AttributesOnly<ThumbnailModel>>> { | ||
45 | |||
46 | @AllowNull(false) | ||
47 | @Column | ||
48 | filename: string | ||
49 | |||
50 | @AllowNull(true) | ||
51 | @Default(null) | ||
52 | @Column | ||
53 | height: number | ||
54 | |||
55 | @AllowNull(true) | ||
56 | @Default(null) | ||
57 | @Column | ||
58 | width: number | ||
59 | |||
60 | @AllowNull(false) | ||
61 | @Column | ||
62 | type: ThumbnailType | ||
63 | |||
64 | @AllowNull(true) | ||
65 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max)) | ||
66 | fileUrl: string | ||
67 | |||
68 | @AllowNull(true) | ||
69 | @Column | ||
70 | automaticallyGenerated: boolean | ||
71 | |||
72 | @AllowNull(false) | ||
73 | @Column | ||
74 | onDisk: boolean | ||
75 | |||
76 | @ForeignKey(() => VideoModel) | ||
77 | @Column | ||
78 | videoId: number | ||
79 | |||
80 | @BelongsTo(() => VideoModel, { | ||
81 | foreignKey: { | ||
82 | allowNull: true | ||
83 | }, | ||
84 | onDelete: 'CASCADE' | ||
85 | }) | ||
86 | Video: VideoModel | ||
87 | |||
88 | @ForeignKey(() => VideoPlaylistModel) | ||
89 | @Column | ||
90 | videoPlaylistId: number | ||
91 | |||
92 | @BelongsTo(() => VideoPlaylistModel, { | ||
93 | foreignKey: { | ||
94 | allowNull: true | ||
95 | }, | ||
96 | onDelete: 'CASCADE' | ||
97 | }) | ||
98 | VideoPlaylist: VideoPlaylistModel | ||
99 | |||
100 | @CreatedAt | ||
101 | createdAt: Date | ||
102 | |||
103 | @UpdatedAt | ||
104 | updatedAt: Date | ||
105 | |||
106 | // If this thumbnail replaced existing one, track the old name | ||
107 | previousThumbnailFilename: string | ||
108 | |||
109 | private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { | ||
110 | [ThumbnailType.MINIATURE]: { | ||
111 | label: 'miniature', | ||
112 | directory: CONFIG.STORAGE.THUMBNAILS_DIR, | ||
113 | staticPath: LAZY_STATIC_PATHS.THUMBNAILS | ||
114 | }, | ||
115 | [ThumbnailType.PREVIEW]: { | ||
116 | label: 'preview', | ||
117 | directory: CONFIG.STORAGE.PREVIEWS_DIR, | ||
118 | staticPath: LAZY_STATIC_PATHS.PREVIEWS | ||
119 | } | ||
120 | } | ||
121 | |||
122 | @BeforeCreate | ||
123 | @BeforeUpdate | ||
124 | static removeOldFile (instance: ThumbnailModel, options) { | ||
125 | return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded()) | ||
126 | } | ||
127 | |||
128 | @AfterDestroy | ||
129 | static removeFiles (instance: ThumbnailModel) { | ||
130 | logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename) | ||
131 | |||
132 | // Don't block the transaction | ||
133 | instance.removeThumbnail() | ||
134 | .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, { err })) | ||
135 | } | ||
136 | |||
137 | static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> { | ||
138 | const query = { | ||
139 | where: { | ||
140 | filename, | ||
141 | type: thumbnailType | ||
142 | } | ||
143 | } | ||
144 | |||
145 | return ThumbnailModel.findOne(query) | ||
146 | } | ||
147 | |||
148 | static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> { | ||
149 | const query = { | ||
150 | where: { | ||
151 | filename, | ||
152 | type: thumbnailType | ||
153 | }, | ||
154 | include: [ | ||
155 | { | ||
156 | model: VideoModel.unscoped(), | ||
157 | required: true | ||
158 | } | ||
159 | ] | ||
160 | } | ||
161 | |||
162 | return ThumbnailModel.findOne(query) | ||
163 | } | ||
164 | |||
165 | static buildPath (type: ThumbnailType, filename: string) { | ||
166 | const directory = ThumbnailModel.types[type].directory | ||
167 | |||
168 | return join(directory, filename) | ||
169 | } | ||
170 | |||
171 | getOriginFileUrl (video: MVideo) { | ||
172 | const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename | ||
173 | |||
174 | if (video.isOwned()) return WEBSERVER.URL + staticPath | ||
175 | |||
176 | return this.fileUrl | ||
177 | } | ||
178 | |||
179 | getLocalStaticPath () { | ||
180 | return ThumbnailModel.types[this.type].staticPath + this.filename | ||
181 | } | ||
182 | |||
183 | getPath () { | ||
184 | return ThumbnailModel.buildPath(this.type, this.filename) | ||
185 | } | ||
186 | |||
187 | getPreviousPath () { | ||
188 | return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename) | ||
189 | } | ||
190 | |||
191 | removeThumbnail () { | ||
192 | return remove(this.getPath()) | ||
193 | } | ||
194 | |||
195 | removePreviousFilenameIfNeeded () { | ||
196 | if (!this.previousThumbnailFilename) return | ||
197 | |||
198 | const previousPath = this.getPreviousPath() | ||
199 | remove(previousPath) | ||
200 | .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err })) | ||
201 | |||
202 | this.previousThumbnailFilename = undefined | ||
203 | } | ||
204 | |||
205 | isOwned () { | ||
206 | return !this.fileUrl | ||
207 | } | ||
208 | } | ||