2022-05-02 16:56:24 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-05-28 18:54:17 +01:00
|
|
|
#include "FileSink.h"
|
2022-04-22 02:12:14 +01:00
|
|
|
|
2016-05-28 18:54:17 +01:00
|
|
|
#include "FileSystem.h"
|
|
|
|
|
2023-04-01 03:25:01 +01:00
|
|
|
#include "logging.h"
|
|
|
|
|
2016-05-28 18:54:17 +01:00
|
|
|
namespace Net {
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State FileSink::init(QNetworkRequest& request)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto result = initCache(request);
|
2022-04-27 22:36:11 +01:00
|
|
|
if (result != Task::State::Running) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// create a new save file and open it for writing
|
2022-04-27 22:36:11 +01:00
|
|
|
if (!FS::ensureFilePathExists(m_filename)) {
|
2023-04-01 03:25:01 +01:00
|
|
|
qCCritical(taskNetLogC) << "Could not create folder for " + m_filename;
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
wroteAnyData = false;
|
|
|
|
m_output_file.reset(new QSaveFile(m_filename));
|
2022-04-27 22:36:11 +01:00
|
|
|
if (!m_output_file->open(QIODevice::WriteOnly)) {
|
2023-04-01 03:25:01 +01:00
|
|
|
qCCritical(taskNetLogC) << "Could not open " + m_filename + " for writing";
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
if (initAllValidators(request))
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Running;
|
|
|
|
return Task::State::Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State FileSink::write(QByteArray& data)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2022-04-27 22:36:11 +01:00
|
|
|
if (!writeAllValidators(data) || m_output_file->write(data) != data.size()) {
|
2023-04-01 03:25:01 +01:00
|
|
|
qCCritical(taskNetLogC) << "Failed writing into " + m_filename;
|
2018-07-15 13:51:05 +01:00
|
|
|
m_output_file->cancelWriting();
|
|
|
|
m_output_file.reset();
|
|
|
|
wroteAnyData = false;
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
wroteAnyData = true;
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Running;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State FileSink::abort()
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_output_file->cancelWriting();
|
|
|
|
failAllValidators();
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-22 02:12:14 +01:00
|
|
|
Task::State FileSink::finalize(QNetworkReply& reply)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
bool gotFile = false;
|
|
|
|
QVariant statusCodeV = reply.attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
bool validStatus = false;
|
|
|
|
int statusCode = statusCodeV.toInt(&validStatus);
|
2022-04-27 22:36:11 +01:00
|
|
|
if (validStatus) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// this leaves out 304 Not Modified
|
|
|
|
gotFile = statusCode == 200 || statusCode == 203;
|
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// if we wrote any data to the save file, we try to commit the data to the real file.
|
|
|
|
// if it actually got a proper file, we write it even if it was empty
|
2022-04-27 22:36:11 +01:00
|
|
|
if (gotFile || wroteAnyData) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// ask validators for data consistency
|
|
|
|
// we only do this for actual downloads, not 'your data is still the same' cache hits
|
2022-04-27 22:36:11 +01:00
|
|
|
if (!finalizeAllValidators(reply))
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// nothing went wrong...
|
2022-04-27 22:36:11 +01:00
|
|
|
if (!m_output_file->commit()) {
|
2023-04-01 03:25:01 +01:00
|
|
|
qCCritical(taskNetLogC) << "Failed to commit changes to " << m_filename;
|
2018-07-15 13:51:05 +01:00
|
|
|
m_output_file->cancelWriting();
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Failed;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
// then get rid of the save file
|
|
|
|
m_output_file.reset();
|
2016-05-28 18:54:17 +01:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
return finalizeCache(reply);
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2022-04-27 22:36:11 +01:00
|
|
|
Task::State FileSink::initCache(QNetworkRequest&)
|
|
|
|
{
|
|
|
|
return Task::State::Running;
|
|
|
|
}
|
|
|
|
|
|
|
|
Task::State FileSink::finalizeCache(QNetworkReply&)
|
2016-05-28 18:54:17 +01:00
|
|
|
{
|
2022-04-22 02:12:14 +01:00
|
|
|
return Task::State::Succeeded;
|
2016-05-28 18:54:17 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 01:19:19 +01:00
|
|
|
bool FileSink::hasLocalData()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QFileInfo info(m_filename);
|
|
|
|
return info.exists() && info.size() != 0;
|
2016-10-28 01:19:19 +01:00
|
|
|
}
|
2022-04-27 22:36:11 +01:00
|
|
|
} // namespace Net
|