Added getName() to retrieve attachment filename.

This commit is contained in:
Vincent Richard 2005-07-15 09:51:55 +00:00
parent 681297e10b
commit b8b096c44f
6 changed files with 67 additions and 8 deletions

View File

@ -2,6 +2,13 @@
VERSION 0.7.2cvs VERSION 0.7.2cvs
================ ================
2005-07-15 Vincent Richard <vincent@vincent-richard.net>
* *attachment, messageParser: added a getName() parameter to retrieve
the attachment filename either from the "filename" parameter of the
"Content-Disposition" field, or from the "name" parameter of the
"Content-Type" field (if available).
2005-07-13 Vincent Richard <vincent@vincent-richard.net> 2005-07-13 Vincent Richard <vincent@vincent-richard.net>
* All files: added reference counting and smart pointers to simplify the * All files: added reference counting and smart pointers to simplify the

View File

@ -50,6 +50,7 @@ int main()
const vmime::attachment& att = *mp.getAttachmentAt(i); const vmime::attachment& att = *mp.getAttachmentAt(i);
// Media type (content type) is in "att.getType()" // Media type (content type) is in "att.getType()"
// Name is in "att.getName()"
// Description is in "att.getDescription()" // Description is in "att.getDescription()"
// Data is in "att.getData()" // Data is in "att.getData()"
} }

View File

@ -31,23 +31,24 @@ defaultAttachment::defaultAttachment()
defaultAttachment::defaultAttachment(ref <contentHandler> data, defaultAttachment::defaultAttachment(ref <contentHandler> data,
const encoding& enc, const mediaType& type, const text& desc) const encoding& enc, const mediaType& type, const text& desc, const word& name)
: m_type(type), m_desc(desc), m_data(data), m_encoding(enc) : m_type(type), m_desc(desc), m_data(data), m_encoding(enc), m_name(name)
{ {
} }
defaultAttachment::defaultAttachment(ref <contentHandler> data, defaultAttachment::defaultAttachment(ref <contentHandler> data,
const mediaType& type, const text& desc) const mediaType& type, const text& desc, const word& name)
: m_type(type), m_desc(desc), m_data(data), : m_type(type), m_desc(desc), m_data(data),
m_encoding(encoding::decide(data)) m_encoding(encoding::decide(data)), m_name(name)
{ {
} }
defaultAttachment::defaultAttachment(const defaultAttachment& attach) defaultAttachment::defaultAttachment(const defaultAttachment& attach)
: attachment(), m_type(attach.m_type), m_desc(attach.m_desc), : attachment(), m_type(attach.m_type), m_desc(attach.m_desc),
m_data(attach.m_data->clone().dynamicCast <contentHandler>()), m_encoding(attach.m_encoding) m_data(attach.m_data->clone().dynamicCast <contentHandler>()),
m_encoding(attach.m_encoding), m_name(attach.m_name)
{ {
} }
@ -61,6 +62,7 @@ defaultAttachment& defaultAttachment::operator=(const defaultAttachment& attach)
{ {
m_type = attach.m_type; m_type = attach.m_type;
m_desc = attach.m_desc; m_desc = attach.m_desc;
m_name = attach.m_name;
m_data = attach.m_data->clone().dynamicCast <contentHandler>(); m_data = attach.m_data->clone().dynamicCast <contentHandler>();
m_encoding = attach.m_encoding; m_encoding = attach.m_encoding;
@ -103,6 +105,12 @@ const text& defaultAttachment::getDescription() const
} }
const word& defaultAttachment::getName() const
{
return (m_name);
}
const ref <const contentHandler> defaultAttachment::getData() const const ref <const contentHandler> defaultAttachment::getData() const
{ {
return (m_data); return (m_data);

View File

@ -153,6 +153,7 @@ void messageParser::findAttachments(const bodyPart& part)
if (isAttachment) if (isAttachment)
{ {
// Determine the media type of this attachment // Determine the media type of this attachment
const contentTypeField* contTypeField = NULL;
mediaType type; mediaType type;
try try
@ -161,6 +162,8 @@ void messageParser::findAttachments(const bodyPart& part)
(*hdr.findField(fields::CONTENT_TYPE)); (*hdr.findField(fields::CONTENT_TYPE));
type = ctf.getValue(); type = ctf.getValue();
contTypeField = &ctf;
} }
catch (exceptions::no_such_field) catch (exceptions::no_such_field)
{ {
@ -184,10 +187,43 @@ void messageParser::findAttachments(const bodyPart& part)
// No description available. // No description available.
} }
// Get the name/filename (if available)
word name;
// -- try the 'filename' parameter of 'Content-Disposition' field
if (contentDispField != NULL)
{
try
{
name = contentDispField->getFilename();
}
catch (exceptions::no_such_parameter)
{
// No 'filename' parameter
}
}
// -- try the 'name' parameter of 'Content-Type' field
if (name.getBuffer().empty() && contTypeField != NULL)
{
try
{
ref <defaultParameter> prm = contTypeField->
findParameter("name").dynamicCast <defaultParameter>();
if (prm != NULL)
name = prm->getValue();
}
catch (exceptions::no_such_parameter)
{
// No attachment name available.
}
}
// Construct the attachment object // Construct the attachment object
ref <attachment> attach = vmime::create <defaultAttachment> ref <attachment> attach = vmime::create <defaultAttachment>
(bdy.getContents()->clone().dynamicCast <contentHandler>(), (bdy.getContents()->clone().dynamicCast <contentHandler>(),
bdy.getEncoding(), type, description); bdy.getEncoding(), type, description, name);
if (contentDispField != NULL) if (contentDispField != NULL)
{ {

View File

@ -60,6 +60,11 @@ public:
*/ */
virtual const text& getDescription() const = 0; virtual const text& getDescription() const = 0;
/** Return the name of this attachment.
* @return attachment name
*/
virtual const word& getName() const = 0;
/** Return the data contained in this attachment. /** Return the data contained in this attachment.
* @return attachment data * @return attachment data
*/ */

View File

@ -41,8 +41,8 @@ protected:
public: public:
defaultAttachment(ref <contentHandler> data, const encoding& enc, const mediaType& type, const text& desc = NULL_TEXT); defaultAttachment(ref <contentHandler> data, const encoding& enc, const mediaType& type, const text& desc = NULL_TEXT, const word& name = NULL_TEXT);
defaultAttachment(ref <contentHandler> data, const mediaType& type, const text& desc = NULL_TEXT); defaultAttachment(ref <contentHandler> data, const mediaType& type, const text& desc = NULL_TEXT, const word& name = NULL_TEXT);
defaultAttachment(const defaultAttachment& attach); defaultAttachment(const defaultAttachment& attach);
~defaultAttachment(); ~defaultAttachment();
@ -51,6 +51,7 @@ public:
const mediaType& getType() const; const mediaType& getType() const;
const text& getDescription() const; const text& getDescription() const;
const word& getName() const;
const ref <const contentHandler> getData() const; const ref <const contentHandler> getData() const;
const encoding& getEncoding() const; const encoding& getEncoding() const;
@ -60,6 +61,7 @@ protected:
text m_desc; // Description (eg. "The image you requested") text m_desc; // Description (eg. "The image you requested")
ref <contentHandler> m_data; // Attachment data (eg. the file contents) ref <contentHandler> m_data; // Attachment data (eg. the file contents)
encoding m_encoding; // Encoding encoding m_encoding; // Encoding
word m_name; // Name/filename (eg. "sunset.jpg")
private: private: