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
+5 -1
View File
@@ -40,7 +40,7 @@
"use_gzip": true,
"use_brotli": false,
"client_max_body_size": "1M",
"client_max_memory_body_size": "64K"
"client_max_memory_body_size": "256K"
},
"plugins": [],
"custom_config": {
@@ -55,6 +55,10 @@
"smtp_port": 1025,
"from": "[email protected]"
},
"uploads": {
"public_base_path": "/uploads",
"reports_path": "uploads/reports"
},
"cors": {
"allowed_origin": "http://localhost:5173"
}
+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));
}
+7 -1
View File
@@ -24,6 +24,9 @@ class ReportController : public drogon::HttpController<ReportController>
ADD_METHOD_TO(ReportController::addFeedback,
"/api/v1/reports/{1}/feedback",
drogon::Post);
ADD_METHOD_TO(ReportController::uploadPhotos,
"/api/v1/reports/{1}/photos",
drogon::Post);
METHOD_LIST_END
void create(const drogon::HttpRequestPtr &req,
@@ -53,5 +56,8 @@ class ReportController : public drogon::HttpController<ReportController>
const drogon::HttpRequestPtr &req,
std::function<void(const drogon::HttpResponsePtr &)> &&callback,
long long reportId);
void uploadPhotos(
const drogon::HttpRequestPtr &req,
std::function<void(const drogon::HttpResponsePtr &)> &&callback,
long long reportId);
};
+1 -1
View File
@@ -28,10 +28,10 @@ Authorization: Bearer <token>
- `GET /reports/{id}/messages`: list report messages.
- `POST /reports/{id}/assignments`: assign a team.
- `POST /reports/{id}/feedback`: add citizen feedback.
- `POST /reports/{id}/photos`: upload one or more multipart files for a report.
## Admin Reference Data
- `GET|POST /admin/categories`
- `GET|POST /admin/areas`
- `GET|POST /admin/teams`
- `GET /admin/stats/overview`
+1
View File
@@ -9,6 +9,7 @@
- OTP email delivery through MailHog.
- Focused regression tests for technical endpoints and core validation helpers.
- Local admin seed: `[email protected]` / `ChangeMe123!`, with forced password change flag.
- Multipart report photo upload endpoint storing files under `uploads/reports/`.
## To do next
- Harden password hashing with Argon2id or bcrypt before production.
+16 -1
View File
@@ -372,5 +372,20 @@ Json::Value ReportService::addFeedback(const auth::UserContext &user,
body.get("comment", "").asString());
return success();
}
} // namespace visione::services
Json::Value ReportService::addPhoto(const auth::UserContext &user,
long long reportId,
const std::string &url)
{
if (!canAccessReport(user, reportId) || url.empty())
{
return failure("forbidden");
}
drogon::app().getDbClient()->execSqlSync(
"INSERT INTO report_photos (report_id, url) VALUES (?, ?)",
reportId,
url);
return success();
}
} // namespace visione::services
+3 -1
View File
@@ -30,6 +30,8 @@ class ReportService
Json::Value addFeedback(const auth::UserContext &user,
long long reportId,
const Json::Value &body);
Json::Value addPhoto(const auth::UserContext &user,
long long reportId,
const std::string &url);
};
} // namespace visione::services