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/attacher.rb,
lib/refile/signature.rb,
lib/refile/attachment.rb,
lib/refile/backend/s3.rb,
lib/refile/random_hasher.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, Attacher, Confirm, Engine, File, ImageProcessor, Invalid, RandomHasher, Signature

Constant Summary collapse

ONE_YEAR_IN_SECONDS =
31_557_600
VERSION =
"0.4.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.allow_originString

Value for Access-Control-Allow-Origin header

Returns:

  • (String)


59
60
61
# File 'lib/refile.rb', line 59

def allow_origin
  @allow_origin
end

.appRefile::App?

A shortcut to the instance of the Rack application. This should be set when the application is initialized. ‘refile/rails` sets this value.

Returns:



32
33
34
# File 'lib/refile.rb', line 32

def app
  @app
end

.automountObject

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`



72
73
74
# File 'lib/refile.rb', line 72

def automount
  @automount
end

.content_max_ageObject

Value for Cache-Control: max-age=<value in seconds> header



62
63
64
# File 'lib/refile.rb', line 62

def content_max_age
  @content_max_age
end

.direct_uploadArray[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.

Returns:

  • (Array[String])


49
50
51
# File 'lib/refile.rb', line 49

def direct_upload
  @direct_upload
end

.hostString?

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.

Returns:

  • (String, nil)


41
42
43
# File 'lib/refile.rb', line 41

def host
  @host
end

.loggerLogger

Logger that should be used by rack application

Returns:

  • (Logger)


54
55
56
# File 'lib/refile.rb', line 54

def logger
  @logger
end

.mount_pointObject

Where should the rack application be mounted? The default is ‘attachments’



66
67
68
# File 'lib/refile.rb', line 66

def mount_point
  @mount_point
end

.read_chunk_sizeFixnum

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`.

Returns:

  • (Fixnum)


25
26
27
# File 'lib/refile.rb', line 25

def read_chunk_size
  @read_chunk_size
end

Class Method Details

.backendsHash{String => Backend}

A global registry of backends.

Returns:



77
78
79
# File 'lib/refile.rb', line 77

def backends
  @backends ||= {}
end

.cacheBackend

A shortcut to retrieving the backend named “cache” from the global registry.

Returns:



141
142
143
# File 'lib/refile.rb', line 141

def cache
  backends["cache"]
end

.cache=(backend) ⇒ Object

A shortcut to setting the backend named “cache” in the global registry.

Parameters:



148
149
150
# File 'lib/refile.rb', line 148

def cache=(backend)
  backends["cache"] = backend
end

.configure { ... } ⇒ Object

Yield the Refile module as a convenience for configuring multiple config options at once.

Yields:

  • Refile



156
157
158
# File 'lib/refile.rb', line 156

def configure
  yield self
end

.extract_content_type(uploadable) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/refile.rb', line 190

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) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/refile.rb', line 181

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| ... } ⇒ 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.

Examples:

With processor class

class Reverse
  def call(file)
    StringIO.new(file.read.reverse)
  en
end
Refile.processor(:reverse, Reverse)

With block

Refile.processor(:reverse) do |file|
  StringIO.new(file.read.reverse)
end

Parameters:

  • name (#to_s)

    The name of the processor

  • processor (Proc, nil) (defaults to: nil)

    The processor, must respond to ‘call` and.

Yields:

Yield Returns:

  • (IO)

    An IO-like object representing the processed file



117
118
119
120
# File 'lib/refile.rb', line 117

def processor(name, processor = nil, &block)
  processor ||= block
  processors[name.to_s] = processor
end

.processorsHash{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.

Returns:

  • (Hash{String => Proc})


87
88
89
# File 'lib/refile.rb', line 87

def processors
  @processors ||= {}
end

.storeBackend

A shortcut to retrieving the backend named “store” from the global registry.

Returns:



126
127
128
# File 'lib/refile.rb', line 126

def store
  backends["store"]
end

.store=(backend) ⇒ Object

A shortcut to setting the backend named “store” in the global registry.

Parameters:



133
134
135
# File 'lib/refile.rb', line 133

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.

Parameters:

  • uploadable (IO)

    The uploadable object to verify

  • max_size (Fixnum)

    The maximum size of the uploadable object

Returns:

  • (true)

    Always returns true if it doesn’t raise

Raises:

  • (ArgumentError)

    If the uploadable is not an IO-like object

  • (Refile::Invalid)

    If the uploadable’s size is too large



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/refile.rb', line 169

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