From 3975f8b0440db12f89cfe607d909bb9bd7a1ad5c Mon Sep 17 00:00:00 2001 From: Ahmed Harhash Date: Tue, 16 Apr 2024 08:53:31 +0200 Subject: [PATCH] easy-installer: Implement Direct Post-Download Integrity Check in DownloadTask --- .../easy/installer/tasks/DownloadTask.java | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/java/ecorp/easy/installer/tasks/DownloadTask.java b/src/main/java/ecorp/easy/installer/tasks/DownloadTask.java index 2d23b134..5e26d1eb 100644 --- a/src/main/java/ecorp/easy/installer/tasks/DownloadTask.java +++ b/src/main/java/ecorp/easy/installer/tasks/DownloadTask.java @@ -32,6 +32,9 @@ import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DecimalFormat; @@ -103,50 +106,51 @@ public class DownloadTask extends Task{ final String localFilePath = AppConstants.getSourcesFolderPath() + fileName; final String checksumFilePath = localFilePath + checkSumExtension; - - if(isCancelled()) return false; - + + if (isCancelled()) return false; + this.updateTitle("Downloading " + latestBuildFilename + checkSumExtension); - - - File checksumLmdFile = new File(AppConstants.getSourcesFolderPath()+"lmd."+fileName+checkSumExtension); + + File checksumLmdFile = new File(AppConstants.getSourcesFolderPath() + "lmd." + fileName + checkSumExtension); checksumLmdFile.createNewFile(); - - // Download checksum. If file not downloaded return false and stop downloadin because integrity isn't guaranteed - if( !downloadFile(targetUrl+checkSumExtension, checksumFilePath,checksumLmdFile) ){ + + if (!downloadFile(targetUrl + checkSumExtension, checksumFilePath, checksumLmdFile)) { updateMessage(i18n.getString("download_lbl_cantcheckIntegrity")); return false; } - //If checksum valid it means that file is already there and up to date - updateMessage(i18n.getString("download_lbl_checkingIntegrity")); - if ( validChecksum(checksumFilePath) ){ + updateMessage(i18n.getString("download_lbl_checkingIntegrity")); + if (validChecksum(checksumFilePath)) { updateMessage(i18n.getString("download_lbl_fileAlreadyUptoDate")); return true; } - if(isCancelled()) return false; + if (isCancelled()) return false; - this.updateTitle("Downloading "+ latestBuildFilename); + this.updateTitle("Downloading " + latestBuildFilename); - final String tmpFilePath = AppConstants.getSourcesFolderPath()+"tmp."+fileName; - - File lmdFile = new File(AppConstants.getSourcesFolderPath()+"lmd."+fileName); //used to Store last modified Date of remote content + final String tmpFilePath = localFilePath; + + File lmdFile = new File(AppConstants.getSourcesFolderPath() + "lmd." + fileName); lmdFile.createNewFile(); - - if ( downloadFile(targetUrl, tmpFilePath, lmdFile) )//Download file - { - logger.debug("Downloaded succeed. Rename temp file to right fileName"); - File tmpFile = new File(tmpFilePath); - tmpFile.renameTo(new File(localFilePath)); - return validChecksum(checksumFilePath); - - }else{ + + if (downloadFile(targetUrl, tmpFilePath, lmdFile)) { + logger.debug("Download succeeded. Performing checksum validation."); + + if (validChecksum(checksumFilePath)) { + return true; // Download successful and checksum validated + } else { + updateMessage(i18n.getString("download_lbl_invalidChecksum")); + // Delete the downloaded file as checksum validation failed + Files.deleteIfExists(Paths.get(localFilePath)); + return false; + } + } else { updateMessage(i18n.getString("download_lbl_downloadError")); return false; } } - + // method link to file downloading /** -- GitLab