]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/thumbnail.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
1 import { join } from 'path'
2 import { ThumbnailType } from '@shared/models'
3 import { generateImageFilename, generateImageFromVideoFile, processImage } from '../helpers/image-utils'
4 import { CONFIG } from '../initializers/config'
5 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
6 import { ThumbnailModel } from '../models/video/thumbnail'
7 import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
8 import { MThumbnail } from '../types/models/video/thumbnail'
9 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
10 import { downloadImageFromWorker } from './local-actor'
11 import { VideoPathManager } from './video-path-manager'
12
13 type ImageSize = { height?: number, width?: number }
14
15 function updatePlaylistMiniatureFromExisting (options: {
16 inputPath: string
17 playlist: MVideoPlaylistThumbnail
18 automaticallyGenerated: boolean
19 keepOriginal?: boolean // default to false
20 size?: ImageSize
21 }) {
22 const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options
23 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
24 const type = ThumbnailType.MINIATURE
25
26 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
27 return updateThumbnailFromFunction({
28 thumbnailCreator,
29 filename,
30 height,
31 width,
32 type,
33 automaticallyGenerated,
34 existingThumbnail
35 })
36 }
37
38 function updatePlaylistMiniatureFromUrl (options: {
39 downloadUrl: string
40 playlist: MVideoPlaylistThumbnail
41 size?: ImageSize
42 }) {
43 const { downloadUrl, playlist, size } = options
44 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
45 const type = ThumbnailType.MINIATURE
46
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 = () => {
53 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
54 }
55
56 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
57 }
58
59 function updateVideoMiniatureFromUrl (options: {
60 downloadUrl: string
61 video: MVideoThumbnail
62 type: ThumbnailType
63 size?: ImageSize
64 }) {
65 const { downloadUrl, video, type, size } = options
66 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
67
68 // Only save the file URL if it is a remote video
69 const fileUrl = video.isOwned()
70 ? null
71 : downloadUrl
72
73 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
74
75 // Do not change the thumbnail filename if the file did not change
76 const filename = thumbnailUrlChanged
77 ? updatedFilename
78 : existingThumbnail.filename
79
80 const thumbnailCreator = () => {
81 if (thumbnailUrlChanged) {
82 return downloadImageFromWorker({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } })
83 }
84
85 return Promise.resolve()
86 }
87
88 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
89 }
90
91 function updateVideoMiniatureFromExisting (options: {
92 inputPath: string
93 video: MVideoThumbnail
94 type: ThumbnailType
95 automaticallyGenerated: boolean
96 size?: ImageSize
97 keepOriginal?: boolean // default to false
98 }) {
99 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
100
101 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
102 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
103
104 return updateThumbnailFromFunction({
105 thumbnailCreator,
106 filename,
107 height,
108 width,
109 type,
110 automaticallyGenerated,
111 existingThumbnail
112 })
113 }
114
115 function generateVideoMiniature (options: {
116 video: MVideoThumbnail
117 videoFile: MVideoFile
118 type: ThumbnailType
119 }) {
120 const { video, videoFile, type } = options
121
122 return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
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 })
138 })
139 }
140
141 function updatePlaceholderThumbnail (options: {
142 fileUrl: string
143 video: MVideoThumbnail
144 type: ThumbnailType
145 size: ImageSize
146 }) {
147 const { fileUrl, video, type, size } = options
148 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
149
150 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
151
152 const thumbnail = existingThumbnail || new ThumbnailModel()
153
154 // Do not change the thumbnail filename if the file did not change
155 const filename = thumbnailUrlChanged
156 ? updatedFilename
157 : existingThumbnail.filename
158
159 thumbnail.filename = filename
160 thumbnail.height = height
161 thumbnail.width = width
162 thumbnail.type = type
163 thumbnail.fileUrl = fileUrl
164
165 return thumbnail
166 }
167
168 // ---------------------------------------------------------------------------
169
170 export {
171 generateVideoMiniature,
172 updateVideoMiniatureFromUrl,
173 updateVideoMiniatureFromExisting,
174 updatePlaceholderThumbnail,
175 updatePlaylistMiniatureFromUrl,
176 updatePlaylistMiniatureFromExisting
177 }
178
179 function 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
188 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
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
202 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
203 const existingThumbnail = Array.isArray(video.Thumbnails)
204 ? video.Thumbnails.find(t => t.type === type)
205 : undefined
206
207 if (type === ThumbnailType.MINIATURE) {
208 const filename = generateImageFilename()
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) {
222 const filename = generateImageFilename()
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
238 async function updateThumbnailFromFunction (parameters: {
239 thumbnailCreator: () => Promise<any>
240 filename: string
241 height: number
242 width: number
243 type: ThumbnailType
244 automaticallyGenerated?: boolean
245 fileUrl?: string
246 existingThumbnail?: MThumbnail
247 }) {
248 const {
249 thumbnailCreator,
250 filename,
251 width,
252 height,
253 type,
254 existingThumbnail,
255 automaticallyGenerated = null,
256 fileUrl = null
257 } = parameters
258
259 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
260 ? existingThumbnail.filename
261 : undefined
262
263 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
264
265 thumbnail.filename = filename
266 thumbnail.height = height
267 thumbnail.width = width
268 thumbnail.type = type
269 thumbnail.fileUrl = fileUrl
270 thumbnail.automaticallyGenerated = automaticallyGenerated
271
272 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
273
274 await thumbnailCreator()
275
276 return thumbnail
277 }