Browse Source

强制清理,超过 7 * 24h 的字幕校正缓存文件

Signed-off-by: 716 <[email protected]>
716 4 years ago
parent
commit
7ad71f7444

+ 14 - 0
internal/logic/downloader_helper/downloader_helper.go

@@ -9,6 +9,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter"
 	"github.com/sirupsen/logrus"
+	"time"
 )
 
 type DownloaderHelper struct {
@@ -86,6 +87,19 @@ func (d DownloaderHelper) Start() error {
 		return err
 	}
 
+	d.logger.Infoln("Will Scan SubFixCache Folder, Clear files that are more than 7 * 24 hours old")
+	// 清理多天没有使用的时间轴字幕校正缓存文件
+	rootSubFixCache, err := my_util.GetRootSubFixCacheFolder()
+	if err != nil {
+		d.logger.Errorln("GetRootSubFixCacheFolder", err)
+		return err
+	}
+	err = my_util.ClearIdleSubFixCacheFolder(rootSubFixCache, 7*24*time.Hour)
+	if err != nil {
+		d.logger.Errorln("ClearIdleSubFixCacheFolder", err)
+		return err
+	}
+
 	return nil
 }
 

+ 1 - 3
internal/pkg/get_access_time/linux.go

@@ -3,7 +3,6 @@
 package get_access_time
 
 import (
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"os"
 	"syscall"
 	"time"
@@ -25,6 +24,5 @@ func (d OneGetAccessTime) GetAccessTime(fileName string) (time.Time, error) {
 	}
 
 	aTime := fi.Sys().(*syscall.Stat_t).Atim
-	return my_util.Second2Time(aTime.Nanoseconds() / 1e9), nil
-
+	return time.Unix(aTime.Nanoseconds()/1e9, 0), nil
 }

+ 1 - 2
internal/pkg/get_access_time/windows.go

@@ -3,7 +3,6 @@
 package get_access_time
 
 import (
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"os"
 	"syscall"
 	"time"
@@ -28,5 +27,5 @@ func (d OneGetAccessTime) GetAccessTime(fileName string) (time.Time, error) {
 	// https://github.com/golang/go/commit/bd75468a089c8ad38bcb1130c4ed7d2703ef85c1
 	// https://github.com/golang/go/issues/31735
 	aTime := fi.Sys().(*syscall.Win32FileAttributeData).LastAccessTime
-	return my_util.Second2Time(aTime.Nanoseconds() / 1e9), nil
+	return time.Unix(aTime.Nanoseconds()/1e9, 0), nil
 }

+ 73 - 1
internal/pkg/my_util/folder.go

@@ -1,12 +1,15 @@
 package my_util
 
 import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/get_access_time"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
-	"os"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
+	os "os"
 	"path/filepath"
 	"runtime"
 	"strconv"
 	"strings"
+	"time"
 )
 
 // --------------------------------------------------------------
@@ -301,6 +304,75 @@ func GetConfigRootDirFPath() string {
 	return nowConfigFPath
 }
 
+// ClearIdleSubFixCacheFolder 清理闲置的字幕修正缓存文件夹
+func ClearIdleSubFixCacheFolder(rootSubFixCacheFolder string, outOfDate time.Duration) error {
+
+	/*
+		从 GetRootSubFixCacheFolder 目录下,遍历第一级目录中的文件夹
+		然后每个文件夹中,统计里面最后的访问时间(可能有多个文件),如果超过某个时间范围就标记删除这个文件夹
+	*/
+	pathSep := string(os.PathSeparator)
+	files, err := os.ReadDir(rootSubFixCacheFolder)
+	if err != nil {
+		return err
+	}
+	wait2ScanFolder := make([]string, 0)
+	for _, curFile := range files {
+
+		fullPath := rootSubFixCacheFolder + pathSep + curFile.Name()
+		if curFile.IsDir() == true {
+			// 需要关注文件夹
+			wait2ScanFolder = append(wait2ScanFolder, fullPath)
+		}
+	}
+
+	wait2DeleteFolder := make([]string, 0)
+	getAccessTimeEx := get_access_time.GetAccessTimeEx{}
+	cutOff := time.Now().Add(-outOfDate)
+	for _, s := range wait2ScanFolder {
+
+		files, err = os.ReadDir(s)
+		if err != nil {
+			return err
+		}
+
+		maxAccessTime := time.Now()
+		// 需要统计这个文件夹下的所有文件的 AccessTIme,找出最新(最大的值)的那个时间,再比较
+		for i, curFile := range files {
+
+			fullPath := s + pathSep + curFile.Name()
+			if curFile.IsDir() == true {
+				continue
+			}
+			// 只需要关注文件
+			accessTime, err := getAccessTimeEx.GetAccessTime(fullPath)
+			if err != nil {
+				return err
+			}
+			if i == 0 {
+				maxAccessTime = accessTime
+			}
+			if Time2SecondNumber(accessTime) > Time2SecondNumber(maxAccessTime) {
+				maxAccessTime = accessTime
+			}
+		}
+		if maxAccessTime.Sub(cutOff) <= 0 {
+			// 确认可以删除
+			wait2DeleteFolder = append(wait2DeleteFolder, s)
+		}
+	}
+	// 统一清理过期的文件夹
+	for _, s := range wait2DeleteFolder {
+		log_helper.GetLogger().Infoln("Try 2 clear SubFixCache Folder:", s)
+		err := os.RemoveAll(s)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 const (
 	DebugFolder       = "CSF-DebugThings" // 调试相关的文件夹
 	TmpFolder         = "CSF-TmpThings"   // 临时缓存的文件夹

+ 14 - 0
internal/pkg/my_util/folder_test.go

@@ -0,0 +1,14 @@
+package my_util
+
+import (
+	"testing"
+	"time"
+)
+
+func TestClearIdleSubFixCacheFolder(t *testing.T) {
+
+	err := ClearIdleSubFixCacheFolder("W:\\CSF-SubFixCache", 24*time.Hour)
+	if err != nil {
+		t.Fatal(err)
+	}
+}