Bon checkpoint + Refactoring
This commit is contained in:
@@ -1,23 +1,32 @@
|
||||
package com.lions.dev.service;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import com.lions.dev.repository.EventsRepository;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
* Service pour la gestion des fichiers uploadés.
|
||||
* Ce service permet de sauvegarder et gérer les fichiers uploadés sur le serveur.
|
||||
*
|
||||
* <p>Toutes les actions sont loguées pour assurer une traçabilité complète.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class FileService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(FileService.class);
|
||||
|
||||
@Inject
|
||||
EventsRepository eventsRepository;
|
||||
|
||||
/**
|
||||
* Sauvegarde le fichier uploadé sur le serveur.
|
||||
* Sauvegarde le fichier uploadé sur le serveur, avec création du répertoire de destination
|
||||
* si nécessaire et gestion des erreurs de manière contrôlée.
|
||||
*
|
||||
* @param uploadedFilePath Le chemin temporaire du fichier uploadé.
|
||||
* @param destinationDir Le répertoire de destination où sauvegarder le fichier.
|
||||
@@ -26,10 +35,34 @@ public class FileService {
|
||||
* @throws IOException Si une erreur survient lors de la sauvegarde.
|
||||
*/
|
||||
public Path saveFile(Path uploadedFilePath, String destinationDir, String fileName) throws IOException {
|
||||
Path destination = Paths.get(destinationDir, fileName);
|
||||
Files.createDirectories(Paths.get(destinationDir)); // Crée le répertoire s'il n'existe pas
|
||||
Files.copy(uploadedFilePath, destination); // Copie le fichier vers sa destination
|
||||
LOG.info("Fichier sauvegardé avec succès : " + destination);
|
||||
return destination;
|
||||
Path destinationPath = Paths.get(destinationDir, fileName);
|
||||
LOG.info("[LOG] Tentative de sauvegarde du fichier vers : " + destinationPath);
|
||||
|
||||
if (Files.notExists(uploadedFilePath)) {
|
||||
LOG.error("[ERROR] Le fichier uploadé n'existe pas : " + uploadedFilePath);
|
||||
throw new IOException("Le fichier uploadé n'existe pas : " + uploadedFilePath);
|
||||
}
|
||||
|
||||
try {
|
||||
Files.createDirectories(Paths.get(destinationDir));
|
||||
LOG.info("[LOG] Répertoire de destination créé ou déjà existant : " + destinationDir);
|
||||
} catch (IOException e) {
|
||||
LOG.error("[ERROR] Impossible de créer le répertoire de destination : " + destinationDir, e);
|
||||
throw new IOException("Impossible de créer le répertoire de destination : " + destinationDir, e);
|
||||
}
|
||||
|
||||
try {
|
||||
Files.copy(uploadedFilePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
LOG.info("[LOG] Fichier sauvegardé avec succès à l'emplacement : " + destinationPath);
|
||||
} catch (FileAlreadyExistsException e) {
|
||||
LOG.warn("[WARNING] Le fichier existe déjà, il sera remplacé : " + destinationPath);
|
||||
Files.copy(uploadedFilePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
LOG.error("[ERROR] Erreur lors de la copie du fichier vers : " + destinationPath, e);
|
||||
throw new IOException("Erreur lors de la sauvegarde du fichier : " + destinationPath, e);
|
||||
}
|
||||
|
||||
return destinationPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user