Pull request #426: RED-4686: Stream objects to storage

Merge in RED/redaction-service from RED-4686 to master

* commit '99a8b9788e1b1062c8d8a93d4b5d4b65f3d34ce2':
  RED-4686: Stream objects to storage
This commit is contained in:
Dominique Eiflaender 2022-07-22 13:54:23 +02:00
commit e4b5bb93e1
3 changed files with 31 additions and 9 deletions

View File

@ -37,7 +37,7 @@
<dependency>
<groupId>com.iqser.red</groupId>
<artifactId>platform-commons-dependency</artifactId>
<version>1.14.0</version>
<version>1.15.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>

View File

@ -55,24 +55,26 @@ public class RedactionStorageService {
@Timed("redactmanager_storeObject")
public void storeObject(String dossierId, String fileId, FileType fileType, Object any) {
var bytes = serializeObject(any);
storageService.storeObject(StorageIdUtils.getStorageId(dossierId, fileId, fileType), bytes);
try(var byteArrayOutputStream = serializeObject(any)){
storageService.storeObject(StorageIdUtils.getStorageId(dossierId, fileId, fileType), byteArrayOutputStream);
}
}
@SneakyThrows
@Timed("redactmanager_serializeObject")
private byte[] serializeObject(Object any) {
private ByteArrayOutputStream serializeObject(Object any) {
try {
var baos = new ByteArrayOutputStream();
dslJson.serialize(any, baos);
return baos.toByteArray();
return baos;
} catch (com.dslplatform.json.SerializationException e) {
// Fails on file 49 Cyprodinil - EU AIR3 - MCA Section 8 Supplement - Ecotoxicological studies on the active substance.pdf
var bytes = objectMapper.writeValueAsBytes(any);
var baos = new ByteArrayOutputStream();
objectMapper.writeValue(baos, any);
dslJson.newWriter();
return bytes;
return baos;
}
}

View File

@ -7,12 +7,14 @@ import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.InputStreamResource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
public class FileSystemBackedStorageService implements StorageService{
@ -63,7 +65,9 @@ public class FileSystemBackedStorageService implements StorageService{
public void storeObject(String objectId, byte[] data) {
File tempFile = File.createTempFile("storage", objectId.replace("/","-"));
IOUtils.write(data, new FileOutputStream(tempFile));
try(var fileOutputStream = new FileOutputStream(tempFile)) {
IOUtils.write(data, fileOutputStream);
}
dataMap.put(objectId, tempFile);
}
@ -73,11 +77,27 @@ public class FileSystemBackedStorageService implements StorageService{
public void storeObject(String objectId, InputStream stream) {
File tempFile = File.createTempFile("test", ".tmp");
IOUtils.copy(stream, new FileOutputStream(tempFile));
try(var fileOutputStream = new FileOutputStream(tempFile)) {
IOUtils.copy(stream, fileOutputStream);
}
dataMap.put(objectId, tempFile);
}
@Override
@SneakyThrows
public void storeObject(String objectId, ByteArrayOutputStream stream) {
File tempFile = File.createTempFile("test", ".tmp");
try(var fileOutputStream = new FileOutputStream(tempFile)){
stream.writeTo(fileOutputStream);
}
dataMap.put(objectId, tempFile);
}
public void clearStorage() {
this.dataMap.forEach((k, v) -> {
v.delete();