I am uploading a PDF in my Rails application using Carrierwave. My aim is to convert each page in the PDF to an PNG and ensure that each of these PNGs resides in the uploads directory which Carrierwave creates based on my model etc.
Current progress is that I am able to upload a PDF, convert it to a series of PNGs in the temporary directory that Carrierwave creates but I am unable to work out the correct approach to moving these converted PNGs into the designated uploads directory:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
UPDATE – Added below my current attempt and error code
My current code is as follows:
def extract(format)
cache_stored_file! if !cached?
images = Magick::ImageList.new(current_path)
images.write File.dirname( current_path ) << "/" << filename
end
def filename
super != nil ? super.split('.').first + '.png' : super
end
All attempts to move the files using any approach to the uploads directory results in some sort of ‘no such file or directory’ Error. For example using:
images.each do |f|
FileUtils.mv f.filename, File.join("#{Rails.root}/#{store_dir}", "image-0.png")
end
Errno::ENOENT (No such file or directory -
(/Users/reggie/ExampleApp/public/uploads/tmp/20120611-2259-7520-3647/image-0.png,
/Users/reggie/ExampleApp/public/uploads/painting/image/39/image-0.png))
Any suggestions would be welcomed to help me get through this wall I have hit.
Just as a side note as to why I am not using the manipulate logic, example code (see below) results in the same as above i.e. converted files in the temporary directory created by Carrierwave, however all the converted images remain with the .pdf file extension.
manipulate!(:format => :png) do |img|
img
end
Well, your main problem is that your storing folder don’t exist.
Carrierwave path construction is relative to “public” folder.
You should use something like this in your uploader:
To remove those files use a callback.