DraftObject: return more attachments attributes

pull/17/head
Francis Lachapelle 2013-12-18 14:12:29 -05:00
parent a8e3418a4c
commit 1a900b05d9
3 changed files with 64 additions and 44 deletions

View File

@ -40,10 +40,11 @@
@class NSData;
@class NSDictionary;
@class NSException;
@class NGImap4Envelope;
@class NGMimeMessage;
@class NSMutableDictionary;
@class NSString;
@class NGImap4Envelope;
@class NGMimeBodyPart;
@class NGMimeMessage;
@class SOGoMailObject;
@ -87,8 +88,10 @@
/* attachments */
- (NSArray *) fetchAttachmentNames;
- (NSArray *) fetchAttachmentAttrs;
- (BOOL) isValidAttachmentName: (NSString *) _name;
- (NGMimeBodyPart *) bodyPartForAttachmentWithName: (NSString *) _name;
- (NSString *) pathToAttachmentWithName: (NSString *) _name;
- (NSException *) saveAttachment: (NSData *) _attach
withMetadata: (NSDictionary *) metadata;
- (NSException *) deleteAttachmentWithName: (NSString *) _name;

View File

@ -559,19 +559,24 @@ static NSString *userAgent = nil;
}
folder = [imap4 imap4FolderNameForURL: [container imap4URL]];
result
= [client append: message toFolder: folder
withFlags: [NSArray arrayWithObjects: @"seen", @"draft", nil]];
result = [client append: message toFolder: folder
withFlags: [NSArray arrayWithObjects: @"seen", @"draft", nil]];
if ([[result objectForKey: @"result"] boolValue])
{
if (IMAP4ID > -1)
error = [imap4 markURLDeleted: [self imap4URL]];
IMAP4ID = [self IMAP4IDFromAppendResult: result];
if (imap4URL)
{
// Invalidate the IMAP message URL since the message ID has changed
[imap4URL release];
imap4URL = nil;
}
[self storeInfo];
}
else
error = [NSException exceptionWithHTTPStatus:500 /* Server Error */
reason: @"Failed to store message"];
error = [NSException exceptionWithHTTPStatus: 500 /* Server Error */
reason: [result objectForKey: @"reason"]];
return error;
}
@ -812,8 +817,7 @@ static NSString *userAgent = nil;
[sourceMail fetchCoreInfos];
[self _fetchAttachments: [sourceMail fetchFileAttachmentKeys]
fromMail: sourceMail];
[self _fetchAttachments: [sourceMail fetchFileAttachmentKeys] fromMail: sourceMail];
info = [NSMutableDictionary dictionaryWithCapacity: 16];
subject = [sourceMail subject];
if ([subject length] > 0)
@ -906,8 +910,7 @@ static NSString *userAgent = nil;
if ([[ud mailMessageForwarding] isEqualToString: @"inline"])
{
[self setText: [sourceMail contentForInlineForward]];
[self _fetchAttachments: [sourceMail fetchFileAttachmentKeys]
fromMail: sourceMail];
[self _fetchAttachments: [sourceMail fetchFileAttachmentKeys] fromMail: sourceMail];
}
else
{
@ -946,13 +949,15 @@ static NSString *userAgent = nil;
/* attachments */
- (NSArray *) fetchAttachmentNames
- (NSArray *) fetchAttachmentAttrs
{
NSMutableArray *ma;
NSFileManager *fm;
NSArray *files;
unsigned count, max;
NSString *filename;
NSDictionary *fileAttrs;
NGMimeBodyPart *bodyPart;
unsigned count, max;
fm = [NSFileManager defaultManager];
files = [fm directoryContentsAtPath: [self draftFolderPath]];
@ -963,7 +968,13 @@ static NSString *userAgent = nil;
{
filename = [files objectAtIndex: count];
if (![filename hasPrefix: @"."])
[ma addObject: filename];
{
fileAttrs = [fm fileAttributesAtPath: [self pathToAttachmentWithName: filename] traverseLink: YES];
bodyPart = [self bodyPartForAttachmentWithName: filename];
[ma addObject: [NSDictionary dictionaryWithObjectsAndKeys: filename, @"name",
[fileAttrs objectForKey: @"NSFileSize"], @"size",
bodyPart, @"part", nil]];
}
}
return ma;
@ -1309,18 +1320,18 @@ static NSString *userAgent = nil;
- (NSArray *) bodyPartsForAllAttachments
{
/* returns nil on error */
NSArray *names;
NSArray *attrs;
unsigned i, count;
NGMimeBodyPart *bodyPart;
NSMutableArray *bodyParts;
names = [self fetchAttachmentNames];
count = [names count];
attrs = [self fetchAttachmentAttrs];
count = [attrs count];
bodyParts = [NSMutableArray arrayWithCapacity: count];
for (i = 0; i < count; i++)
{
bodyPart = [self bodyPartForAttachmentWithName: [names objectAtIndex: i]];
bodyPart = [self bodyPartForAttachmentWithName: [[attrs objectAtIndex: i] objectForKey: @"name"]];
[bodyParts addObject: bodyPart];
}

View File

@ -23,6 +23,7 @@
#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSURL.h>
#import <NGObjWeb/WOApplication.h>
#import <NGObjWeb/WOResponse.h>
@ -277,6 +278,7 @@
- (void) _fetchFileAttachmentKey: (NSDictionary *) part
intoArray: (NSMutableArray *) keys
withPath: (NSString *) path
andPrefix: (NSString *) prefix
{
NSString *filename, *mimeType;
NSDictionary *currentFile;
@ -287,17 +289,7 @@
[part objectForKey: @"type"],
[part objectForKey: @"subtype"]];
if (filename)
{
currentFile = [NSDictionary dictionaryWithObjectsAndKeys:
filename, @"filename",
[mimeType lowercaseString], @"mimetype",
path, @"path",
[part objectForKey: @"encoding"], @"encoding", nil];
[keys addObject: currentFile];
}
else
{
if (!filename)
// We might end up here because of MUA that actually strips the
// Content-Disposition (and thus, the filename) when mails containing
// attachments have been forwarded. Thunderbird (2.x) does just that
@ -306,15 +298,19 @@
[mimeType hasPrefix: @"audio/"] ||
[mimeType hasPrefix: @"image/"] ||
[mimeType hasPrefix: @"video/"])
{
currentFile = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat: @"unkown_%@", path], @"filename",
[mimeType lowercaseString], @"mimetype",
path, @"path",
[part objectForKey: @"encoding"], @"encoding",
nil];
[keys addObject: currentFile];
}
filename = [NSString stringWithFormat: @"unknown_%@", path];
if (filename)
{
currentFile = [NSDictionary dictionaryWithObjectsAndKeys:
filename, @"filename",
[mimeType lowercaseString], @"mimetype",
path, @"path",
[part objectForKey: @"encoding"], @"encoding",
[part objectForKey:@ "size"], @"size",
[NSString stringWithFormat: @"%@/%@", prefix, [filename stringByEscapingURL]], @"url",
nil];
[keys addObject: currentFile];
}
}
@ -323,7 +319,8 @@
//
- (void) _fetchFileAttachmentKeysInPart: (NSDictionary *) part
intoArray: (NSMutableArray *) keys
withPath: (NSString *) path
withPath: (NSString *) path
andPrefix: (NSString *) prefix
{
NSMutableDictionary *currentPart;
NSString *newPath;
@ -343,15 +340,19 @@
else
newPath = [NSString stringWithFormat: @"%d", i];
[self _fetchFileAttachmentKeysInPart: currentPart
intoArray: keys
withPath: newPath];
intoArray: keys
withPath: newPath
andPrefix: [NSString stringWithFormat: @"%@/%i", prefix, i]];
}
}
else
{
if (!path)
path = @"1";
[self _fetchFileAttachmentKey: part intoArray: keys withPath: path];
[self _fetchFileAttachmentKey: part
intoArray: keys
withPath: path
andPrefix: prefix];
}
}
@ -361,11 +362,16 @@
#warning we might need to handle parts with a "name" attribute
- (NSArray *) fetchFileAttachmentKeys
{
NSString *prefix;
NSMutableArray *keys;
prefix = [[self soURL] absoluteString];
if ([prefix hasSuffix: @"/"])
prefix = [prefix substringToIndex: [prefix length] - 1];
keys = [NSMutableArray array];
[self _fetchFileAttachmentKeysInPart: [self bodyStructure]
intoArray: keys withPath: nil];
intoArray: keys withPath: nil andPrefix: prefix];
return keys;
}