Skip to content

Commit

Permalink
Fix upload related bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealKasumi committed Oct 23, 2022
1 parent 443dd82 commit a1fa4ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
31 changes: 21 additions & 10 deletions mcu/src/server/FseqEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ void TesLight::FseqEndpoint::getFseqList()
void TesLight::FseqEndpoint::postFseq()
{
TesLight::Logger::log(TesLight::Logger::LogLevel::INFO, SOURCE_LOCATION, F("Upload of fseq file completed."));
if (TesLight::FseqEndpoint::uploadFile)
{
TesLight::FseqEndpoint::uploadFile.close();
}
webServer->send(200, F("text/plain"), F("File upload successful."));
}

Expand All @@ -64,7 +60,8 @@ void TesLight::FseqEndpoint::postFseq()
*/
void TesLight::FseqEndpoint::fseqUpload()
{
if (!TesLight::FseqEndpoint::uploadFile)
HTTPUpload &upload = webServer->upload();
if (upload.status == UPLOAD_FILE_START)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::INFO, SOURCE_LOCATION, F("Received request to upload a new fseq file."));
const String fileName = webServer->arg(F("fileName"));
Expand All @@ -83,12 +80,26 @@ void TesLight::FseqEndpoint::fseqUpload()
return;
}
}

TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Writing chunk of data."));
HTTPUpload &upload = webServer->upload();
if (TesLight::FseqEndpoint::uploadFile.write(upload.buf, upload.currentSize) != upload.currentSize)
else if (upload.status == UPLOAD_FILE_WRITE)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::WARN, SOURCE_LOCATION, F("Failed to write chunk to file. Not all bytes were written."));
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Writing chunk of data."));
if (TesLight::FseqEndpoint::uploadFile.write(upload.buf, upload.currentSize) != upload.currentSize)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::WARN, SOURCE_LOCATION, F("Failed to write chunk to file. Not all bytes were written."));
webServer->send(500, F("text/plain"), F("Failed to write chunk to file. Not all bytes were written."));
return;
}
}
else if (upload.status == UPLOAD_FILE_END)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Received end of upload."));
TesLight::FseqEndpoint::uploadFile.close();
}
else if (upload.status == UPLOAD_FILE_ABORTED)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Upload was aborted."));
TesLight::FseqEndpoint::uploadFile.close();
webServer->send(400, F("text/plain"), F("Upload was aborted by the client."));
}
}

Expand Down
27 changes: 21 additions & 6 deletions mcu/src/server/UpdateEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void TesLight::UpdateEndpoint::postPackage()
*/
void TesLight::UpdateEndpoint::packageUpload()
{
if (!TesLight::UpdateEndpoint::uploadFile)
HTTPUpload &upload = webServer->upload();
if (upload.status == UPLOAD_FILE_START)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::INFO, SOURCE_LOCATION, F("Received request to upload update package."));
TesLight::UpdateEndpoint::uploadFile = TesLight::UpdateEndpoint::fileSystem->open((String)UPDATE_DIRECTORY + F("/") + UPDATE_FILE_NAME, FILE_WRITE);
Expand All @@ -61,11 +62,25 @@ void TesLight::UpdateEndpoint::packageUpload()
return;
}
}

TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Writing chunk of data."));
HTTPUpload &upload = webServer->upload();
if (TesLight::UpdateEndpoint::uploadFile.write(upload.buf, upload.currentSize) != upload.currentSize)
else if (upload.status == UPLOAD_FILE_WRITE)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Writing chunk of data."));
if (TesLight::UpdateEndpoint::uploadFile.write(upload.buf, upload.currentSize) != upload.currentSize)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::WARN, SOURCE_LOCATION, F("Failed to write chunk to file. Not all bytes were written."));
webServer->send(500, F("text/plain"), F("Failed to write chunk to file. Not all bytes were written."));
return;
}
}
else if (upload.status == UPLOAD_FILE_END)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Received end of upload."));
TesLight::UpdateEndpoint::uploadFile.close();
}
else if (upload.status == UPLOAD_FILE_ABORTED)
{
TesLight::Logger::log(TesLight::Logger::LogLevel::WARN, SOURCE_LOCATION, F("Failed to write chunk to file. Not all bytes were written."));
TesLight::Logger::log(TesLight::Logger::LogLevel::DEBUG, SOURCE_LOCATION, F("Upload was aborted."));
TesLight::UpdateEndpoint::uploadFile.close();
webServer->send(400, F("text/plain"), F("Upload was aborted by the client."));
}
}

0 comments on commit a1fa4ca

Please sign in to comment.