Added metadata message for sending fs transfer info

This commit is contained in:
Niklas Jensen
2026-01-17 00:16:43 +01:00
committed by Rune Harlyk
parent f0c4f0f929
commit a799af360f
7 changed files with 120 additions and 71 deletions
@@ -43,6 +43,7 @@ DEFINE_MESSAGE_TRAITS(CorrelationRequest, correlation_request)
DEFINE_MESSAGE_TRAITS(CorrelationResponse, correlation_response)
// Streaming file transfer messages
DEFINE_MESSAGE_TRAITS(FSDownloadMetadata, fs_download_metadata)
DEFINE_MESSAGE_TRAITS(FSDownloadData, fs_download_data)
DEFINE_MESSAGE_TRAITS(FSDownloadComplete, fs_download_complete)
DEFINE_MESSAGE_TRAITS(FSUploadData, fs_upload_data)
+3
View File
@@ -37,6 +37,7 @@ struct UploadState {
};
// Callback type for sending messages to clients
using SendMetadataCallback = std::function<void(const socket_message_FSDownloadMetadata&, int clientId)>;
using SendCallback = std::function<void(const socket_message_FSDownloadData&, int clientId)>;
using SendCompleteCallback = std::function<void(const socket_message_FSDownloadComplete&, int clientId)>;
using SendUploadCompleteCallback = std::function<void(const socket_message_FSUploadComplete&, int clientId)>;
@@ -47,6 +48,7 @@ class FileSystemHandler {
// Set callbacks for sending streaming data
void setSendCallbacks(
SendMetadataCallback sendMetadata,
SendCallback sendData,
SendCompleteCallback sendComplete,
SendUploadCompleteCallback sendUploadComplete
@@ -84,6 +86,7 @@ class FileSystemHandler {
std::map<std::string, UploadState> uploads_;
uint32_t transferIdCounter_;
SendMetadataCallback sendMetadataCallback_;
SendCallback sendDataCallback_;
SendCompleteCallback sendCompleteCallback_;
SendUploadCompleteCallback sendUploadCompleteCallback_;
+22 -10
View File
@@ -13,10 +13,12 @@ FileSystemHandler fsHandler;
FileSystemHandler::FileSystemHandler() : transferIdCounter_(0) {}
void FileSystemHandler::setSendCallbacks(
SendMetadataCallback sendMetadata,
SendCallback sendData,
SendCompleteCallback sendComplete,
SendUploadCompleteCallback sendUploadComplete
) {
sendMetadataCallback_ = sendMetadata;
sendDataCallback_ = sendData;
sendCompleteCallback_ = sendComplete;
sendUploadCompleteCallback_ = sendUploadComplete;
@@ -208,22 +210,22 @@ void FileSystemHandler::handleDownloadRequest(const socket_message_FSDownloadReq
// Validate file exists
if (!ESP_FS.exists(path.c_str())) {
if (sendCompleteCallback_) {
socket_message_FSDownloadComplete complete = socket_message_FSDownloadComplete_init_zero;
complete.success = false;
strncpy(complete.error, "File not found", sizeof(complete.error) - 1);
sendCompleteCallback_(complete, clientId);
if (sendMetadataCallback_) {
socket_message_FSDownloadMetadata metadata = socket_message_FSDownloadMetadata_init_zero;
metadata.success = false;
strncpy(metadata.error, "File not found", sizeof(metadata.error) - 1);
sendMetadataCallback_(metadata, clientId);
}
return;
}
File file = ESP_FS.open(path.c_str(), "r");
if (!file || file.isDirectory()) {
if (sendCompleteCallback_) {
socket_message_FSDownloadComplete complete = socket_message_FSDownloadComplete_init_zero;
complete.success = false;
strncpy(complete.error, "Cannot open file for reading", sizeof(complete.error) - 1);
sendCompleteCallback_(complete, clientId);
if (sendMetadataCallback_) {
socket_message_FSDownloadMetadata metadata = socket_message_FSDownloadMetadata_init_zero;
metadata.success = false;
strncpy(metadata.error, "Cannot open file for reading", sizeof(metadata.error) - 1);
sendMetadataCallback_(metadata, clientId);
}
return;
}
@@ -235,6 +237,16 @@ void FileSystemHandler::handleDownloadRequest(const socket_message_FSDownloadReq
std::string transferId = generateTransferId();
// Send metadata first so client knows exact file size and can allocate buffer
if (sendMetadataCallback_) {
socket_message_FSDownloadMetadata metadata = socket_message_FSDownloadMetadata_init_zero;
strncpy(metadata.transfer_id, transferId.c_str(), sizeof(metadata.transfer_id) - 1);
metadata.success = true;
metadata.file_size = fileSize;
metadata.total_chunks = totalChunks;
sendMetadataCallback_(metadata, clientId);
}
DownloadState state;
state.path = path;
state.file = file;
+4
View File
@@ -137,6 +137,10 @@ void setupServer() {
void setupEventSocket() {
// Set up filesystem handler callbacks for streaming transfers
FileSystemWS::fsHandler.setSendCallbacks(
// Send download metadata (file size, total chunks)
[](const socket_message_FSDownloadMetadata& metadata, int clientId) {
socket.emit(metadata, clientId);
},
// Send download data chunk
[](const socket_message_FSDownloadData& data, int clientId) {
socket.emit(data, clientId);