Fix remote interaction

When we fetch a ressource that is a redirection of another ressource
pull/4529/head
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 express from 'express'
import { sanitizeUrl } from '@server/helpers/core-utils' import { sanitizeUrl } from '@server/helpers/core-utils'
import { pickSearchChannelQuery } from '@server/helpers/query' 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 { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants' import { WEBSERVER } from '@server/initializers/constants'
import { Hooks } from '@server/lib/plugins/hooks' import { Hooks } from '@server/lib/plugins/hooks'
@ -126,7 +126,9 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean
if (isUserAbleToSearchRemoteURI(res)) { if (isUserAbleToSearchRemoteURI(res)) {
try { 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 videoChannel = actor.VideoChannel
} catch (err) { } catch (err) {
logger.info('Cannot search remote video channel %s.', uri, { 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 { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils'
import { logger } from '@server/helpers/logger' import { logger } from '@server/helpers/logger'
import { pickSearchPlaylistQuery } from '@server/helpers/query' 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 { getFormattedObjects } from '@server/helpers/utils'
import { CONFIG } from '@server/initializers/config' import { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants' import { WEBSERVER } from '@server/initializers/constants'
@ -105,7 +105,9 @@ async function searchVideoPlaylistsURI (search: string, res: express.Response) {
if (isUserAbleToSearchRemoteURI(res)) { if (isUserAbleToSearchRemoteURI(res)) {
try { try {
videoPlaylist = await getOrCreateAPVideoPlaylist(search) const url = await findLatestRedirection(search, { activityPub: true })
videoPlaylist = await getOrCreateAPVideoPlaylist(url)
} catch (err) { } catch (err) {
logger.info('Cannot search remote video playlist %s.', search, { err }) logger.info('Cannot search remote video playlist %s.', search, { err })
} }

View File

@ -1,7 +1,7 @@
import express from 'express' import express from 'express'
import { sanitizeUrl } from '@server/helpers/core-utils' import { sanitizeUrl } from '@server/helpers/core-utils'
import { pickSearchVideoQuery } from '@server/helpers/query' 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 { CONFIG } from '@server/initializers/config'
import { WEBSERVER } from '@server/initializers/constants' import { WEBSERVER } from '@server/initializers/constants'
import { getOrCreateAPVideo } from '@server/lib/activitypub/videos' import { getOrCreateAPVideo } from '@server/lib/activitypub/videos'
@ -142,7 +142,10 @@ async function searchVideoURI (url: string, res: express.Response) {
refreshVideo: false 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 video = result ? result.video : undefined
} catch (err) { } catch (err) {
logger.info('Cannot search remote video %s.', url, { 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']) 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 { export {
@ -192,6 +202,7 @@ export {
doRequestAndSaveToFile, doRequestAndSaveToFile,
isBinaryResponse, isBinaryResponse,
downloadImage, downloadImage,
findLatestRedirection,
peertubeGot peertubeGot
} }