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