Skip to content

Commit 38e4ae4

Browse files
committed
Add new feature: Splitting backup by total size of files in a ZIP file.
1 parent bd62d6d commit 38e4ae4

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

main.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ export default class DiffZipBackupPlugin extends Plugin {
210210
)
211211
.filter((e) => skippableFiles.indexOf(e) == -1);
212212
let processed = 0;
213+
let processedSize = 0;
214+
let hasExtra = false;
213215
const processedFiles = [] as string[];
214216
let zipped = 0;
215217
for (const path of normalFiles) {
@@ -254,6 +256,7 @@ export default class DiffZipBackupPlugin extends Plugin {
254256
}
255257
}
256258
zipped++;
259+
processedSize += content.byteLength;
257260

258261
// Update the file information
259262
toc[path] = {
@@ -282,18 +285,27 @@ export default class DiffZipBackupPlugin extends Plugin {
282285
if (this.settings.maxFilesInZip > 0 && zipped >= this.settings.maxFilesInZip) {
283286
this.logMessage(
284287
`Max files in a single ZIP has been reached. The rest of the files will be archived in the next process`,
285-
key
288+
"result" + key
289+
);
290+
hasExtra = true;
291+
break;
292+
}
293+
if (this.settings.maxTotalSizeInZip > 0 && processedSize >= this.settings.maxTotalSizeInZip * 1024 * 1024) {
294+
this.logMessage(
295+
`Max total size in a single ZIP has been reached (${processedSize} of ${this.settings.maxTotalSizeInZip * 1024 * 1024}). The rest of the files will be archived in the next process`,
296+
"result" + key
286297
);
298+
hasExtra = true;
287299
break;
288300
}
289301
}
290302
this.logMessage(
291-
`All ${processed} files have been scanned, ${zipped} files are now compressing. please wait for a while`,
303+
`${processed} of ${normalFiles.length} files have been scanned, ${zipped} files are now compressing. please wait for a while`,
292304
key
293305
);
294306
if (zipped == 0 && missingFiles == 0) {
295307
this.logMessage(
296-
`All ${processed} files have been scanned, but nothing has been changed!\nGenerating ZIP has been skipped.`,
308+
`${processed} of ${normalFiles.length} files have been scanned, and nothing has been changed!\nGenerating ZIP has been skipped.`,
297309
key
298310
);
299311
return;
@@ -335,16 +347,15 @@ export default class DiffZipBackupPlugin extends Plugin {
335347
throw new Error(`Updating TOC has been failed!`);
336348
}
337349
log(`Backup information has been updated`, key);
338-
if (
339-
this.settings.maxFilesInZip > 0 &&
340-
zipped >= this.settings.maxFilesInZip &&
341-
this.settings.performNextBackupOnMaxFiles
342-
) {
350+
if (hasExtra && this.settings.performNextBackupOnMaxFiles) {
343351
setTimeout(() => {
344352
this.createZip(verbosity, [...skippableFiles, ...processedFiles], onlyNew, skipDeleted);
345353
}, 10);
346354
} else {
347-
this.logMessage(`${processed} of ${normalFiles.length} files have been processed, ${zipped} files have been zipped.`, key);
355+
this.logMessage(
356+
`${processed} of ${normalFiles.length} files have been processed, ${zipped} files have been zipped.`,
357+
key
358+
);
348359
}
349360
// } else {
350361
// this.logMessage(`Backup has been aborted \n${processed} files, ${zipped} zip files`, "proc-zip-process");

src/DiffZipSettingTab.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ export class DiffZipSettingTab extends PluginSettingTab {
239239
await this.plugin.saveSettings();
240240
})
241241
);
242+
new Setting(containerEl)
243+
.setName("Max total source size in a single ZIP in MB")
244+
.setDesc("(0 to disabled) Limit the total size of files in a single ZIP file to better restore performance")
245+
.addText((text) =>
246+
text
247+
.setPlaceholder("30")
248+
.setValue(this.plugin.settings.maxTotalSizeInZip + "")
249+
.onChange(async (value) => {
250+
this.plugin.settings.maxTotalSizeInZip = Number.parseInt(value);
251+
await this.plugin.saveSettings();
252+
})
253+
);
242254
new Setting(containerEl)
243255
.setName("Perform all files over the max files")
244256
.setDesc(

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface DiffZipBackupSettings {
1313
restoreFolder: string;
1414
maxSize: number;
1515
maxFilesInZip: number;
16+
maxTotalSizeInZip: number;
1617
performNextBackupOnMaxFiles: boolean;
1718
startBackupAtLaunch: boolean;
1819
startBackupAtLaunchType: AutoBackupType;
@@ -39,6 +40,7 @@ export const DEFAULT_SETTINGS: DiffZipBackupSettings = {
3940
restoreFolder: "restored",
4041
includeHiddenFolder: false,
4142
maxSize: 30,
43+
maxTotalSizeInZip: 30,
4244
desktopFolderEnabled: false,
4345
bucketEnabled: false,
4446
endPoint: "",

0 commit comments

Comments
 (0)