FastSolutions
FastSolutions

Reputation: 1829

Dragonfly set original filename

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.

Upvotes: 0

Views: 1017

Answers (3)

Ortwin Gentz
Ortwin Gentz

Reputation: 54101

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

Upvotes: 0

urbanczykd
urbanczykd

Reputation: 319

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"

Upvotes: 1

Abu Nashir
Abu Nashir

Reputation: 11

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

Upvotes: 0

Related Questions