Skip to content

Commit

Permalink
Fallback to InputStream.available() if content length is not found or…
Browse files Browse the repository at this point in the history
… smaller
  • Loading branch information
tylerjmchugh committed Dec 17, 2024
1 parent a0e2fdc commit b5b6dbf
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,24 @@ public final MetadataResource putResource(ServiceContext context, String metadat
@Override
public final MetadataResource putResource(ServiceContext context, String metadataUuid, URL fileUrl,
MetadataResourceVisibility visibility, Boolean approved) throws Exception {
long contentLength = getContentLengthFromHeader(fileUrl);
if (contentLength > maxUploadSize) {
throw new GeonetMaxUploadSizeExceededException("uploadedResourceSizeExceededException")
.withMessageKey("exception.maxUploadSizeExceeded",
new String[]{FileUtil.humanizeFileSize(maxUploadSize)})
.withDescriptionKey("exception.maxUploadSizeExceeded.description",
new String[]{FileUtil.humanizeFileSize(contentLength),
FileUtil.humanizeFileSize(maxUploadSize)});
}
String filename = getFilenameFromHeader(fileUrl);
if (filename == null) {
filename = getFilenameFromUrl(fileUrl);
}
return putResource(context, metadataUuid, filename, fileUrl.openStream(), null, visibility, approved);
try (InputStream is = fileUrl.openStream()) {
long contentLength = getContentLengthFromHeader(fileUrl);
long availableBytes = is.available();
long fileSize = availableBytes > contentLength ? availableBytes : contentLength;
if (fileSize > maxUploadSize) {
throw new GeonetMaxUploadSizeExceededException("uploadedResourceSizeExceededException")
.withMessageKey("exception.maxUploadSizeExceeded",
new String[]{FileUtil.humanizeFileSize(maxUploadSize)})
.withDescriptionKey("exception.maxUploadSizeExceeded.description",
new String[]{FileUtil.humanizeFileSize(fileSize),
FileUtil.humanizeFileSize(maxUploadSize)});
}
return putResource(context, metadataUuid, filename, is, null, visibility, approved);
}
}

@Override
Expand Down

0 comments on commit b5b6dbf

Please sign in to comment.