Skip to content

Commit

Permalink
Support different targets for resources_tgz
Browse files Browse the repository at this point in the history
  • Loading branch information
gsurkov committed Jun 8, 2023
1 parent 8e43ad6 commit 7b0839e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
21 changes: 14 additions & 7 deletions backend/flipperupdates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
#include <QJsonArray>
#include <QJsonObject>

#include "debug.h"

using namespace Flipper::Updates;

FileInfo::FileInfo(const QJsonValue &val)
FileInfo::FileInfo():
m_isValid(false)
{}

FileInfo::FileInfo(const QJsonValue &val):
FileInfo()
{
if(!val.isObject()) {
throw std::runtime_error("Expected FileInfo to be an object");
}

const auto &json = val.toObject();
const auto canConstruct = json.contains("target") && json.contains("type") &&
m_isValid = json.contains("target") && json.contains("type") &&
json.contains("url") && json.contains("sha256");

if(!canConstruct) {
if(!m_isValid) {
throw std::runtime_error("Malformed FileInfo");
}

Expand Down Expand Up @@ -50,6 +53,11 @@ const QByteArray &FileInfo::sha256() const
return m_sha256;
}

bool FileInfo::isValid() const
{
return m_isValid;
}

VersionInfo::VersionInfo(const QJsonValue &val)
{
if(!val.isObject()) {
Expand Down Expand Up @@ -103,8 +111,7 @@ const FileInfo VersionInfo::fileInfo(const QString &type, const QString &target)
return (arg.type() == type) && (target == arg.target());
});

check_return_val(it != m_files.cend(), "FileInfo not found", FileInfo());
return *it;
return (it != m_files.cend()) ? *it : FileInfo();
}

qint64 VersionInfo::compare(const VersionInfo &other) const
Expand Down
5 changes: 4 additions & 1 deletion backend/flipperupdates.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ class FileInfo
Q_GADGET

public:
FileInfo() = default;
FileInfo();
FileInfo(const QJsonValue &val);

const QString &target() const;
const QString &type() const;
const QString &url() const;
const QByteArray &sha256() const;

bool isValid() const;

private:
QString m_target;
QString m_type;
QString m_url;
QByteArray m_sha256;
bool m_isValid;
};

class VersionInfo
Expand Down
14 changes: 13 additions & 1 deletion backend/flipperzero/helper/firmwarehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,24 @@ void FirmwareHelper::prepareOptionBytes()
void FirmwareHelper::fetchAssets()
{
m_deviceState->setStatusString(QStringLiteral("Fetching databases..."));
const auto &fileInfo = m_versionInfo.fileInfo(QStringLiteral("resources_tgz"), QStringLiteral("any"));

const auto type = QStringLiteral("resources_tgz");
auto fileInfo = m_versionInfo.fileInfo(type, m_deviceState->deviceInfo().hardware.target);

if(!fileInfo.isValid()) {
fileInfo = m_versionInfo.fileInfo(type, QStringLiteral("any"));
}

fetchFile(FileIndex::AssetsTgz, fileInfo);
}

void FirmwareHelper::fetchFile(FileIndex index, const Updates::FileInfo &fileInfo)
{
if(!fileInfo.isValid()) {
finishWithError(BackendError::DataError, QStringLiteral("File info invalid (missing target?)"));
return;
}

const auto fileName = QUrl(fileInfo.url()).fileName();

auto *file = globalTempDirs->createFile(fileName, this);
Expand Down

0 comments on commit 7b0839e

Please sign in to comment.