-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up all temp files created during processing and storage #285
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
defmodule Arc.File do | ||
defstruct [:path, :file_name, :binary] | ||
defstruct [:path, :file_name, :binary, :tempfile?] | ||
|
||
def generate_temporary_path(file \\ nil) do | ||
extension = Path.extname((file && file.path) || "") | ||
|
@@ -18,7 +18,7 @@ defmodule Arc.File do | |
filename = Path.basename(uri.path) | ||
|
||
case save_file(uri, filename) do | ||
{:ok, local_path} -> %Arc.File{path: local_path, file_name: filename} | ||
{:ok, local_path} -> %Arc.File{path: local_path, file_name: filename, tempfile?: true} | ||
:error -> {:error, :invalid_file_path} | ||
end | ||
end | ||
|
@@ -33,6 +33,7 @@ end | |
|
||
def new(%{filename: filename, binary: binary}) do | ||
%Arc.File{binary: binary, file_name: Path.basename(filename)} | ||
|> write_binary() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest keeping And sure, someone could easily create just use |
||
end | ||
|
||
# Accepts a map conforming to %Plug.Upload{} syntax | ||
|
@@ -43,16 +44,15 @@ end | |
end | ||
end | ||
|
||
def ensure_path(file = %{path: path}) when is_binary(path), do: file | ||
def ensure_path(file = %{binary: binary}) when is_binary(binary), do: write_binary(file) | ||
|
||
defp write_binary(file) do | ||
path = generate_temporary_path(file) | ||
:ok = File.write!(path, file.binary) | ||
|
||
%__MODULE__{ | ||
file_name: file.file_name, | ||
path: path | ||
path: path, | ||
tempfile?: true | ||
} | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is the same code as in
cleanup/2
. I would suggest replacing this (and the ending return ofresult
) with the function callcleanup(result, file)
to minimize unnecessary code duplication.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you!