Added StorageService.java and some utils for it.

This commit is contained in:
redtea committed 2022-08-28 22:42:59 +03:00
1 parent 7556f6de5d
commit 2ba79f8058
5 files changed
+122

No files matched your search

@@ -0,0 +1,4 @@
package site.easystartup.easystartupcore.config;
public class AppConfig {
}
@@ -0,0 +1,16 @@
package site.easystartup.easystartupcore.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import site.easystartup.easystartupcore.storage.FileLocalStorageService;
import site.easystartup.easystartupcore.storage.StorageService;
@Configuration
public class FileConfig {
@Bean
@Scope("singleton")
public StorageService storageService() {
return new FileLocalStorageService();
}
}
@@ -0,0 +1,79 @@
package site.easystartup.easystartupcore.storage;
import lombok.SneakyThrows;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FileLocalStorageService implements StorageService {
private final Path root = Paths.get("uploads");
public FileLocalStorageService() {
init();
}
@Override
public void init() {
try {
if(!root.toFile().exists())
Files.createDirectory(root);
} catch (IOException e) {
throw new RuntimeException("Could not initialize folder for upload!");
}
}
@Override
public void save(MultipartFile file) {
try {
Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()));
} catch (Exception e) {
System.out.println("Could not store the file. Error: " + e.getMessage());
}
}
@SneakyThrows
@Override
public void saveImage(MultipartFile file, String name) {
try {
Files.copy(file.getInputStream(), this.root.resolve(name + ".jpg"));
} catch (Exception e) {
Files.delete(this.root.resolve(name + ".jpg"));
Files.copy(file.getInputStream(), this.root.resolve(name + ".jpg"));
}
}
@Override
public Resource load(String filename) {
try {
Path file = root.resolve(filename);
Resource resource = new UrlResource(file.toUri());
if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new RuntimeException("Could not read the file!");
}
} catch (MalformedURLException e) {
throw new RuntimeException("Error: " + e.getMessage());
}
}
@Override
public void deleteAll() {
FileSystemUtils.deleteRecursively(root.toFile());
}
@Override
public Stream<Path> loadAll() {
try {
return Files.walk(this.root, 1).filter(path -> !path.equals(this.root)).map(this.root::relativize);
} catch (IOException e) {
throw new RuntimeException("Could not load the files!");
}
}
}
@@ -0,0 +1,7 @@
package site.easystartup.easystartupcore.storage;
public class StorageFileNotFoundException extends RuntimeException {
public StorageFileNotFoundException(String filename) {
super("Could not find file with name " + filename);
}
}
@@ -0,0 +1,16 @@
package site.easystartup.easystartupcore.storage;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Path;
import java.util.stream.Stream;
public interface StorageService {
void init();
void save(MultipartFile file);
void saveImage(MultipartFile file, String name);
Resource load(String filename);
void deleteAll();
Stream<Path> loadAll();
}