}
else if (!webPath.startsWith("/")) {
webPath = "/" + webPath;
}
ERFileAttachment attachment = ERFileAttachment.createERFileAttachment(editingContext, Boolean.TRUE, new NSTimestamp(), mimeType, recommendedFileName, Boolean.valueOf(proxy), Integer.valueOf((int) uploadedFile.length()), webPath);
if (delegate() != null) {
delegate().attachmentCreated(this, attachment);
}
try {
webPath = ERAttachmentProcessor._parsePathTemplate(attachment, webPath, recommendedFileName);
filesystemPath = ERAttachmentProcessor._parsePathTemplate(attachment, filesystemPath, recommendedFileName);
File desiredFilesystemPath = new File(filesystemPath);
File actualFilesystemPath = ERXFileUtilities.reserveUniqueFile(desiredFilesystemPath, overwrite);
ERXFileUtilities.copyFileToFile(uploadedFile, actualFilesystemPath, pendingDelete, true);
String desiredFileName = desiredFilesystemPath.getName();
String actualFileName = actualFilesystemPath.getName();
// in case the name was not unique and changed, we need to update webPath ...
webPath = webPath.replaceAll("\\Q" + desiredFileName + "\\E$", actualFileName);
attachment.setWebPath(webPath);
attachment.setFilesystemPath(actualFilesystemPath.getAbsolutePath());
if (delegate() != null) {
delegate().attachmentAvailable(this, attachment);
}
}
catch (IOException e) {
attachment.delete();
throw e;
}
catch (RuntimeException e) {
attachment.delete();
throw e;
}
return attachment;
}