From 114652205c92bf7fcf63f355cab439a5eb4b184f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 6 Jan 2024 00:19:29 +0800 Subject: [PATCH] Fix wrong time stamp values in WebAPI The wrong values are observed when encountered an invalid QDateTime data. --- src/webui/api/serialize/serialize_torrent.cpp | 15 ++++++++++----- src/webui/api/torrentscontroller.cpp | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/webui/api/serialize/serialize_torrent.cpp b/src/webui/api/serialize/serialize_torrent.cpp index 7f14be584..959d6ff43 100644 --- a/src/webui/api/serialize/serialize_torrent.cpp +++ b/src/webui/api/serialize/serialize_torrent.cpp @@ -98,11 +98,16 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio; }; - const auto getLastActivityTime = [&torrent]() -> qlonglong + const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64 + { + return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1; + }; + + const auto getLastActivityTime = [&torrent, &toTimeStamp]() -> qlonglong { const qlonglong timeSinceActivity = torrent.timeSinceActivity(); return (timeSinceActivity < 0) - ? torrent.addedTime().toSecsSinceEpoch() + ? toTimeStamp(torrent.addedTime()) : (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity); }; @@ -134,8 +139,8 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()}, {KEY_TORRENT_DOWNLOAD_PATH, torrent.downloadPath().toString()}, {KEY_TORRENT_CONTENT_PATH, torrent.contentPath().toString()}, - {KEY_TORRENT_ADDED_ON, torrent.addedTime().toSecsSinceEpoch()}, - {KEY_TORRENT_COMPLETION_ON, torrent.completedTime().toSecsSinceEpoch()}, + {KEY_TORRENT_ADDED_ON, toTimeStamp(torrent.addedTime())}, + {KEY_TORRENT_COMPLETION_ON, toTimeStamp(torrent.completedTime())}, {KEY_TORRENT_TRACKER, torrent.currentTracker()}, {KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()}, {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()}, @@ -153,7 +158,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, {KEY_TORRENT_INACTIVE_SEEDING_TIME_LIMIT, torrent.inactiveSeedingTimeLimit()}, - {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toSecsSinceEpoch()}, + {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, toTimeStamp(torrent.lastSeenComplete())}, {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()}, {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, {KEY_TORRENT_SEEDING_TIME, torrent.finishedTime()}, diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 0216eb48a..9cc26ca2d 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -417,6 +417,11 @@ void TorrentsController::propertiesAction() if (!torrent) throw APIError(APIErrorType::NotFound); + const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64 + { + return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1; + }; + QJsonObject dataDict; dataDict[KEY_TORRENT_INFOHASHV1] = torrent->infoHash().v1().toString(); @@ -425,7 +430,7 @@ void TorrentsController::propertiesAction() dataDict[KEY_TORRENT_ID] = torrent->id().toString(); dataDict[KEY_PROP_TIME_ELAPSED] = torrent->activeTime(); dataDict[KEY_PROP_SEEDING_TIME] = torrent->finishedTime(); - dataDict[KEY_PROP_ETA] = static_cast(torrent->eta()); + dataDict[KEY_PROP_ETA] = torrent->eta(); dataDict[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount(); dataDict[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit(); dataDict[KEY_PROP_DOWNLOADED] = torrent->totalDownload(); @@ -454,12 +459,12 @@ void TorrentsController::propertiesAction() dataDict[KEY_PROP_PIECES_HAVE] = torrent->piecesHave(); dataDict[KEY_PROP_CREATED_BY] = torrent->creator(); dataDict[KEY_PROP_ISPRIVATE] = torrent->isPrivate(); - dataDict[KEY_PROP_ADDITION_DATE] = static_cast(torrent->addedTime().toSecsSinceEpoch()); + dataDict[KEY_PROP_ADDITION_DATE] = toTimeStamp(torrent->addedTime()); if (torrent->hasMetadata()) { - dataDict[KEY_PROP_LAST_SEEN] = torrent->lastSeenComplete().isValid() ? torrent->lastSeenComplete().toSecsSinceEpoch() : -1; - dataDict[KEY_PROP_COMPLETION_DATE] = torrent->completedTime().isValid() ? torrent->completedTime().toSecsSinceEpoch() : -1; - dataDict[KEY_PROP_CREATION_DATE] = static_cast(torrent->creationDate().toSecsSinceEpoch()); + dataDict[KEY_PROP_LAST_SEEN] = toTimeStamp(torrent->lastSeenComplete()); + dataDict[KEY_PROP_COMPLETION_DATE] = toTimeStamp(torrent->completedTime()); + dataDict[KEY_PROP_CREATION_DATE] = toTimeStamp(torrent->creationDate()); } else {