From f378aabd4e79ce82b25515e775828114b359981f Mon Sep 17 00:00:00 2001 From: "allan.jensen@digia.com" Date: Thu, 22 Nov 2012 13:50:21 +0000 Subject: [PATCH] [Qt] Correct extensions on preferredFilename https://bugs.webkit.org/show_bug.cgi?id=103054 Reviewed by Simon Hausmann. When guessing a filename we will now ensure it has an extension that is valid for its mimetype. To do this the two missing methods getExtensionsForMIMEType and getPreferredExtensionForMIMEType have been added to the Qt implementation of MIMETypeRegistry. * platform/network/qt/QNetworkReplyHandler.cpp: (WebCore::QNetworkReplyHandler::sendResponseIfNeeded): * platform/qt/MIMETypeRegistryQt.cpp: (WebCore::MIMETypeRegistry::getExtensionsForMIMEType): (WebCore::MIMETypeRegistry::getPreferredExtensionForMIMEType): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@135511 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Source/WebCore/ChangeLog | 17 ++++++++++++++ .../network/qt/QNetworkReplyHandler.cpp | 18 +++++++++++++-- .../platform/qt/MIMETypeRegistryQt.cpp | 22 +++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog index 38a448fd0cd..c1024fdbec0 100644 --- a/Source/WebCore/ChangeLog +++ b/Source/WebCore/ChangeLog @@ -1,3 +1,20 @@ +2012-11-22 Allan Sandfeld Jensen + + [Qt] Correct extensions on preferredFilename + https://bugs.webkit.org/show_bug.cgi?id=103054 + + Reviewed by Simon Hausmann. + + When guessing a filename we will now ensure it has an extension that is valid for its mimetype. + To do this the two missing methods getExtensionsForMIMEType and getPreferredExtensionForMIMEType + have been added to the Qt implementation of MIMETypeRegistry. + + * platform/network/qt/QNetworkReplyHandler.cpp: + (WebCore::QNetworkReplyHandler::sendResponseIfNeeded): + * platform/qt/MIMETypeRegistryQt.cpp: + (WebCore::MIMETypeRegistry::getExtensionsForMIMEType): + (WebCore::MIMETypeRegistry::getPreferredExtensionForMIMEType): + 2012-11-22 Kent Tamura INPUT_MULTIPLE_FIELDS_UI: Refactoring: Do not call updateInnerTextValue if only read-only sub-fields have values diff --git a/Source/WebCore/platform/network/qt/QNetworkReplyHandler.cpp b/Source/WebCore/platform/network/qt/QNetworkReplyHandler.cpp index b83f2ba08c2..904e859831d 100644 --- a/Source/WebCore/platform/network/qt/QNetworkReplyHandler.cpp +++ b/Source/WebCore/platform/network/qt/QNetworkReplyHandler.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -581,8 +582,21 @@ void QNetworkReplyHandler::sendResponseIfNeeded() if (!suggestedFilename.isEmpty()) response.setSuggestedFilename(suggestedFilename); - else - response.setSuggestedFilename(url.lastPathComponent()); + else { + Vector extensions = MIMETypeRegistry::getExtensionsForMIMEType(mimeType); + if (extensions.isEmpty()) + response.setSuggestedFilename(url.lastPathComponent()); + else { + // If the suffix doesn't match the MIME type, correct the suffix. + QString filename = url.lastPathComponent(); + const String suffix = QMimeDatabase().suffixForFileName(filename); + if (!extensions.contains(suffix)) { + filename.chop(suffix.length()); + filename += MIMETypeRegistry::getPreferredExtensionForMIMEType(mimeType); + } + response.setSuggestedFilename(filename); + } + } response.setHTTPStatusCode(statusCode); response.setHTTPStatusText(m_replyWrapper->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().constData()); diff --git a/Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp b/Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp index 850770d1ab5..667291abdbc 100644 --- a/Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp +++ b/Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp @@ -56,6 +56,28 @@ String MIMETypeRegistry::getMIMETypeForPath(const String& path) return defaultMIMEType(); } +Vector MIMETypeRegistry::getExtensionsForMIMEType(const String& mimeTypeName) +{ + Vector extensions; + QMimeType mimeType = QMimeDatabase().mimeTypeForName(mimeTypeName); + if (mimeType.isValid() && !mimeType.isDefault()) { + Q_FOREACH(const QString& suffix, mimeType.suffixes()) { + extensions.append(suffix); + } + } + + return extensions; +} + +String MIMETypeRegistry::getPreferredExtensionForMIMEType(const String& mimeTypeName) +{ + QMimeType mimeType = QMimeDatabase().mimeTypeForName(mimeTypeName); + if (mimeType.isValid() && !mimeType.isDefault()) + return mimeType.preferredSuffix(); + + return String(); +} + String MIMETypeRegistry::getNormalizedMIMEType(const String& mimeTypeName) { // This looks up the mime type object by preferred name or alias, and returns the preferred name.