Class: Mapi::Attachment

Inherits:
Item
  • Object
show all
Defined in:
lib/mapi/base.rb,
lib/mapi/convert/note-mime.rb,
lib/mapi/convert/note-tmail.rb

Overview

a general attachment class. is subclassed by Msg and Pst attachment classes

Direct Known Subclasses

Msg::Attachment, Pst::Attachment

Instance Attribute Summary

Attributes inherited from Item

#properties

Instance Method Summary collapse

Methods inherited from Item

#initialize

Constructor Details

This class inherits a constructor from Mapi::Item

Instance Method Details

#dataObject



22
23
24
# File 'lib/mapi/base.rb', line 22

def data
	@embedded_msg || @embedded_ole || props.attach_data
end

#filenameObject



18
19
20
# File 'lib/mapi/base.rb', line 18

def filename
	props.attach_long_filename || props.attach_filename
end

#inspectObject



37
38
39
40
41
# File 'lib/mapi/base.rb', line 37

def inspect
	"#<#{self.class.to_s[/\w+$/]}" +
		(filename ? " filename=#{filename.inspect}" : '') +
		(@embedded_ole ? " embedded_type=#{@embedded_ole.embedded_type.inspect}" : '') + ">"
end

#save(io) ⇒ Object

with new stream work, its possible to not have the whole thing in memory at one time, just to save an attachment

a = msg.attachments.first a.save open(File.basename(a.filename || ‘attachment’), ‘wb’)



31
32
33
34
35
# File 'lib/mapi/base.rb', line 31

def save io
	raise "can only save binary data blobs, not ole dirs" if @embedded_ole
	data.rewind
	io << data.read(8192) until data.eof?
end

#to_mimeObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/mapi/convert/note-mime.rb', line 225

def to_mime
	# TODO: smarter mime typing.
	mimetype = props.attach_mime_tag || 'application/octet-stream'
	mime = Mime.new "Content-Type: #{mimetype}\r\n\r\n"
	mime.headers['Content-Disposition'] = [%{attachment; filename="#{filename}"}]
	mime.headers['Content-Transfer-Encoding'] = ['base64']
	mime.headers['Content-Location'] = [props.attach_content_location] if props.attach_content_location
	mime.headers['Content-ID'] = [props.attach_content_id] if props.attach_content_id
	# data.to_s for now. data was nil for some reason.
	# perhaps it was a data object not correctly handled?
	# hmmm, have to use read here. that assumes that the data isa stream.
	# but if the attachment data is a string, then it won't work. possible?
	data_str = if @embedded_msg
		mime.headers['Content-Type'] = 'message/rfc822'
		# lets try making it not base64 for now
		mime.headers.delete 'Content-Transfer-Encoding'
		# not filename. rather name, or something else right?
		# maybe it should be inline?? i forget attach_method / access meaning
		mime.headers['Content-Disposition'] = [%{attachment; filename="#{@embedded_msg.subject}"}]
		@embedded_msg.to_mime.to_s
	elsif @embedded_ole
		# kind of hacky
		io = StringIO.new
		Ole::Storage.new io do |ole|
			ole.root.type = :dir
			Ole::Storage::Dirent.copy @embedded_ole, ole.root
		end
		io.string
	else
		# FIXME: shouldn't be required
		data.read.to_s rescue ''
	end
	mime.body.replace @embedded_msg ? data_str : Base64.encode64(data_str).gsub(/\n/, "\r\n")
	mime
end

#to_tmailObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/mapi/convert/note-tmail.rb', line 237

def to_tmail
	# TODO: smarter mime typing.
	mimetype = props.attach_mime_tag || 'application/octet-stream'
	part = TMail::Mail.parse "Content-Type: #{mimetype}\r\n\r\n"
	part['Content-Disposition'] = %{attachment; filename="#{filename}"}
	part['Content-Transfer-Encoding'] = 'base64'
	part['Content-Location'] = props.attach_content_location if props.attach_content_location
	part['Content-ID'] = props.attach_content_id if props.attach_content_id
	# data.to_s for now. data was nil for some reason.
	# perhaps it was a data object not correctly handled?
	# hmmm, have to use read here. that assumes that the data isa stream.
	# but if the attachment data is a string, then it won't work. possible?
	data_str = if @embedded_msg
		raise NotImplementedError
		mime.headers['Content-Type'] = 'message/rfc822'
		# lets try making it not base64 for now
		mime.headers.delete 'Content-Transfer-Encoding'
		# not filename. rather name, or something else right?
		# maybe it should be inline?? i forget attach_method / access meaning
		mime.headers['Content-Disposition'] = [%{attachment; filename="#{@embedded_msg.subject}"}]
		@embedded_msg.to_mime.to_s
	elsif @embedded_ole
		raise NotImplementedError
		# kind of hacky
		io = StringIO.new
		Ole::Storage.new io do |ole|
			ole.root.type = :dir
			Ole::Storage::Dirent.copy @embedded_ole, ole.root
		end
		io.string
	else
		data.read.to_s
	end
	part.body = @embedded_msg ? data_str : Base64.encode64(data_str).gsub(/\n/, "\r\n")
	part
end