Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75f6b444fc | ||
|
|
4294966ab7 | ||
|
|
edef778959 | ||
|
|
0bd9b77856 | ||
|
|
d5f214494c |
12
common/os.go
12
common/os.go
@@ -33,6 +33,18 @@ func PurgeFile(path string) error {
|
||||
return RemoveEmptyDirectories(filepath.Dir(path))
|
||||
}
|
||||
|
||||
func RmFileAfter(path string, td time.Duration) {
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
logger.L.Errorf("Failed to create timer for %s: %s", path, err)
|
||||
return
|
||||
}
|
||||
logger.L.Debugf("Remove file after %s: %s", td, path)
|
||||
time.AfterFunc(td, func() {
|
||||
PurgeFile(path)
|
||||
})
|
||||
}
|
||||
|
||||
// 递归删除空目录
|
||||
func RemoveEmptyDirectories(dirPath string) error {
|
||||
entries, err := os.ReadDir(dirPath)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
threads = 4 # 下载线程数
|
||||
workers = 4 # 同时下载文件数
|
||||
[telegram]
|
||||
token = "" # Bot Token
|
||||
admins = [777000] # 你的 user_id
|
||||
|
||||
@@ -8,6 +8,10 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Threads int `toml:"threads" mapstructure:"threads"`
|
||||
Workers int `toml:"workers" mapstructure:"workers"`
|
||||
ChunkSize int32 `toml:"chunk_size" mapstructure:"chunk_size"`
|
||||
|
||||
Temp tempConfig `toml:"temp" mapstructure:"temp"`
|
||||
Log logConfig `toml:"log" mapstructure:"log"`
|
||||
DB dbConfig `toml:"db" mapstructure:"db"`
|
||||
@@ -72,6 +76,10 @@ func Init() {
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigType("toml")
|
||||
|
||||
viper.SetDefault("threads", 3)
|
||||
viper.SetDefault("workers", 3)
|
||||
viper.SetDefault("chunk_size", 1024*1024)
|
||||
|
||||
viper.SetDefault("temp.base_path", "cache/")
|
||||
viper.SetDefault("temp.cache_ttl", 3600)
|
||||
|
||||
|
||||
15
core/core.go
15
core/core.go
@@ -3,6 +3,7 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/amarnathcjd/gogram/telegram"
|
||||
@@ -16,6 +17,8 @@ import (
|
||||
)
|
||||
|
||||
func processPendingTask(task types.Task) error {
|
||||
os.MkdirAll(config.Cfg.Temp.BasePath, os.ModePerm)
|
||||
|
||||
message, err := bot.Client.GetMessageByID(task.ChatID, task.MessageID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -23,7 +26,9 @@ func processPendingTask(task types.Task) error {
|
||||
logger.L.Debugf("Start downloading file: %s", task.FileName)
|
||||
bot.Client.EditMessage(task.ChatID, task.ReplyMessageID, "正在下载文件...")
|
||||
dest, err := message.Download(&telegram.DownloadOptions{
|
||||
FileName: common.GetCacheFilePath(task.FileName),
|
||||
FileName: common.GetCacheFilePath(task.FileName),
|
||||
Threads: config.Cfg.Threads,
|
||||
ChunkSize: config.Cfg.ChunkSize,
|
||||
// ProgressCallback: func(totalBytes, downloadedBytes int64) {},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -32,9 +37,9 @@ func processPendingTask(task types.Task) error {
|
||||
|
||||
defer func() {
|
||||
if config.Cfg.Temp.CacheTTL > 0 {
|
||||
common.PurgeFileAfter(dest, time.Duration(config.Cfg.Temp.CacheTTL)*time.Second)
|
||||
common.RmFileAfter(dest, time.Duration(config.Cfg.Temp.CacheTTL)*time.Second)
|
||||
} else {
|
||||
if err := common.PurgeFile(dest); err != nil {
|
||||
if err := os.Remove(dest); err != nil {
|
||||
logger.L.Errorf("Failed to purge file: %s", err)
|
||||
}
|
||||
}
|
||||
@@ -90,8 +95,8 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
|
||||
|
||||
func Run() {
|
||||
logger.L.Info("Start processing tasks...")
|
||||
semaphore := make(chan struct{}, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
semaphore := make(chan struct{}, config.Cfg.Workers)
|
||||
for i := 0; i < config.Cfg.Workers; i++ {
|
||||
go worker(queue.Queue, semaphore)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user