]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/thumbnail.ts
Rename studio to editor
[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 { downloadImage } from '../helpers/requests'
5 import { CONFIG } from '../initializers/config'
6 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
7 import { ThumbnailModel } from '../models/video/thumbnail'
8 import { MVideoFile, MVideoThumbnail, MVideoUUID } from '../types/models'
9 import { MThumbnail } from '../types/models/video/thumbnail'
10 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
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 = () => downloadImage(downloadUrl, basePath, filename, { width, height })
53 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
54 }
55
56 function updateVideoMiniatureFromUrl (options: {
57 downloadUrl: string
58 video: MVideoThumbnail
59 type: ThumbnailType
60 size?: ImageSize
61 }) {
62 const { downloadUrl, video, type, size } = options
63 const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
64
65 // Only save the file URL if it is a remote video
66 const fileUrl = video.isOwned()
67 ? null
68 : downloadUrl
69
70 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video)
71
72 // Do not change the thumbnail filename if the file did not change
73 const filename = thumbnailUrlChanged
74 ? updatedFilename
75 : existingThumbnail.filename
76
77 const thumbnailCreator = () => {
78 if (thumbnailUrlChanged) return downloadImage(downloadUrl, basePath, filename, { width, height })
79
80 return Promise.resolve()
81 }
82
83 return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
84 }
85
86 function updateVideoMiniatureFromExisting (options: {
87 inputPath: string
88 video: MVideoThumbnail
89 type: ThumbnailType
90 automaticallyGenerated: boolean
91 size?: ImageSize
92 keepOriginal?: boolean // default to false
93 }) {
94 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
95
96 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
97 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
98
99 return updateThumbnailFromFunction({
100 thumbnailCreator,
101 filename,
102 height,
103 width,
104 type,
105 automaticallyGenerated,
106 existingThumbnail
107 })
108 }
109
110 function generateVideoMiniature (options: {
111 video: MVideoThumbnail
112 videoFile: MVideoFile
113 type: ThumbnailType
114 }) {
115 const { video, videoFile, type } = options
116
117 return VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
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 })
133 })
134 }
135
136 function updatePlaceholderThumbnail (options: {
137 fileUrl: string
138 video: MVideoThumbnail
139 type: ThumbnailType
140 size: ImageSize
141 }) {
142 const { fileUrl, video, type, size } = options
143 const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
144
145 const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video)
146
147 const thumbnail = existingThumbnail || new ThumbnailModel()
148
149 // Do not change the thumbnail filename if the file did not change
150 const filename = thumbnailUrlChanged
151 ? updatedFilename
152 : existingThumbnail.filename
153
154 thumbnail.filename = filename
155 thumbnail.height = height
156 thumbnail.width = width
157 thumbnail.type = type
158 thumbnail.fileUrl = fileUrl
159
160 return thumbnail
161 }
162
163 // ---------------------------------------------------------------------------
164
165 export {
166 generateVideoMiniature,
167 updateVideoMiniatureFromUrl,
168 updateVideoMiniatureFromExisting,
169 updatePlaceholderThumbnail,
170 updatePlaylistMiniatureFromUrl,
171 updatePlaylistMiniatureFromExisting
172 }
173
174 function 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
183 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
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
197 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
198 const existingThumbnail = Array.isArray(video.Thumbnails)
199 ? video.Thumbnails.find(t => t.type === type)
200 : undefined
201
202 if (type === ThumbnailType.MINIATURE) {
203 const filename = generateImageFilename()
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 = generateImageFilename()
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
233 async function updateThumbnailFromFunction (parameters: {
234 thumbnailCreator: () => Promise<any>
235 filename: string
236 height: number
237 width: number
238 type: ThumbnailType
239 automaticallyGenerated?: boolean
240 fileUrl?: string
241 existingThumbnail?: MThumbnail
242 }) {
243 const {
244 thumbnailCreator,
245 filename,
246 width,
247 height,
248 type,
249 existingThumbnail,
250 automaticallyGenerated = null,
251 fileUrl = null
252 } = parameters
253
254 const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
255 ? existingThumbnail.filename
256 : undefined
257
258 const thumbnail: MThumbnail = existingThumbnail || new ThumbnailModel()
259
260 thumbnail.filename = filename
261 thumbnail.height = height
262 thumbnail.width = width
263 thumbnail.type = type
264 thumbnail.fileUrl = fileUrl
265 thumbnail.automaticallyGenerated = automaticallyGenerated
266
267 if (oldFilename) thumbnail.previousThumbnailFilename = oldFilename
268
269 await thumbnailCreator()
270
271 return thumbnail
272 }