Skip to content

Commit

Permalink
feat: delete temp files on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Feb 8, 2025
1 parent 150bfd2 commit 1b3d05d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
19 changes: 17 additions & 2 deletions desktop/src/cn/harryh/arkpets/controllers/RootModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.harryh.arkpets.EmbeddedLauncher;
import cn.harryh.arkpets.concurrent.ProcessPool;
import cn.harryh.arkpets.guitasks.CheckAppUpdateTask;
import cn.harryh.arkpets.guitasks.DeleteTempFilesTask;
import cn.harryh.arkpets.guitasks.GuiTask;
import cn.harryh.arkpets.utils.ArgPending;
import cn.harryh.arkpets.utils.GuiPrefabs;
Expand Down Expand Up @@ -189,12 +190,26 @@ public void syncRemoteMetaInfo() {
new CheckAppUpdateTask(app.body, GuiTask.GuiTaskStyle.HIDDEN, "auto").start();
}

/** Plays the exit animation and then invokes {@link Platform#exit()}.
/** Plays the exit animation, deletes temp files, then invokes {@link Platform#exit()}.
*/
public void exit() {
popSplashScreen(e -> {
Logger.info("Launcher", "User close request");
GuiPrefabs.fadeOutWindow(app.stage, durationNormal, ev -> Platform.exit());
GuiPrefabs.fadeOutWindow(
app.stage,
durationNormal,
ev -> new DeleteTempFilesTask(app.body, GuiTask.GuiTaskStyle.HIDDEN, ".+", 24 * 3600000) {
@Override
protected void onFailed(Throwable e) {
Platform.exit();
}

@Override
protected void onSucceeded(boolean result) {
Platform.exit();
}
}.start()
);
}, durationFast, durationNormal);
}

Expand Down
73 changes: 73 additions & 0 deletions desktop/src/cn/harryh/arkpets/guitasks/DeleteTempFilesTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/** Copyright (c) 2022-2025, Harry Huang
* At GPL-3.0 License
*/
package cn.harryh.arkpets.guitasks;

import cn.harryh.arkpets.Const.PathConfig;
import cn.harryh.arkpets.utils.IOUtils.FileUtil;
import javafx.concurrent.Task;
import javafx.scene.layout.StackPane;

import java.io.File;
import java.nio.file.*;
import java.util.List;
import java.util.stream.Stream;
import java.util.regex.Pattern;


public abstract class DeleteTempFilesTask extends GuiTask {
protected final Pattern regex;
protected final long timestamp;

public DeleteTempFilesTask(StackPane parent, GuiTaskStyle style, String matchRegex, long beforeMillis) {
super(parent, style);
this.regex = Pattern.compile(matchRegex);
this.timestamp = System.currentTimeMillis() - beforeMillis;
}

@Override
protected Task<Boolean> getTask() {
return new Task<>() {
@Override
protected Boolean call() throws Exception {
File temp = new File(PathConfig.tempDirPath);
if (!temp.isDirectory()) {
return true;
}

List<Path> filesToDelete;
try (Stream<Path> pathStream = Files.walk(temp.toPath())) {
filesToDelete = pathStream
.filter(path -> {
File file = path.toFile();
return file.isFile()
&& regex.matcher(file.getName()).matches()
&& file.lastModified() < timestamp;
})
.toList();
}

int total = filesToDelete.size();
if (total == 0) {
return true;
}

this.updateProgress(0, total);
for (int i = 0; i < filesToDelete.size(); i++) {
if (isCancelled()) {
break;
}
FileUtil.delete(filesToDelete.get(i), true);
updateProgress(i + 1, total);
}

return !isCancelled();
}
};
}

@Override
protected String getHeader() {
return "正在清理临时文件...";
}
}

0 comments on commit 1b3d05d

Please sign in to comment.