Skip to content

Commit

Permalink
fix (bug with fr.insee.rmes.bauhaus_services.MinioFilesOperation.exis…
Browse files Browse the repository at this point in the history
…tsInStorageGestion)
  • Loading branch information
Fabrice B committed Dec 13, 2024
1 parent d89a361 commit f508088
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 32 deletions.
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<groupId>fr.insee.rmes</groupId>
<artifactId>Bauhaus-BO</artifactId>
<packaging>jar</packaging>
<version>4.1.7-beta3</version>
<version>4.1.7-beta4</version>
<name>Bauhaus-Back-Office</name>
<description>Back-office services for Bauhaus</description>
<url>https://github.com/InseeFr/Bauhaus-Back-Office</url>
Expand Down Expand Up @@ -87,6 +87,7 @@
<zt.version>1.17</zt.version>
<saxon.version>12.4</saxon.version>
<minio.version>8.5.11</minio.version>
<testcontainers-minio.version>1.20.4</testcontainers-minio.version>


<!-- SONAR -->
Expand All @@ -100,6 +101,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
Expand Down Expand Up @@ -242,15 +244,30 @@
<version>${flexmark.version}</version>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>minio</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void delete(Path absolutePath) {
}

@Override
public InputStream read(String fileName) {
public InputStream readInDirectoryGestion(String fileName) {
try {
return Files.newInputStream(Paths.get(config.getDocumentsStorageGestion()).resolve(fileName));
} catch (IOException e) {
Expand All @@ -36,12 +36,12 @@ public InputStream read(String fileName) {
}

@Override
public boolean existsInStorage(String filename) {
public boolean existsInStorageGestion(String filename) {
return Files.exists(Paths.get(config.getDocumentsStorageGestion()).resolve(filename));
}

@Override
public void write(InputStream content, Path destPath) {
public void writeToDirectoryGestion(InputStream content, Path destPath) {
try {
Files.copy(content, destPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
Expand All @@ -50,7 +50,7 @@ public void write(InputStream content, Path destPath) {
}

@Override
public void copy(String srcPath, String destPath) {
public void copyFromGestionToPublication(String srcPath, String destPath) {
Path file = Paths.get(srcPath);
Path targetPath = Paths.get(destPath);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import java.nio.file.Path;

public interface FilesOperations {
void delete(Path absolutePath);
InputStream read(String filename);
void write(InputStream content, Path destPath);
void copy(String srcPath, String destPath);
default void delete(Path absolutePath){
throw new UnsupportedOperationException("Not implemented yet.");
}
InputStream readInDirectoryGestion(String filename);
void writeToDirectoryGestion(InputStream content, Path destPath);
void copyFromGestionToPublication(String srcPath, String destPath);

boolean dirExists(Path gestionStorageFolder);

boolean existsInStorage(String filename);
boolean existsInStorageGestion(String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ public record MinioFilesOperation(MinioClient minioClient, String bucketName, St
static final Logger logger = LoggerFactory.getLogger(MinioFilesOperation.class);

@Override
public InputStream read(String pathFile){
String fileName= extractFileName(pathFile);
String objectName = directoryGestion + "/" + fileName;
public InputStream readInDirectoryGestion(String filename){
String objectName = directoryGestion + "/" + filename;

logger.debug("Reading file with name {} from path {} as object {} in bucket {}", fileName, pathFile, objectName, bucketName);
logger.debug("Reading file with name {} from path {} as object {} in bucket {}", filename, filename, objectName, bucketName);

try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
throw new RmesFileException(fileName, "Error reading file: " + fileName+" as object `"+objectName+"` in bucket "+bucketName, e);
throw new RmesFileException(filename, "Error reading file: " + filename+" as object `"+objectName+"` in bucket "+bucketName, e);
}
}
private static String extractFileName(String filePath) {
Expand All @@ -42,8 +41,8 @@ private static String extractFileName(String filePath) {
}

@Override
public boolean existsInStorage(String filename) {
var objectName = extractFileName(requireNonNull(filename));
public boolean existsInStorageGestion(String filename) {
String objectName = directoryGestion + "/" + filename;
logger.debug("Check existence of file with name {} as object {} in bucket {}", filename, objectName, bucketName);
try {
return minioClient.statObject(StatObjectArgs.builder()
Expand All @@ -56,7 +55,7 @@ public boolean existsInStorage(String filename) {
}

@Override
public void write(InputStream content, Path filePath) {
public void writeToDirectoryGestion(InputStream content, Path filePath) {
String filename = filePath.getFileName().toString();
String objectName = directoryGestion + "/" + filename;
logger.debug("Writing to file with name {} from path {} as object {} in bucket {}", filename, filePath, objectName, bucketName);
Expand All @@ -74,7 +73,7 @@ public void write(InputStream content, Path filePath) {
}

@Override
public void copy(String srcObjectName, String destObjectName) {
public void copyFromGestionToPublication(String srcObjectName, String destObjectName) {

String srcObject = directoryGestion + "/" + extractFileName(srcObjectName);
String destObject = directoryPublication + "/" + extractFileName(srcObjectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void publishAllDocumentsInSims(String idSims) throws RmesException {
JSONArray listDoc = docUtils.getListDocumentSims(idSims);

Map<Integer,String> mapIdUrls = new HashMap<>();
listDoc.forEach(doc -> mapIdUrls.put(docUtils.getIdFromJson((JSONObject) doc), docUtils.getDocumentUrlFromDocument((JSONObject) doc)));
listDoc.forEach(doc -> mapIdUrls.put(docUtils.getIdFromJson((JSONObject) doc), DocumentsUtils.getDocumentUrlFromDocument((JSONObject) doc)));

for (Map.Entry<Integer, String> doc : mapIdUrls.entrySet()) {
String docId = doc.getKey().toString();
Expand All @@ -68,10 +68,8 @@ public void publishAllDocumentsInSims(String idSims) throws RmesException {
}

private void copyFileInPublicationFolders(String originalPath){
String documentsStoragePublicationInterne = config.getDocumentsStoragePublicationInterne();
String documentsStoragePublicationExterne = config.getDocumentsStoragePublicationExterne();
filesOperations.copy(originalPath, documentsStoragePublicationInterne);
filesOperations.copy(originalPath, documentsStoragePublicationExterne);
filesOperations.copyFromGestionToPublication(originalPath, documentsStoragePublicationExterne);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ private void uploadFile(InputStream documentFile, String documentName, String ur
throw new RmesBadRequestException(ErrorCodes.DOCUMENT_CREATION_EXISTING_FILE,
"There is already a document with that name.", documentName);
}
filesOperations.write(documentFile, path);
filesOperations.writeToDirectoryGestion(documentFile, path);
// don't throw an error if a file already exists under this name
}

Expand Down Expand Up @@ -662,7 +662,7 @@ protected String getDocumentFilename(String id) throws RmesException {
public ResponseEntity<org.springframework.core.io.Resource> downloadDocumentFile(String id) throws RmesException {
String filePath = getDocumentFilename(id);

try (InputStream inputStream = filesOperations.read(filePath)) { // Lire via l'abstraction et utiliser try-with-resources
try (InputStream inputStream = filesOperations.readInDirectoryGestion(filePath)) { // Lire via l'abstraction et utiliser try-with-resources
byte[] data = StreamUtils.copyToByteArray(inputStream); // Convertir InputStream en byte[]

HttpHeaders headers = new HttpHeaders();
Expand All @@ -689,11 +689,11 @@ private String getFileName(String path) {
}

public InputStream retrieveDocumentFromStorage(String filename) {
return filesOperations.read(filename);
return filesOperations.readInDirectoryGestion(filename);
}

public boolean existsInStorage(String filename) {
return filesOperations.existsInStorage(filename);
return filesOperations.existsInStorageGestion(filename);
}
}

2 changes: 1 addition & 1 deletion src/main/java/fr/insee/rmes/config/PropertiesLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class PropertiesLogger implements ApplicationListener<ApplicationEnvironm
public static final String PROPERTY_KEY_FOR_SOURCES_IGNORED = "fr.insee.properties.log.sources.ignored";
public static final String PROPERTY_KEY_FOR_SOURCES_SELECT = "fr.insee.properties.log.key.select";
private static final Set<String> baseMotsCaches = Set.of("password", "pwd", "jeton", "token", "secret", "credential", "pw");
private static final Set<String> prefixesAffichesParDefaut= Set.of("fr.insee","logging","keycloak","spring","application","server","springdoc","management");
private static final Set<String> prefixesAffichesParDefaut= Set.of("fr.insee","logging","keycloak","spring","application","server","springdoc","management","minio");
private static final Set<String> propertySourcesIgnoreesParDefaut = Set.of("systemProperties", "systemEnvironment");
private static final PropertySelectorEnum PROPERTY_SELECTOR_PAR_DEFAUT = PropertySelectorEnum.PREFIX;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ public void delete(Path absolutePath) {
}

@Override
public InputStream read(String path) {
public InputStream readInDirectoryGestion(String path) {
throw new RmesFileException(nomFichier, "Error reading file: " + nomFichier+
" as object `"+objectName+"` in bucket "+bucketName, new MinioException());
}

@Override
public void write(InputStream content, Path destPath) {
public void writeToDirectoryGestion(InputStream content, Path destPath) {

}

@Override
public void copy(String srcPath, String destPath) {
public void copyFromGestionToPublication(String srcPath, String destPath) {

}

Expand All @@ -116,7 +116,7 @@ public boolean dirExists(Path gestionStorageFolder) {
}

@Override
public boolean existsInStorage(String filename) {
public boolean existsInStorageGestion(String filename) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package fr.insee.rmes.testcontainers.minio;

import fr.insee.rmes.bauhaus_services.MinioFilesOperation;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import io.minio.errors.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MinIOContainer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import static org.assertj.core.api.Assertions.assertThat;

class TestMinioFilesOperation {

MinIOContainer container = new MinIOContainer("minio/minio:RELEASE.2024-11-07T00-52-20Z");

@BeforeAll
public static void configureSlf4j() {
System.setProperty("org.slf4j.simpleLogger.log."+MinioFilesOperation.class.getName(), "debug");
System.setProperty("slf4j.provider", "org.slf4j.simple.SimpleServiceProvider");
}

@Test
void testWritingThenCheckExistThenCopyThenRead_shouldBeOK() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
container.start();
var nomFichier = "test.txt";
MinioClient minioClient = MinioClient
.builder()
.endpoint(container.getS3URL())
.credentials(container.getUserName(), container.getPassword())
.build();
MinioFilesOperation minioFilesOperation = new MinioFilesOperation(minioClient,"metadata", "gestion", "publication");
createBucket(minioFilesOperation.bucketName(), minioClient);

Path absolutePathInGestion = Path.of("/mnt/applishare/rmes/data/storage/documents").resolve(nomFichier);
String contenuFichier = "Test";
minioFilesOperation.writeToDirectoryGestion(new ByteArrayInputStream(contenuFichier.getBytes()), absolutePathInGestion);
assertThat(minioFilesOperation.dirExists(Path.of(minioFilesOperation.directoryGestion()))).isTrue();
assertThat(minioFilesOperation.existsInStorageGestion(nomFichier)).isTrue();

minioFilesOperation.copyFromGestionToPublication(String.valueOf(absolutePathInGestion), "/mnt/applishare/rmes/data/storage/documents/tempPub1");
assertThat(minioFilesOperation.dirExists(Path.of(minioFilesOperation.directoryPublication()))).isTrue();
assertThat(fileExistsInPublication(minioClient, minioFilesOperation, nomFichier)
).isTrue();

assertThat(new String(minioFilesOperation.readInDirectoryGestion(nomFichier).readAllBytes())).isEqualTo(contenuFichier);
}

@AfterEach
void tearDown() {
container.stop();
}

private static boolean fileExistsInPublication(MinioClient minioClient, MinioFilesOperation minioFilesOperation, String nomFichier) throws ErrorResponseException, InsufficientDataException, InternalException, InvalidKeyException, InvalidResponseException, IOException, NoSuchAlgorithmException, ServerException, XmlParserException {
return minioClient.statObject(
StatObjectArgs.builder()
.bucket(minioFilesOperation.bucketName())
.object(minioFilesOperation.directoryPublication() + "/" + nomFichier).build()
).size() > 0;
}

private void createBucket(String bucketName, MinioClient minioClient) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}

}

0 comments on commit f508088

Please sign in to comment.