keichan34 / exfile

File upload persistence and processing for Phoenix / Plug
MIT License
90 stars 19 forks source link

How to upload a file through seeds.exs? #33

Closed loongmxbt closed 8 years ago

loongmxbt commented 8 years ago

Here I want to add a record Paper which contains a Exfile.Ecto.File, is there a quick way to upload a file via seeds.exs? Or what is the valid fileUrl? Thanks!

Repo.delete_all Paper
Paper.changeset(%Paper{}, %{title: "paper title", topic_id: 1, user_id: 1, file: "fileurl"})
|> Repo.insert
#Ecto.Changeset<action: nil,
 changes: %{title: "paper title", topic_id: 1, user_id: 1},
 errors: [file: {"is invalid", [type: Exfile.Ecto.File]}], data: #Paper.Paper<>,
 valid?: false>
keichan34 commented 8 years ago

Try this:

backend = Exfile.Config.get_backend("store")
{:ok, file} = Exfile.Backend.upload(backend, %Exfile.LocalFile{path: "path/to/file/on/local/computer"})

Repo.delete_all Paper
Paper.changeset(%Paper{}, %{title: "paper title", topic_id: 1, user_id: 1, file: file})
|> Repo.insert

There's a convenience function in Exfile.BackendTest to upload a string in to a file:

backend = Exfile.Config.get_backend("store")
{:ok, file} = Exfile.BackendTest.upload_string(backend, "string with contents of file")
loongmxbt commented 8 years ago

Thanks a lot! Exfile.BackendTest helps a lot!

keichan34 commented 8 years ago

Sorry for the confusion, and thanks for the reminder. I'll be adding a testing section to the readme in #34 😄