Class: Alula::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/processor.rb

Direct Known Subclasses

DummyProcessor, ImageProcessor, VideoProcessor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, opts) ⇒ Processor

Returns a new instance of Processor.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/alula/processor.rb', line 41

def initialize(item, opts)
  @item = item
  @site = opts.delete(:site)
  @attachments = opts.delete(:attachments)
  
  @options = opts.delete(:options)
  
  # Networked processors, create global 'queues' to prevent multiple simultanous upload/downloads
  @@upload ||= Mutex.new
  @@download ||= Mutex.new
  @@lock ||= Mutex.new
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



9
10
11
# File 'lib/alula/processor.rb', line 9

def attachments
  @attachments
end

#itemObject (readonly)

Returns the value of attribute item.



6
7
8
# File 'lib/alula/processor.rb', line 6

def item
  @item
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/alula/processor.rb', line 7

def options
  @options
end

#siteObject (readonly)

Returns the value of attribute site.



8
9
10
# File 'lib/alula/processor.rb', line 8

def site
  @site
end

Class Method Details

.available?(options) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/alula/processor.rb', line 23

def self.available?(options)
  true
end

.mimetype(*mimetypes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/alula/processor.rb', line 11

def self.mimetype(*mimetypes)
  (class << self; self; end).send(:define_method, "mimetypes") do
    mimetypes.collect do |m|
      if m.kind_of?(String)
        Regexp.new("^#{m}$")
      else
        m
      end
    end
  end
end

.process?(item, opts) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/alula/processor.rb', line 27

def self.process?(item, opts)
  if self.respond_to?(:mimetypes)
    mimetype = if opts[:fast]
      MimeMagic.by_extension(item.extension)
    else
      MimeMagic.by_magic(File.open(item.filepath))
    end
    
    return !(self.mimetypes.select{|re| re.match(mimetype.type)}.empty?)
  end
  
  return false
end

Instance Method Details

#asset_path(name, type) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/alula/processor.rb', line 61

def asset_path(name, type)
  asset_name = self.attachments.asset_name(name, type.to_s)
  output = File.join(self.site.storage.path(:cache, "attachments"), asset_name)
  # Make sure our directory exists
  output_dir = self.site.storage.path(:cache, "attachments", File.dirname(asset_name))
  
  output
end

#cleanupObject



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

def cleanup
  @item = nil
end

#infoObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/alula/processor.rb', line 70

def info
  @info ||= begin
    # info = Dimensions.dimensions(self.item.filepath)
    info ||= begin
      _info = MiniExiftool.new self.item.filepath
      [_info.imagewidth, _info.imageheight, _info.rotation]
    end
    Hashie::Mash.new({
      width: info[0],
      height: info[1],
      rotation: info[2] || 0,
    })
  end
end

#processObject



58
59
# File 'lib/alula/processor.rb', line 58

def process
end