Added FileService
This commit is contained in:
parent
2dae2bb905
commit
8d11279b6e
|
@ -0,0 +1,50 @@
|
|||
package nl.andrewlalis.gymboardcdn;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class FileService {
|
||||
@Value("${app.files.storage-dir}")
|
||||
private String storageDir;
|
||||
|
||||
@Value("${app.files.temp-dir}")
|
||||
private String tempDir;
|
||||
|
||||
public Path saveToTempFile(MultipartFile file) throws IOException {
|
||||
Path tempDir = getTempDir();
|
||||
String suffix = null;
|
||||
String filename = file.getOriginalFilename();
|
||||
if (filename != null) {
|
||||
int idx = filename.lastIndexOf('.');
|
||||
if (idx >= 0) {
|
||||
suffix = filename.substring(idx);
|
||||
}
|
||||
}
|
||||
Path tempFile = Files.createTempFile(tempDir, null, suffix);
|
||||
file.transferTo(tempFile);
|
||||
return tempFile;
|
||||
}
|
||||
|
||||
private Path getStorageDir() throws IOException {
|
||||
Path dir = Path.of(storageDir);
|
||||
if (Files.notExists(dir)) {
|
||||
Files.createDirectories(dir);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
private Path getTempDir() throws IOException {
|
||||
Path dir = Path.of(tempDir);
|
||||
if (Files.notExists(dir)) {
|
||||
Files.createDirectories(dir);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
server.port=8082
|
||||
|
||||
app.web-origin=http://localhost:9000
|
||||
app.files.storage-dir=./cdn-files/
|
||||
app.files.temp-dir=./cdn-files/tmp/
|
||||
|
|
Loading…
Reference in New Issue