From e4edee269b062f0d1ab5980e340f5cb1cc3d894d Mon Sep 17 00:00:00 2001 From: BeyondXinXin <78600221+BeyondXinXin@users.noreply.github.com> Date: Mon, 30 Aug 2021 07:59:36 +0800 Subject: [PATCH] github ImageHost: Modify delete function (#1862) --- src/imagehost/githubimagehost.cpp | 35 +++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/imagehost/githubimagehost.cpp b/src/imagehost/githubimagehost.cpp index 06cfed30..cac8fc6b 100644 --- a/src/imagehost/githubimagehost.cpp +++ b/src/imagehost/githubimagehost.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include @@ -175,15 +177,36 @@ bool GitHubImageHost::remove(const QString &p_url, QString &p_msg) const auto urlStr = QString("%1/repos/%2/%3/contents/%4").arg(c_apiUrl, m_userName, m_repoName, resourcePath); // Get the SHA of the resource. + QString sha = ""; auto reply = vte::NetworkAccess::request(QUrl(urlStr), rawHeader); - if (reply.m_error != QNetworkReply::NoError) { - p_msg = tr("Failed to fetch information about the resource (%1).").arg(resourcePath); - return false; + if (reply.m_error == QNetworkReply::NoError) { + auto replyObj = Utils::fromJsonString(reply.m_data); + Q_ASSERT(!replyObj.isEmpty()); + sha = replyObj[QStringLiteral("sha")].toString(); + } else if (reply.m_error == QNetworkReply::ContentAccessDenied) { // File larger than 1M + + // Get all file information under the warehouse directory + const auto fileInfo = QFileInfo(urlStr); + const auto urlFilePath = QUrl(fileInfo.path()); + const auto fleName = fileInfo.fileName(); + reply = vte::NetworkAccess::request(urlFilePath, rawHeader); + + if (QNetworkReply::NoError == reply.m_error) { + const auto jsonArray = QJsonDocument::fromJson(reply.m_data).array(); + for (auto i = jsonArray.begin(); i < jsonArray.end(); i++) { + if (!i->isObject()) { + continue; + } + const auto jsonObj = i->toObject(); + const auto name = jsonObj[QStringLiteral("name")].toString(); + if (name == fleName) { + sha = jsonObj[QStringLiteral("sha")].toString(); + break; + } + } + } } - auto replyObj = Utils::fromJsonString(reply.m_data); - Q_ASSERT(!replyObj.isEmpty()); - const auto sha = replyObj[QStringLiteral("sha")].toString(); if (sha.isEmpty()) { p_msg = tr("Failed to fetch SHA about the resource (%1) (%2).").arg(resourcePath, QString::fromUtf8(reply.m_data)); return false;