Skip to content

Commit 2bc6567

Browse files
[Backport 4.2.x] Fix jcloud content length is inaccurate (#8623)
* Fix jcloud content length is inaccurate * Remove extra space --------- Co-authored-by: tylerjmchugh <Tyler.McHugh@dfo-mpo.gc.ca>
1 parent 87f47bf commit 2bc6567

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

core/src/main/java/org/fao/geonet/api/records/attachments/AbstractStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public final MetadataResource putResource(ServiceContext context, String metadat
240240
}
241241

242242
// Upload the resource while ensuring the input stream does not exceed the maximum allowed size.
243-
try (LimitedInputStream is = new LimitedInputStream(connection.getInputStream(), maxUploadSize)) {
243+
try (LimitedInputStream is = new LimitedInputStream(connection.getInputStream(), maxUploadSize, contentLength)) {
244244
return putResource(context, metadataUuid, filename, is, null, visibility, approved);
245245
}
246246
}

core/src/main/java/org/fao/geonet/util/LimitedInputStream.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
*/
3535
public class LimitedInputStream extends org.apache.commons.fileupload.util.LimitedInputStream {
3636

37+
/**
38+
* The size of the file being uploaded if known.
39+
*/
40+
long fileSize = -1;
3741

3842
/**
3943
* Creates a new instance.
@@ -46,8 +50,25 @@ public LimitedInputStream(InputStream inputStream, long pSizeMax) {
4650
super(inputStream, pSizeMax);
4751
}
4852

53+
/**
54+
* Creates a new instance.
55+
*
56+
* @param inputStream The input stream, which shall be limited.
57+
* @param pSizeMax The limit; no more than this number of bytes
58+
* shall be returned by the source stream.
59+
* @param fileSize The size of the file being uploaded.
60+
*/
61+
public LimitedInputStream(InputStream inputStream, long pSizeMax, long fileSize) {
62+
super(inputStream, pSizeMax);
63+
this.fileSize = fileSize;
64+
}
65+
4966
@Override
5067
protected void raiseError(long pSizeMax, long pCount) throws IOException {
5168
throw new InputStreamLimitExceededException(pSizeMax);
5269
}
70+
71+
public long getFileSize() {
72+
return fileSize;
73+
}
5374
}

datastorages/jcloud/src/main/java/org/fao/geonet/api/records/attachments/JCloudStore.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.fao.geonet.languages.IsoLanguagesMapper;
4343
import org.fao.geonet.lib.Lib;
4444
import org.fao.geonet.resources.JCloudConfiguration;
45+
import org.fao.geonet.util.LimitedInputStream;
4546
import org.fao.geonet.utils.IO;
4647
import org.fao.geonet.utils.Log;
4748
import org.jclouds.blobstore.ContainerNotFoundException;
@@ -301,9 +302,17 @@ protected MetadataResource putResource(final ServiceContext context, final Strin
301302
// Update/set version
302303
setPropertiesVersion(context, properties, isNewResource, metadataUuid, metadataId, visibility, approved, filename);
303304

305+
long contentLength;
306+
// If the input stream is a LimitedInputStream and the file size is known then use that value otherwise use the available value.
307+
if (is instanceof LimitedInputStream && ((LimitedInputStream) is).getFileSize() > 0) {
308+
contentLength = ((LimitedInputStream) is).getFileSize();
309+
} else {
310+
contentLength = is.available();
311+
}
312+
304313
Blob blob = jCloudConfiguration.getClient().getBlobStore().blobBuilder(key)
305314
.payload(is)
306-
.contentLength(is.available())
315+
.contentLength(contentLength)
307316
.userMetadata(properties)
308317
.build();
309318

0 commit comments

Comments
 (0)