extract interface StorageService
This commit is contained in:
114
src/main/java/ru/di9/mirror/services/FileStorageService.java
Normal file
114
src/main/java/ru/di9/mirror/services/FileStorageService.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,112 +1,16 @@
|
|||||||
package ru.di9.mirror.services;
|
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.FileRecord;
|
||||||
import ru.di9.mirror.domain.PathRecord;
|
|
||||||
import ru.di9.mirror.domain.PathType;
|
|
||||||
import ru.di9.mirror.domain.WalkerResult;
|
import ru.di9.mirror.domain.WalkerResult;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
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.Optional;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@SuppressWarnings("ClassCanBeRecord")
|
public interface StorageService {
|
||||||
@Service
|
|
||||||
public class StorageService {
|
|
||||||
|
|
||||||
private final MavenConfig config;
|
Optional<FileRecord> getFile(String path);
|
||||||
|
|
||||||
public StorageService(MavenConfig config) {
|
void putFile(String path, InputStream inputStream);
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("java:S112")
|
Optional<WalkerResult> walker(String path);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user