Added folder size file limit
							parent
							
								
									a1bb7ed8f3
								
							
						
					
					
						commit
						5aebbdbf75
					
				|  | @ -32,7 +32,9 @@ dependencies { | |||
| 	include "io.github.cottonmc:Jankson-Fabric:2.0.0+j1.2.0" | ||||
| 	include "io.github.cottonmc.cotton:cotton-logging:1.0.0-rc.4" | ||||
| 	include "io.github.cottonmc.cotton:cotton-config:1.0.0-rc.7" | ||||
| 
 | ||||
| 	include "org.apache.commons:commons-compress:1.8.1" | ||||
| 	include "org.apache.commons:commons-io:2.6" | ||||
| 
 | ||||
| 	// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. | ||||
| 	// You may need to force-disable transitiveness on them. | ||||
|  |  | |||
|  | @ -41,6 +41,9 @@ public class ConfigHandler { | |||
|     @Comment("\nMaximum age of backups to keep in seconds.\n if 0 then backups will not be deleted based on age \n") | ||||
|     public int maxAge = 0; | ||||
| 
 | ||||
|     @Comment("\nMaximum size of backup folder in kilo bytes. \n") | ||||
|     public int maxSize = 0; | ||||
| 
 | ||||
|     @Comment("\nCompression level \n0 - 9\n") | ||||
|     public int compression = 1; | ||||
| 
 | ||||
|  |  | |||
|  | @ -24,19 +24,20 @@ import net.minecraft.server.MinecraftServer; | |||
| import net.minecraft.server.command.ServerCommandSource; | ||||
| import net.minecraft.text.TranslatableText; | ||||
| import net.szum123321.textile_backup.TextileBackup; | ||||
| import org.apache.commons.io.FileUtils; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.FileFilter; | ||||
| import java.time.LocalDateTime; | ||||
| import java.time.ZoneOffset; | ||||
| import java.time.format.DateTimeFormatter; | ||||
| import java.util.Arrays; | ||||
| import java.util.Objects; | ||||
| 
 | ||||
| public class BackupHelper { | ||||
| 
 | ||||
|     public static void log(String s, ServerCommandSource ctx){ | ||||
|         if(ctx != null) | ||||
|             ctx.sendFeedback(new TranslatableText(s), true); | ||||
|             ctx.sendFeedback(new TranslatableText(s), false); | ||||
| 
 | ||||
|         if(TextileBackup.config.log) | ||||
|             TextileBackup.logger.info(s); | ||||
|  | @ -81,11 +82,13 @@ public class BackupHelper { | |||
|     public static void executeFileLimit(ServerCommandSource ctx){ | ||||
|         File root = getBackupRootPath(); | ||||
| 
 | ||||
|         FileFilter filter = f -> f.getName().endsWith("zip"); | ||||
| 
 | ||||
|         if(root.isDirectory() && root.exists()){ | ||||
|             if(TextileBackup.config.maxAge > 0){ | ||||
|                 LocalDateTime now = LocalDateTime.now(); | ||||
| 
 | ||||
|                 for(File f: Objects.requireNonNull(root.listFiles())){ | ||||
|                 Arrays.stream(root.listFiles()).forEach(f ->{ | ||||
|                     if(f.exists() && f.isFile()){ | ||||
|                         LocalDateTime creationTime = LocalDateTime.from( | ||||
|                                 getDateTimeFormatter().parse( | ||||
|  | @ -98,14 +101,15 @@ public class BackupHelper { | |||
|                             f.delete(); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|                 }); | ||||
|             } | ||||
| 
 | ||||
|             if(TextileBackup.config.backupsToKeep > 0 && Objects.requireNonNull(root.listFiles()).length > TextileBackup.config.backupsToKeep){ | ||||
|                 int var1 = Objects.requireNonNull(root.listFiles()).length - TextileBackup.config.backupsToKeep; | ||||
|             if(TextileBackup.config.backupsToKeep > 0 && root.listFiles().length > TextileBackup.config.backupsToKeep){ | ||||
|                 int var1 = root.listFiles().length - TextileBackup.config.backupsToKeep; | ||||
| 
 | ||||
|                 File[] files = root.listFiles(); | ||||
|                 File[] files = root.listFiles(filter); | ||||
|                 assert files != null; | ||||
| 
 | ||||
|                 Arrays.sort(files); | ||||
| 
 | ||||
|                 for(int i = 0; i < var1; i++) { | ||||
|  | @ -113,6 +117,15 @@ public class BackupHelper { | |||
|                     files[i].delete(); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             if(TextileBackup.config.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > TextileBackup.config.maxSize){ | ||||
|                  Arrays.stream(root.listFiles()).sorted().forEach(e -> { | ||||
|                     if(FileUtils.sizeOfDirectory(root) / 1024 > TextileBackup.config.maxSize){ | ||||
|                         log("Deleting: " + e.getName(), ctx); | ||||
|                         e.delete(); | ||||
|                     } | ||||
|                 }); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue