diff --git a/src/main/java/site/easystartup/easystartupcore/config/AppConfig.java b/src/main/java/site/easystartup/easystartupcore/config/AppConfig.java new file mode 100644 index 0000000..72de475 --- /dev/null +++ b/src/main/java/site/easystartup/easystartupcore/config/AppConfig.java @@ -0,0 +1,4 @@ +package site.easystartup.easystartupcore.config; + +public class AppConfig { +} diff --git a/src/main/java/site/easystartup/easystartupcore/config/FileConfig.java b/src/main/java/site/easystartup/easystartupcore/config/FileConfig.java new file mode 100644 index 0000000..8208195 --- /dev/null +++ b/src/main/java/site/easystartup/easystartupcore/config/FileConfig.java @@ -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(); + } +} diff --git a/src/main/java/site/easystartup/easystartupcore/storage/FileLocalStorageService.java b/src/main/java/site/easystartup/easystartupcore/storage/FileLocalStorageService.java new file mode 100644 index 0000000..3786d50 --- /dev/null +++ b/src/main/java/site/easystartup/easystartupcore/storage/FileLocalStorageService.java @@ -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 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!"); + } + } +} diff --git a/src/main/java/site/easystartup/easystartupcore/storage/StorageFileNotFoundException.java b/src/main/java/site/easystartup/easystartupcore/storage/StorageFileNotFoundException.java new file mode 100644 index 0000000..03021fd --- /dev/null +++ b/src/main/java/site/easystartup/easystartupcore/storage/StorageFileNotFoundException.java @@ -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); + } +} diff --git a/src/main/java/site/easystartup/easystartupcore/storage/StorageService.java b/src/main/java/site/easystartup/easystartupcore/storage/StorageService.java new file mode 100644 index 0000000..dd6a170 --- /dev/null +++ b/src/main/java/site/easystartup/easystartupcore/storage/StorageService.java @@ -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 loadAll(); +} \ No newline at end of file