Skip to content

Commit

Permalink
feat: Added a cleanup code to remove old Drifty update files during i…
Browse files Browse the repository at this point in the history
…ts initialization
  • Loading branch information
SaptarshiSarkar12 committed Feb 19, 2024
1 parent 79decc0 commit b116295
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
13 changes: 8 additions & 5 deletions CLI/src/main/java/cli/updater/ExecuteUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cli.init.Environment;
import cli.utils.MessageBroker;
import properties.OS;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions Core/src/main/java/init/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 0 additions & 20 deletions GUI/src/main/java/main/Drifty_GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit b116295

Please sign in to comment.