I'm following this article in CarrierWave Wiki https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Add-more-files-and-remove-single-file-when-using-default-multiple-file-uploads-feature to implement adding more images and removing images for a model in my system using CarrierWave Multiple Uploads feature.
The main code in that article is
def add_more_images(new_images)
images = @gallery.images
images += new_images
@gallery.images = images
end
def remove_image_at_index(index)
remain_images = @gallery.images # copy the array
deleted_image = remain_images.delete_at(index) # delete the target image
deleted_image.try(:remove!) # delete image from S3
@gallery.images = remain_images # re-assign back
end
It works. However, it is too slooooow. I have looked at the log and the overall processing time is as follow:
- Upload 1 image: it takes 5000ms for example
- Add 1 more image: it takes 8500ms (2 images)
- Add 1 more image: it takes 12000ms (3 images)
- Remove 1 image: it takes 8400ms (back to 2 images)
I have tested the sample app of this solution written by the author on my local machine and it was very slow too.
It seems like CarrierWave reuploads and re-processes all images even we only add or remove 1 image. I think because we are re-assigning back new array of images to @gallery so that it treats old images as new ones.
Also there is a related issue here https://github.com/carrierwaveuploader/carrierwave/issues/1704#issuecomment-259106600
Does anyone have any better solution for adding and removing images using CarrierWave multiple upload feature?
Thanks.