From 500612e5a39ded4bd4ad6fe43500055668b57f4d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 26 Oct 2020 10:53:56 +0100 Subject: [PATCH] Improve local uploader Check that destination file is not here and temporary file is still here Signed-off-by: Thomas Citharel --- lib/web/upload/uploader/local.ex | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/web/upload/uploader/local.ex b/lib/web/upload/uploader/local.ex index d153ee3d6..97ff58fa6 100644 --- a/lib/web/upload/uploader/local.ex +++ b/lib/web/upload/uploader/local.ex @@ -11,6 +11,7 @@ defmodule Mobilizon.Web.Upload.Uploader.Local do @behaviour Mobilizon.Web.Upload.Uploader alias Mobilizon.Config + alias Mobilizon.Web.Upload @impl true def get_file(_) do @@ -18,15 +19,21 @@ defmodule Mobilizon.Web.Upload.Uploader.Local do end @impl true - def put_file(upload) do - {path, file} = local_path(upload.path) + def put_file(%Upload{path: path, tempfile: tempfile}) do + {path, file} = local_path(path) result_file = Path.join(path, file) - unless File.exists?(result_file) do - File.cp!(upload.tempfile, result_file) - 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) + else + {:result_exists, _} -> + # If the resulting file already exists, it's because of the Dedupe filter + :ok - :ok + {:temp_file_exists, _} -> + {:error, "Temporary file no longer exists"} + end end @impl true