diff options
author | Chocobozzz <me@florianbigard.com> | 2021-11-05 14:11:19 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-11-05 14:11:19 +0100 |
commit | dedcd583b2461895f816c3dd67d56f05d3274f6b (patch) | |
tree | 058880eda468f4282cefce275451aa76567a15b8 | |
parent | 3233acdadf34045b51da91d42bcd6b3cbf3036f4 (diff) | |
download | PeerTube-dedcd583b2461895f816c3dd67d56f05d3274f6b.tar.gz PeerTube-dedcd583b2461895f816c3dd67d56f05d3274f6b.tar.zst PeerTube-dedcd583b2461895f816c3dd67d56f05d3274f6b.zip |
Fix remote interaction
When we fetch a ressource that is a redirection of another ressource
-rw-r--r-- | server/controllers/api/search/search-video-channels.ts | 6 | ||||
-rw-r--r-- | server/controllers/api/search/search-video-playlists.ts | 6 | ||||
-rw-r--r-- | server/controllers/api/search/search-videos.ts | 7 | ||||
-rw-r--r-- | server/helpers/requests.ts | 11 |
4 files changed, 24 insertions, 6 deletions
diff --git a/server/controllers/api/search/search-video-channels.ts b/server/controllers/api/search/search-video-channels.ts index c9e81bffa..aa5afb46e 100644 --- a/server/controllers/api/search/search-video-channels.ts +++ b/server/controllers/api/search/search-video-channels.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { sanitizeUrl } from '@server/helpers/core-utils' | 2 | import { sanitizeUrl } from '@server/helpers/core-utils' |
3 | import { pickSearchChannelQuery } from '@server/helpers/query' | 3 | import { pickSearchChannelQuery } from '@server/helpers/query' |
4 | import { doJSONRequest } from '@server/helpers/requests' | 4 | import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' |
5 | import { CONFIG } from '@server/initializers/config' | 5 | import { CONFIG } from '@server/initializers/config' |
6 | import { WEBSERVER } from '@server/initializers/constants' | 6 | import { WEBSERVER } from '@server/initializers/constants' |
7 | import { Hooks } from '@server/lib/plugins/hooks' | 7 | import { Hooks } from '@server/lib/plugins/hooks' |
@@ -126,7 +126,9 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean | |||
126 | 126 | ||
127 | if (isUserAbleToSearchRemoteURI(res)) { | 127 | if (isUserAbleToSearchRemoteURI(res)) { |
128 | try { | 128 | try { |
129 | const actor = await getOrCreateAPActor(uri, 'all', true, true) | 129 | const latestUri = await findLatestRedirection(uri, { activityPub: true }) |
130 | |||
131 | const actor = await getOrCreateAPActor(latestUri, 'all', true, true) | ||
130 | videoChannel = actor.VideoChannel | 132 | videoChannel = actor.VideoChannel |
131 | } catch (err) { | 133 | } catch (err) { |
132 | logger.info('Cannot search remote video channel %s.', uri, { err }) | 134 | logger.info('Cannot search remote video channel %s.', uri, { err }) |
diff --git a/server/controllers/api/search/search-video-playlists.ts b/server/controllers/api/search/search-video-playlists.ts index 61a11c74a..e76d65fde 100644 --- a/server/controllers/api/search/search-video-playlists.ts +++ b/server/controllers/api/search/search-video-playlists.ts | |||
@@ -3,7 +3,7 @@ import { sanitizeUrl } from '@server/helpers/core-utils' | |||
3 | import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils' | 3 | import { isUserAbleToSearchRemoteURI } from '@server/helpers/express-utils' |
4 | import { logger } from '@server/helpers/logger' | 4 | import { logger } from '@server/helpers/logger' |
5 | import { pickSearchPlaylistQuery } from '@server/helpers/query' | 5 | import { pickSearchPlaylistQuery } from '@server/helpers/query' |
6 | import { doJSONRequest } from '@server/helpers/requests' | 6 | import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' |
7 | import { getFormattedObjects } from '@server/helpers/utils' | 7 | import { getFormattedObjects } from '@server/helpers/utils' |
8 | import { CONFIG } from '@server/initializers/config' | 8 | import { CONFIG } from '@server/initializers/config' |
9 | import { WEBSERVER } from '@server/initializers/constants' | 9 | import { WEBSERVER } from '@server/initializers/constants' |
@@ -105,7 +105,9 @@ async function searchVideoPlaylistsURI (search: string, res: express.Response) { | |||
105 | 105 | ||
106 | if (isUserAbleToSearchRemoteURI(res)) { | 106 | if (isUserAbleToSearchRemoteURI(res)) { |
107 | try { | 107 | try { |
108 | videoPlaylist = await getOrCreateAPVideoPlaylist(search) | 108 | const url = await findLatestRedirection(search, { activityPub: true }) |
109 | |||
110 | videoPlaylist = await getOrCreateAPVideoPlaylist(url) | ||
109 | } catch (err) { | 111 | } catch (err) { |
110 | logger.info('Cannot search remote video playlist %s.', search, { err }) | 112 | logger.info('Cannot search remote video playlist %s.', search, { err }) |
111 | } | 113 | } |
diff --git a/server/controllers/api/search/search-videos.ts b/server/controllers/api/search/search-videos.ts index 6db70acdf..68428d766 100644 --- a/server/controllers/api/search/search-videos.ts +++ b/server/controllers/api/search/search-videos.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { sanitizeUrl } from '@server/helpers/core-utils' | 2 | import { sanitizeUrl } from '@server/helpers/core-utils' |
3 | import { pickSearchVideoQuery } from '@server/helpers/query' | 3 | import { pickSearchVideoQuery } from '@server/helpers/query' |
4 | import { doJSONRequest } from '@server/helpers/requests' | 4 | import { doJSONRequest, findLatestRedirection } from '@server/helpers/requests' |
5 | import { CONFIG } from '@server/initializers/config' | 5 | import { CONFIG } from '@server/initializers/config' |
6 | import { WEBSERVER } from '@server/initializers/constants' | 6 | import { WEBSERVER } from '@server/initializers/constants' |
7 | import { getOrCreateAPVideo } from '@server/lib/activitypub/videos' | 7 | import { getOrCreateAPVideo } from '@server/lib/activitypub/videos' |
@@ -142,7 +142,10 @@ async function searchVideoURI (url: string, res: express.Response) { | |||
142 | refreshVideo: false | 142 | refreshVideo: false |
143 | } | 143 | } |
144 | 144 | ||
145 | const result = await getOrCreateAPVideo({ videoObject: url, syncParam }) | 145 | const result = await getOrCreateAPVideo({ |
146 | videoObject: await findLatestRedirection(url, { activityPub: true }), | ||
147 | syncParam | ||
148 | }) | ||
146 | video = result ? result.video : undefined | 149 | video = result ? result.video : undefined |
147 | } catch (err) { | 150 | } catch (err) { |
148 | logger.info('Cannot search remote video %s.', url, { err }) | 151 | logger.info('Cannot search remote video %s.', url, { err }) |
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 799034b90..57299eee1 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts | |||
@@ -184,6 +184,16 @@ function isBinaryResponse (result: Response<any>) { | |||
184 | return BINARY_CONTENT_TYPES.has(result.headers['content-type']) | 184 | return BINARY_CONTENT_TYPES.has(result.headers['content-type']) |
185 | } | 185 | } |
186 | 186 | ||
187 | async function findLatestRedirection (url: string, options: PeerTubeRequestOptions, iteration = 1) { | ||
188 | if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url) | ||
189 | |||
190 | const { headers } = await peertubeGot(url, { followRedirect: false, ...buildGotOptions(options) }) | ||
191 | |||
192 | if (headers.location) return findLatestRedirection(headers.location, options, iteration + 1) | ||
193 | |||
194 | return url | ||
195 | } | ||
196 | |||
187 | // --------------------------------------------------------------------------- | 197 | // --------------------------------------------------------------------------- |
188 | 198 | ||
189 | export { | 199 | export { |
@@ -192,6 +202,7 @@ export { | |||
192 | doRequestAndSaveToFile, | 202 | doRequestAndSaveToFile, |
193 | isBinaryResponse, | 203 | isBinaryResponse, |
194 | downloadImage, | 204 | downloadImage, |
205 | findLatestRedirection, | ||
195 | peertubeGot | 206 | peertubeGot |
196 | } | 207 | } |
197 | 208 | ||