image_validator 0.1.0
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.
- data/README.markdown +17 -0
- data/lib/image_validator.rb +31 -0
- data/test/fixtures/dolphin.txt +34 -0
- data/test/fixtures/space-lincoln.gif +0 -0
- data/test/fixtures/space-lincoln.jpg +0 -0
- data/test/fixtures/space-lincoln.png +0 -0
- data/test/image_validator_test.rb +62 -0
- data/test/test_helper.rb +26 -0
- metadata +63 -0
data/README.markdown
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# ImageValidator
|
2
|
+
|
3
|
+
A simple image file validator using the identify command line app.
|
4
|
+
|
5
|
+
Checks to see if an image is valid.
|
6
|
+
|
7
|
+
## Uage
|
8
|
+
|
9
|
+
ImageValidator.valid?(file)
|
10
|
+
|
11
|
+
`file` can be an IO, a filename or image data in a string. Returns true if the image exists.
|
12
|
+
|
13
|
+
|
14
|
+
ImageValidator.valid?("missing_image.png") # => false
|
15
|
+
ImageValidator.valid?("") # => false
|
16
|
+
ImageValidator.valid?(File.open("an_image_file.png")) # => true
|
17
|
+
# etc…
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ImageValidator
|
2
|
+
def valid?(file)
|
3
|
+
file = thing_to_io(file)
|
4
|
+
check_image_magick_is_available
|
5
|
+
IO.popen("identify - &> /dev/null", "w") do |identify|
|
6
|
+
some_content_has_been_read = false
|
7
|
+
while blk = file.read(1024**2)
|
8
|
+
identify << blk
|
9
|
+
some_content_has_been_read = true if blk != ""
|
10
|
+
end
|
11
|
+
return false unless some_content_has_been_read
|
12
|
+
end
|
13
|
+
$?.success?
|
14
|
+
end
|
15
|
+
|
16
|
+
protected
|
17
|
+
def check_image_magick_is_available
|
18
|
+
raise "identify command not found, please install imagemagick" if `which identify` == ''
|
19
|
+
end
|
20
|
+
|
21
|
+
def thing_to_io(thing)
|
22
|
+
return thing unless thing.is_a?(String)
|
23
|
+
|
24
|
+
if (File.exists?(thing) rescue false)
|
25
|
+
File.open(thing)
|
26
|
+
else
|
27
|
+
StringIO.new(thing, "rb")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
extend self
|
31
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
I'm a dolphin!
|
2
|
+
|
3
|
+
|
4
|
+
..
|
5
|
+
:` ``´:..
|
6
|
+
´:`:. ``´:.
|
7
|
+
: `-:. `´:.
|
8
|
+
: ::´ .::::::::...
|
9
|
+
: :´` ``´::.
|
10
|
+
:´ `´::
|
11
|
+
:´ `:.
|
12
|
+
.´ `´:
|
13
|
+
: ´:
|
14
|
+
:´ ´:
|
15
|
+
.: :.
|
16
|
+
:´ `:
|
17
|
+
:´ :
|
18
|
+
: :
|
19
|
+
: .:::::::: .: :
|
20
|
+
: .:::::::´´´´; :` ;
|
21
|
+
: ::::´` : :``::. . :. :
|
22
|
+
.´ .::´` : .´ `´:::. ` :
|
23
|
+
: .:´` ; .: `´: . : :
|
24
|
+
: .´ :` ..::´ `: `-. `:..:
|
25
|
+
; .: ```` `:. ´:. ´:
|
26
|
+
: :´ `´;.`: :
|
27
|
+
: : `:.:. :
|
28
|
+
:´´´ : `´::;
|
29
|
+
:´ :
|
30
|
+
: ':
|
31
|
+
´ . ´.
|
32
|
+
; :::`:. `.
|
33
|
+
: :´ `´. :
|
34
|
+
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper")
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class ImageValidatorTest < Test::Unit::TestCase
|
5
|
+
def test_an_invalid_image
|
6
|
+
name = "dolphin.txt"
|
7
|
+
assert_invalid(fixture(name))
|
8
|
+
assert_invalid(fixture_filename(name))
|
9
|
+
assert_invalid(fixture_as_string(name))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_a_missing_image
|
13
|
+
name = "your_face"
|
14
|
+
assert_invalid(StringIO.new("", "rb"))
|
15
|
+
assert_invalid(name)
|
16
|
+
assert_invalid("")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_a_png
|
20
|
+
name = "space-lincoln.png"
|
21
|
+
assert_valid(fixture(name))
|
22
|
+
assert_valid(fixture_filename(name))
|
23
|
+
assert_valid(fixture_as_string(name))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_a_gif
|
27
|
+
name = "space-lincoln.gif"
|
28
|
+
assert_valid(fixture(name))
|
29
|
+
assert_valid(fixture_filename(name))
|
30
|
+
assert_valid(fixture_as_string(name))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_a_jpeg
|
34
|
+
name = "space-lincoln.jpg"
|
35
|
+
assert_valid(fixture(name))
|
36
|
+
assert_valid(fixture_filename(name))
|
37
|
+
assert_valid(fixture_as_string(name))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class ImageValidatorWithNoImageMagickTest < Test::Unit::TestCase
|
42
|
+
def setup
|
43
|
+
@fake_bin = File.expand_path(File.join(ENV["TMPDIR"], "ImageValidatorWithNoImageMagickTest_bin"))
|
44
|
+
Dir.mkdir(@fake_bin)
|
45
|
+
File.symlink(`which which`.chomp, File.join(@fake_bin, "which"))
|
46
|
+
|
47
|
+
@orig_path = ENV["PATH"]
|
48
|
+
ENV["PATH"] = @fake_bin
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_explode_if_image_magick_is_missing
|
52
|
+
e = assert_raise(RuntimeError){
|
53
|
+
assert_valid(fixture("space-lincoln.png"))
|
54
|
+
}
|
55
|
+
assert_equal "identify command not found, please install imagemagick", e.message
|
56
|
+
end
|
57
|
+
|
58
|
+
def teardown
|
59
|
+
FileUtils.rm_rf(@fake_bin)
|
60
|
+
ENV["PATH"] = @orig_path
|
61
|
+
end
|
62
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
2
|
+
|
3
|
+
require "image_validator"
|
4
|
+
require "test/unit"
|
5
|
+
|
6
|
+
class Test::Unit::TestCase
|
7
|
+
def fixture(name)
|
8
|
+
File.open(fixture_filename(name), "rb")
|
9
|
+
end
|
10
|
+
|
11
|
+
def fixture_filename(name)
|
12
|
+
File.join(File.dirname(__FILE__), "fixtures", name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def fixture_as_string(name)
|
16
|
+
fixture(name).read
|
17
|
+
end
|
18
|
+
|
19
|
+
def assert_valid(image)
|
20
|
+
assert ImageValidator.valid?(image), "Expected image to be valid"
|
21
|
+
end
|
22
|
+
|
23
|
+
def assert_invalid(image)
|
24
|
+
assert !ImageValidator.valid?(image), "Expected image to be invalid"
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Lea
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-02 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: contrib@tomlea.co.uk
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- test/fixtures/dolphin.txt
|
27
|
+
- test/fixtures/space-lincoln.gif
|
28
|
+
- test/fixtures/space-lincoln.jpg
|
29
|
+
- test/fixtures/space-lincoln.png
|
30
|
+
- test/image_validator_test.rb
|
31
|
+
- test/test_helper.rb
|
32
|
+
- lib/image_validator.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://tomlea.co.uk
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --main
|
40
|
+
- README.markdown
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project: image_validator
|
58
|
+
rubygems_version: 1.3.5
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A simple wrapper around the ImageMagick command line to check if an image is valid.
|
62
|
+
test_files: []
|
63
|
+
|