Add report photo upload

This commit is contained in:
gabdevele
2026-06-02 19:41:19 +02:00 Unverified
parent e1c65903a3
commit 68d2a46584
7 changed files with 98 additions and 5 deletions
+65
View File
@@ -4,6 +4,9 @@
#include "common/Http.h"
#include "services/ReportService.h"
#include <drogon/MultiPart.h>
#include <filesystem>
#include <optional>
using drogon::HttpResponsePtr;
@@ -174,3 +177,65 @@ void ReportController::addFeedback(
sendService(service.addFeedback(*user, reportId, body), std::move(callback));
}
void ReportController::uploadPhotos(
const drogon::HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback,
long long reportId)
{
auto user = requireUser(req, callback);
if (!user)
{
return;
}
drogon::MultiPartParser parser;
if (parser.parse(req) != 0 || parser.getFiles().empty())
{
callback(visione::http::error("invalid_multipart", drogon::k400BadRequest));
return;
}
const auto &uploadConfig = drogon::app().getCustomConfig()["uploads"];
const auto reportsPath =
uploadConfig.get("reports_path", "uploads/reports").asString();
const auto publicBasePath =
uploadConfig.get("public_base_path", "/uploads").asString();
const auto reportDir =
std::filesystem::path(reportsPath) / std::to_string(reportId);
std::filesystem::create_directories(reportDir);
visione::services::ReportService service;
Json::Value response;
response["ok"] = true;
response["photos"] = Json::arrayValue;
for (const auto &file : parser.getFiles())
{
auto extension = std::string(file.getFileExtension());
if (!extension.empty() && extension.front() != '.')
{
extension = "." + extension;
}
const auto fileName = drogon::utils::getUuid() + extension;
const auto fullPath = reportDir / fileName;
if (file.saveAs(fullPath.string()) != 0)
{
callback(visione::http::error("photo_save_failed",
drogon::k500InternalServerError));
return;
}
const auto url = publicBasePath + "/reports/" + std::to_string(reportId) +
"/" + fileName;
auto saved = service.addPhoto(*user, reportId, url);
if (!saved.get("ok", false).asBool())
{
callback(visione::http::json(std::move(saved), drogon::k403Forbidden));
return;
}
response["photos"].append(url);
}
callback(visione::http::json(std::move(response), drogon::k201Created));
}