Class: Compostr::ImageUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/compostr/image_uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_store, media_cache) ⇒ ImageUploader

Returns a new instance of ImageUploader.



8
9
10
11
12
13
14
# File 'lib/compostr/image_uploader.rb', line 8

def initialize image_store, media_cache
  if !image_store
    Compostr.logger.warn "ImageUploader will not upload anything (no image store specified)."
  end
  @image_store = image_store
  @media_cache = media_cache
end

Instance Attribute Details

#image_storeObject

Returns the value of attribute image_store.



5
6
7
# File 'lib/compostr/image_uploader.rb', line 5

def image_store
  @image_store
end

#media_cacheObject

Returns the value of attribute media_cache.



6
7
8
# File 'lib/compostr/image_uploader.rb', line 6

def media_cache
  @media_cache
end

Instance Method Details

#process(rel_path, wp_event = nil) ⇒ Object

Returns attachment id of an image already uploaded or attachment_id of image after fresh upload (or nil if path empty) path is relative path to file to upload.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/compostr/image_uploader.rb', line 20

def process rel_path, wp_event=nil
  return nil if rel_path.to_s.strip.empty?

  # Do we need URI encoding here?
  if attachment_id = @media_cache.id_of_name(rel_path)
    Compostr.logger.debug "Image already uploaded."
  else
    return nil if @image_store.nil?
    path = File.join(@image_store, rel_path)
    Compostr.logger.info "Uploading file #{path}"
    upload = Compostr::ImageUpload.new(path)
    attachment_id = upload.do_upload!
    Compostr.logger.debug "Uploaded image id: #{attachment_id}"
  end

  return attachment_id
end