class Refile::App

A Rack application which can be mounted or run on its own.

@example mounted in Rails

Rails.application.routes.draw do
  mount Refile::App.new, at: "attachments", as: :refile_app
end

@example as standalone app

require "refile"

run Refile::App.new

Private Instance Methods

backend() click to toggle source
# File lib/refile/app.rb, line 144
def backend
  Refile.backends.fetch(params[:backend]) do |name|
    log_error("Could not find backend: #{name}")
    halt 404
  end
end
download_allowed?() click to toggle source
# File lib/refile/app.rb, line 116
def download_allowed?
  Refile.allow_downloads_from == :all or Refile.allow_downloads_from.include?(params[:backend])
end
file() click to toggle source
# File lib/refile/app.rb, line 151
def file
  file = backend.get(params[:id])
  unless file.exists?
    log_error("Could not find attachment by id: #{params[:id]}")
    halt 404
  end
  file.download
end
force_download?(params) click to toggle source
# File lib/refile/app.rb, line 183
def force_download?(params)
  !params["force_download"].nil?
end
log_error(message) click to toggle source
# File lib/refile/app.rb, line 167
def log_error(message)
  h = { invoker: self.class.name, message: message }
  logger.error h.to_json
end
logger() click to toggle source
# File lib/refile/app.rb, line 124
def logger
  Refile.logger
end
not_expired?(params) click to toggle source
# File lib/refile/app.rb, line 178
def not_expired?(params)
  params["expires_at"].nil? ||
    (Time.at(params["expires_at"].to_i) > Time.now)
end
processor() click to toggle source
# File lib/refile/app.rb, line 160
def processor
  Refile.processors.fetch(params[:processor]) do |name|
    log_error("Could not find processor: #{name}")
    halt 404
  end
end
stream_file(file) click to toggle source
# File lib/refile/app.rb, line 128
def stream_file(file)
  expires Refile.content_max_age, :public

  if file.respond_to?(:path)
    path = file.path
  else
    path = Dir::Tmpname.create(params[:id]) {}
    IO.copy_stream file, path
  end

  filename = Rack::Utils.unescape(request.path.split("/").last)
  disposition = force_download?(params) ? "attachment" : "inline"

  send_file path, filename: filename, disposition: disposition, type: ::File.extname(filename)
end
upload_allowed?() click to toggle source
# File lib/refile/app.rb, line 120
def upload_allowed?
  Refile.allow_uploads_to == :all or Refile.allow_uploads_to.include?(params[:backend])
end
verified?() click to toggle source
# File lib/refile/app.rb, line 172
def verified?
  base_path = request.fullpath.gsub(::File.join(request.script_name, params[:token]), "")

  Refile.valid_token?(base_path, params[:token]) && not_expired?(params)
end