From 8a32246e369e38a4c33fcb15336ce409b9a09123 Mon Sep 17 00:00:00 2001 From: Neil Deakin Date: Fri, 6 May 2022 10:00:54 +0000 Subject: [PATCH] Bug 1746052, use GetValidFileName to determine the filename from the channel when saving a resource, r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D135954 --- dom/webbrowserpersist/nsWebBrowserPersist.cpp | 86 ++++--------------- 1 file changed, 17 insertions(+), 69 deletions(-) diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.cpp b/dom/webbrowserpersist/nsWebBrowserPersist.cpp index 2a855788d5095..f2c2ebf588408 100644 --- a/dom/webbrowserpersist/nsWebBrowserPersist.cpp +++ b/dom/webbrowserpersist/nsWebBrowserPersist.cpp @@ -2152,78 +2152,26 @@ nsresult nsWebBrowserPersist::CalculateAndAppendFileExt( mMIMEService->GetTypeFromURI(uri, contentType); } - // Append the extension onto the file + // Validate the filename if (!contentType.IsEmpty()) { - nsCOMPtr mimeInfo; - mMIMEService->GetFromTypeAndExtension(contentType, ""_ns, - getter_AddRefs(mimeInfo)); - - nsCOMPtr localFile; - GetLocalFileFromURI(aURI, getter_AddRefs(localFile)); - - if (mimeInfo) { - nsCOMPtr url(do_QueryInterface(aURI)); - NS_ENSURE_TRUE(url, NS_ERROR_FAILURE); - - nsAutoCString newFileName; - url->GetFileName(newFileName); - - // Test if the current extension is current for the mime type - bool hasExtension = false; - int32_t ext = newFileName.RFind("."); - if (ext != -1) { - mimeInfo->ExtensionExists(Substring(newFileName, ext + 1), - &hasExtension); - } - - // Append the mime file extension - nsAutoCString fileExt; - if (!hasExtension) { - // Test if previous extension is acceptable - nsCOMPtr oldurl(do_QueryInterface(aOriginalURIWithExtension)); - NS_ENSURE_TRUE(oldurl, NS_ERROR_FAILURE); - oldurl->GetFileExtension(fileExt); - bool useOldExt = false; - if (!fileExt.IsEmpty()) { - mimeInfo->ExtensionExists(fileExt, &useOldExt); - } - - // If the url doesn't have an extension, or we don't know the extension, - // try to use the primary extension for the type. If we don't know the - // primary extension for the type, just continue with the url extension. - if (!useOldExt) { - nsAutoCString primaryExt; - mimeInfo->GetPrimaryExtension(primaryExt); - if (!primaryExt.IsEmpty()) { - fileExt = primaryExt; - } - } - - if (!fileExt.IsEmpty()) { - uint32_t newLength = newFileName.Length() + fileExt.Length() + 1; - if (newLength > kDefaultMaxFilenameLength) { - if (fileExt.Length() > kDefaultMaxFilenameLength / 2) - fileExt.Truncate(kDefaultMaxFilenameLength / 2); - - uint32_t diff = kDefaultMaxFilenameLength - 1 - fileExt.Length(); - if (newFileName.Length() > diff) newFileName.Truncate(diff); - } - newFileName.Append('.'); - newFileName.Append(fileExt); - } - - if (localFile) { - localFile->SetLeafName(NS_ConvertUTF8toUTF16(newFileName)); - - // Resync the URI with the file after the extension has been appended - return NS_MutateURI(url) - .Apply(&nsIFileURLMutator::SetFile, localFile) - .Finalize(aOutURI); - } - return NS_MutateURI(url) - .Apply(&nsIURLMutator::SetFileName, newFileName, nullptr) + nsAutoString newFileName; + if (NS_SUCCEEDED(mMIMEService->GetValidFileName( + aChannel, contentType, aOriginalURIWithExtension, + nsIMIMEService::VALIDATE_DEFAULT, newFileName))) { + nsCOMPtr localFile; + GetLocalFileFromURI(aURI, getter_AddRefs(localFile)); + if (localFile) { + localFile->SetLeafName(newFileName); + + // Resync the URI with the file after the extension has been appended + return NS_MutateURI(aURI) + .Apply(&nsIFileURLMutator::SetFile, localFile) .Finalize(aOutURI); } + return NS_MutateURI(aURI) + .Apply(&nsIURLMutator::SetFileName, + NS_ConvertUTF16toUTF8(newFileName), nullptr) + .Finalize(aOutURI); } }