]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
CommitLineData
a35a2279 1import { join } from 'path'
c729caf6
C
2import { ThumbnailType } from '@shared/models'
3import { generateImageFilename, generateImageFromVideoFile, processImage } from '../helpers/image-utils'
e8bafea3 4import { CONFIG } from '../initializers/config'
453e83ea 5import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
e8bafea3 6import { ThumbnailModel } from '../models/video/thumbnail'
1ef447bd 7import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
26d6bf65 8import { MThumbnail } from '../types/models/video/thumbnail'
a35a2279 9import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
c53853ca 10import { downloadImageFromWorker } from './local-actor'
0305db28 11import { VideoPathManager } from './video-path-manager'
e8bafea3 12
84531547 13type ImageSize = { height?: number, width?: number }
e8bafea3 14
91f8f8db 15function updatePlaylistMiniatureFromExisting (options: {
a35a2279
C
16 inputPath: string
17 playlist: MVideoPlaylistThumbnail
18 automaticallyGenerated: boolean
19 keepOriginal?: boolean // default to false
65af03a2 20 size?: ImageSize
a35a2279
C
21}) {
22 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
e8bafea3 23 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 24 const type = ThumbnailType.MINIATURE
e8bafea3 25
2fb5b3a5 26 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
91f8f8db 27 return updateThumbnailFromFunction({
a35a2279
C
28 thumbnailCreator,
29 filename,
30 height,
31 width,
32 type,
33 automaticallyGenerated,
34 existingThumbnail
35 })
e8bafea3
C
36}
37
91f8f8db 38function updatePlaylistMiniatureFromUrl (options: {
a35a2279
C
39 downloadUrl: string
40 playlist: MVideoPlaylistThumbnail
41 size?: ImageSize
42}) {
43 const { downloadUrl, playlist, size } = options
e8bafea3 44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 45 const type = ThumbnailType.MINIATURE
e8bafea3 46
a8b1b404
C
47 // Only save the file URL if it is a remote playlist
48 const fileUrl = playlist.isOwned()
49 ? null
50 : downloadUrl
51
c53853ca
C
52 const thumbnailCreator = () => {
53 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
54 }
55
91f8f8db 56 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
57}
58
91f8f8db 59function updateVideoMiniatureFromUrl (options: {
a35a2279
C
60 downloadUrl: string
61 video: MVideoThumbnail
62 type: ThumbnailType
63 size?: ImageSize
64}) {
65 const { downloadUrl, video, type, size } = options
d9a2a031 66 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
e8bafea3 67
a8b1b404
C
68 // Only save the file URL if it is a remote video
69 const fileUrl = video.isOwned()
70 ? null
71 : downloadUrl
72
1ef447bd 73 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
d9a2a031
C
74
75 // Do not change the thumbnail filename if the file did not change
76 const filename = thumbnailUrlChanged
77 ? updatedFilename
78 : existingThumbnail.filename
79
374b725d 80 const thumbnailCreator = () => {
c53853ca
C
81 if (thumbnailUrlChanged) {
82 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
83 }
374b725d 84
d9a2a031 85 return Promise.resolve()
374b725d
C
86 }
87
91f8f8db 88 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
89}
90
91f8f8db 91function updateVideoMiniatureFromExisting (options: {
1ef65f4c
C
92 inputPath: string
93 video: MVideoThumbnail
94 type: ThumbnailType
95 automaticallyGenerated: boolean
65af03a2 96 size?: ImageSize
a35a2279 97 keepOriginal?: boolean // default to false
1ef65f4c 98}) {
a35a2279 99 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
1ef65f4c 100
e8bafea3 101 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
1ef65f4c 102 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
e8bafea3 103
91f8f8db 104 return updateThumbnailFromFunction({
a35a2279
C
105 thumbnailCreator,
106 filename,
107 height,
108 width,
109 type,
110 automaticallyGenerated,
111 existingThumbnail
112 })
e8bafea3
C
113}
114
a35a2279
C
115function generateVideoMiniature (options: {
116 video: MVideoThumbnail
117 videoFile: MVideoFile
118 type: ThumbnailType
119}) {
120 const { video, videoFile, type } = options
121
ad5db104 122 return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
0305db28
JB
123 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
124
125 const thumbnailCreator = videoFile.isAudio()
126 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
127 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
128
129 return updateThumbnailFromFunction({
130 thumbnailCreator,
131 filename,
132 height,
133 width,
134 type,
135 automaticallyGenerated: true,
136 existingThumbnail
137 })
a35a2279 138 })
e8bafea3
C
139}
140
91f8f8db 141function updatePlaceholderThumbnail (options: {
a35a2279
C
142 fileUrl: string
143 video: MVideoThumbnail
144 type: ThumbnailType
145 size: ImageSize
146}) {
147 const { fileUrl, video, type, size } = options
1ef447bd
C
148 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
149
150 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
e8bafea3 151
a1587156 152 const thumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3 153
1ef447bd
C
154 // Do not change the thumbnail filename if the file did not change
155 const filename = thumbnailUrlChanged
156 ? updatedFilename
157 : existingThumbnail.filename
158
e8bafea3
C
159 thumbnail.filename = filename
160 thumbnail.height = height
161 thumbnail.width = width
162 thumbnail.type = type
9cc8d43e 163 thumbnail.fileUrl = fileUrl
e8bafea3
C
164
165 return thumbnail
166}
167
168// ---------------------------------------------------------------------------
169
170export {
3acc5084 171 generateVideoMiniature,
91f8f8db
C
172 updateVideoMiniatureFromUrl,
173 updateVideoMiniatureFromExisting,
174 updatePlaceholderThumbnail,
175 updatePlaylistMiniatureFromUrl,
176 updatePlaylistMiniatureFromExisting
e8bafea3
C
177}
178
1ef447bd
C
179function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
180 const existingUrl = existingThumbnail
181 ? existingThumbnail.fileUrl
182 : null
183
184 // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
185 return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
186}
187
453e83ea 188function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
e8bafea3
C
189 const filename = playlist.generateThumbnailName()
190 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
191
192 return {
193 filename,
194 basePath,
195 existingThumbnail: playlist.Thumbnail,
196 outputPath: join(basePath, filename),
197 height: size ? size.height : THUMBNAILS_SIZE.height,
198 width: size ? size.width : THUMBNAILS_SIZE.width
199 }
200}
201
453e83ea 202function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3
C
203 const existingThumbnail = Array.isArray(video.Thumbnails)
204 ? video.Thumbnails.find(t => t.type === type)
205 : undefined
206
3acc5084 207 if (type === ThumbnailType.MINIATURE) {
84531547 208 const filename = generateImageFilename()
e8bafea3
C
209 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
210
211 return {
212 filename,
213 basePath,
214 existingThumbnail,
215 outputPath: join(basePath, filename),
216 height: size ? size.height : THUMBNAILS_SIZE.height,
217 width: size ? size.width : THUMBNAILS_SIZE.width
218 }
219 }
220
221 if (type === ThumbnailType.PREVIEW) {
84531547 222 const filename = generateImageFilename()
e8bafea3
C
223 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
224
225 return {
226 filename,
227 basePath,
228 existingThumbnail,
229 outputPath: join(basePath, filename),
230 height: size ? size.height : PREVIEWS_SIZE.height,
231 width: size ? size.width : PREVIEWS_SIZE.width
232 }
233 }
234
235 return undefined
236}
237
91f8f8db 238async function updateThumbnailFromFunction (parameters: {
a1587156
C
239 thumbnailCreator: () => Promise<any>
240 filename: string
241 height: number
242 width: number
243 type: ThumbnailType
244 automaticallyGenerated?: boolean
245 fileUrl?: string
453e83ea 246 existingThumbnail?: MThumbnail
e8bafea3 247}) {
a35a2279
C
248 const {
249 thumbnailCreator,
250 filename,
251 width,
252 height,
253 type,
254 existingThumbnail,
255 automaticallyGenerated = null,
256 fileUrl = null
257 } = parameters
258
d9a2a031 259 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
a35a2279
C
260 ? existingThumbnail.filename
261 : undefined
6302d599 262
a35a2279 263 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3
C
264
265 thumbnail.filename = filename
266 thumbnail.height = height
267 thumbnail.width = width
268 thumbnail.type = type
9cc8d43e 269 thumbnail.fileUrl = fileUrl
65af03a2 270 thumbnail.automaticallyGenerated = automaticallyGenerated
d9a2a031
C
271
272 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
e8bafea3
C
273
274 await thumbnailCreator()
275
276 return thumbnail
277}