Added FileService

This commit is contained in:
Andrew Lalis 2023-02-01 08:37:06 +01:00
parent 2dae2bb905
commit 8d11279b6e
2 changed files with 53 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -1,3 +1,5 @@
server.port=8082
app.web-origin=http://localhost:9000
app.web-origin=http://localhost:9000
app.files.storage-dir=./cdn-files/
app.files.temp-dir=./cdn-files/tmp/