From 9c391b8d8df97c766033f74857b5fa34076746fa Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 23 Jul 2020 12:56:35 -0400 Subject: [PATCH] fix(mail): use unique names for attachments Fixes #5086 --- SoObjects/Mailer/SOGoDraftObject.h | 4 +- SoObjects/Mailer/SOGoDraftObject.m | 47 ++++++++++++------- UI/MailerUI/UIxMailEditor.m | 18 +++---- UI/Templates/MailerUI/UIxMailViewTemplate.wox | 2 +- .../js/Mailer/MessageEditorController.js | 1 + 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/SoObjects/Mailer/SOGoDraftObject.h b/SoObjects/Mailer/SOGoDraftObject.h index d17d093a9..3a58727cc 100644 --- a/SoObjects/Mailer/SOGoDraftObject.h +++ b/SoObjects/Mailer/SOGoDraftObject.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2007-2014 Inverse inc. + Copyright (C) 2007-2020 Inverse inc. Copyright (C) 2004-2005 SKYRIX Software AG This file is part of SOGo. @@ -112,7 +112,7 @@ - (NGMimeBodyPart *) bodyPartForAttachmentWithName: (NSString *) _name; - (NSString *) pathToAttachmentWithName: (NSString *) _name; - (NSException *) saveAttachment: (NSData *) _attach - withMetadata: (NSDictionary *) metadata; + withMetadata: (NSMutableDictionary *) metadata; - (NSException *) deleteAttachmentWithName: (NSString *) _name; /* NGMime representations */ diff --git a/SoObjects/Mailer/SOGoDraftObject.m b/SoObjects/Mailer/SOGoDraftObject.m index 77014abb5..f89a3532b 100644 --- a/SoObjects/Mailer/SOGoDraftObject.m +++ b/SoObjects/Mailer/SOGoDraftObject.m @@ -1,5 +1,5 @@ /* - Copyright (C) 2007-2018 Inverse inc. + Copyright (C) 2007-2020 Inverse inc. Copyright (C) 2004-2005 SKYRIX Software AG This file is part of SOGo. @@ -838,7 +838,7 @@ static NSString *userAgent = nil; // - (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail { - NSDictionary *currentInfo; + NSMutableDictionary *currentInfo; NSArray *attachments; unsigned int max, count; @@ -878,12 +878,12 @@ static NSString *userAgent = nil; if (filename) { - NSDictionary *currentInfo; + NSMutableDictionary *currentInfo; - currentInfo = [NSDictionary dictionaryWithObjectsAndKeys: - filename, @"filename", - mimeType, @"mimetype", - nil]; + currentInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys: + filename, @"filename", + mimeType, @"mimetype", + nil]; [self saveAttachment: body withMetadata: currentInfo]; } @@ -1041,8 +1041,7 @@ static NSString *userAgent = nil; { BOOL fromSentMailbox; NGImap4Envelope *sourceEnvelope; - NSDictionary *attachment; - NSMutableDictionary *info; + NSMutableDictionary *attachment, *info; NSString *signature, *nl, *space; SOGoUserDefaults *ud; @@ -1089,10 +1088,10 @@ static NSString *userAgent = nil; space = (isHTML ? @" " : @" "); [self setText: [NSString stringWithFormat: @"%@%@--%@%@%@", nl, nl, space, nl, signature]]; } - attachment = [NSDictionary dictionaryWithObjectsAndKeys: - [sourceMail filenameForForward], @"filename", - @"message/rfc822", @"mimetype", - nil]; + attachment = [NSMutableDictionary dictionaryWithObjectsAndKeys: + [sourceMail filenameForForward], @"filename", + @"message/rfc822", @"mimetype", + nil]; [self saveAttachment: [sourceMail content] withMetadata: attachment]; } @@ -1170,9 +1169,11 @@ static NSString *userAgent = nil; * file with its mime type. */ - (NSException *) saveAttachment: (NSData *) _attach - withMetadata: (NSDictionary *) metadata + withMetadata: (NSMutableDictionary *) metadata { - NSString *p, *pmime, *name, *mimeType; + NSFileManager *fm; + NSString *p, *pmime, *name, *baseName, *extension, *mimeType; + int i; if (![_attach isNotNull]) { @@ -1187,7 +1188,21 @@ static NSString *userAgent = nil; } name = [[metadata objectForKey: @"filename"] asSafeFilename]; + baseName = [name stringByDeletingPathExtension]; + extension = [name pathExtension]; + fm = [NSFileManager defaultManager]; p = [self pathToAttachmentWithName: name]; + i = 1; + + while ([fm isReadableFileAtPath: p]) + { + name = [NSString stringWithFormat: @"%@-%x", baseName, i]; + if ([extension length]) + name = [NSString stringWithFormat: @"%@.%@", name, extension]; + p = [self pathToAttachmentWithName: name]; + [metadata setObject: name forKey: @"filename"]; + i++; + } if (![_attach writeToFile: p atomically: YES]) { @@ -2074,7 +2089,7 @@ static NSString *userAgent = nil; if ([[context activeUser] hasEmail: recipient]) message = messageForSent = [self mimeMessageForRecipient: nil]; else - message = [self mimeMessageForRecipient: recipient];; + message = [self mimeMessageForRecipient: recipient]; if (!message) return [NSException exceptionWithHTTPStatus: 500 diff --git a/UI/MailerUI/UIxMailEditor.m b/UI/MailerUI/UIxMailEditor.m index 35f4a2c6b..a359080c0 100644 --- a/UI/MailerUI/UIxMailEditor.m +++ b/UI/MailerUI/UIxMailEditor.m @@ -525,10 +525,9 @@ static NSArray *infoKeys = nil; return newFilename; } -- (NSDictionary *) _scanAttachmentFilenamesInRequest: (id) httpBody +- (NSMutableDictionary *) _scanAttachmentFilenamesInRequest: (id) httpBody { - NSMutableDictionary *files; - NSDictionary *file; + NSMutableDictionary *files, *file; NSArray *parts; unsigned int count, max; NGMimeBodyPart *part; @@ -547,11 +546,11 @@ static NSArray *infoKeys = nil; { mimeType = [(NGMimeType *)[part headerForKey: @"content-type"] stringValue]; filename = [self _fixedFilename: [header filename]]; - file = [NSDictionary dictionaryWithObjectsAndKeys: - filename, @"filename", - mimeType, @"mimetype", - [part body], @"body", - nil]; + file = [NSMutableDictionary dictionaryWithObjectsAndKeys: + filename, @"filename", + mimeType, @"mimetype", + [part body], @"body", + nil]; [files setObject: file forKey: [NSString stringWithFormat: @"%@_%@", [header name], filename]]; } } @@ -564,7 +563,8 @@ static NSArray *infoKeys = nil; NSException *error; WORequest *request; NSEnumerator *allAttachments; - NSDictionary *attrs, *filenames; + NSMutableDictionary *attrs; + NSDictionary *filenames; id httpBody; SOGoDraftObject *co; diff --git a/UI/Templates/MailerUI/UIxMailViewTemplate.wox b/UI/Templates/MailerUI/UIxMailViewTemplate.wox index 0cf5ea28d..a97287c8d 100644 --- a/UI/Templates/MailerUI/UIxMailViewTemplate.wox +++ b/UI/Templates/MailerUI/UIxMailViewTemplate.wox @@ -330,7 +330,7 @@