lib/refile.rb in refile-0.4.2 vs lib/refile.rb in refile-0.5.0
- old
+ new
@@ -1,21 +1,22 @@
require "uri"
require "fileutils"
require "tempfile"
-require "rest_client"
require "logger"
require "mime/types"
module Refile
+ # @api private
class Invalid < StandardError; end
+
+ # @api private
class Confirm < StandardError
def message
"are you sure? this will remove all files in the backend, call as \
`clear!(:confirm)` if you're sure you want to do this"
end
end
- ONE_YEAR_IN_SECONDS = 31_557_600
class << self
# A shortcut to the instance of the Rack application. This should be
# set when the application is initialized. `refile/rails` sets this
# value.
@@ -49,20 +50,27 @@
#
# @return [String]
attr_accessor :allow_origin
# Value for Cache-Control: max-age=<value in seconds> header
+ #
+ # @return [Integer]
attr_accessor :content_max_age
- # Where should the rack application be mounted?
- # The default is 'attachments'
+ # Where should the rack application be mounted? The default is 'attachments'.
+ #
+ # @return [String]
attr_accessor :mount_point
# Should the rack application be automounted in a Rails app?
- # The default is true.
+ #
# If set to false then Refile.app should be mounted in the Rails application
# routes.rb with the options `at: Refile.mount_point, as: :refile_app`
+ #
+ # The default is true.
+ #
+ # @return [Boolean]
attr_accessor :automount
# A global registry of backends.
#
# @return [Hash{String => Backend}]
@@ -78,10 +86,18 @@
# @return [Hash{String => Proc}]
def processors
@processors ||= {}
end
+ # A global registry of types. Currently, types are simply aliases for a set
+ # of content types, but their functionality may expand in the future.
+ #
+ # @return [Hash{Symbol => Refile::Type}]
+ def types
+ @types ||= {}
+ end
+
# Adds a processor. The processor must respond to `call`, both receiving
# and returning an IO-like object. Alternatively a block can be given to
# this method which also receives and returns an IO-like object.
#
# An IO-like object is recommended to be an instance of the `IO` class or
@@ -104,10 +120,11 @@
#
# @param [#to_s] name The name of the processor
# @param [Proc, nil] processor The processor, must respond to `call` and.
# @yield [Refile::File] The file to modify
# @yieldreturn [IO] An IO-like object representing the processed file
+ # @return [void]
def processor(name, processor = nil, &block)
processor ||= block
processors[name.to_s] = processor
end
@@ -168,19 +185,29 @@
raise Refile::Invalid, "#{uploadable.inspect} is too large"
end
true
end
+ # Extract the filename from an uploadable object. If the filename cannot be
+ # determined, this method will return `nil`.
+ #
+ # @param [IO] uploadable The uploadable object to extract the filename from
+ # @return [String, nil] The extracted filename
def extract_filename(uploadable)
path = if uploadable.respond_to?(:original_filename)
uploadable.original_filename
elsif uploadable.respond_to?(:path)
uploadable.path
end
::File.basename(path) if path
end
+ # Extract the content type from an uploadable object. If the content type
+ # cannot be determined, this method will return `nil`.
+ #
+ # @param [IO] uploadable The uploadable object to extract the content type from
+ # @return [String, nil] The extracted content type
def extract_content_type(uploadable)
if uploadable.respond_to?(:content_type)
uploadable.content_type
else
filename = extract_filename(uploadable)
@@ -192,21 +219,24 @@
end
end
require "refile/version"
require "refile/signature"
+ require "refile/type"
require "refile/attacher"
require "refile/attachment"
require "refile/random_hasher"
require "refile/file"
+ require "refile/custom_logger"
require "refile/app"
require "refile/backend/file_system"
end
Refile.configure do |config|
config.direct_upload = ["cache"]
config.allow_origin = "*"
config.logger = Logger.new(STDOUT)
config.mount_point = "attachments"
config.automount = true
- config.content_max_age = Refile::ONE_YEAR_IN_SECONDS
+ config.content_max_age = 60 * 60 * 24 * 365
+ config.types[:image] = Refile::Type.new(:image, content_type: %w[image/jpeg image/gif image/png])
end