Skip to content

Commit

Permalink
Bug 1746052, use ValidateFileNameForSaving to compute the filename fo…
Browse files Browse the repository at this point in the history
…r dragging images to the file system, r=haik

Differential Revision: https://phabricator.services.mozilla.com/D135952
  • Loading branch information
EnnDeakin2 committed May 6, 2022
1 parent 5fc5de5 commit f5705ba
Showing 1 changed file with 22 additions and 68 deletions.
90 changes: 22 additions & 68 deletions dom/base/nsContentAreaDragDrop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,42 +260,23 @@ nsContentAreaDragDropDataProvider::GetFlavorData(nsITransferable* aTransferable,
supportsString = do_QueryInterface(tmp);
if (!supportsString) return NS_ERROR_FAILURE;

nsAutoString imageRequestMime;
supportsString->GetData(imageRequestMime);
nsAutoString contentType;
supportsString->GetData(contentType);

// If we have a MIME type, check the extension is compatible
if (!imageRequestMime.IsEmpty()) {
// Build a URL to get the filename extension
nsCOMPtr<nsIURL> imageURL = do_QueryInterface(sourceURI, &rv);
NS_ENSURE_SUCCESS(rv, rv);

nsAutoCString extension;
rv = imageURL->GetFileExtension(extension);
NS_ENSURE_SUCCESS(rv, rv);

NS_ConvertUTF16toUTF8 mimeCString(imageRequestMime);
bool isValidExtension;
nsAutoCString primaryExtension;
rv = CheckAndGetExtensionForMime(extension, mimeCString,
&isValidExtension, &primaryExtension);
NS_ENSURE_SUCCESS(rv, rv);

if (!isValidExtension && !primaryExtension.IsEmpty()) {
// The filename extension is missing or incompatible
// with the MIME type, replace it with the primary
// extension.
nsAutoCString newFileName;
rv = imageURL->GetFileBaseName(newFileName);
NS_ENSURE_SUCCESS(rv, rv);
newFileName.Append(".");
newFileName.Append(primaryExtension);
CopyUTF8toUTF16(newFileName, targetFilename);
}
nsCOMPtr<nsIMIMEService> mimeService =
do_GetService("@mozilla.org/mime;1");
if (NS_WARN_IF(!mimeService)) {
return NS_ERROR_FAILURE;
}

mimeService->ValidateFileNameForSaving(
targetFilename, NS_ConvertUTF16toUTF8(contentType),
nsIMIMEService::VALIDATE_DEFAULT, targetFilename);
} else {
// make the filename safe for the filesystem
targetFilename.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS,
'-');
}
// make the filename safe for the filesystem
targetFilename.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS,
'-');
#endif /* defined(XP_MACOSX) */

// get the target directory from the kFilePromiseDirectoryMime
Expand Down Expand Up @@ -418,54 +399,27 @@ nsresult DragDataProducer::GetImageData(imgIContainer* aImage,
nsCString mimeType;
aRequest->GetMimeType(getter_Copies(mimeType));

nsAutoCString fileName;
aRequest->GetFileName(fileName);

#if defined(XP_MACOSX)
// Save the MIME type so we can make sure the extension
// is compatible (and replace it if it isn't) when the
// image is dropped. On Mac, we need to get the OS MIME
// handler information in the parent due to sandboxing.
CopyUTF8toUTF16(mimeType, mImageRequestMime);
CopyUTF8toUTF16(fileName, mImageDestFileName);
#else
nsCOMPtr<nsIMIMEService> mimeService = do_GetService("@mozilla.org/mime;1");
if (NS_WARN_IF(!mimeService)) {
return NS_ERROR_FAILURE;
}

nsCOMPtr<nsIMIMEInfo> mimeInfo;
mimeService->GetFromTypeAndExtension(mimeType, ""_ns,
getter_AddRefs(mimeInfo));
if (mimeInfo) {
nsAutoCString extension;
imgUrl->GetFileExtension(extension);

bool validExtension;
if (extension.IsEmpty() ||
NS_FAILED(mimeInfo->ExtensionExists(extension, &validExtension)) ||
!validExtension) {
// Fix the file extension in the URL
nsAutoCString primaryExtension;
mimeInfo->GetPrimaryExtension(primaryExtension);
if (!primaryExtension.IsEmpty()) {
rv = NS_MutateURI(imgUrl)
.Apply(&nsIURLMutator::SetFileExtension, primaryExtension,
nullptr)
.Finalize(imgUrl);
NS_ENSURE_SUCCESS(rv, rv);
}
}
}
#endif /* defined(XP_MACOSX) */

nsAutoCString fileName;
imgUrl->GetFileName(fileName);

NS_UnescapeURL(fileName);

#if !defined(XP_MACOSX)
// make the filename safe for the filesystem
fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
#endif

CopyUTF8toUTF16(fileName, mImageDestFileName);
mimeService->ValidateFileNameForSaving(mImageDestFileName, mimeType,
nsIMIMEService::VALIDATE_DEFAULT,
mImageDestFileName);
#endif

// and the image object
mImage = aImage;
Expand Down

0 comments on commit f5705ba

Please sign in to comment.