From 11601801ab8a1513d8b932e80a915db6af110419 Mon Sep 17 00:00:00 2001 From: Alexandr Chernov Date: Tue, 17 Dec 2024 11:19:03 +0100 Subject: [PATCH] GSvarServer: fixed incorrect cache element retrieval --- src/GSvarServer/ServerController.cpp | 19 ++++++------------- src/cppREST/ServerDB.cpp | 5 ++--- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/GSvarServer/ServerController.cpp b/src/GSvarServer/ServerController.cpp index e47863aa9..17a47ecfd 100644 --- a/src/GSvarServer/ServerController.cpp +++ b/src/GSvarServer/ServerController.cpp @@ -289,35 +289,28 @@ HttpResponse ServerController::locateFileByType(const HttpRequest& request) ServerDB db = ServerDB(); if (db.hasFileLocation(found_file, request.getUrlParams()["type"].toUpper().trimmed(), locus, multiple_files, return_if_missing)) { - QJsonDocument updated_cached_doc; QJsonArray updated_cached_array; - QJsonDocument cache_doc = db.getFileLocation(found_file, request.getUrlParams()["type"].toUpper().trimmed(), locus, multiple_files, return_if_missing); db.updateFileLocation(found_file, request.getUrlParams()["type"].toUpper().trimmed(), locus, multiple_files, return_if_missing); - QJsonArray cached_array = cache_doc.array(); - for (int index = 0; index < cached_array.size(); index++) + for (const QJsonValue &value : cached_array) { - if (cached_array.at(index).isObject()) + if (value.isObject()) { - QJsonObject cached_object = cached_array.takeAt(index).toObject(); - QString cached_filename = cached_object["filename"].toString(); - + QJsonObject cached_object = value.toObject(); + QString cached_filename = cached_object.value("filename").toString(); if (!cached_filename.isEmpty()) - { + { cached_object.insert("filename", createTempUrl(cached_filename, request.getUrlParams()["token"])); updated_cached_array.append(cached_object); } - else - { - continue; - } } } // Ignore the cache entry, if no URLs were genereated if (updated_cached_array.size()>0) { + QJsonDocument updated_cached_doc; updated_cached_doc.setArray(updated_cached_array); BasicResponseData response_data; diff --git a/src/cppREST/ServerDB.cpp b/src/cppREST/ServerDB.cpp index 1716c3b60..9e4349be1 100644 --- a/src/cppREST/ServerDB.cpp +++ b/src/cppREST/ServerDB.cpp @@ -459,7 +459,6 @@ bool ServerDB::addFileLocation(const QString filename_with_path, const QString t { qint64 requested_as_num = requested.toSecsSinceEpoch(); QString json_string = json_content.replace("'", "''"); - // json_string = json_string.replace("\\", "\\\\"); json_string = json_string.replace("\"", "\\\""); QSqlQuery query = db_->exec("INSERT INTO file_locations (filename_with_path, type, locus, multiple_files, return_if_missing, json_content, requested)" " VALUES (\"" + filename_with_path + "\", \"" + type + "\", \"" + locus + "\", " + QString::number(static_cast(multiple_files)) + ", " + QString::number(static_cast(return_if_missing)) + ", \"" + json_string + "\", " + QString::number(requested_as_num) + ")"); @@ -512,8 +511,8 @@ QJsonDocument ServerDB::getFileLocation(const QString filename_with_path, const QSqlQuery query = db_->exec("SELECT * FROM file_locations WHERE (filename_with_path = \"" + filename_with_path + "\" AND type = \"" + type + "\" AND locus = \"" + locus + "\" AND multiple_files=" + QString::number(static_cast(multiple_files)) + " AND return_if_missing=" + QString::number(static_cast(return_if_missing)) + ")"); if (query.next()) { - int index_json_content = query.record().indexOf("json_content"); - return QJsonDocument::fromJson(query.value(index_json_content).toString().toLocal8Bit()); + int index_json_content = query.record().indexOf("json_content"); + return QJsonDocument::fromJson(query.value(index_json_content).toString().toUtf8()); } return QJsonDocument(); }