From b1162951da92a9d9ed999e2aed26f39fcbc0d4bc Mon Sep 17 00:00:00 2001 From: Saptarshi Sarkar Date: Mon, 19 Feb 2024 15:38:52 +0530 Subject: [PATCH] feat: Added a cleanup code to remove old Drifty update files during its initialization --- .../main/java/cli/updater/ExecuteUpdate.java | 13 +++++++----- Core/src/main/java/init/Environment.java | 20 +++++++++++++++++++ GUI/src/main/java/main/Drifty_GUI.java | 20 ------------------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/CLI/src/main/java/cli/updater/ExecuteUpdate.java b/CLI/src/main/java/cli/updater/ExecuteUpdate.java index 6d9f7e7aa..4b334d122 100644 --- a/CLI/src/main/java/cli/updater/ExecuteUpdate.java +++ b/CLI/src/main/java/cli/updater/ExecuteUpdate.java @@ -2,6 +2,7 @@ import cli.init.Environment; import cli.utils.MessageBroker; +import properties.OS; import java.io.File; import java.io.IOException; @@ -51,11 +52,13 @@ public boolean executeUpdate() { M.msgUpdateError("Failed to replace the current executable with the latest version!"); return false; } - try { - Files.deleteIfExists(Paths.get(currentExecutablePath + ".old")); - } catch (IOException e) { - M.msgUpdateError("Failed to delete the old version of Drifty!"); - return false; + if (!OS.isWindows()) { + try { + Files.deleteIfExists(Paths.get(currentExecutablePath + ".old")); + } catch (IOException e) { + M.msgUpdateError("Failed to delete the old version of Drifty!"); + return false; + } } return true; } diff --git a/Core/src/main/java/init/Environment.java b/Core/src/main/java/init/Environment.java index 4d8d97841..63a3bbeed 100644 --- a/Core/src/main/java/init/Environment.java +++ b/Core/src/main/java/init/Environment.java @@ -25,6 +25,7 @@ public class Environment { Finally, it updates yt-dlp if it has not been updated in the last 24 hours. */ public static void initializeEnvironment() { + cleanUpOldUpdateFiles(); msgBroker.msgLogInfo("OS : " + OS.getOSName()); String ytDlpExecName = OS.isWindows() ? "yt-dlp.exe" : OS.isMac() ? "yt-dlp_macos" : "yt-dlp"; String spotDLExecName = OS.isWindows() ? "spotdl_win.exe" : OS.isMac() ? "spotdl_macos" : "spotdl_linux"; @@ -59,6 +60,25 @@ public static void initializeEnvironment() { } } + private static void cleanUpOldUpdateFiles() { + new Thread(() -> { + if (OS.isWindows()) { + try { + File oldExecutableFile = new File(Environment.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath() + ".old"); + if (oldExecutableFile.exists()) { + if (oldExecutableFile.delete()) { + msgBroker.msgLogInfo("Old version of Drifty has been deleted successfully!"); + } else { + msgBroker.msgUpdateError("Failed to delete the old version of Drifty!"); + } + } + } catch (Exception e) { + msgBroker.msgUpdateError("Failed to get the current executable path!"); + } + } + }).start(); + } + public static void setMessageBroker(MessageBroker messageBroker) { Environment.msgBroker = messageBroker; } diff --git a/GUI/src/main/java/main/Drifty_GUI.java b/GUI/src/main/java/main/Drifty_GUI.java index d2c429470..d80732d67 100644 --- a/GUI/src/main/java/main/Drifty_GUI.java +++ b/GUI/src/main/java/main/Drifty_GUI.java @@ -21,13 +21,9 @@ import javafx.scene.text.FontWeight; import javafx.stage.Stage; import properties.Mode; -import properties.OS; import ui.*; import utils.Utility; -import java.io.File; -import java.net.URISyntaxException; - import static gui.support.Constants.GUI_APPLICATION_TERMINATED; import static javafx.scene.layout.AnchorPane.*; import static support.Constants.DRIFTY_WEBSITE_URL; @@ -50,22 +46,6 @@ public void init() { msgBroker = new MessageBroker(); Environment.setMessageBroker(msgBroker); msgBroker.msgLogInfo("Drifty GUI (Graphical User Interface) Application Started !"); - new Thread(() -> { - if (OS.isWindows()) { - try { - File oldExecutable = new File(Drifty_GUI.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath() + ".old"); - if (oldExecutable.exists()) { - if (oldExecutable.delete()) { - msgBroker.msgLogInfo("Old version of Drifty has been deleted successfully!"); - } else { - msgBroker.msgUpdateError("Failed to delete the old version of Drifty!"); - } - } - } catch (URISyntaxException e) { - msgBroker.msgUpdateError("Failed to get the current executable path!"); - } - } - }).start(); } @Override