# Attachment\_fu: Saving files with random filenames (using Dig

**URL:** <https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114>\
**Category:** Rails\
**Created:** [June 29, 2007, 4:16am UTC](https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114 "2007-06-29T04:16:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [June 29, 2007, 4:16am UTC](https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114/1 "2007-06-29T04:16:18Z")

</div>

Hi all,

I’m creating an image uploader of sorts, and would like all of the  
filenames of uploaded files to be completely random when saved. I am  
fairly new to Ruby, Rails, and attachment\_fu, and came up with the  
following earliar:

require ‘digest/sha1’

class Photo \< ActiveRecord::Base  
has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:processor =\> ‘ImageScience’,  
:path\_prefix =\> ‘public/photos’,  
:thumbnails =\> { :thumb =\> ‘100x100\>’ }  
validates\_as\_attachment

def filename=(new\_name)  
new\_name =  
“#{Digest::SHA1.hexdigest(Time.now.to\_s)}#{File.extname(new\_name)}”  
write\_attribute :filename, sanitize\_filename(new\_name)  
end  
end

This works, creating filenames such as  
‘4de12252220d679aa99b12be835ab56c7a5f7fc9.png’, but it doesn’t set the  
thumbnail name correctly (i.e:  
4de12252220d679aa99b12be835ab56c7a5f7fc9\_thumb.png) - instead, it  
saves it with the same filename  
(4de12252220d679aa99b12be835ab56c7a5f7fc9.png).

Does anyone have any ideas on how I can achieve this?

Thanks.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [June 29, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114/2 "2007-06-29T18:21:50Z")

</div>

I figured out how to do it. Here’s the code for those who are  
interested. Seems to work perfect now and generates the thumbnail  
names how it should.

[http://pastie.caboo.se/74792](http://pastie.caboo.se/74792)

Rob

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [June 29, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114/3 "2007-06-29T18:43:52Z")

</div>

Hello,

> 4de12252220d679aa99b12be835ab56c7a5f7fc9\_thumb.png) - instead, it  
> saves it with the same filename  
> (4de12252220d679aa99b12be835ab56c7a5f7fc9.png).

The problem is filename= is sometimes called with the filename of the  
full image and sometimes called with the thumbnail’s filename.

The way to solve this is to change the filename right where it enters  
the system. Remove your filename= method and add this instead to  
your model:

def uploaded\_data=(file\_data)  
return nil if file\_data.nil? || file\_data.size == 0  
self.content\_type = file\_data.content\_type  
self.filename = “#{Digest::SHA1.hexdigest(Time.now.to\_s)}#  
{File.extname(file\_data.original\_filename)}” if respond\_to?(:filename)  
if file\_data.is\_a?(StringIO)  
file\_data.rewind  
self.temp\_data = file\_data.read  
else  
self.temp\_path = file\_data.path  
end  
end

This is the same as the original implementation but for the third  
line in the method.

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![rob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.ruby-forum.com/u/rob)\
**Post date:** [June 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/attachment-fu-saving-files-with-random-filenames-using-dig/108114/4 "2007-06-29T18:45:53Z")

</div>

> I figured out how to do it. Here’s the code for those who are  
> interested. Seems to work perfect now and generates the thumbnail  
> names how it should.
> 
> [Parked at Loopia](http://pastie.caboo.se/74792)

Aha! That’s the same solution I came up with – but cleaner. Nice one.

Regards,  
Andy S.
