Skip to content

Commit

Permalink
Bug 1746052, use GetValidFileName to determine the filename from the …
Browse files Browse the repository at this point in the history
…channel when saving a resource, r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D135954
  • Loading branch information
EnnDeakin2 committed May 5, 2022
1 parent b5d612c commit 352821b
Showing 1 changed file with 17 additions and 69 deletions.
86 changes: 17 additions & 69 deletions dom/webbrowserpersist/nsWebBrowserPersist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,78 +2152,26 @@ nsresult nsWebBrowserPersist::CalculateAndAppendFileExt(
mMIMEService->GetTypeFromURI(uri, contentType);
}

// Append the extension onto the file
// Validate the filename
if (!contentType.IsEmpty()) {
nsCOMPtr<nsIMIMEInfo> mimeInfo;
mMIMEService->GetFromTypeAndExtension(contentType, ""_ns,
getter_AddRefs(mimeInfo));

nsCOMPtr<nsIFile> localFile;
GetLocalFileFromURI(aURI, getter_AddRefs(localFile));

if (mimeInfo) {
nsCOMPtr<nsIURL> 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<nsIURL> 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<nsIFile> 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);
}
}

Expand Down

0 comments on commit 352821b

Please sign in to comment.