fix(audiobook-reader): use preload=none with explicit load() for faster streaming (#2520)
* fix(audiobook-reader): use preload=none with API duration for instant load * fix(audiobook-reader): debounce seek and limit streaming chunk size to 2MB * no message --------- Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+2
-41
@@ -12,25 +12,13 @@ import java.net.SocketTimeoutException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Generic file streaming service with HTTP Range support.
|
||||
* Can be used for streaming audio, video, or any binary files.
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class FileStreamingService {
|
||||
|
||||
private static final int BUFFER_SIZE = 8192;
|
||||
private static final long MAX_CHUNK_SIZE = 2 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Stream a file with HTTP Range support for seeking/scrubbing.
|
||||
* Implements HTTP 206 Partial Content for range requests.
|
||||
*
|
||||
* @param filePath Path to the file to stream
|
||||
* @param contentType MIME type of the file
|
||||
* @param request HTTP request (to read Range header)
|
||||
* @param response HTTP response (to write headers and body)
|
||||
*/
|
||||
public void streamWithRangeSupport(Path filePath, String contentType, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
if (!Files.exists(filePath)) {
|
||||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");
|
||||
@@ -40,28 +28,23 @@ public class FileStreamingService {
|
||||
long fileSize = Files.size(filePath);
|
||||
String rangeHeader = request.getHeader("Range");
|
||||
|
||||
// Always indicate we accept range requests
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setContentType(contentType);
|
||||
response.setHeader("Cache-Control", "public, max-age=3600");
|
||||
|
||||
try {
|
||||
if (rangeHeader == null) {
|
||||
// No Range header - return full file
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentLengthLong(fileSize);
|
||||
streamBytes(filePath, 0, fileSize - 1, response.getOutputStream());
|
||||
} else {
|
||||
// Parse Range header
|
||||
RangeInfo range = parseRange(rangeHeader, fileSize);
|
||||
if (range == null) {
|
||||
// Invalid range
|
||||
response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
|
||||
response.setHeader("Content-Range", "bytes */" + fileSize);
|
||||
return;
|
||||
}
|
||||
|
||||
// Return partial content
|
||||
long contentLength = range.end - range.start + 1;
|
||||
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
response.setContentLengthLong(contentLength);
|
||||
@@ -70,7 +53,6 @@ public class FileStreamingService {
|
||||
streamBytes(filePath, range.start, range.end, response.getOutputStream());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Client disconnected (e.g., during seeking) - this is normal, just log at debug level
|
||||
if (isClientDisconnect(e)) {
|
||||
log.debug("Client disconnected during streaming: {}", e.getMessage());
|
||||
} else {
|
||||
@@ -79,18 +61,13 @@ public class FileStreamingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the exception is due to client disconnect (common during seeking).
|
||||
*/
|
||||
boolean isClientDisconnect(IOException e) {
|
||||
// SocketTimeoutException is common when client disconnects
|
||||
if (e instanceof SocketTimeoutException) {
|
||||
return true;
|
||||
}
|
||||
|
||||
String message = e.getMessage();
|
||||
if (message == null) {
|
||||
// Check the exception class name for common disconnect types
|
||||
String className = e.getClass().getSimpleName();
|
||||
return className.contains("Timeout") || className.contains("Closed");
|
||||
}
|
||||
@@ -103,10 +80,6 @@ public class FileStreamingService {
|
||||
message.contains("timed out");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse HTTP Range header and return start/end byte positions.
|
||||
* Supports formats: "bytes=start-end", "bytes=start-", "bytes=-suffix"
|
||||
*/
|
||||
RangeInfo parseRange(String rangeHeader, long fileSize) {
|
||||
if (!rangeHeader.startsWith("bytes=")) {
|
||||
return null;
|
||||
@@ -118,7 +91,6 @@ public class FileStreamingService {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Only handle first range (most common case)
|
||||
String range = ranges[0].trim();
|
||||
String[] parts = range.split("-", -1);
|
||||
if (parts.length != 2) {
|
||||
@@ -129,26 +101,21 @@ public class FileStreamingService {
|
||||
long start, end;
|
||||
|
||||
if (parts[0].isEmpty()) {
|
||||
// Suffix range: "-500" means last 500 bytes
|
||||
long suffix = Long.parseLong(parts[1]);
|
||||
start = Math.max(0, fileSize - suffix);
|
||||
end = fileSize - 1;
|
||||
} else if (parts[1].isEmpty()) {
|
||||
// Open-ended range: "500-" means from byte 500 to end
|
||||
start = Long.parseLong(parts[0]);
|
||||
end = fileSize - 1;
|
||||
end = Math.min(start + MAX_CHUNK_SIZE - 1, fileSize - 1);
|
||||
} else {
|
||||
// Full range: "500-999"
|
||||
start = Long.parseLong(parts[0]);
|
||||
end = Long.parseLong(parts[1]);
|
||||
}
|
||||
|
||||
// Validate range
|
||||
if (start < 0 || start >= fileSize || end < start) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Clamp end to file size
|
||||
end = Math.min(end, fileSize - 1);
|
||||
|
||||
return new RangeInfo(start, end);
|
||||
@@ -157,9 +124,6 @@ public class FileStreamingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stream bytes from start to end (inclusive) using RandomAccessFile for efficient seeking.
|
||||
*/
|
||||
private void streamBytes(Path filePath, long start, long end, OutputStream outputStream) throws IOException {
|
||||
try (RandomAccessFile raf = new RandomAccessFile(filePath.toFile(), "r")) {
|
||||
raf.seek(start);
|
||||
@@ -182,8 +146,5 @@ public class FileStreamingService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Range information holder.
|
||||
*/
|
||||
record RangeInfo(long start, long end) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user