Using an existing image on s3 with Carrierwave? - ruby

I'm trying to use existing images on my s3 and relate them to a carrierwave object. These images are stored in a different location from the store_dir of my uploader.
I currently have my migration setup using remote_image_url but it is extremely slow and I don't think re-uploading them is necessary.
So, is it possible to point my uploader to an existing image that uses a folder structure different from my store_dir?
Here's an example of what I have now.
example block from migration:
ActiveRecord::Base.connection.execute(update_images)
Models::Product.where('image IS NOT NULL').select(:id, :image).find_in_batches(batch_size: 500).with_index do |group, batch|
puts "Processing group ##{batch}"
group.each do |prod|
next unless prod[:image].present?
image = Models::ProductImage.new(product_id: prod[:id])
image.remote_image_url = prod[:image]
image.save
end
sleep(7)
end
uploader:
class ImageUploader < CarrierWave::Uploader::Base
storage :fog
def filename
"#{secure_token}.#{file.extension}" if original_filename.present?
end
def store_dir
"#{ENV['RACK_ENV']}/#{sub_folder}"
end
def cache_dir
"#{ENV['RACK_ENV']}/#{sub_folder}"
end
def extension_allowlist
%w(jpg jpeg gif png)
end
def size_range
1.byte..10.megabytes
end
def sub_folder
case model
when Models::UserImage
'user_images'
when Models::ProductImage
'product_images'
else
'misplaced'
end
end
protected
def secure_token
var = :"##{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end

Related

How to fix a "value too long for type character varying(255)" error

I'm trying to save a file so that I can upload it to stripe using CarrierWave, but I'm getting the error:
ERROR: value too long for type character varying(255)
and don't understand why as I followed the CarrierWave usage guide on GitHub.
This is my application:
class SplitterStripeServer < Sinatra::Base
CarrierWave.configure do |config|
config.root = File.dirname(__FILE__) + "/public"
end
post "/" do
img = Image.new
img.file = params[:file] #carrierwave will upload the file automatically
img.save!
redirect to("/")
end
get "/upload" do
erb :upload
end
get "/" do
#image = Image.find(1)
erb :index
end
end
This is the uploader:
class ImagesUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
end
This is the model:
class Image
include DataMapper::Resource
property :id, Serial
mount_uploader :file, ImagesUploader
end
I feel like I'm missing something simple.
You need to decrease a length of a file name. Override filename method and cut a file's basename e.g. to 250 characters.
class ImagesUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def filename
"#{file.basename[0..250]}.#{file.extension}" if original_filename.present?
end
end
check if you have additional installed gems
I had was installed gem validates_lengths_from_database and this gem doesn't understand how to work with carrierwave. I am just turn off it in model validates_lengths_from_database except: %i[file]

Where does CarrierWave stores uploads

Please help me understand how does CarrierWave works.
I'm using minimal Sinatra/DataMapper app with following contents:
class VideoUploader < CarrierWave::Uploader::Base
storage :file
end
class Video
include DataMapper::Resource
property :id, Serial
property :name, String
property :desc, Text
mount_uploader :file, VideoUploader
end
get '/' do
slim :form
end
post '/' do
video = Video.new
video.name = params[:name]
video.desc = params[:desc]
video.file = params[:file]
video.save
redirect '/'
end
As I understood mount_uploader :file, VideoUploader string in Video definition adds .video method to Video instance, and I can store uploads assigning params[:file] to it. When I'm trying to send form from browser, the request successfully creates record in DB table, but I can't find any signs of file existence either in DB and public_directory. What I'm doing wrong?
You probably should define the store_dir method inside the VideoUploader class:
class VideoUploader < CarrierWave::Uploader::Base
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
....
end

CarrierWave: Use of file name in store_dir

I'm saving images to S3 using CarrierWave. Before uploading the images are renamed to a random string. Now for faster lookups I wanted to put the files in directories named after the two first letters of the filenames. But doing so, Rails aborts with stack level too deep.
So I assume calling #{model.image[0, 2] leads to an infinite recursion. Is there a way to access just the file name as a string? Or an even simpler solution?
This is the uploader:
class ImageUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"images/#{model.image[0, 2]}/"
end
def filename
"#{secure_token}.#{file.extension}" if original_filename.present?
end
protected
def secure_token
var = :"##{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(8))
end
end
try the :file_identifier method
class ImageUploader < CarrierWave::Uploader::Base
...
def store_dir
"images/#{model.file_identifier[0, 2]}/#{model.id}"
end
...
end
The easy answer is to add a proxy attribute such as filename in the Image model and access that using read_attribute:
class Image < ApplicationRecord
mount_uploader :image, ImageUploader
def filename
read_attribute(:image)
end
...
end
Then in your ImageUploader#store_dir method make reference to the proxy attribute filename:
def store_dir
"images/#{model.filename}"
end
This worked for me and eliminated the recursive error problem.

carrierwave be_identical_to helper not working in rspec

I am having some issues with an rspec test I am trying to run on Carrierwave uploads. Basically, I am trying to test processing to make sure if images are uploaded and processed. I have created a post-processed example file that should be identical to the post-uploaded-and-processed test file. However, I am getting the following warning:
ImageUploader the greyscale version should remove color from the image and make it greyscale
Failure/Error: #uploader.should be_identical_to(#pregreyed_image)
TypeError:
can't convert ImageUploader into String
# ./spec/uploaders/image_uploader_spec.rb:24:in `block (3 levels) in <top (required)>'
Here is my test file:
image_uploader_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
require 'carrierwave/test/matchers'
describe ImageUploader do
include CarrierWave::Test::Matchers
include ActionDispatch::TestProcess
before do
ImageUploader.enable_processing = true
#uploader_attr = fixture_file_upload('/test_images/testimage.jpg', 'image/jpeg')
#uploader = ImageUploader.new(#uploader_attr)
#uploader.store!
#pregreyed_image = fixture_file_upload('/test_images/testimage_GREY.jpg', 'image/jpeg')
end
after do
#uploader.remove!
ImageUploader.enable_processing = false
end
context 'the greyscale version' do
it "should remove color from the image and make it greyscale" do
#uploader.should be_identical_to(#pregreyed_image)
end
end
end
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Process files as they are uploaded:
process :convert_to_grayscale
def convert_to_grayscale
manipulate! do |img|
img.colorspace = Magick::GRAYColorspace
img.quantize(256, Magick::GRAYColorspace)
img = yield(img) if block_given?
img
end
end
Underneath the covers be_identical_to uses FileUtils.identical? on the two arguments. So your expectation:
#uploader.should be_identical_to(#pregreyed_image)
is actually calling:
FileUtils.identcal?(#uploader, #pregreyed_image)
Since in my test environment I'm using a file storage system, I got around this by passing in the #current_path rather than the uploader itself like this:
#uploader.current_path.should be_identical_to(#pregreyed_image)
I actually ended up needing to compare uploaders directly and implemented == on my uploader:
class MyUploader < CarrierWave::Uploader::Base
...
def ==(other)
return true if !present? && !other.present?
FileUtils.identical?(current_path, other.current_path)
end
end

Using a Carrierwave custom process I'm not able to access the model.id (multi-page PDF)

The idea is, I want to convert a PDF to an image per page. I've tried other methods which are on Stackoverflow but to no avail. Below is my uploader file.
Uploader
class PdfUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
#//TODO - Remove converted images when deleting an entry, maybe? if needed?
storage :file
#//TODO - Add images into the same folder. model.id can't be accessed in a custom process.
def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/tmp"
end
def extension_white_list
%w(pdf)
end
def filename
super.chomp(File.extname(super)) + '.png'
end
version :pages do
process :create_pages
process convert: 'png'
end
process convert: 'png'
version :thumb do
process :cover
process :resize_to_fill => [200, 200, Magick::CenterGravity]
process convert: 'png'
end
def cover
manipulate! do |frame, index|
frame if index.zero?
end
end
def create_pages
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
end
The PDF will get converted to images for each page. But it will not recognize the model.id and store it in that folder. Instead it will store in the directory above that.
I've tried the adding the following to force the making of the folder, but it doesn't seem to know the model.id through a little bit of testing?
Tried to Make Dir
def create_pages
Dir.mkdir(store_dir) unless File.exists?(store_dir)
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
Help would be much appreciated, I've been stuck on this for a while and had to leave it for a couple of days out of frustration.
Thank you in advance.
This is most likely because you are saving your images with frame.write before the model itself is saved, and therefore also before an ID has been assigned.
Have a look at this answer for a potential solution: Carrierwave : error with model.id in the store_path

Resources