Bug 1729632 - use original file extension for temporary nsmail attachments. r=benc a=wsmwk
authorMagnus Melin <mkmelin+mozilla@iki.fi>
Thu, 02 Dec 2021 12:33:29 +0200
changeset 43642 93430b46233346cfe0d40a97475e343ad580241a
parent 43641 b4724f1de4a945ca2cdcdc8694bb325a1305961e
child 43643 b4e36b071a3d18141b43ff2c38056303221f1c75
push id77
push userthunderbird@calypsoblue.org
push dateWed, 15 Dec 2021 22:34:59 +0000
treeherdercomm-esr91@15137e450ae8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbenc, wsmwk
bugs1729632
Bug 1729632 - use original file extension for temporary nsmail attachments. r=benc a=wsmwk Differential Revision: https://phabricator.services.mozilla.com/D132560
mailnews/mime/src/mimedrft.cpp
--- a/mailnews/mime/src/mimedrft.cpp
+++ b/mailnews/mime/src/mimedrft.cpp
@@ -1820,40 +1820,44 @@ int mime_decompose_file_init_fn(void* st
   }
 
   nsCOMPtr<nsIFile> tmpFile = nullptr;
   {
     // Let's build a temp file with an extension based on the content-type:
     // nsmail.<extension>
 
     nsAutoCString newAttachName("nsmail");
-    bool extensionAdded = false;
+    nsAutoCString fileExtension;
     // the content type may contain a charset. i.e. text/html; ISO-2022-JP...we
     // want to strip off the charset before we ask the mime service for a mime
     // info for this content type.
     nsAutoCString contentType(newAttachment->m_type);
     int32_t pos = contentType.FindChar(';');
     if (pos > 0) contentType.SetLength(pos);
-    nsresult rv = NS_OK;
-    nsCOMPtr<nsIMIMEService> mimeFinder(
-        do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
-    if (NS_SUCCEEDED(rv) && mimeFinder) {
-      nsAutoCString fileExtension;
-      rv = mimeFinder->GetPrimaryExtension(contentType, EmptyCString(),
-                                           fileExtension);
-
-      if (NS_SUCCEEDED(rv) && !fileExtension.IsEmpty()) {
-        newAttachName.Append('.');
-        newAttachName.Append(fileExtension);
-        extensionAdded = true;
+    int32_t extLoc = newAttachment->m_realName.RFindChar('.');
+    int32_t specLength = newAttachment->m_realName.Length();
+    // @see nsExternalHelperAppService::GetTypeFromURI()
+    if (extLoc != -1 && extLoc != specLength - 1 &&
+        // nothing over 20 chars long can be sanely considered an
+        // extension.... Dat dere would be just data.
+        specLength - extLoc < 20) {
+      fileExtension = Substring(newAttachment->m_realName, extLoc + 1);
+    } else {
+      nsCOMPtr<nsIMIMEService> mimeFinder(
+          do_GetService(NS_MIMESERVICE_CONTRACTID));
+      if (mimeFinder) {
+        mimeFinder->GetPrimaryExtension(contentType, ""_ns, fileExtension);
       }
     }
 
-    if (!extensionAdded) {
+    if (fileExtension.IsEmpty()) {
       newAttachName.AppendLiteral(".tmp");
+    } else {
+      newAttachName.Append('.');
+      newAttachName.Append(fileExtension);
     }
 
     nsMsgCreateTempFile(newAttachName.get(), getter_AddRefs(tmpFile));
   }
   nsresult rv;
 
   // This needs to be done so the attachment structure has a handle
   // on the temp file for this attachment...