Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
[Qt] Correct extensions on preferredFilename
Browse files Browse the repository at this point in the history
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
  • Loading branch information
[email protected] committed Nov 22, 2012
1 parent 9c670fb commit f378aab
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Source/WebCore/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
2012-11-22 Allan Sandfeld Jensen <[email protected]>

[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 <[email protected]>

INPUT_MULTIPLE_FIELDS_UI: Refactoring: Do not call updateInnerTextValue if only read-only sub-fields have values
Expand Down
18 changes: 16 additions & 2 deletions Source/WebCore/platform/network/qt/QNetworkReplyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QNetworkCookie>
#include <QNetworkReply>

Expand Down Expand Up @@ -581,8 +582,21 @@ void QNetworkReplyHandler::sendResponseIfNeeded()

if (!suggestedFilename.isEmpty())
response.setSuggestedFilename(suggestedFilename);
else
response.setSuggestedFilename(url.lastPathComponent());
else {
Vector<String> 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());
Expand Down
22 changes: 22 additions & 0 deletions Source/WebCore/platform/qt/MIMETypeRegistryQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ String MIMETypeRegistry::getMIMETypeForPath(const String& path)
return defaultMIMEType();
}

Vector<String> MIMETypeRegistry::getExtensionsForMIMEType(const String& mimeTypeName)
{
Vector<String> 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.
Expand Down

0 comments on commit f378aab

Please sign in to comment.