下载使用Carrierwave上传到S3的图像时,我得到一张空白图像

时间:2015-05-20 05:49:54

标签: ruby-on-rails carrierwave fog

在我的应用中,我使用carrierwave和fog将图像上传到S3。在网站中,人们可以选择尺寸并下载照片。当他们点击下载时,程序将使用image magick根据所选大小调整图像大小,然后将其发送给用户。它可以找到并发送文件。但问题是当我打开图像时它是空白的!文件中没有任何内容。当我在我的本地服务器上测试时它可以工作,但在生产中它不会。这是发送下载的动作:

def download
    @photo = Photo.friendly.find(params[:photo_id])
    if @photo.free?
        if params[:size].present?
            size = Size.find(params[:size])

            @photo.file.cache_stored_file!
            img = Magick::Image.read(@photo.file.file.file).first
            img.resize!(size.width, size.height)

            filename = "#{size.width}x#{size.height}-#{@photo.file.file.filename}"

            path = "#{Rails.root}/tempfreedownload/#{filename}"
            File.write(path, '')
            img.write path

            File.open(path, 'r') do |f|
                send_data f.read, type: 'image/png', filename: filename
            end

            File.delete(path)
            img.destroy!

            downloads = @photo.downloads + 1
            @photo.update_attribute(:downloads, downloads)
        else

        end

    else
        render file: "#{Rails.root}/public/404.html", layout: false, status: 404
    end
end

可能是什么问题?

0 个答案:

没有答案