информация о SHA1 как метаданные к файлам
This commit is contained in:
@@ -2,4 +2,5 @@ apply plugin: 'java-library'
|
||||
|
||||
dependencies {
|
||||
api("com.squareup.okhttp3:okhttp:${okhttpVersion}")
|
||||
implementation("commons-io:commons-io:${commonsIoVersion}")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package ru.di9.mirror.core.handler;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import ru.di9.mirror.core.domain.FileInfo;
|
||||
import ru.di9.mirror.core.entity.ArtifactEntity;
|
||||
import ru.di9.mirror.core.handler.response.GetFileResponse;
|
||||
@@ -10,6 +11,7 @@ import ru.di9.mirror.core.storage.FileStorage;
|
||||
import ru.di9.mirror.core.service.ExternalMavenService;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -37,11 +39,11 @@ public class MavenHandler {
|
||||
return optionalInputStream
|
||||
.map(inputStream -> new GetFileResponse(fileInfo.name(), inputStream));
|
||||
} else {
|
||||
return findInMirrors(path, fileInfo.name(), artifactEntity);
|
||||
return findInMirrors(path, fileInfo, artifactEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<GetFileResponse> findInMirrors(String path, String fileName, ArtifactEntity artifactEntity) {
|
||||
private Optional<GetFileResponse> findInMirrors(String path, FileInfo fileInfo, ArtifactEntity artifactEntity) {
|
||||
Optional<InputStream> result;
|
||||
for (ExternalMavenService externalMavenService : externalMavenServices) {
|
||||
final String nameForStore = "/" + externalMavenService.getId() + "/" + path;
|
||||
@@ -49,7 +51,7 @@ public class MavenHandler {
|
||||
result = fileStorage.findByName(nameForStore);
|
||||
if (result.isPresent()) {
|
||||
return result
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
.map(inputStream -> new GetFileResponse(fileInfo.name(), inputStream));
|
||||
} else {
|
||||
result = externalMavenService.getFile(path);
|
||||
if (result.isPresent()) {
|
||||
@@ -57,9 +59,18 @@ public class MavenHandler {
|
||||
repository.save(artifactEntity);
|
||||
}
|
||||
|
||||
fileStorage.save(nameForStore, result.get());
|
||||
if (fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
try {
|
||||
String sha1str = IOUtils.toString(result.get(), StandardCharsets.UTF_8);
|
||||
fileStorage.setMetadata(nameForStore.substring(0, nameForStore.lastIndexOf(".")), "sha1", sha1str);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
fileStorage.save(nameForStore, result.get());
|
||||
}
|
||||
return fileStorage.findByName(nameForStore)
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
.map(inputStream -> new GetFileResponse(fileInfo.name(), inputStream));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,7 @@ public interface FileStorage {
|
||||
|
||||
void save(String name, InputStream inputStream);
|
||||
|
||||
void setMetadata(String name, String key, String value);
|
||||
|
||||
List<FileItem> list(String prefix);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import ru.di9.mirror.minio.exception.MinioException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@@ -35,6 +36,19 @@ public class MinioStorage implements FileStorage {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMetadata(String name, String key, String value) {
|
||||
try {
|
||||
minioClient.setObjectTags(SetObjectTagsArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(name)
|
||||
.tags(Map.of(key, value))
|
||||
.build());
|
||||
} catch (Exception e) {
|
||||
throw new MinioException("Error set tags file", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<InputStream> findByName(String name) {
|
||||
try {
|
||||
|
||||
@@ -5,6 +5,7 @@ ext {
|
||||
springBootVerson = '2.6.6'
|
||||
mongoDriver = '4.6.0'
|
||||
okhttpVersion = '4.8.1'
|
||||
commonsIoVersion = '2.11.0'
|
||||
|
||||
// for tests only
|
||||
junitJupiterVersion = '5.5.2'
|
||||
|
||||
Reference in New Issue
Block a user