Skip to content
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

Store original file size and filename to Model #80

Open
prihandi opened this issue Jul 28, 2017 · 1 comment
Open

Store original file size and filename to Model #80

prihandi opened this issue Jul 28, 2017 · 1 comment

Comments

@prihandi
Copy link

Is there any way to store original filename and file size on Repo Model ?
I want to calculate total uploaded file size per user, if it provided directly from database it would be very helpful.
Thanks
Dwi

@kiere
Copy link

kiere commented Jul 12, 2019

@prihandi I am doing this with the File.stat function. In my changeset I have something like this (kind of hacky for now):

(my %Plug.Upload{} is in the key "attachment")

  def create_changeset(relationship_attachment, attrs) do
    upload = Map.get(attrs, "attachment")

    relationship_attachment
    |> changeset(attrs)
    |> put_file_size(upload) # or we could just send this from the JavaScript File API?
    |> validate_required([:file_size])
    |> put_mime_type(upload)
    |> validate_required([:mime_type])
    |> cast_attachments(attrs, [:attachment])
    |> validate_required([:attachment])
  end

  defp put_mime_type(changeset, %Plug.Upload{} = upload) do
    put_change(changeset, :mime_type, upload.content_type)
  end
  defp put_mime_type(changeset, _), do: changeset

  defp put_file_size(changeset, %Plug.Upload{} = upload) do
    put_change(changeset, :file_size, file_size(upload))
  end
  defp put_file_size(changeset, _), do: changeset

  defp file_size(%Plug.Upload{} = upload) do
    case File.stat(upload.path) do
      {:ok, stats} ->
        stats.size
      {:error, _} ->
        nil
    end
  end

This is the first project I'm actually doing this on, so all this is still a work in progress and I'm sure this will change quickly. But it works for now.

I moved off the use of the JavaScript File API because I realized I would want this consistently done on the server and not relying on potentially varying implementations in different browsers AND if we implemented an API for a native app or something else, I would be relying on one more out-of-app variation of file size calculation. This way it's done the same regardless of the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants