extract interface StorageService

This commit is contained in:
2022-04-21 23:01:20 +03:00
parent 10c9eff0ef
commit a1bf130527
2 changed files with 118 additions and 100 deletions

View File

@@ -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<FileRecord> 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<WalkerResult> walker(String path) {
Path fullPath = storageResolve(path);
if (Files.notExists(fullPath)) {
return Optional.empty();
}
if (Files.isDirectory(fullPath)) {
final List<PathRecord> list = new ArrayList<>();
try (Stream<Path> 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));
}
}
}

View File

@@ -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<FileRecord> getFile(String path);
public StorageService(MavenConfig config) {
this.config = config;
}
void putFile(String path, InputStream inputStream);
@SuppressWarnings("java:S112")
public Optional<FileRecord> 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<WalkerResult> walker(String path) {
Path fullPath = storageResolve(path);
if (Files.notExists(fullPath)) {
return Optional.empty();
}
if (Files.isDirectory(fullPath)) {
final List<PathRecord> list = new ArrayList<>();
try (Stream<Path> 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<WalkerResult> walker(String path);
}