Skip to content

Commit

Permalink
fix(mail): use unique names for attachments
Browse files Browse the repository at this point in the history
Fixes #5086
  • Loading branch information
cgx committed Jul 23, 2020
1 parent d549c29 commit 9c391b8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
4 changes: 2 additions & 2 deletions 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.
Expand Down Expand Up @@ -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 */
Expand Down
47 changes: 31 additions & 16 deletions 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.
Expand Down Expand Up @@ -838,7 +838,7 @@ - (void) _fillInReplyAddresses: (NSMutableDictionary *) _info
//
- (void) _fetchAttachmentsFromMail: (SOGoMailObject *) sourceMail
{
NSDictionary *currentInfo;
NSMutableDictionary *currentInfo;
NSArray *attachments;

unsigned int max, count;
Expand Down Expand Up @@ -878,12 +878,12 @@ - (void) _fileAttachmentsFromPart: (id) thePart

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];
}
Expand Down Expand Up @@ -1041,8 +1041,7 @@ - (void) fetchMailForForwarding: (SOGoMailObject *) sourceMail
{
BOOL fromSentMailbox;
NGImap4Envelope *sourceEnvelope;
NSDictionary *attachment;
NSMutableDictionary *info;
NSMutableDictionary *attachment, *info;
NSString *signature, *nl, *space;
SOGoUserDefaults *ud;

Expand Down Expand Up @@ -1089,10 +1088,10 @@ - (void) fetchMailForForwarding: (SOGoMailObject *) sourceMail
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];
}
Expand Down Expand Up @@ -1170,9 +1169,11 @@ - (NSString *) pathToAttachmentWithName: (NSString *) _name
* 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])
{
Expand All @@ -1187,7 +1188,21 @@ - (NSException *) saveAttachment: (NSData *) _attach
}

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])
{
Expand Down Expand Up @@ -2074,7 +2089,7 @@ - (NSException *) sendMailAndCopyToSent: (BOOL) copyToSent
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
Expand Down
18 changes: 9 additions & 9 deletions UI/MailerUI/UIxMailEditor.m
Expand Up @@ -525,10 +525,9 @@ - (NSString *) _fixedFilename: (NSString *) filename
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;
Expand All @@ -547,11 +546,11 @@ - (NSDictionary *) _scanAttachmentFilenamesInRequest: (id) httpBody
{
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]];
}
}
Expand All @@ -564,7 +563,8 @@ - (NSException *) _saveAttachments
NSException *error;
WORequest *request;
NSEnumerator *allAttachments;
NSDictionary *attrs, *filenames;
NSMutableDictionary *attrs;
NSDictionary *filenames;
id httpBody;
SOGoDraftObject *co;

Expand Down
2 changes: 1 addition & 1 deletion UI/Templates/MailerUI/UIxMailViewTemplate.wox
Expand Up @@ -330,7 +330,7 @@
<div class="msg-body">
<div 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">
<div class="md-flex sg-mail-part"
tabindex="-1"
Expand Down
1 change: 1 addition & 0 deletions UI/WebServerResources/js/Mailer/MessageEditorController.js
Expand Up @@ -138,6 +138,7 @@
vm.message.$setUID(response.uid);
vm.message.$reload();
item.inlineUrl = response.lastAttachmentAttrs[0].url;
item.file.name = response.lastAttachmentAttrs[0].filename;
//console.debug(item); console.debug('success = ' + JSON.stringify(response, undefined, 2));
},
onCancelItem: function(item, response, status, headers) {
Expand Down

0 comments on commit 9c391b8

Please sign in to comment.