Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/quick fix minio bugs #839

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 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.6</version>
<version>4.1.7-beta2</version>
<name>Bauhaus-Back-Office</name>
<description>Back-office services for Bauhaus</description>
<url>https://github.com/InseeFr/Bauhaus-Back-Office</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class FileSystemOperation implements FilesOperations {
protected Config config;

@Override
public void delete(String path) {
public void delete(Path absolutePath) {
try {
Files.delete(Paths.get(path));
Files.delete(absolutePath);
} catch (IOException e) {
throw new RmesFileException("Failed to delete file: " + path, e);
throw new RmesFileException(absolutePath.getFileName().toString(), "Failed to delete file: " + absolutePath, e);
}
}

Expand All @@ -31,16 +31,21 @@ public InputStream read(String fileName) {
try {
return Files.newInputStream(Paths.get(config.getDocumentsStorageGestion()).resolve(fileName));
} catch (IOException e) {
throw new RmesFileException("Failed to read file: " + fileName, e);
throw new RmesFileException(fileName, "Failed to read file: " + fileName, e);
}
}

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

@Override
public void write(InputStream content, Path destPath) {
try {
Files.copy(content, destPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RmesFileException("Failed to write file: " + destPath, e);
throw new RmesFileException(destPath.toString(),"Failed to write file: " + destPath, e);
}
}

Expand All @@ -51,12 +56,13 @@ public void copy(String srcPath, String destPath) {
try {
Files.copy(file, targetPath.resolve(file.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RmesFileException("Failed to copy file : " + srcPath + " to " + destPath, e);
throw new RmesFileException(srcPath, "Failed to copy file : " + srcPath + " to " + destPath, e);
}
}

@Override
public boolean dirExists(Path gestionStorageFolder) {
return Files.isDirectory(requireNonNull(gestionStorageFolder));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.nio.file.Path;

public interface FilesOperations {
void delete(String path);
InputStream read(String path);
void delete(Path absolutePath);
InputStream read(String filename);
void write(InputStream content, Path destPath);
void copy(String srcPath, String destPath);

boolean dirExists(Path gestionStorageFolder);

boolean existsInStorage(String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
import fr.insee.rmes.exceptions.RmesFileException;
import io.minio.*;
import io.minio.errors.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public record MinioFilesOperation(MinioClient minioClient, String bucketName, String directoryGestion, String directoryPublication) implements FilesOperations {
import static java.util.Objects.requireNonNull;

private static final Logger logger = LoggerFactory.getLogger(MinioFilesOperation.class);
public record MinioFilesOperation(MinioClient minioClient, String bucketName, String directoryGestion, String directoryPublication) implements FilesOperations {

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

try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(directoryGestion +"/"+ objectName)
.object(objectName)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
throw new RmesFileException("Error reading file: " + objectName, e);
throw new RmesFileException(fileName, "Error reading file: " + fileName+" as object `"+objectName+"` in bucket "+bucketName, e);
}
}
private static String extractFileName(String filePath) {
Expand All @@ -36,38 +35,55 @@ private static String extractFileName(String filePath) {
return Path.of(filePath).getFileName().toString();
}

@Override
public boolean existsInStorage(String filename) {
var objectName = extractFileName(requireNonNull(filename));
try {
return minioClient.statObject(StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build()).size() > 0;
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
return false;
}
}

@Override
public void write(InputStream content, Path objectName) {
public void write(InputStream content, Path filePath) {
String filename = filePath.getFileName().toString();
String objectName = directoryGestion + "/" + filename;
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(directoryGestion +"/"+ objectName.getFileName().toString())
.object(objectName)
.stream(content, content.available(), -1)
.build());
} catch (IOException | ErrorResponseException | InsufficientDataException | InternalException |
InvalidKeyException | InvalidResponseException | NoSuchAlgorithmException | ServerException |
XmlParserException e) {
throw new RmesFileException("Error writing file: " + objectName, e);
throw new RmesFileException(filePath.toString(), "Error writing file: " + filename+ "as object `"+objectName+"` in bucket "+bucketName, e);
}
}

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

String srcObject = directoryGestion + "/" + extractFileName(srcObjectName);
String destObject = directoryPublication + "/" + extractFileName(srcObjectName);

try {
CopySource source = CopySource.builder()
.bucket(bucketName)
.object(directoryGestion +"/"+ extractFileName(srcObjectName))
.object(srcObject)
.build();

minioClient.copyObject(CopyObjectArgs.builder()
.bucket(bucketName)
.object(directoryPublication +"/"+ extractFileName(srcObjectName))
.object(destObject)
.source(source)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
throw new RmesFileException("Error copying file from " + srcObjectName + " to " + destObjectName, e);
throw new RmesFileException(srcObjectName,"Error copying file from `" + srcObject + "` to `" + destObject+"` in bucket "+bucketName, e);
}
}

Expand All @@ -78,14 +94,15 @@ public boolean dirExists(Path gestionStorageFolder) {
}

@Override
public void delete(String objectName) {
public void delete(Path absolutePath) {
String objectName = absolutePath.getFileName().toString();
try {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
throw new RmesFileException("Error deleting file: " + objectName, e);
throw new RmesFileException(objectName,"Error deleting file: " + objectName+" in bucket "+bucketName, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ private Set<String> exportRubricsDocuments(JSONObject sims, Path directory) thro

for (int i = 0; i < documents.length(); i++) {
JSONObject document = documents.getJSONObject(i);
String url = document.getString("url").replace("file://", "");
String url = DocumentsUtils.getDocumentUrlFromDocument(document);
if(!history.contains(url)){
history.add(url);
logger.debug("Extracting document {}", url);


Path documentPath = Path.of(url);
String documentFilename = DocumentsUtils.getDocumentNameFromUrl(url);

if(!Files.exists(documentPath)){
if(!documentsUtils.existsInStorage(documentFilename)){
missingDocuments.add(document.getString("id"));
} else {
String documentFileName = FilesUtils.generateFinalFileNameWithExtension(UriUtils.getLastPartFromUri(url), maxLength);
try (InputStream inputStream = Files.newInputStream(documentPath)){
try (InputStream inputStream = documentsUtils.retrieveDocumentFromStorage(documentFilename)){
Path documentDirectory = Path.of(directory.toString(), "documents");
if (!Files.exists(documentDirectory)) {
logger.debug("Creating the documents folder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void publishAllDocumentsInSims(String idSims) throws RmesException {
for (Map.Entry<Integer, String> doc : mapIdUrls.entrySet()) {
String docId = doc.getKey().toString();
String originalPath = doc.getValue();
String filename = docUtils.getDocumentNameFromUrl(originalPath);
String filename = DocumentsUtils.getDocumentNameFromUrl(originalPath);
// Publish the physical files
copyFileInPublicationFolders(originalPath);
// Change url in document (getModelToPublish) and publish the RDF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -282,7 +281,7 @@ private void checkUrlDoesNotExist(String id, String url, int errorCode, String e
JSONObject existingUriJson = repoGestion.getResponseAsObject(DocumentsQueries.getDocumentUriQuery(url));
if (existingUriJson.length() > 0) {
String uri = existingUriJson.getString(Constants.DOCUMENT);
String existingId = getIdFromUri(uri);
String existingId = getDocumentNameFromUrl(uri);
if (!existingId.equals(id)) {
throw new RmesNotAcceptableException(errorCode, errorMessage, uri);
}
Expand Down Expand Up @@ -556,21 +555,13 @@ private void changeDocumentsURL(String iri, String docUrl, String newUrl) throws

private void deleteFile(String docUrl) {
Path path = Paths.get(docUrl);
try {
Files.delete(path);
} catch (IOException e) {
logger.error(e.getMessage());
}
filesOperations.delete(path);
}

public String getDocumentNameFromUrl(String docUrl) {
public static String getDocumentNameFromUrl(String docUrl) {
return UriUtils.getLastPartFromUri(docUrl);
}

private String getIdFromUri(String uri) {
return UriUtils.getLastPartFromUri(uri);
}

private String createFileUrl(String name) throws RmesException {
Path gestionStorageFolder=Path.of(config.getDocumentsStorageGestion());
if (!filesOperations.dirExists(gestionStorageFolder)){
Expand Down Expand Up @@ -603,7 +594,7 @@ private IRI getDocumentUri(IRI url) throws RmesException {
return RdfUtils.toURI(uri.getString(Constants.DOCUMENT));
}

public String getDocumentUrlFromDocument(JSONObject jsonDoc) {
public static String getDocumentUrlFromDocument(JSONObject jsonDoc) {
return jsonDoc.getString(Constants.URL).replace(SCHEME_FILE, "");
}

Expand Down Expand Up @@ -655,7 +646,7 @@ public Document buildDocumentFromJson(JSONObject jsonDoc) {
return doc;
}

private String getDocumentFilename(String id) throws RmesException {
protected String getDocumentFilename(String id) throws RmesException {
JSONObject jsonDoc = getDocument(id, false);
String url = getDocumentUrlFromDocument(jsonDoc);
return getDocumentNameFromUrl(url);
Expand Down Expand Up @@ -696,5 +687,13 @@ private String getFileName(String path) {
// Extraire juste le nom de fichier du chemin
return Paths.get(path).getFileName().toString();
}

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public final ResponseEntity<String> handleSubclassesOfRmesException(RmesExceptio
return ResponseEntity.status(exception.getStatus()).body(exception.getDetails());
}

@ExceptionHandler(RmesFileException.class)
public final ResponseEntity<String> handleRmesFileException(RmesFileException exception){
logger.error(exception.getMessage(), exception);
return ResponseEntity.internalServerError().body(exception.toString());
}

@ExceptionHandler(RmesException.class)
public final ResponseEntity<String> handleRmesException(RmesException exception){
logger.error(exception.getMessageAndDetails(), exception);
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/fr/insee/rmes/exceptions/RmesFileException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package fr.insee.rmes.exceptions;

public class RmesFileException extends RuntimeException {
public RmesFileException(String message, Throwable cause) {

private final String fileName;

public RmesFileException(String filename, String message, Throwable cause) {
super(message, cause);
this.fileName = filename;
}

public String getFileName() {
return fileName;
}

@Override
public String toString() {
return "RmesFileException{" +
"fileName='" + fileName + '\'' +
'}';
}
}
Loading
Loading