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