My user uploads a zip file with 3 files (A.ttf, A.svg, A.otf) and I want to store the original zip plus the 3 font files inside it. I’ve created the 3 versions with this code
version :ttf
process :font => :ttf
end
version :svg
process :font => :svg
end
version :otf
process :font => :otf
end
It successfully saves 4 copies of the original file, all with the proper file name. However, I don’t know how to get CarrierWave to store the individual files. This code doesn’t work. 🙁
def font(format)
new_file = nil
# Loop through the zip file and extract the files
Zip::ZipFile.open(@file.file) do |files|
files.each do |f|
next unless f.file?
filename = f.name.split("/").last
ext = filename.split('.').last
# Save the file with the proper file extension
new_file = f if ext == format
end
# Return the file to be stored by CarrierWave
new_file
end
OK, after many hours of banging my head against the wall the light finally came on. The solution is in how CarrierWave processes the upload. When you define a version, CW duplicates the file with a new name ([version name]_original_filename) and gives it to you in the
current_pathvariable. You can do anything you want with this file reference (ie open the file and truncate it, or fill it with random date, etc.) and when you get done CW will store the file for you.Somehow I missed the connection and when I realized what was going on it almost blinded me. I’m answering this question here so that it might help some other poor soul lost in the dark. And to show the world my ignorance. :/