载波原始文件名错误

时间:2019-05-03 15:56:29

标签: carrierwave

我有一个大的PNG文件,我想上传并存储在服务器上,大小限制为2000像素平方。然后,我想从该文件生成版本并将其另存为JPEG。这部分正在工作;但是,当我引用“原始”文件的路径时,即使我没有将原始文件转换为JPEG,它也表示文件类型是JPEG而不是PNG。

即: photo.file.path应该以“ whatever.jpg”结尾时以“ whatever.jpg”结尾。 photo.show.file.path应该以“ show_whatever.jpg”结尾。

module Manager
  # Handle Wine image
  class WinePhotoUploader < CarrierWave::Uploader::Base
    include CarrierWave::MiniMagick
    include CarrierWave::ImageOptimizer

    # Choose what kind of storage to use for this uploader:
    storage :file

    after :cache, :update_timestamp

    # Override the directory where uploaded files will be stored.
    # This is a sensible default for uploaders that are meant to be mounted:
    def store_dir
      'for/wines'.freeze
    end

    CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/

    # Provide a default URL as a default if there hasn't been a file uploaded:
    def default_url
      'manager/general/missing-image.png'.freeze
    end

    # To save disk space, make sure the original image is no larger than the croppable + a few pixels
    process :strip_all_but_profile!
    process resize_to_limit: [2000, 2000]

    version :show do
      process :resize_to_show
      process convert: :jpg
      process :optimize
      def full_filename(_)
        super.chomp(File.extname(super)) + '.jpg'
      end
    end

    def common(q = 100, s = '1.2x1+0.75+0.05')
      manipulate! do |img|
        img.combine_options do |image|
          image.quality q
          image.antialias
          image.background :white
          image.flatten
          image.density 72
          image.profile "#{ profiles_path }/sRGB_v4_ICC_preference_displayclass.icc".freeze
          image.strip
        end
        img = yield(img) if block_given?
        img.combine_options do |image|
          image.unsharp s
        end
        img
      end
    end

    def resize_to_show
      common do |img|
        img.combine_options do |image|
          image.trim
          image.thumbnail 110
          image.background :white
          image.gravity :center
          image.extent 170
        end
        img
      end
    end

    # Add a white list of extensions which are allowed to be uploaded.
    # For images you might use something like this:
    def extension_white_list
      %w[jpg jpeg gif png tif tiff]
    end

    # Override the filename of the uploaded files:
    # Avoid using model.id or version_name here, see uploader/store.rb for details.
    def filename
      "#{ model.normalized_permalink }.#{ file.extension }" if original_filename
    end

    def update_timestamp(_)
      return unless version_name.blank?
      model.assets_updated_at = Time.zone.now
    end

    def strip_all_but_profile!
      # Retrieve embedded ICC/ICM profile name if present
      source_profile = begin
                         /Profile-ic(?:c|m):.*\n?(.*)?/.match(`identify -quiet -verbose #{ @file.file }`)[1].strip
                       rescue
                         # No profile present, assume sRGB
                         destination_profile
                       end

      source_profile = "#{ profiles_path }/" << case source_profile
                                                when /1998/
                                                  'AdobeRGB1998.icc'.freeze
                                                when /pro photo/
                                                  'ProPhoto.icm'.freeze
                                                else
                                                  destination_profile
                                                end

      # Strip quietly and convert profiles
      manipulate! do |img|
        img.combine_options do |i|
          i.strip
          i.quiet
          i.profile source_profile
          i.profile destination_profile(include_path: true)
        end
        img
      end
    end

    def destination_profile(include_path: false)
      profile = 'sRGB_v2.icm'.freeze
      include_path ? "#{ profiles_path }/#{ profile }" : profile
    end

    def profiles_path
      "#{ Manager::Engine.root }/lib/manager/color_profiles".freeze
    end
  end
end

0 个答案:

没有答案