Update FileService.java

- Refactored code
- Added simple getFileById()
This commit is contained in:
Max W. 2024-02-04 22:55:11 +01:00
parent 05720583ba
commit f3939d9f13

View File

@ -35,7 +35,25 @@ public class FileService {
this.fileUploadRepository = fileUploadRepository;
}
public void processUploadedFile(MultipartFile file, String uploaderIp) {
@PostConstruct
public void createFolder() {
Path path = Paths.get(getTempDirPath());
log.debug("Checking temp file directory " + path.getFileName());
try {
if (!Files.exists(path)) {
Files.createDirectory(path);
log.debug("Directory created");
} else {
log.debug("Directory already exists. Clearing content.");
FileUtils.cleanDirectory(new File(getTempDirPath()));
log.debug("Directory content cleared.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public String processUploadedFile(MultipartFile file, String uploaderIp) {
String fileId = fileIdService.generateNewUniqueId();
@ -58,30 +76,18 @@ public class FileService {
try {
Files.write(path, file.getBytes());
log.debug("File " + file.getOriginalFilename() + " written to " + path.getFileName());
return fileId;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
// create a folder
@PostConstruct
public void createFolder() {
Path path = Paths.get(getTempDirPath());
log.debug("Checking temp file directory " + path.getFileName());
try {
if (!Files.exists(path)) {
Files.createDirectory(path);
log.debug("Directory created");
} else {
log.debug("Directory already exists. Clearing content.");
FileUtils.cleanDirectory(new File(getTempDirPath()));
log.debug("Directory content cleared.");
}
} catch (IOException e) {
e.printStackTrace();
}
public File getFileById(String fileId) {
return new File(getTempDirPath() + File.separator + fileId);
}
private String getTempDirPath() {
return System.getProperty("user.dir") + tempDirPath;
}