PeerTube/server/lib/activitypub/videos/shared/url-to-object.ts

26 lines
921 B
TypeScript
Raw Normal View History

2021-06-02 15:47:05 +02:00
import { sanitizeAndCheckVideoTorrentObject } from '@server/helpers/custom-validators/activitypub/videos'
2021-06-02 16:49:59 +02:00
import { logger, loggerTagsFactory } from '@server/helpers/logger'
2021-06-02 15:47:05 +02:00
import { VideoObject } from '@shared/models'
import { fetchAP } from '../../activity'
import { checkUrlsSameHost } from '../../url'
2021-06-02 15:47:05 +02:00
2021-06-02 16:49:59 +02:00
const lTags = loggerTagsFactory('ap', 'video')
2021-06-02 15:47:05 +02:00
async function fetchRemoteVideo (videoUrl: string): Promise<{ statusCode: number, videoObject: VideoObject }> {
2021-06-02 16:49:59 +02:00
logger.info('Fetching remote video %s.', videoUrl, lTags(videoUrl))
2021-06-02 15:47:05 +02:00
const { statusCode, body } = await fetchAP<any>(videoUrl)
2021-06-02 15:47:05 +02:00
if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) {
2021-06-02 16:49:59 +02:00
logger.debug('Remote video JSON is not valid.', { body, ...lTags(videoUrl) })
2021-06-02 15:47:05 +02:00
return { statusCode, videoObject: undefined }
}
return { statusCode, videoObject: body }
}
export {
fetchRemoteVideo
}