From ef75dfef1e6933d9a8c024384760b7d802469501 Mon Sep 17 00:00:00 2001 From: Sascha Date: Sat, 23 Dec 2023 00:38:14 +0100 Subject: [PATCH] improved serving of small and large (tested ~1Mb) cover images --- src/Web.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Web.cpp b/src/Web.cpp index 03e1dcd8..1fa58fed 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -1936,20 +1936,17 @@ static void handleCoverImageRequest(AsyncWebServerRequest *request) { coverFile.read(); } - int imageSize = gPlayProperties.coverFileSize; - AsyncWebServerResponse *response = request->beginChunkedResponse(mimeType, [coverFile, imageSize](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { - // some kind of webserver bug with actual size available, reduce the len - maxLen = maxLen >> 1; - + size_t imageSize = gPlayProperties.coverFileSize; + AsyncWebServerResponse *response = request->beginResponse(mimeType, imageSize, [coverFile, imageSize](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { File file = coverFile; // local copy of file pointer - size_t leftToWrite = imageSize - index; - if (!leftToWrite) { - return 0; // end of transfer - } - size_t willWrite = (leftToWrite > maxLen) ? maxLen : leftToWrite; - file.read(buffer, willWrite); - index += willWrite; - return willWrite; + + // read a maximum of 512 bytes to avoid blocking too long + // higher values result in higher performance at the cost of stability + size_t willWrite = min((size_t) 512, min(maxLen, imageSize - index)); + + size_t readLen = file.read(buffer, willWrite); + + return readLen; }); response->addHeader("Cache Control", "no-cache, must-revalidate"); request->send(response);