Merge branch 'RED-6864-replaceGetObject' into 'master'

RED-6864 - Optimization of Upload and Download for Large Files in Azure Blob...

Closes RED-6864

See merge request redactmanager/redaction-report-service!8
This commit is contained in:
Timo Bejan 2023-08-16 11:58:54 +02:00
commit 84a238c76a
3 changed files with 36 additions and 4 deletions

View File

@ -33,7 +33,7 @@
<dependency>
<groupId>com.iqser.red</groupId>
<artifactId>platform-commons-dependency</artifactId>
<version>2.6.0</version>
<version>2.7.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>

View File

@ -1,7 +1,13 @@
package com.iqser.red.service.redaction.report.v1.server.storage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.UUID;
@ -43,7 +49,10 @@ public class ReportStorageService {
public byte[] getReportTemplate(String storageId) {
try {
return IOUtils.toByteArray(storageService.getObject(TenantContext.getTenantId(), storageId).getInputStream());
InputStream inputStream = getObject(TenantContext.getTenantId(), storageId);
byte[] storedObjectBytes = IOUtils.toByteArray(inputStream);
inputStream.close();
return storedObjectBytes;
} catch (IOException e) {
throw new RuntimeException("Could not get report template.");
}
@ -53,7 +62,11 @@ public class ReportStorageService {
@SneakyThrows
public byte[] getStoredObjectBytes(String dossierId, String fileId, FileType fileType) {
return IOUtils.toByteArray(storageService.getObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType)).getInputStream());
InputStream inputStream = getObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType));
byte[] storedObjectBytes = IOUtils.toByteArray(inputStream);
inputStream.close();
return storedObjectBytes;
}
@ -62,4 +75,11 @@ public class ReportStorageService {
return storageService.objectExists(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType));
}
@SneakyThrows
public InputStream getObject(String tenantId, String storageId) {
File destFile = File.createTempFile("destFile", ".data");
storageService.downloadTo(tenantId, storageId, destFile);
return Files.newInputStream(Paths.get(destFile.getPath()), StandardOpenOption.DELETE_ON_CLOSE);
}
}

View File

@ -31,7 +31,6 @@ public class FileSystemBackedStorageService implements StorageService {
@SneakyThrows
@Override
public InputStreamResource getObject(String tenantId, String objectId) {
var res = dataMap.get(objectId);
@ -42,6 +41,18 @@ public class FileSystemBackedStorageService implements StorageService {
}
@SneakyThrows
@Override
public void downloadTo(String tenantId, String objectId, File destinationFile) {
var res = dataMap.get(objectId);
if (res == null) {
throw new StorageObjectDoesNotExist(new RuntimeException());
}
IOUtils.copy(new FileInputStream(res), new FileOutputStream(destinationFile));
}
@Override
public void deleteObject(String tenantId, String objectId) {
@ -118,4 +129,5 @@ public class FileSystemBackedStorageService implements StorageService {
this.dataMap.clear();
}
}