magic_bytes 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/magic_bytes.rb +14 -5
- data/magic_bytes.gemspec +3 -3
- data/spec/magic_bytes_spec.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f329a7e5d53256b5f4889346b2f95fee7159aa11
|
4
|
+
data.tar.gz: 0380ac028a591eb9210691d22a74e716ac11e9f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41a12b10b5136b481dfd3df78b108daee7ed3e2d965780e9aab96194aecd54be42cb0863094732cec98fe6cc71150c423f986c10b011b405f1749564bb1e8407
|
7
|
+
data.tar.gz: b70c2fd86ea65e9594c6363607da6444d77eb60f3a9438c8d79af02b92d56b5144ba19d2f69f02e1ade4173b80f2c41db33e36212570482af3f95f6c7e1e32b1
|
data/lib/magic_bytes.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
module MagicBytes
|
2
|
-
VERSION = '1.0.
|
2
|
+
VERSION = '1.0.2'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
# The maximum length supported (needed for .tar archives)
|
5
|
+
HEADER_SIZE = 262
|
6
|
+
|
7
|
+
# Gets raised when the file being read from is at EOF or empty
|
8
|
+
# (when read() from the file returns `nil`)
|
9
|
+
ReadError = Class.new(StandardError)
|
10
|
+
|
11
|
+
# Describes a file type with its file extension and MIME type
|
12
|
+
FileType = Struct.new(:ext, :mime)
|
6
13
|
|
7
14
|
extend self
|
8
15
|
|
@@ -11,8 +18,9 @@ module MagicBytes
|
|
11
18
|
# @param io[#read] a readable object
|
12
19
|
# @return [Hash, nil] the hash of ext: and mime: or nil if the type could not be deduced
|
13
20
|
def read_and_detect(io)
|
14
|
-
|
15
|
-
|
21
|
+
first_n_bytes = io.read(HEADER_SIZE)
|
22
|
+
raise ReadError unless first_n_bytes
|
23
|
+
detect(first_n_bytes)
|
16
24
|
end
|
17
25
|
|
18
26
|
# This is a line-for-line port of https://github.com/sindresorhus/file-type
|
@@ -21,6 +29,7 @@ module MagicBytes
|
|
21
29
|
# @param header_bytes[String] the header bytes of the file
|
22
30
|
# @return [Hash, nil] the hash of ext: and mime: or nil if the type could not be deduced
|
23
31
|
def detect(header_bytes)
|
32
|
+
raise ReadError unless header_bytes
|
24
33
|
d = _detect(header_bytes)
|
25
34
|
FileType.new(d.fetch(:ext), d.fetch(:mime))
|
26
35
|
end
|
data/magic_bytes.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: magic_bytes 1.0.
|
5
|
+
# stub: magic_bytes 1.0.2 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "magic_bytes"
|
9
|
-
s.version = "1.0.
|
9
|
+
s.version = "1.0.2"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Julik Tarkhanov"]
|
14
|
-
s.date = "2016-
|
14
|
+
s.date = "2016-06-13"
|
15
15
|
s.description = "Basic file type checks based on a few header bytes"
|
16
16
|
s.email = "me@julik.nl"
|
17
17
|
s.extra_rdoc_files = [
|
data/spec/magic_bytes_spec.rb
CHANGED
@@ -5,7 +5,24 @@ def get_extension(ext, name='fixture')
|
|
5
5
|
file_type = File.open(path, 'r'){|f| MagicBytes.read_and_detect(f) }
|
6
6
|
end
|
7
7
|
|
8
|
+
describe 'MagicBytes.detect' do
|
9
|
+
it 'raises a meaningful exception with nil as argument' do
|
10
|
+
expect {
|
11
|
+
MagicBytes.detect(nil)
|
12
|
+
}.to raise_error(MagicBytes::ReadError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
8
16
|
describe 'MagicBytes.read_and_detect' do
|
17
|
+
describe 'with an empty file' do
|
18
|
+
it 'raises a meaningful exception' do
|
19
|
+
tf = Tempfile.new('x')
|
20
|
+
expect {
|
21
|
+
MagicBytes.read_and_detect(tf)
|
22
|
+
}.to raise_error(MagicBytes::ReadError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
9
26
|
describe 'with the original tests from the JS version' do
|
10
27
|
extensions = [
|
11
28
|
'jpg',
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magic_bytes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julik Tarkhanov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|