Module: Refile
- Defined in:
- lib/refile.rb,
lib/refile/app.rb,
lib/refile/file.rb,
lib/refile/rails.rb,
lib/refile/version.rb,
lib/refile/attachment.rb,
lib/refile/backend/s3.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, AttachmentFieldHelper, AttachmentHelper, Backend Classes: App, Engine, File, ImageProcessor, Invalid, RandomHasher
Constant Summary collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.app ⇒ Refile::App?
A shortcut to the instance of the Rack application.
-
.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.
-
.read_chunk_size ⇒ Fixnum
The number of bytes to read when files are streamed.
Class Method Summary collapse
-
.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.
-
.processor(name, processor = nil) {|Refile::File| ... } ⇒ Object
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.
-
.verify_uploadable(uploadable, max_size) ⇒ true
Verify that the given uploadable is indeed a valid uploadable.
Class Attribute Details
.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.
24 25 26 |
# File 'lib/refile.rb', line 24 def app @app 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.
41 42 43 |
# File 'lib/refile.rb', line 41 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.
33 34 35 |
# File 'lib/refile.rb', line 33 def host @host end |
.read_chunk_size ⇒ Fixnum
The number of bytes to read when files are streamed. Refile uses this in a couple of places where files should be streamed in a memory efficient way instead of reading the entire file into memory at once. The default value of this is 3000.
17 18 19 |
# File 'lib/refile.rb', line 17 def read_chunk_size @read_chunk_size end |
Class Method Details
.backends ⇒ Hash{String => Backend}
A global registry of backends.
46 47 48 |
# File 'lib/refile.rb', line 46 def backends @backends ||= {} end |
.cache ⇒ Backend
A shortcut to retrieving the backend named “cache” from the global registry.
110 111 112 |
# File 'lib/refile.rb', line 110 def cache backends["cache"] end |
.cache=(backend) ⇒ Object
A shortcut to setting the backend named “cache” in the global registry.
117 118 119 |
# File 'lib/refile.rb', line 117 def cache=(backend) backends["cache"] = backend end |
.configure { ... } ⇒ Object
Yield the Refile module as a convenience for configuring multiple config options at once.
125 126 127 |
# File 'lib/refile.rb', line 125 def configure yield self end |
.processor(name, processor = nil) {|Refile::File| ... } ⇒ Object
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.
86 87 88 89 |
# File 'lib/refile.rb', line 86 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.
56 57 58 |
# File 'lib/refile.rb', line 56 def processors @processors ||= {} end |
.store ⇒ Backend
A shortcut to retrieving the backend named “store” from the global registry.
95 96 97 |
# File 'lib/refile.rb', line 95 def store backends["store"] end |
.store=(backend) ⇒ Object
A shortcut to setting the backend named “store” in the global registry.
102 103 104 |
# File 'lib/refile.rb', line 102 def store=(backend) backends["store"] = backend end |
.verify_uploadable(uploadable, max_size) ⇒ true
Verify that the given uploadable is indeed a valid uploadable. This method is used by backends as a sanity check, you should not have to use this method unless you are writing a backend.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/refile.rb', line 138 def verify_uploadable(uploadable, max_size) [:size, :read, :eof?, :close].each do |m| unless uploadable.respond_to?(m) raise ArgumentError, "does not respond to `#{m}`." end end if max_size and uploadable.size > max_size raise Refile::Invalid, "#{uploadable.inspect} is too large" end true end |