github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/pkg/crontab/collect.go (about)

     1  package crontab
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"time"
     9  
    10  	model "github.com/cloudreve/Cloudreve/v3/models"
    11  	"github.com/cloudreve/Cloudreve/v3/pkg/cache"
    12  	"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
    13  	"github.com/cloudreve/Cloudreve/v3/pkg/util"
    14  )
    15  
    16  func garbageCollect() {
    17  	// 清理打包下载产生的临时文件
    18  	collectArchiveFile()
    19  
    20  	// 清理过期的内置内存缓存
    21  	if store, ok := cache.Store.(*cache.MemoStore); ok {
    22  		collectCache(store)
    23  	}
    24  
    25  	util.Log().Info("Crontab job \"cron_garbage_collect\" complete.")
    26  }
    27  
    28  func collectArchiveFile() {
    29  	// 读取有效期、目录设置
    30  	tempPath := util.RelativePath(model.GetSettingByName("temp_path"))
    31  	expires := model.GetIntSetting("download_timeout", 30)
    32  
    33  	// 列出文件
    34  	root := filepath.Join(tempPath, "archive")
    35  	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
    36  		if err == nil && !info.IsDir() &&
    37  			strings.HasPrefix(filepath.Base(path), "archive_") &&
    38  			time.Now().Sub(info.ModTime()).Seconds() > float64(expires) {
    39  			util.Log().Debug("Delete expired batch download temp file %q.", path)
    40  			// 删除符合条件的文件
    41  			if err := os.Remove(path); err != nil {
    42  				util.Log().Debug("Failed to delete temp file %q: %s", path, err)
    43  			}
    44  		}
    45  		return nil
    46  	})
    47  
    48  	if err != nil {
    49  		util.Log().Debug("Crontab job cannot list temp batch download folder: %s", err)
    50  	}
    51  
    52  }
    53  
    54  func collectCache(store *cache.MemoStore) {
    55  	util.Log().Debug("Cleanup memory cache.")
    56  	store.GarbageCollect()
    57  }
    58  
    59  func uploadSessionCollect() {
    60  	placeholders := model.GetUploadPlaceholderFiles(0)
    61  
    62  	// 将过期的上传会话按照用户分组
    63  	userToFiles := make(map[uint][]uint)
    64  	for _, file := range placeholders {
    65  		_, sessionExist := cache.Get(filesystem.UploadSessionCachePrefix + *file.UploadSessionID)
    66  		if sessionExist {
    67  			continue
    68  		}
    69  
    70  		if _, ok := userToFiles[file.UserID]; !ok {
    71  			userToFiles[file.UserID] = make([]uint, 0)
    72  		}
    73  
    74  		userToFiles[file.UserID] = append(userToFiles[file.UserID], file.ID)
    75  	}
    76  
    77  	// 删除过期的会话
    78  	for uid, filesIDs := range userToFiles {
    79  		user, err := model.GetUserByID(uid)
    80  		if err != nil {
    81  			util.Log().Warning("Owner of the upload session cannot be found: %s", err)
    82  			continue
    83  		}
    84  
    85  		fs, err := filesystem.NewFileSystem(&user)
    86  		if err != nil {
    87  			util.Log().Warning("Failed to initialize filesystem: %s", err)
    88  			continue
    89  		}
    90  
    91  		if err = fs.Delete(context.Background(), []uint{}, filesIDs, false, false); err != nil {
    92  			util.Log().Warning("Failed to delete upload session: %s", err)
    93  		}
    94  
    95  		fs.Recycle()
    96  	}
    97  
    98  	util.Log().Info("Crontab job \"cron_recycle_upload_session\" complete.")
    99  }