Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved serving of small and large (tested ~1Mb) cover images #281

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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