Class: ImageVise

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise.rb,
lib/image_vise/version.rb

Defined Under Namespace

Classes: AutoOrient, AutoWriter, BackgroundFill, Crop, EllipseStencil, ExpireAfter, FetcherFile, FetcherHTTP, FileResponse, FitCrop, ForceJPGOut, Geom, ImageRequest, JPGWriter, Pipeline, RenderEngine, SRGB, Sharpen, StripMetadata

Constant Summary collapse

DEFAULT_CACHE_LIFETIME =

The default cache liftime is 30 days, and will be used if no custom lifetime is set.

2_592_000
DEFAULT_MAXIMUM_SOURCE_FILE_SIZE =

The default limit on how large may a file loaded for processing be, in bytes. This is in addition to the constraints on the file format.

48 * 1024 * 1024
VERSION =
'0.8.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_allowed_host!(hostname) ⇒ Object

Add an allowed host



40
41
42
# File 'lib/image_vise.rb', line 40

def add_allowed_host!(hostname)
  S_MUTEX.synchronize { @allowed_hosts << hostname }
end

.add_operator(operator_name, object_responding_to_new) ⇒ Object

Adds an operator



115
116
117
# File 'lib/image_vise.rb', line 115

def add_operator(operator_name, object_responding_to_new)
  @operators[operator_name.to_s] = object_responding_to_new
end

.add_secret_key!(key) ⇒ Object

Adds a key against which the parameters are going to be verified. Multiple applications may have their own different keys, so we need to have multiple keys.



84
85
86
87
# File 'lib/image_vise.rb', line 84

def add_secret_key!(key)
  S_MUTEX.synchronize { @keys << key }
  self
end

.allow_filesystem_source!(glob_pattern) ⇒ Object



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

def allow_filesystem_source!(glob_pattern)
  S_MUTEX.synchronize { @allowed_glob_patterns << glob_pattern }
end

.allowed_filesystem_sourcesObject



58
59
60
# File 'lib/image_vise.rb', line 58

def allowed_filesystem_sources
  S_MUTEX.synchronize { @allowed_glob_patterns.to_a }
end

.allowed_hostsObject

Returns both the allowed hosts added at runtime and the ones set in the constant



45
46
47
# File 'lib/image_vise.rb', line 45

def allowed_hosts
  S_MUTEX.synchronize { @allowed_hosts.to_a }
end

.cache_lifetime_secondsObject



73
74
75
# File 'lib/image_vise.rb', line 73

def cache_lifetime_seconds
  S_MUTEX.synchronize { @cache_lifetime }
end

.cache_lifetime_seconds=(length) ⇒ Object



66
67
68
69
70
71
# File 'lib/image_vise.rb', line 66

def cache_lifetime_seconds=(length)
  Integer(length)
  S_MUTEX.synchronize { @cache_lifetime = length.to_i }
rescue => e
  raise ArgumentError, "The custom cache lifetime value must be an integer"
end

.call(rack_env) ⇒ Object

Made available since the object that is used with ‘mount()` in Rails has to, by itself, to respond to `call`.

Thanks to this method you can do this:

mount ImageVise => '/thumbnails'

instead of having to do

mount ImageVise.new => '/thumbnails'


154
155
156
# File 'lib/image_vise.rb', line 154

def self.call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end

Used as a shorthand to force-dealloc Tempfiles in an ensure() blocks. Since ensure blocks sometimes deal with variables in inconsistent states (variable in scope but not yet set to an image) we take the possibility of nils into account.



178
179
180
181
182
183
184
# File 'lib/image_vise.rb', line 178

def self.close_and_unlink(maybe_tempfile)
  return unless maybe_tempfile
  Measurometer.instrument('image_vise.tempfile_unlink') do
    maybe_tempfile.close unless maybe_tempfile.closed?
    maybe_tempfile.unlink if maybe_tempfile.respond_to?(:unlink)
  end
end

.defined_operator_namesObject



124
125
126
# File 'lib/image_vise.rb', line 124

def defined_operator_names
  @operators.keys
end

.deny_filesystem_sources!Object



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

def deny_filesystem_sources!
  S_MUTEX.synchronize { @allowed_glob_patterns.clear }
end

.destroy(maybe_image) ⇒ Object

Used as a shorthand to force-destroy Magick images in ensure() blocks. Since ensure blocks sometimes deal with variables in inconsistent states (variable in scope but not yet set to an image) we take the possibility of nils into account. We also deal with Magick::Image objects that already have been destroyed in a clean manner.



166
167
168
169
170
171
172
173
# File 'lib/image_vise.rb', line 166

def self.destroy(maybe_image)
  return unless maybe_image
  return unless maybe_image.respond_to?(:destroy!)
  return if maybe_image.destroyed?
  Measurometer.instrument('image_vise.image_destroy_dealloc') do
    maybe_image.destroy!
  end
end

.fetcher_for(scheme) ⇒ Object



132
133
134
# File 'lib/image_vise.rb', line 132

def fetcher_for(scheme)
  S_MUTEX.synchronize { @fetchers[scheme.to_s] or raise "No fetcher registered for #{scheme}" }
end

.image_path(src_url:, secret:) {|ImageVise::Pipeline| ... } ⇒ String

Generate a path for a resized image. Yields a Pipeline object that will receive method calls for adding image operations to a stack.

ImageVise.image_path(src_url: image_url_on_s3, secret: '...') do |p|
   p.center_fit width: 128, height: 128
   p.elliptic_stencil
end #=> "/abcdef/xyz123"

The query string elements can be then passed on to RenderEngine for validation and execution.

Yields:

Returns:

  • (String)

Raises:

  • (ArgumentError)


107
108
109
110
111
112
# File 'lib/image_vise.rb', line 107

def image_path(src_url:, secret:)
  p = Pipeline.new
  yield(p)
  raise ArgumentError, "Image pipeline has no steps defined" if p.empty?
  ImageRequest.new(src_url: URI(src_url), pipeline: p).to_path_params(secret)
end

.operator_from(operator_name) ⇒ Object

Gets an operator by name



120
121
122
# File 'lib/image_vise.rb', line 120

def operator_from(operator_name)
  @operators.fetch(operator_name.to_s)
end

.operator_name_for(operator) ⇒ Object



136
137
138
139
140
# File 'lib/image_vise.rb', line 136

def operator_name_for(operator)
  S_MUTEX.synchronize do
    @operators.key(operator.class) or raise "Operator #{operator.inspect} not registered using ImageVise.add_operator"
  end
end

.register_fetcher(scheme, fetcher) ⇒ Object



128
129
130
# File 'lib/image_vise.rb', line 128

def register_fetcher(scheme, fetcher)
  S_MUTEX.synchronize { @fetchers[scheme.to_s] = fetcher }
end

.reset_allowed_hosts!Object

Resets all allowed hosts



35
36
37
# File 'lib/image_vise.rb', line 35

def reset_allowed_hosts!
  S_MUTEX.synchronize { @allowed_hosts.clear }
end

.reset_cache_lifetime_seconds!Object



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

def reset_cache_lifetime_seconds!
  S_MUTEX.synchronize { @cache_lifetime = DEFAULT_CACHE_LIFETIME }
end

.reset_secret_keys!Object

Removes all set keys



50
51
52
# File 'lib/image_vise.rb', line 50

def reset_secret_keys!
  S_MUTEX.synchronize { @keys.clear }
end

.secret_keysObject

Returns the array of defined keys or raises an exception if no keys have been set yet



90
91
92
93
# File 'lib/image_vise.rb', line 90

def secret_keys
  keys = S_MUTEX.synchronize { @keys.any? && @keys.to_a }
  keys or raise "No keys set, add a key using `ImageVise.add_secret_key!(key)'"
end

Instance Method Details

#call(rack_env) ⇒ Object



158
159
160
# File 'lib/image_vise.rb', line 158

def call(rack_env)
  ImageVise::RenderEngine.new.call(rack_env)
end