Fix lint
[oweals/peertube.git] / server / lib / activitypub / videos.ts
1 import { join } from 'path'
2 import * as request from 'request'
3 import { ActivityIconObject } from '../../../shared/index'
4 import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests'
5 import { CONFIG, REMOTE_SCHEME, STATIC_PATHS } from '../../initializers/constants'
6 import { VideoInstance } from '../../models/video/video-interface'
7
8 function fetchRemoteVideoPreview (video: VideoInstance) {
9   // FIXME: use url
10   const host = video.VideoChannel.Account.Server.host
11   const path = join(STATIC_PATHS.PREVIEWS, video.getPreviewName())
12
13   return request.get(REMOTE_SCHEME.HTTP + '://' + host + path)
14 }
15
16 async function fetchRemoteVideoDescription (video: VideoInstance) {
17   // FIXME: use url
18   const host = video.VideoChannel.Account.Server.host
19   const path = video.getDescriptionPath()
20   const options = {
21     uri: REMOTE_SCHEME.HTTP + '://' + host + path,
22     json: true
23   }
24
25   const { body } = await doRequest(options)
26   return body.description ? body.description : ''
27 }
28
29 function generateThumbnailFromUrl (video: VideoInstance, icon: ActivityIconObject) {
30   const thumbnailName = video.getThumbnailName()
31   const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
32
33   const options = {
34     method: 'GET',
35     uri: icon.url
36   }
37   return doRequestAndSaveToFile(options, thumbnailPath)
38 }
39
40 export {
41   fetchRemoteVideoPreview,
42   fetchRemoteVideoDescription,
43   generateThumbnailFromUrl
44 }