X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuploaders%2Flocal.ex;h=2e6fe32921ebe8eb25c6109033f9392c270d23f1;hb=4e069ce53776e99f243adb61969546bed5f2c3fd;hp=d4624661f9ec638468ffefb573700601c382ad5d;hpb=af01f0196a43454728f6e0ca8b9b8be208743251;p=akkoma diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index d4624661f..2e6fe3292 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -1,47 +1,49 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.Uploaders.Local do @behaviour Pleroma.Uploaders.Uploader - alias Pleroma.Web + @impl true + def get_file(_) do + {:ok, {:static_dir, upload_path()}} + end - def put_file(name, uuid, tmpfile, _content_type, should_dedupe) do - upload_folder = get_upload_path(uuid, should_dedupe) - url_path = get_url(name, uuid, should_dedupe) + @impl true + def put_file(upload) do + {local_path, file} = + case Enum.reverse(Path.split(upload.path)) do + [file] -> + {upload_path(), file} - File.mkdir_p!(upload_folder) + [file | folders] -> + path = Path.join([upload_path()] ++ Enum.reverse(folders)) + File.mkdir_p!(path) + {path, file} + end - result_file = Path.join(upload_folder, name) + result_file = Path.join(local_path, file) - if File.exists?(result_file) do - File.rm!(tmpfile) - else - File.cp!(tmpfile, result_file) + if not File.exists?(result_file) do + File.cp!(upload.tempfile, result_file) end - {:ok, url_path} + :ok end def upload_path do - settings = Application.get_env(:pleroma, Pleroma.Uploaders.Local) - Keyword.fetch!(settings, :uploads) + 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) + @impl true + def delete_file(path) do + upload_path() + |> Path.join(path) + |> File.rm() + |> case do + :ok -> :ok + {:error, posix_error} -> {:error, to_string(posix_error)} end end - - defp get_url(name, uuid, should_dedupe) do - if should_dedupe do - url_for(:cow_uri.urlencode(name)) - else - url_for(Path.join(uuid, :cow_uri.urlencode(name))) - end - end - - defp url_for(file) do - "#{Web.base_url()}/media/#{file}" - end end