Small fixes in HTTP server

This commit is contained in:
2026-04-12 20:02:08 +02:00
parent c440399d11
commit 765f486938

View File

@@ -3,6 +3,7 @@ package sh.rhiobet.lalafin.file;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -73,8 +74,9 @@ public class FileServeService {
} }
response = Response.ok(path.toFile()); response = Response.ok(path.toFile());
channel.position(rangeStart);
response.entity(new FileServeInputStream(Channels.newInputStream(channel), response.entity(new FileServeInputStream(Channels.newInputStream(channel),
rangeStart, rangeEnd)); rangeEnd + 1 - rangeStart));
response.header("Content-Length", response.header("Content-Length",
Long.toString(rangeEnd + 1 - rangeStart)); Long.toString(rangeEnd + 1 - rangeStart));
@@ -87,8 +89,8 @@ public class FileServeService {
channel.close(); channel.close();
} }
response.header("Accept-Ranges", "bytes"); response.header("Accept-Ranges", "bytes");
response.header("Content-Disposition", response.header("Content-Disposition", "inline; filename*=UTF-8''" +
"inline; filename=\"" + fileInfo.filename + "\""); URLEncoder.encode(fileInfo.filename, StandardCharsets.UTF_8).replace("+", "%20"));
response.header("Content-Type", FileHelper.getMimeType(fileInfo.filename)); response.header("Content-Type", FileHelper.getMimeType(fileInfo.filename));
if (path.toString().contains("/.thumbnails/")) { if (path.toString().contains("/.thumbnails/")) {
response.header("Cache-Control", "max-age=604800"); response.header("Cache-Control", "max-age=604800");
@@ -120,7 +122,8 @@ public class FileServeService {
response.header("Content-Length", Long.toString(fileSize)); response.header("Content-Length", Long.toString(fileSize));
response.header("Accept-Ranges", "bytes"); 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)); response.header("Content-Type", FileHelper.getMimeType(name));
return response.build(); return response.build();
} catch (IOException e) { } catch (IOException e) {
@@ -133,22 +136,14 @@ class FileServeInputStream extends InputStream {
private InputStream is; private InputStream is;
private long remaining; private long remaining;
public FileServeInputStream(InputStream is, long startRange, long endRange) throws IOException { public FileServeInputStream(InputStream is, long remaining) throws IOException {
this.is = is; this.is = is;
try { this.remaining = remaining;
this.is.skip(startRange);
} catch (IOException e) {
try {
this.is.close();
} catch (Exception ignored) {}
throw e;
}
this.remaining = endRange + 1 - startRange;
} }
@Override @Override
public int available() throws IOException { public int available() throws IOException {
return this.is.available(); return (int) Math.min(this.is.available(), this.remaining);
} }
@Override @Override
@@ -170,27 +165,37 @@ class FileServeInputStream extends InputStream {
@Override @Override
public byte[] readAllBytes() throws IOException { public byte[] readAllBytes() throws IOException {
return this.is.readAllBytes(); byte[] bytes = this.is.readNBytes((int) this.remaining);
this.remaining -= bytes.length;
return bytes;
} }
@Override @Override
public byte[] readNBytes(int arg0) throws IOException { public byte[] readNBytes(int len) throws IOException {
return this.is.readNBytes(arg0); byte[] bytes = this.is.readNBytes((int) Math.min(len, this.remaining));
this.remaining -= bytes.length;
return bytes;
} }
@Override @Override
public int readNBytes(byte[] arg0, int arg1, int arg2) throws IOException { public int readNBytes(byte[] buf, int off, int len) throws IOException {
return this.is.readNBytes(arg0, arg1, arg2); int read = this.is.readNBytes(buf, off, (int) Math.min(len, this.remaining));
this.remaining -= read;
return read;
} }
@Override @Override
public long skip(long arg0) throws IOException { public long skip(long n) throws IOException {
return this.is.skip(arg0); long skipped = this.is.skip(Math.min(n, this.remaining));
this.remaining -= skipped;
return skipped;
} }
@Override @Override
public void skipNBytes(long arg0) throws IOException { public void skipNBytes(long n) throws IOException {
this.is.skipNBytes(arg0); long toSkip = Math.min(n, this.remaining);
this.is.skipNBytes(toSkip);
this.remaining -= toSkip;
} }
@Override @Override