diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c36215..f72073b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(${PROJECT_NAME} src/Log.cpp src/LogDatabase.cpp src/LogLoader.cpp + src/MealaUploadTarget.cpp src/Sqlite.cpp src/StatusBoard.cpp src/UploadTarget.cpp) diff --git a/README.md b/README.md index f18ca59..dc36ff7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![image](logloader_logo.png) -Downloads flight logs from the vehicle over MAVLink FTP and uploads them to a local Flight Review and, optionally, a remote one. Works with PX4 (`.ulg`) and ArduPilot (`.BIN`). +Downloads flight logs from the vehicle over MAVLink FTP and uploads them to a local Flight Review and, optionally, a remote server. Works with PX4 (`.ulg`) and ArduPilot (`.BIN`). ### What it fetches, and when @@ -59,6 +59,36 @@ Downloads are staged in a temporary directory and only moved next to the finishe Older versions kept a database per server and identified logs by the timestamp `LOG_ENTRY` reported, which MAVLink FTP cannot reproduce. On first start `local_server.db` and `remote_server.db` are imported into a single `logloader.db` and their rows matched against the FTP listing by size, so logs already downloaded and uploaded are not fetched or uploaded a second time. The old files are left untouched. +### Upload backends + +A target's `backend` says which upload API it speaks. It is per target, so the local Flight Review on the companion and the remote server do not have to be the same kind of thing. + +| `backend` | Server | Auth | Notes | +| --- | --- | --- | --- | +| `flight_review` (default) | [Flight Review](https://github.com/PX4/flight_review), e.g. [review.px4.io](https://review.px4.io) | None, or `api_key` on an authenticated instance | Records the `/plot_app?log=` path it redirects to, which is what lets the UI link to the plot. `.BIN` is not accepted by review.px4.io | +| `meala` | [Meala](https://apisdynamics.ca) ([API docs](https://apisdynamics.ca/apidocs/)) | `credentials_file`, required | Logs in for a session cookie, then uploads in 5 MB Dropzone-style chunks. Returns no per-log url, so nothing is recorded to link to | + +Switching a target's backend does not re-upload what is already up: a target is identified in the database by its name (`local`, `remote`), not by where it points. + +To upload to Meala, sign up, download the account's JSON credentials, and point a target at it: + +```toml +[upload_remote] +enabled = true +backend = "meala" +url = "https://apisdynamics.ca" +credentials_file = "/home/pilot/.config/ark/logloader/meala_creds.json" + +# Filled in on every log this target uploads. +comment = "Uploaded by logloader" +battery = "" +pic = "" +gso = "" +vehicle_id = "" +``` + +The credentials file is the one Meala hands out, `{"username": "...", "token": "..."}`, where the token is the API token from the account page. Give an absolute path: like every other path in the config, it is taken as written and `~` is not expanded. Meala has no anonymous upload, so a `meala` target with no `credentials_file` is disabled at startup with a warning rather than failing one login per upload pass. `email`, `public` and `api_key` are Flight Review's and are ignored. + ### Configuration `config.toml`, in full, is documented inline in the shipped file. The keys: @@ -78,6 +108,9 @@ Older versions kept a database per server and identified logs by the timestamp ` | `upload.interval` | `10` | Seconds between upload passes | | `upload_local.*` | enabled, `http://127.0.0.1:5006` | Flight Review on the companion | | `upload_remote.*` | disabled, `https://review.px4.io` | `url`, `email`, `public`, `api_key` | +| `*.backend` | `flight_review` | Upload API this target speaks; see above | +| `*.credentials_file` | `""` | Meala account credentials; required by that backend | +| `*.comment` / `battery` / `pic` / `gso` / `vehicle_id` | `Uploaded by logloader` / `""` | Meala form fields sent with every upload | Tables are one level deep on purpose: ARK-OS's config editor renders exactly that, and a setting an operator cannot reach from the web UI may as well not exist. @@ -152,4 +185,4 @@ make install ### Future developments - Resume interrupted transfers rather than restarting them. -- Multiple backends: e.g. RobotoAI, DroneLogbook, Auterion Suite, Aloft. +- Further upload backends: e.g. RobotoAI, DroneLogbook, Auterion Suite, Aloft. diff --git a/config.toml b/config.toml index 3dada54..55e4179 100644 --- a/config.toml +++ b/config.toml @@ -61,3 +61,32 @@ public = false # Per-account key for an authenticated Flight Review. When empty no # authorization headers are sent at all, which is what open servers expect. api_key = "" + +# Which upload API this target speaks: "flight_review" (the default) or +# "meala". Only the transport changes -- the target is still "remote" +# everywhere else, so switching backends does not re-upload what is already up. +# +# For Meala, point url at the Meala server and credentials_file at the JSON +# credentials downloaded from your Meala account: +# +# backend = "meala" +# url = "https://apisdynamics.ca" +# credentials_file = "/home/pilot/.config/ark/logloader/meala_creds.json" +# +# Paths are taken as written, so give an absolute one -- "~" is not expanded. +# +# Meala has no anonymous upload, so a meala target without credentials_file is +# disabled at startup. email, public and api_key are Flight Review's and are +# ignored by it. +backend = "flight_review" +credentials_file = "" + +# Meala only: sent with every log this target uploads, as the flight's details +# on Meala. Edit these to match the aircraft and crew; empty fields are sent +# empty. comment is for observations or descriptions, pic is the Pilot in +# Command, gso the Ground Station Operator or other second crew +comment = "Uploaded by logloader" +battery = "" +pic = "" +gso = "" +vehicle_id = "" diff --git a/src/Config.cpp b/src/Config.cpp index 27379d0..4649594 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,7 @@ #include "Config.hpp" #include +#include #include #include #include @@ -63,6 +64,96 @@ T value_or(const Preferred& preferred, const Legacy& legacy, T fallback) } // namespace +bool parse_upload_backend(const std::string& text, UploadBackend& backend) +{ + std::string key; + + // Accept "flight_review", "flight-review" and "flightreview" alike; the + // separator is the kind of thing an operator gets wrong once per install. + for (const char c : text) { + if (c != '_' && c != '-' && c != ' ') { + key += static_cast(std::tolower(static_cast(c))); + } + } + + if (key == "flightreview") { + backend = UploadBackend::FlightReview; + return true; + } + + if (key == "meala") { + backend = UploadBackend::Meala; + return true; + } + + return false; +} + +const char* to_string(UploadBackend backend) +{ + switch (backend) { + case UploadBackend::Meala: + return "meala"; + + case UploadBackend::FlightReview: + default: + return "flight_review"; + } +} + +namespace +{ + +// Reads one target's backend, warning rather than throwing on a typo: an +// unreadable backend name should not stop the daemon from starting with the +// default one. +void load_backend(const toml::node_view& table, UploadTargetConfig& target) +{ + const auto text = table["backend"].value(); + + if (!text.has_value() || text->empty()) { + return; + } + + if (!parse_upload_backend(*text, target.backend)) { + LOG_WARN("Unknown backend \"" << *text << "\" for upload target " << target.name + << ", using " << to_string(target.backend)); + } +} + +// Meala's form fields are free text, but an operator writing vehicle_id = 3 +// means the same as vehicle_id = "3"; refusing the bare number would silently +// send an empty field instead. +std::string text_or(const toml::node_view& node, const std::string& fallback) +{ + if (auto value = node.value(); value.has_value()) { + return trim(*value); + } + + if (auto value = node.value(); value.has_value()) { + return std::to_string(*value); + } + + if (auto value = node.value(); value.has_value()) { + std::ostringstream text; + text << *value; + return text.str(); + } + + return fallback; +} + +void load_meala_fields(const toml::node_view& table, UploadTargetConfig& target) +{ + target.meala_comment = text_or(table["comment"], target.meala_comment); + target.meala_battery = text_or(table["battery"], target.meala_battery); + target.meala_pic = text_or(table["pic"], target.meala_pic); + target.meala_gso = text_or(table["gso"], target.meala_gso); + target.meala_vehicle_id = text_or(table["vehicle_id"], target.meala_vehicle_id); +} + +} // namespace + std::string resolve_config_path(int argc, char** argv) { for (int i = 1; i < argc; i++) { @@ -147,6 +238,9 @@ Config load_config(const std::string& path) config.local.public_logs = local["public"].value_or(true); config.local.email = local["email"].value_or(""); config.local.api_key = trim(local["api_key"].value_or("")); + load_backend(local, config.local); + config.local.credentials_file = trim(local["credentials_file"].value_or("")); + load_meala_fields(local, config.local); config.remote.name = kTargetRemote; config.remote.url = value_or(remote["url"], file["remote_server"], "https://review.px4.io"); @@ -154,6 +248,9 @@ Config load_config(const std::string& path) config.remote.public_logs = value_or(remote["public"], file["public_logs"], false); config.remote.email = value_or(remote["email"], file["email"], ""); config.remote.api_key = trim(value_or(remote["api_key"], file["remote_api_key"], "")); + load_backend(remote, config.remote); + config.remote.credentials_file = trim(remote["credentials_file"].value_or("")); + load_meala_fields(remote, config.remote); if (config.local.url.empty()) { config.local.enabled = false; @@ -163,6 +260,17 @@ Config load_config(const std::string& path) config.remote.enabled = false; } + // Meala authenticates every upload, so a target without credentials would + // fail the login once per pass forever. Saying so at startup beats one + // warning per upload interval. + for (UploadTargetConfig* target : {&config.local, &config.remote}) { + if (target->enabled && target->backend == UploadBackend::Meala && target->credentials_file.empty()) { + LOG_WARN("Upload target " << target->name << " uses the meala backend but has no " + "credentials_file; disabling it"); + target->enabled = false; + } + } + if (g_used_legacy_key) { LOG_WARN(path << " uses the pre-overhaul flat key layout. It still works, but the " "shipped config.toml shows the current one and the fallback will be removed."); diff --git a/src/Config.hpp b/src/Config.hpp index b819746..5619418 100644 --- a/src/Config.hpp +++ b/src/Config.hpp @@ -10,15 +10,41 @@ constexpr const char* kTargetLocal = "local"; constexpr const char* kTargetRemote = "remote"; +// Which upload API a target speaks. The name is a target's identity in the +// database, so it stays "remote" whatever it is pointed at; this is how the +// bytes get there. +enum class UploadBackend { + FlightReview, + Meala, +}; + +// Parses the config spelling ("flight_review", "meala"). False leaves the +// backend untouched and tells the caller the text was not recognised. +bool parse_upload_backend(const std::string& text, UploadBackend& backend); + +const char* to_string(UploadBackend backend); + struct UploadTargetConfig { std::string name; bool enabled {false}; + UploadBackend backend {UploadBackend::FlightReview}; std::string url; std::string email; // Per-account key for authenticated Flight Review instances. Empty means // no auth headers are sent at all, which is what open servers expect. std::string api_key; + // Meala only: JSON file holding the account's username and token, as + // downloaded from the Meala account page. Meala has no anonymous upload, so + // a target without this cannot be enabled. + std::string credentials_file; bool public_logs {false}; + // Meala only: what each upload fills its form fields with. Meala requires + // every field to be present, so empty is sent as an empty field. + std::string meala_comment {"Uploaded by logloader"}; + std::string meala_battery; + std::string meala_pic; + std::string meala_gso; + std::string meala_vehicle_id; }; struct Config { diff --git a/src/LogLoader.cpp b/src/LogLoader.cpp index eea052b..1ebf1e2 100644 --- a/src/LogLoader.cpp +++ b/src/LogLoader.cpp @@ -53,7 +53,7 @@ LogLoader::LogLoader(const Config& config) fs::create_directories(_config.logs_directory, ec); for (const auto* target : _config.targets()) { - _targets.push_back(std::make_unique(*target)); + _targets.push_back(make_upload_target(*target)); } } diff --git a/src/MealaUploadTarget.cpp b/src/MealaUploadTarget.cpp new file mode 100644 index 0000000..54dfee0 --- /dev/null +++ b/src/MealaUploadTarget.cpp @@ -0,0 +1,254 @@ +#include "MealaUploadTarget.hpp" +#include "Log.hpp" + +#include +#include +#include +#include + +// CPPHTTPLIB_OPENSSL_SUPPORT is set for the whole target in CMakeLists.txt; +// see the note in UploadTarget.cpp for why it is not defined per-file. +#include +#include + +namespace fs = std::filesystem; + +MealaUploadTarget::MealaUploadTarget(const UploadTargetConfig& config) + : UploadTarget(config) +{} + +std::optional MealaUploadTarget::load_credentials() +{ + std::error_code ec; + + if (!fs::exists(_config.credentials_file, ec)) { + return Result{Outcome::Unauthorized, 0, "credentials file is missing: " + _config.credentials_file, ""}; + } + + std::ifstream file(_config.credentials_file); + + if (!file) { + return Result{Outcome::Unauthorized, 0, "cannot open credentials file: " + _config.credentials_file, ""}; + } + + MealaCredentials credentials; + + try { + const nlohmann::json parsed = nlohmann::json::parse(file); + credentials.username = parsed.value("username", ""); + credentials.token = parsed.value("token", ""); + + } catch (const nlohmann::json::exception& error) { + // A truncated or hand-edited file is a stable failure, not a transient + // one: it stays broken until somebody replaces it. + return Result{Outcome::Unauthorized, 0, std::string("cannot parse credentials file: ") + error.what(), ""}; + } + + if (credentials.username.empty() || credentials.token.empty()) { + return Result{Outcome::Unauthorized, 0, "credentials file has no username/token pair: " + _config.credentials_file, ""}; + } + + _credentials = std::move(credentials); + return std::nullopt; +} + +std::optional MealaUploadTarget::login() +{ + if (_logged_in) { + return std::nullopt; + } + + if (const auto problem = load_credentials(); problem.has_value()) { + note_unauthorized(); + return problem; + } + + httplib::Params params; + params.emplace("username", _credentials.username); + params.emplace("token", _credentials.token); + + httplib::Client client(_config.url); + client.set_connection_timeout(30, 0); + client.set_read_timeout(30, 0); + + const httplib::Result response = client.Post("/login", params); + + if (!response) { + return Result{Outcome::Unreachable, 0, "connection failed during login", ""}; + } + + // A refused login is documented as 400, but anything that is not a 200 leaves + // us without a session and will keep doing so until the credentials change. + if (response->status != 200) { + note_unauthorized(); + return Result{Outcome::Unauthorized, response->status, "login was refused", ""}; + } + + // The session cookie is the only thing that makes the upload calls + // authenticated; a 200 without one would upload as nobody. + if (!response->has_header("Set-Cookie")) { + note_unauthorized(); + return Result{Outcome::Unauthorized, response->status, "login returned no session cookie", ""}; + } + + // Set-Cookie carries attributes after the first ";" (HttpOnly, Path, and an + // Expires whose value contains a comma). Only the name=value pair belongs in + // a Cookie header; sending the attributes back would be sending junk pairs. + _session_cookie = response->get_header_value("Set-Cookie"); + _session_cookie = _session_cookie.substr(0, _session_cookie.find(';')); + + if (_session_cookie.empty()) { + note_unauthorized(); + return Result{Outcome::Unauthorized, response->status, "login returned an empty session cookie", ""}; + } + + if (const auto problem = verify_session(client); problem.has_value()) { + _session_cookie.clear(); + return problem; + } + + _logged_in = true; + return std::nullopt; +} + +std::optional MealaUploadTarget::verify_session(httplib::Client& client) +{ + // A refused login is documented as a 400, but in practice Meala answers one + // with 200 and a session cookie all the same -- the session is simply not + // authenticated. Without this check the first thing to notice would be a + // chunk POST, so every upload pass would push a multi-megabyte body just to + // be told 401. This endpoint takes no parameters and answers in a few dozen + // bytes, which is a cheap way to find out before sending the log. + httplib::Headers headers; + headers.emplace("Cookie", _session_cookie); + + const httplib::Result response = client.Post("/api/get-dashboard-shares", headers, "{}", "application/json"); + + // Only an explicit refusal stops the upload. This call is an optimisation, + // not a gate: if it fails for any other reason -- the endpoint moved, the + // server erred, the connection dropped -- the upload goes ahead and reports + // whatever it finds, exactly as it would have without the check. + if (response && (response->status == 401 || response->status == 403)) { + note_unauthorized(); + return Result{Outcome::Unauthorized, response->status, "the credentials were not accepted", ""}; + } + + if (!response || response->status != 200) { + LOG_DEBUG("Could not confirm the Meala session (" + << (response ? std::to_string(response->status) : "no response") + << "); continuing with the upload"); + } + + return std::nullopt; +} + +UploadTarget::Result MealaUploadTarget::upload(const std::string& file_path) +{ + // Same reasoning as the base class: a rejected account stays rejected, and + // re-posting a whole log to find that out again helps nobody. + if (in_unauthorized_cooldown()) { + return {Outcome::Unreachable, 0, "waiting out an unauthorized response", ""}; + } + + size_t size = 0; + + if (const auto problem = check_local_file(file_path, size); problem.has_value()) { + return *problem; + } + + if (!reachable()) { + return {Outcome::Unreachable, 0, "server unreachable", ""}; + } + + if (const auto problem = login(); problem.has_value()) { + return *problem; + } + + std::ifstream file(file_path, std::ios::binary); + + if (!file) { + return {Outcome::Missing, 0, "cannot open local file: " + file_path, ""}; + } + + const std::string name = fs::path(file_path).filename().string(); + const size_t total_chunks = (size + kChunkSize - 1) / kChunkSize; + + LOG("Uploading " << name << " to " << _config.name << " (" << _config.url << ") in " + << total_chunks << (total_chunks == 1 ? " chunk" : " chunks")); + + httplib::Client client(_config.url); + client.set_connection_timeout(30, 0); + client.set_read_timeout(300, 0); + client.set_write_timeout(300, 0); + client.set_follow_location(false); + + httplib::Headers headers; + headers.emplace("Cookie", _session_cookie); + + std::vector buffer; + + for (size_t index = 0; index < total_chunks; index++) { + const size_t offset = index * kChunkSize; + const size_t length = std::min(kChunkSize, size - offset); + + buffer.resize(length); + + if (!file.read(buffer.data(), static_cast(length))) { + // The file shrank or went away mid-upload; fetching it again is the + // way out, which is what Missing asks the caller to do. + return {Outcome::Missing, 0, "local file ended early: " + file_path, ""}; + } + + // Meala reads every one of these off the form, so the ones left empty in + // the config still have to be present. + const httplib::MultipartFormDataItems fields = { + {"comments", _config.meala_comment, "", ""}, + {"battery", _config.meala_battery, "", ""}, + {"pic", _config.meala_pic, "", ""}, + {"gso", _config.meala_gso, "", ""}, + {"vehicle_id", _config.meala_vehicle_id, "", ""}, + {"dzchunkbyteoffset", std::to_string(offset), "", ""}, + {"dzchunkindex", std::to_string(index), "", ""}, + {"dztotalchunkcount", std::to_string(total_chunks), "", ""}, + {"files", std::string(buffer.begin(), buffer.end()), name, "application/octet-stream"}, + }; + + const httplib::Result response = client.Post("/upload/api", headers, fields); + + if (!response) { + return {Outcome::Retry, 0, "connection failed during upload", ""}; + } + + const int status = response->status; + + // 200 is a chunk accepted (and, on the last one, the log processed). 202 + // means the server has the bytes but its database was busy and will get to + // it later -- the log is up either way, so re-sending it would be a second + // copy rather than a retry. + if (status == 200 || status == 202) { + continue; + } + + // 401 is an expired session; 403 is a lapsed subscription. Both are a + // human's problem and stable until they fix it. + if (status == 401 || status == 403) { + // The session expired mid-upload, or the account lost its access. + // Either way the next attempt logs in again from the top. + _logged_in = false; + _session_cookie.clear(); + note_unauthorized(); + return {Outcome::Unauthorized, status, "not authorized", ""}; + } + + if (status == 400) { + return {Outcome::Rejected, status, "rejected by the server", ""}; + } + + return {Outcome::Retry, status, "server error on chunk " + std::to_string(index + 1) + + "/" + std::to_string(total_chunks), ""}; + } + + // Meala's upload API answers per chunk and hands back no per-log url, so + // there is no location to record the way Flight Review's redirect gives one. + return {Outcome::Success, 200, "uploaded", ""}; +} diff --git a/src/MealaUploadTarget.hpp b/src/MealaUploadTarget.hpp new file mode 100644 index 0000000..f91ff8b --- /dev/null +++ b/src/MealaUploadTarget.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include + +#include "UploadTarget.hpp" + +namespace httplib +{ +class Client; +} + +// One Meala account, as read from the JSON credentials file the Meala account +// page hands out: +// +// {"username": "...", "token": "..."} +// +// Meala has no anonymous upload, so a target without these cannot upload at +// all; Config rejects one that is enabled without a credentials_file. +struct MealaCredentials { + std::string username; + std::string token; +}; + +// Uploads to Meala instead of Flight Review. Two things differ from the base +// class: every request carries a session cookie obtained from /login, and the +// log goes up in fixed-size chunks rather than as one streamed body, because +// /upload/api takes the Dropzone-style chunk fields. +class MealaUploadTarget : public UploadTarget +{ +public: + explicit MealaUploadTarget(const UploadTargetConfig& config); + + Result upload(const std::string& file_path) override; + +private: + // POSTs /login and keeps the session cookie. Returns an unset optional once + // there is a usable session, and the failure to report otherwise. + std::optional login(); + + // Read on the first login rather than at construction, so a credentials file + // that appears after logloader started is picked up on the next attempt. + std::optional load_credentials(); + + // Confirms the session the login handed back is actually authenticated; see + // the comment on the definition for why a 200 from /login is not enough. + std::optional verify_session(httplib::Client& client); + + MealaCredentials _credentials; + std::string _session_cookie; + bool _logged_in {false}; + + // What /upload/api expects per part. The whole log is never held in memory; + // one chunk at a time is. + static constexpr size_t kChunkSize = 5 * 1024 * 1024; +}; diff --git a/src/UploadTarget.cpp b/src/UploadTarget.cpp index cc3e545..9e4ab7e 100644 --- a/src/UploadTarget.cpp +++ b/src/UploadTarget.cpp @@ -1,5 +1,6 @@ #include "UploadTarget.hpp" #include "Log.hpp" +#include "MealaUploadTarget.hpp" #include #include @@ -58,6 +59,51 @@ UploadTarget::UploadTarget(const UploadTargetConfig& config) } } +void UploadTarget::note_unauthorized() +{ + _unauthorized_until = std::chrono::steady_clock::now() + kUnauthorizedCooldown; +} + +bool UploadTarget::in_unauthorized_cooldown() const +{ + return std::chrono::steady_clock::now() < _unauthorized_until; +} + +std::optional UploadTarget::check_local_file(const std::string& file_path, size_t& size) const +{ + std::error_code ec; + + if (!fs::exists(file_path, ec)) { + return Result{Outcome::Missing, 0, "local file is missing: " + file_path, ""}; + } + + size = fs::file_size(file_path, ec); + + if (ec) { + return Result{Outcome::Missing, 0, "cannot stat local file: " + file_path, ""}; + } + + // Not Missing: re-fetching a log the vehicle reports as zero bytes would + // download nothing, succeed, and come straight back here forever. + if (size == 0) { + return Result{Outcome::Rejected, 0, "log is empty: " + file_path, ""}; + } + + return std::nullopt; +} + +std::unique_ptr make_upload_target(const UploadTargetConfig& config) +{ + switch (config.backend) { + case UploadBackend::Meala: + return std::make_unique(config); + + case UploadBackend::FlightReview: + default: + return std::make_unique(config); + } +} + bool UploadTarget::reachable() { const auto now = std::chrono::steady_clock::now(); @@ -103,26 +149,14 @@ UploadTarget::Result UploadTarget::upload(const std::string& file_path) // Reported once when the 401/403 arrived; repeating a multi-megabyte POST // every upload interval just to be told no again is the spam this daemon // exists to avoid. Unreachable keeps the caller quiet and the log queued. - if (std::chrono::steady_clock::now() < _unauthorized_until) { + if (in_unauthorized_cooldown()) { return {Outcome::Unreachable, 0, "waiting out an unauthorized response", ""}; } - std::error_code ec; + size_t size = 0; - if (!fs::exists(file_path, ec)) { - return {Outcome::Missing, 0, "local file is missing: " + file_path, ""}; - } - - const auto size = fs::file_size(file_path, ec); - - if (ec) { - return {Outcome::Missing, 0, "cannot stat local file: " + file_path, ""}; - } - - // Not Missing: re-fetching a log the vehicle reports as zero bytes would - // download nothing, succeed, and come straight back here forever. - if (size == 0) { - return {Outcome::Rejected, 0, "log is empty: " + file_path, ""}; + if (const auto problem = check_local_file(file_path, size); problem.has_value()) { + return *problem; } if (!reachable()) { @@ -196,7 +230,7 @@ UploadTarget::Result UploadTarget::upload(const std::string& file_path) const size_t in_file = offset - prologue.size(); if (in_file < size) { - const size_t n = std::min(kChunk, static_cast(size) - in_file); + const size_t n = std::min(kChunk, size - in_file); std::vector buffer(n); file->seekg(static_cast(in_file)); @@ -230,7 +264,7 @@ UploadTarget::Result UploadTarget::upload(const std::string& file_path) const std::string detail = one_line(response->body); if (status == 401 || status == 403) { - _unauthorized_until = std::chrono::steady_clock::now() + kUnauthorizedCooldown; + note_unauthorized(); return {Outcome::Unauthorized, status, detail.empty() ? "not authorized" : detail, ""}; } diff --git a/src/UploadTarget.hpp b/src/UploadTarget.hpp index 2b4fb5d..204c694 100644 --- a/src/UploadTarget.hpp +++ b/src/UploadTarget.hpp @@ -1,13 +1,19 @@ #pragma once #include +#include +#include #include #include "Config.hpp" -// One Flight Review endpoint. Knows how to reach it and how to post a log to -// it; it holds no record of which logs exist or what has been uploaded, which -// is what LogDatabase is for. +// One upload endpoint. Knows how to reach it and how to post a log to it; it +// holds no record of which logs exist or what has been uploaded, which is what +// LogDatabase is for. +// +// This class speaks Flight Review's upload API, which is the default and the +// only one that needs no account. Another backend subclasses it and overrides +// upload(); make_upload_target() builds the one the config asks for. class UploadTarget { public: @@ -37,21 +43,36 @@ class UploadTarget }; explicit UploadTarget(const UploadTargetConfig& config); + virtual ~UploadTarget() = default; + + UploadTarget(const UploadTarget&) = delete; + UploadTarget& operator=(const UploadTarget&) = delete; const std::string& name() const { return _config.name; } const std::string& url() const { return _config.url; } bool enabled() const { return _config.enabled; } - Result upload(const std::string& file_path); + virtual Result upload(const std::string& file_path); -private: +protected: // Probes at most once per cooldown and logs only on the down/up // transitions, so a server that is simply off does not produce one failure // line per pending log. bool reachable(); + // A subclass reports a 401/403 through these so every backend gets the same + // cooldown; see kUnauthorizedCooldown. + void note_unauthorized(); + bool in_unauthorized_cooldown() const; + + // Common to every backend: the local file has to exist and have something in + // it before any of them is worth opening a socket for. Returns an unset + // optional when the file is fit to upload. + std::optional check_local_file(const std::string& file_path, size_t& size) const; + UploadTargetConfig _config; +private: static constexpr auto kUnreachableCooldown = std::chrono::seconds(60); std::chrono::steady_clock::time_point _unreachable_until {}; bool _reported_unreachable {false}; @@ -62,3 +83,6 @@ class UploadTarget static constexpr auto kUnauthorizedCooldown = std::chrono::minutes(5); std::chrono::steady_clock::time_point _unauthorized_until {}; }; + +// Builds the target config.backend asks for. +std::unique_ptr make_upload_target(const UploadTargetConfig& config);