Skip to content

Commit

Permalink
improved serving of small and large (tested ~1Mb) cover images
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha committed Dec 28, 2023
1 parent b8c14cc commit ef75dfe
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ef75dfe

Please sign in to comment.