168 lines
5.4 KiB
C++
168 lines
5.4 KiB
C++
#include "direct_upload_repo.hpp"
|
|
|
|
#include <pqxx/pqxx>
|
|
|
|
#include <chrono>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
|
|
namespace {
|
|
|
|
std::string epoch_to_rfc3339(long long epoch) {
|
|
const std::time_t t = static_cast<std::time_t>(epoch);
|
|
std::tm tm{};
|
|
gmtime_r(&t, &tm);
|
|
std::ostringstream out;
|
|
out << std::put_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
|
|
return out.str();
|
|
}
|
|
|
|
std::string now_epoch_str() {
|
|
const auto now = std::chrono::system_clock::now();
|
|
const auto secs = std::chrono::duration_cast<std::chrono::seconds>(
|
|
now.time_since_epoch())
|
|
.count();
|
|
return std::to_string(secs);
|
|
}
|
|
|
|
DirectUpload row_to(const pqxx::row& r) {
|
|
DirectUpload d;
|
|
d.id = r["id"].as<std::string>();
|
|
d.chat_id = r["chat_id"].as<std::string>();
|
|
d.owner_id = r["owner_id"].as<std::string>();
|
|
d.filename = r["filename"].as<std::string>();
|
|
d.content_type = r["content_type"].as<std::string>();
|
|
d.expected_size = r["expected_size"].as<int>();
|
|
d.expected_sha256 = r["expected_sha256"].as<std::string>();
|
|
d.s3_key = r["s3_key"].as<std::string>();
|
|
d.status = r["status"].as<std::string>();
|
|
if (r["attachment_id"].is_null()) {
|
|
d.attachment_id = std::nullopt;
|
|
} else {
|
|
d.attachment_id = r["attachment_id"].as<std::string>();
|
|
}
|
|
d.expires_at = epoch_to_rfc3339(r["expires_at_epoch"].as<long long>());
|
|
d.created_at = epoch_to_rfc3339(r["created_at_epoch"].as<long long>());
|
|
d.updated_at = epoch_to_rfc3339(r["updated_at_epoch"].as<long long>());
|
|
return d;
|
|
}
|
|
|
|
constexpr const char* kCols =
|
|
"id, chat_id, owner_id, filename, content_type, expected_size, "
|
|
"expected_sha256, s3_key, status, attachment_id, "
|
|
"EXTRACT(EPOCH FROM expires_at)::bigint AS expires_at_epoch, "
|
|
"EXTRACT(EPOCH FROM created_at)::bigint AS created_at_epoch, "
|
|
"EXTRACT(EPOCH FROM updated_at)::bigint AS updated_at_epoch";
|
|
|
|
} // namespace
|
|
|
|
DirectUpload DirectUploadRepo::insert(const DirectUpload& in) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
const std::string ts = now_epoch_str();
|
|
// expires_at is already an absolute epoch string from the caller.
|
|
auto r = tx.exec(
|
|
"INSERT INTO direct_uploads ("
|
|
" id, chat_id, owner_id, filename, content_type, expected_size, "
|
|
" expected_sha256, s3_key, status, attachment_id, "
|
|
" expires_at, created_at, updated_at"
|
|
") VALUES ("
|
|
" $1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, "
|
|
" to_timestamp($10::bigint), to_timestamp($11::bigint), "
|
|
" to_timestamp($12::bigint)"
|
|
") RETURNING " +
|
|
std::string(kCols),
|
|
pqxx::params{in.id, in.chat_id, in.owner_id, in.filename,
|
|
in.content_type, in.expected_size, in.expected_sha256,
|
|
in.s3_key, in.status, in.expires_at_epoch, ts, ts});
|
|
tx.commit();
|
|
return row_to(r[0]);
|
|
}
|
|
|
|
std::optional<DirectUpload> DirectUploadRepo::find_by_id(
|
|
const std::string& id) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
auto r = tx.exec(
|
|
"SELECT " + std::string(kCols) +
|
|
" FROM direct_uploads WHERE id = $1::uuid",
|
|
pqxx::params{id});
|
|
if (r.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
return row_to(r[0]);
|
|
}
|
|
|
|
std::optional<DirectUpload> DirectUploadRepo::mark_completed(
|
|
const std::string& id, const std::string& attachment_id) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
const std::string ts = now_epoch_str();
|
|
auto r = tx.exec(
|
|
"UPDATE direct_uploads SET status = 'completed', "
|
|
" attachment_id = $2::uuid, "
|
|
" updated_at = to_timestamp($3::bigint) "
|
|
"WHERE id = $1::uuid AND status = 'pending' "
|
|
"RETURNING " +
|
|
std::string(kCols),
|
|
pqxx::params{id, attachment_id, ts});
|
|
if (r.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
tx.commit();
|
|
return row_to(r[0]);
|
|
}
|
|
|
|
std::optional<DirectUpload> DirectUploadRepo::mark_aborted(
|
|
const std::string& id) {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
const std::string ts = now_epoch_str();
|
|
auto r = tx.exec(
|
|
"UPDATE direct_uploads SET status = 'aborted', "
|
|
" updated_at = to_timestamp($2::bigint) "
|
|
"WHERE id = $1::uuid AND status = 'pending' "
|
|
"RETURNING " +
|
|
std::string(kCols),
|
|
pqxx::params{id, ts});
|
|
if (r.empty()) {
|
|
return std::nullopt;
|
|
}
|
|
tx.commit();
|
|
return row_to(r[0]);
|
|
}
|
|
|
|
std::vector<DirectUpload> DirectUploadRepo::expire_stale() {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
const std::string ts = now_epoch_str();
|
|
auto r = tx.exec(
|
|
"UPDATE direct_uploads SET status = 'expired', "
|
|
" updated_at = to_timestamp($1::bigint) "
|
|
"WHERE status = 'pending' AND expires_at < now() "
|
|
"RETURNING " +
|
|
std::string(kCols),
|
|
pqxx::params{ts});
|
|
tx.commit();
|
|
std::vector<DirectUpload> out;
|
|
out.reserve(r.size());
|
|
for (const auto& row : r) {
|
|
out.push_back(row_to(row));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::vector<std::string> DirectUploadRepo::pending_s3_keys() {
|
|
auto lease = pool_.acquire();
|
|
pqxx::work tx(*lease);
|
|
auto r = tx.exec(
|
|
"SELECT s3_key FROM direct_uploads WHERE status = 'pending'");
|
|
std::vector<std::string> out;
|
|
out.reserve(r.size());
|
|
for (const auto& row : r) {
|
|
out.push_back(row["s3_key"].as<std::string>());
|
|
}
|
|
return out;
|
|
}
|