mojo_magick 0.5.4 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,94 +0,0 @@
1
- # This module provides some mix-in methods to permit resource limitation commands in MojoMagick
2
- module ImageMagick
3
- module ResourceLimits
4
- @@resource_limits = {}
5
-
6
- # controls limits on memory and other resources for imagemagick.
7
- # possible values for type can include:
8
- # Area, Disk, File, Map, or Memory
9
- # value is byte size for everything but Disk, where it's number of files
10
- # type can be string or symbol.
11
- # Just limiting Memory will not solve problems with imagemagick going out of
12
- # control with resource consumption on certain bad files. You have to set disk,
13
- # area and map limits too. Read up on imagemagick website for more.
14
- # Different options have different units
15
- # DISK: N GB
16
- # AREA, MAP, MEMORY: N MB
17
- # FILE: N num file handles
18
- # Examples:
19
- # # set disk to 5 gigabytes limit
20
- # MiniMagick::Image::set_limit(:disk => 5)
21
- # # set memory to 32mb, map to 64mb and disk to 0
22
- # MiniMagick::Image::set_limit(:memory => 32, 'map' => 64, 'disk' => 0)
23
- def set_limits(options)
24
- mem_fix = 1
25
- options.each do |resource, value|
26
- @@resource_limits[resource.to_s.downcase.to_sym] = value.to_s
27
- end
28
- end
29
-
30
- # remove a limit
31
- def remove_limits(*options)
32
- mem_fix = 1
33
- @@resource_limits.delete_if do |resource, value|
34
- idx = options.index(resource)
35
- resource == options.values_at(idx)[0].to_s.downcase.to_sym if idx
36
- end
37
- end
38
-
39
- # remove limits from resources
40
- def unset_limits(options = {})
41
- mem_fix = 1
42
- @@resource_limits = {}
43
- if options[:unset_env]
44
- ENV["MAGICK_AREA_LIMIT"]=nil
45
- ENV["MAGICK_MAP_LIMIT"]=nil
46
- ENV["MAGICK_MEMORY_LIMIT"]=nil
47
- ENV["MAGICK_DISK_LIMIT"]=nil
48
- end
49
- end
50
-
51
- # returns the default limits that imagemagick is using, when run with no "-limit" parameters
52
- # options:
53
- # :show_actual_values => true (default false) - will return integers instead of readable values
54
- def get_default_limits(options = {})
55
- mem_fix = 1
56
- parse_limits(options.merge(:get_current_limits => false))
57
- end
58
-
59
- # returns the limits that imagemagick is running based on any "set_limits" calls
60
- def get_current_limits(options = {})
61
- mem_fix = 1
62
- parse_limits(options.merge(:get_current_limits => true))
63
- end
64
-
65
- alias :get_limits :get_current_limits
66
-
67
- def parse_limits(options)
68
- show_actual_values = options[:show_actual_values]
69
- if options[:get_current_limits]
70
- raw_limits = self.raw_command('identify', '-list resource')
71
- else
72
- # we run a raw shell command here to obtain limits without applying command line limit params
73
- raw_limits = `identify -list resource`
74
- end
75
- actual_values, readable_values = parser.parse_limits(raw_limits)
76
- show_actual_values ? actual_values : readable_values
77
- end # parse_limits
78
-
79
- # returns a string suitable for passing as a set of imagemagick params
80
- # that contains all the limit constraints
81
- def get_limits_as_params
82
- retval = ''
83
- # we upcase the value here for newer versions of ImageMagick (>=6.8.x)
84
- @@resource_limits.each do |type, value|
85
- retval += " -limit #{type.to_s} #{value.upcase} "
86
- end
87
- retval
88
- end
89
-
90
- def parser
91
- @parser ||= MojoMagick::Util::Parser.new
92
- end
93
- end
94
- end
@@ -1,51 +0,0 @@
1
- require File::join(File::dirname(__FILE__), 'test_helper')
2
-
3
- class ResourceLimitsTest < MiniTest::Unit::TestCase
4
-
5
- def setup
6
- @orig_limits = MojoMagick::get_default_limits
7
- end
8
-
9
- def test_set_limits
10
- # set area to 32mb limit
11
- MojoMagick::set_limits(:area => '32mb')
12
- new_limits = MojoMagick::get_current_limits
13
- assert_equal '32mb', new_limits[:area].downcase
14
- end
15
-
16
- def test_get_limits
17
- assert(@orig_limits.size >= 7)
18
- end
19
-
20
- def test_resource_limits
21
- orig_limits_test = @orig_limits.dup
22
- orig_limits_test.delete_if do |resource, value|
23
- assert [:throttle, :area, :map, :disk, :memory, :file, :thread, :time].include?(resource), "Found unexpected resource #{resource}"
24
- true
25
- end
26
- assert_equal 0, orig_limits_test.size
27
- end
28
-
29
- def test_get_current_limits
30
- # remove limits on area
31
- MojoMagick::remove_limits(:area)
32
- new_limits = MojoMagick::get_current_limits
33
- assert_equal @orig_limits[:area], new_limits[:area]
34
- end
35
-
36
- def test_set_limits
37
- # set memory to 64 mb, disk to 0 and
38
- MojoMagick::set_limits(:memory => '64mb', :disk => '0b')
39
- new_limits = MojoMagick::get_current_limits(:show_actual_values => true)
40
- assert_equal 61, new_limits[:memory]
41
- assert_equal 0, new_limits[:disk]
42
- end
43
-
44
- def test_unset_limits
45
- # return to original/default limit values
46
- MojoMagick::unset_limits
47
- new_limits = MojoMagick::get_current_limits
48
- assert_equal @orig_limits, new_limits
49
- end
50
-
51
- end