Commit 13052025

Old Photo Deleter .
This commit is contained in:
2025-05-13 14:52:26 +07:00
parent d2e7d1155d
commit bc6821a33e
9 changed files with 302 additions and 14 deletions

View File

@@ -0,0 +1,130 @@
package Database;
import Config.SomeCodes;
import org.tinylog.Logger;
import java.nio.file.Path;
import java.time.LocalDateTime;
/**
* PhotoCleaner class
* Responsible for deleting photos older than a certain number of days
*/
public class PhotoCleaner {
private final int days;
private final boolean[] started;
/**
* PhotoCleaner constructor
* @param days how many days old photos to delete
*/
public PhotoCleaner(int days){
this.days = days;
this.started = new boolean[]{false};
System.out.println("PhotoCleaner created, days: "+days);
}
/**
* Start PhotoCleaner Thread
*/
@SuppressWarnings("BusyWait")
public void Start(){
Thread thread = new Thread(() -> {
started[0] = true;
System.out.println("PhotoCleaner started");
while (started[0]) {
try {
System.out.println("Rechecking Database for older photos...");
// Delete photos older than the specified number of days
LocalDateTime now = LocalDateTime.now();
// delete from database
Thread sqldelete = new Thread(()->{
Sqlite sql = new Sqlite();
PhotoReviewClass[] prcs = sql.GetAll();
System.out.println("Database contains "+prcs.length+" PhotoReviewClass");
for (PhotoReviewClass prc : prcs) {
if (!started[0]) break;
if (prc != null && prc.getDateTime() != null) {
LocalDateTime ldt = SomeCodes.StringToLocalDateTime(prc.getDateTime());
if (ldt!=null){
if (ldt.isBefore(now.minusDays(days))) {
SomeCodes.Delete(prc.fullres());
SomeCodes.Delete(prc.cropped());
SomeCodes.Delete(prc.compressed());
SomeCodes.Delete(prc.compressedcrop());
SomeCodes.Delete(prc.thumbnails());
sql.Delete(prc);
System.out.println("Deleted PhotoReviewClass: " + prc.getDateTime() + " " + prc.getPrefix());
}
}
}
}
});
RunThread(sqldelete, "DeleteFromDatabase");
// delete from disk
Thread deletefullres = new Thread(new DiskDeleter(SomeCodes.config.getFullQualityDirectory()));
RunThread(deletefullres, "DeleteFullRes");
Thread deletefullrescrop = new Thread(new DiskDeleter(SomeCodes.config.getFullQualityCropDirectory()));
RunThread(deletefullrescrop, "DeleteFullResCrop");
Thread deletecompressed = new Thread(new DiskDeleter(SomeCodes.config.getCompressedDirectory()));
RunThread(deletecompressed, "DeleteCompressed");
Thread deletecompressedcrop = new Thread(new DiskDeleter(SomeCodes.config.getCompressedCropDirectory()));
RunThread(deletecompressedcrop, "DeleteCompressedCrop");
Thread deletethumbnails = new Thread(new DiskDeleter(SomeCodes.config.getThumbsDirectory()));
RunThread(deletethumbnails, "DeleteThumbnails");
// Sleep for 1 hour
Thread.sleep(60 * 60 * 1000);
} catch (Exception e) {
Logger.error("Error in PhotoCleaner Thread: " + e.getMessage());
}
}
System.out.println("PhotoCleaner stopped");
});
thread.setName("PhotoCleaner Thread");
thread.setDaemon(true);
thread.start();
}
private void RunThread(Thread thread, String name){
thread.setName(name);
thread.setDaemon(true);
thread.start();
}
class DiskDeleter implements Runnable{
private final String path;
private final LocalDateTime now ;
public DiskDeleter(String path){
this.path = path;
this.now = LocalDateTime.now();
}
public void run(){
System.out.println("DiskDeleter started, path: "+path);
Path[] ppp = SomeCodes.GetFilesInDirectory(path);
if (ppp != null){
System.out.println("DiskDeleter found "+ppp.length+" files in path: "+path);
for(Path p : ppp){
if (!started[0]) break;
LocalDateTime ldt = SomeCodes.GetCreationTime(p);
if (ldt != null) {
if (ldt.isBefore(now.minusDays(days))) {
SomeCodes.Delete(p);
}
}
}
}
}
}
/**
* Stop PhotoCleaner Thread
*/
public void Stop(){
started[0] = false;
}
}