rootstrap / active-storage-base64

Base64 support for ActiveStorage
https://rootstrap.com
MIT License
160 stars 16 forks source link

[Questions] how to validate file size #62

Closed rslhdyt closed 3 years ago

rslhdyt commented 3 years ago

Hello, thank you for this cool gem. It's helped me a lot to handle upload images from base64 strings. I have a question, how do validate the file size before saving it to the database?

I've tried to use rails active storage validation, but it seems not working. tried to upload images that were bigger than 1 megabyte and are was stored in the database.

model.rb
has_many_base64_attached :images

validates :images, size: { less_than: 1.megabytes }, limit: { min: 1, max: 5 }

*_controller.rb
def create
   Product.create(permitted_params)
end

private

def permitted_params
    params.require(:product).permit(:name, images: [:data])
end

I am using Rails 6.1

Ricoch commented 3 years ago

Hey @rslhdyt! I might be wrong here, but I don't think there's currently an OOTB way to do this in AS, I found this PR that is still open on rails repository for adding size validation for AS and also found a couple of other PRs aiming to add validation for AS attachments but they were closed. I found a couple of gems that might help you do that, although I'm not sure how reliable they are since I have personally never used them: https://github.com/igorkasyanchuk/active_storage_validations https://github.com/aki77/activestorage-validator

Other than this, maybe you could do a custom implementation like the one here: https://stackoverflow.com/questions/48158770/activestorage-file-attachment-validation

Hope to have helped!

flomosq commented 3 years ago

Hi @rslhdyt, we have been trying to reproduce this issue but we couldn't. Can you provide more details on how to reproduce this?

rslhdyt commented 3 years ago

Hi, @Ricoch Sorry for the late reply, I forgot to mention yes, I was using the gem active_storage_validations to validate image size, content type, etc.

@flomosq When I tried to upload without using the base64 string, the validation worked. for example, this is my model setup

class Product
   ...
   has_many_attached :image

   validates :image, size: { less_than: 1.megabytes }
   ...
end

then I tried to upload an image with a size bigger than 1MB and then I got error validation "Max image size is 1MB". but since I need to upload with base64 string and update the model like this

class Product
   ...
   has_many_base64_attached :image

   validates :image, size: { less_than: 1.megabytes }
   ...
end

when I upload an image with a size bigger than 1MB the validation is not working. I think the problem is from the active storage validations.

Ricoch commented 3 years ago

Hey @rslhdyt Thanks for adding you are using active_storage_validations. Added the gem to a test project and did a test locally, but unfortunately I was unable to reproduce your issue: image May I ask what versions of active_storage_base64 and active_storage_validations are you using?

Ricoch commented 3 years ago

Maybe it's just an error in your comment, but, I see that in your example above you have:

def permitted_params
    params.require(:product).permit(:name, images: [:data])
end

But on your latest comment you have:

class Product
   ...
   has_many_base64_attached :image

   validates :image, size: { less_than: 1.megabytes }
   ...
end

would it be the mismatch between images on your controller and image on your model that is causing this issue?

rslhdyt commented 3 years ago

Hi, @Ricoch I just found out that the error was caused by my fault when processing the image from frontend sides.

now the validation works perfectly.

Thank you very much!