carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.66k forks source link

Carrierwave is not storing images locally to Rails public folder after processing #2005

Closed mankind closed 5 years ago

mankind commented 8 years ago

I am trying to upload a pdf file and then convert it to jpeg image where each page of the pdf file will be saved as an individual jpeg file.

The problem is that the jpeg images are written to the wrong folder . Instead of writing the images to the usual path where carrierwave store files locally that Rails public folder, it is writing the converted pdf files to the Rails root just underneath the gemfile.

I have tried out the 2 commented out lines in my code and it still saves to the wrong folder path.

 class UploadImgUploader  < CarrierWave::Uploader::Base
   include CarrierWave::MiniMagick
   storage :file

   def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

   process :generate_png

   def generate_png
      pdf = MiniMagick::Image.open(self.file.path)
      pdf.pages.each_with_index do |page, index|
        #page.write("page#{index}.png")
        #page.write("#{File.dirname(self.file.path)}/#{self.filename}_#{index}.png")

         page.write("#{File.expand_path(store_dir)}/#{self.filename}_#{index}.png")
      end
   end

 end
vshy108 commented 5 years ago

For my case, it is because of not installed ImageMagick, for you case, I think you need to install MiniMagick in your OS system.

mshibuya commented 5 years ago

Be aware that store_dir is relative path, you must give absolute path to MiniMagick.