Added metadata message for sending fs transfer info
This commit is contained in:
committed by
Rune Harlyk
parent
f0c4f0f929
commit
a799af360f
+22
-10
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user