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

cast_filename doesn't work in local storage. #48

Open
loongmxbt opened this issue Sep 4, 2016 · 2 comments
Open

cast_filename doesn't work in local storage. #48

loongmxbt opened this issue Sep 4, 2016 · 2 comments
Labels
Milestone

Comments

@loongmxbt
Copy link

loongmxbt commented Sep 4, 2016

My model paper looks like this.

schema "papers" do
    field :title, :string
    field :file, Exfile.Ecto.File
    field :content_type, :string
    field :filename, :string
    belongs_to :topic, Confer.Topic
    belongs_to :user, Confer.User
    has_many :reviews, Confer.Review

    timestamps()
  end

and changeset looks like this

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:title, :file, :topic_id, :user_id, :content_type, :filename])
    |> validate_required([:title, :topic_id, :file, :user_id])
    |> validate_content_type(:file, ~w(application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document application/zip))
    |> cast_content_type(:file, :content_type)
    |> cast_filename(:file, :filename) # BUG: filename not inserted
    |> Exfile.Ecto.prepare_uploads([:file])
  end

When I upload a file, there is filename field at first. filename: "TensorFlow whitepaper2015.pdf".
But when goes to INSERT INTO, there is not a filename. Can't figure out why.

[info] POST /papers
[debug] Processing by Confer.PaperController.create/2
  Parameters: %{"_csrf_token" => "X1oPfAV4ATE0DA9aCz4CB2Qffy53EAAA7md+v3FVAcYinpfA+y9T9A==", "_utf8" => "✓", "paper" => %{"file" => %Plug.Upload{content_type: "application/pdf", filename: "TensorFlow whitepaper2015.pdf", path: "/var/folders/wl/8_mrlnx52ls04sfl1my7g7l80000gn/T//plug-1472/multipart-956497-510178-2"}, "title" => "ppp", "topic_id" => "1"}}
  Pipelines: [:protected]
[debug] QUERY OK db=0.3ms
SELECT t0."name", t0."id" FROM "topics" AS t0 ORDER BY t0."id" []
[debug] QUERY OK db=0.1ms
begin []
[debug] QUERY OK db=1.0ms
INSERT INTO "papers" ("content_type","file","title","topic_id","user_id","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "id" ["application/pdf", "exfile://store/b9fea1752b7288c4aa71cb7bc0b58c9db6fb52f690d98d5c19d8c479e11f", "ppp", 1, 1, {{2016, 9, 4}, {2, 34, 57, 0}}, {{2016, 9, 4}, {2, 34, 57, 0}}]
[debug] QUERY OK db=0.3ms

Really wish your help! Thanks!

@keichan34 keichan34 added the bug label Sep 4, 2016
@keichan34 keichan34 added this to the v0.4 milestone Sep 4, 2016
@loongmxbt
Copy link
Author

loongmxbt commented Sep 25, 2016

Seems something with non-ascii/utf-8 characters filename, need to encode? filename in pure english and numbers are ok!

My mistake, not mentioning the filename is not pure english.

@cblock
Copy link
Contributor

cblock commented Nov 4, 2017

The problem ist that cast_content_type is not idempotent in that it not only adds content_type to changeset but also replaces the Plug.Upload file representation with Exfile.File representation with the latter not possessing the filename information.

Quick fix is to add cast_filename before cast_content_type to your changeset function. The aforementioned pull request makes cast_content_type idempotent.

scarfacedeb added a commit to scarfacedeb/exfile that referenced this issue Jun 7, 2019
* upstream/master:
  Fix POSIX error typo
  Better error message for failed uploads
  Rename perform_uploads/2 back to perform_uploads!/2
  Gracefully handle optional exfile fields not being set
  Do not add processed file back to changeset in cast_content_type. Fixes  keichan34#43 and keichan34#48
  Replace deprecated String.strip/1 with String.trim/1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants