Giter Club home page Giter Club logo

Comments (1)

kiere avatar kiere commented on August 29, 2024

@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.

from arc_ecto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.