]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/videos/shared/url-to-object.ts
Refactor getOrCreateAPVideo
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / url-to-object.ts
1 import { checkUrlsSameHost } from '@server/helpers/activitypub'
2 import { sanitizeAndCheckVideoTorrentObject } from '@server/helpers/custom-validators/activitypub/videos'
3 import { logger } from '@server/helpers/logger'
4 import { doJSONRequest } from '@server/helpers/requests'
5 import { VideoObject } from '@shared/models'
6
7 async function fetchRemoteVideo (videoUrl: string): Promise<{ statusCode: number, videoObject: VideoObject }> {
8 logger.info('Fetching remote video %s.', videoUrl)
9
10 const { statusCode, body } = await doJSONRequest<any>(videoUrl, { activityPub: true })
11
12 if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) {
13 logger.debug('Remote video JSON is not valid.', { body })
14 return { statusCode, videoObject: undefined }
15 }
16
17 return { statusCode, videoObject: body }
18 }
19
20 export {
21 fetchRemoteVideo
22 }