Rails + carrierwave:msword内容类型的默认拇指

时间:2014-01-23 11:27:13

标签: ruby ruby-on-rails-3

我使用的是Rails 3.2.11 + Carrierwave。 请帮忙,如何设置特定内容类型的默认拇指,例如.doc或.docx? 默认拇指放在/ public / assets中 - thumb_for_doc.jpg

我的上传者:





      # Include RMagick or MiniMagick support:
      include CarrierWave::RMagick
      #include CarrierWave::MiniMagick
      include CarrierWave::MimeTypes

      # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
       include Sprockets::Helpers::RailsHelper
       include Sprockets::Helpers::IsolatedHelper

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


      def store_dir
        "uploads/order_#{model.order.id}" unless model.order.nil?
      end

      process :set_content_type

      version :pdf, :if => :pdf? do
        process :cover
        process :resize_to_fit => [100, 100]
        process :convert => :jpg

        def full_filename (for_file = model.source.file)
          super.chomp(File.extname(super)) + '.jpg'
        end
      end

      version :msword, :if => :msword? do
        #????
      end

      version :thumb, :if => :image? do
        process :resize_to_limit => [100, 100]
      end

      def filename
        if model.document_specification_id
          document_specification = Lists::DocumentSpecification.find_by_id(model.document_specification_id)
          margins = Lists::PrintMargin.find_by_id(document_specification.print_margin_id).margin
          paper_type = Lists::PaperType.find_by_id(Lists::PaperSpecification.find_by_id(document_specification.paper_specification_id).paper_type_id).paper_type
          paper_size = Lists::PaperSize.find_by_id(Lists::PaperSpecification.find_by_id(document_specification.paper_specification_id).paper_size_id).size
          "#{secure_token}_PP_#{paper_size.parameterize}_#{paper_type.parameterize}_#{model.quantity}_#{margins.parameterize}.#{file.extension}" if original_filename.present?
        else
          "#{secure_token}_PP_not_set_spesification.#{file.extension}" if original_filename.present?
        end
      end

      def cover
        manipulate! do |frame, index|
          frame if index.zero? # take only the first page of the file
        end
      end

      protected
        def secure_token
          var = :"@#{mounted_as}_secure_token"
          model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(4))
        end

        def image?(file)
          file.content_type.start_with? 'image'
        end

        def pdf?(file)
          file.content_type.start_with? 'application/pdf' 
        end

        def msword?(file)
          file.content_type.start_with? 'application/msword' 
        end
    end

谢谢!

0 个答案:

没有答案