Synchronous file rename for versions

177 views
Skip to first unread message

mg

unread,
Dec 15, 2010, 2:33:51 PM12/15/10
to carrierwave
I'm trying to rename an uploaded image file that generates several
thumbnail versions. I want to either generate a random filename or a
filename that references a counter. First, I'm working on the random
file name.

In my uploader I've tried several versions with filename

# Override the filename of the uploaded files:
# def filename
# "something.jpg" if original_filename
1. basename = (0...8).map{65.+(rand(25)).chr}.join
@filename = basename+model.image.file.basename -- a random
string for each generated version.
2. @filename = "test"+model.image.file.basename
+"."+model.image.file.extension -- essentially does nothing but
prepend "test"
3. basename = "waht"+original_filename if original_filename --
works
4. basename = (0...8).map{65.+(rand(25)).chr}.join if
original_filename -- will create a random name for each version, e.g.
the original, the thumb, and the filename in the db, useless
# end

The issue is mostly that I have 3 thumb types, and working with the
filename function, I get 5 random strings in the folder, the original,
the 3 thumb types, and then the filename itself is a random string,
and this is of no use. I added a hook to the datamapper model I'm
using to create a random file name to use, but I still get 5 random
version names.

My skill level is such that I'm not able to pin down a filename to
each generated version. I'm sending this to the list for carrierwave
to get some pointers to what I need to learn to do this. I'm basically
beginner level.

mg

unread,
Dec 16, 2010, 12:02:03 PM12/16/10
to carrierwave
After poking around to see how others have done similar renaming

I created a hook in the model that generates a random string, then
this:

def filename
@filename = "#{model.randomstring}.#{model.image.file.extension}"
end

I get several versions all named to the same random string BUUUT

#<CarrierWave::SanitizedFile:0x00000001ed7078 @file="..public/uploads/
post/image/13/.jpg", @original_filename=nil, @content_type=nil>

and the the view will not show the thumb, or access the image because
the file name isn't recorded correctly.


More poking about with this...any suggestions for fine-tuning what I
need to know or any suggestions for doing the renaming in the uploader
appreciated.

mg

unread,
Dec 16, 2010, 4:10:56 PM12/16/10
to carrierwave
After going through relevant searches for this list, I can see replies
are going to be sparse.

With this:

def filename
@filename = "#{model.randomstring}.#{model.image.file.extension}"
end

where randomstring is a datamapper hook generated field in the model

I get

#<CarrierWave::SanitizedFile:0x00000001ed7078 @file="..public/uploads/
post/image/13/.png", @original_filename=nil, @content_type=nil>

BUT:

the files are uploaded with the changed name

12345.png
athumb_12345.png
bthumb_12345.png

but model.image.file is all wrong. What am I missing?

mg

unread,
Dec 17, 2010, 1:51:31 PM12/17/10
to carrierwave
Compare:

1.
def filename
"something.#{model.image.file.extension}"
end
#<CarrierWave::SanitizedFile:0x0000000337bc68 @file="../public/uploads/
post/image/10/something.jpg", @original_filename=nil,
@content_type=nil>

NO PROBLEM renaming all thumbs and accessing in views, though
@content_type turns up nil still

2.
def filename
"#{model.randomstring}.#{model.image.file.extension}"
end

#<CarrierWave::SanitizedFile:0x000000032c4ea0 @file="../public/uploads/
post/image/11/.jpg", @original_filename=nil, @content_type=nil>

Problem. Is this a bug or me learning? I've tried various ways of
escaping and formatting model.randomstring and in the above but
santized file scrubs the basename leaving "(dot)extension"

model.randomstring is simply this
'Digest::SHA1.hexdigest([Time.zone.now.utc, rand].join).to_s' in the
model which should be benign and accessible.

mg

unread,
Dec 18, 2010, 6:18:42 PM12/18/10
to carrierwave
Both of these methods work:

def filename
"#{model.randomstring}.#{model.image.file.extension}"
end

def filename
binter = (model.randomstring ||= "doh")
cinter = model.image.file.extension
dinter = binter+"."+cinter
"#{dinter}"
end

AFTER I rearranged the datamapper hook to create random string on
create instead of before save. Before saving the randomstring wasn't
accessible to the model.

Simple enough now that I've sat and determined what exactly goes on
where. I still think I should be able to set a random string for the
file name somewhere on the uploader but is beyond me at the moment.
Perhaps my notes will be of some use.
Reply all
Reply to author
Forward
0 new messages