]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Try to fix tests
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
CommitLineData
e8bafea3
C
1import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
2import { CONFIG } from '../initializers/config'
453e83ea 3import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
e8bafea3
C
4import { ThumbnailModel } from '../models/video/thumbnail'
5import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
6import { processImage } from '../helpers/image-utils'
7import { join } from 'path'
8import { downloadImage } from '../helpers/requests'
453e83ea
C
9import { MVideoPlaylistThumbnail } from '../typings/models/video/video-playlist'
10import { MVideoFile, MVideoThumbnail } from '../typings/models'
11import { MThumbnail } from '../typings/models/video/thumbnail'
e8bafea3
C
12
13type ImageSize = { height: number, width: number }
14
65af03a2
C
15function createPlaylistMiniatureFromExisting (
16 inputPath: string,
453e83ea 17 playlist: MVideoPlaylistThumbnail,
65af03a2
C
18 automaticallyGenerated: boolean,
19 keepOriginal = false,
20 size?: ImageSize
21) {
e8bafea3 22 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 23 const type = ThumbnailType.MINIATURE
e8bafea3 24
2fb5b3a5 25 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal)
65af03a2 26 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
e8bafea3
C
27}
28
453e83ea 29function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
e8bafea3 30 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
3acc5084 31 const type = ThumbnailType.MINIATURE
e8bafea3 32
9cc8d43e
C
33 const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
34 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
35}
36
453e83ea 37function createVideoMiniatureFromUrl (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3 38 const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
9cc8d43e 39 const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
e8bafea3 40
9cc8d43e 41 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
e8bafea3
C
42}
43
65af03a2
C
44function createVideoMiniatureFromExisting (
45 inputPath: string,
453e83ea 46 video: MVideoThumbnail,
65af03a2
C
47 type: ThumbnailType,
48 automaticallyGenerated: boolean,
49 size?: ImageSize
50) {
e8bafea3 51 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
2fb5b3a5 52 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height })
e8bafea3 53
65af03a2 54 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
e8bafea3
C
55}
56
453e83ea 57function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
e8bafea3
C
58 const input = video.getVideoFilePath(videoFile)
59
536598cf
C
60 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
61 const thumbnailCreator = videoFile.isAudio()
62 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true)
63 : () => generateImageFromVideoFile(input, basePath, filename, { height, width })
e8bafea3 64
65af03a2 65 return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
e8bafea3
C
66}
67
453e83ea 68function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
e8bafea3
C
69 const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
70
71 const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel()
72
73 thumbnail.filename = filename
74 thumbnail.height = height
75 thumbnail.width = width
76 thumbnail.type = type
9cc8d43e 77 thumbnail.fileUrl = fileUrl
e8bafea3
C
78
79 return thumbnail
80}
81
82// ---------------------------------------------------------------------------
83
84export {
3acc5084
C
85 generateVideoMiniature,
86 createVideoMiniatureFromUrl,
87 createVideoMiniatureFromExisting,
e8bafea3 88 createPlaceholderThumbnail,
3acc5084
C
89 createPlaylistMiniatureFromUrl,
90 createPlaylistMiniatureFromExisting
e8bafea3
C
91}
92
453e83ea 93function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
e8bafea3
C
94 const filename = playlist.generateThumbnailName()
95 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
96
97 return {
98 filename,
99 basePath,
100 existingThumbnail: playlist.Thumbnail,
101 outputPath: join(basePath, filename),
102 height: size ? size.height : THUMBNAILS_SIZE.height,
103 width: size ? size.width : THUMBNAILS_SIZE.width
104 }
105}
106
453e83ea 107function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
e8bafea3
C
108 const existingThumbnail = Array.isArray(video.Thumbnails)
109 ? video.Thumbnails.find(t => t.type === type)
110 : undefined
111
3acc5084 112 if (type === ThumbnailType.MINIATURE) {
e8bafea3
C
113 const filename = video.generateThumbnailName()
114 const basePath = CONFIG.STORAGE.THUMBNAILS_DIR
115
116 return {
117 filename,
118 basePath,
119 existingThumbnail,
120 outputPath: join(basePath, filename),
121 height: size ? size.height : THUMBNAILS_SIZE.height,
122 width: size ? size.width : THUMBNAILS_SIZE.width
123 }
124 }
125
126 if (type === ThumbnailType.PREVIEW) {
127 const filename = video.generatePreviewName()
128 const basePath = CONFIG.STORAGE.PREVIEWS_DIR
129
130 return {
131 filename,
132 basePath,
133 existingThumbnail,
134 outputPath: join(basePath, filename),
135 height: size ? size.height : PREVIEWS_SIZE.height,
136 width: size ? size.width : PREVIEWS_SIZE.width
137 }
138 }
139
140 return undefined
141}
142
143async function createThumbnailFromFunction (parameters: {
144 thumbnailCreator: () => Promise<any>,
145 filename: string,
146 height: number,
147 width: number,
148 type: ThumbnailType,
65af03a2 149 automaticallyGenerated?: boolean,
9cc8d43e 150 fileUrl?: string,
453e83ea 151 existingThumbnail?: MThumbnail
e8bafea3 152}) {
65af03a2 153 const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
e8bafea3
C
154
155 const thumbnail = existingThumbnail ? existingThumbnail : new ThumbnailModel()
156
157 thumbnail.filename = filename
158 thumbnail.height = height
159 thumbnail.width = width
160 thumbnail.type = type
9cc8d43e 161 thumbnail.fileUrl = fileUrl
65af03a2 162 thumbnail.automaticallyGenerated = automaticallyGenerated
e8bafea3
C
163
164 await thumbnailCreator()
165
166 return thumbnail
167}