Skip to content

Commit

Permalink
[NAE-1946] Remote file connector to S3
Browse files Browse the repository at this point in the history
- refactor
  • Loading branch information
martinkranec committed Mar 26, 2024
1 parent 783c233 commit c3e8a2a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.netgrif.application.engine.petrinet.domain.dataset

class FileFieldValue implements Serializable {

private static final long serialVersionUID = 1299918326436821185L;
private static final long serialVersionUID = 1299918326436821185L

private String name

Expand Down Expand Up @@ -48,6 +48,13 @@ class FileFieldValue implements Serializable {
this.path = path
}

String getPreviewPath() {
return previewPath
}

void setPreviewPath(String previewPath) {
this.previewPath = previewPath
}

@Override
String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,20 @@
import com.netgrif.application.engine.files.throwable.BadRequestException;
import com.netgrif.application.engine.files.throwable.ServiceErrorException;
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.FileField;
import com.netgrif.application.engine.petrinet.domain.dataset.FileFieldValue;
import com.netgrif.application.engine.petrinet.domain.dataset.FileListField;
import com.netgrif.application.engine.workflow.domain.Case;
import org.springframework.web.multipart.MultipartFile;

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

public interface IStorageService {
StorageType getType();

InputStream get(FileListField field, String path) throws BadRequestException, ServiceErrorException;
InputStream get(String path) throws BadRequestException, ServiceErrorException;

InputStream get(FileField field, Case useCase, boolean getPreview) throws BadRequestException, ServiceErrorException, FileNotFoundException;
boolean save(String path, MultipartFile file) throws StorageException;

boolean save(FileField field, String path, MultipartFile file) throws StorageException;
boolean save(String path, InputStream stream) throws StorageException;

boolean save(FileField field, String path, InputStream stream) throws StorageException;

void delete(FileField fileField, Case useCase) throws StorageException;

void delete(FileListField fileField, Case useCase, FileFieldValue fileFieldValue) throws StorageException;

boolean save(FileListField field, String path, MultipartFile file) throws StorageException;

boolean save(FileListField field, String path, InputStream stream) throws StorageException;
void delete(String path) throws StorageException;

String getPreviewPath(String caseId, String fieldId, String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import com.netgrif.application.engine.files.throwable.BadRequestException;
import com.netgrif.application.engine.files.throwable.ServiceErrorException;
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.FileField;
import com.netgrif.application.engine.petrinet.domain.dataset.FileFieldValue;
import com.netgrif.application.engine.petrinet.domain.dataset.FileListField;
import com.netgrif.application.engine.workflow.domain.Case;
import com.netgrif.application.engine.workflow.domain.EventNotExecutableException;
import com.netgrif.application.engine.workflow.domain.FileStorageConfiguration;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,68 +27,46 @@ public StorageType getType() {
}

@Override
public InputStream get(FileListField field, String path) throws BadRequestException, ServiceErrorException {

return null;
}

@Override
public InputStream get(FileField field, Case useCase, boolean getPreview) throws BadRequestException, ServiceErrorException {
public InputStream get(String path) throws BadRequestException, ServiceErrorException {
try {
if (getPreview) {
return new FileInputStream(getPreviewPath(useCase.getStringId(), field.getImportId(), field.getValue().getName()));
}
return new FileInputStream(field.getValue().getPath());
return new FileInputStream(path);
} catch (FileNotFoundException e) {
return null;
}
}

@Override
public boolean save(FileField field, String path, MultipartFile file) throws StorageException {
public boolean save(String path, MultipartFile file) throws StorageException {
try (InputStream stream = file.getInputStream()) {
return this.save(path, stream);
} catch (StorageException | IOException e) {
throw new StorageException("File cannot be save", e);
}
}

@Override
public boolean save(String path, InputStream stream) throws StorageException {
File savedFile = new File(path);
try {
savedFile.getParentFile().mkdirs();
if (!savedFile.createNewFile()) {
savedFile.delete();
savedFile.createNewFile();
}

FileOutputStream fout = new FileOutputStream(savedFile);
fout.write(file.getBytes());
stream.transferTo(fout);
fout.close();
} catch (IOException e) {
log.error(e.getMessage());
throw new EventNotExecutableException("File " + file.getName() + " could not be saved to file field " + field.getStringId(), e);
throw new EventNotExecutableException("File " + path + " could not be saved", e);
}

return true;
}

@Override
public boolean save(FileField field, String path, InputStream stream) throws StorageException {
return false;
}

@Override
public void delete(FileField fileField, Case useCase) throws StorageException {
new File(getPath(useCase.getStringId(), fileField.getStringId(), fileField.getValue().getName())).delete();
new File(getPreviewPath(useCase.getStringId(), fileField.getStringId(), fileField.getValue().getName())).delete();
}

@Override
public void delete(FileListField fileField, Case useCase, FileFieldValue fileFieldValue) throws StorageException {

}

@Override
public boolean save(FileListField field, String path, MultipartFile file) throws StorageException {
return false;
}

@Override
public boolean save(FileListField field, String path, InputStream stream) throws StorageException {
return false;
public void delete(String path) throws StorageException {
new File(path).delete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import com.netgrif.application.engine.files.throwable.BadRequestException;
import com.netgrif.application.engine.files.throwable.ServiceErrorException;
import com.netgrif.application.engine.files.throwable.StorageException;
import com.netgrif.application.engine.petrinet.domain.dataset.FileField;
import com.netgrif.application.engine.petrinet.domain.dataset.FileFieldValue;
import com.netgrif.application.engine.petrinet.domain.dataset.FileListField;
import com.netgrif.application.engine.workflow.domain.Case;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
Expand Down Expand Up @@ -47,24 +43,12 @@ public StorageType getType() {
}

@Override
public InputStream get(FileListField field, String path) throws BadRequestException, ServiceErrorException {
// field.getValue()
// TODO resolve filename
return get(path);
}

@Override
public InputStream get(FileField field, Case useCase, boolean getPreview) throws BadRequestException, ServiceErrorException {
String fieldValue = getPreview ? getPreviewPath(useCase.getStringId(), field.getImportId(), field.getValue().getName()) : getPath(useCase.getStringId(), field.getImportId(), field.getValue().getName());
return this.get(fieldValue);
}

private InputStream get(String name) {
public InputStream get(String path) throws BadRequestException, ServiceErrorException {
try {
return minioClient.getObject(
GetObjectArgs.builder()
.bucket(properties.getBucketName())
.object(name)
.object(path)
.build()
);
} catch (ErrorResponseException e) {
Expand All @@ -77,7 +61,7 @@ private InputStream get(String name) {
throw new ServiceErrorException("Some http error from minio", e);
}
} catch (InvalidKeyException e) {
throw new BadRequestException("Key " + name + " is corrupted.", e);
throw new BadRequestException("Key " + path + " is corrupted.", e);
} catch (ServerException | InsufficientDataException | IOException | NoSuchAlgorithmException |
InvalidResponseException | XmlParserException | InternalException e) {
log.error("Some internal error from minio", e);
Expand All @@ -86,16 +70,16 @@ private InputStream get(String name) {
}

@Override
public boolean save(FileField field, String path, MultipartFile file) throws StorageException {
public boolean save(String path, MultipartFile file) throws StorageException {
try (InputStream stream = file.getInputStream()) {
return this.save(field, path, stream);
return this.save(path, stream);
} catch (StorageException | IOException e) {
throw new StorageException("File cannot be save", e);
}
}

@Override
public boolean save(FileField field, String path, InputStream stream) throws StorageException {
public boolean save(String path, InputStream stream) throws StorageException {
try {
return minioClient.putObject(PutObjectArgs
.builder()
Expand All @@ -113,13 +97,11 @@ public boolean save(FileField field, String path, InputStream stream) throws Sto
}

@Override
public void delete(FileField field, Case useCase) throws StorageException {
public void delete(String path) throws StorageException {
try {
minioClient.removeObject(RemoveObjectArgs
.builder()
.bucket(properties.getBucketName()).object(getPath(useCase.getStringId(), field.getImportId(), field.getValue().getName())).build());
minioClient.removeObject(RemoveObjectArgs.builder().bucket(properties.getBucketName()).object(getPreviewPath(useCase.getStringId(), field.getImportId(), field.getValue().getName()))
.build());
.bucket(properties.getBucketName()).object(path).build());
} catch (InsufficientDataException | InternalException | InvalidResponseException |
IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
throw new ServiceErrorException(e.getMessage(), e);
Expand All @@ -131,20 +113,6 @@ public void delete(FileField field, Case useCase) throws StorageException {
}
}

@Override
public void delete(FileListField fileField, Case useCase, FileFieldValue fileFieldValue) throws StorageException {

}

@Override
public boolean save(FileListField field, String path, MultipartFile file) throws StorageException {
return false;
}

@Override
public boolean save(FileListField field, String path, InputStream stream) throws StorageException {
return false;
}

@Override
public String getPreviewPath(String caseId, String fieldId, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.netgrif.application.engine.workflow.service.interfaces.IDataValidationExpressionEvaluator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -30,6 +31,9 @@
@Slf4j
public final class FieldFactory {

@Value("${nae.storage.default-type}")
private String defaultStorageType;

@Autowired
private FormatFactory formatFactory;

Expand Down Expand Up @@ -511,7 +515,7 @@ private UserListField buildUserListField(Data data, Importer importer) {

private FileField buildFileField(Data data) {
FileField fileField = new FileField();
fileField.setStorageType(data.getRemote() == null ? StorageType.LOCAL : StorageType.fromString(data.getRemote()));
fileField.setStorageType(data.getRemote() == null ? StorageType.fromString(defaultStorageType) : StorageType.fromString(data.getRemote()));
setDefaultValue(fileField, data, defaultValue -> {
if (defaultValue != null) {
fileField.setDefaultValue(defaultValue);
Expand All @@ -522,7 +526,7 @@ private FileField buildFileField(Data data) {

private FileListField buildFileListField(Data data) {
FileListField fileListField = new FileListField();
fileListField.setStorageType(data.getRemote() == null ? StorageType.LOCAL : StorageType.fromString(data.getRemote()));
fileListField.setStorageType(data.getRemote() == null ? StorageType.fromString(defaultStorageType) : StorageType.fromString(data.getRemote()));
setDefaultValues(fileListField, data, defaultValues -> {
if (defaultValues != null && !defaultValues.isEmpty()) {
fileListField.setDefaultValue(defaultValues);
Expand Down
Loading

0 comments on commit c3e8a2a

Please sign in to comment.