/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Controllers/FileController.cpp
188 строк
6 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "FileController.h" namespace Controllers { FileController::FileController(Services::FileService& fileService) : m_fileService(fileService) {} nlohmann::json FileController::startUpload(const Core::Network::RequestContext& ctx) { if(const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); Data::Models::FileAttachment info; info.id = json.value("file_id", ""); info.fileName = json.value("file_name", ""); info.size = json.value("size", 0); info.mimeType = json.value("mime_type", ""); info.uploaderId = *ctx.userId; std::string checksum = json.value("checksum", ""); m_fileService.startUploadSession(info, checksum); return {{"request_id", static_cast<int64_t>(request_id)}, {"status", "ok"}, {"file_id", info.id}}; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json FileController::uploadChunk(const Core::Network::RequestContext& ctx) { if(const auto& file = ctx.file()) { const auto& fileBody = file->get(); const auto& header = fileBody.header; const auto& rawData = fileBody.rawData; std::string fileId = header.value("file_id", ""); size_t offset = header.value("offset", 0); size_t length = header.value("length", 0); bool ok = m_fileService.uploadChunk(fileId, offset, length, rawData); nlohmann::json response; if(ok) { response["status"] = "ok"; nlohmann::json fileObj; fileObj["file_id"] = fileId; fileObj["offset"] = offset; response["files"] = fileObj; } else { response["status"] = "error"; response["message"] = "Upload chunk failed"; } return response; } return {{"status", "error"}, {"message", "The request is not a file request"}}; } nlohmann::json FileController::completeUpload(const Core::Network::RequestContext& ctx) { if(const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string fileId = json.value("file_id", ""); auto result = m_fileService.completeUpload(fileId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(result.success) { response["status"] = "ok"; response["file_id"] = result.fileId; } else { response["status"] = "error"; response["file_id"] = result.fileId; response["message"] = result.errorMessage; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json FileController::cancelUpload(const Core::Network::RequestContext& ctx) { if (const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string fileId = json.value("file_id", ""); bool ok = m_fileService.cancelUpload(fileId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(ok) { response["status"] = "ok"; } else { response["status"] = "error"; response["message"] = "File not found"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } nlohmann::json FileController::startDownload(const Core::Network::RequestContext& ctx) { if(const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string fileId = json.value("file_id", ""); auto transferInfo = m_fileService.startDownloadSession(fileId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); response["status"] = "ok"; response["checksum"] = transferInfo.checksum; return response; } return {{{"status", "error"}, {"message", "The request is not a json request"}}, {}}; } std::pair<nlohmann::json, std::vector<std::byte>> FileController::downloadChunk(const Core::Network::RequestContext& ctx) { if(const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); std::string fileId = json.value("file_id", ""); size_t offset = json.value("offset", 0); size_t length = json.value("length", 8192); auto chunk = m_fileService.downloadChunk(fileId, offset, length); nlohmann::json response; if(!chunk.empty()) { response["status"] = "ok"; nlohmann::json fileObj; fileObj["file_id"] = fileId; fileObj["offset"] = offset; fileObj["length"] = chunk.size(); response["files"] = fileObj; return {response, chunk}; } else { response["status"] = "error"; response["message"] = "Error downloading the chunk"; return {response, {}}; } } return {{{"status", "error"}, {"message", "The request is not a json request"}}, {}}; } } // namespace Controllers