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

What is the best way to allow for multiple image uploads? #45

Open
Cleop opened this issue Nov 7, 2018 · 2 comments
Open

What is the best way to allow for multiple image uploads? #45

Cleop opened this issue Nov 7, 2018 · 2 comments
Assignees
Labels
help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@Cleop
Copy link
Member

Cleop commented Nov 7, 2018

What is the best way to allow for multiple image uploads?

For an e-commerce site where a product can has one primary photo plus multiple secondary photos, what is the best way to enable uploading multiple images?

  1. If you upload multiple images one at a time just like a single upload app, what naming conventions should you follow in order to store the images linked to a particular product? E.g. product a1b has images a1b-1, a1b-2 etc. Is using an end number useful? Could this number go into double or triple digits if necessary?
  2. How can you enable users to upload multiple images in one go? E.g.
    image
  3. How can you differentiate between the primary and secondary images? How do you update the primary image?

Question 1 is of the highest priority for me right now but I will also want to consider the other questions in due course.

@Cleop Cleop added question A question needs to be answered before progress can be made on this issue help wanted If you can help make progress with this issue, please comment! labels Nov 7, 2018
@Cleop
Copy link
Member Author

Cleop commented Nov 7, 2018

Having spoken to @Danwhy about this, he suggested using the venue id (or in our case entry_id as that is our non changeable id) as a folder name and then saving the images associated with that venue within that folder with a number as their file name.

I've got the folder bit working and I'm now working on the file name...

I noticed on the example on this repo that it uses a randomly generated string for the file names:
https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback

I was thinking we could do something similar but the phoenix equivalent, however I was wondering whether we need our naming to follow any numerical order/logic or whether random would work too? My concern with having an ordered number is that I'm not sure how we'd query to look up and check which numbers already exist for a given venue.

@Cleop Cleop self-assigned this Nov 7, 2018
@nelsonic
Copy link
Member

nelsonic commented Nov 7, 2018

@Cleop, Dan's intuition is good for storing images in a logical way according to products if a human is going to need to organise/manage/edit/delete images directly on S3.
However in general S3 is used as a "flat" filesystem and the reference (ID) for the image is stored in the Database. (i.e. I would advise you to not use product_id as the "folder" on S3 and just store all images in the same directory and let the database take care of organising images by Product. This is how Amazon does it for their vast e-commerce empire ...)

The short answer for how to store multiple images is a PostgreSQL images "lookup table" that looks like this:

inserted_at image_id product_id order
1541609554 48146852-e096d100-e2ad-11e8-871e-b4f54f713284.png 123 1
1541609876 37981997-1ab4362a-31e7-11e8-9bd8-9566834fc199.png 123 2
1541605678 48127822-8f221e00-e27c-11e8-9b55-3e2389e69e25.png 123 3
  • image_id is a UUID to avoid any chance of collision.
  • product_id is 123 for illustrative purposes only, in CS it's actually a UUID too because it's using alog which uses UUIDs for all IDs.
  • order is the order in which we want the images to be shown if there are multiple images for a product. Having a numeric index makes it easy for us to sort the images and to create an "Admin UI" (later) to allow the Product Owner / Brand Manager to change the image order.

you would "get_by" with the following syntax:
SELECT * FROM images WHERE product_id = '123' ORDER by order DESC
That would retrieve all the images for a given product.

If you only wanted to show one image for a product, simply LIMIT the query to 1.

In the app's config file we need an S3_BASE_URL environment variable e.g:

https://dwyl.s3.amazonaws.com/images/

Such that when we need to display the image in the UI we simply concat the S3_BASE_URL and image_id together to get:
https://dwyl.s3.amazonaws.com/images/48146852-e096d100-e2ad-11e8-871e-b4f54f713284.png

If you've not done image uploading before, you can't be expected to know how to store images.
However reading the relevant StackOverflow issues is within "reach" of all people, the "hard part" is asking the relevant questions to Google (or here).
How many StackOverflow threads did you read before (or since) opening this issue?
E.g: the most up-voted answer on this thread:
https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay

Meta: we need to "fill in the gaps" in core Relational Database terminology/skills
so that everyone on the team can naturally/automatically reach for the most rational way of solving data-related issues.

@nelsonic nelsonic added the technical A technical issue that requires understanding of the code, infrastructure or dependencies label Nov 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

2 participants