]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/thumbnail.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / lib / thumbnail.ts
CommitLineData
e8bafea3
C
1import { VideoFileModel } from '../models/video/video-file'
2import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
3import { CONFIG } from '../initializers/config'
536598cf 4import { PREVIEWS_SIZE, THUMBNAILS_SIZE, ASSETS_PATH } from '../initializers/constants'
e8bafea3
C
5import { VideoModel } from '../models/video/video'
6import { ThumbnailModel } from '../models/video/thumbnail'
7import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
8import { processImage } from '../helpers/image-utils'
9import { join } from 'path'
10import { downloadImage } from '../helpers/requests'
11import { VideoPlaylistModel } from '../models/video/video-playlist'
12
13type ImageSize = { height: number, width: number }
14
65af03a2
C
15function createPlaylistMiniatureFromExisting (
16 inputPath: string,
17 playlist: VideoPlaylistModel,
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
9cc8d43e 29function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: VideoPlaylistModel, 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
9cc8d43e 37function createVideoMiniatureFromUrl (fileUrl: string, video: VideoModel, 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,
46 video: VideoModel,
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
3acc5084 57function generateVideoMiniature (video: VideoModel, videoFile: VideoFileModel, 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
9cc8d43e 68function createPlaceholderThumbnail (fileUrl: string, video: VideoModel, 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
93function buildMetadataFromPlaylist (playlist: VideoPlaylistModel, size: ImageSize) {
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
107function buildMetadataFromVideo (video: VideoModel, type: ThumbnailType, size?: ImageSize) {
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,
e8bafea3
C
151 existingThumbnail?: ThumbnailModel
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}