fix(mail): use unique names for attachments

Fixes #5086
pull/285/head
Francis Lachapelle 2020-07-23 12:56:35 -04:00
parent d549c296ae
commit 9c391b8d8d
5 changed files with 44 additions and 28 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007-2014 Inverse inc. Copyright (C) 2007-2020 Inverse inc.
Copyright (C) 2004-2005 SKYRIX Software AG Copyright (C) 2004-2005 SKYRIX Software AG
This file is part of SOGo. This file is part of SOGo.
@ -112,7 +112,7 @@
- (NGMimeBodyPart *) bodyPartForAttachmentWithName: (NSString *) _name; - (NGMimeBodyPart *) bodyPartForAttachmentWithName: (NSString *) _name;
- (NSString *) pathToAttachmentWithName: (NSString *) _name; - (NSString *) pathToAttachmentWithName: (NSString *) _name;
- (NSException *) saveAttachment: (NSData *) _attach - (NSException *) saveAttachment: (NSData *) _attach
withMetadata: (NSDictionary *) metadata; withMetadata: (NSMutableDictionary *) metadata;
- (NSException *) deleteAttachmentWithName: (NSString *) _name; - (NSException *) deleteAttachmentWithName: (NSString *) _name;
/* NGMime representations */ /* NGMime representations */

View File

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007-2018 Inverse inc. Copyright (C) 2007-2020 Inverse inc.
Copyright (C) 2004-2005 SKYRIX Software AG Copyright (C) 2004-2005 SKYRIX Software AG
This file is part of SOGo. This file is part of SOGo.
@ -838,7 +838,7 @@ static NSString *userAgent = nil;
// //
- (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail - (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail
{ {
NSDictionary *currentInfo; NSMutableDictionary *currentInfo;
NSArray *attachments; NSArray *attachments;
unsigned int max, count; unsigned int max, count;
@ -878,12 +878,12 @@ static NSString *userAgent = nil;
if (filename) if (filename)
{ {
NSDictionary *currentInfo; NSMutableDictionary *currentInfo;
currentInfo = [NSDictionary dictionaryWithObjectsAndKeys: currentInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
filename, @"filename", filename, @"filename",
mimeType, @"mimetype", mimeType, @"mimetype",
nil]; nil];
[self saveAttachment: body [self saveAttachment: body
withMetadata: currentInfo]; withMetadata: currentInfo];
} }
@ -1041,8 +1041,7 @@ static NSString *userAgent = nil;
{ {
BOOL fromSentMailbox; BOOL fromSentMailbox;
NGImap4Envelope *sourceEnvelope; NGImap4Envelope *sourceEnvelope;
NSDictionary *attachment; NSMutableDictionary *attachment, *info;
NSMutableDictionary *info;
NSString *signature, *nl, *space; NSString *signature, *nl, *space;
SOGoUserDefaults *ud; SOGoUserDefaults *ud;
@ -1089,10 +1088,10 @@ static NSString *userAgent = nil;
space = (isHTML ? @" " : @" "); space = (isHTML ? @" " : @" ");
[self setText: [NSString stringWithFormat: @"%@%@--%@%@%@", nl, nl, space, nl, signature]]; [self setText: [NSString stringWithFormat: @"%@%@--%@%@%@", nl, nl, space, nl, signature]];
} }
attachment = [NSDictionary dictionaryWithObjectsAndKeys: attachment = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[sourceMail filenameForForward], @"filename", [sourceMail filenameForForward], @"filename",
@"message/rfc822", @"mimetype", @"message/rfc822", @"mimetype",
nil]; nil];
[self saveAttachment: [sourceMail content] [self saveAttachment: [sourceMail content]
withMetadata: attachment]; withMetadata: attachment];
} }
@ -1170,9 +1169,11 @@ static NSString *userAgent = nil;
* file with its mime type. * file with its mime type.
*/ */
- (NSException *) saveAttachment: (NSData *) _attach - (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]) if (![_attach isNotNull])
{ {
@ -1187,7 +1188,21 @@ static NSString *userAgent = nil;
} }
name = [[metadata objectForKey: @"filename"] asSafeFilename]; name = [[metadata objectForKey: @"filename"] asSafeFilename];
baseName = [name stringByDeletingPathExtension];
extension = [name pathExtension];
fm = [NSFileManager defaultManager];
p = [self pathToAttachmentWithName: name]; 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]) if (![_attach writeToFile: p atomically: YES])
{ {
@ -2074,7 +2089,7 @@ static NSString *userAgent = nil;
if ([[context activeUser] hasEmail: recipient]) if ([[context activeUser] hasEmail: recipient])
message = messageForSent = [self mimeMessageForRecipient: nil]; message = messageForSent = [self mimeMessageForRecipient: nil];
else else
message = [self mimeMessageForRecipient: recipient];; message = [self mimeMessageForRecipient: recipient];
if (!message) if (!message)
return [NSException exceptionWithHTTPStatus: 500 return [NSException exceptionWithHTTPStatus: 500

View File

@ -525,10 +525,9 @@ static NSArray *infoKeys = nil;
return newFilename; return newFilename;
} }
- (NSDictionary *) _scanAttachmentFilenamesInRequest: (id) httpBody - (NSMutableDictionary *) _scanAttachmentFilenamesInRequest: (id) httpBody
{ {
NSMutableDictionary *files; NSMutableDictionary *files, *file;
NSDictionary *file;
NSArray *parts; NSArray *parts;
unsigned int count, max; unsigned int count, max;
NGMimeBodyPart *part; NGMimeBodyPart *part;
@ -547,11 +546,11 @@ static NSArray *infoKeys = nil;
{ {
mimeType = [(NGMimeType *)[part headerForKey: @"content-type"] stringValue]; mimeType = [(NGMimeType *)[part headerForKey: @"content-type"] stringValue];
filename = [self _fixedFilename: [header filename]]; filename = [self _fixedFilename: [header filename]];
file = [NSDictionary dictionaryWithObjectsAndKeys: file = [NSMutableDictionary dictionaryWithObjectsAndKeys:
filename, @"filename", filename, @"filename",
mimeType, @"mimetype", mimeType, @"mimetype",
[part body], @"body", [part body], @"body",
nil]; nil];
[files setObject: file forKey: [NSString stringWithFormat: @"%@_%@", [header name], filename]]; [files setObject: file forKey: [NSString stringWithFormat: @"%@_%@", [header name], filename]];
} }
} }
@ -564,7 +563,8 @@ static NSArray *infoKeys = nil;
NSException *error; NSException *error;
WORequest *request; WORequest *request;
NSEnumerator *allAttachments; NSEnumerator *allAttachments;
NSDictionary *attrs, *filenames; NSMutableDictionary *attrs;
NSDictionary *filenames;
id httpBody; id httpBody;
SOGoDraftObject *co; SOGoDraftObject *co;

View File

@ -330,7 +330,7 @@
<div class="msg-body"> <div class="msg-body">
<div layout="row" layout-wrap="layout-wrap"> <div layout="row" layout-wrap="layout-wrap">
<div class="mailer_mailcontent" layout="row" layout-wrap="layout-wrap" <div class="mailer_mailcontent" layout="row" layout-wrap="layout-wrap"
ng-repeat="part in viewer.message.$content() track by $index" ng-repeat="part in viewer.message.$content()"
ng-class="::part.msgclass"> ng-class="::part.msgclass">
<div class="md-flex sg-mail-part" <div class="md-flex sg-mail-part"
tabindex="-1" tabindex="-1"

View File

@ -138,6 +138,7 @@
vm.message.$setUID(response.uid); vm.message.$setUID(response.uid);
vm.message.$reload(); vm.message.$reload();
item.inlineUrl = response.lastAttachmentAttrs[0].url; item.inlineUrl = response.lastAttachmentAttrs[0].url;
item.file.name = response.lastAttachmentAttrs[0].filename;
//console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2)); //console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2));
}, },
onCancelItem: function(item, response, status, headers) { onCancelItem: function(item, response, status, headers) {