From 567d70f3b586e49d6b02cd18a2fca54717c0f27b Mon Sep 17 00:00:00 2001 From: Robert Sehr Date: Tue, 10 Dec 2024 10:03:38 +0100 Subject: [PATCH] send success message for each file --- .../DownloadAndVerifyAssetsStepPlugin.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java b/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java index a15e0ce..3ccb5e0 100644 --- a/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java +++ b/module-base/src/main/java/de/intranda/goobi/plugins/DownloadAndVerifyAssetsStepPlugin.java @@ -107,6 +107,9 @@ public class DownloadAndVerifyAssetsStepPlugin implements IStepPluginVersion2 { // @urlProperty -> @folder private Map urlFolderMap = new HashMap<>(); + // url -> FILEID + private Map urlIdMap = new HashMap<>(); + private String authenticationToken; private String downloadUrl; @@ -236,7 +239,7 @@ public PluginReturnValue run() { prepareUrlHashAndFolderMaps(); for (int i = 0; i < maxTryTimes; ++i) { - urlHashMap = processAllFiles(urlHashMap, urlFolderMap); + urlHashMap = processAllFiles(); } boolean successful = urlHashMap.isEmpty(); @@ -248,9 +251,6 @@ public PluginReturnValue run() { } } - // report success / errors via REST to the BACH system - successful = reportResults(successful) && successful; - log.info("DownloadAndVerifyAssets step plugin executed"); return successful ? PluginReturnValue.FINISH : PluginReturnValue.ERROR; } @@ -303,10 +303,12 @@ private boolean addUrlsToBothMaps(List urls, List hashes, String } for (int i = 0; i < urls.size(); ++i) { - String url = downloadUrl.replace("{FILEID}", urls.get(i)); + String fileId = urls.get(i); + String url = downloadUrl.replace("{FILEID}", fileId); String hash = hashes.get(i); urlHashMap.put(url, hash); urlFolderMap.put(url, folder); + urlIdMap.put(url, fileId); log.debug("URL-Hash pair added: " + url + " -> " + hash); log.debug("URL-folder pair added: " + url + " -> " + folder); } @@ -340,16 +342,16 @@ private Map> preparePropertiesMap() { * @param urlFolderMap * @return a map containing infos of unsuccessful files */ - private Map processAllFiles(Map urlHashMap, Map urlFolderMap) { + private Map processAllFiles() { Map unsuccessfulMap = new HashMap<>(); // download and verify files for (Map.Entry urlHashPair : urlHashMap.entrySet()) { String url = urlHashPair.getKey(); String hash = urlHashPair.getValue(); String targetFolder = urlFolderMap.get(url); - + String fileId = urlIdMap.get(url); try { - processFile(url, hash, targetFolder); + processFile(url, hash, targetFolder, fileId); } catch (Exception e) { unsuccessfulMap.put(url, hash); @@ -367,10 +369,10 @@ private Map processAllFiles(Map urlHashMap, Map< * @param targetFolder folder to save the downloaded file * @throws IOException */ - private void processFile(String fileUrl, String hash, String targetFolder) throws IOException { + private void processFile(String fileUrl, String hash, String targetFolder, String fileId) throws IOException { // prepare URL log.debug("downloading file from url: " + fileUrl); - + boolean successful = false; String fileName = Paths.get(fileUrl).getFileName().toString(); CloseableHttpClient httpclient = null; @@ -418,7 +420,7 @@ private void processFile(String fileUrl, String hash, String targetFolder) throw try (InputStream inputStream = StorageProvider.getInstance().newInputStream(destination)) { actualHash = calculateHash(inputStream); } - + successful = true; } catch (Exception e) { log.error("Unable to connect to url " + fileUrl, e); } @@ -427,9 +429,16 @@ private void processFile(String fileUrl, String hash, String targetFolder) throw String message = "checksums do not match, the file might be corrupted: " + destination; // delete the downloaded file Files.delete(destination); + successful = false; throw new IOException(message); } + //if file exist and is valid: send success message + if (StorageProvider.getInstance().isFileExists(destination)) { + reportResults(successful, fileId); + + } + } /** @@ -473,7 +482,7 @@ private void logError(String message) { * @param success final status * @return true if results are successfully reported, false otherwise */ - private boolean reportResults(boolean success) { + private boolean reportResults(boolean success, String fileId) { boolean reportSuccess = true; List responses = success ? successResponses : errorResponses; @@ -490,7 +499,7 @@ private boolean reportResults(boolean success) { String jsonBase = response.getJson(); String json = generateJsonMessage(jsonBase); log.debug("json = " + json); - reportSuccess = sendResponseViaRest(method, url, json) && reportSuccess; + reportSuccess = sendResponseViaRest(method, url.replace("{FILEID}", fileId), json) && reportSuccess; } }