From 58af0787be605d0b382b30aba3749c7c8bbf10bc Mon Sep 17 00:00:00 2001 From: Hakaba Hitoyo Date: Thu, 15 Nov 2018 14:19:10 +0900 Subject: add mdii uploader --- lib/pleroma/uploaders/mdii.ex | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/pleroma/uploaders/mdii.ex (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex new file mode 100644 index 000000000..3cf71df43 --- /dev/null +++ b/lib/pleroma/uploaders/mdii.ex @@ -0,0 +1,19 @@ +defmodule Pleroma.Uploaders.Mdii do + @behaviour Pleroma.Uploaders.Uploader + + def put_file(name, uuid, path, content_type, _should_dedupe) do + settings = Application.get_env(:pleroma, Pleroma.Uploaders.Mdii) + host_name = Keyword.fetch!(settings, :host_name) + + {:ok, file_data} = File.read(path) + + File.rm!(path) + + remote_file_name = "00000" + extension = "png" + + public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" + + {:ok, public_url} + end +end -- cgit v1.2.3 From 698cb3587cf66f4946c2baab69ecd45f339e1392 Mon Sep 17 00:00:00 2001 From: Hakaba Hitoyo Date: Thu, 15 Nov 2018 14:38:45 +0900 Subject: omplement mdii uploader --- lib/pleroma/uploaders/mdii.ex | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index 3cf71df43..f21a7e1e2 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -1,6 +1,8 @@ defmodule Pleroma.Uploaders.Mdii do @behaviour Pleroma.Uploaders.Uploader + @httpoison Application.get_env(:pleroma, :httpoison) + def put_file(name, uuid, path, content_type, _should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Uploaders.Mdii) host_name = Keyword.fetch!(settings, :host_name) @@ -8,12 +10,15 @@ defmodule Pleroma.Uploaders.Mdii do {:ok, file_data} = File.read(path) File.rm!(path) + + extension = Regex.replace(~r/^image\//, content_type, "") + query = "https://#{host_name}/mdii.cgi?#{extension}" - remote_file_name = "00000" - extension = "png" - - public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" - - {:ok, public_url} + with {:ok, %{status_code: 200, body: body}} <- + @httpoison.get(url, file_data) do + remote_file_name = body + public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" + {:ok, public_url} + end end end -- cgit v1.2.3 From ebe658c16976d7a1938a3a132027e5b438937f39 Mon Sep 17 00:00:00 2001 From: Hakaba Hitoyo Date: Thu, 15 Nov 2018 14:46:43 +0900 Subject: debuf --- lib/pleroma/uploaders/mdii.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index f21a7e1e2..0f68b0805 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Uploaders.Mdii do query = "https://#{host_name}/mdii.cgi?#{extension}" with {:ok, %{status_code: 200, body: body}} <- - @httpoison.get(url, file_data) do + @httpoison.post(query, file_data) do remote_file_name = body public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" {:ok, public_url} -- cgit v1.2.3 From 8e707aba29921666a50878c39751cd53ee5cde7e Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Thu, 15 Nov 2018 15:11:59 +0900 Subject: format --- lib/pleroma/uploaders/mdii.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index 0f68b0805..c5deaf73f 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -10,12 +10,11 @@ defmodule Pleroma.Uploaders.Mdii do {:ok, file_data} = File.read(path) File.rm!(path) - + extension = Regex.replace(~r/^image\//, content_type, "") query = "https://#{host_name}/mdii.cgi?#{extension}" - with {:ok, %{status_code: 200, body: body}} <- - @httpoison.post(query, file_data) do + with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do remote_file_name = body public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" {:ok, public_url} -- cgit v1.2.3 From 4fbfacf5e129ad3f60e67f5ade2489f4bae48f56 Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Thu, 15 Nov 2018 16:08:55 +0900 Subject: debug --- lib/pleroma/uploaders/mdii.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index c5deaf73f..f1f996fca 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -12,10 +12,10 @@ defmodule Pleroma.Uploaders.Mdii do File.rm!(path) extension = Regex.replace(~r/^image\//, content_type, "") - query = "https://#{host_name}/mdii.cgi?#{extension}" + query = "https://#{host_name}/mdii-post.cgi?#{extension}" with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do - remote_file_name = body + remote_file_name = List.first(String.split(body)) public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" {:ok, public_url} end -- cgit v1.2.3 From 52224de39fe757077448853a7e1546c0f01509af Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Fri, 16 Nov 2018 20:22:36 +0900 Subject: better extension detection --- lib/pleroma/uploaders/mdii.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index f1f996fca..71f3d1be7 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -11,11 +11,11 @@ defmodule Pleroma.Uploaders.Mdii do File.rm!(path) - extension = Regex.replace(~r/^image\//, content_type, "") + extension = String.split(name, ".") |> List.last() query = "https://#{host_name}/mdii-post.cgi?#{extension}" with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do - remote_file_name = List.first(String.split(body)) + remote_file_name = String.split(body) |> List.first() public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" {:ok, public_url} end -- cgit v1.2.3 From 55abd8482ed604587ffa9f5f3f64d36d4d5aa296 Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Fri, 16 Nov 2018 20:41:12 +0900 Subject: better config --- lib/pleroma/uploaders/mdii.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index 71f3d1be7..c62bab077 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -5,18 +5,19 @@ defmodule Pleroma.Uploaders.Mdii do def put_file(name, uuid, path, content_type, _should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Uploaders.Mdii) - host_name = Keyword.fetch!(settings, :host_name) + cgi = Keyword.fetch!(settings, :cgi) + files = Keyword.fetch!(settings, :files) {:ok, file_data} = File.read(path) File.rm!(path) extension = String.split(name, ".") |> List.last() - query = "https://#{host_name}/mdii-post.cgi?#{extension}" + query = "#{cgi}?#{extension}" with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do remote_file_name = String.split(body) |> List.first() - public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" + public_url = "#{files}/#{remote_file_name}.#{extension}" {:ok, public_url} end end -- cgit v1.2.3 From 8fd0556c78472f8bdeee3520d6192ce96339d545 Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Sat, 17 Nov 2018 18:14:42 +0900 Subject: better config reading --- lib/pleroma/uploaders/mdii.ex | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index c62bab077..b0c7e19e7 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -1,12 +1,13 @@ -defmodule Pleroma.Uploaders.Mdii do +defmodule Pleroma.Uploaders.MDII do + alias Pleroma.Config + @behaviour Pleroma.Uploaders.Uploader @httpoison Application.get_env(:pleroma, :httpoison) def put_file(name, uuid, path, content_type, _should_dedupe) do - settings = Application.get_env(:pleroma, Pleroma.Uploaders.Mdii) - cgi = Keyword.fetch!(settings, :cgi) - files = Keyword.fetch!(settings, :files) + cgi = Pleroma.Config.get([Pleroma.Uploaders.MDII, :cgi]) + files = Pleroma.Config.get([Pleroma.Uploaders.MDII, :files]) {:ok, file_data} = File.read(path) -- cgit v1.2.3 From 59e079f641c2aed70a698fdcf4908a9f31a959f2 Mon Sep 17 00:00:00 2001 From: hakabahitoyo Date: Sat, 17 Nov 2018 20:16:25 +0900 Subject: fallbacking into local uploader --- lib/pleroma/uploaders/mdii.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index b0c7e19e7..a9d52b0dc 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -5,21 +5,22 @@ defmodule Pleroma.Uploaders.MDII do @httpoison Application.get_env(:pleroma, :httpoison) - def put_file(name, uuid, path, content_type, _should_dedupe) do + def put_file(name, uuid, path, content_type, should_dedupe) do cgi = Pleroma.Config.get([Pleroma.Uploaders.MDII, :cgi]) files = Pleroma.Config.get([Pleroma.Uploaders.MDII, :files]) {:ok, file_data} = File.read(path) - File.rm!(path) - extension = String.split(name, ".") |> List.last() query = "#{cgi}?#{extension}" with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do + File.rm!(path) remote_file_name = String.split(body) |> List.first() public_url = "#{files}/#{remote_file_name}.#{extension}" {:ok, public_url} + else + _ -> Pleroma.Uploaders.Local.put_file(name, uuid, path, content_type, should_dedupe) end end end -- cgit v1.2.3