0

本质上,photo_url(:thumb) 会显示预期的缩略图 url,即使该文件实际上并不存在。

# encoding: utf-8

class WinePhotoUploader < CarrierWave::Uploader::Base

    include CarrierWave::MiniMagick

    storage :file

    # So the :thumb will be stored in "public/assets/wines/thumb/2008-meritage-750mL.jpg" for example.
    def store_dir
        "assets/wines/#{version_name}"
    end

    def cache_dir
        Rails.root.join 'tmp/uploads'
    end

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

    # If no version name is present (original), just provide the thumb path
    # This method is never called!!! Why not?
    def default_url
        ActionController::Base.helpers.asset_path("pages/wines/#{version_name || :thumb}/default.png")
    end

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

    version :croppable, :if => :not_cropping? do
        process :common
        process :resize_and_pad => [1200, 1200, :white]
        process :convert => "jpg"
        # Override the default naming convention...I store in folders,
        # so I don't want the version prepended to the file name.
        def full_filename(for_file = model.photo.file)
            the_filename
        end
    end

    version :show, :if => :not_cropping? do
        process :resize_to_show
        process :convert => "jpg"
        def full_filename(for_file = model.photo.file)
            the_filename
        end
    end

    version :thumb, :from_version => :croppable, :if => :viewing_or_cropping? do
        process :custom_thumbnail => [200, 200]
        def full_filename(for_file = model.photo.file)
            the_filename
        end
    end




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

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

    def custom_thumbnail(width, height)
        manipulate! do |img|
            img.combine_options do |i|
            i.distort :srt, "#{model.crop_x},#{model.crop_y} 1 -30 600,600"
            i.repage.+
            i.gravity :center
            i.crop "#{model.crop_w}x#{model.crop_h}+0+0"
            i.repage.+
            i.thumbnail "#{width}x#{height}"
            i.unsharp '1.2x1+0.75+0.05'
            end
            img
        end
    end




    def not_cropping? picture
        !model.cropping?
    end
    # For some stupid reason, if I only check for 'model.cropping?' and
    # it returns false, the photo_url(:thumb) will always show the default_url
    # and never show the actual url! This is terrible logic. The version declarations
    # above should only affect the generation of thumbnails, not whether the call
    # to photo_url(:thumb) works.
    def viewing_or_cropping? picture
        model.cropping? or model.changes.merge(model.previous_changes).empty?
    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

    # Only modify the version names, not the original uploaded file name
    def filename
        "#{ model.get_permalink(:normalized => true) }#{ File.extname original_filename }" if original_filename
    end

    # The filename should be the wine's permalink name (ex., 2008-merlot-reserve-750mL.jpg)
    def the_filename
        "#{ model.get_permalink(:normalized => true) }.jpg"
    end

    # Remove this when https://github.com/carrierwaveuploader/carrierwave/issues/1164 is solved.
    def recreate_versions!(*versions)
    if versions.any?
        from_versions = versions.group_by { |v| self.class.versions[v][:options][:from_version] }
        from_versions.each do |from, versions|
        next if from.nil?
        file = CarrierWave::SanitizedFile.new(self.versions[from.to_sym].file)
        store_versions!(file, versions)
        end
    else
        super(*versions)
    end
    end

end

所以调用@wine.photo_url(:thumb) 会产生

http://dev.mydomain.com/assets/wines/thumb/2006-cabernet-sauvignon-750mL.jpg

什么时候应该生产

http://dev.mydomain.com/assets/pages/wines/thumb/default.png

因为该文件不存在。

4

1 回答 1

0

我最好的猜测是 Carrierwave 根据数据库中模型属性的存在而不是文件的特定版本是否存在来生成文件名。不是我对 default_url 的期望,但它仍然以有限的方式有意义。

于 2014-02-13T00:59:01.993 回答