]>
Commit | Line | Data |
---|---|---|
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/core-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, 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 | @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 | ||
102 | // If this thumbnail replaced existing one, track the old name | |
103 | previousThumbnailFilename: string | |
104 | ||
105 | private static readonly types: { [ id in ThumbnailType ]: { label: string, directory: string, staticPath: string } } = { | |
106 | [ThumbnailType.MINIATURE]: { | |
107 | label: 'miniature', | |
108 | directory: CONFIG.STORAGE.THUMBNAILS_DIR, | |
109 | staticPath: STATIC_PATHS.THUMBNAILS | |
110 | }, | |
111 | [ThumbnailType.PREVIEW]: { | |
112 | label: 'preview', | |
113 | directory: CONFIG.STORAGE.PREVIEWS_DIR, | |
114 | staticPath: LAZY_STATIC_PATHS.PREVIEWS | |
115 | } | |
116 | } | |
117 | ||
118 | @BeforeCreate | |
119 | @BeforeUpdate | |
120 | static removeOldFile (instance: ThumbnailModel, options) { | |
121 | return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded()) | |
122 | } | |
123 | ||
124 | @AfterDestroy | |
125 | static removeFiles (instance: ThumbnailModel) { | |
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 | ||
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> { | |
145 | const query = { | |
146 | where: { | |
147 | filename, | |
148 | type: thumbnailType | |
149 | }, | |
150 | include: [ | |
151 | { | |
152 | model: VideoModel.unscoped(), | |
153 | required: true | |
154 | } | |
155 | ] | |
156 | } | |
157 | ||
158 | return ThumbnailModel.findOne(query) | |
159 | } | |
160 | ||
161 | static buildPath (type: ThumbnailType, filename: string) { | |
162 | const directory = ThumbnailModel.types[type].directory | |
163 | ||
164 | return join(directory, filename) | |
165 | } | |
166 | ||
167 | getFileUrl (video: MVideo) { | |
168 | const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename | |
169 | ||
170 | if (video.isOwned()) return WEBSERVER.URL + staticPath | |
171 | ||
172 | return this.fileUrl | |
173 | } | |
174 | ||
175 | getPath () { | |
176 | return ThumbnailModel.buildPath(this.type, this.filename) | |
177 | } | |
178 | ||
179 | getPreviousPath () { | |
180 | return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename) | |
181 | } | |
182 | ||
183 | removeThumbnail () { | |
184 | return remove(this.getPath()) | |
185 | } | |
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 | } | |
196 | } |