Skip to content

Commit

Permalink
#5 delivery method
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 11, 2023
1 parent 4dd705a commit bebdd05
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,9 @@ public boolean handle(Request request, Response response, Callback callback) thr
if (Files.exists(assetPath)) {
var bytes = Files.readAllBytes(assetPath);
var mimetype = Files.probeContentType(assetPath);

response.getHeaders().add("Content-Type", mimetype);
response.getHeaders().add("Content-Length", bytes.length);
response.getHeaders().add("Access-Control-Max-Age", Duration.ofMinutes(1).toSeconds());
response.setStatus(200);

Content.Sink.write(response, true, ByteBuffer.wrap(bytes));
deliver(bytes, mimetype, response);

callback.succeeded();
return true;
}
Expand All @@ -90,12 +86,9 @@ public boolean handle(Request request, Response response, Callback callback) thr

var result = getScaledContent(mediaPath, format);
if (result.isPresent()) {
response.getHeaders().add("Content-Type", Scale.mime4Format(format.format()));
response.getHeaders().add("Content-Length", result.get().length);
response.getHeaders().add("Access-Control-Max-Age", Duration.ofMinutes(1).toSeconds());
response.setStatus(200);

Content.Sink.write(response, true, ByteBuffer.wrap(result.get()));
deliver(result.get(), Scale.mime4Format(format.format()), response);

callback.succeeded();
return true;
}
Expand All @@ -109,6 +102,15 @@ public boolean handle(Request request, Response response, Callback callback) thr
return true;
}

private void deliver(final byte[] bytes, final String mimetype, Response response) throws IOException {
response.getHeaders().add("Content-Type", mimetype);
response.getHeaders().add("Content-Length", bytes.length);
response.getHeaders().add("Access-Control-Max-Age", Duration.ofMinutes(1).toSeconds());
response.setStatus(200);

Content.Sink.write(response, true, ByteBuffer.wrap(bytes));
}

private Path getTempDirectory() throws IOException {
if (tempDirectory == null) {
tempDirectory = Files.createTempDirectory("cms-media-temp");
Expand Down

0 comments on commit bebdd05

Please sign in to comment.