mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-01 03:46:44 +01:00
propagate filename through scraper (fixes derpibooru/philomena#226)
This commit is contained in:
parent
ad83d625d3
commit
1f57d0041c
2 changed files with 24 additions and 6 deletions
|
@ -2,7 +2,7 @@ defmodule Camo.Image do
|
||||||
def image_url(input) do
|
def image_url(input) do
|
||||||
%{host: host} = URI.parse(input)
|
%{host: host} = URI.parse(input)
|
||||||
|
|
||||||
if !host or String.ends_with?(host, cdn_host()) do
|
if !host or String.ends_with?(host, cdn_host()) or is_nil(camo_key()) do
|
||||||
input
|
input
|
||||||
else
|
else
|
||||||
camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower)
|
camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower)
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
defmodule PhilomenaWeb.ScraperPlug do
|
defmodule PhilomenaWeb.ScraperPlug do
|
||||||
|
@filename_regex ~r/filename="([^"]+)"/
|
||||||
|
|
||||||
def init(opts) do
|
def init(opts) do
|
||||||
opts
|
opts
|
||||||
end
|
end
|
||||||
|
@ -14,25 +16,30 @@ defmodule PhilomenaWeb.ScraperPlug do
|
||||||
%{"scraper_cache" => url} when not is_nil(url) ->
|
%{"scraper_cache" => url} when not is_nil(url) ->
|
||||||
url
|
url
|
||||||
|> Philomena.Http.get()
|
|> Philomena.Http.get()
|
||||||
|> maybe_fixup_params(opts, conn)
|
|> maybe_fixup_params(url, opts, conn)
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
conn
|
conn
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_fixup_params({:ok, %Tesla.Env{body: body, status: 200}}, opts, conn) do
|
defp maybe_fixup_params(
|
||||||
|
{:ok, %Tesla.Env{body: body, status: 200, headers: headers}},
|
||||||
|
url,
|
||||||
|
opts,
|
||||||
|
conn
|
||||||
|
) do
|
||||||
params_name = Keyword.get(opts, :params_name, "image")
|
params_name = Keyword.get(opts, :params_name, "image")
|
||||||
params_key = Keyword.get(opts, :params_key, "image")
|
params_key = Keyword.get(opts, :params_key, "image")
|
||||||
|
name = extract_filename(url, headers)
|
||||||
file = Briefly.create!()
|
file = Briefly.create!()
|
||||||
now = DateTime.utc_now() |> DateTime.to_unix(:microsecond)
|
|
||||||
|
|
||||||
File.write!(file, body)
|
File.write!(file, body)
|
||||||
|
|
||||||
fake_upload = %Plug.Upload{
|
fake_upload = %Plug.Upload{
|
||||||
path: file,
|
path: file,
|
||||||
content_type: "application/octet-stream",
|
content_type: "application/octet-stream",
|
||||||
filename: "scraper-#{now}"
|
filename: name
|
||||||
}
|
}
|
||||||
|
|
||||||
updated_form = Map.put(conn.params[params_name], params_key, fake_upload)
|
updated_form = Map.put(conn.params[params_name], params_key, fake_upload)
|
||||||
|
@ -42,5 +49,16 @@ defmodule PhilomenaWeb.ScraperPlug do
|
||||||
%Plug.Conn{conn | params: updated_params}
|
%Plug.Conn{conn | params: updated_params}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_fixup_params(_response, _opts, conn), do: conn
|
defp maybe_fixup_params(_response, _url, _opts, conn), do: conn
|
||||||
|
|
||||||
|
defp extract_filename(url, resp_headers) do
|
||||||
|
{_, header} =
|
||||||
|
Enum.find(resp_headers, {nil, "filename=\"#{Path.basename(url)}\""}, fn {key, value} ->
|
||||||
|
key == "content-disposition" and Regex.match?(@filename_regex, value)
|
||||||
|
end)
|
||||||
|
|
||||||
|
[name] = Regex.run(@filename_regex, header, capture: :all_but_first)
|
||||||
|
|
||||||
|
String.slice(name, 0, 127)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue