From 765f486938a888a91b706c1d402658791977cdf8 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Sun, 12 Apr 2026 20:02:08 +0200 Subject: [PATCH] Small fixes in HTTP server --- .../lalafin/file/FileServeService.java | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java b/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java index a6fd455..f6661fd 100644 --- a/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java +++ b/src/main/java/sh/rhiobet/lalafin/file/FileServeService.java @@ -3,6 +3,7 @@ package sh.rhiobet.lalafin.file; import java.io.IOException; import java.io.InputStream; import java.net.URLDecoder; +import java.net.URLEncoder; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; @@ -73,8 +74,9 @@ public class FileServeService { } response = Response.ok(path.toFile()); + channel.position(rangeStart); response.entity(new FileServeInputStream(Channels.newInputStream(channel), - rangeStart, rangeEnd)); + rangeEnd + 1 - rangeStart)); response.header("Content-Length", Long.toString(rangeEnd + 1 - rangeStart)); @@ -87,8 +89,8 @@ public class FileServeService { channel.close(); } response.header("Accept-Ranges", "bytes"); - response.header("Content-Disposition", - "inline; filename=\"" + fileInfo.filename + "\""); + response.header("Content-Disposition", "inline; filename*=UTF-8''" + + URLEncoder.encode(fileInfo.filename, StandardCharsets.UTF_8).replace("+", "%20")); response.header("Content-Type", FileHelper.getMimeType(fileInfo.filename)); if (path.toString().contains("/.thumbnails/")) { response.header("Cache-Control", "max-age=604800"); @@ -120,7 +122,8 @@ public class FileServeService { response.header("Content-Length", Long.toString(fileSize)); response.header("Accept-Ranges", "bytes"); - response.header("Content-Disposition", "inline; filename=\"" + name + "\""); + response.header("Content-Disposition", "inline; filename*=UTF-8''" + + URLEncoder.encode(fileInfo.filename, StandardCharsets.UTF_8).replace("+", "%20")); response.header("Content-Type", FileHelper.getMimeType(name)); return response.build(); } catch (IOException e) { @@ -133,22 +136,14 @@ class FileServeInputStream extends InputStream { private InputStream is; private long remaining; - public FileServeInputStream(InputStream is, long startRange, long endRange) throws IOException { + public FileServeInputStream(InputStream is, long remaining) throws IOException { this.is = is; - try { - this.is.skip(startRange); - } catch (IOException e) { - try { - this.is.close(); - } catch (Exception ignored) {} - throw e; - } - this.remaining = endRange + 1 - startRange; + this.remaining = remaining; } @Override public int available() throws IOException { - return this.is.available(); + return (int) Math.min(this.is.available(), this.remaining); } @Override @@ -170,27 +165,37 @@ class FileServeInputStream extends InputStream { @Override public byte[] readAllBytes() throws IOException { - return this.is.readAllBytes(); + byte[] bytes = this.is.readNBytes((int) this.remaining); + this.remaining -= bytes.length; + return bytes; } @Override - public byte[] readNBytes(int arg0) throws IOException { - return this.is.readNBytes(arg0); + public byte[] readNBytes(int len) throws IOException { + byte[] bytes = this.is.readNBytes((int) Math.min(len, this.remaining)); + this.remaining -= bytes.length; + return bytes; } @Override - public int readNBytes(byte[] arg0, int arg1, int arg2) throws IOException { - return this.is.readNBytes(arg0, arg1, arg2); + public int readNBytes(byte[] buf, int off, int len) throws IOException { + int read = this.is.readNBytes(buf, off, (int) Math.min(len, this.remaining)); + this.remaining -= read; + return read; } @Override - public long skip(long arg0) throws IOException { - return this.is.skip(arg0); + public long skip(long n) throws IOException { + long skipped = this.is.skip(Math.min(n, this.remaining)); + this.remaining -= skipped; + return skipped; } @Override - public void skipNBytes(long arg0) throws IOException { - this.is.skipNBytes(arg0); + public void skipNBytes(long n) throws IOException { + long toSkip = Math.min(n, this.remaining); + this.is.skipNBytes(toSkip); + this.remaining -= toSkip; } @Override