Skip to content

Instantly share code, notes, and snippets.

@dce
Last active June 29, 2018 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dce/4e79183a105e415ca0e5e1f1709089b8 to your computer and use it in GitHub Desktop.
Save dce/4e79183a105e415ca0e5e1f1709089b8 to your computer and use it in GitHub Desktop.
# https://viget.com/articles/the-right-way-to-store-and-serve-dragonfly-thumbnails
Dragonfly.app.configure do
# ... (other settings)
define_url do |app, job, opts|
thumb = Thumb.find_by_signature(job.signature)
if thumb
app.datastore.url_for(thumb.uid)
else
app.server.url_for(job)
end
end
before_serve do |job, env|
thumb = Thumb.find_by_signature(job.signature)
if thumb
throw :halt, [301, { 'Location' => job.app.remote_url_for(thumb.uid) }, [""]]
else
uid = job.store
Thumb.create!(uid: uid, signature: job.signature)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment