Skip to content

Commit

Permalink
Fix jcloud content length is inaccurate (#8619)
Browse files Browse the repository at this point in the history
* Fix jcloud content length is inaccurate

* Remove extra space
  • Loading branch information
tylerjmchugh authored Jan 28, 2025
1 parent 3e3dacb commit 4dc61ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public final MetadataResource putResource(ServiceContext context, String metadat
}

// Upload the resource while ensuring the input stream does not exceed the maximum allowed size.
try (LimitedInputStream is = new LimitedInputStream(connection.getInputStream(), maxUploadSize)) {
try (LimitedInputStream is = new LimitedInputStream(connection.getInputStream(), maxUploadSize, contentLength)) {
return putResource(context, metadataUuid, filename, is, null, visibility, approved);
}
}
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/org/fao/geonet/util/LimitedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
*/
public class LimitedInputStream extends org.apache.commons.fileupload.util.LimitedInputStream {

/**
* The size of the file being uploaded if known.
*/
long fileSize = -1;

/**
* Creates a new instance.
Expand All @@ -46,8 +50,25 @@ public LimitedInputStream(InputStream inputStream, long pSizeMax) {
super(inputStream, pSizeMax);
}

/**
* Creates a new instance.
*
* @param inputStream The input stream, which shall be limited.
* @param pSizeMax The limit; no more than this number of bytes
* shall be returned by the source stream.
* @param fileSize The size of the file being uploaded.
*/
public LimitedInputStream(InputStream inputStream, long pSizeMax, long fileSize) {
super(inputStream, pSizeMax);
this.fileSize = fileSize;
}

@Override
protected void raiseError(long pSizeMax, long pCount) throws IOException {
throw new InputStreamLimitExceededException(pSizeMax);
}

public long getFileSize() {
return fileSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.fao.geonet.languages.IsoLanguagesMapper;
import org.fao.geonet.lib.Lib;
import org.fao.geonet.resources.JCloudConfiguration;
import org.fao.geonet.util.LimitedInputStream;
import org.fao.geonet.utils.IO;
import org.fao.geonet.utils.Log;
import org.jclouds.blobstore.ContainerNotFoundException;
Expand Down Expand Up @@ -301,9 +302,17 @@ protected MetadataResource putResource(final ServiceContext context, final Strin
// Update/set version
setPropertiesVersion(context, properties, isNewResource, metadataUuid, metadataId, visibility, approved, filename);

long contentLength;
// If the input stream is a LimitedInputStream and the file size is known then use that value otherwise use the available value.
if (is instanceof LimitedInputStream && ((LimitedInputStream) is).getFileSize() > 0) {
contentLength = ((LimitedInputStream) is).getFileSize();
} else {
contentLength = is.available();
}

Blob blob = jCloudConfiguration.getClient().getBlobStore().blobBuilder(key)
.payload(is)
.contentLength(is.available())
.contentLength(contentLength)
.userMetadata(properties)
.build();

Expand Down

0 comments on commit 4dc61ea

Please sign in to comment.