refactoring
This commit is contained in:
@@ -4,7 +4,7 @@ import io.minio.MinioClient;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.di9.mirror.core.domain.ItemRecord2;
|
||||
import ru.di9.mirror.core.domain.ItemRecord;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
@@ -35,7 +35,7 @@ class MinioServiceIntTest {
|
||||
|
||||
@Test
|
||||
void list() {
|
||||
List<ItemRecord2> list = minioService.list("/local/ghast/ghast-tools/");
|
||||
List<ItemRecord> list = minioService.list("/local/ghast/ghast-tools/");
|
||||
|
||||
assertNotNull(list);
|
||||
assertFalse(list.isEmpty());
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package ru.di9.mirror.core;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@UtilityClass
|
||||
public final class Utils {
|
||||
|
||||
public static boolean isEmptyString(String string) {
|
||||
public boolean isEmptyString(String string) {
|
||||
return string == null || string.isEmpty() || string.isBlank();
|
||||
}
|
||||
|
||||
public static boolean isNotEmptyString(String string) {
|
||||
public boolean isNotEmptyString(String string) {
|
||||
return !isEmptyString(string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import io.minio.Result;
|
||||
import io.minio.messages.Item;
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
public record ItemRecord2(Result<Item> itemResult, String prefix) {
|
||||
public record ItemRecord(Result<Item> itemResult, String prefix) {
|
||||
|
||||
@SneakyThrows
|
||||
public boolean isDir() {
|
||||
@@ -1,6 +0,0 @@
|
||||
package ru.di9.mirror.core.domain;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public record MavenHandlerGetFileResponse(String name, InputStream inputStream) {
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package ru.di9.mirror.core.domain;
|
||||
|
||||
public record WalkerResult(PathType type) {
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.di9.mirror.core.handler;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import ru.di9.mirror.core.domain.ItemRecord2;
|
||||
import ru.di9.mirror.core.domain.ItemRecord;
|
||||
import ru.di9.mirror.core.service.MinioService;
|
||||
|
||||
import java.util.List;
|
||||
@@ -11,7 +11,7 @@ public class IndexOfHandler {
|
||||
|
||||
private final MinioService minioService;
|
||||
|
||||
public List<ItemRecord2> walker(String path) {
|
||||
public List<ItemRecord> walker(String path) {
|
||||
return minioService.list("/local/" + path);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.di9.mirror.core.handler;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import ru.di9.mirror.core.domain.MavenHandlerGetFileResponse;
|
||||
import ru.di9.mirror.core.handler.response.GetFileResponse;
|
||||
import ru.di9.mirror.core.service.ExternalMavenService;
|
||||
import ru.di9.mirror.core.service.MinioService;
|
||||
|
||||
@@ -15,13 +15,13 @@ public class MavenHandler {
|
||||
private final MinioService minioService;
|
||||
private final List<ExternalMavenService> externalMavenServices;
|
||||
|
||||
public Optional<MavenHandlerGetFileResponse> getFile(String path) {
|
||||
public Optional<GetFileResponse> getFile(String path) {
|
||||
final String fileName = path.substring(path.lastIndexOf("/") + 1);
|
||||
|
||||
Optional<InputStream> optionalInputStream = minioService.get("/local/" + path);
|
||||
if (optionalInputStream.isPresent()) {
|
||||
return optionalInputStream
|
||||
.map(inputStream -> new MavenHandlerGetFileResponse(fileName, inputStream));
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
} else {
|
||||
for (ExternalMavenService externalMavenService : externalMavenServices) {
|
||||
final String nameForStore = "/" + externalMavenService.getId() + "/" + path;
|
||||
@@ -29,13 +29,13 @@ public class MavenHandler {
|
||||
optionalInputStream = minioService.get(nameForStore);
|
||||
if (optionalInputStream.isPresent()) {
|
||||
return optionalInputStream
|
||||
.map(inputStream -> new MavenHandlerGetFileResponse(fileName, inputStream));
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
} else {
|
||||
optionalInputStream = externalMavenService.getFile(path);
|
||||
if (optionalInputStream.isPresent()) {
|
||||
minioService.put(nameForStore, optionalInputStream.get());
|
||||
return minioService.get(nameForStore)
|
||||
.map(inputStream -> new MavenHandlerGetFileResponse(fileName, inputStream));
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package ru.di9.mirror.core.handler.response;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public record GetFileResponse(String name, InputStream inputStream) {
|
||||
}
|
||||
@@ -5,12 +5,9 @@ import io.minio.errors.*;
|
||||
import io.minio.messages.Item;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import ru.di9.mirror.core.domain.ItemRecord2;
|
||||
import ru.di9.mirror.core.domain.ItemRecord;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -48,7 +45,7 @@ public class MinioService {
|
||||
}
|
||||
}
|
||||
|
||||
public List<ItemRecord2> list(String prefix) {
|
||||
public List<ItemRecord> list(String prefix) {
|
||||
if (!prefix.endsWith("/")) {
|
||||
prefix = prefix + "/";
|
||||
}
|
||||
@@ -58,9 +55,9 @@ public class MinioService {
|
||||
.prefix(prefix)
|
||||
.build());
|
||||
|
||||
List<ItemRecord2> list = new ArrayList<>();
|
||||
List<ItemRecord> list = new ArrayList<>();
|
||||
for (Result<Item> result : results) {
|
||||
list.add(new ItemRecord2(result, prefix));
|
||||
list.add(new ItemRecord(result, prefix));
|
||||
}
|
||||
|
||||
return list;
|
||||
|
||||
@@ -7,7 +7,7 @@ import io.minio.messages.Item;
|
||||
import lombok.SneakyThrows;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.di9.mirror.core.domain.ItemRecord2;
|
||||
import ru.di9.mirror.core.domain.ItemRecord;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -32,7 +32,7 @@ class MinioServiceTest {
|
||||
@Test
|
||||
void list() {
|
||||
MinioService minioService = new MinioService(mockMinioClient, bucket);
|
||||
List<ItemRecord2> list = minioService.list("/ghast/ghast-tools/");
|
||||
List<ItemRecord> list = minioService.list("/ghast/ghast-tools/");
|
||||
|
||||
assertNotNull(list);
|
||||
assertFalse(list.isEmpty());
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import ru.di9.mirror.core.Utils;
|
||||
import ru.di9.mirror.core.domain.ItemRecord2;
|
||||
import ru.di9.mirror.core.domain.ItemRecord;
|
||||
import ru.di9.mirror.core.handler.IndexOfHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -28,7 +28,7 @@ public class IndexOfMavenController {
|
||||
@ModelAttribute("model") ModelMap model) {
|
||||
String path = correctingHttpPath(httpPath);
|
||||
|
||||
List<ItemRecord2> walker = indexOfHandler.walker(path);
|
||||
List<ItemRecord> walker = indexOfHandler.walker(path);
|
||||
List<ModelLink> links = new ArrayList<>();
|
||||
|
||||
StringBuilder sb = new StringBuilder("/maven");
|
||||
@@ -46,13 +46,13 @@ public class IndexOfMavenController {
|
||||
sb.setLength(resetLength);
|
||||
sb.append("/");
|
||||
resetLength = sb.length();
|
||||
for (ItemRecord2 itemRecord2 : walker) {
|
||||
for (ItemRecord itemRecord : walker) {
|
||||
if (Utils.isNotEmptyString(path)) {
|
||||
sb.append(path).append("/");
|
||||
}
|
||||
sb.append(itemRecord2.name());
|
||||
sb.append(itemRecord.name());
|
||||
|
||||
links.add(new ModelLink(itemRecord2.name() + (itemRecord2.isDir() ? "/" : ""), sb.toString()));
|
||||
links.add(new ModelLink(itemRecord.name() + (itemRecord.isDir() ? "/" : ""), sb.toString()));
|
||||
sb.setLength(resetLength);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user