Class: Thingfish::Processor::Image

Inherits:
Thingfish::Processor
  • Object
show all
Extended by:
Configurability, Loggability, Strelka::MethodUtilities
Defined in:
lib/thingfish/processor/image.rb

Overview

Image processor plugin for Thingfish

Defined Under Namespace

Classes: MagickOperations

Constant Summary collapse

VERSION =

Package version

'0.2.1'
REVISION =

Version control revision

%q$Revision: 5dada1e86f21 $
IGNORED_MIMETYPES =

An array of mediatypes to ignore, even though ImageMagick claims it groks them

%w[
  application/octet-stream
  text/html
  text/plain
  text/x-server-parsed-html
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImage

Set up a new Filter object



54
55
56
57
58
59
60
# File 'lib/thingfish/processor/image.rb', line 54

def initialize( * ) # :notnew:
  super

  @supported_formats = find_supported_formats()
  @handled_types     = find_handled_types( @supported_formats )
  @generated_types   = find_generated_types( @supported_formats )
end

Instance Attribute Details

#generated_typesObject (readonly)

The mediatypes the plugin can generated in a response, as ThingFish::AcceptParam objects



76
77
78
# File 'lib/thingfish/processor/image.rb', line 76

def generated_types
  @generated_types
end

#handled_typesObject (readonly)

The mediatypes the plugin accepts in a request, as ThingFish::AcceptParam objects



72
73
74
# File 'lib/thingfish/processor/image.rb', line 72

def handled_types
  @handled_types
end

#supported_formatsObject (readonly)

The Hash of formats and the operations they support.



68
69
70
# File 'lib/thingfish/processor/image.rb', line 68

def supported_formats
  @supported_formats
end

Instance Method Details

#handled_type?(type) ⇒ Boolean Also known as: is_handled_type?

Returns true if the given media type is one the processor handles. Overridden so the types can be used by the instance.

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/thingfish/processor/image.rb', line 81

def handled_type?( type )
  self.log.debug "Looking for handled type for: %p" % [ type ]
  result = self.handled_types.find {|handled_type| type =~ handled_type }
  self.log.debug "  found: %p" % [ result ]
  return result
end

#inspectObject

Return a human-readable representation of the receiving object suitable for debugging.



130
131
132
133
134
135
136
137
138
# File 'lib/thingfish/processor/image.rb', line 130

def inspect
  return "#<%p:%#x %d supported image formats, %d readable, %d writable>" % [
    self.class,
    self.object_id * 2,
    self.supported_formats.size,
    self.handled_types.size,
    self.generated_types.size,
  ]
end

#on_request(request) ⇒ Object

Synchronous processor API – extract metadata from uploaded images.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/thingfish/processor/image.rb', line 91

def on_request( request )
  self.log.debug "Image-processing %p" % [ request ]
  image = case request.body
      when StringIO
        self.log.debug "  making a single image from a StringIO"
        Magick::Image.from_blob( request.body.read )
      else
        path = request.body.path
        self.log.debug "  making a flattened image out of %p" % [ path ]
        path = path.to_s + '[0]' # ImageMagick, read a single frame
        list = Magick::ImageList.new( path )
        list.flatten_images
      end

  image = image.first if image.respond_to?( :first )
  self.log.debug "  image is: %p" % [ image ]
   = self.( image )
  self.log.debug "  extracted image metadata: %p" % [  ]
  request.(  )

  self.log.debug "  going to generate a thumbnail..."
  self.generate_thumbnail( image, ['title'] ) do |thumbio, |
    self.log.debug "  generated: %p (%p)" % [ thumbio,  ]
    request.add_related_resource( thumbio,  )
  end

  self.log.debug "  destroying the image to free up memory"

rescue Magick::ImageMagickError => err
  self.log.error "Problem while processing file %p: %s" % [ err.class, err.message ]
  self.log.debug { err.backtrace.join( "\n  " ) }

ensure
  image.destroy! if image
end