Skip to content

Commit dfc1285

Browse files
authored
Merge pull request #25 from Cosr-Backup/origin
Origin
2 parents d53914b + fcc8ff3 commit dfc1285

File tree

4 files changed

+47
-72
lines changed

4 files changed

+47
-72
lines changed

client/middleware/retry/retry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
"github.com/charmbracelet/log"
8-
"github.com/go-faster/errors"
98
"github.com/gotd/td/bin"
109
"github.com/gotd/td/telegram"
1110
"github.com/gotd/td/tg"
@@ -37,7 +36,8 @@ func (r retry) Handle(next tg.Invoker) telegram.InvokeFunc {
3736
retries++
3837
continue
3938
}
40-
return errors.Wrap(err, "retry middleware skip")
39+
// retry middleware skip
40+
return err
4141
}
4242

4343
return nil

common/utils/tgutil/message.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/celestix/gotgproto/ext"
9+
"github.com/celestix/gotgproto/functions"
910
"github.com/duke-git/lancet/v2/maputil"
1011

1112
"github.com/duke-git/lancet/v2/mathutil"
@@ -19,6 +20,9 @@ import (
1920
"github.com/rs/xid"
2021
)
2122

23+
// generate a file name from the message content and media type
24+
//
25+
// it will never return an empty string
2226
func GenFileNameFromMessage(message tg.Message) string {
2327
ext := func(media tg.MessageMediaClass) string {
2428
switch media := media.(type) {
@@ -82,7 +86,13 @@ func GenFileNameFromMessage(message tg.Message) string {
8286
}()
8387

8488
if filename == "" {
85-
filename = fmt.Sprintf("%d_%s", message.GetID(), xid.New().String())
89+
mname, err := functions.GetMediaFileNameWithId(message.Media)
90+
if err != nil {
91+
filename = fmt.Sprintf("%d_%s", message.GetID(), xid.New().String())
92+
} else {
93+
filename = mname
94+
}
95+
8696
}
8797
return filename + ext
8898
}

config.example.toml

Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,34 @@
1-
#创建文件时,若需要保留中文注释,请务必确保本文件编码为 UTF-8 ,否则会无法读取。
2-
workers = 4 # 同时下载文件数
3-
retry = 3 # 下载失败重试次数
4-
threads = 4 # 单个任务下载最大线程数
5-
stream = false # 使用stream模式, 详情请查看文档
1+
# 创建文件时,若需要保留中文注释,请务必确保本文件编码为 UTF-8 ,否则会无法读取。
2+
# 更详细的配置请在 https://sabot.unv.app/deployment/configuration 查看
3+
workers = 4 # 同时下载文件数
4+
retry = 3 # 下载失败重试次数
5+
threads = 4 # 单个任务下载使用的最大线程数
6+
stream = false # 使用流式传输模式, 建议仅在硬盘空间十分有限时使用.
67

78
[telegram]
89
# Bot Token
9-
# 更换 Bot Token 后请删除数据库文件和 session.db
10+
# 更换 Bot Token 后请删除会话数据库文件 (默认路径为 data/session.db )
1011
token = ""
1112
# Telegram API 配置, 若不配置也可运行, 将使用默认的 API ID 和 API HASH
1213
# 推荐使用自己的 API ID 和 API HASH (https://my.telegram.org)
1314
# app_id = 1025907
1415
# app_hash = "452b0359b988148995f22ff0f4229750"
15-
16-
# 初始化超时时间, 单位: 秒
17-
timeout = 60
18-
# flood_retry = 5
19-
# rpc_retry = 5
20-
2116
[telegram.proxy]
2217
# 启用代理连接 telegram, 只支持 socks5
2318
enable = false
2419
url = "socks5://127.0.0.1:7890"
2520

26-
# 用户列表
27-
[[users]]
28-
# telegram user id
29-
id = 114514
30-
# 使用黑名单模式,开启后下方留空以使用所有存储,反之则为白名单,白名单请在下方输入允许的存储名
31-
blacklist = true
32-
# 将列表留空并开启黑名单模式以允许使用所有存储,此处示例为黑名单模式,用户 114514 可使用所有存储
33-
storages = []
34-
35-
[[users]]
36-
id = 123456
37-
blacklist = false # 使用白名单模式,此时,用户123456 仅可使用下方列表中的存储
38-
# 此时该用户只能使用名为 本机1 的存储
39-
storages = ["本机1"]
40-
4121
# 存储列表
4222
[[storages]]
4323
# 标识名, 需要唯一
4424
name = "本机1"
45-
# 存储类型, 目前可用: local, alist, webdav, minio
25+
# 存储类型, 目前可用: local, alist, webdav, minio, telegram
4626
type = "local"
4727
# 启用存储
4828
enable = true
4929
# 文件保存根路径
5030
base_path = "./downloads"
5131

52-
[[storages]]
53-
name = "MyAlist"
54-
type = "alist"
55-
enable = false #记得启用
56-
base_path = '/'
57-
url = 'https://alist.com'
58-
username = 'admin'
59-
password = 'password'
60-
# alist token 刷新时间
61-
# 86400--1天 604800--7天 1296000--15天 2592000--30天 15552000--180天
62-
token_exp = 86400
63-
# alist 可直接使用 token 登录, 此时 username, password, token_exp 将被忽略
64-
# 请自行在 alist 侧配置合理的 token 过期时间
65-
# token = ""
66-
6732
[[storages]]
6833
name = "MyWebdav"
6934
type = "webdav"
@@ -73,28 +38,17 @@ url = 'https://example.com/dav'
7338
username = 'username'
7439
password = 'password'
7540

76-
[[storages]]
77-
name = "MyMinio"
78-
type = "minio"
79-
enable = true
80-
endpoint = 'play.min.io'
81-
use_ssl = true
82-
access_key_id = 'Q3AM3UQ867SPQQA43P2F'
83-
secret_access_key = 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
84-
bucket_name = 'saveanybot'
85-
base_path = '/path/telegram'
86-
87-
[[storages]]
88-
name = "mychannel"
89-
type = "telegram"
90-
enable = true
91-
chat_id = 1820371480
92-
93-
# [temp]
94-
# # 下载文件临时目录, 请不要在此目录下存放任何其他文件
95-
# base_path = "cache/"
96-
97-
# [db]
98-
# path = "data/data.db" # 数据库文件路径
99-
# session = "data/session.db"
41+
# 用户列表
42+
[[users]]
43+
# telegram user id
44+
id = 114514
45+
# 存储过滤列表, 元素为存储标识名.
46+
# 将该列表留空并开启黑名单过滤模式以允许使用所有存储,此处示例为黑名单模式,用户 114514 可使用所有存储
47+
storages = []
48+
# 使用列表过滤黑名单模式,反之则为白名单,白名单请在列表中指定可用的存储.
49+
blacklist = true
10050

51+
[[users]]
52+
id = 123456
53+
storages = ["本机1"]
54+
blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储

storage/telegram/telegram.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"fmt"
66
"io"
77
"path"
8+
"strconv"
9+
"strings"
810
"time"
911

12+
"github.com/duke-git/lancet/v2/convertor"
1013
"github.com/gabriel-vasile/mimetype"
1114
"github.com/gotd/td/telegram/message"
1215
"github.com/gotd/td/telegram/message/styling"
@@ -70,9 +73,17 @@ func (t *Telegram) Save(ctx context.Context, r io.Reader, storagePath string) er
7073
if tctx == nil {
7174
return fmt.Errorf("failed to get telegram context")
7275
}
73-
peer := tctx.PeerStorage.GetInputPeerById(t.config.ChatID)
76+
chatID := t.config.ChatID
77+
if after, ok0 := strings.CutPrefix(convertor.ToString(chatID), "-100"); ok0 {
78+
cid, err := strconv.ParseInt(after, 10, 64)
79+
if err != nil {
80+
return fmt.Errorf("failed to parse chat ID: %w", err)
81+
}
82+
chatID = cid
83+
}
84+
peer := tctx.PeerStorage.GetInputPeerById(chatID)
7485
if peer == nil {
75-
return fmt.Errorf("failed to get input peer for chat ID %d", t.config.ChatID)
86+
return fmt.Errorf("failed to get input peer for chat ID %d", chatID)
7687
}
7788
mtype, err := mimetype.DetectReader(rs)
7889
if err != nil {

0 commit comments

Comments
 (0)