Skip to content
Merged
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 @@ -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