]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Don't pause video upon modal open (#3909)
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
CommitLineData
a35a2279 1import { join } from 'path'
a35a2279 2import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
e8bafea3 3import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
a35a2279
C
4import { processImage } from '../helpers/image-utils'
5import { downloadImage } from '../helpers/requests'
e8bafea3 6import { CONFIG } from '../initializers/config'
453e83ea 7import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
e8bafea3 8import { ThumbnailModel } from '../models/video/thumbnail'
1ef447bd 9import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
26d6bf65 10import { MThumbnail } from '../types/models/video/thumbnail'
a35a2279 11import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
d7a25329 12import { getVideoFilePath } from './video-paths'
e8bafea3
C
13
14type ImageSize = { height: number, width: number }
15
a35a2279
C
16function createPlaylistMiniatureFromExisting (options: {
17 inputPath: string
18 playlist: MVideoPlaylistThumbnail
19 automaticallyGenerated: boolean
20 keepOriginal?: boolean // default to false
65af03a2 21 size?: ImageSize
a35a2279
C
22}) {
23 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
e8bafea3 24 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 25 const type = ThumbnailType.MINIATURE
e8bafea3 26
2fb5b3a5 27 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
a35a2279
C
28 return createThumbnailFromFunction({
29 thumbnailCreator,
30 filename,
31 height,
32 width,
33 type,
34 automaticallyGenerated,
35 existingThumbnail
36 })
e8bafea3
C
37}
38
a35a2279
C
39function createPlaylistMiniatureFromUrl (options: {
40 downloadUrl: string
41 playlist: MVideoPlaylistThumbnail
42 size?: ImageSize
43}) {
44 const { downloadUrl, playlist, size } = options
e8bafea3 45 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 46 const type = ThumbnailType.MINIATURE
e8bafea3 47
a8b1b404
C
48 // Only save the file URL if it is a remote playlist
49 const fileUrl = playlist.isOwned()
50 ? null
51 : downloadUrl
52
53 const thumbnailCreator = () => downloadImage(downloadUrl, basePath, filename, { width, height })
9cc8d43e 54 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
55}
56
a35a2279
C
57function createVideoMiniatureFromUrl (options: {
58 downloadUrl: string
59 video: MVideoThumbnail
60 type: ThumbnailType
61 size?: ImageSize
62}) {
63 const { downloadUrl, video, type, size } = options
d9a2a031 64 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
e8bafea3 65
a8b1b404
C
66 // Only save the file URL if it is a remote video
67 const fileUrl = video.isOwned()
68 ? null
69 : downloadUrl
70
1ef447bd 71 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
d9a2a031
C
72
73 // Do not change the thumbnail filename if the file did not change
74 const filename = thumbnailUrlChanged
75 ? updatedFilename
76 : existingThumbnail.filename
77
374b725d
C
78 const thumbnailCreator = () => {
79 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
80
d9a2a031 81 return Promise.resolve()
374b725d
C
82 }
83
9cc8d43e 84 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
85}
86
1ef65f4c
C
87function createVideoMiniatureFromExisting (options: {
88 inputPath: string
89 video: MVideoThumbnail
90 type: ThumbnailType
91 automaticallyGenerated: boolean
65af03a2 92 size?: ImageSize
a35a2279 93 keepOriginal?: boolean // default to false
1ef65f4c 94}) {
a35a2279 95 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
1ef65f4c 96
e8bafea3 97 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
1ef65f4c 98 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
e8bafea3 99
a35a2279
C
100 return createThumbnailFromFunction({
101 thumbnailCreator,
102 filename,
103 height,
104 width,
105 type,
106 automaticallyGenerated,
107 existingThumbnail
108 })
e8bafea3
C
109}
110
a35a2279
C
111function generateVideoMiniature (options: {
112 video: MVideoThumbnail
113 videoFile: MVideoFile
114 type: ThumbnailType
115}) {
116 const { video, videoFile, type } = options
117
d7a25329 118 const input = getVideoFilePath(video, videoFile)
e8bafea3 119
536598cf
C
120 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
121 const thumbnailCreator = videoFile.isAudio()
122 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
123 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
e8bafea3 124
a35a2279
C
125 return createThumbnailFromFunction({
126 thumbnailCreator,
127 filename,
128 height,
129 width,
130 type,
131 automaticallyGenerated: true,
132 existingThumbnail
133 })
e8bafea3
C
134}
135
a35a2279
C
136function createPlaceholderThumbnail (options: {
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
C
166 generateVideoMiniature,
167 createVideoMiniatureFromUrl,
168 createVideoMiniatureFromExisting,
e8bafea3 169 createPlaceholderThumbnail,
3acc5084
C
170 createPlaylistMiniatureFromUrl,
171 createPlaylistMiniatureFromExisting
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) {
e8bafea3
C
203 const filename = video.generateThumbnailName()
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) {
217 const filename = video.generatePreviewName()
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
233async function createThumbnailFromFunction (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}