robin850 / carrierwave-dropbox

Carrierwave storage for Dropbox
MIT License
66 stars 28 forks source link

undefined method 'content_type' for #Hash #16

Open andisthejackass opened 9 years ago

andisthejackass commented 9 years ago

I am trying to use publify with this gem. I could upload resources locally, but when I switched to carrierwave-dropbox I get the error Undefined method 'content_type' for #<Hash:0x0000000683a990> while the images are normally uploaded to my dropbox folder. Any idea why this is happening? Here's the app trace

NoMethodError in Admin::ResourcesController#upload
undefined method `content_type' for #<Hash:0x0000000683a990>

app/uploaders/resource_uploader.rb:42:in `image?'
app/controllers/admin/resources_controller.rb:15:in `upload'

resource_uploader.rb

class ResourceUploader < CarrierWave::Uploader::Base
  storage :dropbox
  # To handle Base64 uploads...
  class FilelessIO < StringIO
    attr_accessor :original_filename

    def initialize(data, filename)
      super(data)
      @original_filename = filename
    end
  end

  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  process :set_content_type

  def store_dir
    "files/#{model.class.to_s.underscore}/#{model.id}"
  end

  version :thumb, :if => :image? do
    process :dynamic_resize_to_fit => :thumb
  end

  version :medium, :if => :image? do
    process :dynamic_resize_to_fit => :medium
  end

  version :avatar, :if => :image? do
    process :dynamic_resize_to_fit => :avatar
  end

  def dynamic_resize_to_fit(size)
    blog = Blog.default
    resize_setting = blog.send("image_#{size}_size").to_i

    resize_to_fit(resize_setting, resize_setting)
  end

  def image?(new_file)
    new_file.content_type && new_file.content_type.include?('image')
  end
end

resources_controller.rb

require 'fog'

class Admin::ResourcesController < Admin::BaseController
  cache_sweeper :blog_sweeper

  def upload
    if ! params[:upload].blank?
      file = params[:upload][:filename]

      unless file.content_type
        mime = 'text/plain'
      else
        mime = file.content_type.chomp
      end
      @up = Resource.create(:upload => file, :mime => mime, :created_at => Time.now)
      flash[:success] = I18n.t('admin.resources.upload.success')
    else
      flash[:warning] = I18n.t('admin.resources.upload.warning')
    end

    redirect_to :action => "index"
  end

  def index
    @r = Resource.new
    @resources = Resource.order('created_at DESC').page(params[:page]).per(this_blog.admin_display_elements)
  end

  def get_thumbnails
    position = params[:position].to_i
    @resources = Resource.without_images.by_created_at.limit(10).offset(position)

    render 'get_thumbnails', :layout => false
  end

  def destroy
    begin
      @record = Resource.find(params[:id])
      mime = @record.mime
      return(render 'admin/shared/destroy') unless request.post?

      @record.destroy
      flash[:notice] = I18n.t('admin.resources.destroy.notice')
      redirect_to :action => 'index'
    rescue
      raise
    end
  end
end
n-studio commented 8 years ago

store! and retrieve! should return a CarrierWave::SanitizedFile instance instead of a hash.

Working on a fix.