]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Fix infohash with object storage
[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'
84531547 4import { generateImageFilename, processImage } from '../helpers/image-utils'
a35a2279 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'
0305db28 12import { VideoPathManager } from './video-path-manager'
e8bafea3 13
84531547 14type ImageSize = { height?: number, width?: number }
e8bafea3 15
91f8f8db 16function updatePlaylistMiniatureFromExisting (options: {
a35a2279
C
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)
91f8f8db 28 return updateThumbnailFromFunction({
a35a2279
C
29 thumbnailCreator,
30 filename,
31 height,
32 width,
33 type,
34 automaticallyGenerated,
35 existingThumbnail
36 })
e8bafea3
C
37}
38
91f8f8db 39function updatePlaylistMiniatureFromUrl (options: {
a35a2279
C
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 })
91f8f8db 54 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
55}
56
91f8f8db 57function updateVideoMiniatureFromUrl (options: {
a35a2279
C
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
91f8f8db 84 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
85}
86
91f8f8db 87function updateVideoMiniatureFromExisting (options: {
1ef65f4c
C
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
91f8f8db 100 return updateThumbnailFromFunction({
a35a2279
C
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
0305db28
JB
118 return VideoPathManager.Instance.makeAvailableVideoFile(video, videoFile, input => {
119 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
120
121 const thumbnailCreator = videoFile.isAudio()
122 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
123 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
124
125 return updateThumbnailFromFunction({
126 thumbnailCreator,
127 filename,
128 height,
129 width,
130 type,
131 automaticallyGenerated: true,
132 existingThumbnail
133 })
a35a2279 134 })
e8bafea3
C
135}
136
91f8f8db 137function updatePlaceholderThumbnail (options: {
a35a2279
C
138 fileUrl: string
139 video: MVideoThumbnail
140 type: ThumbnailType
141 size: ImageSize
142}) {
143 const { fileUrl, video, type, size } = options
1ef447bd
C
144 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
145
146 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
e8bafea3 147
a1587156 148 const thumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3 149
1ef447bd
C
150 // Do not change the thumbnail filename if the file did not change
151 const filename = thumbnailUrlChanged
152 ? updatedFilename
153 : existingThumbnail.filename
154
e8bafea3
C
155 thumbnail.filename = filename
156 thumbnail.height = height
157 thumbnail.width = width
158 thumbnail.type = type
9cc8d43e 159 thumbnail.fileUrl = fileUrl
e8bafea3
C
160
161 return thumbnail
162}
163
164// ---------------------------------------------------------------------------
165
166export {
3acc5084 167 generateVideoMiniature,
91f8f8db
C
168 updateVideoMiniatureFromUrl,
169 updateVideoMiniatureFromExisting,
170 updatePlaceholderThumbnail,
171 updatePlaylistMiniatureFromUrl,
172 updatePlaylistMiniatureFromExisting
e8bafea3
C
173}
174
1ef447bd
C
175function hasThumbnailUrlChanged (existingThumbnail: MThumbnail, downloadUrl: string, video: MVideoUUID) {
176 const existingUrl = existingThumbnail
177 ? existingThumbnail.fileUrl
178 : null
179
180 // If the thumbnail URL did not change and has a unique filename (introduced in 3.1), avoid thumbnail processing
181 return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`)
182}
183
453e83ea 184function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
e8bafea3
C
185 const filename = playlist.generateThumbnailName()
186 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
187
188 return {
189 filename,
190 basePath,
191 existingThumbnail: playlist.Thumbnail,
192 outputPath: join(basePath, filename),
193 height: size ? size.height : THUMBNAILS_SIZE.height,
194 width: size ? size.width : THUMBNAILS_SIZE.width
195 }
196}
197
453e83ea 198function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3
C
199 const existingThumbnail = Array.isArray(video.Thumbnails)
200 ? video.Thumbnails.find(t => t.type === type)
201 : undefined
202
3acc5084 203 if (type === ThumbnailType.MINIATURE) {
84531547 204 const filename = generateImageFilename()
e8bafea3
C
205 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
206
207 return {
208 filename,
209 basePath,
210 existingThumbnail,
211 outputPath: join(basePath, filename),
212 height: size ? size.height : THUMBNAILS_SIZE.height,
213 width: size ? size.width : THUMBNAILS_SIZE.width
214 }
215 }
216
217 if (type === ThumbnailType.PREVIEW) {
84531547 218 const filename = generateImageFilename()
e8bafea3
C
219 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
220
221 return {
222 filename,
223 basePath,
224 existingThumbnail,
225 outputPath: join(basePath, filename),
226 height: size ? size.height : PREVIEWS_SIZE.height,
227 width: size ? size.width : PREVIEWS_SIZE.width
228 }
229 }
230
231 return undefined
232}
233
91f8f8db 234async function updateThumbnailFromFunction (parameters: {
a1587156
C
235 thumbnailCreator: () => Promise<any>
236 filename: string
237 height: number
238 width: number
239 type: ThumbnailType
240 automaticallyGenerated?: boolean
241 fileUrl?: string
453e83ea 242 existingThumbnail?: MThumbnail
e8bafea3 243}) {
a35a2279
C
244 const {
245 thumbnailCreator,
246 filename,
247 width,
248 height,
249 type,
250 existingThumbnail,
251 automaticallyGenerated = null,
252 fileUrl = null
253 } = parameters
254
d9a2a031 255 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
a35a2279
C
256 ? existingThumbnail.filename
257 : undefined
6302d599 258
a35a2279 259 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
e8bafea3
C
260
261 thumbnail.filename = filename
262 thumbnail.height = height
263 thumbnail.width = width
264 thumbnail.type = type
9cc8d43e 265 thumbnail.fileUrl = fileUrl
65af03a2 266 thumbnail.automaticallyGenerated = automaticallyGenerated
d9a2a031
C
267
268 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
e8bafea3
C
269
270 await thumbnailCreator()
271
272 return thumbnail
273}