Dragonfly set original filename - ruby-on-rails

I'm using Dropbox with Dragonfly to store my file uploads.
I have the following code:
if hash[:file_dropbox].present? #DROPBOX
path = "Attachments/" + Time.now.strftime("%d%m%Y_%H%M") + "_" + rand(1000).to_s + "_" + File.basename(hash[:file_dropbox])
data = open(hash[:file_dropbox]) { |f| f.read }
file["file"] = Dragonfly[:images].store(data, path: path)
end
Problem:
Dragonfly uses the filename I use for path as name for storing. Is it possible to set a different separate filename when using Dragonfly.store?
With normal files Dragonfly uses original_filename, but I cannot seem to set this via Dragonfly.store.
Any suggestions?
EDIT1:
I've tried the following:
file["file"] = Dragonfly[:images].store(data, path: path,
original_filename: "hello.docx")
file["original_filename"] = File.basename(hash[:file_dropbox])
When using normal file upload, the original_filename can be set. But I can't set data.original_filename because it's just binary data.

Not sure what is your setup, but I have model Photo and dragonfly attachment is set as image. In my case, it works like that:
photo.image.meta["name"] = "new_file_name.jpg"

I never used Dragonfly but I was going through their source code and i think passing a name option might work. Please have a look at Github repo
def name
meta["name"] || temp_object.original_filename
end

In your Model class using DragonFly, add an accessor for your image:
dragonfly_accessor :image do
storage_options{|attachment| {path: "<somehow generate your path>"}
end

Related

How to save a copy of image after using variant option in ActiveStorage.

I am facing this issue with ActiveStorage where I need to process an image, my requirement is to save the processed image and attach it to a new model after crop and other transformations.
ActiveStorage::Blob#variant accommodates a different use case, so deal with ActiveStorage::Variation directly. The following assumes latest Rails master rather than Rails 5.2:
variation = ActiveStorage::Variation.new(resize_to_fit: [100, 100], crop: true)
message.header_image.open do |input|
variation.transform(input, format: "png") do |output|
message.cropped_header_image.attach \
io: output,
filename: "#{message.header_image.filename.base}.png",
content_type: "image/png"
end
end
#George has a great answer, still I would mention mine,which should work with rails 5.2 as per my understanding of your question,
Create a temporary file, and fetch the file first, if it is in your cloud storage, if not then you don't need this part, just get the path using blob in that case.
path = Rails.root.join('tmp', ModelVariable.main_image.blob.filename.to_s).to_s
File.open(path, 'wb') do |file|
file.write(ModelVariable.main_image.blob.download)
end
Do your customization
customize_image = MiniMagick::Image.open(path)
customize_image.crop(crop_params)
Attach it to the different model you wanted to
file = File.open(customize_image.path)
filename = Time.zone.now.strftime("%Y%m%d%H%M%S") + ModelVariable.main_image.blob.filename.to_s
NewModelVaribale.customized_image.attach(io: file, filename: filename)
Save it
customized_product.save
Hope this works for you :)
I would like to add this to Faizaan's answer
If you are storing locally use
path = ActiveStorage::Blob.service.send(:path_for, ModelVariable.main_image.blob.key)

How to specify a prefix when uploading to S3 using activestorage's direct upload?

With a standard S3 configuration:
AWS_ACCESS_KEY_ID: [AWS ID]
AWS_BUCKET: [bucket name]
AWS_REGION: [region]
AWS_SECRET_ACCESS_KEY: [secret]
I can upload a file to S3 (using direct upload) with this Rails 5.2 code (only relevant code shown):
form.file_field :my_asset, direct_upload: true
This will effectively put my asset in the root of my S3 bucket, upon submitting the form.
How can I specify a prefix (e.g. "development/", so that I can mimic a folder on S3)?
2022 update: as of Rails 6.1 (check this commit), this is actually supported:
user.avatar.attach(key: "avatars/#{user.id}.jpg", io: io, content_type: "image/jpeg", filename: "avatar.jpg")
My current workaround (at least until ActiveStorage introduces the option to pass a path for the has_one_attached and has_many_attached macros) on S3 is to implement the move_to method.
So I'm letting ActiveStorage save the image to S3 as it normally does right now (at the top of the bucket), then moving the file into a folder structure.
The move_to method basically copies the file into the folder structure you pass then deletes the file that was put at the root of the bucket. This way your file ends up where you want it.
So for instance if we were storing driver details: name and drivers_license, save them as you're already doing it so that it's at the top of the bucket.
Then implement the following (I put mine in a helper):
module DriversHelper
def restructure_attachment(driver_object, new_structure)
old_key = driver_object.image.key
begin
# Passing S3 Configs
config = YAML.load_file(Rails.root.join('config', 'storage.yml'))
s3 = Aws::S3::Resource.new(region: config['amazon']['region'],
credentials: Aws::Credentials.new(config['amazon']['access_key_id'], config['amazon']['secret_access_key']))
# Fetching the licence's Aws::S3::Object
old_obj = s3.bucket(config['amazon']['bucket']).object(old_key)
# Moving the license into the new folder structure
old_obj.move_to(bucket: config['amazon']['bucket'], key: "#{new_structure}")
update_blob_key(driver_object, new_structure)
rescue => ex
driver_helper_logger.error("Error restructuring license belonging to driver with id #{driver_object.id}: #{ex.full_message}")
end
end
private
# The new structure becomes the new ActiveStorage Blob key
def update_blob_key(driver_object, new_key)
blob = driver_object.image_attachment.blob
begin
blob.key = new_key
blob.save!
rescue => ex
driver_helper_logger.error("Error reassigning the new key to the blob object of the driver with id #{driver_object.id}: #{ex.full_message}")
end
end
def driver_helper_logger
#driver_helper_logger ||= Logger.new("#{Rails.root}/log/driver_helper.log")
end
end
It's important to update the blob key so that references to the key don't return errors.
If the key is not updated any function attempting to reference the image will look for it in it's former location (at the top of the bucket) rather than in it's new location.
I'm calling this function from my controller as soon as the file is saved (that is, in the create action) so that it looks seamless even though it isn't.
While this may not be the best way, it works for now.
FYI: Based on the example you gave, the new_structure variable would be new_structure = "development/#{driver_object.image.key}".
I hope this helps! :)
Thank you, Sonia, for your answer.
I tried your solution and it works great, but I encountered problems with overwriting attachments. I often got IntegrityError while doing it. I think, that this and checksum handling may be the reason why the Rails core team don't want to add passing pathname feature. It would require changing the entire logic of the upload method.
ActiveStorage::Attached#create_from_blob method, could also accepts an ActiveStorage::Blob object. So I tried a different approach:
Create a Blob manually with a key that represents desired file structure and uploaded attachment.
Attach created Blob with the ActiveStorage method.
In my usage, the solution was something like that:
def attach file # method for attaching in the model
blob_key = destination_pathname(file)
blob = ActiveStorage::Blob.find_by(key: blob_key.to_s)
unless blob
blob = ActiveStorage::Blob.new.tap do |blob|
blob.filename = blob_key.basename.to_s
blob.key = blob_key
blob.upload file
blob.save!
end
end
# Attach method from ActiveStorage
self.file.attach blob
end
Thanks to passing a full pathname to Blob's key I received desired file structure on a server.
Sorry, that’s not currently possible. I’d suggest creating a bucket for Active Storage to use exclusively.
The above solution will still give IntegrityError, need to use File.open(file). Thank Though for idea.
class History < ApplicationRecord
has_one_attached :gs_history_file
def attach(file) # method for attaching in the model
blob_key = destination_pathname(file)
blob = ActiveStorage::Blob.find_by(key: blob_key.to_s)
unless blob
blob = ActiveStorage::Blob.new.tap do |blob|
blob.filename = blob_key.to_s
blob.key = blob_key
#blob.byte_size = 123123
#blob.checksum = Time.new.strftime("%Y%m%d-") + Faker::Alphanumeric.alpha(6)
blob.upload File.open(file)
blob.save!
end
end
# Attach method from ActiveStorage
self.gs_history_file.attach blob
end
def destination_pathname(file)
"testing/filename-#{Time.now}.xlsx"
end
end

Carrierwave no such file or directory

I am trying to write a method that uses the EXIFR gem to retrieve data (in this case camera model) about photos as they are uploaded using Carrierwave. The current method I am using looks like this in my model:
before_save :get_exif_data
def get_exif_data
imgfile = EXIFR::JPEG.new(photo.filename)
return
self.model = imgfile.model
end
However, I get an error "No such file or directory - IMG_0953.JPG" (or another filename).
My goal is to extract the "type of camera" data using the EXIFR gem's .model method before saving. From the EXIFR gem documentation:
EXIFR::JPEG.new('IMG_6841.JPG').model # => "Canon PowerShot G3"
The error suggests to me that my photo.filename has not yet been created. How might I process the image before it is saved?
Actually, something like the following ended up working for me:
before_save :get_camera
private
def get_camera
self.model = EXIFR::JPEG.new(photo.file.path).model
end
end
Try
EXIFR::JPEG.new(Rails.root.to_s + photo.filename_url)
carrierwave return an url look like /uploads/...
its a relative path to webapp, but EXIFR::JPEG will think it a path of system file

Using Tempfile to create a zip file in rails

I want to create a temporary .zip in rails. For creating zip file I am using rubyzip gem.
Currently I am doing this:
zfname = Tempfile.new(['somename','.zip'], Rails.root.to_s + '/tmp/')
Zip::ZipFile.open(zfname.path, Zip::ZipFile::CREATE) do |zipfile|
zipfile.add(file, basepath + file)
end
This generates following error:
Zip::ZipError: Zip end of central directory signature not found
Is it possible to use Tempfile for zip? If yes, what is wrong here?
In my rails app when I needed to send zip files to user, I just stored them in a buffer and used 'send data' method in controller.
I was trying to use 'Tempfile' initially but it had an added task of removing zip files after sending it to user which is a pain.
Is this something you are looking for?
Zip::OutputStream.write_buffer do |stream|
file_paths.each_with_index do |file_path, index|
# rename the pdf
stream.put_next_entry("#{name}-#{index + 1}.pdf")
# add pdf to zip
stream.write IO.read(file_path)
end
end
So it doesn't actually look like you need or want a Tempfile. You really want a random path in the Rails.root/tmp directory. Try something like this:
zfpath = Rails.root.join('tmp', "somename-#{SecureRandom.hex(8)}.zip"
Zip::ZipFile.open(zfpath, Zip::ZipFile::CREATE) do |zipfile|
zipfile.add(file, basepath + file)
end
Update:
While it's far more complex, you can find a discussion of how to do this with a Tempfile here - http://thinkingeek.com/2013/11/15/create-temporary-zip-file-send-response-rails/ .

Carrierwave & Zipfiles: Using an extracted file as a version

Something I'm not getting about the version process...
I have a zip file with a file inside, and I want to upload the file as a "version" of the zip:
Uploader:
version :specificFile do
process :extract_file
end
def extract_file
file = nil
Zip::ZipFile.open(current_path) do |zip_file|
file = zip_file.select{|f| f.name.match(/specificFile/)}.first
zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
end
File.open("tmp/" + file.name.gsub("/", "-"))
end
Usage:
=link_to "Specific File", instance.uploader.specificFile.url
Only this just nets me two copies of the zip. Clearly, there's something I'm missing about how version / process works, and I haven't been able to find documentation that actually explains the magic.
So how do I do this, and what am I missing?
This provided the "why", although it took a bit to understand:
How do you create a new file in a CarrierWave process?
To rephrase, when you go to create a version, carrierwave makes a copy of the file and then passes the process the file path. When the process exits, carrierwave will upload the contents of that path - not the file the process returns, which is what I thought was going on.
Working code:
version :specificFile do
process :extract_file
def full_filename (for_file = model.logo.file)
"SpecificFile.ext"
end
end
def extract_plist
file = nil
Zip::ZipFile.open(current_path) do |zip_file|
file = zip_file.select{|f| f.name.match(/specificFile/)}.first
zip_file.extract(file, "tmp/" + file.name.gsub("/", "-")){ true }
end
File.delete(current_path)
FileUtils.cp("tmp/" + file.name.gsub("/", "-"), current_path)
end
So, to make what I want to happen, happen, I:
Tell carrierwave to use a particular filename. I'm using a hardcoded value but you should be able to use whatever you want.
Overwrite the contents of current_path with the contents you want under the version name. In my case, I can't just overwrite the zip while I'm "in it" (I think), so I make a copy of the file I care about and overwrite the zip via File and FileUtils.
PS - It would be nice to avoid the duplication of the zip, but it doesn't look like you can tell carrierwave to skip the duplication.

Resources