From fc5e88d8f059a8e37db072bda22fd97732d797dc Mon Sep 17 00:00:00 2001 From: Roman Vladimirov Date: Fri, 4 Oct 2024 17:41:47 +0300 Subject: [PATCH] > Fixed bug in detect Conte-Disposition filename --- src/ViewModels/httprequestresultviewmodel.cpp | 23 +++------------- src/globalhelpers.cpp | 26 +++++++++++++++++++ src/globalhelpers.h | 2 ++ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/ViewModels/httprequestresultviewmodel.cpp b/src/ViewModels/httprequestresultviewmodel.cpp index 0547676..c397d51 100644 --- a/src/ViewModels/httprequestresultviewmodel.cpp +++ b/src/ViewModels/httprequestresultviewmodel.cpp @@ -18,6 +18,7 @@ #include #include #include "httprequestresultviewmodel.h" +#include "../globalhelpers.h" HttpRequestResultViewModel::HttpRequestResultViewModel(QObject *parent) : QObject{parent} @@ -325,7 +326,7 @@ QString HttpRequestResultViewModel::getFormatFromContentType() noexcept continue; } if (header.startsWith("content-disposition ", Qt::CaseInsensitive)) { - contentDisposition = header.toLower(); + contentDisposition = header; continue; } } @@ -349,25 +350,7 @@ QString HttpRequestResultViewModel::getFormatFromContentType() noexcept } if (!contentDisposition.isEmpty()) { - m_defaultDownloadFile = ""; - auto value = contentDisposition - .replace(StartHeaderTag, "") - .replace(EndHeaderTag, "") - .replace("content-disposition: ", ""); - if (!value.contains("attachment", Qt::CaseInsensitive)) return ""; - auto parts = value.split(";"); - foreach (auto part, parts) { - auto nameParts = part.split("="); - if (nameParts.length() != 2) continue; - - auto fileNamePart = nameParts[0].trimmed(); - if (fileNamePart != "filename" && fileNamePart != "filename*") continue; - bool isEncoded = fileNamePart == "filename*"; - auto fileNameValue = nameParts[1]; - if (fileNameValue.size() > 2 && !isEncoded) fileNameValue = fileNameValue.mid(1, fileNameValue.size() - 2); // remove quotes - if (fileNameValue.size() > 8 && isEncoded) fileNameValue = fileNameValue.mid(7); // remove utf-8'' - m_defaultDownloadFile = isEncoded ? QUrl::fromPercentEncoding(fileNameValue.toUtf8()) : fileNameValue; - } + m_defaultDownloadFile = extractFileNameFromContentDisposition(contentDisposition); return OutputNeedDownloaded; } diff --git a/src/globalhelpers.cpp b/src/globalhelpers.cpp index b10e25a..e477d51 100644 --- a/src/globalhelpers.cpp +++ b/src/globalhelpers.cpp @@ -1,3 +1,4 @@ +#include #include "globalhelpers.h" #include "globalconstants.h" @@ -65,3 +66,28 @@ QString removeFileProtocol(QString &value) noexcept return value.replace("file://", ""); #endif } + +QString extractFileNameFromContentDisposition(const QString &value) noexcept +{ + auto contentDisposition = value.toLower(); + QString downloadFileName = ""; + if (contentDisposition.isEmpty()) return downloadFileName; + + contentDisposition = contentDisposition.replace("content-disposition: ", ""); + + if (!value.contains("attachment", Qt::CaseInsensitive)) return downloadFileName; + + auto parts = contentDisposition.split(";"); + foreach (auto part, parts) { + auto nameParts = part.split("="); + if (nameParts.length() != 2) continue; + + auto fileNamePart = nameParts[0].replace("\"","").trimmed().toLower(); + if (fileNamePart != "filename" && fileNamePart != "filename*") continue; + bool isEncoded = fileNamePart == "filename*"; + auto fileNameValue = nameParts[1].replace("\"","").replace("utf-8''", ""); + downloadFileName = isEncoded ? QUrl::fromPercentEncoding(fileNameValue.toUtf8()) : fileNameValue; + } + + return downloadFileName; +} diff --git a/src/globalhelpers.h b/src/globalhelpers.h index 38bd7dd..4a98c16 100644 --- a/src/globalhelpers.h +++ b/src/globalhelpers.h @@ -9,3 +9,5 @@ void createIfNotExistsFile(const QString& path, const QString& defaultContent) n QString adjustShortcutsForDisplay(QString& value) noexcept; QString removeFileProtocol(QString& value) noexcept; + +QString extractFileNameFromContentDisposition(const QString& value) noexcept;