Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ public String getContentType() {
/**
* Creates an {@link InputStream} on this artifact. Caller has to take care
* of closing the stream. Repeatable calls open a new {@link InputStream}.
*
*
* @return {@link InputStream} to read from artifact.
*/
public abstract InputStream getFileInputStream();

/**
* Used to abort the stream before it is finally closed. This is needed when the
* download is aborted, for example.
*/
public void abortIfNeeded() {
// do abort input stream if needed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,32 @@ private static ResponseEntity<InputStream> handleFullFileRequest(final AbstractD
final ByteRange r = full;
response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal());
response.setContentLengthLong(r.getLength());

try (InputStream from = artifact.getFileInputStream()) {
final ServletOutputStream to = response.getOutputStream();
copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
try {
read(artifact, filename, response, progressListener, r);
} catch (final IOException e) {
throw new FileStreamingFailedException("fullfileRequest " + filename, e);
}

return ResponseEntity.ok().build();
}

private static void read(final AbstractDbArtifact artifact, final String filename, final HttpServletResponse response,
final FileStreamingProgressListener progressListener, final ByteRange r) throws IOException {
read(artifact, filename, response.getOutputStream(), progressListener, r);
}

private static void read(final AbstractDbArtifact artifact, final String filename, final ServletOutputStream to,
final FileStreamingProgressListener progressListener, final ByteRange r) throws IOException {
InputStream from = artifact.getFileInputStream();
try {
copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
} catch (final IOException e) {
artifact.abortIfNeeded();
throw e;
} finally {
from.close();
}
}

private static ResponseEntity<InputStream> extractRange(final HttpServletResponse response, final long length,
final List<ByteRange> ranges, final String range) {

Expand Down Expand Up @@ -268,17 +283,13 @@ private static ResponseEntity<InputStream> handleMultipartRangeRequest(final Abs
final ServletOutputStream to = response.getOutputStream();

for (final ByteRange r : ranges) {
try (InputStream from = artifact.getFileInputStream()) {

// Add multipart boundary and header fields for every range.
to.println();
to.println("--" + ByteRange.MULTIPART_BOUNDARY);
to.println(HttpHeaders.CONTENT_RANGE + ": bytes " + r.getStart() + "-" + r.getEnd() + "/"
+ r.getTotal());
// Add multipart boundary and header fields for every range.
to.println();
to.println("--" + ByteRange.MULTIPART_BOUNDARY);
to.println(
HttpHeaders.CONTENT_RANGE + ": bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal());

// Copy single part range of multi part range.
copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
}
read(artifact, filename, to, progressListener, r);
}

// End with final multipart boundary.
Expand All @@ -299,9 +310,8 @@ private static ResponseEntity<InputStream> handleStandardRangeRequest(final Abst
response.setContentLengthLong(r.getLength());
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

try (InputStream from = artifact.getFileInputStream()) {
final ServletOutputStream to = response.getOutputStream();
copyStreams(from, to, progressListener, r.getStart(), r.getLength(), filename);
try {
read(artifact, filename, response, progressListener, r);
} catch (final IOException e) {
LOG.error("standardRangeRequest of file ({}) failed!", filename, e);
throw new FileStreamingFailedException(filename);
Expand Down