Saving files using Carrierwave without forms - ruby-on-rails

I have 2 models - Album and AlbumImage.
Each album has album images associated with them, and they are uploaded via the AlbumImageUploader class using Carrierwave.
Now I want to select an album cover for each album using the existing associated album images. I need to process (crop and resize) this selected image before I use it as the album cover. I have the cropping and the resizing functions down, and i created an AlbumCoverUploader class to save this processed version of the album image to.
The problem is that this time I am not using a form to upload a new image file and instead using an existing album image in the file system, and I'm not sure how to transfer this image from my AlbumImageUploader class to my AlbumCoverUploader class.
Any ideas?

This is really simple.
You have to configure your AlbumCoverUploader the same way as if you would upload it from a form.
Though, to use an image which is associated with an existing record, you must do the following:
album = Album.find(id) # your existing album
album_image = album.album_images.first # the image you want as cover
album.cover = File.open(album_image.image.current_path)
album.save
This will grab the image file and use as an input for the AlbumCoverUploader to create its own copy of the 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.

RoR + Paperclip - Have two objects reference same paperclip image

I'm creating a demo portion to my website. Where each new demo account generates dummy data. The dummy data includes images. It takes way too long and takes up unnecessary space when creating these dummy images though. They are just copies of images that already exist in the database.
I have an Image object with the relationship has_attached_file :pc_image. I want a new image to reference the pc_image in an old image, not create a duplicate. I've been trying something like this (p statements added to show the difference), but it just won't work:
p old_image
=> images/001/original/old_image.jpg
new_image.pc_image = old_image.pc_image
new_image.save
p new_image
=> images/002/original/old_image.jpg
Notice the ID after images... 001 and 002. Paperclip seems to automatically create a new URL using the ID of its parent object instead of using the old URL. It doesn't duplicate the images though, which is what I want. But I'm getting a missing image in the view because the pc_image URL is pointing to an image that was never created instead of pointing to the old_image URL, which is what I want. Is this possible? Is there a good workaround?

How to upload multiple pictures to Volusion through API

This is my Volusion item insert method, which relies on the HTTParty gem for rails. It works for posting items without photos, and posting items with a single photos by using the <PhotoURL_Large> and <PhotoURL_Small> XML tags.
def self.post_volusion_item(hide_product, product_code, product_name, product_description, availability, custom_field1, custom_field2, enable_options_inv_control, free_shipping_item, height, length, width, metatag_description, metatag_title, photo_alt_text, photo_xml, product_category, product_price, product_weight, metatag_keywords)
encrypted_password = ENV['VOLUSION_PASSWORD']
post_url = "https://WEBSITE-HERE/net/WebService.aspx?Login=LOGIN-HERE&EncryptedPassword=#{encrypted_password}&Import=Insert"
body = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Volusion_API><Products><HideProduct>#{hide_product}</HideProduct><ProductCode>#{product_code}</ProductCode><ProductName>#{product_name}</ProductName><ProductDescription>#{product_description}</ProductDescription><Availability>#{availability}</Availability><CustomField1>#{custom_field1}</CustomField1><CustomField2>#{custom_field2}</CustomField2><EnableOptions_InventoryControl>#{enable_options_inv_control}</EnableOptions_InventoryControl><FreeShippingItem>#{free_shipping_item}</FreeShippingItem><Height>#{height}</Height><Length>#{length}</Length><Width>#{width}</Width><METATAG_Description>#{metatag_description}</METATAG_Description><METATAG_Title>#{metatag_title}</METATAG_Title><Photo_AltText>#{photo_alt_text}</Photo_AltText><PhotoURL_Large>#{photo_url_large}</PhotoURL_Large><PhotoURL_Small>#{photo_url_small}</PhotoURL_Small><ProductCategory>#{product_category}</ProductCategory><ProductPrice>#{product_price}</ProductPrice><ProductWeight>#{product_weight}</ProductWeight><METATAG_Keywords>#{metatag_keywords}</METATAG_Keywords></Products></Volusion_API>"
post(post_url, body: body)
end
I can not find any documentation that gives an example with multiple photos in the item. I have tried duplicating the calls. Example: <PhotoURL_Large>img1</PhotoURL_Large><PhotoURL_Small>img1</PhotoURL_Small><PhotoURL_Large>img2</PhotoURL_Large><PhotoURL_Small>img2</PhotoURL_Small>
This resulted in no images uploading.
I am realizing the the PhotoURL is not related to the urls of the item's photos, but only tied to the data in Volusion's add item form.
Can someone point me in the right direction for accessing the true photo urls for volusion items?
In Volusion, PhotoURL_Small and PhotoURL_Large are alternate image URLs that can either point to an internal or external location. Volusion only provides one of each for each product code which will be used by the product in place of any loaded image. So if you populate any of the above two fields for that product it will use that URL in place of any image uploaded via the image manager or any images directly uploaded via FTP. There is no provision in the software for additional PhotoURL_Small and PhotoURL_Large images.
Here is an excellent explanation of image file structure.
Image structure

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?

Resources