attachments 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,6 +18,12 @@ times faster at decoding large attachments if you us ruby 1.9 instead of 1.8.
18
18
 
19
19
  == NEW STUFF:
20
20
 
21
+ Version 0.0.14
22
+
23
+ * Support in-memory storage of attachments to support processing on
24
+ systems without access to file system (i.e. Heroku + Cloudmailin)
25
+ * Include set in extract.rb to avoid exception in ruby 1.9.2
26
+
21
27
  Version 0.0.11
22
28
 
23
29
  * Convert the subject to utf-8
@@ -90,7 +96,7 @@ when they are uncertain.
90
96
  puts f.inspect
91
97
  case f[:mime_type]
92
98
  when "text/plain" then
93
- puts File.read(f[:tmpfile])
99
+ puts f[:body] || File.read(f[:tmpfile])
94
100
  end
95
101
  end
96
102
  # Remove tmp files and prepare for new parsing action
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.13
1
+ 0.0.14
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{attachments}
8
+ s.version = "0.0.14"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Rune Myrland"]
12
+ s.date = %q{2011-04-07}
13
+ s.description = %q{It's original intention is as a helper class class for filtering email attachments before forwarding. The extracted attachments are saved in the /tmp directory.}
14
+ s.email = %q{rune@epubify.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "attachments.gemspec",
26
+ "lib/attachments.rb",
27
+ "lib/attachments/extract.rb",
28
+ "lib/attachments/filemagic.rb",
29
+ "test/data/mail_0001.eml",
30
+ "test/data/mail_0002.eml",
31
+ "test/data/mail_0002.jpg",
32
+ "test/data/mail_0004.eml",
33
+ "test/helper.rb",
34
+ "test/test_attachments.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/wrimle/attachments}
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.6.2}
39
+ s.summary = %q{Extract mail parts of specified mime types.}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_attachments.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<mail>, [">= 2.2.5"])
50
+ s.add_runtime_dependency(%q<uuidtools>, [">= 2.1.1"])
51
+ else
52
+ s.add_dependency(%q<mail>, [">= 2.2.5"])
53
+ s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<mail>, [">= 2.2.5"])
57
+ s.add_dependency(%q<uuidtools>, [">= 2.1.1"])
58
+ end
59
+ end
60
+
@@ -5,6 +5,7 @@ require 'uuidtools'
5
5
  require 'fileutils'
6
6
  require 'mail'
7
7
  require 'iconv'
8
+ require 'set'
8
9
 
9
10
 
10
11
  module Attachments
@@ -19,14 +20,15 @@ module Attachments
19
20
  UNCERTAIN_TYPES = [ "application/octet-stream" ].to_set
20
21
 
21
22
 
22
- def initialize include_types = [ "text/plain" ]
23
+ def initialize include_types = [ "text/plain" ], options = { :cache_in_memory => false }
23
24
  @include_types = include_types
25
+ @cache_in_memory = options [ :cache_in_memory ] || false
24
26
  reset
25
27
  end
26
28
 
27
29
  def close
28
30
  files.each do |f|
29
- FileUtils::rm(f[:tmpfile])
31
+ FileUtils::rm(f[:tmpfile]) unless @cache_in_memory
30
32
  end
31
33
  reset
32
34
  end
@@ -95,7 +97,7 @@ module Attachments
95
97
  end
96
98
 
97
99
  def text_body
98
- if(@mail && @mail.text_part && @mail.text_part.body)
100
+ if(@mail && @mail.text_part && @mail.text_part.body)
99
101
  m = @mail.text_part.body.decoded
100
102
  charset = @mail.text_part.charset
101
103
  text = charset ? Iconv.conv("utf-8", charset, m) : m
@@ -173,14 +175,7 @@ module Attachments
173
175
  @name = mail.content_type_parameters['name'] || "unnamed"
174
176
  @name.gsub! /[^\w\.\-]/, '_' # Sanitize
175
177
 
176
- # Make it unique
177
- uuid = UUIDTools::UUID.random_create.hexdigest
178
-
179
- # The filename used for storage
180
- filename = "#{@name}.#{uuid}"
181
- filepath = "/tmp/#{filename}"
182
-
183
-
178
+ # Extract body
184
179
  body = case ct
185
180
  when "text/plain" then
186
181
  m = mail.body.decoded
@@ -207,24 +202,38 @@ module Attachments
207
202
  mail.body.decoded
208
203
  end
209
204
 
210
- # Save part as it is of a supported type
211
- f = File.new(filepath, "wb")
212
- f.write(body)
213
- f.close
205
+ # Make it unique
206
+ uuid = UUIDTools::UUID.random_create.hexdigest
214
207
 
215
- unless File.exists?(filepath)
216
- raise Error::SaveFailed, "Save failed for ", filepath, "\n"
217
- return
218
- end
208
+ # The filename used for storage
209
+ filename = "#{@name}.#{uuid}"
219
210
 
220
- # Sort out the mime type
221
- mime_type = ct
222
- if(UNCERTAIN_TYPES.include? mime_type)
223
- mime_type = FileMagic::mime filename
211
+ if @cache_in_memory
212
+ mime_type = ct
213
+ @files << { :name => @name, :body => body, :save_as => name, :upload_to => filename, :mime_type => mime_type }
214
+ else
215
+ # Tmp file
216
+ filepath = "/tmp/#{filename}"
217
+
218
+ # Save part as it is of a supported type
219
+ f = File.new(filepath, "wb")
220
+ f.write(body)
221
+ f.close
222
+
223
+ unless File.exists?(filepath)
224
+ raise Error::SaveFailed, "Save failed for ", filepath, "\n"
225
+ return
226
+ end
227
+
228
+ # Sort out the mime type
229
+ mime_type = ct
230
+ if(UNCERTAIN_TYPES.include? mime_type)
231
+ mime_type = FileMagic::mime filename
232
+ end
233
+
234
+ # Save meta-data for further processing
235
+ @files << { :name => @name, :tmpfile => filepath, :save_as => name, :upload_to => filename, :mime_type => mime_type }
224
236
  end
225
-
226
- # Save meta-data for further processing
227
- @files << { :name => @name, :tmpfile => filepath, :save_as => name, :upload_to => filename, :mime_type => mime_type }
228
237
  end
229
238
  end
230
239
  end
@@ -4,7 +4,7 @@ require 'helper'
4
4
  require 'iconv'
5
5
 
6
6
  class TestAttachments < Test::Unit::TestCase
7
- def compare_extractors a, b
7
+ def compare_extractors_old a, b
8
8
  (0...a.files.length).each do |i|
9
9
  tmp_a = a.files[i][:tmpfile]
10
10
  tmp_b = b.files[i][:tmpfile]
@@ -14,9 +14,20 @@ class TestAttachments < Test::Unit::TestCase
14
14
  end
15
15
  end
16
16
 
17
+
18
+ def compare_extractors a, b
19
+ (0...a.files.length).each do |i|
20
+ tmp_a = a.files[i][:body] || File.read(a.files[i][:tmpfile])
21
+ tmp_b = b.files[i][:body] || File.read(a.files[i][:tmpfile])
22
+
23
+ isIdentical = (tmp_a === tmp_b)
24
+ assert(isIdentical)
25
+ end
26
+ end
27
+
17
28
  context "Parse test cases without crashes" do
18
29
  setup do
19
- @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
30
+ @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ], { :cache_in_memory => true }
20
31
  end
21
32
 
22
33
  teardown do
@@ -45,7 +56,7 @@ class TestAttachments < Test::Unit::TestCase
45
56
 
46
57
  context "Just text text/plain and text/html" do
47
58
  setup do
48
- @extract = Attachments::Extract.new [ "text/plain" ]
59
+ @extract = Attachments::Extract.new [ "text/plain" ], { :cache_in_memory => false }
49
60
  @extract.parse "./test/data/mail_0001.eml"
50
61
  end
51
62
 
@@ -87,7 +98,7 @@ class TestAttachments < Test::Unit::TestCase
87
98
 
88
99
  context "UTF-8 and text/plain and image attachment" do
89
100
  setup do
90
- @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
101
+ @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ], { :cache_in_memory => false }
91
102
  @extract.parse "./test/data/mail_0002.eml"
92
103
  end
93
104
 
@@ -106,7 +117,7 @@ class TestAttachments < Test::Unit::TestCase
106
117
 
107
118
  context "convenience accessors" do
108
119
  setup do
109
- @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
120
+ @extract = Attachments::Extract.new [ "text/plain", "image/jpeg" ], { :cache_in_memory => true }
110
121
  @extract.parse "./test/data/mail_0001.eml"
111
122
  end
112
123
 
@@ -138,8 +149,8 @@ class TestAttachments < Test::Unit::TestCase
138
149
 
139
150
  context "Parse parameters" do
140
151
  setup do
141
- @a = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
142
- @b = Attachments::Extract.new [ "text/plain", "image/jpeg" ]
152
+ @a = Attachments::Extract.new [ "text/plain", "image/jpeg" ], { :cache_in_memory => true }
153
+ @b = Attachments::Extract.new [ "text/plain", "image/jpeg" ], { :cache_in_memory => true }
143
154
  end
144
155
 
145
156
  teardown do
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attachments
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 0
9
- - 13
10
- version: 0.0.13
4
+ prerelease:
5
+ version: 0.0.14
11
6
  platform: ruby
12
7
  authors:
13
8
  - Rune Myrland
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-09-14 00:00:00 +02:00
13
+ date: 2011-04-07 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,11 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ">="
28
23
  - !ruby/object:Gem::Version
29
- hash: 13
30
- segments:
31
- - 2
32
- - 2
33
- - 5
34
24
  version: 2.2.5
35
25
  type: :runtime
36
26
  version_requirements: *id001
@@ -42,11 +32,6 @@ dependencies:
42
32
  requirements:
43
33
  - - ">="
44
34
  - !ruby/object:Gem::Version
45
- hash: 9
46
- segments:
47
- - 2
48
- - 1
49
- - 1
50
35
  version: 2.1.1
51
36
  type: :runtime
52
37
  version_requirements: *id002
@@ -61,11 +46,11 @@ extra_rdoc_files:
61
46
  - README.rdoc
62
47
  files:
63
48
  - .document
64
- - .gitignore
65
49
  - LICENSE
66
50
  - README.rdoc
67
51
  - Rakefile
68
52
  - VERSION
53
+ - attachments.gemspec
69
54
  - lib/attachments.rb
70
55
  - lib/attachments/extract.rb
71
56
  - lib/attachments/filemagic.rb
@@ -80,8 +65,8 @@ homepage: http://github.com/wrimle/attachments
80
65
  licenses: []
81
66
 
82
67
  post_install_message:
83
- rdoc_options:
84
- - --charset=UTF-8
68
+ rdoc_options: []
69
+
85
70
  require_paths:
86
71
  - lib
87
72
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -89,26 +74,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
74
  requirements:
90
75
  - - ">="
91
76
  - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 0
95
77
  version: "0"
96
78
  required_rubygems_version: !ruby/object:Gem::Requirement
97
79
  none: false
98
80
  requirements:
99
81
  - - ">="
100
82
  - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
103
- - 0
104
83
  version: "0"
105
84
  requirements: []
106
85
 
107
86
  rubyforge_project:
108
- rubygems_version: 1.3.7
87
+ rubygems_version: 1.6.2
109
88
  signing_key:
110
89
  specification_version: 3
111
90
  summary: Extract mail parts of specified mime types.
112
91
  test_files:
113
- - test/test_attachments.rb
114
92
  - test/helper.rb
93
+ - test/test_attachments.rb
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC