1
0
Fork 0

Fix remote interaction

When we fetch a ressource that is a redirection of another ressource
This commit is contained in:
Chocobozzz 2021-11-05 14:11:19 +01:00
parent 3233acdadf
commit dedcd583b2
No known key found for this signature in database
GPG key ID: 583A612D890159BE
4 changed files with 24 additions and 6 deletions

View file

@ -1,7 +1,7 @@
import express from 'express'
import { sanitizeUrl } from '@server/helpers/core-utils'
import { pickSearchChannelQuery } from '@server/helpers/query'
import { doJSONRequest } from '@server/helpers/requests'
import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests'
import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants'
import { Hooks } from '@server/lib/plugins/hooks'
@ -126,7 +126,9 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean
if (isUserAbleToSearchRemoteURI(res)) {
try {
const actor = await getOrCreateAPActor(uri, 'all', true, true)
const latestUri = await findLatestRedirection(uri, { activityPub: true })
const actor = await getOrCreateAPActor(latestUri, 'all', true, true)
videoChannel = actor.VideoChannel
} catch (err) {
logger.info('Cannot search remote video channel %s.', uri, { err })

View file

@ -3,7 +3,7 @@ import { sanitizeUrl } from '@server/helpers/core-utils'
import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils'
import { logger } from '@server/helpers/logger'
import { pickSearchPlaylistQuery } from '@server/helpers/query'
import { doJSONRequest } from '@server/helpers/requests'
import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests'
import { getFormattedObjects } from '@server/helpers/utils'
import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants'
@ -105,7 +105,9 @@ async function searchVideoPlaylistsURI (search: string, res: express.Response) {
if (isUserAbleToSearchRemoteURI(res)) {
try {
videoPlaylist = await getOrCreateAPVideoPlaylist(search)
const url = await findLatestRedirection(search, { activityPub: true })
videoPlaylist = await getOrCreateAPVideoPlaylist(url)
} catch (err) {
logger.info('Cannot search remote video playlist %s.', search, { err })
}

View file

@ -1,7 +1,7 @@
import express from 'express'
import { sanitizeUrl } from '@server/helpers/core-utils'
import { pickSearchVideoQuery } from '@server/helpers/query'
import { doJSONRequest } from '@server/helpers/requests'
import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests'
import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants'
import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
@ -142,7 +142,10 @@ async function searchVideoURI (url: string, res: express.Response) {
refreshVideo: false
}
const result = await getOrCreateAPVideo({ videoObject: url, syncParam })
const result = await getOrCreateAPVideo({
videoObject: await findLatestRedirection(url, { activityPub: true }),
syncParam
})
video = result ? result.video : undefined
} catch (err) {
logger.info('Cannot search remote video %s.', url, { err })

View file

@ -184,6 +184,16 @@ function isBinaryResponse (result: Response<any>) {
return BINARY_CONTENT_TYPES.has(result.headers['content-type'])
}
async function findLatestRedirection (url: string, options: PeerTubeRequestOptions, iteration = 1) {
if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url)
const { headers } = await peertubeGot(url, { followRedirect: false, ...buildGotOptions(options) })
if (headers.location) return findLatestRedirection(headers.location, options, iteration + 1)
return url
}
// ---------------------------------------------------------------------------
export {
@ -192,6 +202,7 @@ export {
doRequestAndSaveToFile,
isBinaryResponse,
downloadImage,
findLatestRedirection,
peertubeGot
}