mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-07 08:31:02 +08:00
fix: cleaning up the cache folder caused permission issues
This commit is contained in:
@@ -3,7 +3,6 @@ package bot
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto"
|
"github.com/celestix/gotgproto"
|
||||||
@@ -90,12 +89,10 @@ func Init() {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
common.Log.Fatal("初始化客户端失败: 超时")
|
common.Log.Panic("初始化客户端失败: 超时")
|
||||||
os.Exit(1)
|
|
||||||
case result := <-resultChan:
|
case result := <-resultChan:
|
||||||
if result.err != nil {
|
if result.err != nil {
|
||||||
common.Log.Fatalf("初始化客户端失败: %s", result.err)
|
common.Log.Panicf("初始化客户端失败: %s", result.err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
Client = result.client
|
Client = result.client
|
||||||
RegisterHandlers(Client.Dispatcher)
|
RegisterHandlers(Client.Dispatcher)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func Run(_ *cobra.Command, _ []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
common.Log.Info("正在清理缓存文件夹: ", cachePath)
|
common.Log.Info("正在清理缓存文件夹: ", cachePath)
|
||||||
if err := os.RemoveAll(cachePath); err != nil {
|
if err := common.RemoveAllInDir(cachePath); err != nil {
|
||||||
common.Log.Error("清理缓存失败: ", err)
|
common.Log.Error("清理缓存失败: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
common/os.go
35
common/os.go
@@ -1,31 +1,11 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 创建文件, 自动创建目录
|
|
||||||
func MkFile(path string, data []byte) error {
|
|
||||||
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return os.WriteFile(path, data, os.ModePerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除文件, 并清理空目录. 如果文件不存在则返回 nil
|
|
||||||
func PurgeFile(path string) error {
|
|
||||||
if err := os.Remove(path); err != nil {
|
|
||||||
if !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RemoveEmptyDirectories(filepath.Dir(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
func RmFileAfter(path string, td time.Duration) {
|
func RmFileAfter(path string, td time.Duration) {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -34,22 +14,23 @@ func RmFileAfter(path string, td time.Duration) {
|
|||||||
}
|
}
|
||||||
Log.Debugf("Remove file after %s: %s", td, path)
|
Log.Debugf("Remove file after %s: %s", td, path)
|
||||||
time.AfterFunc(td, func() {
|
time.AfterFunc(td, func() {
|
||||||
PurgeFile(path)
|
if err := os.Remove(path); err != nil {
|
||||||
|
Log.Errorf("Failed to remove file %s: %s", path, err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归删除空目录
|
// 删除目录下的所有内容, 但不删除目录本身
|
||||||
func RemoveEmptyDirectories(dirPath string) error {
|
func RemoveAllInDir(dirPath string) error {
|
||||||
entries, err := os.ReadDir(dirPath)
|
entries, err := os.ReadDir(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(entries) == 0 {
|
for _, entry := range entries {
|
||||||
err := os.Remove(dirPath)
|
entryPath := filepath.Join(dirPath, entry.Name())
|
||||||
if err != nil {
|
if err := os.RemoveAll(entryPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return RemoveEmptyDirectories(filepath.Dir(dirPath))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user