From 1a900b05d9cb6def48849209cde72225ab65fcff Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Wed, 18 Dec 2013 14:12:29 -0500 Subject: [PATCH] DraftObject: return more attachments attributes --- SoObjects/Mailer/SOGoDraftObject.h | 9 ++-- SoObjects/Mailer/SOGoDraftObject.m | 43 ++++++++++++------- SoObjects/Mailer/SOGoMailObject+Draft.m | 56 ++++++++++++++----------- 3 files changed, 64 insertions(+), 44 deletions(-) diff --git a/SoObjects/Mailer/SOGoDraftObject.h b/SoObjects/Mailer/SOGoDraftObject.h index a53d2a9ba..94c5d2888 100644 --- a/SoObjects/Mailer/SOGoDraftObject.h +++ b/SoObjects/Mailer/SOGoDraftObject.h @@ -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; diff --git a/SoObjects/Mailer/SOGoDraftObject.m b/SoObjects/Mailer/SOGoDraftObject.m index 2e0da59d7..16246aec5 100644 --- a/SoObjects/Mailer/SOGoDraftObject.m +++ b/SoObjects/Mailer/SOGoDraftObject.m @@ -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]; } diff --git a/SoObjects/Mailer/SOGoMailObject+Draft.m b/SoObjects/Mailer/SOGoMailObject+Draft.m index 121983c8a..4b61e2b01 100644 --- a/SoObjects/Mailer/SOGoMailObject+Draft.m +++ b/SoObjects/Mailer/SOGoMailObject+Draft.m @@ -23,6 +23,7 @@ #import #import #import +#import #import #import @@ -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; }