I am using carrierwave to upload images. In uploaders/image_uploader.rb, I have
class ImageUploader < CarrierWave::Uploader::Base
def store_dir
'/public/uploads/images'
end
end
I would like to move the uploads directory to:
/shared/uploads/images
I obviously need to modify uploaders/image_uploader.rb to reflect the new path. In addition, I need to move the already uploaded images from:
/public/uploads/images
to:
/shared/uploads/images
My assumption is that these are the only changes I need to make, as in I don't have to make any changes to the DB or anything else. Is this correct?
Nothing to change the side of the DB.
To be safe, you just have to see how its stored your images.
;)
Related
I use Shrine in a Ruby on Rails application to create the process of resizing and uploading images to storage.
My current code is:
image_uploader.rb
require "image_processing/mini_magick"
class ImageUploader < Shrine
plugin :derivatives
Attacher.derivatives_processor do |original|
magick = ImageProcessing::MiniMagick.source(original)
{
resized: magick.resize_to_limit!(120, 120)
}
end
end
user.rb
class User < ApplicationRecord
include ImageUploader::Attachment(:image)
before_save :image_resize
def image_resize
self.image_derivatives!
end
end
I implemented it while reading the official documentation, but this is not desirable in two ways.
Requires trigger in model code. Can it be completed with only image_uploader.rb?
Access to images generated with this code requires a "resized" prefix(e.g. #user.image(:resized).url), and the original image will also remain in storage. I want to process the original image itself.
Is there a way to upload while solving these two issues?
You can add the following patch, which will trigger derivatives creation as part of promoting cached file to permanent storage:
# put this in your initializer
class Shrine::Attacher
def promote(*)
create_derivatives
super
end
end
You can just override the model method that retrieves the attached file to return the resized version. You can use the included plugin to do this for all models using this uploader:
class ImageUploader < Shrine
# ...
plugin :included do |name|
define_method(name) { super(:resized) }
end
end
As for the second question: It will still keep the original file in the storage, but just return the resized version instead by default. It's generally better to do this in a view decorator instead.
You always want to keep the original file in the storage, because you never know when you'll need to reprocess it. It can happen that you find your current resizing logic not to be ideal for certain filetypes and sizes, in which case you'll need to regenerate the resized version for previous attachments. And you wouldn't be able to do that if you didn't have the original file anymore.
The files uploaded succesfully using fog to s3 on carrierwave seem to be disappearing automatically.How do I prevent this from happening. The fog public setting is set to true.
For some reason, Carrierwave deletes files not only when the model is deleted, but also when it's updated, even if you don't touch the mounted uploader field. There is supposed to be a config setting remove_previously_stored_files_after_update that when set false, prevents this from happening, but I haven't had any luck setting it false – it still deletes my files on model update.
Inside your uploader class you can modify the #remove! method.
class FileUploader < CarrierWave::Uploader::Base
#...
def remove!
# do some stuff to confirm that you want the file removed,
# otherwise return. You have access to model record as 'model'
super
end
end
I am using CarrierWave with fog to upload my images to S3.
I have model Image that can represent images of different sizes and according to that needs to be saved in different folder.
For example, for image.jpg I could have two different uploaded versions that need to be saved as:
'images/large/image.jpg'
'images/small/image.jpg'
There could be arbitrary number of use cases and versions using minimagick can't cover them all.
So far I haven't been able to find solution. Does anyone know how to do this?
I've seen this question asked a few times so I'll write what my final solution is.
Instead of defining mount_uploader on model I decided to just use Uploader independently and save urls to records later.
Dynamically changing store_dir and filename can be accomplished like this
uploader = Uploader.new
uploader.define_singleton_method(:store_dir) do
'new_store_dir'
end
uploader.define_singleton_method(:filename) do
'new_filename'
end
uploader.store!(image)
Using this approach you can also define names with local variables or whatever you have available in controller.
Hopefully it helps someone else as well.
in order to change where uploaded files are put, just override the store_dir method:, for your case (reference link)
class Image < CarrierWave::Uploader::Base
storage :file
# this if you using use condition for folder location
def store_dir
if model.somefield == "somecondition
"uploads/#{model.somefield}"
elsif model.somefield == "somecondition
"uploads/location2"
else
"uploads/default_dir"
end
end
# this is if you want to use version_name
def store_dir
'images/#{version_name}/'
end
end
I am using carrierwave to handle my uploads. I have specified the store_dir following way:
def store_dir
"uploads/#{Time.now.year}/#{Time.now.month}/#{Time.now.day}"
end
Uploading files work like a charm - each time I upload a file it ends up in directory where it should end; i.e. "today's directory".
When I try to download the file, carrierwave is constructing the download path dynamically based on store_dir options. So lets say a file which was uploaded on 1.12.2012 is available on the following path on fliesystem:
/uploads/2012/12/01/file.ext
will be retrieved by carrierwave as:
/uploads/2012/12/12/file.ext
Which obviously leads to "Cannot read file" error.
I came with 2 different possible solutions:
Create a separate filed where I will be storing the actual filepath to the file upon it's creation and then will use this value to retrieve file.
Overload retrieve_from_store! method (which is part of carrierwave gem) and make it construct path based on created_at field from the file record than rather from store_dir.
I am inclining to the second possibility since it feels not that dirty. Yet both feel "not-rails-way". Which one will be better to use and why? Or maybe carrierwave provides a way to solve this issue?
Totally guessing here but by looking at the docs I think something like this should work:
def store_dir
"uploads/#{model.created_at.year}/#{model.created_at.month}/#{model.created_at.day}"
end
I'm using carrierwave in a rails 3 application to upload and store a file from a remote source in my server's file system. I've got a setup that's totally standard, with an uploader mounted on the model that the image is associated with.
It works perfectly 99.9% of the time, but every 600th graph or so I run into an issue where the app persistently fails to serve the stored image. If I check on the filesystem, the graph image has been uploaded and stored in the correct location, with the correct file permissions and everything, but rails is totally unaware of it and continues to serve the default graph image for that instance.
In other words a graph with id 123 has it's image stored at /uploads/graphs/123/graph.png ... the correct image is there but as far as rails is concerned it has no image stored. All the other graph images still work fine, but I can't get rails aware of the image stored for 123. Removing and re-storing the image doesn't work. Manually removing the image and re-uploading doesn't work. I'm totally lost. The graph instance is valid, no errors in the logs when I save.
for example, in the console:
g = Goal.find_by_id("123")
g.remote_graph_url = "http://image.source/url.png"
> "http://image.source/url.png"
g.save
> true
g.graph?
> false
g.graph_url >> /default/image.png
here's the relevant code:
class GraphUploader < CarrierWave::Uploader::Base
def store_dir
"../uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
"/images/" + [version_name, "default_large_graph.png"].compact.join('_')
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"graph.png" if original_filename
end
end
class Graph
mount_uploader :graph, GraphUploader
end
Wish I could just reply...
So you are saving uploads outside of the public directory? Try removing the "../" before images, if that wasn't your intention.