smallseo.info

carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks carrierwaveuploader/carrierwave · GitHub carrierwave - classier solution for file uploads for rails, sinatra and other ruby web frameworks

Displaying a Carrierwave filename in the view

I am trying to display the filename of a Carrierwave attachment in a Rails erb template. The following does not work:

<%= @page.form.filename %>

This seems in line with the documentation. Is some additional step needed?

My page model looks like this:

class Page < ActiveRecord::Base

  mount_uploader :form, FormUploader

end

The form uploader looks like this:

class FormUploader < CarrierWave::Uploader::Base

  storage :file

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

  def extension_white_list
    %w(pdf)
  end

end

Source: (StackOverflow)

Validate image size in carrierwave uploader

All uploads should be at least 150x150 pixels. How to validate it with Carrierwave?


Source: (StackOverflow)

Undefined Method Scale In CarrierWave

I'm trying to use this code in my uploader

  version :thumb do
    process :scale => [50, 50]
  end

and I get an error saying

undefined method `scale' for #<#<Class:0x235b680>:0x0fb4c8>

I'm using Carrierwave with MiniMagick. How can I fix this error?


Source: (StackOverflow)

Configure Environment to Use File System Locally and Amazon on Heroku

I am not sure how to configure the environment such that Carrier Wave will use the local file storage when running the app locally (development) and s3 after i load to heroku (production)

in Development storage :file

in Production storage :s3


Source: (StackOverflow)

Downloading and zipping files that were uploaded to S3 with CarrierWave

I have a small Rails 3.2.1 app that uses CarrierWave 0.5.8 for file uploads to S3 (using Fog)

I want users to be able to select some images that they'd like to download, then zip them up and send them a zip. Here is what I've come up with:

def generate_zip
  #A collection of Photo objects. The Photo object has a PhotoUploader mounted.
  photos = Photo.all

  tmp_filename = "#{Rails.root}/tmp/" << Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s << ".zip"
  zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE)
  zip.close

  photos.each do |photo|
    file_to_add = photo.photo.file
    zip = Zip::ZipFile.open(tmp_filename)
    zip.add("tmp/", file_to_add.path)
    zip.close
  end

  #do the rest.. like send zip or upload file and e-mail link

end

This doesn't work because photo.photo.file returns an instance of CarrierWave::Storage::Fog::File instead of a regular file.

EDIT: The error this leads to:

Errno::ENOENT: No such file or directory - uploads/photos/name.jpg

I also tried the following:

tmp_filename = "#{Rails.root}/tmp/" << Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s << ".zip"
    zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE)
    zip.close

    photos.each do |photo|
      processed_uri = URI.parse(URI.escape(URI.unescape(photo.photo.file.authenticated_url)).gsub("[", "%5B").gsub("]", "%5D"))
      file_to_add = CarrierWave::Uploader::Download::RemoteFile.new(processed_uri)
      zip = Zip::ZipFile.open(tmp_filename)
      zip.add("tmp/", file_to_add.path)
      zip.close
    end

But this gives me a 403. Some help would be greatly appreciated.. It probably is not that hard I'm just Doing it Wrong™


Source: (StackOverflow)

Rails: carrierwave uninitialized constant error

I have Rails 3

Carrierwave 0.5.4

//app/uploaders/fasta_uploader.rb

class FastaUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
      'public/data/01_fasta'
  end
end

//migration

class AddFileUpToCvits < ActiveRecord::Migration
  def self.up
    add_column :cvits, :fasta, :string
  end

  def self.down
    remove_column :cvits, :fasta
  end
end

//app/models/cvit.rb

class Cvit < ActiveRecord::Base
    attr_accessible :fasta
    mount_uploader :fasta, FastaUploader
end

//form

<%= form_for(@cvit, :html => {:multipart => true, :onsubmit => "return ray.ajax()" }) do |f| %>
  ...
  ...
  <%= f.file_field :fasta %><br></br>
  <div class="actions">
    <%= f.submit "Submit"%>
  </div>
<% end %>

I get this error: uninitialized constant Cvit::FastaUploader

Any suggestions???


Source: (StackOverflow)

rspec testing carrierwave - newbie

I'm making a rails 3.1 app using carrierwave to upload files to aws s3. I've followed the instructions on the carrierwave github repository and am now able to upload files to my aws bucket. It's the testing that got me stuck. Over the last two days I've been googling and revising, using all the other Q&A's I've found but am finally crying 'mama.' Here's what I've got:

/app/uploaders/image_file_uploader.rb

storage :fog

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

/config/initializers/carrierwave.rb

if Rails.env.test? or Rails.env.cucumber?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false
  end
end

/spec/uploaders/image_file_uploader_spec.rb

require 'spec_helper'
require 'support/fog_helper'
require 'carrierwave/test/matchers'

describe ImageFileUploader do
  include CarrierWave::Test::Matchers

  before do
    ImageFileUploader.enable_processing = true
    @user = Factory(:user, :email => "photo_taker@example.edu")
    @uploader = ImageFileUploader.new(@user, Factory(:image))
    @uploader.store!(File.open("#{Rails.root}/tmp/uploads/#{Rails.env}/images/"))
  end

  after do
    @uploader.remove!
    ImageFileUploader.enable_processing = false
  end

  context 'the tiny version' do
    it "should scale down a landscape image to be exactly 50 by 50 pixels" do
      @uploader.tiny.should have_dimensions(50, 50)
    end
  end

spec/factories.rb

Factory.define :image do |image|
  include ActionDispatch::TestProcess

  image.date_taken            "Sun, 09 Oct 2011"
  image.time_taken            "2000-01-01 03:41:00 UTC"
  image.image_file            fixture_file_upload('spec/support/test_images/audi.png', 'image/png')
  image.taken_by              "John Doe"
  image.collection            "N/A"
  image.comments              "Beautiful day!"
  image.association   :user

end

While my /public/uploads/tmp/ is getting filled up with 'tiny' (and other versions) of the images whose generation I am testing, the tests continue to fail with the following error message:

1) ImageFileUploader the tiny version should scale down a landscape image to be exactly 50 by 50 pixels

 Failure/Error: @uploader = ImageFileUploader.new(@user, Factory(:image))
 Excon::Errors::NotFound:
   Expected(200) <=> Actual(404 Not Found)
     request => {:expects=>200}
     response => #<Excon::Response:0x0000010569f928 @body="", @headers={}, @status=404>
 # ./spec/uploaders/image_file_uploader_spec.rb:11:in `block (2 levels) in <top (required)>'

I know that the above means that rspec isn't finding my testing bucket. Anyone have any thoughts about what I'm doing wrong?

would be super grateful for any new leads.

UPDATE: 10/11/11 The file upload works but I've stalled in figuring out how to get test involving the images to pass. In the short term, I will use a placeholder image as I flesh out the rest of my app and return to this later. I'll post a further update once I figure this out. (Do leave any thoughts if you have any insights, however.)


Source: (StackOverflow)

Carrierwave; multiple uploaders or just one?

I have a post model and a podcast model. Both models have an attribute titled: image. I'm using one Carrierwave uploader (named ImageUploader) to handle both models. I have two questions before I go into production.

Dumb question:

Is it ok to use the same uploader for two different models when they both have the same attribute name for their file attachements? sorry if it seems obvious

Main question:

I want to create three versions of each blog post image (thumb, large, sepia) and only 1 version of each podcast image (thumb).

Do I need to use two uploaders now or can I namespace with the one that I'm already using?

Again it probably seems obvious. I could probably have written the second uploader in the time its taken me to ask these questions


Source: (StackOverflow)

Factory with carrierwave upload field [duplicate]

This question already has an answer here:

Hello i need to build up Factory for my model, for example

Factory.define :farm do |f|
  f.name { Factory.next :name }
  f.harvest '3'
  f.offers 'Random'
  f.latitude '43'
  f.longitude '-70'
  f.about 'We rocks!'
  f.logo { Factory.next :logo } # this doesn't work
end

For now im just pass string "#{n}.jpg" into my logo field and this dont work, how to evalute this field? Im using CarrierWave for uploading.


Source: (StackOverflow)

Uploading a remote file url from Rails Console with Carrierwave

I just wanted to know how one would go about uploading a remote file url using Carrierwave in the Rails console.

I tried the following without any luck. I presume it's not processing the Uploader?

user = User.first
user.remote_avatar_url = "http://www.image.com/file.jpg"
user.save

Many thanks


Source: (StackOverflow)

Deleting files in ruby

I'm using the carrierwave gem to upload files. I have built a system for users to flag images as inappropriate and for admins to remove the images. From what I can tell, calling destroy on the image will only remove the path name from the tabel. Is there a way to have ruby actually remove the file itself? Or should rails be automatically removing the file when I destroy the image path?


Source: (StackOverflow)

Carrierwave files with Capistrano

I'm using rails 3.2 with asset and carrierwave for upload some images, they store in /public/uploads/photo/..... but when I do a cap:deploy (with capistrano) my current directory application doesn't contain the files I uploaded, because capistrano make a new version ....

=== Update ===

After all I use this :

inside :deploy namespace

   task :symlink_uploads do
     run "ln -nfs #{shared_path}/uploads  #{release_path}/public/uploads"
   end

and after:

after 'deploy:update_code', 'deploy:symlink_uploads'

=== Re Update ===

The solution of @tristanm is the best way to solve this.


Source: (StackOverflow)

Seeding file uploads with CarrierWave, Rails 3

I'm trying to seed a database in Rails 3 with images using CarrierWave, however nothing I try seems to work short of having to upload them all by hand.

pi = ProductImage.new(:product => product)
pi.image = File.open(File.join(Rails.root, 'test.jpg'))
pi.store_image! # tried with and without this
product.product_images << pi
product.save!

Anybody know how to seed using CarrierWave at all?


Source: (StackOverflow)

Renaming uploaded files with Carrierwave

I'm using Carrierwave to upload files, and I have it working.

My issue is attempting to change the name of the uploaded file.

In the generated uploader.rb there is a method I think I should be using

def filename
   "something.jpg" if original_filename
   basename = "what"+orginal_filename if original_filename, works
   basename = (0...8).map{65.+(rand(25)).chr}.join if original_filename  # will create a random name for each version, e.g. the orginal, the thumb, and the filename in the db, useless
 end

I can't seem to access items like 'extension' or 'content_type' in sanitized_file.rb, so this is a bit beyond my current skill level right now.

Any suggestions or exercises for doing this, i.e. generate filename for an uploaded file that works as well as the carrierwave default (do nothing, but does carry on to each version)? Seems like it should be simple enough but I've stumbled over this.


Source: (StackOverflow)

Reprocessing images in Carrierwave

Let's say my model has an image with :thumb and the client wants :tiny and :nano thumbnails.

How do I reprocess all the existing images using a rake task?

I've found a rask that I thought would do it https://gist.github.com/777788 but it's giving me errors.


Source: (StackOverflow)