Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store source URL MIME type #45

Merged
merged 1 commit into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Store source URL MIME type
  • Loading branch information
Betree committed Nov 10, 2018
commit 4330eb51e5fee351beff8abdab822779cc28d35a
19 changes: 16 additions & 3 deletions apps/cf/lib/sources/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ defmodule CF.Sources.Fetcher do
try do
fetch(url, callback)
rescue
e -> Logger.error("Fetch metadata for #{url} crashed - #{inspect(e)}")
e ->
Logger.error("Fetch metadata for #{url} crashed - #{inspect(e)}")
after
Fetcher.LinkChecker.free_url(url)
end
Expand All @@ -51,14 +52,20 @@ defmodule CF.Sources.Fetcher do

def get_queue, do: Fetcher.LinkChecker.get_queue()

@url_regex ~r/^https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/

defp fetch(url, callback) do
case do_fetch_source_metadata(url) do
without_domain = Regex.replace(@url_regex, url, "\\1")

case do_fetch_source_metadata(url, MIME.from_path(without_domain)) do
{:error, _} -> :error
{:ok, result} -> callback.(result)
end
end

defp do_fetch_source_metadata(url) do
@fetchable_mime_types ~w(text/html application/octet-stream application/xhtml+xml application/vnd.ms-htmlhelp)

defp do_fetch_source_metadata(url, mime_types) when mime_types in @fetchable_mime_types do
case HTTPoison.get(
url,
[],
Expand All @@ -80,6 +87,12 @@ defmodule CF.Sources.Fetcher do
end
end

defp do_fetch_source_metadata(url, mime_type) do
ext = Path.extname(url)
title = Path.basename(url, ext)
{:ok, %{file_mime_type: mime_type, title: title}}
end

defp source_params_from_tree(tree) do
head = Floki.find(tree, "head")

Expand Down
10 changes: 10 additions & 0 deletions apps/cf/test/cf/sources/fetcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ defmodule CF.Sources.FetcherTest do
wait_fetcher()
end

test "stores file name and mime type" do
# Start web server providing a page with giver metadata
Fetcher.fetch_source_metadata("https://CaptainFact.io/great-file.pdf", fn response ->
assert response.title == "great-file"
assert response.file_mime_type == "application/pdf"
end)

wait_fetcher()
end

test "return error if status is not 2xx" do
# Error 404
@valid_attributes.url
Expand Down
12 changes: 11 additions & 1 deletion apps/db/lib/db_schema/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule DB.Schema.Source do
field(:title, :string)
field(:language, :string)
field(:site_name, :string)
field(:file_mime_type, :string)

timestamps()
end
Expand Down Expand Up @@ -39,7 +40,7 @@ defmodule DB.Schema.Source do

def changeset_fetched(struct, params) do
struct
|> cast(params, [:og_url, :url, :title, :language, :site_name])
|> cast(params, [:og_url, :url, :title, :language, :site_name, :file_mime_type])
|> update_change(:url, &prepare_url/1)
|> update_change(:og_url, &prepare_url/1)
|> update_change(:title, &clean_and_truncate/1)
Expand All @@ -60,6 +61,15 @@ defmodule DB.Schema.Source do
|> unique_constraint(:url)
|> validate_format(:url, @url_regex)
|> validate_length(:url, min: 10, max: @url_max_length)
|> validate_change(:file_mime_type, &validate_file_mime_type/2)
end

defp validate_file_mime_type(:file_mime_type, mime_type) do
if MIME.valid?(mime_type) do
[]
else
[file_mime_type: "Invalid MIME type"]
end
end

defp clean_and_truncate(str) do
Expand Down
1 change: 1 addition & 0 deletions apps/db/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule DB.Mixfile do
{:burnex, "~> 1.0"},
{:hashids, "~> 2.0"},
{:kaur, "~> 1.1"},
{:mime, "~> 1.2"},
{:scrivener_ecto, "~> 1.0"},

# Dev only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule DB.Repo.Migrations.AddFileTypeToSource do
use Ecto.Migration

def change do
alter table(:sources) do
add(:file_mime_type, :string, null: true)
end
end
end
6 changes: 6 additions & 0 deletions apps/db/test/db_schema/source_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule DB.Schema.SourceTest do

@valid_attrs %{
title: "some content",
file_mime_type: "application/pdf",
url:
"http://www.lemonde.fr/idees/article/2017/04/24/les-risques-d-une-explosion_5116380_3232.html"
}
Expand All @@ -23,6 +24,11 @@ defmodule DB.Schema.SourceTest do
refute Source.changeset(%Source{}, %{@valid_attrs | url: "https://xxxxxx"}).valid?
end

test "changeset_fetched" do
assert Source.changeset_fetched(%Source{}, @valid_attrs).valid?
refute Source.changeset_fetched(%Source{}, %{@valid_attrs | file_mime_type: "zzzz"}).valid?
end

test "must add https:// if url doesn't start with http:// or https://" do
changeset = Source.changeset(%Source{}, Map.put(@valid_attrs, :url, "amazing.com/article"))
assert changeset.changes.url == "https://amazing.com/article"
Expand Down