How do I: confirm remote location with CarrierWave?

I use CarrierWave in a Rails 3 application. I want to check the remote location of the location, so I don’t get the standard error exception when a user sends an invalid URL with either a blank or not an image:

CarrierWave::DownloadError in ImageController#create
trying to download a file which is not served over HTTP

This is my model:

class Painting < ActiveRecord::Base
  attr_accessible :gallery_id, :name, :image, :remote_image_url
  belongs_to :gallery
  mount_uploader :image, ImageUploader

  validates :name,        :presence => true,
                          :length =>  { :minimum => 5, :maximum => 100 }
  validates :image,       :presence => true

end

This is my controller:

class PaintingsController < ApplicationController
  def new
    @painting = Painting.new(:gallery_id => params[:gallery_id])
  end

  def create
    @painting = Painting.new(params[:painting])
    if @painting.save
      flash[:notice] = "Successfully created painting."
      redirect_to @painting.gallery
    else
      render :action => 'new'
    end
  end

  def edit
    @painting = Painting.find(params[:id])
  end

  def update
    @painting = Painting.find(params[:id])
    if @painting.update_attributes(params[:painting])
      flash[:notice] = "Successfully updated painting."
      redirect_to @painting.gallery
    else
      render :action => 'edit'
    end
  end

  def destroy
    @painting = Painting.find(params[:id])
    @painting.destroy
    flash[:notice] = "Successfully destroyed painting."
    redirect_to @painting.gallery
  end
end

I'm not quite sure how to solve this problem so that any understanding is wonderful.

+5
source share
4 answers

A solution to this problem was added to the CarrierWave Wiki on Github.

Edit:
, . AR on Rails 3.1.3.

, , , . , . , .

0

. , , CarrierWave... URL. CarrierWave , URL- , . DownloadError IntegrityErrors , - .

, . :

valid = false
begin
  par = params[:image].except(:remote_upload_url)
  @image = Image.new(par)
  # this may fail:
  @image.remote_upload_url = params[:image][:remote_upload_url]
  valid = true
rescue CarrierWave::DownloadError
  @image.errors.add(:remote_upload_url, "This url doesn't appear to be valid")
rescue CarrierWave::IntegrityError
  @image.errors.add(:remote_upload_url, "This url does not appear to point to a valid image")
end 

# validate and save if no exceptions were thrown above
if valid && @image.save
  redirect_to(images_configure_path)
else
 render :action => 'new'
end

, , URL. , , , . , . , .

, , .

+8

. rescue_from application_controller.rb -, . , . , , .

  rescue_from CarrierWave::DownloadError, :with => :carrierwave_download_error
  rescue_from CarrierWave::IntegrityError, :with => :carrierwave_integrity_error

  def carrierwave_download_error
    flash[:error] = "There was an error trying to download that remote file for upload. Please try again or download to your computer first."
    redirect_to :back
  end

  def carrierwave_integrity_error
    flash[:error] = "There was an error with that remote file for upload. It seems it not a valid file."
    redirect_to :back
  end
+1

wiki- CarrierWave . , CarrierWave . , , , . - , , , . .

config, .

attr_accessor :additional_error_message, :original_attributes

def initialize(*args)
  self.original_attributes = args[0]
  begin
    super
  rescue CarrierWave::IntegrityError # bad file type
    self.additional_error_message = 'must be a PNG, JPEG, or GIF file' # depends on your whitelist
  rescue OpenURI::HTTPError # 404
    self.additional_error_message = 'could not be found'
  rescue RuntimeError # redirection
    self.additional_error_message = 'could not be loaded'
  rescue CarrierWave::DownloadError
    self.additional_error_message = 'could not be loaded'
  rescue
    self.additional_error_message = 'could not be loaded'
  end
end

before_validation do |image|
  if additional_error_message.present?
    errors.add(remote_image_url, additional_error_message)
    self.name = original_attributes[:name] # replace this with re-adding all of your original attributes other than the remote_image_url
  end
end

# the image will have an "is blank" error, this removes that
after_validation do |image|
  errors.delete(:image) if additional_error_message.present?
end
0

All Articles