Update FileService.java
- Refactored code - Added simple getFileById()
This commit is contained in:
parent
05720583ba
commit
f3939d9f13
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user