0sc / activestorage-cloudinary-service

MIT License
60 stars 8 forks source link

CORBS warning after 5minutes or more #13

Closed jbarnobal closed 6 years ago

jbarnobal commented 6 years ago

My Activestorage routes outside post 'upload', to: 'projects#upload'

my upload method

class ProjectsController < ApplicationController

 def upload
  @image = Project.new (image: params[:files].first)
   url = [
    {
     url: @image.image.service_url,
     thumbnail: @image.image.service_url,
     delete_url: @image.image.service_url
     delete_type: "DELETE"
    }
   ]
   end
   render json: url
end

Upload is working fine but after 5minutes or more i got a CORBS warning GET https://api.cloudinary.com/v1_1/saintlucifer91/image/download?api_key=576932957151598&attachment=false&expires_at=1532661832&public_id=ndN5keoAccUwGzF7AUqyqSZ7&signature=033b7b7c4c95dcdbb54a77ef37caf32b96d0a657&timestamp=1532661532&type=upload 401 (Unauthorized)

jbarnobal commented 6 years ago

my image link got this

{
"error": {
"message": "Stale request - expires_at 2018-07-27 03:23:52 +0000 has passed"
}
}
jbarnobal commented 6 years ago

Seem the problem is like this https://leavingharbor.com/posts/gotcha-with-active-storage-and-cloudflare

0sc commented 6 years ago

@jbarnobal, have you tried using the provided helpers, #url_for and/or #rails_blob_url? Either will provide you a permanent URL that points to your application and redirects your to the actual (short-lived) service url only when you want to access it.

Ideally, you shouldn't be accessing the #service_url directly but rather use either of the provided helpers. So in your case, you will want to do

...
url: url_for(@image.image)
...

or

...
url: rails_blob_url(@image.image)
...

The 5 mins expiration is an expected behaviour. Please see: https://edgeguides.rubyonrails.org/active_storage_overview.html#linking-to-files