From a5296d6e5a93ab8046b41f874d70ab9971608d67 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 25 Apr 2022 07:42:59 +0100 Subject: [PATCH] Add support for a different address for proxying images. (#252) --- src/main/java/me/kavin/piped/ServerLauncher.java | 2 +- src/main/java/me/kavin/piped/consts/Constants.java | 3 +++ .../java/me/kavin/piped/utils/ResponseHelper.java | 13 ++++++------- src/main/java/me/kavin/piped/utils/URLUtils.java | 10 +++++++++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/me/kavin/piped/ServerLauncher.java b/src/main/java/me/kavin/piped/ServerLauncher.java index 21732ec..4102b74 100644 --- a/src/main/java/me/kavin/piped/ServerLauncher.java +++ b/src/main/java/me/kavin/piped/ServerLauncher.java @@ -370,7 +370,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher { HttpResponse response = HttpResponse.ofCode(code).withBody(body).withHeader(CONTENT_TYPE, contentType) .withHeader(CACHE_CONTROL, cache); if (prefetchProxy) - response = response.withHeader(LINK, String.format("<%s>; rel=preconnect", Constants.PROXY_PART)); + response = response.withHeader(LINK, String.format("<%s>; rel=preconnect", Constants.IMAGE_PROXY_PART)); return response; } diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 90da1f1..40c5b84 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -26,6 +26,8 @@ public class Constants { public static final String PROXY_PART; + public static final String IMAGE_PROXY_PART; + public static final String CAPTCHA_BASE_URL, CAPTCHA_API_KEY; public static final StreamingService YOUTUBE_SERVICE; @@ -61,6 +63,7 @@ public class Constants { HTTP_WORKERS = getProperty(prop, "HTTP_WORKERS", String.valueOf(Runtime.getRuntime().availableProcessors())); PROXY_PART = getProperty(prop, "PROXY_PART"); + IMAGE_PROXY_PART = getProperty(prop, "IMAGE_PROXY_PART", PROXY_PART); CAPTCHA_BASE_URL = getProperty(prop, "CAPTCHA_BASE_URL"); CAPTCHA_API_KEY = getProperty(prop, "CAPTCHA_API_KEY"); PUBLIC_URL = getProperty(prop, "API_URL"); diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 1eb7692..e1e6fac 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -56,8 +56,7 @@ import java.util.concurrent.TimeUnit; import static java.nio.charset.StandardCharsets.UTF_8; import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE; -import static me.kavin.piped.utils.URLUtils.rewriteURL; -import static me.kavin.piped.utils.URLUtils.substringYouTube; +import static me.kavin.piped.utils.URLUtils.*; import static org.schabi.newpipe.extractor.NewPipe.getPreferredContentCountry; import static org.schabi.newpipe.extractor.NewPipe.getPreferredLocalization; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse; @@ -126,17 +125,17 @@ public class ResponseHelper { boolean livestream = info.getStreamType() == StreamType.LIVE_STREAM; if (!livestream) { - info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteURL(stream.getUrl()), + info.getVideoOnlyStreams().forEach(stream -> videoStreams.add(new PipedStream(rewriteVideoURL(stream.getUrl()), String.valueOf(stream.getFormat()), stream.getResolution(), stream.getFormat().getMimeType(), true, stream.getBitrate(), stream.getInitStart(), stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec(), stream.getWidth(), stream.getHeight(), 30))); info.getVideoStreams() .forEach(stream -> videoStreams - .add(new PipedStream(rewriteURL(stream.getUrl()), String.valueOf(stream.getFormat()), + .add(new PipedStream(rewriteVideoURL(stream.getUrl()), String.valueOf(stream.getFormat()), stream.getResolution(), stream.getFormat().getMimeType(), false))); info.getAudioStreams() - .forEach(stream -> audioStreams.add(new PipedStream(rewriteURL(stream.getUrl()), + .forEach(stream -> audioStreams.add(new PipedStream(rewriteVideoURL(stream.getUrl()), String.valueOf(stream.getFormat()), stream.getAverageBitrate() + " kbps", stream.getFormat().getMimeType(), false, stream.getBitrate(), stream.getInitStart(), stream.getInitEnd(), stream.getIndexStart(), stream.getIndexEnd(), stream.getCodec()))); @@ -167,8 +166,8 @@ public class ResponseHelper { info.getTextualUploadDate(), info.getUploaderName(), substringYouTube(info.getUploaderUrl()), rewriteURL(info.getUploaderAvatarUrl()), rewriteURL(info.getThumbnailUrl()), info.getDuration(), info.getViewCount(), info.getLikeCount(), info.getDislikeCount(), info.getUploaderSubscriberCount(), info.isUploaderVerified(), - audioStreams, videoStreams, relatedStreams, subtitles, livestream, rewriteURL(info.getHlsUrl()), - rewriteURL(info.getDashMpdUrl()), lbryId, chapters); + audioStreams, videoStreams, relatedStreams, subtitles, livestream, rewriteVideoURL(info.getHlsUrl()), + rewriteVideoURL(info.getDashMpdUrl()), lbryId, chapters); return Constants.mapper.writeValueAsBytes(streams); diff --git a/src/main/java/me/kavin/piped/utils/URLUtils.java b/src/main/java/me/kavin/piped/utils/URLUtils.java index c700d99..2ec0430 100644 --- a/src/main/java/me/kavin/piped/utils/URLUtils.java +++ b/src/main/java/me/kavin/piped/utils/URLUtils.java @@ -34,6 +34,14 @@ public class URLUtils { } public static String rewriteURL(final String old) { + return rewriteURL(old, Constants.IMAGE_PROXY_PART); + } + + public static String rewriteVideoURL(final String old) { + return rewriteURL(old, Constants.PROXY_PART); + } + + public static String rewriteURL(final String old, final String proxy) { if (StringUtils.isEmpty(old)) return null; @@ -57,7 +65,7 @@ public class URLUtils { path = StringUtils.substringBefore(path, "=") + "=" + StringUtils.substringAfter(path, "=").replace("-rj", "-rw"); } - return Constants.PROXY_PART + path + (hasQuery ? "?" + query + "&host=" : "?host=") + silentEncode(host); + return proxy + path + (hasQuery ? "?" + query + "&host=" : "?host=") + silentEncode(host); } }