fileinfo 0.5.0 → 0.5.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef15eb6d6152498892fa78fa6d4361e6c3d14932
4
- data.tar.gz: 2313c820cbafacdb9c707966c1a0786554464627
3
+ metadata.gz: 68f73db3aad615d284bfbdd368005c6aefc2d111
4
+ data.tar.gz: d63bf9b1aede2fa3b31030dfa135afac5abdf059
5
5
  SHA512:
6
- metadata.gz: e885d126ade6f5f077b181b24bed9d316e236105a6b15dc22977d297d7f1363388ef4f75cdf7329b6f32b3672b54407d9c4040edc40c543c4f588d676eb3c1ec
7
- data.tar.gz: 3415d0dfcfd593df5177a6a660fbc0f6b605b5607cb934a4e44f100cbeacf582b1425611758097c5b9d2c33fd0fe4c2cd7dd2cb4ece170da54e75c6e9e14e410
6
+ metadata.gz: a17b722285eddd803477a48b77e48323e34a8f7d3d24959c58167fd8f84f50838fc2de14675caf9758814ed597ec3e63abd84579b1c0f0890d53ed2faec3bc77
7
+ data.tar.gz: 55f417126301e67db3e3ba5ccbeeacb963e158c89e0adf6b6674d65d5615faf50989d31a5038ff613f69b7c436cf6758fb4eec6f6b30b8c84a6312dc3e03c29c
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Rafaël Blais Masson
1
+ Copyright (c) 2017 Rafaël Blais Masson
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -79,4 +79,4 @@ info.content_type # => "text/plain; charset=utf-8"
79
79
 
80
80
  ---
81
81
 
82
- © 2014 [Rafaël Blais Masson](http://rafbm.com). FileInfo is released under the MIT license.
82
+ © 2017 [Rafaël Blais Masson](http://rafbm.com). FileInfo is released under the MIT license.
@@ -20,6 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec', '~> 2.14'
23
+ spec.add_development_dependency 'rspec'
24
24
  spec.add_development_dependency 'mime-types'
25
25
  end
@@ -10,6 +10,7 @@ class FileInfo
10
10
  CHARSET_REGEX = /charset=(\S+)/
11
11
 
12
12
  MIME_TYPE_ERROR_MESSAGE = 'You must install the "mime-types" gem to use FileInfo#mime_type'
13
+ DEFAULT_MIME_TYPE = 'application/octet-stream'
13
14
 
14
15
  attr_reader :content_type
15
16
 
@@ -18,7 +19,10 @@ class FileInfo
18
19
  end
19
20
 
20
21
  def type
21
- @type ||= content_type.match(MIME_TYPE_REGEX)[0]
22
+ @type ||= begin
23
+ match = content_type.match(MIME_TYPE_REGEX)
24
+ match ? match[0] : DEFAULT_MIME_TYPE
25
+ end
22
26
  end
23
27
 
24
28
  def media_type
@@ -1,3 +1,3 @@
1
1
  class FileInfo
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FileInfo do
4
- let(:binary_file) { fixture('one_byte.txt') }
4
+ let(:one_byte_file) { fixture('one_byte.txt') }
5
5
  let(:ascii_file) { fixture('encoding_ascii.csv') }
6
6
  let(:isolatin_file) { fixture('encoding_isolatin.csv') }
7
7
  let(:isowindows_file) { fixture('encoding_isowindows.csv') }
@@ -10,6 +10,7 @@ describe FileInfo do
10
10
 
11
11
  let(:photoshop_file) { fixture('mockup.psd') }
12
12
  let(:binary_file) { fixture('bytes') }
13
+ let(:max_file) { fixture('earth.max') }
13
14
 
14
15
  describe '#charset' do
15
16
  it 'returns encoding string' do
@@ -18,16 +19,18 @@ describe FileInfo do
18
19
  expect(FileInfo.load(isolatin_file.path).charset).to eq 'iso-8859-1'
19
20
  expect(FileInfo.load(isowindows_file.path).charset).to eq 'iso-8859-1'
20
21
  expect(FileInfo.load(utf8_file.path).charset).to eq 'utf-8'
22
+ expect(FileInfo.load(max_file.path).charset).to eq 'binary'
21
23
  end
22
24
  end
23
25
 
24
26
  describe '#encoding' do
25
27
  it 'returns Encoding instance' do
26
- expect(FileInfo.load(binary_file.path).encoding).to eq Encoding::BINARY
28
+ expect(FileInfo.load(one_byte_file.path).encoding).to eq Encoding::BINARY
27
29
  expect(FileInfo.load(ascii_file.path).encoding).to eq Encoding::US_ASCII
28
30
  expect(FileInfo.load(isolatin_file.path).encoding).to eq Encoding::ISO_8859_1
29
31
  expect(FileInfo.load(isowindows_file.path).encoding).to eq Encoding::ISO_8859_1
30
32
  expect(FileInfo.load(utf8_file.path).encoding).to eq Encoding::UTF_8
33
+ expect(FileInfo.load(max_file.path).encoding).to eq Encoding::BINARY
31
34
  end
32
35
 
33
36
  it 'raises UnknownEncodingError' do
@@ -59,6 +62,18 @@ describe FileInfo do
59
62
  it 'raises ArgumentError if file does not exist' do
60
63
  expect { FileInfo.load('WRONG!!1') }.to raise_error ArgumentError
61
64
  end
65
+
66
+ it 'prevents command line hacks' do
67
+ filename = "#{File.dirname(__FILE__)}/hacked.txt"
68
+ File.unlink(filename) if File.exists? filename
69
+
70
+ expect {
71
+ begin
72
+ FileInfo.load("foo; touch #{filename}")
73
+ rescue ArgumentError
74
+ end
75
+ }.not_to change { File.exists? filename }
76
+ end
62
77
  end
63
78
 
64
79
  describe '.parse' do
@@ -69,7 +84,7 @@ describe FileInfo do
69
84
  expect(FileInfo.parse(utf8_file.read).encoding).to eq Encoding::UTF_8
70
85
  end
71
86
 
72
- context 'when internal encoding is UTF-8 (like Rails forces)' do
87
+ context 'when internal encoding is UTF-8 (like Rails enforces)' do
73
88
  around do |example|
74
89
  previous_encoding = Encoding.default_internal
75
90
  Encoding.default_internal = Encoding::UTF_8
@@ -85,11 +100,12 @@ describe FileInfo do
85
100
  end
86
101
  end
87
102
 
88
- let(:txt) { FileInfo.parse(FileInfo.parse('Hello, world!')) }
103
+ let(:txt) { FileInfo.parse('Hello, world!') }
89
104
  let(:csv) { FileInfo.load(utf8_file.path) }
90
105
  let(:psd) { FileInfo.load(photoshop_file.path) }
91
106
  let(:empty) { FileInfo.parse('') }
92
107
  let(:bytes) { FileInfo.load(binary_file.path) }
108
+ let(:max) { FileInfo.load(max_file.path) }
93
109
 
94
110
  describe '#content_type' do
95
111
  it 'returns full Content-Type string' do
@@ -98,6 +114,7 @@ describe FileInfo do
98
114
  expect(psd.content_type).to eq 'image/vnd.adobe.photoshop; charset=binary'
99
115
  expect(empty.content_type).to match %r{(application|inode)/x-empty; charset=binary}
100
116
  expect(bytes.content_type).to eq 'application/octet-stream; charset=binary'
117
+ expect(max.content_type).to eq '; charset=binary'
101
118
  end
102
119
  end
103
120
 
@@ -108,6 +125,7 @@ describe FileInfo do
108
125
  expect(psd.type).to eq 'image/vnd.adobe.photoshop'
109
126
  expect(empty.type).to match %r{(application|inode)/x-empty}
110
127
  expect(bytes.type).to eq 'application/octet-stream'
128
+ expect(max.type).to eq 'application/octet-stream'
111
129
  end
112
130
  end
113
131
 
@@ -118,6 +136,7 @@ describe FileInfo do
118
136
  expect(psd.media_type).to eq 'image'
119
137
  expect(empty.media_type).to match %r{(application|inode)}
120
138
  expect(bytes.media_type).to eq 'application'
139
+ expect(max.media_type).to eq 'application'
121
140
  end
122
141
  end
123
142
 
@@ -128,12 +147,13 @@ describe FileInfo do
128
147
  expect(psd.sub_type).to eq 'vnd.adobe.photoshop'
129
148
  expect(empty.sub_type).to eq 'x-empty'
130
149
  expect(bytes.sub_type).to eq 'octet-stream'
150
+ expect(max.sub_type).to eq 'octet-stream'
131
151
  end
132
152
  end
133
153
 
134
154
  describe '#mime_type' do
135
155
  it 'returns a MIME::Type instance' do
136
- [txt, csv, psd, bytes].each do |fileinfo|
156
+ [txt, csv, psd, bytes, max].each do |fileinfo|
137
157
  expect(fileinfo.mime_type).to be_a MIME::Type
138
158
  end
139
159
  end
@@ -144,7 +164,7 @@ describe FileInfo do
144
164
 
145
165
  context 'when "mime-types" gem is not available' do
146
166
  before do
147
- MIME.send(:remove_const, :Types)
167
+ hide_const 'MIME::Types'
148
168
  allow_any_instance_of(FileInfo).to receive(:require).with('mime/types').and_raise(LoadError)
149
169
  end
150
170
 
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fileinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafaël Blais Masson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
11
+ date: 2017-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '2.14'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '2.14'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mime-types
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +87,7 @@ files:
87
87
  - lib/fileinfo.rb
88
88
  - spec/file_info_spec.rb
89
89
  - spec/fixtures/bytes
90
+ - spec/fixtures/earth.max
90
91
  - spec/fixtures/encoding_ascii.csv
91
92
  - spec/fixtures/encoding_isolatin.csv
92
93
  - spec/fixtures/encoding_isowindows.csv
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  version: '0'
116
117
  requirements: []
117
118
  rubyforge_project:
118
- rubygems_version: 2.2.2
119
+ rubygems_version: 2.5.2
119
120
  signing_key:
120
121
  specification_version: 4
121
122
  summary: FileInfo detects file encodings and MIME types using the wonderful Unix `file`
@@ -123,6 +124,7 @@ summary: FileInfo detects file encodings and MIME types using the wonderful Unix
123
124
  test_files:
124
125
  - spec/file_info_spec.rb
125
126
  - spec/fixtures/bytes
127
+ - spec/fixtures/earth.max
126
128
  - spec/fixtures/encoding_ascii.csv
127
129
  - spec/fixtures/encoding_isolatin.csv
128
130
  - spec/fixtures/encoding_isowindows.csv