Generating a random filename for the original, but not for every version.

Showing 1-16 of 16 messages
Generating a random filename for the original, but not for every version. karb 2/25/11 8:19 AM
I am using this example from the wiki:

  def filename
    @name ||= ActiveSupport::SecureRandom.hex
    "#{@name}#{File.extname(original_filename).downcase}" if
original_filename
  end

To generate a random filename, however it generates a new random hex
for every version.  How would I go about creating one random_filename
in the uploader class and using that one for every version (with the
version prefix of course in the filename)?

Thanks in advance!
Re: Generating a random filename for the original, but not for every version. Trevor Turk 2/25/11 2:31 PM
On Friday, February 25, 2011 4:19:00 PM UTC, karb wrote:
How would I go about creating one random_filename
in the uploader class and using that one for every version (with the
version prefix of course in the filename)?

I thought about this a bit, and I wonder if a workaround might be to make a hash of the filename that's not exactly random. Something like:

  def filename 
    @name ||= Digest::MD5.hexdigest "#{original_filename}#{model.class}#{mounted_as}#{model.id}"
  end 

...of course that code wouldn't work, but maybe you see what I'm getting at. 
Re: Generating a random filename for the original, but not for every version. karb 2/26/11 7:15 AM
On Feb 25, 11:31 pm, Trevor Turk <trevort...@gmail.com> wrote:

> make a hash of the filename that's not exactly random. Something like:
>
>   def filename
>     @name ||= Digest::MD5.hexdigest
> "#{original_filename}#{model.class}#{mounted_as}#{model.id}"
>   end

I see what you're getting at but that doesn't solve the problem why
I'm using a random filename in the first place.
Consider this:
- User uploads picture.jpg
- Store it as md5 hash
- User uploads updated picture.jpg (same name)
- Gives the same md5 hash, hence, same filename for another file.

Re: [CarrierWave] Re: Generating a random filename for the original, but not for every version. Micke 2/26/11 12:27 PM

You can use the updated_at from the model. But make to a timestamp by using updated_at.to_i

Thats what i use for my photos

> --
> You received this message because you are subscribed to the Google Groups "carrierwave" group.
> To post to this group, send email to carri...@googlegroups.com.
> To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.
>
Re: Generating a random filename for the original, but not for every version. karb 2/28/11 8:50 AM
On Feb 26, 9:27 pm, Micke Lisinge <mi...@lisinge.com> wrote:
> You can use the updated_at from the model. But make to a timestamp by using
> updated_at.to_i

That works like a charm! Thanks.
Side-note: This could be problematic when used in a multi-user
(thousands) environment, but when stored in folders on a per-user
basis this isn't a problem.

Re: Generating a random filename for the original, but not for every version. karb 3/1/11 4:13 AM
Correction, it doesn't work upon creation of the model, because there
isn't an updated_at attribute available then.
The problem is, that when you ask for the url after upload, updated_at
-is- available and it returns an url that won't be valid.

How do you handle this on creation of new records?
Re: Generating a random filename for the original, but not for every version. karb 3/1/11 4:28 AM
It has to do with something user 'mg' discovered in this thread:
http://groups.google.com/group/carrierwave/browse_thread/thread/7216c2aca0266794/47b5dab5d3238a0d

"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. "

Obviously, that's not at all what we have to go through to accomplish
such a simple task as having a consistent unique filename across
versions?
Is there a reason for the filename function getting called 2 times for
every version? first when it didn't create the record yet, and
afterwards when it did and uploads it off to s3.
So you wind up with a 0.jpg in the database and a correct string on
s3, but you can't refer to it.
Re: Generating a random filename for the original, but not for every version. Trevor Turk 3/1/11 4:49 AM
On Tuesday, March 1, 2011 12:28:35 PM UTC, karb wrote:
Obviously, that's not at all what we have to go through to accomplish
such a simple task as having a consistent unique filename across
versions?

Perhaps you could introduce a new attribute called "unique_hash" or something, calculate that once, and then use that in your filename definition. 
 
Is there a reason for the filename function getting called 2 times for
every version? first when it didn't create the record yet, and
afterwards when it did and uploads it off to s3.
So you wind up with a 0.jpg in the database and a correct string on
s3, but you can't refer to it.

I'm not sure - of course, please feel free to investigate more about this. We can discuss more in a pull request if you like. 
Re: Generating a random filename for the original, but not for every version. karb 3/1/11 4:53 AM
On Mar 1, 1:49 pm, Trevor Turk <trevort...@gmail.com> wrote:
> please feel free to investigate more about this.
> We can discuss more in a pull request if you like.

Will definitely think about this some more and tinker with the code,
as soon as I have something ready will put it in a pull request.
More thoughts on this in this thread couldn't hurt tho =)
Re: Generating a random filename for the original, but not for every version. s shaw 4/14/11 2:29 PM


Trevor Turk wrote:
> On Tuesday, March 1, 2011 12:28:35 PM UTC, karb wrote:
> >
> > Obviously, that's not at all what we have to go through to accomplish
> > such a simple task as having a consistent unique filename across
> > versions?
> >
>
> Perhaps you could introduce a new attribute called "unique_hash" or
> something,

Depending on how the temp file is stored there's already a unique
attribute: the temp path. How about:

class AvatarUploader < CarrierWave::Uploader::Base
  def filename
    if original_filename
      @name ||= Digest::MD5.hexdigest(File.dirname(current_path))
      "#{@name}#{File.extname(original_filename)}"
    end
  end
end

-Skye
Aw: Generating a random filename for the original, but not for every version. Corin Langosch 4/29/11 3:10 AM
How about this solution? Works great for me :)

# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base

  ...
  
  def filename
    @name ||= "#{model.id}_#{secure_token}.jpg"
  end

  private
  
  def secure_token
    ivar = "@#{mounted_as}_secure_token"
    token = model.instance_variable_get(ivar)
    token ||= model.instance_variable_set(ivar, ActiveSupport::SecureRandom.hex(4))  
  end
end

unk...@googlegroups.com 4/29/11 3:10 AM <This message has been deleted.>
Re: Aw: Generating a random filename for the original, but not for every version. iwasrobbed 5/23/11 10:12 PM
Thanks Corin, that worked for me!  I really hope they rethink this feature in CarrierWave in the future.  This was more of a headache than it should have been.
Ang.: Aw: Generating a random filename for the original, but not for every version. Andrew Crookston 6/3/11 3:21 AM
I tried your solution and came across a couple of problems.
1. The model.id value is not always available, and should not be used in file names, see more in this thread: http://groups.google.com/group/carrierwave/browse_thread/thread/e728999803df30f9/0d49395188905cd0?lnk=gst&q=model.id#0d49395188905cd0 
2. The random hex string was being regenerated for every view of this file (image in my case).

I solved both problems by changing the filename method to:

  def filename
    @name ||= "#{secure_token}.#{file.extension}" if original_filename
  end

Re: Ang.: Aw: Generating a random filename for the original, but not for every version. Trevor Turk 6/5/11 9:42 AM
Please do consider updating the wiki with this kind of thing. Things get lost in the Google Group, and this kind of thing gets brought up a lot!
Re: Ang.: Aw: Generating a random filename for the original, but not for every version. karb 6/5/11 10:04 AM
My attempt: https://github.com/jnicklas/carrierwave/wiki/How-to:-Use-random-and-UUID-filenames