]>
Commit | Line | Data |
---|---|---|
a8b1b404 | 1 | import { remove } from 'fs-extra' |
e8bafea3 | 2 | import { join } from 'path' |
2735a154 C |
3 | import { |
4 | AfterDestroy, | |
5 | AllowNull, | |
a35a2279 C |
6 | BeforeCreate, |
7 | BeforeUpdate, | |
2735a154 C |
8 | BelongsTo, |
9 | Column, | |
10 | CreatedAt, | |
11 | DataType, | |
12 | Default, | |
13 | ForeignKey, | |
14 | Model, | |
15 | Table, | |
16 | UpdatedAt | |
17 | } from 'sequelize-typescript' | |
a35a2279 | 18 | import { afterCommitIfTransaction } from '@server/helpers/database-utils' |
8efc27bf | 19 | import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models' |
16c016e8 | 20 | import { AttributesOnly } from '@shared/core-utils' |
a8b1b404 | 21 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' |
e8bafea3 | 22 | import { logger } from '../../helpers/logger' |
e8bafea3 | 23 | import { CONFIG } from '../../initializers/config' |
a8b1b404 | 24 | import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, STATIC_PATHS, WEBSERVER } from '../../initializers/constants' |
e8bafea3 C |
25 | import { VideoModel } from './video' |
26 | import { VideoPlaylistModel } from './video-playlist' | |
e8bafea3 C |
27 | |
28 | @Table({ | |
29 | tableName: 'thumbnail', | |
30 | indexes: [ | |
31 | { | |
32 | fields: [ 'videoId' ] | |
33 | }, | |
34 | { | |
35 | fields: [ 'videoPlaylistId' ], | |
36 | unique: true | |
a8b1b404 C |
37 | }, |
38 | { | |
39 | fields: [ 'filename', 'type' ], | |
40 | unique: true | |
e8bafea3 C |
41 | } |
42 | ] | |
43 | }) | |
16c016e8 | 44 | export class ThumbnailModel extends Model<Partial<AttributesOnly<ThumbnailModel>>> { |
e8bafea3 C |
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) | |
2735a154 | 65 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max)) |
9cc8d43e | 66 | fileUrl: string |
e8bafea3 | 67 | |
65af03a2 C |
68 | @AllowNull(true) |
69 | @Column | |
70 | automaticallyGenerated: boolean | |
71 | ||
e8bafea3 C |
72 | @ForeignKey(() => VideoModel) |
73 | @Column | |
74 | videoId: number | |
75 | ||
76 | @BelongsTo(() => VideoModel, { | |
77 | foreignKey: { | |
78 | allowNull: true | |
79 | }, | |
80 | onDelete: 'CASCADE' | |
81 | }) | |
82 | Video: VideoModel | |
83 | ||
84 | @ForeignKey(() => VideoPlaylistModel) | |
85 | @Column | |
86 | videoPlaylistId: number | |
87 | ||
88 | @BelongsTo(() => VideoPlaylistModel, { | |
89 | foreignKey: { | |
90 | allowNull: true | |
91 | }, | |
92 | onDelete: 'CASCADE' | |
93 | }) | |
94 | VideoPlaylist: VideoPlaylistModel | |
95 | ||
96 | @CreatedAt | |
97 | createdAt: Date | |
98 | ||
99 | @UpdatedAt | |
100 | updatedAt: Date | |
101 | ||
a35a2279 C |
102 | // If this thumbnail replaced existing one, track the old name |
103 | previousThumbnailFilename: string | |
104 | ||
a1587156 | 105 | private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { |
3acc5084 C |
106 | [ThumbnailType.MINIATURE]: { |
107 | label: 'miniature', | |
e8bafea3 C |
108 | directory: CONFIG.STORAGE.THUMBNAILS_DIR, |
109 | staticPath: STATIC_PATHS.THUMBNAILS | |
110 | }, | |
111 | [ThumbnailType.PREVIEW]: { | |
112 | label: 'preview', | |
113 | directory: CONFIG.STORAGE.PREVIEWS_DIR, | |
557b13ae | 114 | staticPath: LAZY_STATIC_PATHS.PREVIEWS |
e8bafea3 C |
115 | } |
116 | } | |
117 | ||
a35a2279 C |
118 | @BeforeCreate |
119 | @BeforeUpdate | |
120 | static removeOldFile (instance: ThumbnailModel, options) { | |
121 | return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded()) | |
122 | } | |
123 | ||
e8bafea3 | 124 | @AfterDestroy |
65af03a2 | 125 | static removeFiles (instance: ThumbnailModel) { |
e8bafea3 C |
126 | logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename) |
127 | ||
128 | // Don't block the transaction | |
129 | instance.removeThumbnail() | |
130 | .catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, err)) | |
131 | } | |
132 | ||
a35a2279 C |
133 | static loadByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnail> { |
134 | const query = { | |
135 | where: { | |
136 | filename, | |
137 | type: thumbnailType | |
138 | } | |
139 | } | |
140 | ||
141 | return ThumbnailModel.findOne(query) | |
142 | } | |
143 | ||
144 | static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType): Promise<MThumbnailVideo> { | |
e2600d8b C |
145 | const query = { |
146 | where: { | |
a8b1b404 C |
147 | filename, |
148 | type: thumbnailType | |
149 | }, | |
150 | include: [ | |
151 | { | |
152 | model: VideoModel.unscoped(), | |
153 | required: true | |
154 | } | |
155 | ] | |
e2600d8b C |
156 | } |
157 | ||
158 | return ThumbnailModel.findOne(query) | |
159 | } | |
160 | ||
374b725d C |
161 | static buildPath (type: ThumbnailType, filename: string) { |
162 | const directory = ThumbnailModel.types[type].directory | |
163 | ||
164 | return join(directory, filename) | |
165 | } | |
166 | ||
8efc27bf | 167 | getFileUrl (video: MVideo) { |
ca6d3622 | 168 | const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename |
e8bafea3 | 169 | |
ca6d3622 | 170 | if (video.isOwned()) return WEBSERVER.URL + staticPath |
ca6d3622 | 171 | |
d9a2a031 | 172 | return this.fileUrl |
e8bafea3 C |
173 | } |
174 | ||
536598cf | 175 | getPath () { |
374b725d | 176 | return ThumbnailModel.buildPath(this.type, this.filename) |
536598cf | 177 | } |
e8bafea3 | 178 | |
a35a2279 | 179 | getPreviousPath () { |
374b725d | 180 | return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename) |
a35a2279 C |
181 | } |
182 | ||
536598cf C |
183 | removeThumbnail () { |
184 | return remove(this.getPath()) | |
e8bafea3 | 185 | } |
a35a2279 C |
186 | |
187 | removePreviousFilenameIfNeeded () { | |
188 | if (!this.previousThumbnailFilename) return | |
189 | ||
190 | const previousPath = this.getPreviousPath() | |
191 | remove(previousPath) | |
192 | .catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err })) | |
193 | ||
194 | this.previousThumbnailFilename = undefined | |
195 | } | |
e8bafea3 | 196 | } |