Skip to content

Instantly share code, notes, and snippets.

@ZempTime
Last active August 29, 2015 14:13
Show Gist options
  • Save ZempTime/041e6d71662b43b53a21 to your computer and use it in GitHub Desktop.
Save ZempTime/041e6d71662b43b53a21 to your computer and use it in GitHub Desktop.
Refile Attachment Disposition
module Refile
class << self
def attachment_url(object, name, *args, prefix: nil, filename: nil, format: nil, host: nil, disposition: nil)
attacher = object.send(:"#{name}_attacher")
file = attacher.get
return unless file
host ||= Refile.host
prefix ||= Refile.mount_point
filename ||= attacher.basename || name.to_s
format ||= attacher.extension
backend_name = Refile.backends.key(file.backend)
filename = Rack::Utils.escape(filename)
filename << "." << format.to_s if format
uri = URI(host.to_s)
uri.path = ::File.join("", *prefix, backend_name, *args.map(&:to_s), file.id.to_s, filename)
uri.to_s
end
end
class App < Sinatra::Base
private
def stream_file(file)
expires Refile.content_max_age, :public, :must_revalidate
if file.respond_to?(:path)
path = file.path
else
path = Dir::Tmpname.create(params[:id]) {}
IO.copy_stream file, path
end
filename = request.path.split("/").last
send_file path, filename: filename, disposition: params.fetch(:disposition, :inline), type: ::File.extname(request.path)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment