diff --git a/src/main/java/ru/di9/mirror/services/FileStorageService.java b/src/main/java/ru/di9/mirror/services/FileStorageService.java new file mode 100644 index 0000000..b31fe8c --- /dev/null +++ b/src/main/java/ru/di9/mirror/services/FileStorageService.java @@ -0,0 +1,114 @@ +package ru.di9.mirror.services; + +import org.springframework.stereotype.Service; +import ru.di9.mirror.Utils; +import ru.di9.mirror.config.MavenConfig; +import ru.di9.mirror.domain.FileRecord; +import ru.di9.mirror.domain.PathRecord; +import ru.di9.mirror.domain.PathType; +import ru.di9.mirror.domain.WalkerResult; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Stream; + +@Service +public class FileStorageService implements StorageService { + + private final MavenConfig config; + + public FileStorageService(MavenConfig config) { + this.config = config; + } + + @SuppressWarnings("java:S112") + @Override + public Optional getFile(String path) { + Path fullPath = storageResolve(path); + + if (Files.notExists(fullPath)) { + return Optional.empty(); + } + + try { + return Optional.of(new FileRecord( + fullPath.toFile().getName(), + Files.size(fullPath), + Files.newInputStream(fullPath) + )); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("java:S112") + @Override + public void putFile(String path, InputStream inputStream) { + Path fullPath = storageResolve(path); + try { + Files.createDirectories(fullPath.getParent()); + } catch (IOException e) { + throw new RuntimeException(e); + } + + try (OutputStream outputStream = Files.newOutputStream(fullPath)) { + byte[] buff = new byte[8 * 1024]; + int len; + while ((len = inputStream.read(buff)) > 0) { + outputStream.write(buff, 0, len); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("java:S112") + @Override + public Optional walker(String path) { + Path fullPath = storageResolve(path); + + if (Files.notExists(fullPath)) { + return Optional.empty(); + } + + if (Files.isDirectory(fullPath)) { + final List list = new ArrayList<>(); + + try (Stream walk = Files.walk(fullPath, 1)) { + walk.skip(1).forEach(path1 -> list.add(new PathRecord( + path1.getFileName().toString(), + Files.isDirectory(path1) ? PathType.DIRECTORY : PathType.FILE))); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return Optional.of(new WalkerResult(PathType.DIRECTORY, null, list)); + } else { + try { + var fileRecord = new FileRecord( + fullPath.getFileName().toString(), + Files.size(fullPath), + Files.newInputStream(fullPath) + ); + + return Optional.of(new WalkerResult(PathType.FILE, fileRecord, null)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private Path storageResolve(String httpPath) { + if (Utils.isEmptyString(httpPath)) { + return config.getStoragePath(); + } else { + return config.getStoragePath().resolve(httpPath.substring(1)); + } + } +} diff --git a/src/main/java/ru/di9/mirror/services/StorageService.java b/src/main/java/ru/di9/mirror/services/StorageService.java index c3065ce..640d6b7 100644 --- a/src/main/java/ru/di9/mirror/services/StorageService.java +++ b/src/main/java/ru/di9/mirror/services/StorageService.java @@ -1,112 +1,16 @@ package ru.di9.mirror.services; -import org.springframework.stereotype.Service; -import ru.di9.mirror.Utils; -import ru.di9.mirror.config.MavenConfig; import ru.di9.mirror.domain.FileRecord; -import ru.di9.mirror.domain.PathRecord; -import ru.di9.mirror.domain.PathType; import ru.di9.mirror.domain.WalkerResult; -import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import java.util.Optional; -import java.util.stream.Stream; -@SuppressWarnings("ClassCanBeRecord") -@Service -public class StorageService { +public interface StorageService { - private final MavenConfig config; + Optional getFile(String path); - public StorageService(MavenConfig config) { - this.config = config; - } + void putFile(String path, InputStream inputStream); - @SuppressWarnings("java:S112") - public Optional getFile(String path) { - Path fullPath = storageResolve(path); - - if (Files.notExists(fullPath)) { - return Optional.empty(); - } - - try { - return Optional.of(new FileRecord( - fullPath.toFile().getName(), - Files.size(fullPath), - Files.newInputStream(fullPath) - )); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @SuppressWarnings("java:S112") - public void putFile(String path, InputStream inputStream) { - Path fullPath = storageResolve(path); - try { - Files.createDirectories(fullPath.getParent()); - } catch (IOException e) { - throw new RuntimeException(e); - } - - try (OutputStream outputStream = Files.newOutputStream(fullPath)) { - byte[] buff = new byte[8 * 1024]; - int len; - while ((len = inputStream.read(buff)) > 0) { - outputStream.write(buff, 0, len); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @SuppressWarnings("java:S112") - public Optional walker(String path) { - Path fullPath = storageResolve(path); - - if (Files.notExists(fullPath)) { - return Optional.empty(); - } - - if (Files.isDirectory(fullPath)) { - final List list = new ArrayList<>(); - - try (Stream walk = Files.walk(fullPath, 1)) { - walk.skip(1).forEach(path1 -> list.add(new PathRecord( - path1.getFileName().toString(), - Files.isDirectory(path1) ? PathType.DIRECTORY : PathType.FILE))); - } catch (IOException e) { - throw new RuntimeException(e); - } - - return Optional.of(new WalkerResult(PathType.DIRECTORY, null, list)); - } else { - try { - var filreRecord = new FileRecord( - fullPath.getFileName().toString(), - Files.size(fullPath), - Files.newInputStream(fullPath) - ); - - return Optional.of(new WalkerResult(PathType.FILE, filreRecord, null)); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - private Path storageResolve(String httpPath) { - if (Utils.isEmptyString(httpPath)) { - return config.getStoragePath(); - } else { - return config.getStoragePath().resolve(httpPath.substring(1)); - } - } + Optional walker(String path); }