Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allowing any extensions when uploading ingestionFlowFile #31

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package it.gov.pagopa.pu.fileshare.service;

import it.gov.pagopa.pu.fileshare.exception.custom.InvalidFileException;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Stream;

@Slf4j
@Service
public class FileService {

public void validateFile(MultipartFile ingestionFlowFile, String validFileExt) {
public void validateFile(MultipartFile ingestionFlowFile) {
if( ingestionFlowFile == null){
log.debug("Invalid ingestion flow file");
throw new InvalidFileException("Invalid file");
}
String filename = StringUtils.defaultString(ingestionFlowFile.getOriginalFilename());
validateFileExtension(validFileExt, filename);
validateFilename(filename);
}

Expand All @@ -28,10 +28,4 @@ public static void validateFilename(String filename) {
}
}

private static void validateFileExtension(String validFileExt, String filename) {
if(!filename.endsWith(validFileExt)){
log.debug("Invalid ingestion flow file extension");
throw new InvalidFileException("Invalid file extension");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class IngestionFlowFileFacadeServiceImpl implements IngestionFlowFileFaca
private final FoldersPathsConfig foldersPathsConfig;
private final IngestionFlowFileService ingestionFlowFileService;
private final IngestionFlowFileDTOMapper ingestionFlowFileDTOMapper;
private final String validIngestionFlowFileExt;
private final String archivedSubFolder;

public IngestionFlowFileFacadeServiceImpl(
Expand All @@ -46,7 +45,6 @@ public IngestionFlowFileFacadeServiceImpl(
FoldersPathsConfig foldersPathsConfig,
IngestionFlowFileService ingestionFlowFileService,
IngestionFlowFileDTOMapper ingestionFlowFileDTOMapper,
@Value("${uploads.ingestion-flow-file.valid-extension}") String validIngestionFlowFileExt,
@Value("${folders.process-target-sub-folders.archive}") String archivedSubFolder
) {
this.userAuthorizationService = userAuthorizationService;
Expand All @@ -55,7 +53,6 @@ public IngestionFlowFileFacadeServiceImpl(
this.foldersPathsConfig = foldersPathsConfig;
this.ingestionFlowFileService = ingestionFlowFileService;
this.ingestionFlowFileDTOMapper = ingestionFlowFileDTOMapper;
this.validIngestionFlowFileExt = validIngestionFlowFileExt;
this.archivedSubFolder = archivedSubFolder;
}

Expand All @@ -64,7 +61,7 @@ public Long uploadIngestionFlowFile(Long organizationId, IngestionFlowFileType i
FileOrigin fileOrigin, String fileName, MultipartFile ingestionFlowFile,
UserInfo user, String accessToken) {
userAuthorizationService.checkUserAuthorization(organizationId, user, accessToken);
fileService.validateFile(ingestionFlowFile, validIngestionFlowFileExt);
fileService.validateFile(ingestionFlowFile);

String ingestionFlowFilePath = foldersPathsConfig.getIngestionFlowFilePath(ingestionFlowFileType);

Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,5 @@ rest:
wait-time-millis: "\${PROCESS_EXECUTIONS_WAIT_TIME_MILLIS:500}"
print-body-when-error: "\${PROCESS_EXECUTIONS_PRINT_BODY_WHEN_ERROR:true}"

uploads:
ingestion-flow-file:
valid-extension: "\${INGESTION_FLOW_FILE_VALID_EXTENSION:.zip}"

app:
fileEncryptPassword: "\${FILE_ENCRYPT_PASSWORD:ENCR_PSW}"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@ExtendWith(MockitoExtension.class)
class FileServiceTest {
private FileService fileService;
private static final String VALID_FILE_EXTENSION = ".zip";

@BeforeEach
void setUp() {
Expand All @@ -23,35 +22,18 @@ void setUp() {
void givenValidFileExtensionWhenValidateFileThenOk(){
MockMultipartFile file = new MockMultipartFile(
"ingestionFlowFile",
"test"+VALID_FILE_EXTENSION,
"test.zip",
MediaType.TEXT_PLAIN_VALUE,
"this is a test file".getBytes()
);

fileService.validateFile(file, VALID_FILE_EXTENSION);
fileService.validateFile(file);
}

@Test
void givenNoFileWhenValidateFileThenInvalidFileException(){
try{
fileService.validateFile(null, VALID_FILE_EXTENSION);
Assertions.fail("Expected InvalidFileException");
}catch(InvalidFileException e){
//do nothing
}
}

@Test
void givenInvalidFileExtensionWhenValidateFileThenInvalidFileException(){
MockMultipartFile file = new MockMultipartFile(
"ingestionFlowFile",
"test.txt",
MediaType.TEXT_PLAIN_VALUE,
"this is a test file".getBytes()
);

try{
fileService.validateFile(file, VALID_FILE_EXTENSION);
fileService.validateFile(null);
Assertions.fail("Expected InvalidFileException");
}catch(InvalidFileException e){
//do nothing
Expand All @@ -68,7 +50,7 @@ void givenInvalidFilenameWhenValidateFileThenInvalidFileException(){
);

try{
fileService.validateFile(file, VALID_FILE_EXTENSION);
fileService.validateFile(file);
Assertions.fail("Expected InvalidFileException");
}catch(InvalidFileException e){
//do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class IngestionFlowFileFacadeServiceImplTest {
@Mock
private IngestionFlowFileDTOMapper ingestionFlowFileDTOMapperMock;
private IngestionFlowFileFacadeServiceImpl ingestionFlowFileService;
private static final String VALID_FILE_EXTENSION = ".zip";
private static final String ARCHIVED_SUB_FOLDER = "Archived";

@BeforeEach
Expand All @@ -59,7 +58,6 @@ void setUp() {
foldersPathsConfigMock,
ingestionFlowFileServiceMock,
ingestionFlowFileDTOMapperMock,
VALID_FILE_EXTENSION,
ARCHIVED_SUB_FOLDER);
}

Expand All @@ -84,7 +82,7 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk() {
String fileName = "fileName.txt";
MockMultipartFile file = new MockMultipartFile(
"ingestionFlowFile",
"test" + VALID_FILE_EXTENSION,
"test.zip",
MediaType.TEXT_PLAIN_VALUE,
"this is a test file".getBytes()
);
Expand All @@ -108,7 +106,7 @@ void givenAuthorizedUserWhenUploadIngestionFlowFileThenOk() {

Assertions.assertSame(expectedIngestionFlowFileId, result);
Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, TestUtils.getSampleUser(), accessToken);
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
Mockito.verify(fileServiceMock).validateFile(file);
}

@Test
Expand Down Expand Up @@ -146,7 +144,7 @@ void givenAlreadyUploadedWhenThenFileAlreadyExistsException() {
fileName, file, userInfo, accessToken));

Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, userInfo, accessToken);
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
Mockito.verify(fileServiceMock).validateFile(file);
}
}

Expand Down Expand Up @@ -191,7 +189,7 @@ void givenAlreadyArchivedWhenThenFileAlreadyExistsException() {
fileName, file, userInfo, accessToken));

Mockito.verify(userAuthorizationServiceMock).checkUserAuthorization(organizationId, userInfo, accessToken);
Mockito.verify(fileServiceMock).validateFile(file, VALID_FILE_EXTENSION);
Mockito.verify(fileServiceMock).validateFile(file);
}
}

Expand Down