carrierwave multiple file uploads and storing - ruby-on-rails

I want to do a multiple file upload with carrierwave.
When I upload I transcode a movie in sveral formats .mp4 .mov ...
Now I want to upload all those and store them in DB?
how can I save versions of a file with carrierwave?
thanks

Add the relevant attributes to your model and introduce a before_save callback.
class Video < ActiveRecord::Base
mount_uploader :video, VideoUploader
before_save :update_video_attributes
private
def update_video_attributes
if video.present? && video_changed?
self.content_type = video.file.content_type
self.file_size = video.file.size
end
end
end
For more details see github

Related

Carrierwave object.url VS object.image_url

In my Rails 5 app I am using Carrierwave to upload images.
I have to model that uses the same uploader:
account.rb:
mount_uploader :logo, ImageUploader
image.rb:
mount_uploader :image, ImageUploader
This uploads the file to:
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
The strange this now is that I can use:
#account.logo&.url(:thumb) // works!
#account.logo&.image_url(:thumb) // error!
But on the image model (one product has many images):
#product.images.first&.image_url(:thumb) // works!
#product.images.first&.url(:thumb) // error!
So in the first case I have to use .url and in the second one .image_url
An I have no idea why...any help?
The instance method image_url is defined dynamically based on the column that is passed to mount_uploader and simply calls url on the column. The definition looks like this...
def #{column}_url(*args)
#{column}.url(*args)
end
So, I would suspect that logo_url would work on #account (although I have not tested this)
source

Report from backend to upload to s3 using carrierwave gem ruby

I have Axlsx::Package.new object which is used to create a excel file in backend and now i have to upload this excel file to s3 using carrierwave gem. I have either file path or Axlsx::Package.new object but in both case it is throwing file can't be blank.. I am not sure what I am doing wrong.
Please help me how can I achieve this.
class Attachment < ApplicationRecord
# CarrierWave
mount_uploader :file, AttachmentUploader
validates :file, presence: true
end
when I execute this line, where I don't have ActionDispath::FileUpload object as i am creating file in backend not from UI form. due to this I have
file = Axlsx::Package.new
file_path = "uploads/exports/download_request_#{current_datetime}.xlsx"
attachment = Attachment.create!(file: file, entity_type: self.class)
i tried with file also and file_path also but it is not working and throwing file can't be blank...
class Attachment < ApplicationRecord
# CarrierWave
mount_uploader :file, AttachmentUploader
validates :file, presence: true
end
it should save to s3 as how it is working in case when i upload file from UI form using input type file tag.
Please help

Carrierwave remote attachment url not working on creation

I have the following model called ApplicationAttachment in my Ruby on Rails project.
I also have uploaded files on my s3 bucket. When i try to upload the image to my model, i dont get an error but attachment is nil and the remote_attachment_url is not saving the file. Not sure what is the problem.
My code is this
ApplicationAttachment.create!(remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg")
This doesnt return any error but it doesnt save the image.
class ApplicationAttachment < ActiveRecord::Base
mount_uploader :attachment, DeveloperReferralAttachmentUploader
attr_accessible :id, :attachment, :remote_attachment_url, :created_at, :updated_at
end
class DeveloperReferralAttachmentUploader < 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 jpg jpeg gif png)
end
end
How do i ensure that the remote_image_url saves when created via Carrierwave.
Thanks
I think you might want to explicitly save the attachment instead of using create.
aa = ApplicationAttachment.new(
remote_attachment_url: "http://www.jqueryscript.net/images/jQuery-Plugin-For-Fullscreen-Image-Viewer-Chroma-Gallery.jpg"
)
aa.save

Save CarrierWave Upload as Base64

I would like to take an uploaded attachment from CarrierWave and save it as a Base64 string in my database table, as opposed to saving the path of the upload.
I have absolutely no idea where to start the process. I tried to create the following:
........
before_create :process_image
mount_uploader :image_upload, ImageUploader
.......
attr_accessible :name, :image_upload, :authentication_type
attr_reader :image
private
def process_image
if !self.image_upload.path.nil?
self.image = Base64.encode64(file.open(self.image_upload.path).read)
self.image_upload = nil
end
end
However, during the upload I get the following error:
NoMethodError (undefined method `image_upload_will_change!'
I am sure it's something trivial, however, it seems like I am the only one that wants to try this.

File downloaded from url with Carrierwave but "nil" field in my model

I'm integrating a video section in my small CMS.
I create a new video, I put the url in a form input field and then I must save the video url and also a thumbnail. Uusually videos are from Vimeo and Youtube.
I'm using CarrierWave to download thumb from url.
My Video model is this (simplified)
class Video < ActiveRecord::Base
mount_uploader :thumb_url, VideoThumbUploader
before_save :save_thumb_url
private
def save_thumb_url
self.remote_thumb_url_url = extract_thumb
end
def extract_thumb
thumb_url = if is_youtube_video?
youtube_thumb_url
elsif is_vimeo_video?
vimeo_thumb_url
else
'http://placehold.it/100x100'
end
end
end
In my folder I have the downloaded thumbnail, but the thumb_url column of my db is empty: when I save my form _thumb_url is NIL.
Why?
https://github.com/carrierwaveuploader/carrierwave/issues/1078
It's an issue. Here the workaround!

Resources