Skip to content

Commit

Permalink
[NAE-1946] Connector to S3
Browse files Browse the repository at this point in the history
- updated according to PR
  • Loading branch information
renczesnetgrif committed Sep 19, 2024
1 parent 1c5b4eb commit e613d76
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@ enum FileFieldDataType {
return item
}
}
return null
}

static FileFieldDataType resolveTypeFromName(String name) {
int dot = name.lastIndexOf(".")
return resolveType((dot == -1) ? "" : name.substring(dot + 1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ abstract class StorageField<T> extends Field<T> {
super()
}

String getStorageType() {
StorageType getStorageType() {
if (storage == null) {
return StorageType.LOCAL
}
return storage.getType().name()
return storage.getType()
}

void setStorageType(StorageType storageType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.netgrif.application.engine.files.interfaces.IStorageService;
import com.netgrif.application.engine.files.throwable.StorageNotFoundException;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -15,20 +16,20 @@
@Service
public class StorageResolverService {

private Map<String, IStorageService> storageServices;
private Map<StorageType, IStorageService> storageServices;

@Autowired
private void setStorageServices(List<IStorageService> storageServices) {
this.storageServices = storageServices.stream().collect(Collectors.toMap(IStorageService::getType, Function.identity()));
}

public IStorageService resolve(String type) {
public IStorageService resolve(StorageType type) {
if (storageServices == null) {
log.error("Storage services with interface IStorageService not found.");
throw new StorageNotFoundException("Remote Storage not available.");
}
if (storageServices.containsKey(type.toUpperCase())) {
return storageServices.get(type.toUpperCase());
if (storageServices.containsKey(type)) {
return storageServices.get(type);
}
throw new StorageNotFoundException("Storage Service with type: " + type + " not available.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.netgrif.application.engine.files.throwable.ServiceErrorException;
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageField;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageType;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.InputStream;

public interface IStorageService {
String getType();
StorageType getType();

InputStream get(StorageField<?> field, String path) throws BadRequestException, ServiceErrorException, FileNotFoundException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.netgrif.application.engine.files.throwable.ServiceErrorException;
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageField;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageType;
import com.netgrif.application.engine.workflow.domain.FileStorageConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,8 +25,8 @@ public void setFileStorageConfiguration(FileStorageConfiguration fileStorageConf
}

@Override
public String getType() {
return "LOCAL";
public StorageType getType() {
return StorageType.LOCAL;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.MinIoStorage;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageField;
import com.netgrif.application.engine.petrinet.domain.dataset.StorageType;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
Expand Down Expand Up @@ -38,8 +39,8 @@ public void setProperties(MinIoProperties properties) {
}

@Override
public String getType() {
return "MINIO";
public StorageType getType() {
return StorageType.MINIO;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,8 @@ private void runGetActionsFromFileField(Map<DataEventType, DataEvent> events, Ca

private FileFieldInputStream getFilePreview(FileField field, Case useCase) throws IOException, StorageException {
IStorageService storageService = storageResolverService.resolve(field.getStorageType());
if (field.getValue().getPreviewPath() != null) {
return new FileFieldInputStream(field, storageService.get(field, field.getValue().getPreviewPath()));
}
InputStream stream = storageService.get(field, field.getValue().getPath());
File file = File.createTempFile(field.getStringId(), ".pdf");
File file = File.createTempFile(field.getStringId(), "." + FileFieldDataType.resolveTypeFromName(field.getValue().getName()).extension);
file.deleteOnExit();
FileOutputStream fos = new FileOutputStream(file);
IOUtils.copy(stream, fos);
Expand All @@ -539,8 +536,6 @@ private FileFieldInputStream getFilePreview(FileField field, Case useCase) throw
String previewPath = storageService.getPreviewPath(useCase.getStringId(), field.getImportId(), field.getValue().getName());
storageService.save(field, previewPath, inputStream);
field.getValue().setPreviewPath(previewPath);
useCase.getDataSet().get(field.getStringId()).setValue(field.getValue());
workflowService.save(useCase);
inputStream.reset();
return new FileFieldInputStream(field, inputStream);
} catch (StorageException e) {
Expand All @@ -550,8 +545,7 @@ private FileFieldInputStream getFilePreview(FileField field, Case useCase) throw
}

private ByteArrayOutputStream generateFilePreviewToStream(File file) throws IOException {
int dot = file.getName().lastIndexOf(".");
FileFieldDataType fileType = FileFieldDataType.resolveType((dot == -1) ? "" : file.getName().substring(dot + 1));
FileFieldDataType fileType = FileFieldDataType.resolveTypeFromName(file.getName());
BufferedImage image = getBufferedImageFromFile(file, fileType);
if (image.getWidth() > imageScale || image.getHeight() > imageScale) {
image = scaleImagePreview(image);
Expand Down

0 comments on commit e613d76

Please sign in to comment.