Gem carrierwave-upyun 重建缩略图时出错

iwinux · March 16, 2012 · Last by hz_qiuyuanxin replied at July 24, 2013 · 4117 hits

问题描述

在 rails console (RAILS_ENV=production) 中调用 photo.image.recreate_versions! 时报错,错误信息如下:

NoMethodError: undefined method `content_type' for #<CarrierWave::Storage::UpYun::File:0x00000005bc84c8>
        from carrierwave-0.5.8/lib/carrierwave/uploader/cache.rb:74:in `cache_stored_file!'
        from carrierwave-0.5.8/lib/carrierwave/uploader/versions.rb:179:in `recreate_versions!'

请问这是又拍云的问题,还是 carrierwave-upyun 的问题?据说使用 Mongoid 的 GridFS 也会有同样的情况,大家有没有遇到过呢?

环境

  • rails 3.1 (ActiveRecord + pg)
  • carrierwave 0.5.8
  • carrierwave-upyun 0.1.2

相关代码

class Photo < ActiveRecord::Base
  mount_uploader :image, PhotoUploader
end

class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :upyun

  version :thumb do
    process :resize_and_pad => [150, 150, '#000']
  end

  version :thumb_wide do
    process :resize_and_pad => [230, 143, '#000']
  end
end

额,之前没有考虑过这个功能,基本上算是没有实现。

upyun 可以自己缩略 何必用自己的服务器 CPU 涅?

#2 楼 @huobazi 又拍云提供的缩略方式不符合我们的要求(比如 resize_and_pad ——按原图比例缩小到指定尺寸范围,空白的地方就用黑色填充)

#1 楼 @huacnlee 原来是这样……那我有空把它补上好了 =。=

今天更新了 carrierwave-upyun 的版本到 0.1.5,发现 @nowa 已经修复了这个功能~不过这次我发现了又拍云 API 的一个小问题:

  1. 通过 http://bitbucket-name.b0.upaiyun.com/file-path 访问图片文件时,content-type 正常,比如 image/jpeg

  2. 通过 http://v0.api.upyun.com/bitbucket-name/file-path 访问图片文件时,content-type 总是会返回 text/html,导致调用 recreate_versions! 重新上传图片文件时会把原先的 content-type 覆盖掉,在浏览器里访问图片文件会显示一堆乱码

话说我在又拍云的网站上找不到提交 ticket 的地方,联系客服貌似还要预约!?目前暂时的解决访问是 monkey-path 掉 CarrierWave::Storage::Upyun::File#content_type 这个方法:

class CarrierWave::Storage::UpYun::File 
  def content_type
    'image/jpeg' # 我们会把所有图片都转成 jpg...
  end  
end

我更衰,crop image 后,竟然啥都没变,我估计是不会自动重新上传。。。

You need to Sign in before reply, if you don't have an account, please Sign up first.