Skip to content

Commit

Permalink
refactor(#2708): Make all methods of FileManager none static
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe committed Apr 7, 2024
1 parent 45528b2 commit 0ed7c88
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public byte[] generateExportPackage() throws IOException {
String filename = fileResolver.findDocument(item.getResourceId()).getFilename();
addDoc(builder, item, new FileResolver(), manifest::addFile);
try {
builder.addBinary(filename, Files.readAllBytes(FileManager.getFile(filename).toPath()));
builder.addBinary(filename, Files.readAllBytes(new FileManager().getFile(filename).toPath()));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@

public class FileManager {

public static List<FileMetadata> getAllFiles() {
public List<FileMetadata> getAllFiles() {
return getAllFiles(null);
}

public static List<FileMetadata> getAllFiles(String filetypes) {
public List<FileMetadata> getAllFiles(String filetypes) {
List<FileMetadata> allFiles = getFileMetadataStorage().getAllFileMetadataDescriptions();
return filetypes != null ? filterFiletypes(allFiles, filetypes) : allFiles;
}

public static File getFile(String filename) {
public File getFile(String filename) {
return new FileHandler().getFile(filename);
}

Expand All @@ -56,7 +56,7 @@ public static File getFile(String filename) {
* @param fileInputStream content of file
* @return metadata of file
*/
public static FileMetadata storeFile(String user,
public FileMetadata storeFile(String user,
String filename,
InputStream fileInputStream) throws IOException {

Expand All @@ -72,13 +72,13 @@ public static FileMetadata storeFile(String user,
}


public static void deleteFile(String id) {
public void deleteFile(String id) {
FileMetadata fileMetadata = getFileMetadataStorage().getMetadataById(id);
new FileHandler().deleteFile(fileMetadata.getFilename());
getFileMetadataStorage().deleteFileMetadata(id);
}

private static InputStream validateFileNameAndCleanFile(String filename,
private InputStream validateFileNameAndCleanFile(String filename,
String filetype,
InputStream fileInputStream) {
if (!validateFileType(filename)) {
Expand All @@ -95,42 +95,42 @@ private static InputStream validateFileNameAndCleanFile(String filename,
* @param filetype file of type
* @return input stream without BOM
*/
protected static InputStream cleanFile(InputStream fileInputStream, String filetype) {
protected InputStream cleanFile(InputStream fileInputStream, String filetype) {
if (Filetypes.CSV.getFileExtensions().contains(filetype.toLowerCase())) {
fileInputStream = new BOMInputStream(fileInputStream);
}

return fileInputStream;
}

public static boolean checkFileContentChanged(String filename, String hash) throws IOException {
public boolean checkFileContentChanged(String filename, String hash) throws IOException {
var fileHash = FileHasher.hash(getFile(filename));
return !fileHash.equals(hash);
}

public static String sanitizeFilename(String filename) {
public String sanitizeFilename(String filename) {
return filename.replaceAll("[^a-zA-Z0-9.\\-]", "_");
}

public static boolean validateFileType(String filename) {
public boolean validateFileType(String filename) {
return Filetypes.getAllFileExtensions()
.stream()
.anyMatch(filename::endsWith);
}

protected static void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException {
protected void writeToFile(String sanitizedFilename, InputStream fileInputStream) throws IOException {
new FileHandler().storeFile(sanitizedFilename, fileInputStream);
}


private static IFileMetadataStorage getFileMetadataStorage() {
private IFileMetadataStorage getFileMetadataStorage() {
return StorageDispatcher
.INSTANCE
.getNoSqlStore()
.getFileMetadataStorage();
}

protected static FileMetadata makeAndStoreFileMetadata(String user,
protected FileMetadata makeAndStoreFileMetadata(String user,
String sanitizedFilename,
String filetype) {
var fileMetadata = makeFileMetadata(user, sanitizedFilename, filetype);
Expand All @@ -139,7 +139,7 @@ protected static FileMetadata makeAndStoreFileMetadata(String user,
return fileMetadata;
}

private static FileMetadata makeFileMetadata(String user,
private FileMetadata makeFileMetadata(String user,
String filename,
String filetype) {

Expand All @@ -152,12 +152,12 @@ private static FileMetadata makeFileMetadata(String user,
return fileMetadata;
}

private static void storeFileMetadata(FileMetadata fileMetadata) {
private void storeFileMetadata(FileMetadata fileMetadata) {
getFileMetadataStorage().addFileMetadata(fileMetadata);
}


private static List<FileMetadata> filterFiletypes(List<FileMetadata> allFiles, String filetypes) {
private List<FileMetadata> filterFiletypes(List<FileMetadata> allFiles, String filetypes) {
return allFiles
.stream()
.filter(fileMetadata -> Arrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.manager.file;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -32,19 +33,26 @@

public class TestFileManager {

private FileManager fileManager;

@BeforeEach
public void setup() {
fileManager = new FileManager();
}

@Test
public void storeFile_throwsExceptionForInvalidFileType() {
var filename = "testFile.invalid";

assertThrows(IllegalArgumentException.class, () ->
FileManager.storeFile("", filename, mock(InputStream.class)));
fileManager.storeFile("", filename, mock(InputStream.class)));
}

@Test
public void testCleanFileWithoutBom() throws IOException {
var expected = "test";
var inputStream = IOUtils.toInputStream(expected, StandardCharsets.UTF_8);
var resultStream = FileManager.cleanFile(inputStream, "CSV");
var resultStream = fileManager.cleanFile(inputStream, "CSV");
var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8);

assertEquals(expected, resultString);
Expand All @@ -56,7 +64,7 @@ public void testCleanFileWithBom() throws IOException {
var utf8Bom = "\uFEFF";
var inputString = utf8Bom + expected;
var inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8);
var resultStream = FileManager.cleanFile(inputStream, "CSV");
var resultStream = fileManager.cleanFile(inputStream, "CSV");
var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8);

assertEquals(expected, resultString);
Expand All @@ -68,7 +76,7 @@ public void testCleanFileWithBomAndUmlauts() throws IOException {
var utf8Bom = "\uFEFF";
var inputString = utf8Bom + expected;
var inputStream = IOUtils.toInputStream(inputString, StandardCharsets.UTF_8);
var resultStream = FileManager.cleanFile(inputStream, "CSV");
var resultStream = fileManager.cleanFile(inputStream, "CSV");
var resultString = IOUtils.toString(resultStream, StandardCharsets.UTF_8);

assertEquals(expected, resultString);
Expand All @@ -77,57 +85,57 @@ public void testCleanFileWithBomAndUmlauts() throws IOException {
@Test
public void sanitizeFilename_replacesNonAlphanumericCharactersWithUnderscore() {
var filename = "file@name#with$special%characters";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals("file_name_with_special_characters", sanitizedFilename);
}

@Test
public void sanitizeFilename_keepsAlphanumericAndDotAndHyphenCharacters() {
var filename = "file.name-with_alphanumeric123";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals(filename, sanitizedFilename);
}

@Test
public void sanitizeFilename_returnsUnderscoreForFilenameWithAllSpecialCharacters() {
var filename = "@#$%^&*()";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals("_________", sanitizedFilename);
}

@Test
public void sanitizeFilename_returnsEmptyStringForEmptyFilename() {
var filename = "";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals("", sanitizedFilename);
}

@Test
public void sanitizeFilename_removesSingleParentDirectory() {
var filename = "../file.csv";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals(".._file.csv", sanitizedFilename);
}

@Test
public void sanitizeFilename_removesDoubleParentDirectoryy() {
var filename = "../../file";
var sanitizedFilename = FileManager.sanitizeFilename(filename);
var sanitizedFilename = fileManager.sanitizeFilename(filename);
assertEquals(".._.._file", sanitizedFilename);
}

@Test
public void validateFileName_returnsTrueForCsv() {
assertTrue(FileManager.validateFileType("file.csv"));
assertTrue(fileManager.validateFileType("file.csv"));
}

@Test
public void validateFileName_returnsTrueForJson() {
assertTrue(FileManager.validateFileType("file.json"));
assertTrue(fileManager.validateFileType("file.json"));
}

@Test
public void validateFileName_returnsFalseForSh() {
assertFalse(FileManager.validateFileType("file.sh"));
assertFalse(fileManager.validateFileType("file.sh"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ private static void stopAndDeleteAllAdapters() {
}

private static void deleteAllFiles() {
List<FileMetadata> allFiles = FileManager.getAllFiles();
allFiles.forEach(fileMetadata -> FileManager.deleteFile(fileMetadata.getFileId()));
var fileManager = new FileManager();
List<FileMetadata> allFiles = fileManager.getAllFiles();
allFiles.forEach(fileMetadata -> fileManager.deleteFile(fileMetadata.getFileId()));
}

private static void removeAllDataInDataLake() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public ResponseEntity<Void> deleteAssetDashboard(@PathVariable("dashboardId") St
@GetMapping(path = "/images/{imageName}")
public ResponseEntity<byte[]> getDashboardImage(@PathVariable("imageName") String imageName) {
try {
var sanitizedFileName = FileManager.sanitizeFilename(imageName);
var sanitizedFileName = new FileManager().sanitizeFilename(imageName);
java.nio.file.Path path = Paths.get(getTargetFile(sanitizedFileName));
File file = new File(path.toString());
FileNameMap fileNameMap = URLConnection.getFileNameMap();
Expand All @@ -128,7 +128,7 @@ public ResponseEntity<Void> storeDashboardImage(@RequestPart("file_upload") Mult
try {
var fileName = fileDetail.getName();

if (!FileManager.validateFileType(fileName)) {
if (!new FileManager().validateFileType(fileName)) {
LOG.error("File type of file " + fileName + " is not supported");
fail();
}
Expand All @@ -148,7 +148,7 @@ private void storeFile(String fileName, InputStream fileInputStream) {
targetDirectory.mkdirs();
}

var sanitizedFileName = FileManager.sanitizeFilename(fileName);
var sanitizedFileName = new FileManager().sanitizeFilename(fileName);

var targetFile = new File(getTargetFile(sanitizedFileName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@
@RequestMapping("/api/v2/files")
public class PipelineElementFile extends AbstractAuthGuardedRestResource {

private final FileManager fileManager;

public PipelineElementFile() {
this.fileManager = new FileManager();
}

@PostMapping(
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize(AuthConstants.IS_ADMIN_ROLE)
public ResponseEntity<?> storeFile(@RequestPart("file_upload") MultipartFile fileDetail) {
try {
FileMetadata metadata =
FileManager.storeFile(
fileManager.storeFile(
getAuthenticatedUsername(),
fileDetail.getOriginalFilename(),
fileDetail.getInputStream()
Expand All @@ -74,7 +80,7 @@ public ResponseEntity<?> storeFile(@RequestPart("file_upload") MultipartFile fil
@DeleteMapping(path = "{fileId}")
@PreAuthorize(AuthConstants.IS_ADMIN_ROLE)
public ResponseEntity<Void> deleteFile(@PathVariable("fileId") String fileId) {
FileManager.deleteFile(fileId);
fileManager.deleteFile(fileId);
return ok();
}

Expand All @@ -83,7 +89,7 @@ public ResponseEntity<Void> deleteFile(@PathVariable("fileId") String fileId) {
public ResponseEntity<List<FileMetadata>> getFileInfo(
@RequestParam(value = "filetypes", required = false) String filetypes
) {
return ok(FileManager.getAllFiles(filetypes));
return ok(fileManager.getAllFiles(filetypes));
}

@GetMapping(path = "/{filename}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
Expand All @@ -109,7 +115,7 @@ public ResponseEntity<byte[]> getFile(
@PathVariable("filename") String filename
) {
try {
return ok(getFileContents(FileManager.getFile(filename)));
return ok(getFileContents(fileManager.getFile(filename)));
} catch (IOException e) {
throw new SpMessageException(
NOT_FOUND,
Expand All @@ -125,7 +131,7 @@ private byte[] getFileContents(File file) throws IOException {
@GetMapping(path = "/allFilenames", produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize(AuthConstants.HAS_READ_FILE_PRIVILEGE)
public ResponseEntity<List<String>> getAllOriginalFilenames() {
return ok(FileManager.getAllFiles()
return ok(fileManager.getAllFiles()
.stream()
.map(fileMetadata -> fileMetadata.getFilename()
.toLowerCase())
Expand All @@ -141,7 +147,7 @@ public ResponseEntity<Boolean> checkFileContentChanged(
@PathVariable(value = "hash") String hash
) {
try {
return ok(FileManager.checkFileContentChanged(filename, hash));
return ok(fileManager.checkFileContentChanged(filename, hash));
} catch (IOException e) {
throw new SpMessageException(
NOT_FOUND,
Expand Down

0 comments on commit 0ed7c88

Please sign in to comment.