aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/thumbnail.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/models/video/thumbnail.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/models/video/thumbnail.ts')
-rw-r--r--server/models/video/thumbnail.ts208
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 @@
1import { remove } from 'fs-extra'
2import { join } from 'path'
3import {
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'
18import { afterCommitIfTransaction } from '@server/helpers/database-utils'
19import { MThumbnail, MThumbnailVideo, MVideo } from '@server/types/models'
20import { AttributesOnly } from '@shared/typescript-utils'
21import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type'
22import { logger } from '../../helpers/logger'
23import { CONFIG } from '../../initializers/config'
24import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, WEBSERVER } from '../../initializers/constants'
25import { VideoModel } from './video'
26import { 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})
44export 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}