Class: Murlsh::ImgStore

Inherits:
Object
  • Object
show all
Defined in:
lib/murlsh/img_store.rb

Overview

Store images from various sources in asset storage.

Storage is determined by store_asset plugins.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ImgStore

Store images from various sources in asset storage.

Storage is determined by store_asset plugins.



18
19
20
# File 'lib/murlsh/img_store.rb', line 18

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



70
71
72
# File 'lib/murlsh/img_store.rb', line 70

def config
  @config
end

Instance Method Details

#headersObject

Build headers to send with request.



23
24
25
26
27
# File 'lib/murlsh/img_store.rb', line 23

def headers
  result = {}
  result['User-Agent'] = config['user_agent']  if config['user_agent']
  result
end

#store_img(img) ⇒ Object

Store a Magick::ImageList in asset storage.

Returns image url.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/murlsh/img_store.rb', line 54

def store_img(img)
  img.extend(Murlsh::ImageList)  unless img.is_a?(Murlsh::ImageList)
  img_data = img.to_blob
  md5 = Digest::MD5.hexdigest(img_data)

  name = "img/thumb/#{md5}#{img.preferred_extension}"

  Murlsh::Plugin.hooks('store_asset') do |p|
    # run until one returns something
    if url = p.run(name, img_data, config)
      return url
    end
  end
  nil
end

#store_img_data(img_data) {|img| ... } ⇒ Object

Store a blob of image data in asset storage.

If a block is given the Magick::ImageList created will be yielded before storage.

Returns image url.

Yields:

  • (img)


45
46
47
48
49
# File 'lib/murlsh/img_store.rb', line 45

def store_img_data(img_data, &block)
  img = Magick::ImageList.new.from_blob(img_data)
  yield img if block_given?
  store_img img
end

#store_url(url, &block) ⇒ Object

Fetch an image from a url and store it in asset storage.

If a block is given the Magick::ImageList created will be yielded before storage.

Returns image url.



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

def store_url(url, &block)
  open(url, headers) { |fin| store_img_data(fin.read, &block) }
end