From fea9599a9fde957041ccfec420a751ef99c4a2b2 Mon Sep 17 00:00:00 2001 From: Arved Solth Date: Thu, 12 Oct 2023 16:44:16 +0200 Subject: [PATCH] Enable reverting of partial media renaming --- .../production/services/file/FileService.java | 10 ++++++++-- .../production/thread/RenameMediaThread.java | 19 +++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java index 0ad94460589..25b0c0c99bb 100644 --- a/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java +++ b/Kitodo/src/main/java/org/kitodo/production/services/file/FileService.java @@ -1572,8 +1572,14 @@ public void revertRenaming(BidiMap filenameMappings, Workpiece workpie try { List tempUris = new LinkedList<>(); for (Entry mapping : filenameMappings.entrySet()) { - tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString() - + TEMP_EXTENSION)); + if (mapping.getKey().toString().endsWith(TEMP_EXTENSION)) { + // if current URI has '.tmp' extension, directly revert to original name (without '.tmp' extension) + tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString())); + } else { + // rename to new filename with '.tmp' extension otherwise + tempUris.add(fileManagementModule.rename(mapping.getKey(), mapping.getValue().toString() + + TEMP_EXTENSION)); + } } for (URI tempUri : tempUris) { fileManagementModule.rename(tempUri, StringUtils.removeEnd(tempUri.toString(), TEMP_EXTENSION)); diff --git a/Kitodo/src/main/java/org/kitodo/production/thread/RenameMediaThread.java b/Kitodo/src/main/java/org/kitodo/production/thread/RenameMediaThread.java index aaab86cfcaa..b8154f8d2a7 100644 --- a/Kitodo/src/main/java/org/kitodo/production/thread/RenameMediaThread.java +++ b/Kitodo/src/main/java/org/kitodo/production/thread/RenameMediaThread.java @@ -16,14 +16,13 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.List; +import java.util.Objects; import org.apache.commons.collections4.bidimap.DualHashBidiMap; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.kitodo.api.dataformat.Workpiece; import org.kitodo.data.database.beans.Process; -import org.kitodo.data.elasticsearch.exceptions.CustomResponseException; -import org.kitodo.data.exceptions.DataException; import org.kitodo.production.helper.tasks.EmptyTask; import org.kitodo.production.metadata.MetadataLock; import org.kitodo.production.services.ServiceManager; @@ -53,26 +52,22 @@ public void run() { int processId = process.getId(); URI metaXmlUri = ServiceManager.getProcessService().getMetadataFileUri(process); DualHashBidiMap renamingMap = new DualHashBidiMap<>(); - boolean success = false; + Workpiece workpiece = null; try { - Workpiece workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlUri); + workpiece = ServiceManager.getMetsService().loadWorkpiece(metaXmlUri); int numberOfRenamedFiles = ServiceManager.getFileService().renameMediaFiles(process, workpiece, renamingMap); try (OutputStream out = ServiceManager.getFileService().write(metaXmlUri)) { ServiceManager.getMetsService().save(workpiece, out); - ServiceManager.getProcessService().saveToIndex(process, false); - success = true; logger.info("Renamed " + numberOfRenamedFiles + " media files for process " + process.getId()); - } catch (CustomResponseException | DataException e) { - logger.error(e.getMessage()); } - if (!success) { - ServiceManager.getFileService().revertRenaming(renamingMap, workpiece); - } - MetadataLock.setFree(processId); } catch (IOException | URISyntaxException e) { logger.error(e.getMessage()); + if (Objects.nonNull(workpiece)) { + ServiceManager.getFileService().revertRenaming(renamingMap.inverseBidiMap(), workpiece); + } } + MetadataLock.setFree(processId); setProgress((100 / processes.size()) * (processes.indexOf(process) + 1)); } setProgress(100);