Split types and typings
[oweals/peertube.git] / server / lib / thumbnail.ts
1 import { generateImageFromVideoFile } from '../helpers/ffmpeg-utils'
2 import { CONFIG } from '../initializers/config'
3 import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
4 import { ThumbnailModel } from '../models/video/thumbnail'
5 import { ThumbnailType } from '../../shared/models/videos/thumbnail.type'
6 import { processImage } from '../helpers/image-utils'
7 import { join } from 'path'
8 import { downloadImage } from '../helpers/requests'
9 import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
10 import { MVideoFile, MVideoThumbnail } from '../types/models'
11 import { MThumbnail } from '../types/models/video/thumbnail'
12 import { getVideoFilePath } from './video-paths'
13
14 type ImageSize = { height: number, width: number }
15
16 function createPlaylistMiniatureFromExisting (
17   inputPath: string,
18   playlist: MVideoPlaylistThumbnail,
19   automaticallyGenerated: boolean,
20   keepOriginal = false,
21   size?: ImageSize
22 ) {
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 createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
28 }
29
30 function createPlaylistMiniatureFromUrl (fileUrl: string, playlist: MVideoPlaylistThumbnail, size?: ImageSize) {
31   const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
32   const type = ThumbnailType.MINIATURE
33
34   const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
35   return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
36 }
37
38 function createVideoMiniatureFromUrl (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
39   const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
40   const thumbnailCreator = () => downloadImage(fileUrl, basePath, filename, { width, height })
41
42   return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl })
43 }
44
45 function createVideoMiniatureFromExisting (
46   inputPath: string,
47   video: MVideoThumbnail,
48   type: ThumbnailType,
49   automaticallyGenerated: boolean,
50   size?: ImageSize
51 ) {
52   const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
53   const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height })
54
55   return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated, existingThumbnail })
56 }
57
58 function generateVideoMiniature (video: MVideoThumbnail, videoFile: MVideoFile, type: ThumbnailType) {
59   const input = getVideoFilePath(video, videoFile)
60
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 })
65
66   return createThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, automaticallyGenerated: true, existingThumbnail })
67 }
68
69 function createPlaceholderThumbnail (fileUrl: string, video: MVideoThumbnail, type: ThumbnailType, size: ImageSize) {
70   const { filename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
71
72   const thumbnail = existingThumbnail || new ThumbnailModel()
73
74   thumbnail.filename = filename
75   thumbnail.height = height
76   thumbnail.width = width
77   thumbnail.type = type
78   thumbnail.fileUrl = fileUrl
79
80   return thumbnail
81 }
82
83 // ---------------------------------------------------------------------------
84
85 export {
86   generateVideoMiniature,
87   createVideoMiniatureFromUrl,
88   createVideoMiniatureFromExisting,
89   createPlaceholderThumbnail,
90   createPlaylistMiniatureFromUrl,
91   createPlaylistMiniatureFromExisting
92 }
93
94 function buildMetadataFromPlaylist (playlist: MVideoPlaylistThumbnail, size: ImageSize) {
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
108 function buildMetadataFromVideo (video: MVideoThumbnail, type: ThumbnailType, size?: ImageSize) {
109   const existingThumbnail = Array.isArray(video.Thumbnails)
110     ? video.Thumbnails.find(t => t.type === type)
111     : undefined
112
113   if (type === ThumbnailType.MINIATURE) {
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
144 async function createThumbnailFromFunction (parameters: {
145   thumbnailCreator: () => Promise<any>
146   filename: string
147   height: number
148   width: number
149   type: ThumbnailType
150   automaticallyGenerated?: boolean
151   fileUrl?: string
152   existingThumbnail?: MThumbnail
153 }) {
154   const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters
155
156   const thumbnail = existingThumbnail || new ThumbnailModel()
157
158   thumbnail.filename = filename
159   thumbnail.height = height
160   thumbnail.width = width
161   thumbnail.type = type
162   thumbnail.fileUrl = fileUrl
163   thumbnail.automaticallyGenerated = automaticallyGenerated
164
165   await thumbnailCreator()
166
167   return thumbnail
168 }