Seeking advice on handle file upload and database operations

I’m working on a screen which does the following

  • a single user has multiple work items stored in a separate table
  • depending on the condition, will render either a single text input, a text input and a file input, or both.
  • submits the data to the server and upload the files to Google Cloud Storage

Example payload sent by the frontend:

%{
action_item_id[]: [ %Plug.Upload{}]
action_item_id-input: "test input"
}

The map key corresponds to the row id of the action item.

Here’s what I’ve done so far.

  • Loop through params entries using Enum.map
  • Create a 3 functions that matches a string map value,
    a single Plug.Upload struct or a list of Plug.Upload struct
  • I’m thinking of performing DB ops and file upload inside the Enum.map

Seeking advice for the following:

  • File validation (checking file extension is okay)
  • Check if action_item_id is a valid id or not
  • Should I put uploading and database operations near each other

I can already upload a file and the db operation is for updating fields of that particular action item.

If information above are not enough I will gladly put more informstion to it.

Hi!

You should check Arc Ecto GitHub - stavro/arc_ecto: An integration with Arc and Ecto..

Regards, Karlo.

2 Likes

You can also check Waffle, it’s a fork from Arc…

Waffle and Waffle Ecto.

2 Likes

How do you deal with list of Plug Uploads? Based on the docs, it seems to only store one file at a time.

The same way you do lists in SQL in general: More rows. Each saved file should get a single corresponding row that tracks metadata about that file.

3 Likes