2009-02-18 02:59:31 +01:00
|
|
|
module CouchRest
|
2010-06-20 22:01:11 +02:00
|
|
|
module Model
|
2009-02-18 02:59:31 +01:00
|
|
|
module ExtendedAttachments
|
|
|
|
|
2010-05-12 23:43:17 +02:00
|
|
|
# Add a file attachment to the current document. Expects
|
|
|
|
# :file and :name to be included in the arguments.
|
2009-02-18 02:59:31 +01:00
|
|
|
def create_attachment(args={})
|
|
|
|
raise ArgumentError unless args[:file] && args[:name]
|
|
|
|
return if has_attachment?(args[:name])
|
|
|
|
set_attachment_attr(args)
|
|
|
|
rescue ArgumentError => e
|
|
|
|
raise ArgumentError, 'You must specify :file and :name'
|
|
|
|
end
|
2010-08-04 04:57:51 +02:00
|
|
|
|
|
|
|
# return all attachments
|
|
|
|
def attachments
|
|
|
|
self['_attachments'] ||= {}
|
|
|
|
end
|
2009-02-18 02:59:31 +01:00
|
|
|
|
|
|
|
# reads the data from an attachment
|
|
|
|
def read_attachment(attachment_name)
|
2010-01-29 20:00:06 +01:00
|
|
|
database.fetch_attachment(self, attachment_name)
|
2009-02-18 02:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# modifies a file attachment on the current doc
|
|
|
|
def update_attachment(args={})
|
|
|
|
raise ArgumentError unless args[:file] && args[:name]
|
|
|
|
return unless has_attachment?(args[:name])
|
|
|
|
delete_attachment(args[:name])
|
|
|
|
set_attachment_attr(args)
|
|
|
|
rescue ArgumentError => e
|
|
|
|
raise ArgumentError, 'You must specify :file and :name'
|
|
|
|
end
|
|
|
|
|
|
|
|
# deletes a file attachment from the current doc
|
|
|
|
def delete_attachment(attachment_name)
|
2010-08-04 04:57:51 +02:00
|
|
|
return unless attachments
|
|
|
|
attachments.delete attachment_name
|
2009-02-18 02:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns true if attachment_name exists
|
|
|
|
def has_attachment?(attachment_name)
|
2010-08-04 04:57:51 +02:00
|
|
|
!!(attachments && attachments[attachment_name] && !attachments[attachment_name].empty?)
|
2009-02-18 02:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns URL to fetch the attachment from
|
|
|
|
def attachment_url(attachment_name)
|
|
|
|
return unless has_attachment?(attachment_name)
|
|
|
|
"#{database.root}/#{self.id}/#{attachment_name}"
|
|
|
|
end
|
|
|
|
|
2009-06-08 03:51:31 +02:00
|
|
|
# returns URI to fetch the attachment from
|
|
|
|
def attachment_uri(attachment_name)
|
|
|
|
return unless has_attachment?(attachment_name)
|
|
|
|
"#{database.uri}/#{self.id}/#{attachment_name}"
|
|
|
|
end
|
|
|
|
|
2009-02-18 02:59:31 +01:00
|
|
|
private
|
|
|
|
|
2010-05-12 23:43:17 +02:00
|
|
|
def get_mime_type(path)
|
2010-06-08 00:17:43 +02:00
|
|
|
return nil if path.nil?
|
2010-05-12 23:43:17 +02:00
|
|
|
type = ::MIME::Types.type_for(path)
|
|
|
|
type.empty? ? nil : type.first.content_type
|
2009-02-18 02:59:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_attachment_attr(args)
|
2010-05-12 23:43:17 +02:00
|
|
|
content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file].path)
|
|
|
|
content_type ||= (get_mime_type(args[:name]) || 'text/plain')
|
2010-08-04 04:57:51 +02:00
|
|
|
attachments[args[:name]] = {
|
2009-08-06 22:04:17 +02:00
|
|
|
'content_type' => content_type,
|
2009-11-05 00:58:14 +01:00
|
|
|
'data' => args[:file].read
|
2009-02-18 02:59:31 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
end # module ExtendedAttachments
|
|
|
|
end
|
2009-11-05 00:58:14 +01:00
|
|
|
end
|