How to use version numbers with CarrierWave? - ruby-on-rails

Suppose you want to have an ImageUploader, and when an image is uploaded, it is saved using the filename "/image_1.png". Each time a new image is uploaded, the number is incremented: "/image_2.png".
What is a good way to do this?
Do you add a field in the ImageUploader to store the version_number? Or does this field need to be added to the Image model?
What is a good way to increment when a new file is uploaded?

for i=1:10
filename=[ 'image_'num2str( i )'.txt' ]
end
or add '/' before 'image_'

Related

CarrierWave how to change object path after downloaded

I've to duplicate a Model which contains a CarrierWave image but I need to use the same image file, the problem is that my file path it's using the model UUID and after saving new object the UUID of the image is updated too.
How can I change the image file path after saving the object to keep the original image path?
This is my image stored at aws:
#mounted_as=:my_cover, #storage=#<CarrierWave::Storage::Fog:0x0000000002ef #uploader=#<CoverUploader::Uploader82999640:0x0000000002ef ...>>, #file=#<CarrierWave::Storage::Fog::File:0x0000000002ef #uploader=#<CoverUploader::Uploader82999640:0x0000000002ef
I can access Model.my_cover.file.path or Model.my_cover.cover.path but I can't change the path value.
I don't want to duplicate the image because this will double the file storage space.
Thanks for any help!
When duplicating the object, you should be able to copy along the image from the previous object before saving the new one.
Let's say your model is called Model and has an image attribute called my_cover.
a = Model.new
b = a.dup
b.my_cover = a.my_cover
b.save
Carrierwave uses an image object that references an image URL and that you can copy to a different object.

Session loses its data with Carrierwave multi files upload using jquery file upload

Simply I've a model called Estate which has many :images (Image model) and
I'm using CarrierWave & jQueryFileUpload to handle these in Estate New/Edit forms.
While creating new estate, and upload an image, jQueryFileUpload is doing that using AJAX request, so I make all uploaded images paths be stored in a session array then I use this array in create or update actions to save the images from tmp directory to actual directory.
This works fine with me, but The problem is when I've selected more than one photo at a time, session array store only last selected image, and not all images be pushed to the session array except the last one.
def images_url_list
#image = Image.new(image_params)
session[:cached_images_paths] << #image.image_file.current_path
end
I've debugged this action and I found that if I selected 5 images at a time, images_url_list action is fired 5 times, so say that I upload an image called "path0" then I uploaded 5 images which paths called ["path1","path2","path3","path4","path5"] and the session already has a path called "path0", first time after uploading first image the session will be ["path0", "path1"], second time ["path0","path2"] and so on until last image which is path5.
So the final count of image paths is only 2 rather than 6 image paths.
Can anyone tell me what exactly the problem is?
I think the problem is the multi-request and the session save.
In my case using a similar environment, only the last uploaded file was saved. I couldn't make it work adding it in a filesUploaded array, so I though about an alternative solution.
A good approach might be:
Generate a session upload hash and store it for each uploaded file on the upload listener
Use it to identify the uploaded files after the multi-upload is finished

How to update model in a paperclip processor?

I've build a processor that determine which image format is best to compress my model attached thumbnail. The processor simply build images in png and jpg and check which one is the smallest.
Since Paperclip use the original thumbnail format to build it's thumbnail style URL I had to create a field in my model to store the format of each of the styled thumbnail.
ex: thumbnail_small_content_type, would be "image/png"
In my processor I tried to save the format by using the Paperclip:attachment method : instance_write.
#attachment.instance_write "#{#style_name}_content_type", "image/#{optimised_format}"
Strangely it work perfectly when a create a new model, but failed to work when I use the paperclip method reprocess! to crop my image. Any idea how I could workaround that limitation?

Rails and Paperclip... don't save the original image, just the styles?

In paperclip when you save an image with lots of style and sizes it also saves the original.
But in my app it's not necessary to save the original, just the style will do, what I was wondering was how do you not save the original. Just store it in memory or in a temporary area and then not save it once the style have been generated.
Ideally it would not save original at all. I guess one solution would be to save the original, process the style and delete afterwards, however I'm trying to save on bandwidth and deleting the original after it has been saved kindof defeats the point.
Cheers!
check this: How do I tell paperclip to not save the original file?
This worked for me
def destroy_original
File.unlink(self.photo.path)
end
Taken from here: http://tekn0t.net/delete-original-image-when-using-papercliprai
Edit: the provided link is no longer valid. Here is a valid one from the same author: https://gist.github.com/tekn0t/755593

using paperclip to get list of style/geometry pairs, even without valid object

I am working on maintaing an old code base and I'm migrating attachment_fu to paperclip. I migrated one thing but now I'm having a small issue.
There's a partial that renders images given the type of image and a thumbnail style. I fixed the part to render the image and that's fine, but the "else" assumes that there actually is no photo or image. I basically just want a completely detached list of the style=> geometry pairs that aren't dependent on a particular object, but I can't seem to do this without doing things like creating a new object and pulling the string from there, and even that didn't work correctly. Is there a way I could pull it straight out of paperclip or straight out of the model? The old method was using something refelect_on_association which I don't even understand... Help please. thanks :)
Paperclip has a notion of "default_url", if you specify this in your model it will attempt to pull the default url if an image isn't assigned to that object yet (your "else" case).
The default_url accepts the :style interpolations, so you can setup your style/geometry pairs in a separate folder.
Step 1
Put your default images in a directory like "/images/users/avatar/missing/".
Example file names:
missing_thumb.png
missing_small.png
Step 2
Add this line to your has_attached_file declaration in your model:
:default_url => "/images/:class/:attachment/missing/missing_:style.png"

Resources