Rails Shrine Image uploader, can't remove attachment - ruby-on-rails

I've tried adding the remove_attachment plugin, but nothing works. The attachment is still attached to the User. I've also tried just sending "null" and empty objects as the image, this does not work either.
class ImageUploader < Shrine
plugin :remove_attachment
Attacher.validate do
validate_mime_type %w[image/jpeg image/png image/webp]
validate_max_size 1*1000*1000
end
end

Related

How to implement attachment versioning for files with same names? RoR 5.2

Project based on RoR 5.2. For attachments uploading I use Carrierwave gem and AWS S3 bucket as storage.
I need to implement versioning for uploaded files with same names.
I mean that file with the same name should not be replaced when I will upload a new one.
For example: model User has a mounted uploader for documents. If I will upload file with name «doc.docx» and after I will upload one else file for the same model object and with the same name, old file will be replaced with new one.
I have already investigated that I can generate token or add timestamp to get unique filename, but I need that if I will upload the new file with same name (for example 'doc.docx') for the same object, it shoild be renamed to 'doc(1).docx' or 'doc_1.docx'. And the new one will be renamed to 'doc(2).docx' or 'doc_2.docx', etc.
Versioning in the AWS panel for this bucket has already enabled.
Is there any way to achieve this? I'm ready to change from Carrierwave to Active Storage. But I have not found at least one case for this.
Any suggestions?
Thanks in advance!
I have some ideas about renaming. At first, we can split your problem:
Problem with file renaming
Problem with file versioning
I solved my problem with renaming with similar code (Carrierwave + video processing). Here I created thumbnail version for an uploaded video, with a specific name (write your custom function instead of png_name, you can check a number of previously uploaded files and etc):
# Create different versions of your uploaded files:
# ffmpegthumbnailer
version :thumb do
process thumbnail: [{ format: 'png', size: 360, logger: Rails.logger }]
def full_filename for_file
png_name for_file, version_name
end
end
version :preview do
process thumbnail: [{ format: 'png', size: 600, logger: Rails.logger }]
def full_filename for_file
png_name for_file, version_name
end
end
def png_name(for_file, version_name)
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.png}
end
And you can solve the problem with versioning by creating proxy-model. For instance, you have User model, than you create an association called Document. You can mount your uploader to the Document model, and use code above for the renaming.
class User < ApplicationRecord
has_many :documents, dependent: :destroy
def last_document
documents.last
end
end
class Document < ApplicationRecord
mount_uploader :file, DocumentUploader
validates_integrity_of :file
end

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

Retrieve file from S3. - Carrierwave Rails Gem

I'm implementing Carrierwave with fog storage into my Rails App. The whole purpose of this app is to store pdf articles and have the ability for anyone with access to the app to retrieve them. Right now, I have the functionality of storing the article pdf working great. But, now I need to be able to retrieve the pdf from S3. Is this possible? I noticed in the docs there is a uploader.retrieve_from_store!("my_file.png") method. I tried to run this in the console and I got this error NoMethodError: undefined method retrieve_from_store! for ArticleUploader:Class Any help with this would be great! I'm just not finding any suitable answers so far. Thanks!
Article Uploader
class ArticleUploader < CarrierWave::Uploader::Base
storage :fog
def extension_whitelist
%w(jpg jpeg gif png pdf)
end
end
Article Model
class Article < ApplicationRecord
mount_uploader :file, ArticleUploader
validates :title, presence: :true
validates :publication_date, presence: :true
validates :source, presence: :true
end
You can access it with the methods that carrierwave has, in your case:
article = Article.find(1)
article.file.url
If you are on development it will output the path for the file and if you are on production and using S3, it will output the whole url, http://s3.amazonaws.com/<vendor>/articles/1/file.pdf for example.
You can find more information on the official docs:
https://github.com/carrierwaveuploader/carrierwave#activerecord
There is also an old rails cast on that http://railscasts.com/episodes/253-carrierwave-file-uploads

Resources