Skip to content

Commit

Permalink
chore: fully async backups
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 28, 2024
1 parent 6a9006a commit 859c5b3
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/backend/backup-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { app } from "electron";
import path from "path";
import fs from "fs";
import fsp from "fs/promises";
import { glob } from "glob";
import { DeflateOptions, Zip, ZipDeflate, unzipSync } from "fflate";
Expand Down Expand Up @@ -144,23 +143,23 @@ class BackupManager {
const maxBackups = SettingsManager.getSetting("MaxBackupCount");

if (maxBackups !== "All") {
const fileNames = (await fsp.readdir(this._backupFolderPath))
.map((v) => {
const fileNames = (await Promise.all((await fsp.readdir(this._backupFolderPath))
.map(async (v) => {
return {
name: v,
time: (fs.statSync(path.join(this._backupFolderPath, v))).birthtime.getTime()
time: (await fsp.stat(path.join(this._backupFolderPath, v))).birthtime.getTime()
};
})
})))
.sort((a, b) => b.time - a.time)
.map(v => v.name)
.filter(n => !n.includes("NODELETE") && n.endsWith(".zip"));

fileNames.splice(0, maxBackups);

fileNames.forEach((f) => {
for (const f of fileNames) {
logger.info(`Deleting old backup: ${f}`);
fs.unlinkSync(path.join(this._backupFolderPath, f));
});
await fsp.unlink(path.join(this._backupFolderPath, f));
}

if (callback instanceof Function) {
callback();
Expand All @@ -184,15 +183,9 @@ class BackupManager {
}.${fileExtension}`;

await fsp.mkdir(this._backupFolderPath, { recursive: true });
const output = fs.createWriteStream(path.join(this._backupFolderPath, filename));

// listen for all archive data to be written
output.on("close", async () => {
SettingsManager.saveSetting("LastBackupDate", new Date());
await this.cleanUpOldBackups(callback);
});
const output = await fsp.open(path.join(this._backupFolderPath, filename), "w");

const archive = new Zip((err, data, final) => {
const archive = new Zip(async (err, data, final) => {
if (err) {
// throw error
if (manualActivation) {
Expand All @@ -204,10 +197,12 @@ class BackupManager {
globalThis.renderWindow.webContents.send("error", err);
throw err;
} else {
output.write(data);
await output.write(data);

if (final) {
output.close();
await output.close();
SettingsManager.saveSetting("LastBackupDate", new Date());
await this.cleanUpOldBackups(callback);
}
}
});
Expand Down

0 comments on commit 859c5b3

Please sign in to comment.