diff --git a/Core/WebServer.cpp b/Core/WebServer.cpp index ab5891dcc0..2309239aaf 100644 --- a/Core/WebServer.cpp +++ b/Core/WebServer.cpp @@ -33,6 +33,7 @@ #include "Common/TimeUtil.h" #include "Common/StringUtils.h" #include "Common/System/System.h" +#include "Common/System/OSD.h" #include "Core/Util/RecentFiles.h" #include "Core/Config.h" #include "Core/Debugger/WebSocket.h" @@ -527,6 +528,18 @@ public: std::lock_guard guard(g_webServerLock); g_uploadsInProgress.erase(sessionId_); } + void SetFile(std::string_view filename, size_t size) { + std::lock_guard guard(g_webServerLock); + auto &upload = g_uploadsInProgress[sessionId_]; + upload.currentFilename = filename; + upload.currentFileSize = size; + upload.uploadedFiles++; + upload.totalBytesBeforeCurrentFile = upload.uploadedBytes; + } + void AddBytes(size_t bytes) { + std::lock_guard guard(g_webServerLock); + g_uploadsInProgress[sessionId_].uploadedBytes += bytes; + } private: int sessionId_; }; @@ -566,6 +579,8 @@ static MultiPartResult HandleMultipartPart(const http::ServerRequest &request, s } else if (key == "filename") { INFO_LOG(Log::HTTP, "Upload filename: %.*s", STR_VIEW(value)); filename = value; + } else if (key == "fileSize") { + INFO_LOG(Log::HTTP, "Upload filesize: %.*s", STR_VIEW(value)); } } else if (equalsNoCase(StripSpaces(part), "Content-Disposition: form-data")) { // this is the first part, ok, ignore. @@ -606,6 +621,7 @@ static MultiPartResult HandleMultipartPart(const http::ServerRequest &request, s } } + progress.SetFile(filename); u64 bytesTransferred = 0; char buffer[net::InputSink::BUFFER_SIZE]; while (true) { @@ -618,6 +634,7 @@ static MultiPartResult HandleMultipartPart(const http::ServerRequest &request, s return MultiPartResult::RequestError; } } + progress.AddBytes(readBytes); bytesTransferred += readBytes; if (terminatorFound) { INFO_LOG(Log::HTTP, "Found terminator, skipping and proceeding."); @@ -630,6 +647,8 @@ static MultiPartResult HandleMultipartPart(const http::ServerRequest &request, s fclose(fp); } + g_OSD.Show(OSDType::, StringFromFormat("Uploaded '%s' (%d bytes)", filename.c_str(), (int)bytesTransferred), 5.0f); + // NOTE: We already read the boundary above. // However if this is the last part, the boundary will have "--\r\n" after it, otherwise there will be a line break. // So, let's read two more bytes, to see if it's "--" or "\r\n". diff --git a/Core/WebServer.h b/Core/WebServer.h index 1613f365d8..36e2bf4e3f 100644 --- a/Core/WebServer.h +++ b/Core/WebServer.h @@ -44,9 +44,11 @@ void WebServerSetUploadPath(const Path &path); int WebServerPort(); struct UploadProgress { - size_t uploadedBytes = 0; - size_t totalBytes = 0; - size_t uploadedFiles = 0; // we don't know the count ahead of time + s64 uploadedBytes = 0; + s64 totalBytes = 0; + s64 uploadedFiles = 0; // we don't know the count ahead of time + s64 currentFileSize = 0; + s64 totalBytesBeforeCurrentFile = 0; std::string currentFilename; }; diff --git a/UI/UploadScreen.cpp b/UI/UploadScreen.cpp index 2613542431..3a04bcef49 100644 --- a/UI/UploadScreen.cpp +++ b/UI/UploadScreen.cpp @@ -43,16 +43,11 @@ void UploadScreen::CreateViews() { // where files are uploaded sequentially). std::vector uploads = GetUploadsInProgress(); for (const auto &upload : uploads) { - std::string uploadText = StringFromFormat("%zu / %zu bytes uploaded (%zu files). Current file: %s", + std::string uploadText = StringFromFormat("%zu / %zu total bytes (%zu files). Current: %s", upload.uploadedBytes, upload.totalBytes, upload.uploadedFiles, upload.currentFilename.c_str()); root_->Add(new TextView(uploadText)); } - - //infoText->SetTextAlignment(TEXT_ALIGN_CENTER); - //verticalLayout->AddView(infoText); - //AddStandardBack(verticalLayout); - //root_->AddView(verticalLayout); } void UploadScreen::update() { diff --git a/assets/upload/index.html b/assets/upload/index.html index b17a35c7fa..febdd98621 100644 --- a/assets/upload/index.html +++ b/assets/upload/index.html @@ -252,6 +252,7 @@ // Try to preserve any directory structure, if supported. const formData = new FormData(); formData.append("file", file, file.webkitRelativePath || file.name); + formData.append("fileSize", file.size.toString()); xhr.send(formData); }); }