Skip to content

Commit

Permalink
implemented auto-download for new client versions. close #133.
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Jan 18, 2024
1 parent 654d9ee commit 00b379b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
54 changes: 32 additions & 22 deletions j-lawyer-client/src/com/jdimension/jlawyer/client/LoginDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,13 @@
package com.jdimension.jlawyer.client;

import com.formdev.flatlaf.FlatClientProperties;
import com.jdimension.jlawyer.client.editors.EditorsRegistry;
import com.jdimension.jlawyer.client.processing.ProgressIndicator;
import com.jdimension.jlawyer.client.security.ssh.SshTunnel;
import com.jdimension.jlawyer.client.settings.ClientSettings;
import com.jdimension.jlawyer.client.settings.UserSettings;
import com.jdimension.jlawyer.client.utils.DesktopUtils;
import com.jdimension.jlawyer.client.utils.FileUtils;
import com.jdimension.jlawyer.client.utils.FrameUtils;
import com.jdimension.jlawyer.client.utils.ThreadUtils;
import com.jdimension.jlawyer.client.utils.VersionUtils;
Expand Down Expand Up @@ -1790,38 +1793,49 @@ private void loginPerformed(boolean saveProfile) {
Thread downloadThread = new Thread(() -> {

try {
String downloadUrl=VersionUtils.getLatestClientDownloadForServer(serverVersion);
String downloadUrl = VersionUtils.getLatestClientDownloadForServer(serverVersion);
URL updateURL = new URL(downloadUrl);
URLConnection urlCon = updateURL.openConnection();
urlCon.setRequestProperty("User-Agent", "j-lawyer Client v" + VersionUtils.getFullClientVersion());
urlCon.setConnectTimeout(1500);
urlCon.setReadTimeout(3500);


String userHome = System.getProperty("user.home");
String downloadsFolder = userHome + System.getProperty("file.separator") + "Downloads";
File tmpF = new File(downloadsFolder + File.separator + downloadUrl.substring(downloadUrl.lastIndexOf("=")+1));

String downloadsFolder = userHome + System.getProperty("file.separator") + "Downloads" + System.getProperty("file.separator") + "j-lawyer-Update_" + latestClientVersion;
new File(downloadsFolder).mkdirs();
String fullTempFile=downloadsFolder + File.separator + downloadUrl.substring(downloadUrl.lastIndexOf("=") + 1);
File tmpF = new File(fullTempFile);

try (InputStream is = urlCon.getInputStream(); FileOutputStream fOut = new FileOutputStream(tmpF)) {

byte[] buffer = new byte[1024*1024];
byte[] buffer = new byte[1024 * 1024];
int len = 0;
int totalChunk = 0;
int total = 0;
while ((len = is.read(buffer)) > -1) {

fOut.write(buffer, 0, len);
ThreadUtils.updateLabel(lblAutoUpdate, len + " bytes");
lblAutoUpdate.setText(len + " bytes");
lblAutoUpdate.repaint();
lblAutoUpdate.revalidate();
final int byteLen=len;
SwingUtilities.invokeAndWait(() -> {
lblAutoUpdate.setText(byteLen + " bytes");
});
System.out.println(len + " bytes");
totalChunk = totalChunk + len;
total=total+len;

final int byteLen = total;
if (totalChunk > 1000000) {
SwingUtilities.invokeLater(() -> {
lblAutoUpdate.setText("Lade Installer für Version " + latestClientVersion + "... " + FileUtils.getFileSizeHumanReadable(byteLen));
});
totalChunk = 0;
}
}
}

FileUtils.setPosixFilePermissions(fullTempFile, true, true, true);

SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(this, "<html>Die Anwendung wird nun beendet. F&uuml;hren Sie anschlie&szlig;end diese Datei aus:<br/>" + tmpF.getAbsolutePath(), "bereit zur Installation", JOptionPane.INFORMATION_MESSAGE);
DesktopUtils.openFileManager(new File(downloadsFolder));
System.exit(0);
});

DesktopUtils.openFileManager(new File(downloadsFolder));
} catch (Exception ex) {
log.error(ex);
}
Expand All @@ -1832,12 +1846,8 @@ private void loginPerformed(boolean saveProfile) {
});
downloadThread.start();

try {
// Wait for the thread to finish using the join() method
downloadThread.join();
} catch (InterruptedException e) {
log.error(e);
}
// do not log in if user selected to download the update
return;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,17 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
Expand Down Expand Up @@ -1118,4 +1124,33 @@ public static String getNewFileName(String currentFileName, boolean datetimePref

return null;
}

public static void setPosixFilePermissions(String absolutePathToFile, boolean read, boolean write, boolean execute) {
Path path = FileSystems.getDefault().getPath(absolutePathToFile);

// Create a set of PosixFilePermission
Set<PosixFilePermission> perms = new HashSet<>();
if(read)
perms.add(PosixFilePermission.OWNER_READ);
if(write)
perms.add(PosixFilePermission.OWNER_WRITE);
if(execute)
perms.add(PosixFilePermission.OWNER_EXECUTE);

// You can also add permissions for group and others if needed
// perms.add(PosixFilePermission.GROUP_READ);
// perms.add(PosixFilePermission.GROUP_WRITE);
// perms.add(PosixFilePermission.GROUP_EXECUTE);
// perms.add(PosixFilePermission.OTHERS_READ);
// perms.add(PosixFilePermission.OTHERS_WRITE);
// perms.add(PosixFilePermission.OTHERS_EXECUTE);

try {
// Set the permissions on the file
Files.setPosixFilePermissions(path, perms);
log.info("Executable flag added to the file: " + path);
} catch (IOException e) {
log.error("Error adding executable flag: " + e.getMessage());
}
}
}

0 comments on commit 00b379b

Please sign in to comment.