From 90378b76eb055a24deca5fd705c7ae900ad6ce0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Garc=C3=ADa=20S=C3=A1ez?= Date: Tue, 12 Jan 2016 16:10:21 +0100 Subject: [PATCH] draft: remove attachment on failure Attachment creation can succeed and attachment mime file could fail This can happen, e.g., when the filename's length is close to the maximum allowed but your filesystem and then mime file will exceed that limit (because it has a prefix). --- SoObjects/Mailer/SOGoDraftObject.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/SoObjects/Mailer/SOGoDraftObject.m b/SoObjects/Mailer/SOGoDraftObject.m index 11da52e80..4fadcea70 100644 --- a/SoObjects/Mailer/SOGoDraftObject.m +++ b/SoObjects/Mailer/SOGoDraftObject.m @@ -24,6 +24,7 @@ #import #import #import +#import #import #import #import @@ -1094,7 +1095,7 @@ static NSString *userAgent = nil; - (NSException *) saveAttachment: (NSData *) _attach withMetadata: (NSDictionary *) metadata { - NSString *p, *name, *mimeType; + NSString *p, *pmime, *name, *mimeType; NSRange r; if (![_attach isNotNull]) { @@ -1126,14 +1127,13 @@ static NSString *userAgent = nil; mimeType = [metadata objectForKey: @"mimetype"]; if ([mimeType length] > 0) { - p = [self pathToAttachmentWithName: - [NSString stringWithFormat: @".%@.mime", name]]; - if (![[mimeType dataUsingEncoding: NSUTF8StringEncoding] - writeToFile: p atomically: YES]) - { - return [NSException exceptionWithHTTPStatus:500 /* Server Error */ - reason: @"Could not write attachment to draft!"]; - } + pmime = [self pathToAttachmentWithName: [NSString stringWithFormat: @".%@.mime", name]]; + if (![[mimeType dataUsingEncoding: NSUTF8StringEncoding] writeToFile: pmime atomically: YES]) + { + [[NSFileManager defaultManager] removeItemAtPath: p error: nil]; + return [NSException exceptionWithHTTPStatus: 500 /* Server Error */ + reason: @"Could not write attachment to draft!"]; + } } return nil; /* everything OK */