Skip to content

Commit

Permalink
Proxy profile pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
omarroth committed Sep 17, 2018
1 parent bd5ec2f commit 35bee98
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/invidious.cr
Expand Up @@ -3024,6 +3024,55 @@ get "/videoplayback" do |env|
end
end

get "/ggpht*" do |env|
end

get "/ggpht/*" do |env|
host = "https://yt3.ggpht.com"
client = make_client(URI.parse(host))
url = env.request.path.lchop("/ggpht")

headers = env.request.headers
headers.delete("Host")
headers.delete("Cookie")
headers.delete("User-Agent")
headers.delete("Referer")

client.get(url, headers) do |response|
env.response.status_code = response.status_code
response.headers.each do |key, value|
env.response.headers[key] = value
end

if response.status_code == 304
break
end

chunk_size = 4096
size = 1
if response.headers.includes_word?("Content-Encoding", "gzip")
Gzip::Writer.open(env.response) do |deflate|
until size == 0
size = IO.copy(response.body_io, deflate)
env.response.flush
end
end
elsif response.headers.includes_word?("Content-Encoding", "deflate")
Flate::Writer.open(env.response) do |deflate|
until size == 0
size = IO.copy(response.body_io, deflate)
env.response.flush
end
end
else
until size == 0
size = IO.copy(response.body_io, env.response, chunk_size)
env.response.flush
end
end
end
end

get "/vi/:id/:name" do |env|
id = env.params.url["id"]
name = env.params.url["name"]
Expand Down
4 changes: 3 additions & 1 deletion src/invidious/comments.cr
Expand Up @@ -100,10 +100,12 @@ def template_youtube_comments(comments)
END_HTML
end

author_thumbnail = "/ggpht#{URI.parse(child["authorThumbnails"][-1]["url"].as_s).full_path}"

html += <<-END_HTML
<div class="pure-g">
<div class="pure-u-2-24">
<img style="width:90%; padding-right:1em; padding-top:1em;" src="#{child["authorThumbnails"][-1]["url"]}">
<img style="width:90%; padding-right:1em; padding-top:1em;" src="#{author_thumbnail}">
</div>
<div class="pure-u-22-24">
<p>
Expand Down
2 changes: 1 addition & 1 deletion src/invidious/helpers/helpers.cr
Expand Up @@ -18,7 +18,7 @@ class Config
end

class FilteredCompressHandler < Kemal::Handler
exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/api/*"]
exclude ["/videoplayback", "/videoplayback/*", "/vi/*", "/api/*", "/ggpht/*"]

def call(env)
return call_next env if exclude_match? env
Expand Down

0 comments on commit 35bee98

Please sign in to comment.