Skip to content

Commit

Permalink
Clean old folders in <home.user>/.weasis #283
Browse files Browse the repository at this point in the history
  • Loading branch information
nroduit committed Jun 8, 2022
1 parent 57aecaf commit 2e0ee62
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ description: |
used in healthcare by hospitals, health networks, multicenter research
trials, and patients.
Note: With Snap, the .weasis directory is now located in /home/<username>/snap/weasis/current/.
Key features:
- Flexible integration to PACS, VNA, RIS, HIS or PHR
- Responsive user interface and work well on high DPI screens
Expand Down
2 changes: 2 additions & 0 deletions weasis-distributions/etc/config/ext-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#weasis.show.disclaimer=false
##### Show a message when the release has changed
#weasis.show.release=false
##### Show a message when a new release is available
#weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
3 changes: 3 additions & 0 deletions weasis-distributions/etc/config/ext-dicomizer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ felix.auto.start.85= \
felix.auto.start.110= \
${dollar}{weasis.codebase.url}/bundle/weasis-dicom-send-${app.version}.jar${xz.ext}


########## Properties which are stored in local preferences, used only to overload application default values during the first launch on a user session ##########
##### FaltLaf Look and feel, see https://www.formdev.com/flatlaf/themes/ and https://github.com/JFormDesigner/FlatLaf/tree/main/flatlaf-intellij-themes#themes.
weasis.theme=com.formdev.flatlaf.FlatIntelliJLaf
Expand Down Expand Up @@ -111,6 +112,8 @@ org.weasis.dicom.explorer.pref.node.DicomNodePrefFactory=false
weasis.show.disclaimer=false
##### Show a message when the release has changed
#weasis.show.release=false
##### Show a message when a new release is available
#weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ felix.auto.start.110=
weasis.show.disclaimer=false
##### Show a message when the release has changed
#weasis.show.release=false
##### Show a message when a new release is available
#weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
1 change: 0 additions & 1 deletion weasis-launcher/conf/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ felix.auto.start.110= \
file:${maven.localRepository}/org/weasis/dicom/weasis-dicom-send/${app.version}/weasis-dicom-send-${app.version}.jar \
file:${maven.localRepository}/org/weasis/dicom/weasis-dicom-qr/${app.version}/weasis-dicom-qr-${app.version}.jar


#felix.auto.start.115= \
# file:${maven.localRepository}/org/weasis/dicom/weasis-dicom-rt/${app.version}/weasis-dicom-rt-${app.version}.jar \

Expand Down
2 changes: 1 addition & 1 deletion weasis-launcher/conf/ext-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ weasis.show.disclaimer=false
##### Show a message when the release has changed
weasis.show.release=false
##### Show a message when a new release is available
weasis.update.release=true
weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
2 changes: 2 additions & 0 deletions weasis-launcher/conf/ext-dicomizer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ org.weasis.dicom.explorer.pref.node.DicomNodePrefFactory=false
weasis.show.disclaimer=false
##### Show a message when the release has changed
weasis.show.release=false
##### Show a message when a new release is available
weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
2 changes: 2 additions & 0 deletions weasis-launcher/conf/ext-non-dicom-explorer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ org.apache.sling.commons.log.stack.limit=-1
weasis.show.disclaimer=false
##### Show a message when the release has changed
weasis.show.release=false
##### Show a message when a new release is available
weasis.update.release=false
##### Allows exporting DICOM files. Default value is true.
#weasis.export.dicom=false
##### Allows DICOM send. Is always false when weasis.export.dicom=false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
package org.weasis.launcher;

import static java.time.temporal.ChronoUnit.DAYS;

import com.formdev.flatlaf.FlatSystemProperties;
import com.formdev.flatlaf.util.SystemInfo;
import java.awt.Desktop;
Expand All @@ -29,15 +31,23 @@
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.management.ObjectName;
import javax.swing.JComponent;
import javax.swing.JDialog;
Expand Down Expand Up @@ -1174,13 +1184,71 @@ private void shutdownHook() {
} finally {
cleanImageCache();
stopSingletonServer();
cleanOldFolders();

// If System.exit() hangs call Runtime.getRuntime().halt(1) to kill the application
Timer timer = new Timer();
timer.schedule(new HaltTask(), 15000);
}
}

private void cleanOldFolders() {
String dir = System.getProperty(P_WEASIS_PATH);
if (Utils.hasText(dir)) {
Path path = Paths.get(dir);
if( Files.isDirectory(path)){
try {
List<Path> folders = listOldFolders(path);
folders.forEach( p -> {
System.err.println("Delete old folder: " + p);
FileUtil.delete(p.toFile());
Optional<String> id = getID(p.getFileName().toString());
if(id.isPresent()) {
Path file = Paths.get(dir,id.get() + ".properties");
if(Files.isReadable(file)) {
FileUtil.delete(file.toFile());
}
Path data = Paths.get(dir, "data", id.get());
if(Files.isReadable(data)) {
FileUtil.delete(data.toFile());
}
}
});
} catch (IOException e) {
System.err.println("Cannot clean old folders - " + e);
}
}
}
}

private List<Path> listOldFolders(Path dir) throws IOException {
long days = Math.max(Long.parseLong(System.getProperty("weasis.clean.old.version.days", "100")), 30);
try (Stream<Path> stream = Files.list(dir)) {
return stream
.filter(path -> Files.isDirectory(path) && path.getFileName().toString().startsWith("cache-") && isOlderThan(path, days))
.toList();
}
}

private boolean isOlderThan(Path path, long days) {
try {
FileTime fileTime = Files.getLastModifiedTime(path);
LocalDateTime now = LocalDateTime.now();
LocalDateTime convertedFileTime = LocalDateTime.ofInstant(fileTime.toInstant(), ZoneId.systemDefault());
long daysBetween = DAYS.between(convertedFileTime, now);
return daysBetween > days;
} catch (Exception e) {
System.err.println("Cannot get the last modified time - " + e);
}
return false;
}

public Optional<String> getID(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("-"))
.map(f -> f.substring(filename.lastIndexOf("-") + 1));
}

protected void stopSingletonServer() {
// Do nothing in this class
}
Expand Down

0 comments on commit 2e0ee62

Please sign in to comment.