Skip to content

Commit

Permalink
send success message for each file
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Dec 10, 2024
1 parent 573285d commit 567d70f
Showing 1 changed file with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public class DownloadAndVerifyAssetsStepPlugin implements IStepPluginVersion2 {
// @urlProperty -> @folder
private Map<String, String> urlFolderMap = new HashMap<>();

// url -> FILEID
private Map<String, String> urlIdMap = new HashMap<>();

private String authenticationToken;

private String downloadUrl;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down Expand Up @@ -303,10 +303,12 @@ private boolean addUrlsToBothMaps(List<String> urls, List<String> 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);
}
Expand Down Expand Up @@ -340,16 +342,16 @@ private Map<String, List<String>> preparePropertiesMap() {
* @param urlFolderMap
* @return a map containing infos of unsuccessful files
*/
private Map<String, String> processAllFiles(Map<String, String> urlHashMap, Map<String, String> urlFolderMap) {
private Map<String, String> processAllFiles() {
Map<String, String> unsuccessfulMap = new HashMap<>();
// download and verify files
for (Map.Entry<String, String> 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);
Expand All @@ -367,10 +369,10 @@ private Map<String, String> processAllFiles(Map<String, String> 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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);

}

}

/**
Expand Down Expand Up @@ -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<SingleResponse> responses = success ? successResponses : errorResponses;
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 567d70f

Please sign in to comment.