Module: Refile
- Defined in:
- lib/refile.rb,
lib/refile/app.rb,
lib/refile/file.rb,
lib/refile/type.rb,
lib/refile/rails.rb,
lib/refile/version.rb,
lib/refile/attacher.rb,
lib/refile/signature.rb,
lib/refile/attachment.rb,
lib/refile/backend/s3.rb,
lib/refile/custom_logger.rb,
lib/refile/random_hasher.rb,
lib/refile/backend_macros.rb,
lib/refile/image_processing.rb,
lib/refile/backend/file_system.rb,
lib/refile/rails/attachment_helper.rb,
lib/refile/attachment/active_record.rb
Defined Under Namespace
Modules: ActiveRecord, Attachment, AttachmentHelper, Backend Classes: App, File, ImageProcessor, RandomHasher, Signature, Type
Constant Summary collapse
- VERSION =
"0.5.3"
Class Attribute Summary collapse
-
.allow_origin ⇒ String
Value for Access-Control-Allow-Origin header.
-
.app ⇒ Refile::App?
A shortcut to the instance of the Rack application.
-
.automount ⇒ Boolean
Should the rack application be automounted in a Rails app?.
-
.content_max_age ⇒ Integer
Value for Cache-Control: max-age=
header. -
.direct_upload ⇒ Array[String]
A list of names which identify backends in the global backend registry.
-
.host ⇒ String?
The host name that the Rack application can be reached at.
-
.logger ⇒ Logger
Logger that should be used by rack application.
-
.mount_point ⇒ String
Where should the rack application be mounted? The default is 'attachments'.
Class Method Summary collapse
-
.attachment_url(object, name, *args, prefix: nil, filename: nil, format: nil, host: nil) ⇒ String?
Generate a URL to an attachment.
-
.backends ⇒ Hash{String => Backend}
A global registry of backends.
-
.cache ⇒ Backend
A shortcut to retrieving the backend named "cache" from the global registry.
-
.cache=(backend) ⇒ Object
A shortcut to setting the backend named "cache" in the global registry.
-
.configure { ... } ⇒ Object
Yield the Refile module as a convenience for configuring multiple config options at once.
-
.extract_content_type(uploadable) ⇒ String?
Extract the content type from an uploadable object.
-
.extract_filename(uploadable) ⇒ String?
Extract the filename from an uploadable object.
-
.processor(name, processor = nil) {|Refile::File| ... }
Adds a processor.
-
.processors ⇒ Hash{String => Proc}
A global registry of processors.
-
.store ⇒ Backend
A shortcut to retrieving the backend named "store" from the global registry.
-
.store=(backend) ⇒ Object
A shortcut to setting the backend named "store" in the global registry.
-
.types ⇒ Hash{Symbol => Refile::Type}
A global registry of types.
Class Attribute Details
.allow_origin ⇒ String
Value for Access-Control-Allow-Origin header
55 56 57 |
# File 'lib/refile.rb', line 55 def allow_origin @allow_origin end |
.app ⇒ Refile::App?
A shortcut to the instance of the Rack application. This should be
set when the application is initialized. refile/rails sets this
value.
28 29 30 |
# File 'lib/refile.rb', line 28 def app @app end |
.automount ⇒ Boolean
Should the rack application be automounted in a Rails app?
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.
75 76 77 |
# File 'lib/refile.rb', line 75 def automount @automount end |
.content_max_age ⇒ Integer
Value for Cache-Control: max-age=
60 61 62 |
# File 'lib/refile.rb', line 60 def content_max_age @content_max_age end |
.direct_upload ⇒ Array[String]
A list of names which identify backends in the global backend registry.
The Rack application allows POST requests to only the backends specified
in this config option. This defaults to ["cache"], only allowing direct
uploads to the cache backend.
45 46 47 |
# File 'lib/refile.rb', line 45 def direct_upload @direct_upload end |
.host ⇒ String?
The host name that the Rack application can be reached at. If not set, Refile will use an absolute URL without hostname. It is strongly recommended to run Refile behind a CDN and to set this to the hostname of the CDN distribution. A protocol relative URL is recommended for this value.
37 38 39 |
# File 'lib/refile.rb', line 37 def host @host end |
.logger ⇒ Logger
Logger that should be used by rack application
50 51 52 |
# File 'lib/refile.rb', line 50 def logger @logger end |
.mount_point ⇒ String
Where should the rack application be mounted? The default is 'attachments'.
65 66 67 |
# File 'lib/refile.rb', line 65 def mount_point @mount_point end |
Class Method Details
.attachment_url(object, name, *args, prefix: nil, filename: nil, format: nil, host: nil) ⇒ String?
Generate a URL to an attachment. This method receives an instance of a class which has used the Refile::Attachment#attachment macro to generate an attachment column, and the name of this column, and based on this generates a URL to a App.
Optionally the name of a processor and arguments to it can be appended.
If the filename option is not given, the filename is taken from the
metadata stored in the attachment, or eventually falls back to the
name.
The host defaults to host, which is useful for serving all
attachments from a CDN. You can also override the host via the host
option.
Returns nil if there is no file attached.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/refile.rb', line 233 def (object, name, *args, prefix: nil, filename: nil, format: nil, host: 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 |
.backends ⇒ Hash{String => Backend}
A global registry of backends.
80 81 82 |
# File 'lib/refile.rb', line 80 def backends @backends ||= {} end |
.cache ⇒ Backend
A shortcut to retrieving the backend named "cache" from the global registry.
153 154 155 |
# File 'lib/refile.rb', line 153 def cache backends["cache"] end |
.cache=(backend) ⇒ Object
A shortcut to setting the backend named "cache" in the global registry.
160 161 162 |
# File 'lib/refile.rb', line 160 def cache=(backend) backends["cache"] = backend end |
.configure { ... } ⇒ Object
Yield the Refile module as a convenience for configuring multiple config options at once.
168 169 170 |
# File 'lib/refile.rb', line 168 def configure yield self end |
.extract_content_type(uploadable) ⇒ String?
Extract the content type from an uploadable object. If the content type
cannot be determined, this method will return nil.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/refile.rb', line 191 def extract_content_type(uploadable) if uploadable.respond_to?(:content_type) uploadable.content_type else filename = extract_filename(uploadable) if filename content_type = MIME::Types.of(filename).first content_type.to_s if content_type end end end |
.extract_filename(uploadable) ⇒ String?
Extract the filename from an uploadable object. If the filename cannot be
determined, this method will return nil.
177 178 179 180 181 182 183 184 |
# File 'lib/refile.rb', line 177 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 |
.processor(name, processor = nil) {|Refile::File| ... }
This method returns an undefined value.
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
one of its subclasses, like File or a StringIO, or a Refile::File.
It can also be any other object which responds to size, read, eof?
and close and mimics the behaviour of IO objects for these methods.
129 130 131 132 |
# File 'lib/refile.rb', line 129 def processor(name, processor = nil, &block) processor ||= block processors[name.to_s] = processor end |
.processors ⇒ Hash{String => Proc}
A global registry of processors. These will be used by the Rack application to manipulate files prior to serving them up to the user, based on options sent trough the URL. This can be used for example to resize images or to convert files to another file format.
90 91 92 |
# File 'lib/refile.rb', line 90 def processors @processors ||= {} end |
.store ⇒ Backend
A shortcut to retrieving the backend named "store" from the global registry.
138 139 140 |
# File 'lib/refile.rb', line 138 def store backends["store"] end |
.store=(backend) ⇒ Object
A shortcut to setting the backend named "store" in the global registry.
145 146 147 |
# File 'lib/refile.rb', line 145 def store=(backend) backends["store"] = backend end |
.types ⇒ Hash{Symbol => Refile::Type}
A global registry of types. Currently, types are simply aliases for a set of content types, but their functionality may expand in the future.
98 99 100 |
# File 'lib/refile.rb', line 98 def types @types ||= {} end |