diff options
author | href <href@random.sh> | 2018-11-29 21:11:45 +0100 |
---|---|---|
committer | href <href@random.sh> | 2018-11-30 18:02:37 +0100 |
commit | 02d3dc6869f388388ea744ea4ee3b54279d55e86 (patch) | |
tree | 45febb27f539a0516c9ab29c5801055352ebb4f5 /lib/pleroma/uploaders | |
parent | 97b00d366f5d0bdf80efa2c425ccc8fb16681256 (diff) | |
download | pleroma-02d3dc6869f388388ea744ea4ee3b54279d55e86.tar.gz |
Uploads fun, part. 2
Diffstat (limited to 'lib/pleroma/uploaders')
-rw-r--r-- | lib/pleroma/uploaders/local.ex | 45 | ||||
-rw-r--r-- | lib/pleroma/uploaders/mdii.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/uploaders/s3.ex | 10 | ||||
-rw-r--r-- | lib/pleroma/uploaders/swift/uploader.ex | 11 | ||||
-rw-r--r-- | lib/pleroma/uploaders/uploader.ex | 25 |
5 files changed, 46 insertions, 54 deletions
diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index 7ca1ba07d..434a6b515 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -7,39 +7,28 @@ defmodule Pleroma.Uploaders.Local do {:ok, {:static_dir, upload_path()}} end - def put_file(name, uuid, tmpfile, _content_type, opts) do - upload_folder = get_upload_path(uuid, opts.dedupe) - - File.mkdir_p!(upload_folder) - - result_file = Path.join(upload_folder, name) - - if File.exists?(result_file) do - File.rm!(tmpfile) - else - File.cp!(tmpfile, result_file) + def put_file(upload) do + {local_path, file} = + case Enum.reverse(String.split(upload.path, "/", trim: true)) do + [file] -> + {upload_path(), file} + + [file | folders] -> + path = Path.join([upload_path()] ++ Enum.reverse(folders)) + File.mkdir_p!(path) + {path, file} + end + + result_file = Path.join(local_path, file) + + unless File.exists?(result_file) do + File.cp!(upload.tempfile, result_file) end - {:ok, {:file, get_url(name, uuid, opts.dedupe)}} + :ok end def upload_path do Pleroma.Config.get!([__MODULE__, :uploads]) end - - defp get_upload_path(uuid, should_dedupe) do - if should_dedupe do - upload_path() - else - Path.join(upload_path(), uuid) - end - end - - defp get_url(name, uuid, should_dedupe) do - if should_dedupe do - :cow_uri.urlencode(name) - else - Path.join(uuid, :cow_uri.urlencode(name)) - end - end end diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index 1d93c8154..35d36d3e4 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -11,22 +11,21 @@ defmodule Pleroma.Uploaders.MDII do Pleroma.Uploaders.Local.get_file(file) end - def put_file(name, uuid, path, content_type, opts) do + def put_file(upload) do cgi = Pleroma.Config.get([Pleroma.Uploaders.MDII, :cgi]) files = Pleroma.Config.get([Pleroma.Uploaders.MDII, :files]) - {:ok, file_data} = File.read(path) + {:ok, file_data} = File.read(upload.tempfile) - extension = String.split(name, ".") |> List.last() + extension = String.split(upload.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, {:url, public_url}} else - _ -> Pleroma.Uploaders.Local.put_file(name, uuid, path, content_type, opts) + _ -> Pleroma.Uploaders.Local.put_file(upload) end end end diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index 2d1ddef75..19832a7ec 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -15,20 +15,18 @@ defmodule Pleroma.Uploaders.S3 do ])}} end - def put_file(name, uuid, path, content_type, _opts) do + def put_file(upload = %Pleroma.Upload{}) do config = Pleroma.Config.get([__MODULE__]) bucket = Keyword.get(config, :bucket) - {:ok, file_data} = File.read(path) + {:ok, file_data} = File.read(upload.tempfile) - File.rm!(path) - - s3_name = "#{uuid}/#{strict_encode(name)}" + s3_name = strict_encode(upload.path) op = ExAws.S3.put_object(bucket, s3_name, file_data, [ {:acl, :public_read}, - {:content_type, content_type} + {:content_type, upload.content_type} ]) case ExAws.request(op) do diff --git a/lib/pleroma/uploaders/swift/uploader.ex b/lib/pleroma/uploaders/swift/uploader.ex index 5db35fe50..b35b9807b 100644 --- a/lib/pleroma/uploaders/swift/uploader.ex +++ b/lib/pleroma/uploaders/swift/uploader.ex @@ -5,10 +5,11 @@ defmodule Pleroma.Uploaders.Swift do {:ok, {:url, Path.join([Pleroma.Config.get!([__MODULE__, :object_url]), name])}} end - def put_file(name, uuid, tmp_path, content_type, _opts) do - {:ok, file_data} = File.read(tmp_path) - remote_name = "#{uuid}/#{name}" - - Pleroma.Uploaders.Swift.Client.upload_file(remote_name, file_data, content_type) + def put_file(upload) do + Pleroma.Uploaders.Swift.Client.upload_file( + upload.path, + File.read!(upload.tmpfile), + upload.content_type + ) end end diff --git a/lib/pleroma/uploaders/uploader.ex b/lib/pleroma/uploaders/uploader.ex index 8ef82b4ef..afda5609e 100644 --- a/lib/pleroma/uploaders/uploader.ex +++ b/lib/pleroma/uploaders/uploader.ex @@ -16,20 +16,25 @@ defmodule Pleroma.Uploaders.Uploader do Returns: + * `:ok` which assumes `{:ok, upload.path}` * `{:ok, spec}` where spec is: * `{:file, filename :: String.t}` to handle reads with `get_file/1` (recommended) - This allows to correctly proxy or redirect requests to the backend, while allowing to migrate backends without breaking any URL. - - * `{url, url :: String.t}` to bypass `get_file/2` and use the `url` directly in the activity. + This allows to correctly proxy or redirect requests to the backend, while allowing to migrate backends without breaking any URL. + * `{url, url :: String.t}` to bypass `get_file/2` and use the `url` directly in the activity. * `{:error, String.t}` error information if the file failed to be saved to the backend. + """ - @callback put_file( - name :: String.t(), - uuid :: String.t(), - file :: File.t(), - content_type :: String.t(), - options :: Map.t() - ) :: {:ok, {:file, String.t()} | {:url, String.t()}} | {:error, String.t()} + @callback put_file(Pleroma.Upload.t()) :: + :ok | {:ok, {:file | :url, String.t()}} | {:error, String.t()} + + @spec put_file(module(), Pleroma.Upload.t()) :: + {:ok, {:file | :url, String.t()}} | {:error, String.t()} + def put_file(uploader, upload) do + case uploader.put_file(upload) do + :ok -> {:ok, {:file, upload.path}} + other -> other + end + end end |