parent
d2cdcc097b
commit
08fdbd6f3a
@ -0,0 +1,30 @@
|
|||||||
|
defmodule Pleroma.Upload do
|
||||||
|
def store(%Plug.Upload{} = file) do
|
||||||
|
uuid = Ecto.UUID.generate
|
||||||
|
upload_folder = Path.join(upload_path(), uuid)
|
||||||
|
File.mkdir_p!(upload_folder)
|
||||||
|
result_file = Path.join(upload_folder, file.filename)
|
||||||
|
File.cp!(file.path, result_file)
|
||||||
|
|
||||||
|
%{
|
||||||
|
"type" => "Image",
|
||||||
|
"href" => url_for(Path.join(uuid, file.filename)),
|
||||||
|
"name" => file.filename,
|
||||||
|
"uuid" => uuid
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp upload_path do
|
||||||
|
Application.get_env(:pleroma, Pleroma.Upload)
|
||||||
|
|> Keyword.fetch!(:uploads)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp url_for(file) do
|
||||||
|
host =
|
||||||
|
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|
||||||
|
|> Keyword.fetch!(:url)
|
||||||
|
|> Keyword.fetch!(:host)
|
||||||
|
|
||||||
|
"https://#{host}/media/#{file}"
|
||||||
|
end
|
||||||
|
end
|
After Width: | Height: | Size: 285 KiB |
@ -0,0 +1,12 @@
|
|||||||
|
defmodule Pleroma.UploadTest do
|
||||||
|
alias Pleroma.Upload
|
||||||
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
describe "Storing a file" do
|
||||||
|
test "copies the file to the configured folder" do
|
||||||
|
file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"}
|
||||||
|
data = Upload.store(file)
|
||||||
|
assert data["name"] == "an_image.jpg"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue