fix(streaming): rewrite file streaming with NIO and HTTP caching (#512)
* fix(streaming): rewrite file streaming with NIO and HTTP caching * fix(streaming): improve file transfer handling and enhance client disconnect detection * fix(streaming): improve If-None-Match header evaluation for improved caching * fix(streaming): improve chapter tracking and loading in audiobook player * fix(streaming): remove unused imports in audiobook player component * fix(streaming): improve chapter tracking and state management in audiobook player (again) * fix: fix merge conflict errors and update code
This commit is contained in:
@@ -3,21 +3,40 @@ package org.booklore.service;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Locale;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FileStreamingService {
|
||||
|
||||
private static final int BUFFER_SIZE = 64 * 1024;
|
||||
private static final DateTimeFormatter HTTP_DATE_FORMAT = DateTimeFormatter
|
||||
.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
|
||||
.withZone(ZoneId.of("GMT"));
|
||||
|
||||
/**
|
||||
* Streams a file with HTTP Range support for seeking.
|
||||
* Uses Java NIO FileChannel for zero-copy transfer (sendfile) where supported.
|
||||
* Supports conditional requests via ETag / If-None-Match and If-Range (RFC 7233).
|
||||
*/
|
||||
public void streamWithRangeSupport(
|
||||
Path filePath,
|
||||
String contentType,
|
||||
@@ -25,46 +44,63 @@ public class FileStreamingService {
|
||||
HttpServletResponse response
|
||||
) throws IOException {
|
||||
|
||||
if (!Files.exists(filePath)) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");
|
||||
// Single syscall: existence check + size + last-modified
|
||||
BasicFileAttributes attrs;
|
||||
try {
|
||||
attrs = Files.readAttributes(filePath, BasicFileAttributes.class);
|
||||
} catch (NoSuchFileException _) {
|
||||
throw ApiError.FILE_NOT_FOUND.createException(filePath.toString());
|
||||
}
|
||||
|
||||
long fileSize = attrs.size();
|
||||
Instant lastModified = attrs.lastModifiedTime().toInstant();
|
||||
String etag = generateETag(fileSize, lastModified);
|
||||
String rangeHeader = request.getHeader("Range");
|
||||
|
||||
// Standard headers for media streaming
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setContentType(contentType);
|
||||
response.setHeader("ETag", etag);
|
||||
response.setDateHeader("Last-Modified", lastModified.toEpochMilli());
|
||||
// Allow caching with mandatory revalidation via ETag eliminates
|
||||
// redundant byte transfers on seeks while keeping access-control checks.
|
||||
response.setHeader("Cache-Control", "no-cache");
|
||||
response.setHeader("Content-Disposition", "inline");
|
||||
|
||||
// Conditional: If-None-Match, 304
|
||||
String ifNoneMatch = request.getHeader("If-None-Match");
|
||||
if (evaluateIfNoneMatch(ifNoneMatch, etag)) {
|
||||
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
|
||||
return;
|
||||
}
|
||||
|
||||
long fileSize = Files.size(filePath);
|
||||
String rangeHeader = request.getHeader("Range");
|
||||
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
response.setHeader("Content-Disposition", "inline");
|
||||
response.setContentType(contentType);
|
||||
|
||||
// -------------------------
|
||||
// HEAD
|
||||
// -------------------------
|
||||
// HEAD request 200 with Content-Length, no body
|
||||
if ("HEAD".equalsIgnoreCase(request.getMethod())) {
|
||||
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
response.setHeader(
|
||||
"Content-Range",
|
||||
"bytes 0-" + (fileSize - 1) + "/" + fileSize
|
||||
);
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentLengthLong(fileSize);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// -------------------------
|
||||
// NO RANGE
|
||||
// -------------------------
|
||||
if (rangeHeader == null) {
|
||||
try (var fileChannel = FileChannel.open(filePath, StandardOpenOption.READ)) {
|
||||
|
||||
String ifRange = request.getHeader("If-Range");
|
||||
if (rangeHeader != null && ifRange != null && !validateIfRange(ifRange, etag, lastModified)) {
|
||||
// If-Range failed: return full file
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentLengthLong(fileSize);
|
||||
streamBytes(filePath, 0, fileSize - 1, response.getOutputStream());
|
||||
transferFile(fileChannel, 0, fileSize, response.getOutputStream());
|
||||
return;
|
||||
}
|
||||
|
||||
// NO RANGE
|
||||
if (rangeHeader == null) {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentLengthLong(fileSize);
|
||||
transferFile(fileChannel, 0, fileSize, response.getOutputStream());
|
||||
return;
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
// RANGE
|
||||
// -------------------------
|
||||
Range range = parseRange(rangeHeader, fileSize);
|
||||
if (range == null) {
|
||||
response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
|
||||
@@ -73,28 +109,124 @@ public class FileStreamingService {
|
||||
}
|
||||
|
||||
long length = range.end - range.start + 1;
|
||||
|
||||
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
response.setHeader(
|
||||
"Content-Range",
|
||||
"bytes " + range.start + "-" + range.end + "/" + fileSize
|
||||
);
|
||||
response.setHeader("Content-Range", "bytes " + range.start + "-" + range.end + "/" + fileSize);
|
||||
response.setContentLengthLong(length);
|
||||
|
||||
streamBytes(filePath, range.start, range.end, response.getOutputStream());
|
||||
transferFile(fileChannel, range.start, length, response.getOutputStream());
|
||||
|
||||
} catch (NoSuchFileException _) {
|
||||
throw ApiError.FILE_NOT_FOUND.createException(filePath.toString());
|
||||
} catch (IOException e) {
|
||||
if (isClientDisconnect(e)) {
|
||||
log.debug("Client disconnected during streaming: {}", e.getMessage());
|
||||
} else {
|
||||
throw e;
|
||||
log.error("Error during file streaming: {}", filePath, e);
|
||||
if (!response.isCommitted()) {
|
||||
throw ApiError.INTERNAL_SERVER_ERROR.createException("Streaming error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// RANGE PARSER — RFC 7233 compliant
|
||||
// ------------------------------------------------------------
|
||||
/**
|
||||
* Validates the If-Range header against current ETag and lastModified.
|
||||
* RFC 7233: If-Range can be a strong ETag OR an HTTP-date.
|
||||
*/
|
||||
private boolean validateIfRange(String ifRange, String etag, Instant lastModified) {
|
||||
if (!ifRange.isEmpty() && ifRange.charAt(0) == '\"') {
|
||||
// ETag comparison (Strong match required)
|
||||
return etag.equals(ifRange);
|
||||
} else {
|
||||
// HTTP-date comparison
|
||||
try {
|
||||
Instant ifRangeDate = Instant.from(HTTP_DATE_FORMAT.parse(ifRange));
|
||||
// Exact match on seconds required by RFC
|
||||
return lastModified.getEpochSecond() == ifRangeDate.getEpochSecond();
|
||||
} catch (DateTimeParseException e) {
|
||||
log.trace("Failed to parse If-Range date: {}", ifRange);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the If-None-Match header against the current ETag.
|
||||
* RFC 7232: If-None-Match can be "*", a single entity-tag, or a list of them.
|
||||
*/
|
||||
private boolean evaluateIfNoneMatch(String ifNoneMatch, String currentEtag) {
|
||||
if (ifNoneMatch == null || ifNoneMatch.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String trimmedValue = ifNoneMatch.trim();
|
||||
if ("*".equals(trimmedValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Split by comma. Entity-tags are [weak] opaque-tag.
|
||||
String[] parts = trimmedValue.split(",");
|
||||
for (String part : parts) {
|
||||
String tag = part.trim();
|
||||
if (tag.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (isWeakMatch(tag, currentEtag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Weak comparison of two ETags.
|
||||
* RFC 7232 Section 2.3.2: Two entity-tags are a weak match if their opaque-tags are equal,
|
||||
* regardless of whether either or both have a "W/" prefix.
|
||||
*/
|
||||
private boolean isWeakMatch(String tag1, String tag2) {
|
||||
String opaque1 = tag1.startsWith("W/") ? tag1.substring(2) : tag1;
|
||||
String opaque2 = tag2.startsWith("W/") ? tag2.substring(2) : tag2;
|
||||
return opaque1.equals(opaque2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Zero-copy transfer from file channel to output stream via NIO.
|
||||
* Delegates to sendfile(2) on Linux / equivalent on macOS when the
|
||||
* servlet container's OutputStream maps to a socket channel.
|
||||
* Closes the output stream upon completion as the channel wrapper propagates close.
|
||||
*/
|
||||
private void transferFile(FileChannel source, long position, long count, OutputStream out) throws IOException {
|
||||
try (WritableByteChannel destination = Channels.newChannel(out)) {
|
||||
long remaining = count;
|
||||
long currentPos = position;
|
||||
int zeroTransferCount = 0;
|
||||
|
||||
while (remaining > 0) {
|
||||
long transferred = source.transferTo(currentPos, remaining, destination);
|
||||
if (transferred <= 0) {
|
||||
++zeroTransferCount;
|
||||
if (zeroTransferCount > 100) {
|
||||
throw new IOException("File transfer stalled with " + remaining + " bytes remaining");
|
||||
}
|
||||
Thread.onSpinWait();
|
||||
continue;
|
||||
}
|
||||
zeroTransferCount = 0;
|
||||
currentPos += transferred;
|
||||
remaining -= transferred;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Strong ETag derived from file size and last-modified epoch millis.
|
||||
* Sufficient for static-file identity without content hashing overhead.
|
||||
*/
|
||||
String generateETag(long fileSize, Instant lastModified) {
|
||||
return "\"" + Long.toHexString(fileSize) + "-" + Long.toHexString(lastModified.toEpochMilli()) + "\"";
|
||||
}
|
||||
|
||||
// RANGE PARSER (not) RFC 7233 compliant
|
||||
Range parseRange(String header, long size) {
|
||||
if (header == null || !header.startsWith("bytes=")) {
|
||||
return null;
|
||||
@@ -136,45 +268,23 @@ public class FileStreamingService {
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// STREAM BYTES
|
||||
// ------------------------------------------------------------
|
||||
private void streamBytes(Path path, long start, long end, OutputStream out)
|
||||
throws IOException {
|
||||
|
||||
try (RandomAccessFile raf = new RandomAccessFile(path.toFile(), "r")) {
|
||||
raf.seek(start);
|
||||
|
||||
long remaining = end - start + 1;
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
|
||||
while (remaining > 0) {
|
||||
int read = raf.read(buffer, 0, (int) Math.min(buffer.length, remaining));
|
||||
if (read == -1) break;
|
||||
out.write(buffer, 0, read);
|
||||
remaining -= read;
|
||||
}
|
||||
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// DISCONNECT DETECTION
|
||||
// ------------------------------------------------------------
|
||||
boolean isClientDisconnect(IOException e) {
|
||||
if (e instanceof SocketTimeoutException) return true;
|
||||
|
||||
String msg = e.getMessage();
|
||||
if (msg == null) return false;
|
||||
|
||||
return msg.contains("Broken pipe")
|
||||
|| msg.contains("Connection reset")
|
||||
|| msg.contains("connection was aborted")
|
||||
|| msg.contains("An established connection was aborted")
|
||||
|| msg.contains("SocketTimeout")
|
||||
|| msg.contains("timed out");
|
||||
return switch (e) {
|
||||
case SocketTimeoutException _, AsyncRequestNotUsableException _ -> true;
|
||||
case IOException io when io.getMessage() != null -> {
|
||||
String msg = io.getMessage();
|
||||
yield msg.contains("Broken pipe")
|
||||
|| msg.contains("Connection reset")
|
||||
|| msg.contains("connection was aborted")
|
||||
|| msg.contains("An established connection was aborted")
|
||||
|| msg.contains("Response not usable")
|
||||
|| msg.contains("SocketTimeout")
|
||||
|| msg.contains("timed out");
|
||||
}
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
record Range(long start, long end) {}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-3
@@ -110,10 +110,23 @@ public class AudioFileUtilityService {
|
||||
while (i2 < s2.length() && Character.isDigit(s2.charAt(i2))) {
|
||||
b2.append(s2.charAt(i2++));
|
||||
}
|
||||
String n1 = b1.toString();
|
||||
String n2 = b2.toString();
|
||||
|
||||
long v1 = Long.parseLong(b1.toString());
|
||||
long v2 = Long.parseLong(b2.toString());
|
||||
if (v1 != v2) return Long.compare(v1, v2);
|
||||
// Strip leading zeros for numeric comparison
|
||||
int start1 = 0;
|
||||
while (start1 < n1.length() - 1 && n1.charAt(start1) == '0') start1++;
|
||||
int start2 = 0;
|
||||
while (start2 < n2.length() - 1 && n2.charAt(start2) == '0') start2++;
|
||||
|
||||
String s1_norm = n1.substring(start1);
|
||||
String s2_norm = n2.substring(start2);
|
||||
|
||||
if (s1_norm.length() != s2_norm.length()) {
|
||||
return s1_norm.length() - s2_norm.length();
|
||||
}
|
||||
int cmp = s1_norm.compareTo(s2_norm);
|
||||
if (cmp != 0) return cmp;
|
||||
} else {
|
||||
int cmp = Character.compare(Character.toLowerCase(c1), Character.toLowerCase(c2));
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
Reference in New Issue
Block a user