Various typespec and compilation improvements
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -19,10 +19,24 @@ defmodule Mobilizon.Web.Upload.Uploader.Local do
|
||||
end
|
||||
|
||||
@impl true
|
||||
@spec put_file(Upload.t()) ::
|
||||
:ok | {:ok, {:file, String.t()}} | {:error, :tempfile_no_longer_exists}
|
||||
def put_file(%Upload{path: path, tempfile: tempfile}) do
|
||||
{path, file} = local_path(path)
|
||||
result_file = Path.join(path, file)
|
||||
|
||||
if File.exists?(result_file) do
|
||||
# If the resulting file already exists, it's because of the Dedupe filter
|
||||
:ok
|
||||
else
|
||||
if File.exists?(tempfile) do
|
||||
File.cp!(tempfile, result_file)
|
||||
{:ok, {:file, result_file}}
|
||||
else
|
||||
{:error, :tempfile_no_longer_exists}
|
||||
end
|
||||
end
|
||||
|
||||
with {:result_exists, false} <- {:result_exists, File.exists?(result_file)},
|
||||
{:temp_file_exists, true} <- {:temp_file_exists, File.exists?(tempfile)} do
|
||||
File.cp!(tempfile, result_file)
|
||||
@@ -37,28 +51,54 @@ defmodule Mobilizon.Web.Upload.Uploader.Local do
|
||||
end
|
||||
|
||||
@impl true
|
||||
@spec remove_file(String.t()) ::
|
||||
{:ok, {:file, String.t()}}
|
||||
| {:error, :folder_not_empty}
|
||||
| {:error, :enofile}
|
||||
| {:error, File.posix()}
|
||||
def remove_file(path) do
|
||||
with {path, file} <- local_path(path),
|
||||
full_path <- Path.join(path, file),
|
||||
true <- File.exists?(full_path),
|
||||
:ok <- File.rm(full_path),
|
||||
:ok <- remove_folder(path) do
|
||||
{:ok, path}
|
||||
{path, file} = local_path(path)
|
||||
full_path = Path.join(path, file)
|
||||
|
||||
if File.exists?(full_path) do
|
||||
do_remove_file(path, full_path)
|
||||
else
|
||||
false -> {:error, "File #{path} doesn't exist"}
|
||||
{:error, :enofile}
|
||||
end
|
||||
end
|
||||
|
||||
@spec do_remove_file(String.t(), String.t()) ::
|
||||
{:ok, {:file, String.t()}}
|
||||
| {:error, :folder_not_empty}
|
||||
| {:error, File.posix()}
|
||||
defp do_remove_file(path, full_path) do
|
||||
case File.rm(full_path) do
|
||||
:ok ->
|
||||
case remove_folder(path) do
|
||||
:ok ->
|
||||
{:ok, {:file, path}}
|
||||
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
|
||||
{:error, err} ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec remove_folder(String.t()) :: :ok | {:error, :folder_not_empty} | {:error, File.posix()}
|
||||
defp remove_folder(path) do
|
||||
with {:subfolder, true} <- {:subfolder, path != upload_path()},
|
||||
{:empty_folder, {:ok, [] = _files}} <- {:empty_folder, File.ls(path)} do
|
||||
File.rmdir(path)
|
||||
else
|
||||
{:subfolder, _} -> :ok
|
||||
{:empty_folder, _} -> {:error, "Error: Folder is not empty"}
|
||||
{:empty_folder, _} -> {:error, :folder_not_empty}
|
||||
end
|
||||
end
|
||||
|
||||
@spec local_path(String.t()) :: {String.t(), String.t()}
|
||||
defp local_path(path) do
|
||||
case Enum.reverse(String.split(path, "/", trim: true)) do
|
||||
[file] ->
|
||||
@@ -71,6 +111,7 @@ defmodule Mobilizon.Web.Upload.Uploader.Local do
|
||||
end
|
||||
end
|
||||
|
||||
@spec upload_path :: String.t()
|
||||
def upload_path do
|
||||
Config.get!([__MODULE__, :uploads])
|
||||
end
|
||||
|
||||
@@ -33,9 +33,9 @@ defmodule Mobilizon.Web.Upload.Uploader do
|
||||
"""
|
||||
@type file_spec :: {:file | :url, String.t()}
|
||||
@callback put_file(Mobilizon.Web.Upload.t()) ::
|
||||
:ok | {:ok, file_spec()} | {:error, String.t()} | :wait_callback
|
||||
:ok | {:ok, file_spec()} | {:error, atom()} | :wait_callback
|
||||
|
||||
@callback remove_file(file_spec()) :: :ok | {:ok, file_spec()} | {:error, String.t()}
|
||||
@callback remove_file(file_spec()) :: :ok | {:ok, file_spec()} | {:error, atom()}
|
||||
|
||||
@callback http_callback(Plug.Conn.t(), map()) ::
|
||||
{:ok, Plug.Conn.t()}
|
||||
@@ -43,7 +43,7 @@ defmodule Mobilizon.Web.Upload.Uploader do
|
||||
| {:error, Plug.Conn.t(), String.t()}
|
||||
@optional_callbacks http_callback: 2
|
||||
|
||||
@spec put_file(module(), Mobilizon.Web.Upload.t()) :: {:ok, file_spec()} | {:error, String.t()}
|
||||
@spec put_file(module(), Mobilizon.Web.Upload.t()) :: {:ok, file_spec()} | {:error, atom()}
|
||||
def put_file(uploader, upload) do
|
||||
case uploader.put_file(upload) do
|
||||
:ok -> {:ok, {:file, upload.path}}
|
||||
@@ -53,6 +53,7 @@ defmodule Mobilizon.Web.Upload.Uploader do
|
||||
end
|
||||
end
|
||||
|
||||
@spec remove_file(module(), String.t()) :: {:ok, String.t()} | {:error, atom()}
|
||||
def remove_file(uploader, path) do
|
||||
uploader.remove_file(path)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user