Encode URL for a file upload

Hi,
  I'm creating an file uploading application. I'm using paperclip to
upload files.

my upload path is
"http://localhost:3000/system/uploads/uploaded_files/000/000/064/original/Cell%20phone%20ringing.mp4"
when I'm clicking on this I can see the file.

But I want to encode this URL something like
http://localhost:3000/UitMvriwuSc3VLy3IyU9MKYbSqSnxaZk5qcX6BgYGEGx

and when I click on this link, it should decode the URL & I should be
able to see the actual file in browser.

Anybody can help me.

Thanks in advance.
Avantec

Hi,
I'm creating an file uploading application. I'm using paperclip to
upload files.

I just did this, a couple weeks ago.

my upload path is
"http://localhost:3000/system/uploads/uploaded_files/000/000/064/original/Cell%20phone%20ringing.mp4"
when I'm clicking on this I can see the file.

But I want to encode this URL something like
http://localhost:3000/UitMvriwuSc3VLy3IyU9MKYbSqSnxaZk5qcX6BgYGEGx

and when I click on this link, it should decode the URL & I should be
able to see the actual file in browser.

Make a new column in your uploaded_files model called 'slug' or whatever you like (and index it, too!). Either alter your show method or make a new get_file method in your controller, which should look like this:

  @uploaded_file = UploadedFile.find_by_slug(params[:slug])

In your routes.rb, add a line like this (fairly low in the list, so it doesn't clobber the other routes)

  match '/:slug', :to => 'uploaded_files#show'

That should do the trick.

Here's a way to make a very terse code for your URL:

  def set_slug
    code = Array.new(7) { (('A'..'Z').to_a + (0..9).to_a)[rand(36)] }.join.to_s
    if UploadedFile.find_by_slug(code)
      return self.set_slug()
    else
      self.slug = code
    end
  end

Call it in an after_create.

Walter

Walter Davis wrote in post #1065538:

  match '/:slug', :to => 'uploaded_files#show'

That should do the trick.

Here's a way to make a very terse code for your URL:

  def set_slug
    code = Array.new(7) { (('A'..'Z').to_a + (0..9).to_a)[rand(36)]
}.join.to_s
    if UploadedFile.find_by_slug(code)
      return self.set_slug()
    else
      self.slug = code
    end
  end

Call it in an after_create.

Walter

Thanks a lot. It is working :slight_smile: cheers....

Glad it works for you!

Walter