Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85832691
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/config/config.exs b/config/config.exs
index 8eea4dd0a..bf050197f 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -1,27 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.
use Mix.Config
# General application configuration
config :pleroma,
ecto_repos: [Pleroma.Repo]
+config :pleroma, Pleroma.Upload,
+ uploads: "uploads"
+
# Configures the endpoint
config :pleroma, Pleroma.Web.Endpoint,
url: [host: "localhost"],
secret_key_base: "aK4Abxf29xU9TTDKre9coZPUgevcVCFQJe/5xP/7Lt4BEif6idBIbjupVbOrbKxl",
render_errors: [view: Pleroma.Web.ErrorView, accepts: ~w(json)],
pubsub: [name: Pleroma.PubSub,
adapter: Phoenix.PubSub.PG2]
# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
diff --git a/config/test.exs b/config/test.exs
index 07f49d3e9..f5d6f240d 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -1,23 +1,26 @@
use Mix.Config
# We don't run a server during test. If one is required,
# you can enable the server option below.
config :pleroma, Pleroma.Web.Endpoint,
http: [port: 4001],
server: false
# Print only warnings and errors during test
config :logger, level: :warn
+config :pleroma, Pleroma.Upload,
+ uploads: "test/uploads"
+
# Configure your database
config :pleroma, Pleroma.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "pleroma_test",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
# Reduce hash rounds for testing
config :comeonin, :pbkdf2_rounds, 1
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex
new file mode 100644
index 000000000..43ebe98e3
--- /dev/null
+++ b/lib/pleroma/upload.ex
@@ -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
diff --git a/test/fixtures/image.jpg b/test/fixtures/image.jpg
new file mode 100644
index 000000000..09834bb5c
Binary files /dev/null and b/test/fixtures/image.jpg differ
diff --git a/test/upload_test.exs b/test/upload_test.exs
new file mode 100644
index 000000000..e8c954341
--- /dev/null
+++ b/test/upload_test.exs
@@ -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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 2, 8:59 AM (8 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1804022
Default Alt Text
(3 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline