I'm using paperclip and AWS S3 for file storage.
I have a Car model and an Image model.
A car has_many :images
.
The image has_attachment :file
.
A car can have as many images as I want.
What I wanted is a way to download all these car's images at the same time.
I have working code:
def download
@images = @car.images
compressed_filestream = Zip::OutputStream.write_buffer do |zos|
@images.each do |img|
zos.put_next_entry img.file_file_name
zos.print open(img.file.url).read
end
end
compressed_filestream.rewind
send_data compressed_filestream.read, filename: "#{@car.name}.zip"
end
When /cars/1/download
is requested the above controller actions runs.
It works but I find it really slow.
What I want now is a faster solution for mass download.
I find download time to take 6 seconds per megabyte.
I want a faster way. I know that you can go to any web page, right-click, and "Save As..." in order to save that particular page. When the page has images, they appear in a new folder after the download finishes. The download is also really fast. I guess this is so because the browser has already downloaded those images, so it justs gives them to my computer instead of fetching the images again. If the browser can download an HTML file and a folder of assets, we should be able to make the browser download just a folder of images right?
I have a few ideas that I will work on but I want to know if anyone has some faster solutions or input at least on current ideas.
Ideas:
Instead of drafting a new .zip file everytime someone wants to download, edit the .zip file everytime the car's images get updated. This way when the user requests all the images, the file already exists, and they just download it. But where should these .zip files go? Where and how do we save them?
In JavaScript you can create blob files using some image url. Can we load all the images after the page has loaded? This way the page load is fast, but then in the background, while the user is viewing the page, the browser is downloading the images in the background. If the user decides to download them, the download time is fast.
Maybe my controller action could be improved to create a temporary .zip file faster.
Ideas anyone?