Skip to content

Commit 47acd93

Browse files
authored
Enhance updater functionality: backup current jar before downloading updates
1 parent 29f51a9 commit 47acd93

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/main/java/me/crazyg/everything/utils/Updater.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonParser;
66
import java.io.BufferedReader;
77
import java.io.File;
8+
import java.io.FileInputStream;
89
import java.io.FileOutputStream;
910
import java.io.IOException;
1011
import java.io.InputStream;
@@ -96,29 +97,53 @@ private void downloadUpdate() {
9697

9798
CompletableFuture.runAsync(() -> {
9899
try {
100+
// Prepare update folder
99101
File updateFolder = new File(plugin.getDataFolder().getParentFile(), UPDATE_FOLDER);
100102
if (!updateFolder.exists()) {
101103
updateFolder.mkdir();
102104
}
103105

104-
String fileName = "everything-" + latestVersion + ".jar";
105-
File outputFile = new File(updateFolder, fileName);
106+
// Get the current plugin jar file
107+
File currentJar = null;
108+
try {
109+
currentJar = new File(plugin.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
110+
} catch (Exception e) {
111+
plugin.getLogger().severe(Component.text("Could not determine the current plugin JAR file: " + e.getMessage())
112+
.color(NamedTextColor.RED)
113+
.toString());
114+
return;
115+
}
116+
if (currentJar == null || !currentJar.exists()) {
117+
plugin.getLogger().severe(Component.text("Could not determine the current plugin JAR file!")
118+
.color(NamedTextColor.RED)
119+
.toString());
120+
return;
121+
}
122+
123+
// Backup the current jar to the update folder
124+
File backupFile = new File(updateFolder, currentJar.getName() + ".bak-" + currentVersion);
125+
try (FileInputStream fis = new FileInputStream(currentJar); FileOutputStream fos = new FileOutputStream(backupFile)) {
126+
byte[] buffer = new byte[1024];
127+
int len;
128+
while ((len = fis.read(buffer)) > 0) {
129+
fos.write(buffer, 0, len);
130+
}
131+
}
106132

133+
// Download the new jar and overwrite the current one
107134
URL url = URI.create(downloadUrl).toURL();
108135
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
109136
connection.setRequestProperty("Accept", "application/octet-stream");
110137

111-
try (InputStream in = connection.getInputStream();
112-
FileOutputStream out = new FileOutputStream(outputFile)) {
113-
byte[] buffer = new byte[1024];
114-
int bytesRead;
115-
long totalBytesRead = 0;
116-
long fileSize = connection.getContentLengthLong();
138+
long fileSize = connection.getContentLengthLong();
139+
long totalBytesRead = 0;
140+
int bytesRead;
141+
byte[] buffer = new byte[1024];
117142

143+
try (InputStream in = connection.getInputStream(); FileOutputStream out = new FileOutputStream(currentJar)) {
118144
while ((bytesRead = in.read(buffer)) != -1) {
119145
out.write(buffer, 0, bytesRead);
120146
totalBytesRead += bytesRead;
121-
122147
if (fileSize > 0) {
123148
int progress = (int) ((totalBytesRead * 100) / fileSize);
124149
if (progress % 5 == 0) {
@@ -131,16 +156,18 @@ private void downloadUpdate() {
131156
}
132157

133158
plugin.getLogger().info(Component.text()
134-
.append(Component.text("Update downloaded successfully to: ").color(NamedTextColor.GREEN))
135-
.append(Component.text(outputFile.getAbsolutePath()).color(NamedTextColor.WHITE))
159+
.append(Component.text("Update downloaded and replaced plugin jar: ").color(NamedTextColor.GREEN))
160+
.append(Component.text(currentJar.getAbsolutePath()).color(NamedTextColor.WHITE))
161+
.build().toString());
162+
plugin.getLogger().info(Component.text()
163+
.append(Component.text("Backup of old jar saved to: ").color(NamedTextColor.YELLOW))
164+
.append(Component.text(backupFile.getAbsolutePath()).color(NamedTextColor.WHITE))
136165
.build().toString());
137-
138166
plugin.getLogger().info(Component.text("Please restart your server to apply the update.")
139167
.color(NamedTextColor.YELLOW)
140168
.toString());
141-
142-
} catch (IOException e) {
143-
plugin.getLogger().severe(Component.text("Failed to download update: " + e.getMessage())
169+
} catch (Exception e) {
170+
plugin.getLogger().severe(Component.text("Failed to download or replace update: " + e.getMessage())
144171
.color(NamedTextColor.RED)
145172
.toString());
146173
}

0 commit comments

Comments
 (0)