diff --git a/.dockerignore b/.dockerignore
index 7af87d81..84b6ddf3 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -7,6 +7,7 @@ downloads/
data/
cache/
docs/
-config.example.toml
docker-compose.*
-playwright/
\ No newline at end of file
+playwright/
+web/ui/node_modules
+web/dist
diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml
index 7d34bfa1..f9851644 100644
--- a/.github/workflows/build-docker.yml
+++ b/.github/workflows/build-docker.yml
@@ -14,7 +14,11 @@ concurrency:
cancel-in-progress: true
jobs:
+ test:
+ uses: ./.github/workflows/ci.yml
+
prepare:
+ needs: test
runs-on: ubuntu-latest
permissions:
contents: read
@@ -47,7 +51,6 @@ jobs:
strategy:
matrix:
arch: [amd64, arm64]
- type: [default, micro, pico]
fail-fast: false
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || 'ubuntu-24.04-arm' }}
@@ -70,7 +73,7 @@ jobs:
uses: docker/build-push-action@v6
with:
context: .
- file: ${{ matrix.type == 'default' && './Dockerfile' || format('./Dockerfile.{0}', matrix.type) }}
+ file: ./Dockerfile
platforms: ${{ matrix.arch == 'amd64' && 'linux/amd64' || 'linux/arm64' }}
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
build-args: |
@@ -93,7 +96,7 @@ jobs:
- name: Upload digest
uses: actions/upload-artifact@v6
with:
- name: digest-${{ matrix.type }}-${{ matrix.arch }}
+ name: digest-${{ matrix.arch }}
path: /tmp/digests/digest
if-no-files-found: error
retention-days: 1
@@ -103,9 +106,6 @@ jobs:
runs-on: ubuntu-latest
permissions:
packages: write
- strategy:
- matrix:
- type: [default, micro, pico]
steps:
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
@@ -118,43 +118,40 @@ jobs:
uses: actions/download-artifact@v7
with:
path: /tmp/digests
- pattern: digest-${{ matrix.type }}-*
+ pattern: digest-*
merge-multiple: false
- - name: Create and push manifest lists
+ - name: Create and push manifest list
run: |
REPO="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
VERSION="${{ needs.prepare.outputs.version }}"
MAJOR_MINOR="${{ needs.prepare.outputs.major_minor }}"
SHA="${{ needs.prepare.outputs.short_sha }}"
- TYPE="${{ matrix.type }}"
- DIGEST_AMD64=$(cat /tmp/digests/digest-${TYPE}-amd64/digest)
- DIGEST_ARM64=$(cat /tmp/digests/digest-${TYPE}-arm64/digest)
+ DIGEST_AMD64=$(cat /tmp/digests/digest-amd64/digest)
+ DIGEST_ARM64=$(cat /tmp/digests/digest-arm64/digest)
- echo "Found digests for $TYPE:"
echo "AMD64: $DIGEST_AMD64"
echo "ARM64: $DIGEST_ARM64"
- TAGS=()
-
- if [ "$TYPE" == "default" ]; then
- TAGS+=("$REPO:latest")
- TAGS+=("$REPO:$VERSION")
- TAGS+=("$REPO:$MAJOR_MINOR")
- TAGS+=("$REPO:sha-$SHA")
- else
- TAGS+=("$REPO:$TYPE")
- TAGS+=("$REPO:$TYPE-latest")
- TAGS+=("$REPO:$TYPE-$VERSION")
- fi
-
SRC_AMD64="${REPO}@${DIGEST_AMD64}"
SRC_ARM64="${REPO}@${DIGEST_ARM64}"
- echo "Creating manifest list with sources:"
- echo " $SRC_AMD64"
- echo " $SRC_ARM64"
+ if [[ "$VERSION" == *-* ]]; then
+ # Prereleases must not move stable channels such as latest or 1.0.
+ echo "Prerelease $VERSION: publishing exact and commit tags only"
+ TAGS=(
+ "$REPO:$VERSION"
+ "$REPO:sha-$SHA"
+ )
+ else
+ TAGS=(
+ "$REPO:latest"
+ "$REPO:$VERSION"
+ "$REPO:$MAJOR_MINOR"
+ "$REPO:sha-$SHA"
+ )
+ fi
for TAG in "${TAGS[@]}"; do
echo "Pushing tag: $TAG"
diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml
index 77885e9e..57877ccf 100644
--- a/.github/workflows/build-release.yml
+++ b/.github/workflows/build-release.yml
@@ -10,8 +10,12 @@ permissions:
packages: write
jobs:
+ test:
+ uses: ./.github/workflows/ci.yml
+
create-release:
name: Create Release
+ needs: test
runs-on: ubuntu-latest
steps:
- name: Checkout
@@ -21,18 +25,26 @@ jobs:
- name: Create Release
uses: softprops/action-gh-release@v3
+ with:
+ prerelease: ${{ contains(github.ref_name, '-') }}
+
+ - name: Setup pnpm
+ uses: pnpm/action-setup@v4
+ with:
+ version: 12.14.0
- name: Setup node
uses: actions/setup-node@v7
with:
- node-version: 24
+ node-version: 26
- - run: npx changelogithub
+ - run: pnpm dlx changelogithub
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
build-matrix:
name: Release Go Binary
+ needs: test
runs-on: ubuntu-latest
strategy:
matrix:
@@ -45,12 +57,35 @@ jobs:
- name: Checkout
uses: actions/checkout@v6
+ - name: Setup Go
+ uses: actions/setup-go@v5
+ with:
+ go-version: "1.27.x"
+
+ - name: Setup pnpm
+ uses: pnpm/action-setup@v4
+ with:
+ version: 12.14.0
+
+ - name: Setup Node
+ uses: actions/setup-node@v7
+ with:
+ node-version: 26
+ cache: pnpm
+ cache-dependency-path: web/ui/pnpm-lock.yaml
+
- name: Extract version from Git Ref
id: extract_version
run: |
VERSION=$(echo "${{ github.ref }}" | sed 's/refs\/tags\/v//')
echo "VERSION=${VERSION}" >> $GITHUB_ENV
+ - name: Build Web UI
+ run: |
+ cd web/ui
+ pnpm install --frozen-lockfile
+ pnpm run build
+
- name: Release Go Binary
uses: wangyoucao577/go-release-action@v1
with:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 00000000..9cbc5979
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,100 @@
+name: CI
+
+on:
+ push:
+ branches:
+ - main
+ - v1-dev
+ pull_request:
+ workflow_dispatch:
+ workflow_call:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: ci-${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ backend:
+ name: Backend tests
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ steps:
+ - uses: actions/checkout@v6
+
+ - uses: actions/setup-go@v5
+ with:
+ go-version: "1.27.x"
+ cache: true
+
+ - name: Test
+ run: go test ./...
+
+ - name: Vet
+ run: go vet ./...
+
+ - name: API end-to-end tests
+ run: go test -tags=e2e ./e2e -count=1
+
+ - name: Telegram end-to-end tests
+ run: go test -tags=e2e ./e2e/telegram -count=1
+
+ web:
+ name: Web checks
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ steps:
+ - uses: actions/checkout@v6
+
+ - uses: pnpm/action-setup@v4
+ with:
+ version: 12.14.0
+
+ - uses: actions/setup-node@v7
+ with:
+ node-version: 26
+ cache: pnpm
+ cache-dependency-path: web/ui/pnpm-lock.yaml
+
+ - name: Install
+ run: pnpm --dir web/ui install --frozen-lockfile
+
+ - name: Typecheck
+ run: pnpm --dir web/ui run typecheck
+
+ - name: Build
+ run: pnpm --dir web/ui run build
+
+ web-e2e:
+ name: Browser end-to-end tests
+ needs: web
+ runs-on: ubuntu-latest
+ timeout-minutes: 20
+ steps:
+ - uses: actions/checkout@v6
+
+ - uses: actions/setup-go@v5
+ with:
+ go-version: "1.27.x"
+ cache: true
+
+ - uses: pnpm/action-setup@v4
+ with:
+ version: 12.14.0
+
+ - uses: actions/setup-node@v7
+ with:
+ node-version: 26
+ cache: pnpm
+ cache-dependency-path: web/ui/pnpm-lock.yaml
+
+ - name: Install
+ run: pnpm --dir web/ui install --frozen-lockfile
+
+ - name: Install Chromium
+ run: pnpm --dir web/ui exec playwright install --with-deps chromium
+
+ - name: Browser tests
+ run: make e2e-web
diff --git a/.gitignore b/.gitignore
index 480bc4ef..e69e2aa9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,6 @@ testplugins/
tmp-*
saveany-bot
.codegraph/
+web/ui/node_modules/
+web/dist/*
+!web/dist/.gitkeep
diff --git a/Dockerfile b/Dockerfile
index 4d155aa0..9f1577ae 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,4 +1,12 @@
-FROM golang:alpine AS builder
+FROM node:26-alpine AS web
+WORKDIR /ui
+RUN npm install -g pnpm
+COPY web/ui/package.json web/ui/pnpm-lock.yaml web/ui/pnpm-workspace.yaml ./
+RUN pnpm install --frozen-lockfile
+COPY web/ui ./
+RUN pnpm run build
+
+FROM golang:1.27-alpine AS builder
ARG VERSION="dev"
ARG GitCommit="Unknown"
@@ -11,6 +19,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
+COPY --from=web /dist ./web/dist
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
CGO_ENABLED=0 \
diff --git a/Dockerfile.micro b/Dockerfile.micro
deleted file mode 100644
index ca82d24b..00000000
--- a/Dockerfile.micro
+++ /dev/null
@@ -1,41 +0,0 @@
-FROM golang:alpine AS builder
-
-ARG VERSION="dev"
-ARG GitCommit="Unknown"
-ARG BuildTime="Unknown"
-
-WORKDIR /app
-
-COPY go.mod go.sum ./
-RUN --mount=type=cache,target=/go/pkg/mod \
- go mod download
-
-COPY . .
-RUN --mount=type=cache,target=/root/.cache/go-build \
- --mount=type=cache,target=/go/pkg \
- CGO_ENABLED=0 \
- go build -trimpath \
- -tags=no_jsparser,no_minio \
- -ldflags=" \
- -s -w \
- -X 'github.com/krau/SaveAny-Bot/config.Version=${VERSION}' \
- -X 'github.com/krau/SaveAny-Bot/config.GitCommit=${GitCommit}' \
- -X 'github.com/krau/SaveAny-Bot/config.BuildTime=${BuildTime}' \
- -X 'github.com/krau/SaveAny-Bot/config.Docker=true' \
- " \
- -o saveany-bot .
-
-FROM alpine:latest
-
-RUN apk add --no-cache curl
-
-WORKDIR /app
-
-COPY --from=builder /app/saveany-bot .
-COPY entrypoint.sh .
-
-RUN chmod +x /app/saveany-bot && \
- chmod +x /app/entrypoint.sh
-
-ENTRYPOINT ["/app/entrypoint.sh"]
-
diff --git a/Dockerfile.pico b/Dockerfile.pico
deleted file mode 100644
index 8671dad8..00000000
--- a/Dockerfile.pico
+++ /dev/null
@@ -1,41 +0,0 @@
-# pico is the minimum build of SaveAnyBot, which disables all the optional features like JS parsing and MinIO support.
-FROM golang:alpine AS builder
-
-ARG VERSION="dev"
-ARG GitCommit="Unknown"
-ARG BuildTime="Unknown"
-
-WORKDIR /app
-
-RUN apk add --no-cache ca-certificates
-
-COPY go.mod go.sum ./
-RUN --mount=type=cache,target=/go/pkg/mod \
- go mod download
-
-COPY . .
-RUN --mount=type=cache,target=/root/.cache/go-build \
- --mount=type=cache,target=/go/pkg \
- CGO_ENABLED=0 \
- go build -trimpath \
- -tags=no_jsparser,no_minio,sqlite_glebarez \
- -ldflags=" \
- -s -w \
- -X 'github.com/krau/SaveAny-Bot/config.Version=${VERSION}' \
- -X 'github.com/krau/SaveAny-Bot/config.GitCommit=${GitCommit}' \
- -X 'github.com/krau/SaveAny-Bot/config.BuildTime=${BuildTime}' \
- -X 'github.com/krau/SaveAny-Bot/config.Docker=true' \
- " \
- -o saveany-bot . && chmod +x saveany-bot
-
-FROM scratch
-
-WORKDIR /app
-
-COPY --from=builder /app/saveany-bot .
-COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
-
-ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
-ENV SSL_CERT_DIR=/etc/ssl/certs
-
-ENTRYPOINT ["/app/saveany-bot"]
diff --git a/Makefile b/Makefile
index 404f3819..3c21fbe7 100644
--- a/Makefile
+++ b/Makefile
@@ -6,3 +6,28 @@ run:
down:
docker compose -f docker-compose.local.yml down --remove-orphans
+
+web:
+ cd web/ui && pnpm install --frozen-lockfile && pnpm run build && touch ../dist/.gitkeep
+
+build: web
+ CGO_ENABLED=0 go build -trimpath -o saveany-bot .
+
+testenv:
+ go run . testenv
+
+e2e: e2e-api e2e-telegram e2e-web
+
+e2e-api:
+ go test -tags=e2e ./e2e -count=1
+
+e2e-telegram:
+ go test -tags=e2e ./e2e/telegram -count=1
+
+e2e-web-setup:
+ pnpm --dir web/ui exec playwright install chromium
+
+e2e-web: web
+ mkdir -p tmp
+ go build -o tmp/saveany-bot-e2e .
+ pnpm --dir web/ui exec playwright test
diff --git a/README.md b/README.md
index 1ee7dc56..ca01365a 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-#
Save Any Bot
+#
Save Any Bot
**English** | [简体中文](./README_zh.md)
@@ -40,39 +40,45 @@
## 📦 Quick Start
-Create a `config.toml` file with the following content:
-
-```toml
-lang = "en" # Language setting, "en" for English
-[telegram]
-token = "" # Your bot token, obtained from @BotFather
-[telegram.proxy]
-# Enable proxy for Telegram
-enable = false
-url = "socks5://127.0.0.1:7890"
-
-[[storages]]
-name = "Local Disk"
-type = "local"
-enable = true
-base_path = "./downloads"
-
-[[users]]
-id = 114514 # Your Telegram account id
-storages = []
-blacklist = true
-```
-
-Run Save Any Bot with Docker:
+SaveAny-Bot is configured in the web console. Start it with empty data, downloads, and cache directories:
```bash
docker run -d --name saveany-bot \
- -v ./config.toml:/app/config.toml \
+ -p 28080:28080 \
+ -e SAVEANY_LISTEN=0.0.0.0:28080 \
+ -v ./data:/app/data \
-v ./downloads:/app/downloads \
+ -v ./cache:/app/cache \
ghcr.io/krau/saveany-bot:latest
```
-Please [**read the docs**](https://sabot.unv.app/en/) for more configuration options and usage.
+The web UI listens on `127.0.0.1:28080` by default. The startup log prints a direct URL with an access token:
+
+```bash
+docker logs saveany-bot | grep -E "Web UI available at|Generated web admin password"
+```
+
+For a binary deployment, pass the address directly:
+
+```bash
+./saveany-bot --listen 0.0.0.0:28080
+```
+
+Open the URL to sign in. On the first start, the same log also contains the generated admin password; use it to change the password from the sidebar after signing in. Unless an access token is set under **Settings → Web**, a new token is generated on every start. The Guide page covers the required setup:
+
+1. Connect your Telegram bot.
+2. Add a storage destination.
+3. Allow your Telegram account.
+
+Then send files or message links to the bot and watch them in the Tasks page.
+
+To run the binary, download a release, extract it, and start it. It creates `data/` and prints the same tokenized URL:
+
+```bash
+./saveany-bot
+```
+
+Please [**read the docs**](https://sabot.unv.app/en/) for storage backends and usage.
## Sponsors
diff --git a/README_zh.md b/README_zh.md
index 8907c82e..49e952cd 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -1,6 +1,6 @@
-#
Save Any Bot
+#
Save Any Bot
> **把 Telegram 上的文件转存到多种存储端**
@@ -15,19 +15,19 @@
-## 🎯 特性
+## 特性
-- 支持文档/视频/图片/贴纸…甚至还有 [Telegraph](https://telegra.ph/)
-- 破解禁止保存的文件
+- 支持文档、视频、图片、贴纸及 [Telegraph](https://telegra.ph/) 图集
+- 支持转存受限频道中禁止转发与保存的内容
- 批量下载
- 流式传输
-- 多用户使用
-- 基于存储规则的自动整理
-- 监听并自动转存指定聊天的消息, 支持过滤
-- 在不同存储端之间转存文件
-- 集成 yt-dlp, 从所支持的网站下载并转存媒体文件
-- 集成 Aria2, 支持直链/磁力下载和转存
-- 使用 js 编写解析器插件以转存任意网站的文件
+- 多用户支持
+- 基于规则自动分类整理
+- 监听指定聊天并按过滤器自动转存
+- 存储端之间互相传输文件
+- 集成 yt-dlp,下载视频并转存
+- 集成 Aria2,支持直链与磁力链接转存
+- 支持编写 JavaScript 解析器插件扩展站点
- 存储端支持:
- Alist
- S3
@@ -38,38 +38,45 @@
## 快速开始
-创建文件 `config.toml` 并填入以下内容:
-
-```toml
-[telegram]
-token = "" # 你的 Bot Token, 在 @BotFather 获取
-[telegram.proxy]
-# 启用代理连接 telegram
-enable = false
-url = "socks5://127.0.0.1:7890"
-
-[[storages]]
-name = "本地磁盘"
-type = "local"
-enable = true
-base_path = "./downloads"
-
-[[users]]
-id = 114514 # 你的 Telegram 账号 id
-storages = []
-blacklist = true
-```
-
-使用 Docker 运行 Save Any Bot:
+SaveAny-Bot 通过 Web 控制台配置。启动时挂载数据、下载和缓存目录:
```bash
docker run -d --name saveany-bot \
- -v ./config.toml:/app/config.toml \
+ -p 28080:28080 \
+ -e SAVEANY_LISTEN=0.0.0.0:28080 \
+ -v ./data:/app/data \
-v ./downloads:/app/downloads \
+ -v ./cache:/app/cache \
ghcr.io/krau/saveany-bot:latest
```
-请 [**查看文档**](https://sabot.unv.app/) 以获取更多配置选项和使用方法.
+Web 界面默认监听 `127.0.0.1:28080`。启动日志会输出带访问 Token 的直达地址:
+
+```bash
+docker logs saveany-bot | grep -E "Web UI available at|Generated web admin password"
+```
+
+直接运行二进制文件时,可通过命令行指定监听地址:
+
+```bash
+./saveany-bot --listen 0.0.0.0:28080
+```
+
+访问该地址即可登录。首启日志同时包含初始管理员密码,登录后可在侧栏修改。若未在“设置 → Web”中指定 Token,每次启动均会重新生成。初次登录可根据指南页面完成配置:
+
+1. 连接 Telegram Bot。
+2. 添加存储位置。
+3. 授权 Telegram 账号。
+
+完成后向 Bot 发送文件或消息链接,即可在任务页查看转存进度。
+
+若使用预编译二进制文件,解压后直接运行,程序将自动创建 `data/` 目录并输出访问地址:
+
+```bash
+./saveany-bot
+```
+
+更多存储配置与使用方法请参阅[官方文档](https://sabot.unv.app/)。
## 赞助
diff --git a/api/auth.go b/api/auth.go
index 454ed055..2100124d 100644
--- a/api/auth.go
+++ b/api/auth.go
@@ -1,38 +1,283 @@
package api
import (
+ "context"
+ "crypto/rand"
"crypto/subtle"
+ "encoding/base64"
+ "encoding/json"
+ "net"
"net/http"
"strings"
+ "sync"
+ "time"
- "github.com/krau/SaveAny-Bot/config"
+ "github.com/charmbracelet/log"
+ "golang.org/x/time/rate"
+
+ "github.com/krau/SaveAny-Bot/database"
)
-func AuthMiddleware() func(http.Handler) http.Handler {
- return func(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- cfg := config.C().API
+const (
+ sessionCookieName = "sab_session"
+ sessionTTL = 7 * 24 * time.Hour
+ csrfHeader = "X-CSRF-Token"
+)
- authHeader := r.Header.Get("Authorization")
- if authHeader == "" {
- WriteError(w, http.StatusUnauthorized, "unauthorized", "missing authorization header")
- return
+type sessionCtxKey struct{}
+
+func sessionFromContext(ctx context.Context) *database.WebSession {
+ session, _ := ctx.Value(sessionCtxKey{}).(*database.WebSession)
+ return session
+}
+
+type loginLimiter struct {
+ mu sync.Mutex
+ limiter map[string]*rate.Limiter
+ lastSeen map[string]time.Time
+}
+
+func newLoginLimiter() *loginLimiter {
+ return &loginLimiter{
+ limiter: make(map[string]*rate.Limiter),
+ lastSeen: make(map[string]time.Time),
+ }
+}
+
+func (l *loginLimiter) cleanup(maxAge time.Duration) {
+ cutoff := time.Now().Add(-maxAge)
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ for key, seen := range l.lastSeen {
+ if seen.Before(cutoff) {
+ delete(l.lastSeen, key)
+ delete(l.limiter, key)
+ }
+ }
+}
+
+func (l *loginLimiter) allow(key string) bool {
+ l.mu.Lock()
+ defer l.mu.Unlock()
+ l.lastSeen[key] = time.Now()
+ lim, ok := l.limiter[key]
+ if !ok {
+ lim = rate.NewLimiter(rate.Every(12*time.Second), 5)
+ l.limiter[key] = lim
+ }
+ return lim.Allow()
+}
+
+func randomToken() (string, error) {
+ buf := make([]byte, 32)
+ if _, err := rand.Read(buf); err != nil {
+ return "", err
+ }
+ return base64.RawURLEncoding.EncodeToString(buf), nil
+}
+
+func (s *server) isPublic(r *http.Request) bool {
+ if r.URL.Path == "/health" {
+ return true
+ }
+ if r.Method == http.MethodGet && r.URL.Path == "/api/v1/auth/token" {
+ return true
+ }
+ return r.Method == http.MethodPost && r.URL.Path == "/api/v1/auth/login"
+}
+
+func (s *server) authToken() string {
+ if token := strings.TrimSpace(s.deps.Settings.Config().Web.APIToken); token != "" {
+ return token
+ }
+ return s.deps.WebToken
+}
+
+func (s *server) validToken(token string) bool {
+ expected := s.authToken()
+ if expected == "" || token == "" {
+ return false
+ }
+ return subtle.ConstantTimeCompare([]byte(token), []byte(expected)) == 1
+}
+
+func (s *server) authMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if s.isPublic(r) {
+ next.ServeHTTP(w, r)
+ return
+ }
+
+ if token := s.authToken(); token != "" {
+ header := r.Header.Get("Authorization")
+ if len(header) > 7 && strings.EqualFold(header[:7], "Bearer ") {
+ if subtle.ConstantTimeCompare([]byte(header[7:]), []byte(token)) == 1 {
+ next.ServeHTTP(w, r)
+ return
+ }
}
+ }
- parts := strings.SplitN(authHeader, " ", 2)
- if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
- WriteError(w, http.StatusUnauthorized, "unauthorized", "invalid authorization header format")
+ cookie, err := r.Cookie(sessionCookieName)
+ if err != nil || cookie.Value == "" {
+ WriteError(w, http.StatusUnauthorized, "unauthorized", "authentication required")
+ return
+ }
+ session, err := database.GetWebSession(r.Context(), cookie.Value)
+ if err != nil {
+ WriteError(w, http.StatusUnauthorized, "unauthorized", "session expired")
+ return
+ }
+ if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodOptions {
+ if subtle.ConstantTimeCompare([]byte(r.Header.Get(csrfHeader)), []byte(session.CSRF)) != 1 {
+ WriteError(w, http.StatusForbidden, "csrf_failed", "invalid csrf token")
return
}
+ }
+ next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), sessionCtxKey{}, session)))
+ })
+}
- token := parts[1]
+func (s *server) tokenLogin(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Cache-Control", "no-store")
+ if !s.validToken(strings.TrimSpace(r.URL.Query().Get("token"))) {
+ WriteError(w, http.StatusUnauthorized, "invalid_token", "invalid access token")
+ return
+ }
+ if _, err := s.establishSession(w, r); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", "failed to create session")
+ return
+ }
+ http.Redirect(w, r, "/", http.StatusSeeOther)
+}
- if subtle.ConstantTimeCompare([]byte(token), []byte(cfg.Token)) != 1 {
- WriteError(w, http.StatusUnauthorized, "unauthorized", "invalid token")
- return
- }
+func (s *server) login(w http.ResponseWriter, r *http.Request) {
+ host, _, err := net.SplitHostPort(r.RemoteAddr)
+ if err != nil {
+ host = r.RemoteAddr
+ }
+ if !s.logins.allow(host) {
+ WriteError(w, http.StatusTooManyRequests, "too_many_attempts", "too many login attempts")
+ return
+ }
+ var body struct {
+ Password string `json:"password"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if !s.deps.Settings.VerifyAdminPassword(body.Password) {
+ WriteError(w, http.StatusUnauthorized, "invalid_credentials", "invalid password")
+ return
+ }
+ csrf, err := s.establishSession(w, r)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", "failed to create session")
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]string{"csrf_token": csrf})
+}
- next.ServeHTTP(w, r)
- })
+func (s *server) establishSession(w http.ResponseWriter, r *http.Request) (string, error) {
+ id, err := randomToken()
+ if err != nil {
+ return "", err
+ }
+ csrf, err := randomToken()
+ if err != nil {
+ return "", err
+ }
+ session := &database.WebSession{
+ ID: id,
+ CSRF: csrf,
+ CreatedAt: time.Now(),
+ ExpiresAt: time.Now().Add(sessionTTL),
+ }
+ if err := database.CreateWebSession(r.Context(), session); err != nil {
+ return "", err
+ }
+ http.SetCookie(w, &http.Cookie{
+ Name: sessionCookieName,
+ Value: id,
+ Path: "/",
+ MaxAge: int(sessionTTL.Seconds()),
+ HttpOnly: true,
+ SameSite: http.SameSiteLaxMode,
+ Secure: s.deps.Settings.Config().Web.SecureCookie,
+ })
+ return csrf, nil
+}
+
+func (s *server) logout(w http.ResponseWriter, r *http.Request) {
+ if session := sessionFromContext(r.Context()); session != nil {
+ if err := database.DeleteWebSession(r.Context(), session.ID); err != nil {
+ log.FromContext(r.Context()).Warnf("Failed to delete web session: %v", err)
+ }
+ }
+ http.SetCookie(w, &http.Cookie{
+ Name: sessionCookieName,
+ Value: "",
+ Path: "/",
+ MaxAge: -1,
+ HttpOnly: true,
+ SameSite: http.SameSiteLaxMode,
+ })
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) me(w http.ResponseWriter, r *http.Request) {
+ session := sessionFromContext(r.Context())
+ if session == nil {
+ WriteJSON(w, http.StatusOK, map[string]any{"authenticated": false})
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{
+ "authenticated": true,
+ "csrf_token": session.CSRF,
+ })
+}
+
+func (s *server) changePassword(w http.ResponseWriter, r *http.Request) {
+ var body struct {
+ CurrentPassword string `json:"current_password"`
+ NewPassword string `json:"new_password"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if err := s.deps.Settings.ChangeAdminPassword(body.CurrentPassword, body.NewPassword); err != nil {
+ WriteError(w, http.StatusBadRequest, "password_change_failed", err.Error())
+ return
+ }
+ keepID := ""
+ if session := sessionFromContext(r.Context()); session != nil {
+ keepID = session.ID
+ }
+ if err := database.DeleteOtherWebSessions(r.Context(), keepID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "session_cleanup_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
+}
+
+func (s *server) sessionCleanup(ctx context.Context) {
+ if err := database.DeleteExpiredWebSessions(ctx); err != nil {
+ log.FromContext(ctx).Warnf("Failed to delete expired web sessions: %v", err)
+ }
+ s.logins.cleanup(24 * time.Hour)
+ ticker := time.NewTicker(time.Hour)
+ defer ticker.Stop()
+ for {
+ select {
+ case <-ctx.Done():
+ return
+ case <-ticker.C:
+ if err := database.DeleteExpiredWebSessions(ctx); err != nil {
+ log.FromContext(ctx).Warnf("Failed to delete expired web sessions: %v", err)
+ }
+ s.logins.cleanup(24 * time.Hour)
+ }
}
}
diff --git a/api/factory.go b/api/factory.go
index f5d511b6..5f160838 100644
--- a/api/factory.go
+++ b/api/factory.go
@@ -3,9 +3,15 @@ package api
import (
"context"
"encoding/json"
+ "errors"
"fmt"
+ "path"
"time"
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/client/bot"
+ userclient "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/core/tasks/aria2dl"
@@ -18,14 +24,18 @@ import (
"github.com/krau/SaveAny-Bot/core/tasks/ytdlp"
"github.com/krau/SaveAny-Bot/parsers/parsers"
"github.com/krau/SaveAny-Bot/pkg/aria2"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/parser"
"github.com/krau/SaveAny-Bot/pkg/taskevent"
"github.com/krau/SaveAny-Bot/pkg/telegraph"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
"github.com/rs/xid"
)
+var ErrClientUnavailable = errors.New("no telegram client available")
+
type TaskFactory struct {
ctx context.Context
}
@@ -35,32 +45,47 @@ func NewTaskFactory(ctx context.Context) *TaskFactory {
}
func (f *TaskFactory) CreateTask(req *CreateTaskRequest) (*CreateTaskResponse, error) {
- stor, ok := storage.GetStorage(req.Storage)
- if !ok {
- return nil, fmt.Errorf("storage not found: %s", req.Storage)
+ if req.StorageID == 0 {
+ return nil, errors.New("storage_id is required")
+ }
+ stor, err := storage.GetStorageByID(f.ctx, req.StorageID)
+ if err != nil {
+ return nil, fmt.Errorf("storage_id %d not found: %w", req.StorageID, err)
+ }
+ if req.Type != tasktype.TaskTypeTransfer && stor.Type() == storenum.Telegram && bot.ExtContext() == nil && userclient.GetCtx() == nil {
+ return nil, ErrClientUnavailable
}
taskID := xid.New().String()
createdAt := time.Now()
+ var resp *CreateTaskResponse
switch req.Type {
case tasktype.TaskTypeDirectlinks:
- return f.createDirectLinksTask(taskID, createdAt, req, stor)
+ resp, err = f.createDirectLinksTask(taskID, createdAt, req, stor)
case tasktype.TaskTypeYtdlp:
- return f.createYTDLPTask(taskID, createdAt, req, stor)
+ resp, err = f.createYTDLPTask(taskID, createdAt, req, stor)
case tasktype.TaskTypeAria2:
- return f.createAria2Task(taskID, createdAt, req, stor)
+ resp, err = f.createAria2Task(taskID, createdAt, req, stor)
case tasktype.TaskTypeParseditem:
- return f.createParsedTask(taskID, createdAt, req, stor)
+ resp, err = f.createParsedTask(taskID, createdAt, req, stor)
case tasktype.TaskTypeTgfiles:
- return f.createTGFilesTask(taskID, createdAt, req, stor)
+ resp, err = f.createTGFilesTask(taskID, createdAt, req, stor)
case tasktype.TaskTypeTphpics:
- return f.createTPHPicsTask(taskID, createdAt, req, stor)
+ resp, err = f.createTPHPicsTask(taskID, createdAt, req, stor)
case tasktype.TaskTypeTransfer:
- return f.createTransferTask(taskID, createdAt, req)
+ resp, err = f.createTransferTask(taskID, createdAt, req)
default:
return nil, fmt.Errorf("unsupported task type: %s", req.Type)
}
+ if err != nil {
+ return nil, err
+ }
+ if req.Type != tasktype.TaskTypeTransfer {
+ resp.StorageID = storage.ID(stor)
+ resp.StorageName = stor.Name()
+ }
+ return resp, nil
}
func (f *TaskFactory) registerAndEnqueueTask(task core.Executable, taskType tasktype.TaskType, storageName, path, webhook string) error {
@@ -71,12 +96,19 @@ func (f *TaskFactory) registerAndEnqueueTask(task core.Executable, taskType task
// the API store (and fire the webhook on terminal states) without the task
// knowing about the API.
taskCtx := taskevent.WithSink(f.ctx, info)
+ if botCtx := bot.ExtContext(); botCtx != nil {
+ taskCtx = tgutil.ExtWithContext(taskCtx, botCtx)
+ } else if userCtx := userclient.GetCtx(); userCtx != nil {
+ taskCtx = tgutil.ExtWithContext(taskCtx, userCtx)
+ }
- err := core.AddTask(taskCtx, task)
- if err != nil {
+ if err := core.AddTaskWithSource(taskCtx, task, core.TaskSourceAPI); err != nil {
DeleteTask(taskID)
return fmt.Errorf("failed to add task: %w", err)
}
+ if err := core.EnrichTaskRecord(f.ctx, taskID, storageName, path); err != nil {
+ log.Errorf("Failed to enrich task record %s: %v", taskID, err)
+ }
return nil
}
@@ -93,16 +125,18 @@ func (f *TaskFactory) createDirectLinksTask(taskID string, createdAt time.Time,
task := directlinks.NewTask(taskID, f.ctx, params.URLs, stor, req.Path, nil)
- err := f.registerAndEnqueueTask(task, tasktype.TaskTypeDirectlinks, req.Storage, req.Path, req.Webhook)
+ err := f.registerAndEnqueueTask(task, tasktype.TaskTypeDirectlinks, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeDirectlinks,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeDirectlinks,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -118,16 +152,18 @@ func (f *TaskFactory) createYTDLPTask(taskID string, createdAt time.Time, req *C
task := ytdlp.NewTask(taskID, f.ctx, params.URLs, params.Flags, stor, req.Path, nil)
- err := f.registerAndEnqueueTask(task, tasktype.TaskTypeYtdlp, req.Storage, req.Path, req.Webhook)
+ err := f.registerAndEnqueueTask(task, tasktype.TaskTypeYtdlp, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeYtdlp,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeYtdlp,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -158,16 +194,18 @@ func (f *TaskFactory) createAria2Task(taskID string, createdAt time.Time, req *C
task := aria2dl.NewTask(taskID, f.ctx, gid, params.URLs, aria2Client, stor, req.Path, nil)
- err = f.registerAndEnqueueTask(task, tasktype.TaskTypeAria2, req.Storage, req.Path, req.Webhook)
+ err = f.registerAndEnqueueTask(task, tasktype.TaskTypeAria2, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeAria2,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeAria2,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -200,16 +238,18 @@ func (f *TaskFactory) createParsedTask(taskID string, createdAt time.Time, req *
task := parsed.NewTask(taskID, f.ctx, stor, req.Path, item, nil)
- err = f.registerAndEnqueueTask(task, tasktype.TaskTypeParseditem, req.Storage, req.Path, req.Webhook)
+ err = f.registerAndEnqueueTask(task, tasktype.TaskTypeParseditem, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeParseditem,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeParseditem,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -219,23 +259,28 @@ func (f *TaskFactory) createTGFilesTask(taskID string, createdAt time.Time, req
return nil, fmt.Errorf("invalid params: %w", err)
}
- if len(params.MessageLinks) == 0 {
- return nil, fmt.Errorf("no message links provided")
+ if bot.ExtContext() == nil && userclient.GetCtx() == nil {
+ return nil, ErrClientUnavailable
}
- files, err := ExtractFilesFromLinks(f.ctx, params.MessageLinks)
+ var files []tfilepkg.TGFileMessage
+ var err error
+ if len(params.MessageLinks) > 0 {
+ files, err = ExtractFilesFromLinks(f.ctx, params.MessageLinks)
+ } else {
+ files, err = ExtractFilesFromRange(f.ctx, params.ChatID, params.MessageRange, params.Filter)
+ }
if err != nil {
return nil, fmt.Errorf("failed to extract files: %w", err)
}
-
if len(files) == 0 {
- return nil, fmt.Errorf("no files found in provided links")
+ return nil, fmt.Errorf("no files found")
}
var task core.Executable
if len(files) == 1 {
- tfileTask, err := tfile.NewTGFileTask(taskID, f.ctx, files[0], stor, req.Path, nil)
+ tfileTask, err := tfile.NewTGFileTask(taskID, f.ctx, files[0], stor, path.Join(req.Path, files[0].Name()), nil)
if err != nil {
return nil, fmt.Errorf("failed to create tfile task: %w", err)
}
@@ -243,7 +288,7 @@ func (f *TaskFactory) createTGFilesTask(taskID string, createdAt time.Time, req
} else {
elems := make([]batchtfile.TaskElement, 0, len(files))
for _, file := range files {
- elem, err := batchtfile.NewTaskElement(stor, req.Path, file)
+ elem, err := batchtfile.NewTaskElement(stor, path.Join(req.Path, file.Name()), file)
if err != nil {
return nil, fmt.Errorf("failed to create task element: %w", err)
}
@@ -253,16 +298,18 @@ func (f *TaskFactory) createTGFilesTask(taskID string, createdAt time.Time, req
task = batchtfile.NewBatchTGFileTask(taskID, f.ctx, elems, nil, true)
}
- err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTgfiles, req.Storage, req.Path, req.Webhook)
+ err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTgfiles, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeTgfiles,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeTgfiles,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -288,16 +335,18 @@ func (f *TaskFactory) createTPHPicsTask(taskID string, createdAt time.Time, req
client := telegraph.NewClient()
task := tphtask.NewTask(taskID, f.ctx, phPath, pics, stor, req.Path, client, nil)
- err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTphpics, req.Storage, req.Path, req.Webhook)
+ err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTphpics, stor.Name(), req.Path, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeTphpics,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeTphpics,
+ Status: TaskStatusQueued,
+ StorageID: storage.ID(stor),
+ StorageName: stor.Name(),
+ CreatedAt: createdAt,
}, nil
}
@@ -307,24 +356,30 @@ func (f *TaskFactory) createTransferTask(taskID string, createdAt time.Time, req
return nil, fmt.Errorf("invalid params: %w", err)
}
- sourceStor, ok := storage.GetStorage(params.SourceStorage)
- if !ok {
- return nil, fmt.Errorf("source storage not found: %s", params.SourceStorage)
+ if params.SourceStorageID == 0 || params.TargetStorageID == 0 {
+ return nil, errors.New("source_storage_id and target_storage_id are required")
+ }
+ sourceStor, err := storage.GetStorageByID(f.ctx, params.SourceStorageID)
+ if err != nil {
+ return nil, fmt.Errorf("source_storage_id %d not found: %w", params.SourceStorageID, err)
}
- targetStor, ok := storage.GetStorage(params.TargetStorage)
- if !ok {
- return nil, fmt.Errorf("target storage not found: %s", params.TargetStorage)
+ targetStor, err := storage.GetStorageByID(f.ctx, params.TargetStorageID)
+ if err != nil {
+ return nil, fmt.Errorf("target_storage_id %d not found: %w", params.TargetStorageID, err)
+ }
+ if targetStor.Type() == storenum.Telegram && bot.ExtContext() == nil && userclient.GetCtx() == nil {
+ return nil, ErrClientUnavailable
}
sourceReadable, ok := sourceStor.(storage.StorageReadable)
if !ok {
- return nil, fmt.Errorf("source storage does not support reading: %s", params.SourceStorage)
+ return nil, fmt.Errorf("source storage does not support reading: %s", sourceStor.Name())
}
sourceListable, ok := sourceStor.(storage.StorageListable)
if !ok {
- return nil, fmt.Errorf("source storage does not support listing: %s", params.SourceStorage)
+ return nil, fmt.Errorf("source storage does not support listing: %s", sourceStor.Name())
}
files, err := sourceListable.ListFiles(f.ctx, params.SourcePath)
@@ -344,15 +399,17 @@ func (f *TaskFactory) createTransferTask(taskID string, createdAt time.Time, req
task := transfer.NewTransferTask(taskID, f.ctx, elems, nil, true)
- err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTransfer, params.TargetStorage, params.TargetPath, req.Webhook)
+ err = f.registerAndEnqueueTask(task, tasktype.TaskTypeTransfer, targetStor.Name(), params.TargetPath, req.Webhook)
if err != nil {
return nil, err
}
return &CreateTaskResponse{
- TaskID: taskID,
- Type: tasktype.TaskTypeTransfer,
- Status: TaskStatusQueued,
- CreatedAt: createdAt,
+ TaskID: taskID,
+ Type: tasktype.TaskTypeTransfer,
+ Status: TaskStatusQueued,
+ StorageID: params.TargetStorageID,
+ StorageName: targetStor.Name(),
+ CreatedAt: createdAt,
}, nil
}
diff --git a/api/filesystem.go b/api/filesystem.go
new file mode 100644
index 00000000..94826ae7
--- /dev/null
+++ b/api/filesystem.go
@@ -0,0 +1,141 @@
+package api
+
+import (
+ "net/http"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+)
+
+const pathListLimit = 500
+
+type filesystemEntry struct {
+ Name string `json:"name"`
+ Path string `json:"path"`
+ IsDir bool `json:"is_dir"`
+ Size int64 `json:"size,omitempty"`
+}
+
+type filesystemListing struct {
+ Path string `json:"path"`
+ Parent string `json:"parent,omitempty"`
+ Root string `json:"root"`
+ Cwd string `json:"cwd"`
+ Selected string `json:"selected,omitempty"`
+ Entries []filesystemEntry `json:"entries"`
+ Truncated bool `json:"truncated,omitempty"`
+}
+
+// listFilesystem lists one directory on the deployment host for the server-side
+// path picker. It returns names and metadata only, never file contents.
+func (s *server) listFilesystem(w http.ResponseWriter, r *http.Request) {
+ requested := strings.TrimSpace(r.URL.Query().Get("path"))
+ if requested == "" {
+ cwd, err := os.Getwd()
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ requested = cwd
+ }
+ absolute, err := filepath.Abs(requested)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_path", err.Error())
+ return
+ }
+ dir := absolute
+ selected := ""
+ info, err := os.Stat(absolute)
+ switch {
+ case err == nil:
+ if !info.IsDir() {
+ selected = absolute
+ dir = filepath.Dir(absolute)
+ }
+ case os.IsNotExist(err):
+ // A target file or directory may not exist yet. Start the picker at the
+ // nearest existing ancestor instead of failing outright.
+ for {
+ parent := filepath.Dir(dir)
+ if parent == dir {
+ break
+ }
+ dir = parent
+ if parentInfo, statErr := os.Stat(dir); statErr == nil && parentInfo.IsDir() {
+ break
+ }
+ }
+ if _, statErr := os.Stat(dir); statErr != nil {
+ WriteError(w, http.StatusNotFound, "path_not_found", statErr.Error())
+ return
+ }
+ case os.IsPermission(err):
+ WriteError(w, http.StatusForbidden, "path_forbidden", err.Error())
+ return
+ default:
+ WriteError(w, http.StatusBadRequest, "path_unavailable", err.Error())
+ return
+ }
+
+ dirEntries, err := os.ReadDir(dir)
+ if err != nil {
+ status := http.StatusBadRequest
+ code := "path_unavailable"
+ if os.IsPermission(err) {
+ status = http.StatusForbidden
+ code = "path_forbidden"
+ }
+ WriteError(w, status, code, err.Error())
+ return
+ }
+
+ entries := make([]filesystemEntry, 0, len(dirEntries))
+ for _, entry := range dirEntries {
+ entryInfo, err := entry.Info()
+ if err != nil {
+ continue
+ }
+ entries = append(entries, filesystemEntry{
+ Name: entry.Name(),
+ Path: filepath.Join(dir, entry.Name()),
+ IsDir: entryInfo.IsDir(),
+ Size: entryInfo.Size(),
+ })
+ }
+ sort.Slice(entries, func(i, j int) bool {
+ if entries[i].IsDir != entries[j].IsDir {
+ return entries[i].IsDir
+ }
+ return strings.ToLower(entries[i].Name) < strings.ToLower(entries[j].Name)
+ })
+ truncated := false
+ if len(entries) > pathListLimit {
+ entries = entries[:pathListLimit]
+ truncated = true
+ }
+
+ parent := filepath.Dir(dir)
+ if parent == dir {
+ parent = ""
+ }
+ root := string(filepath.Separator)
+ if volume := filepath.VolumeName(dir); volume != "" {
+ root = volume + string(filepath.Separator)
+ }
+ cwd, err := os.Getwd()
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ w.Header().Set("Cache-Control", "no-store")
+ WriteJSON(w, http.StatusOK, filesystemListing{
+ Path: dir,
+ Parent: parent,
+ Root: root,
+ Cwd: cwd,
+ Selected: selected,
+ Entries: entries,
+ Truncated: truncated,
+ })
+}
diff --git a/api/handlers.go b/api/handlers.go
deleted file mode 100644
index 350196f7..00000000
--- a/api/handlers.go
+++ /dev/null
@@ -1,270 +0,0 @@
-package api
-
-import (
- "context"
- "encoding/json"
- "net/http"
- "sort"
- "strings"
- "time"
-
- "github.com/krau/SaveAny-Bot/core"
- "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
- "github.com/krau/SaveAny-Bot/storage"
-)
-
-type mediaMetadataExtractor func(ctx context.Context, url string) (*MediaMetadataResponse, error)
-
-type Handlers struct {
- factory *TaskFactory
- mediaMetadataExtractor mediaMetadataExtractor
-}
-
-func NewHandlers(factory *TaskFactory) *Handlers {
- return &Handlers{
- factory: factory,
- mediaMetadataExtractor: extractMediaMetadata,
- }
-}
-
-func (h *Handlers) CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only POST method is allowed")
- return
- }
-
- var req CreateTaskRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- WriteError(w, http.StatusBadRequest, "invalid_request", "failed to decode request body: "+err.Error())
- return
- }
-
- if req.Type == "" {
- WriteError(w, http.StatusBadRequest, "invalid_request", "task type is required")
- return
- }
-
- if req.Storage == "" {
- WriteError(w, http.StatusBadRequest, "invalid_request", "storage is required")
- return
- }
-
- resp, err := h.factory.CreateTask(&req)
- if err != nil {
- WriteError(w, http.StatusBadRequest, "task_creation_failed", err.Error())
- return
- }
-
- WriteJSON(w, http.StatusCreated, resp)
-}
-
-func (h *Handlers) ListTasksHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only GET method is allowed")
- return
- }
-
- tasks := GetAllTasks()
- response := make([]TaskInfoResponse, 0, len(tasks))
-
- for _, task := range tasks {
- info := convertTaskProgressToResponse(task)
- response = append(response, info)
- }
-
- // GetAllTasks walks a map, so the order is otherwise nondeterministic.
- // Newest first, breaking ties on task ID to keep the order stable across
- // calls when two tasks share a creation timestamp.
- sort.Slice(response, func(i, j int) bool {
- if !response[i].CreatedAt.Equal(response[j].CreatedAt) {
- return response[i].CreatedAt.After(response[j].CreatedAt)
- }
- return response[i].TaskID > response[j].TaskID
- })
-
- WriteJSON(w, http.StatusOK, TasksListResponse{
- Tasks: response,
- Total: len(response),
- })
-}
-
-func (h *Handlers) GetTaskHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only GET method is allowed")
- return
- }
-
- taskID := extractTaskIDFromPath(r.URL.Path)
- if taskID == "" {
- WriteError(w, http.StatusBadRequest, "invalid_request", "task ID is required")
- return
- }
-
- task, ok := GetTask(taskID)
- if !ok {
- WriteError(w, http.StatusNotFound, "task_not_found", "task not found: "+taskID)
- return
- }
-
- resp := convertTaskProgressToResponse(task)
- WriteJSON(w, http.StatusOK, resp)
-}
-
-func (h *Handlers) CancelTaskHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodDelete {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only DELETE method is allowed")
- return
- }
-
- taskID := extractTaskIDFromPath(r.URL.Path)
- if taskID == "" {
- WriteError(w, http.StatusBadRequest, "invalid_request", "task ID is required")
- return
- }
-
- task, ok := GetTask(taskID)
- if !ok {
- WriteError(w, http.StatusNotFound, "task_not_found", "task not found: "+taskID)
- return
- }
-
- // Cancel the task; the terminal status is set via the task event stream.
- if err := core.CancelTask(r.Context(), taskID); err != nil {
- WriteError(w, http.StatusInternalServerError, "cancel_failed", "failed to cancel task: "+err.Error())
- return
- }
-
- task.UpdateStatus(TaskStatusCancelled)
- WriteJSON(w, http.StatusOK, map[string]string{"message": "task cancelled successfully"})
-}
-
-func (h *Handlers) ListStoragesHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only GET method is allowed")
- return
- }
-
- all := storage.AllStorages()
- storages := make([]StorageInfo, 0, len(all))
- for name, stor := range all {
- storages = append(storages, StorageInfo{
- Name: name,
- Type: string(stor.Type()),
- })
- }
-
- WriteJSON(w, http.StatusOK, StoragesResponse{Storages: storages})
-}
-
-func (h *Handlers) GetMediaMetadataHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only GET method is allowed")
- return
- }
-
- mediaURL := strings.TrimSpace(r.URL.Query().Get("url"))
- if mediaURL == "" {
- WriteError(w, http.StatusBadRequest, "invalid_request", "url query parameter is required")
- return
- }
-
- ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
- defer cancel()
-
- resp, err := h.mediaMetadataExtractor(ctx, mediaURL)
- if err != nil {
- WriteError(w, http.StatusBadRequest, "metadata_extraction_failed", err.Error())
- return
- }
-
- WriteJSON(w, http.StatusOK, resp)
-}
-
-func (h *Handlers) GetTaskTypesHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodGet {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "only GET method is allowed")
- return
- }
-
- types := []tasktype.TaskType{
- tasktype.TaskTypeDirectlinks,
- tasktype.TaskTypeYtdlp,
- tasktype.TaskTypeAria2,
- tasktype.TaskTypeParseditem,
- tasktype.TaskTypeTgfiles,
- tasktype.TaskTypeTphpics,
- tasktype.TaskTypeTransfer,
- }
-
- WriteJSON(w, http.StatusOK, map[string]any{
- "types": types,
- })
-}
-
-func (h *Handlers) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
- WriteJSON(w, http.StatusOK, map[string]string{
- "status": "ok",
- })
-}
-
-// 路径格式: /api/v1/tasks/:id
-func extractTaskIDFromPath(path string) string {
- parts := strings.Split(strings.Trim(path, "/"), "/")
- if len(parts) < 4 {
- return ""
- }
- return parts[3]
-}
-
-// convertTaskProgressToResponse renders a task's current state, computing
-// percent and speed from the snapshot taken under the task's mutex.
-func convertTaskProgressToResponse(task *TaskProgressInfo) TaskInfoResponse {
- status, total, downloaded, totalFiles, downloadedFiles, startedAt, errMsg, updatedAt := task.snapshot()
-
- resp := TaskInfoResponse{
- TaskID: task.TaskID,
- Type: tasktype.TaskType(task.Type),
- Status: status,
- Title: task.Title,
- Storage: task.Storage,
- Path: task.Path,
- Error: errMsg,
- CreatedAt: task.CreatedAt,
- UpdatedAt: updatedAt,
- }
-
- var percent float64
- var speedMBPS float64
- if total > 0 {
- percent = float64(downloaded) * 100 / float64(total)
- } else if totalFiles > 0 {
- percent = float64(downloadedFiles) * 100 / float64(totalFiles)
- }
- if !startedAt.IsZero() {
- elapsed := time.Since(startedAt).Seconds()
- if elapsed > 0 {
- speedMBPS = float64(downloaded) / elapsed / (1024 * 1024)
- }
- }
-
- if total > 0 || totalFiles > 0 {
- resp.Progress = &TaskProgress{
- TotalBytes: total,
- DownloadedBytes: downloaded,
- TotalFiles: totalFiles,
- DownloadedFiles: downloadedFiles,
- Percent: percent,
- SpeedMBPS: speedMBPS,
- }
- }
-
- return resp
-}
-
-func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
- WriteError(w, http.StatusNotFound, "not_found", "endpoint not found: "+r.URL.Path)
-}
-
-func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
- WriteError(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed: "+r.Method)
-}
diff --git a/api/meta.go b/api/meta.go
new file mode 100644
index 00000000..8b1a5390
--- /dev/null
+++ b/api/meta.go
@@ -0,0 +1,66 @@
+package api
+
+import (
+ "net/http"
+
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/fnamest"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/rule"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
+)
+
+type metaResponse struct {
+ APIVersion string `json:"api_version"`
+ Version string `json:"version"`
+ SchemaVersion int `json:"schema_version"`
+ ChatIDFormat string `json:"chat_id_format"`
+ TaskTypes []string `json:"task_types"`
+ TaskStatuses []string `json:"task_statuses"`
+ TaskItemStatuses []string `json:"task_item_statuses"`
+ StorageTypes []string `json:"storage_types"`
+ RuleTypes []string `json:"rule_types"`
+ FilenameStrategies []string `json:"filename_strategies"`
+ ConflictStrategies []string `json:"conflict_strategies"`
+}
+
+func (s *server) meta(w http.ResponseWriter, _ *http.Request) {
+ taskTypes := tasktype.TaskTypeValues()
+ taskTypeNames := make([]string, 0, len(taskTypes))
+ for _, value := range taskTypes {
+ taskTypeNames = append(taskTypeNames, value.String())
+ }
+ ruleTypes := rule.Values()
+ ruleTypeNames := make([]string, 0, len(ruleTypes))
+ for _, value := range ruleTypes {
+ ruleTypeNames = append(ruleTypeNames, value.String())
+ }
+ WriteJSON(w, http.StatusOK, metaResponse{
+ APIVersion: "v1",
+ Version: config.Version,
+ SchemaVersion: database.CurrentSchemaVersion,
+ ChatIDFormat: "bot_api",
+ TaskTypes: taskTypeNames,
+ TaskStatuses: []string{
+ string(TaskStatusQueued),
+ string(TaskStatusRunning),
+ string(TaskStatusCompleted),
+ string(TaskStatusPartial),
+ string(TaskStatusFailed),
+ string(TaskStatusCancelled),
+ },
+ TaskItemStatuses: []string{
+ string(core.ItemStatusCompleted),
+ string(core.ItemStatusFailed),
+ string(core.ItemStatusSkipped),
+ string(core.ItemStatusCancelled),
+ },
+ StorageTypes: storenum.StorageTypeNames(),
+ RuleTypes: ruleTypeNames,
+ FilenameStrategies: fnamest.FnameSTNames(),
+ ConflictStrategies: tcbdata.ConflictStrategyValues(),
+ })
+}
diff --git a/api/onboarding.go b/api/onboarding.go
new file mode 100644
index 00000000..71d3c281
--- /dev/null
+++ b/api/onboarding.go
@@ -0,0 +1,93 @@
+package api
+
+import (
+ "net/http"
+ "strings"
+
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/settings"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+type OnboardingSteps struct {
+ BotConfigured bool `json:"bot_configured"`
+ StorageReady bool `json:"storage_ready"`
+ UserReady bool `json:"user_ready"`
+ BotRunning bool `json:"bot_running"`
+ FirstTask bool `json:"first_task"`
+}
+
+type OnboardingLegacy struct {
+ Imported bool `json:"imported"`
+ ImportedAt string `json:"imported_at,omitempty"`
+}
+
+type OnboardingStatus struct {
+ Complete bool `json:"complete"`
+ Steps OnboardingSteps `json:"steps"`
+ BotState string `json:"bot_state"`
+ Legacy OnboardingLegacy `json:"legacy"`
+}
+
+// onboarding reports the minimum configuration needed before the bot can
+// accept a task. It intentionally exposes no credentials.
+func (s *server) onboarding(w http.ResponseWriter, r *http.Request) {
+ cfg := s.deps.Settings.Config()
+ records, err := database.ListStorageRecords(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ users, err := database.GetAllUsers(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ _, taskTotal, err := database.ListTaskRecords(r.Context(), database.TaskFilter{Limit: 1})
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+
+ storageReady := false
+ initErrors := storage.LastErrors()
+ for _, record := range records {
+ if !record.Enable {
+ continue
+ }
+ if message, failed := initErrors[record.Name]; failed && message != "" {
+ continue
+ }
+ storageReady = true
+ break
+ }
+ userReady := false
+ for _, user := range users {
+ if user.Enabled {
+ userReady = true
+ break
+ }
+ }
+
+ botToken := strings.TrimSpace(cfg.Telegram.Token) != ""
+ botStatus := s.deps.Telegram.Status(r.Context())
+ importedAt, imported := settings.LegacyImportTime(r.Context())
+ legacy := OnboardingLegacy{Imported: imported}
+ if !importedAt.IsZero() {
+ legacy.ImportedAt = importedAt.UTC().Format("2006-01-02T15:04:05Z07:00")
+ }
+
+ status := OnboardingStatus{
+ Complete: botToken && storageReady && userReady,
+ Steps: OnboardingSteps{
+ BotConfigured: botToken,
+ StorageReady: storageReady,
+ UserReady: userReady,
+ BotRunning: botStatus.Bot.State == "running",
+ FirstTask: taskTotal > 0,
+ },
+ BotState: botStatus.Bot.State,
+ Legacy: legacy,
+ }
+ WriteJSON(w, http.StatusOK, status)
+}
diff --git a/api/parsers.go b/api/parsers.go
new file mode 100644
index 00000000..6719d2b1
--- /dev/null
+++ b/api/parsers.go
@@ -0,0 +1,96 @@
+package api
+
+import (
+ "encoding/json"
+ "errors"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/parsers"
+)
+
+type parserFileResponse struct {
+ Name string `json:"name"`
+ Directory string `json:"directory"`
+ Size int64 `json:"size"`
+ Modified time.Time `json:"modified"`
+}
+
+type parserListResponse struct {
+ Enabled bool `json:"enabled"`
+ Dirs []string `json:"dirs"`
+ Files []parserFileResponse `json:"files"`
+}
+
+func parserDirs() []string {
+ cfg := config.C()
+ if len(cfg.Parser.PluginDirs) > 0 {
+ return cfg.Parser.PluginDirs
+ }
+ return []string{"plugins"}
+}
+
+func (s *server) listParsers(w http.ResponseWriter, r *http.Request) {
+ cfg := config.C()
+ dirs := parserDirs()
+ files := make([]parserFileResponse, 0)
+ for _, dir := range dirs {
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ continue
+ }
+ WriteError(w, http.StatusInternalServerError, "read_failed", err.Error())
+ return
+ }
+ for _, entry := range entries {
+ if entry.IsDir() || !strings.HasSuffix(strings.ToLower(entry.Name()), ".js") {
+ continue
+ }
+ info, err := entry.Info()
+ if err != nil {
+ continue
+ }
+ files = append(files, parserFileResponse{
+ Name: entry.Name(),
+ Directory: dir,
+ Size: info.Size(),
+ Modified: info.ModTime(),
+ })
+ }
+ }
+ WriteJSON(w, http.StatusOK, parserListResponse{Enabled: cfg.Parser.PluginEnable, Dirs: dirs, Files: files})
+}
+
+func (s *server) createParser(w http.ResponseWriter, r *http.Request) {
+ if !config.C().Parser.PluginEnable {
+ WriteError(w, http.StatusConflict, "parser_disabled", "parser plugins are disabled")
+ return
+ }
+ var body struct {
+ Filename string `json:"filename"`
+ Content string `json:"content"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 10<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ filename := filepath.Base(strings.TrimSpace(body.Filename))
+ if filename == "" || filename == "." || !strings.HasSuffix(strings.ToLower(filename), ".js") {
+ WriteFieldErrors(w, http.StatusBadRequest, "filename must end with .js", map[string]string{"filename": "invalid filename"})
+ return
+ }
+ if body.Content == "" {
+ WriteFieldErrors(w, http.StatusBadRequest, "content is required", map[string]string{"content": "required"})
+ return
+ }
+ if err := parsers.AddPlugin(r.Context(), body.Content, filename); err != nil {
+ WriteError(w, http.StatusBadRequest, "install_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusCreated, map[string]any{"name": filename})
+}
diff --git a/api/progress.go b/api/progress.go
index f168f840..b6f55c3d 100644
--- a/api/progress.go
+++ b/api/progress.go
@@ -2,15 +2,18 @@ package api
import (
"context"
+
+ "github.com/krau/SaveAny-Bot/config"
"sync"
"time"
+ "github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/pkg/taskevent"
)
-// TaskProgressInfo stores the progress of an API-submitted task. All fields are
-// guarded by mu. It implements taskevent.Sink so the task layer can update it
-// without knowing about the API.
+// TaskProgressInfo stores live progress for a task. All fields are guarded by
+// mu. It implements taskevent.Sink so the task layer can update it without
+// knowing about the API.
type TaskProgressInfo struct {
mu sync.Mutex
TaskID string
@@ -21,6 +24,7 @@ type TaskProgressInfo struct {
DownloadedBytes int64
TotalFiles int
DownloadedFiles int
+ StorageID uint
Storage string
Path string
Error string
@@ -31,8 +35,8 @@ type TaskProgressInfo struct {
webhookNotified bool
}
-// progressStore holds all API tasks. Entries are removed a fixed duration after
-// they reach a terminal state to bound memory usage.
+// progressStore holds all tracked tasks. Entries are removed a fixed duration
+// after they reach a terminal state to bound memory usage.
type progressStore struct {
mu sync.RWMutex
tasks map[string]*TaskProgressInfo
@@ -44,12 +48,26 @@ var store = &progressStore{
retention: 24 * time.Hour,
}
+// TaskProgressSink creates the in-memory observer used by tasks started
+// outside the API. Their progress is rendered in the task list but no webhook
+// is attached.
+func TaskProgressSink(task core.Executable) taskevent.Sink {
+ storageName := ""
+ storagePath := ""
+ if destination, ok := task.(core.DestinationInfo); ok {
+ storageName = destination.StorageName()
+ storagePath = destination.StoragePath()
+ }
+ return RegisterTask(task.TaskID(), task.Type().String(), storageName, storagePath, task.Title(), "")
+}
+
func RegisterTask(taskID, taskType, storage, path, title, webhook string) *TaskProgressInfo {
info := &TaskProgressInfo{
TaskID: taskID,
Type: taskType,
Status: TaskStatusQueued,
Title: title,
+ StorageID: config.C().GetStorageIDByName(storage),
Storage: storage,
Path: path,
CreatedAt: time.Now(),
@@ -96,7 +114,7 @@ func CleanupExpired() {
defer store.mu.Unlock()
for id, info := range store.tasks {
info.mu.Lock()
- terminal := info.Status == TaskStatusCompleted || info.Status == TaskStatusFailed || info.Status == TaskStatusCancelled
+ terminal := info.Status.IsTerminal()
stale := terminal && now.Sub(info.UpdatedAt) > store.retention
info.mu.Unlock()
if stale {
@@ -153,6 +171,10 @@ func (t *TaskProgressInfo) snapshot() (status TaskStatus, total, downloaded int6
// status/progress updates and fires the webhook on terminal transitions.
func (t *TaskProgressInfo) Emit(e taskevent.Event) {
t.mu.Lock()
+ if t.Status.IsTerminal() {
+ t.mu.Unlock()
+ return
+ }
switch e.Phase {
case taskevent.PhaseStart:
t.Status = TaskStatusRunning
@@ -175,22 +197,30 @@ func (t *TaskProgressInfo) Emit(e taskevent.Event) {
t.DownloadedFiles = e.DownloadedFiles
}
case taskevent.PhaseDone:
- if e.Err != nil {
- t.Status = TaskStatusFailed
- t.Error = e.Err.Error()
- } else {
- t.Status = TaskStatusCompleted
+ if t.Status != TaskStatusCancelled {
+ switch {
+ case e.Cancelled:
+ t.Status = TaskStatusCancelled
+ case e.Err != nil:
+ t.Status = TaskStatusFailed
+ t.Error = e.Err.Error()
+ case e.Partial:
+ t.Status = TaskStatusPartial
+ default:
+ t.Status = TaskStatusCompleted
+ }
}
}
t.UpdatedAt = time.Now()
- notify := t.Webhook != "" && !t.webhookNotified && (t.Status == TaskStatusCompleted || t.Status == TaskStatusFailed)
+ notify := t.Webhook != "" && !t.webhookNotified && t.Status.IsTerminal() && t.Status != TaskStatusCancelled
+ status := t.Status
if notify {
t.webhookNotified = true
}
t.mu.Unlock()
if notify {
- payload := CreateWebhookPayload(t.TaskID, t.Type, t.Status, t.Storage, t.Path, e.Err)
+ payload := CreateWebhookPayload(t.TaskID, t.Type, status, t.StorageID, t.Storage, t.Path, e.Err)
SendWebhook(context.Background(), payload)
}
}
diff --git a/api/router.go b/api/router.go
new file mode 100644
index 00000000..4480766c
--- /dev/null
+++ b/api/router.go
@@ -0,0 +1,148 @@
+package api
+
+import (
+ "net/http"
+ "time"
+
+ "github.com/charmbracelet/log"
+
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
+)
+
+type server struct {
+ deps Deps
+ factory *TaskFactory
+ mediaMetadataExtractor mediaMetadataExtractor
+ logins *loginLimiter
+ startedAt time.Time
+}
+
+// NewHandler builds the redesigned JSON API mounted under /api/v1.
+func NewHandler(deps Deps) http.Handler {
+ s := &server{
+ deps: deps,
+ factory: NewTaskFactory(deps.Ctx),
+ mediaMetadataExtractor: extractMediaMetadata,
+ logins: newLoginLimiter(),
+ startedAt: time.Now(),
+ }
+ go s.sessionCleanup(deps.Ctx)
+ StartCleanupLoop(deps.Ctx)
+ core.SetTaskSinkFactory(TaskProgressSink)
+
+ mux := http.NewServeMux()
+ mux.HandleFunc("GET /health", s.health)
+ mux.HandleFunc("POST /api/v1/auth/login", s.login)
+ mux.HandleFunc("GET /api/v1/auth/token", s.tokenLogin)
+ mux.HandleFunc("POST /api/v1/auth/logout", s.logout)
+ mux.HandleFunc("GET /api/v1/auth/me", s.me)
+ mux.HandleFunc("POST /api/v1/auth/password", s.changePassword)
+ mux.HandleFunc("GET /api/v1/onboarding", s.onboarding)
+ mux.HandleFunc("GET /api/v1/meta", s.meta)
+
+ mux.HandleFunc("GET /api/v1/settings", s.getSettings)
+ mux.HandleFunc("PUT /api/v1/settings/{group}", s.updateSettings)
+
+ mux.HandleFunc("GET /api/v1/storages", s.listStorages)
+ mux.HandleFunc("POST /api/v1/storages", s.createStorage)
+ mux.HandleFunc("PUT /api/v1/storages/{storage_id}", s.updateStorage)
+ mux.HandleFunc("DELETE /api/v1/storages/{storage_id}", s.deleteStorage)
+ mux.HandleFunc("POST /api/v1/storages/{storage_id}/test", s.testStorage)
+
+ mux.HandleFunc("GET /api/v1/users", s.listUsers)
+ mux.HandleFunc("POST /api/v1/users", s.createUser)
+ mux.HandleFunc("PUT /api/v1/users/{id}", s.updateUser)
+ mux.HandleFunc("DELETE /api/v1/users/{id}", s.deleteUser)
+ mux.HandleFunc("GET /api/v1/users/{id}/dirs", s.listUserDirs)
+ mux.HandleFunc("POST /api/v1/users/{id}/dirs", s.createUserDir)
+ mux.HandleFunc("DELETE /api/v1/users/{id}/dirs/{dirID}", s.deleteUserDir)
+ mux.HandleFunc("GET /api/v1/users/{id}/rules", s.listUserRules)
+ mux.HandleFunc("POST /api/v1/users/{id}/rules", s.createUserRule)
+ mux.HandleFunc("POST /api/v1/users/{id}/rules/presets", s.importUserRulePresets)
+ mux.HandleFunc("DELETE /api/v1/users/{id}/rules/{ruleID}", s.deleteUserRule)
+ mux.HandleFunc("GET /api/v1/users/{id}/watches", s.listUserWatches)
+ mux.HandleFunc("POST /api/v1/users/{id}/watches", s.createUserWatch)
+ mux.HandleFunc("DELETE /api/v1/users/{id}/watches/{chatID}", s.deleteUserWatch)
+
+ mux.HandleFunc("GET /api/v1/telegram/status", s.telegramStatus)
+ mux.HandleFunc("GET /api/v1/telegram/workers", s.listBotWorkers)
+ mux.HandleFunc("POST /api/v1/telegram/workers", s.createBotWorker)
+ mux.HandleFunc("PUT /api/v1/telegram/workers/{id}", s.updateBotWorker)
+ mux.HandleFunc("DELETE /api/v1/telegram/workers/{id}", s.deleteBotWorker)
+ mux.HandleFunc("POST /api/v1/telegram/workers/{id}/start", s.startBotWorker)
+ mux.HandleFunc("POST /api/v1/telegram/workers/{id}/stop", s.stopBotWorker)
+ mux.HandleFunc("POST /api/v1/telegram/workers/{id}/test", s.testBotWorker)
+ mux.HandleFunc("POST /api/v1/telegram/bot/start", s.startBot)
+ mux.HandleFunc("POST /api/v1/telegram/bot/stop", s.stopBot)
+ mux.HandleFunc("POST /api/v1/telegram/bot/restart", s.restartBot)
+ mux.HandleFunc("POST /api/v1/telegram/userbot/login", s.startUserbotLogin)
+ mux.HandleFunc("POST /api/v1/telegram/userbot/login/{flow}/code", s.submitUserbotCode)
+ mux.HandleFunc("POST /api/v1/telegram/userbot/login/{flow}/password", s.submitUserbotPassword)
+ mux.HandleFunc("DELETE /api/v1/telegram/userbot/login/{flow}", s.cancelUserbotLogin)
+ mux.HandleFunc("POST /api/v1/telegram/userbot/logout", s.logoutUserbot)
+ mux.HandleFunc("POST /api/v1/telegram/userbot/sync-peers", s.syncUserbotPeers)
+
+ mux.HandleFunc("GET /api/v1/tasks", s.listTasks)
+ mux.HandleFunc("POST /api/v1/tasks", s.createTask)
+ mux.HandleFunc("GET /api/v1/tasks/{id}", s.getTask)
+ mux.HandleFunc("GET /api/v1/tasks/{id}/metadata", s.getTaskMetadata)
+ mux.HandleFunc("POST /api/v1/tasks/{id}/cancel", s.cancelTask)
+ mux.HandleFunc("POST /api/v1/tasks/{id}/retry", s.retryTask)
+ mux.HandleFunc("DELETE /api/v1/tasks/{id}", s.deleteTask)
+ mux.HandleFunc("GET /api/v1/task-types", s.taskTypes)
+ mux.HandleFunc("GET /api/v1/media-metadata", s.mediaMetadata)
+
+ mux.HandleFunc("GET /api/v1/system/info", s.systemInfo)
+ mux.HandleFunc("POST /api/v1/system/restart", s.restartSystem)
+ mux.HandleFunc("GET /api/v1/system/update", s.getUpdateInfo)
+ mux.HandleFunc("POST /api/v1/system/update", s.applyUpdate)
+ mux.HandleFunc("GET /api/v1/parsers", s.listParsers)
+ mux.HandleFunc("POST /api/v1/parsers", s.createParser)
+ mux.HandleFunc("GET /api/v1/filesystem", s.listFilesystem)
+
+ mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ WriteError(w, http.StatusNotFound, "not_found", "endpoint not found: "+r.URL.Path)
+ })
+
+ var handler http.Handler = mux
+ handler = s.authMiddleware(handler)
+ handler = loggingMiddleware(handler)
+ handler = recoveryMiddleware(handler)
+ return handler
+}
+
+func (s *server) health(w http.ResponseWriter, r *http.Request) {
+ WriteJSON(w, http.StatusOK, map[string]string{"status": "ok", "version": config.Version})
+}
+
+func loggingMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ start := time.Now()
+ wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
+ next.ServeHTTP(wrapped, r)
+ log.Infof("%s %s %d %s", r.Method, r.URL.Path, wrapped.statusCode, time.Since(start))
+ })
+}
+
+func recoveryMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ defer func() {
+ if err := recover(); err != nil {
+ log.Errorf("Panic recovered: %v", err)
+ WriteError(w, http.StatusInternalServerError, "internal_error", "internal server error")
+ }
+ }()
+ next.ServeHTTP(w, r)
+ })
+}
+
+type responseWriter struct {
+ http.ResponseWriter
+ statusCode int
+}
+
+func (rw *responseWriter) WriteHeader(code int) {
+ rw.statusCode = code
+ rw.ResponseWriter.WriteHeader(code)
+}
diff --git a/api/server.go b/api/server.go
deleted file mode 100644
index 42c2060d..00000000
--- a/api/server.go
+++ /dev/null
@@ -1,160 +0,0 @@
-package api
-
-import (
- "context"
- "fmt"
- "net"
- "net/http"
- "time"
-
- "github.com/charmbracelet/log"
- "github.com/krau/SaveAny-Bot/config"
-)
-
-type Server struct {
- httpServer *http.Server
- factory *TaskFactory
-}
-
-func NewServer(ctx context.Context) *Server {
- cfg := config.C().API
-
- factory := NewTaskFactory(ctx)
- handlers := NewHandlers(factory)
-
- mux := http.NewServeMux()
-
- mux.HandleFunc("/health", handlers.HealthCheckHandler)
-
- mux.HandleFunc("/api/v1/tasks", func(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case http.MethodGet:
- handlers.ListTasksHandler(w, r)
- case http.MethodPost:
- handlers.CreateTaskHandler(w, r)
- default:
- MethodNotAllowedHandler(w, r)
- }
- })
- mux.HandleFunc("/api/v1/tasks/", func(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case http.MethodGet:
- handlers.GetTaskHandler(w, r)
- case http.MethodDelete:
- handlers.CancelTaskHandler(w, r)
- default:
- MethodNotAllowedHandler(w, r)
- }
- })
- mux.HandleFunc("/api/v1/storages", handlers.ListStoragesHandler)
- mux.HandleFunc("/api/v1/media-metadata", handlers.GetMediaMetadataHandler)
- mux.HandleFunc("/api/v1/task-types", handlers.GetTaskTypesHandler)
-
- mux.HandleFunc("/", NotFoundHandler)
-
- var handler http.Handler = mux
-
- token := cfg.Token
- if token != "" {
- handler = AuthMiddleware()(handler)
- }
-
- handler = loggingMiddleware(handler)
-
- handler = recoveryMiddleware(handler)
-
- return &Server{
- httpServer: &http.Server{
- Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
- Handler: handler,
- ReadTimeout: 30 * time.Second,
- WriteTimeout: 30 * time.Second,
- IdleTimeout: 120 * time.Second,
- },
- factory: factory,
- }
-}
-
-func (s *Server) Start(ctx context.Context) error {
- logger := log.FromContext(ctx).With("module", "api")
-
- logger.Infof("Starting API server on %s", s.httpServer.Addr)
-
- // Bind synchronously so listen failures are returned to the caller.
- ln, err := net.Listen("tcp", s.httpServer.Addr)
- if err != nil {
- return fmt.Errorf("failed to listen on %s: %w", s.httpServer.Addr, err)
- }
-
- go func() {
- if err := s.httpServer.Serve(ln); err != nil && err != http.ErrServerClosed {
- logger.Errorf("API server error: %v", err)
- }
- }()
-
- go func() {
- <-ctx.Done()
- shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if err := s.httpServer.Shutdown(shutdownCtx); err != nil {
- logger.Errorf("API server shutdown error: %v", err)
- }
- }()
-
- return nil
-}
-
-func loggingMiddleware(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- start := time.Now()
-
- wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
-
- next.ServeHTTP(wrapped, r)
-
- log.Infof("%s %s %d %s", r.Method, r.URL.Path, wrapped.statusCode, time.Since(start))
- })
-}
-
-func recoveryMiddleware(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- defer func() {
- if err := recover(); err != nil {
- log.Errorf("Panic recovered: %v", err)
- WriteError(w, http.StatusInternalServerError, "internal_error", "internal server error")
- }
- }()
- next.ServeHTTP(w, r)
- })
-}
-
-type responseWriter struct {
- http.ResponseWriter
- statusCode int
-}
-
-func (rw *responseWriter) WriteHeader(code int) {
- rw.statusCode = code
- rw.ResponseWriter.WriteHeader(code)
-}
-
-// Start initializes and starts the API server. It refuses to start without a
-// token, since an open download proxy is a security risk.
-func Start(ctx context.Context) error {
- cfg := config.C().API
-
- if !cfg.Enable {
- return nil
- }
-
- if cfg.Token == "" {
- return fmt.Errorf("API server is enabled but no token is set; refusing to start insecurely")
- }
-
- server := NewServer(ctx)
- if err := server.Start(ctx); err != nil {
- return err
- }
- StartCleanupLoop(ctx)
- return nil
-}
diff --git a/api/settings.go b/api/settings.go
new file mode 100644
index 00000000..b3f8eb36
--- /dev/null
+++ b/api/settings.go
@@ -0,0 +1,39 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+)
+
+func (s *server) getSettings(w http.ResponseWriter, r *http.Request) {
+ values, err := s.deps.Settings.PublicSettings()
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, values)
+}
+
+func (s *server) updateSettings(w http.ResponseWriter, r *http.Request) {
+ group := r.PathValue("group")
+ var patch json.RawMessage
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&patch); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ restart, err := s.deps.Settings.UpdateGroup(r.Context(), group, patch)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "settings_update_failed", err.Error())
+ return
+ }
+ warnings := make([]string, 0)
+ if group == "telegram" {
+ if err := s.deps.Telegram.ApplyTelegram(r.Context()); err != nil {
+ warnings = append(warnings, err.Error())
+ }
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{
+ "restart_required": restart,
+ "warnings": warnings,
+ })
+}
diff --git a/api/storages.go b/api/storages.go
new file mode 100644
index 00000000..3cfc78ce
--- /dev/null
+++ b/api/storages.go
@@ -0,0 +1,142 @@
+package api
+
+import (
+ "encoding/json"
+ "errors"
+ "net/http"
+ "strconv"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/settings"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var storageSecretKeys = map[string]struct{}{
+ "password": {}, "token": {}, "secret": {}, "access_key_id": {}, "secret_access_key": {},
+}
+
+func maskStorageConfig(config map[string]any) map[string]any {
+ out := make(map[string]any, len(config))
+ for key, value := range config {
+ if _, secret := storageSecretKeys[key]; secret {
+ if s, ok := value.(string); ok && s != "" {
+ out[key] = "__MASKED__"
+ continue
+ }
+ }
+ out[key] = value
+ }
+ return out
+}
+
+func storageResponse(record *database.StorageRecord, initErr string) StorageRecord {
+ var config map[string]any
+ if record.ConfigJSON != "" {
+ if err := json.Unmarshal([]byte(record.ConfigJSON), &config); err != nil {
+ log.Warnf("Invalid storage config for %s: %v", record.Name, err)
+ }
+ }
+ if config == nil {
+ config = make(map[string]any)
+ }
+ return StorageRecord{
+ StorageID: record.ID,
+ Name: record.Name,
+ Type: record.Type,
+ Enable: record.Enable,
+ Config: maskStorageConfig(config),
+ ConfigVersion: record.ConfigVersion,
+ Error: initErr,
+ }
+}
+
+func (s *server) listStorages(w http.ResponseWriter, r *http.Request) {
+ records, err := database.ListStorageRecords(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ initErrors := storage.LastErrors()
+ out := make([]StorageRecord, 0, len(records))
+ for i := range records {
+ out = append(out, storageResponse(&records[i], initErrors[records[i].Name]))
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"storages": out})
+}
+
+func (s *server) createStorage(w http.ResponseWriter, r *http.Request) {
+ var in StorageInput
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&in); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ record, err := s.deps.Settings.CreateStorage(r.Context(), settings.StorageInput{
+ Name: in.Name, Type: in.Type, Enable: in.Enable, Config: in.Config,
+ })
+ if err != nil {
+ if errors.Is(err, settings.ErrStorageExists) {
+ WriteError(w, http.StatusConflict, "storage_exists", err.Error())
+ return
+ }
+ WriteError(w, http.StatusBadRequest, "storage_create_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusCreated, storageResponse(record, ""))
+}
+
+func (s *server) updateStorage(w http.ResponseWriter, r *http.Request) {
+ id64, err := strconv.ParseUint(r.PathValue("storage_id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid storage id")
+ return
+ }
+ var in StorageInput
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&in); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ record, err := s.deps.Settings.UpdateStorage(r.Context(), uint(id64), settings.StorageInput{
+ Name: in.Name, Type: in.Type, Enable: in.Enable, Config: in.Config,
+ })
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "storage_update_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, storageResponse(record, ""))
+}
+
+func (s *server) deleteStorage(w http.ResponseWriter, r *http.Request) {
+ id64, err := strconv.ParseUint(r.PathValue("storage_id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid storage id")
+ return
+ }
+ if err := s.deps.Settings.DeleteStorage(r.Context(), uint(id64)); err != nil {
+ if errors.Is(err, settings.ErrStorageInUse) {
+ WriteError(w, http.StatusConflict, "storage_in_use", err.Error())
+ return
+ }
+ WriteError(w, http.StatusBadRequest, "storage_delete_failed", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) testStorage(w http.ResponseWriter, r *http.Request) {
+ id64, err := strconv.ParseUint(r.PathValue("storage_id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid storage id")
+ return
+ }
+ testCtx := r.Context()
+ if clientCtx, err := getClientContext(); err == nil {
+ testCtx = tgutil.ExtWithContext(testCtx, clientCtx)
+ }
+ if err := s.deps.Settings.TestStorage(testCtx, uint(id64)); err != nil {
+ WriteJSON(w, http.StatusOK, map[string]any{"ok": false, "error": err.Error()})
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"ok": true})
+}
diff --git a/api/system.go b/api/system.go
new file mode 100644
index 00000000..3e8f5e7b
--- /dev/null
+++ b/api/system.go
@@ -0,0 +1,42 @@
+package api
+
+import (
+ "net/http"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
+)
+
+func (s *server) systemInfo(w http.ResponseWriter, r *http.Request) {
+ cfg := s.deps.Settings.Config()
+ WriteJSON(w, http.StatusOK, map[string]any{
+ "version": config.Version,
+ "commit": config.GitCommit,
+ "build_time": config.BuildTime,
+ "docker": config.Docker == "true",
+ "uptime_seconds": int64(time.Since(s.startedAt).Seconds()),
+ "data_dir": s.deps.DataDir,
+ "db_path": s.deps.DBPath,
+ "workers": cfg.Workers,
+ "queued_tasks": len(core.GetQueuedTasks(r.Context())),
+ "running_tasks": len(core.GetRunningTasks(r.Context())),
+ })
+}
+
+func (s *server) restartSystem(w http.ResponseWriter, r *http.Request) {
+ force, err := decodeForce(r)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if !force && core.ActiveTaskCount() > 0 {
+ WriteError(w, http.StatusConflict, "tasks_running", "tasks are running; use {\"force\":true} to restart anyway")
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"restarting": true})
+ go func() {
+ time.Sleep(200 * time.Millisecond)
+ s.deps.Telegram.RestartProcess()
+ }()
+}
diff --git a/api/tasks.go b/api/tasks.go
new file mode 100644
index 00000000..4e8039e9
--- /dev/null
+++ b/api/tasks.go
@@ -0,0 +1,355 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "net/http"
+ "strconv"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/client/bot"
+ userclient "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/core/retry"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/mygotg/ext"
+)
+
+type mediaMetadataExtractor func(ctx context.Context, url string) (*MediaMetadataResponse, error)
+
+func (s *server) createTask(w http.ResponseWriter, r *http.Request) {
+ var req CreateTaskRequest
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&req); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "failed to decode request body: "+err.Error())
+ return
+ }
+ if req.Type == "" {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "task type is required")
+ return
+ }
+ if req.StorageID == 0 {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "storage_id is required")
+ return
+ }
+ resp, err := s.factory.CreateTask(&req)
+ if err != nil {
+ if errors.Is(err, ErrClientUnavailable) {
+ WriteError(w, http.StatusServiceUnavailable, "client_unavailable", err.Error())
+ return
+ }
+ WriteError(w, http.StatusBadRequest, "task_creation_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusCreated, resp)
+}
+
+func (s *server) listTasks(w http.ResponseWriter, r *http.Request) {
+ query := r.URL.Query()
+ limit := 0
+ if raw := query.Get("limit"); raw != "" {
+ parsed, err := strconv.Atoi(raw)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid limit")
+ return
+ }
+ limit = parsed
+ }
+ offset := 0
+ if raw := query.Get("offset"); raw != "" {
+ parsed, err := strconv.Atoi(raw)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid offset")
+ return
+ }
+ offset = parsed
+ }
+ records, total, err := database.ListTaskRecords(r.Context(), database.TaskFilter{
+ Status: query.Get("status"),
+ Type: query.Get("type"),
+ Limit: limit,
+ Offset: offset,
+ })
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ tasks := make([]TaskInfoResponse, 0, len(records))
+ taskIDs := make([]string, 0, len(records))
+ for i := range records {
+ taskIDs = append(taskIDs, records[i].TaskID)
+ }
+ failedItems, err := database.ListFailedTaskItems(r.Context(), taskIDs, 5)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ failedCounts, err := database.CountFailedTaskItems(r.Context(), taskIDs)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ metadataCounts, err := database.CountTaskMetadata(r.Context(), taskIDs)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ for i := range records {
+ resp := taskResponse(&records[i])
+ resp.FailedItems = taskItemResponses(failedItems[records[i].TaskID], nil)
+ resp.FailedCount = failedCounts[records[i].TaskID]
+ resp.MetadataAvailable = metadataCounts[records[i].TaskID] > 0
+ tasks = append(tasks, resp)
+ }
+ WriteJSON(w, http.StatusOK, TasksListResponse{Tasks: tasks, Total: total})
+}
+
+func (s *server) getTask(w http.ResponseWriter, r *http.Request) {
+ record, err := database.GetTaskRecord(r.Context(), r.PathValue("id"))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "task_not_found", "task not found")
+ return
+ }
+ resp := taskResponse(record)
+ items, err := database.ListTaskItems(r.Context(), record.TaskID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ metadataRecords, err := database.ListTaskItemMetadata(r.Context(), record.TaskID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ metadataAvailable := make(map[int]bool, len(metadataRecords))
+ for _, item := range metadataRecords {
+ metadataAvailable[item.ItemIndex] = true
+ }
+ resp.MetadataAvailable = len(metadataRecords) > 0
+ resp.Items = taskItemResponses(items, metadataAvailable)
+ for _, item := range items {
+ if item.Status == string(core.ItemStatusFailed) {
+ resp.FailedCount++
+ }
+ }
+ WriteJSON(w, http.StatusOK, resp)
+}
+
+func taskItemResponses(items []database.TaskItemRecord, metadataAvailable map[int]bool) []TaskItemResponse {
+ if len(items) == 0 {
+ return nil
+ }
+ out := make([]TaskItemResponse, 0, len(items))
+ for _, item := range items {
+ out = append(out, TaskItemResponse{
+ Index: item.Index,
+ Name: item.Name,
+ Path: item.Path,
+ StorageID: item.StorageID,
+ StorageName: item.StorageName,
+ Status: item.Status,
+ Error: item.Error,
+ Size: item.Size,
+ SourceChatID: item.SourceChatID,
+ SourceMessageID: item.SourceMsgID,
+ MetadataAvailable: metadataAvailable[item.Index],
+ })
+ }
+ return out
+}
+
+func (s *server) getTaskMetadata(w http.ResponseWriter, r *http.Request) {
+ record, err := database.GetTaskRecord(r.Context(), r.PathValue("id"))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "task_not_found", "task not found")
+ return
+ }
+ records, err := database.ListTaskItemMetadata(r.Context(), record.TaskID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if len(records) == 0 {
+ WriteError(w, http.StatusNotFound, "metadata_not_found", "no metadata recorded for this task")
+ return
+ }
+ taskItems, err := database.ListTaskItems(r.Context(), record.TaskID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ byIndex := make(map[int]database.TaskItemRecord, len(taskItems))
+ for _, item := range taskItems {
+ byIndex[item.Index] = item
+ }
+ items := make([]TaskMetadataItem, 0, len(records))
+ for _, item := range records {
+ taskItem := byIndex[item.ItemIndex]
+ items = append(items, TaskMetadataItem{
+ Index: item.ItemIndex,
+ Name: taskItem.Name,
+ Path: taskItem.Path,
+ Metadata: json.RawMessage(item.JSON),
+ })
+ }
+ WriteJSON(w, http.StatusOK, TaskMetadataResponse{TaskID: record.TaskID, Items: items})
+}
+
+func (s *server) retryTask(w http.ResponseWriter, r *http.Request) {
+ record, err := database.GetTaskRecord(r.Context(), r.PathValue("id"))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "task_not_found", "task not found")
+ return
+ }
+ newID, count, err := retry.FailedTask(s.deps.Ctx, []*ext.Context{bot.ExtContext(), userclient.GetCtx()}, record.TaskID)
+ switch {
+ case err == nil:
+ WriteJSON(w, http.StatusAccepted, map[string]any{"task_id": newID, "status": TaskStatusQueued, "file_count": count})
+ case errors.Is(err, retry.ErrTaskActive):
+ WriteError(w, http.StatusConflict, "task_active", err.Error())
+ case errors.Is(err, retry.ErrNothingToRetry):
+ WriteError(w, http.StatusConflict, "nothing_to_retry", err.Error())
+ case errors.Is(err, retry.ErrUnsupportedType):
+ WriteError(w, http.StatusBadRequest, "retry_unsupported", err.Error())
+ case errors.Is(err, retry.ErrStorageMissing):
+ WriteError(w, http.StatusConflict, "storage_missing", err.Error())
+ case errors.Is(err, retry.ErrSourceUnavailable):
+ WriteError(w, http.StatusConflict, "source_unavailable", err.Error())
+ case errors.Is(err, retry.ErrClientUnavailable):
+ WriteError(w, http.StatusServiceUnavailable, "client_unavailable", err.Error())
+ default:
+ WriteError(w, http.StatusInternalServerError, "retry_failed", err.Error())
+ }
+}
+
+func (s *server) cancelTask(w http.ResponseWriter, r *http.Request) {
+ record, err := database.GetTaskRecord(r.Context(), r.PathValue("id"))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "task_not_found", "task not found")
+ return
+ }
+ if isTerminal(record.Status) {
+ WriteError(w, http.StatusConflict, "task_not_active", "task is not active")
+ return
+ }
+ if task, ok := GetTask(record.TaskID); ok {
+ task.UpdateStatus(TaskStatusCancelled)
+ }
+ if err := core.CancelTask(r.Context(), record.TaskID); err != nil {
+ if task, ok := GetTask(record.TaskID); ok {
+ task.UpdateStatus(TaskStatus(record.Status))
+ }
+ WriteError(w, http.StatusInternalServerError, "cancel_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"task_id": record.TaskID, "status": TaskStatusCancelled})
+}
+
+func (s *server) deleteTask(w http.ResponseWriter, r *http.Request) {
+ taskID := r.PathValue("id")
+ record, err := database.GetTaskRecord(r.Context(), taskID)
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "task_not_found", "task not found")
+ return
+ }
+ if !isTerminal(record.Status) {
+ WriteError(w, http.StatusConflict, "task_active", "cancel the task before deleting it")
+ return
+ }
+ if err := database.DeleteTaskRecord(r.Context(), taskID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ DeleteTask(taskID)
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) taskTypes(w http.ResponseWriter, r *http.Request) {
+ types := []tasktype.TaskType{
+ tasktype.TaskTypeDirectlinks,
+ tasktype.TaskTypeYtdlp,
+ tasktype.TaskTypeAria2,
+ tasktype.TaskTypeParseditem,
+ tasktype.TaskTypeTgfiles,
+ tasktype.TaskTypeTphpics,
+ tasktype.TaskTypeTransfer,
+ }
+ out := make([]map[string]string, 0, len(types))
+ for _, t := range types {
+ out = append(out, map[string]string{"type": t.String(), "label": t.String()})
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"types": out})
+}
+
+func (s *server) mediaMetadata(w http.ResponseWriter, r *http.Request) {
+ mediaURL := r.URL.Query().Get("url")
+ if mediaURL == "" {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "url query parameter is required")
+ return
+ }
+ ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
+ defer cancel()
+ resp, err := s.mediaMetadataExtractor(ctx, mediaURL)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "metadata_extraction_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, resp)
+}
+
+func isTerminal(status string) bool {
+ return TaskStatus(status).IsTerminal()
+}
+
+func taskResponse(record *database.TaskRecord) TaskInfoResponse {
+ resp := TaskInfoResponse{
+ TaskID: record.TaskID,
+ Type: tasktype.TaskType(record.Type),
+ Status: TaskStatus(record.Status),
+ Title: record.Title,
+ StorageID: record.StorageID,
+ StorageName: record.StorageName,
+ Path: record.Path,
+ Source: record.Source,
+ Error: record.Error,
+ CreatedAt: record.CreatedAt,
+ StartedAt: record.StartedAt,
+ FinishedAt: record.FinishedAt,
+ UpdatedAt: record.UpdatedAt,
+ }
+ if info, ok := GetTask(record.TaskID); ok {
+ status, total, downloaded, totalFiles, downloadedFiles, startedAt, errMsg, updatedAt := info.snapshot()
+ resp.Status = status
+ resp.Error = errMsg
+ resp.UpdatedAt = updatedAt
+ if resp.StartedAt == nil && !startedAt.IsZero() {
+ resp.StartedAt = &startedAt
+ }
+ var percent, speedMBPS float64
+ if total > 0 {
+ percent = float64(downloaded) * 100 / float64(total)
+ } else if totalFiles > 0 {
+ percent = float64(downloadedFiles) * 100 / float64(totalFiles)
+ }
+ if !startedAt.IsZero() {
+ if elapsed := time.Since(startedAt).Seconds(); elapsed > 0 {
+ speedMBPS = float64(downloaded) / elapsed / (1024 * 1024)
+ }
+ }
+ if total > 0 || totalFiles > 0 {
+ resp.Progress = &TaskProgress{
+ TotalBytes: total,
+ DownloadedBytes: downloaded,
+ TotalFiles: totalFiles,
+ DownloadedFiles: downloadedFiles,
+ Percent: percent,
+ SpeedMBPS: speedMBPS,
+ }
+ }
+ }
+ if resp.Progress == nil && resp.Status == TaskStatusCompleted {
+ resp.Progress = &TaskProgress{Percent: 100}
+ }
+ return resp
+}
diff --git a/api/telegram.go b/api/telegram.go
new file mode 100644
index 00000000..b350ed81
--- /dev/null
+++ b/api/telegram.go
@@ -0,0 +1,135 @@
+package api
+
+import (
+ "encoding/json"
+ "errors"
+ "io"
+ "net/http"
+ "strings"
+
+ userclient "github.com/krau/SaveAny-Bot/client/user"
+)
+
+func decodeForce(r *http.Request) (bool, error) {
+ var body struct {
+ Force bool `json:"force"`
+ }
+ if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil && !errors.Is(err, io.EOF) {
+ return false, err
+ }
+ return body.Force, nil
+}
+
+func (s *server) telegramStatus(w http.ResponseWriter, r *http.Request) {
+ WriteJSON(w, http.StatusOK, s.deps.Telegram.Status(r.Context()))
+}
+
+func (s *server) startBot(w http.ResponseWriter, r *http.Request) {
+ cfg := s.deps.Settings.Config()
+ if strings.TrimSpace(cfg.Telegram.Token) == "" {
+ WriteError(w, http.StatusBadRequest, "missing_bot_token", "bot token is not configured")
+ return
+ }
+ if err := s.deps.Telegram.StartBot(r.Context()); err != nil {
+ WriteError(w, http.StatusBadRequest, "bot_start_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"state": "starting"})
+}
+
+func (s *server) stopBot(w http.ResponseWriter, r *http.Request) {
+ force, err := decodeForce(r)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if err := s.deps.Telegram.StopBot(r.Context(), force); err != nil {
+ WriteError(w, http.StatusConflict, "bot_stop_failed", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) restartBot(w http.ResponseWriter, r *http.Request) {
+ force, err := decodeForce(r)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if err := s.deps.Telegram.RestartBot(r.Context(), force); err != nil {
+ WriteError(w, http.StatusConflict, "bot_restart_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"state": "starting"})
+}
+
+func (s *server) startUserbotLogin(w http.ResponseWriter, r *http.Request) {
+ var body struct {
+ Phone string `json:"phone"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ flowID, err := s.deps.Telegram.StartUserbotLogin(r.Context(), strings.TrimSpace(body.Phone))
+ if err != nil {
+ WriteError(w, http.StatusConflict, "userbot_login_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"flow_id": flowID})
+}
+
+func (s *server) submitUserbotCode(w http.ResponseWriter, r *http.Request) {
+ var body struct {
+ Code string `json:"code"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if err := s.deps.Telegram.SubmitUserbotCode(r.Context(), r.PathValue("flow"), strings.TrimSpace(body.Code)); err != nil {
+ WriteError(w, http.StatusConflict, "userbot_login_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"ok": true})
+}
+
+func (s *server) submitUserbotPassword(w http.ResponseWriter, r *http.Request) {
+ var body struct {
+ Password string `json:"password"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if err := s.deps.Telegram.SubmitUserbotPassword(r.Context(), r.PathValue("flow"), body.Password); err != nil {
+ WriteError(w, http.StatusConflict, "userbot_login_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{"ok": true})
+}
+
+func (s *server) cancelUserbotLogin(w http.ResponseWriter, r *http.Request) {
+ if err := s.deps.Telegram.CancelUserbotLogin(r.Context(), r.PathValue("flow")); err != nil {
+ WriteError(w, http.StatusConflict, "userbot_login_failed", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) logoutUserbot(w http.ResponseWriter, r *http.Request) {
+ if err := s.deps.Telegram.LogoutUserbot(r.Context()); err != nil {
+ WriteError(w, http.StatusInternalServerError, "userbot_logout_failed", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) syncUserbotPeers(w http.ResponseWriter, r *http.Request) {
+ count, err := userclient.SyncPeers(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusConflict, "sync_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"synced": count})
+}
diff --git a/api/tgfiles.go b/api/tgfiles.go
index 066a7e78..60e8b12a 100644
--- a/api/tgfiles.go
+++ b/api/tgfiles.go
@@ -4,16 +4,19 @@ import (
"context"
"fmt"
"net/url"
+ "regexp"
"strconv"
"strings"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
userclient "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/common/utils/strutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/ext"
)
type MessageContext struct {
@@ -35,10 +38,9 @@ func getClientContext() (*ext.Context, error) {
}
func resolveChatID(_ context.Context, idOrUsername string) (int64, error) {
- if id, err := strconv.ParseInt(idOrUsername, 10, 64); err == nil {
- // 私有频道 ID 需要加上 -100 前缀
- if id > 0 {
- return -1000000000000 - id, nil
+ if id, err := strconv.ParseInt(strings.TrimPrefix(idOrUsername, "@"), 10, 64); err == nil {
+ if id == 0 {
+ return 0, fmt.Errorf("chat ID must not be zero")
}
return id, nil
}
@@ -89,7 +91,13 @@ func ParseMessageLink(ctx context.Context, link string) (int64, int, error) {
if paths[0] != "c" {
chatPart = paths[0]
}
- chatID, err := resolveChatID(ctx, chatPart)
+ var chatID int64
+ var err error
+ if paths[0] == "c" {
+ chatID, err = tgutil.ParseChannelID(nil, chatPart)
+ } else {
+ chatID, err = resolveChatID(ctx, chatPart)
+ }
if err != nil {
return 0, 0, fmt.Errorf("failed to resolve chat ID: %w", err)
}
@@ -103,7 +111,7 @@ func ParseMessageLink(ctx context.Context, link string) (int64, int, error) {
if paths[0] != "c" {
return 0, 0, fmt.Errorf("invalid message link format: %s", link)
}
- chatID, err := resolveChatID(ctx, paths[1])
+ chatID, err := tgutil.ParseChannelID(nil, paths[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to resolve chat ID: %w", err)
}
@@ -178,8 +186,12 @@ func ExtractFilesFromLinks(ctx context.Context, links []string) ([]tfile.TGFileM
continue
}
- u, _ := url.Parse(link)
- single := u != nil && u.Query().Has("single")
+ u, err := url.Parse(link)
+ if err != nil {
+ logger.Warnf("Invalid message link %s: %v", link, err)
+ continue
+ }
+ single := u.Query().Has("single")
msgCtx, err := getMessageWithContext(ctx, chatID, msgID)
if err != nil {
@@ -254,3 +266,73 @@ func isValidMessageLink(link string) bool {
}
return false
}
+
+// ExtractFilesFromRange resolves a chat and message ID range and returns the
+// supported media messages as files. It is the web equivalent of the bot's
+// batch /save syntax.
+func ExtractFilesFromRange(ctx context.Context, chatRef, rangeRef, filterRef string) ([]tfile.TGFileMessage, error) {
+ logger := log.FromContext(ctx)
+ chatRef = strings.TrimSpace(chatRef)
+ rangeRef = strings.TrimSpace(rangeRef)
+ if chatRef == "" {
+ return nil, fmt.Errorf("chat_id is required")
+ }
+ start, end, err := strutil.ParseIntStrRange(rangeRef, "-")
+ if err != nil {
+ return nil, fmt.Errorf("invalid message range: %w", err)
+ }
+
+ clientCtx, err := getClientContext()
+ if err != nil {
+ return nil, err
+ }
+ if _, err := strconv.ParseInt(strings.TrimPrefix(chatRef, "@"), 10, 64); err == nil {
+ if uc := userclient.GetCtx(); uc != nil {
+ clientCtx = uc
+ }
+ }
+ chatID, err := tgutil.ParseChatID(clientCtx, chatRef)
+ if err != nil {
+ return nil, fmt.Errorf("invalid chat: %w", err)
+ }
+ msgs, err := tgutil.GetMessagesRange(clientCtx, chatID, int(start), int(end))
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch messages: %w", err)
+ }
+ if len(msgs) == 0 {
+ return nil, fmt.Errorf("no messages found in the specified range")
+ }
+
+ var filter *regexp.Regexp
+ if strings.TrimSpace(filterRef) != "" {
+ filter, err = regexp.Compile(strings.TrimSpace(filterRef))
+ if err != nil {
+ return nil, fmt.Errorf("invalid filter: %w", err)
+ }
+ }
+ files := make([]tfile.TGFileMessage, 0, len(msgs))
+ for _, msg := range msgs {
+ if msg == nil || msg.Media == nil || !mediautil.IsSupported(msg.Media) {
+ continue
+ }
+ if filter != nil {
+ text := msg.GetMessage()
+ if name, err := tgutil.GetMediaFileName(msg.Media); err == nil {
+ text += " " + name
+ }
+ if !filter.MatchString(text) {
+ continue
+ }
+ }
+ file, err := tfile.FromMediaMessage(msg.Media, clientCtx.Raw, msg, tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*msg)))
+ if err != nil {
+ logger.Warnf("Failed to create file from message %d: %v", msg.ID, err)
+ continue
+ }
+ files = append(files, file)
+ }
+ if len(files) == 0 {
+ return nil, fmt.Errorf("no savable files found in the specified range")
+ }
+ return files, nil
+}
diff --git a/api/tphpics.go b/api/tphpics.go
index 73a60a5b..06b0d551 100644
--- a/api/tphpics.go
+++ b/api/tphpics.go
@@ -2,74 +2,12 @@ package api
import (
"context"
- "fmt"
- "net/url"
- "strings"
- "github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/common/utils/tphutil"
- "github.com/krau/SaveAny-Bot/pkg/telegraph"
)
+// ExtractTelegraphImages fetches image URLs from a Telegraph page. The shared
+// helper keeps the API and bot paths on the same URL validation and client.
func ExtractTelegraphImages(ctx context.Context, pageURL string) ([]string, string, error) {
- logger := log.FromContext(ctx)
-
- if !isValidTelegraphURL(pageURL) {
- return nil, "", fmt.Errorf("invalid telegraph URL format: %s", pageURL)
- }
-
- pagepath, err := parseTelegraphPath(pageURL)
- if err != nil {
- return nil, "", err
- }
-
- logger.Debugf("Fetching telegraph page: %s", pagepath)
-
- client := telegraph.NewClient()
- page, err := client.GetPage(ctx, pagepath)
- if err != nil {
- return nil, "", fmt.Errorf("failed to get telegraph page: %w", err)
- }
-
- var imgs []string
- for _, elem := range page.Content {
- imgs = append(imgs, tphutil.GetNodeImages(elem)...)
- }
-
- if len(imgs) == 0 {
- return nil, "", fmt.Errorf("no images found in telegraph page")
- }
-
- return imgs, pagepath, nil
-}
-
-func parseTelegraphPath(pageURL string) (string, error) {
- u, err := url.Parse(pageURL)
- if err != nil {
- return "", fmt.Errorf("invalid telegraph URL: %w", err)
- }
-
- if !strings.HasSuffix(u.Host, "telegra.ph") && !strings.HasSuffix(u.Host, "telegraph.co") {
- return "", fmt.Errorf("invalid telegraph URL host: %s", u.Host)
- }
-
- paths := strings.Split(strings.TrimPrefix(u.Path, "/"), "/")
- if len(paths) == 0 || paths[0] == "" {
- return "", fmt.Errorf("invalid telegraph URL path: %s", u.Path)
- }
-
- pagepath := paths[len(paths)-1]
- pagepath, err = url.PathUnescape(pagepath)
- if err != nil {
- return "", fmt.Errorf("failed to unescape telegraph path: %w", err)
- }
-
- return strings.TrimSpace(pagepath), nil
-}
-
-func isValidTelegraphURL(url string) bool {
- return strings.HasPrefix(url, "https://telegra.ph/") ||
- strings.HasPrefix(url, "http://telegra.ph/") ||
- strings.HasPrefix(url, "https://telegraph.co/") ||
- strings.HasPrefix(url, "http://telegraph.co/")
+ return tphutil.ExtractImages(ctx, pageURL)
}
diff --git a/api/types.go b/api/types.go
index 7b82909d..e7b70b3e 100644
--- a/api/types.go
+++ b/api/types.go
@@ -1,36 +1,112 @@
package api
import (
+ "context"
"encoding/json"
"net/http"
"time"
+ "github.com/krau/SaveAny-Bot/pkg/enums/storagefilter"
+ "github.com/krau/SaveAny-Bot/pkg/enums/taskstatus"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/settings"
)
-type TaskStatus string
+type Deps struct {
+ Ctx context.Context
+ Settings *settings.Service
+ Telegram TelegramController
+ DataDir string
+ DBPath string
+ WebToken string
+}
+
+type StorageInput struct {
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Enable bool `json:"enable"`
+ Config map[string]any `json:"config"`
+}
+
+type StorageRecord struct {
+ StorageID uint `json:"storage_id"`
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Enable bool `json:"enable"`
+ Config map[string]any `json:"config"`
+ ConfigVersion int `json:"config_version,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+type BotStatus struct {
+ State string `json:"state"`
+ ID int64 `json:"id,omitempty"`
+ Username string `json:"username,omitempty"`
+ Error string `json:"error,omitempty"`
+ StartedAt time.Time `json:"started_at"`
+}
+
+type LoginFlow struct {
+ FlowID string `json:"flow_id"`
+ State string `json:"state"`
+ Error string `json:"error,omitempty"`
+}
+
+type UserbotStatus struct {
+ Enabled bool `json:"enabled"`
+ State string `json:"state"`
+ ID int64 `json:"id,omitempty"`
+ Username string `json:"username,omitempty"`
+ Phone string `json:"phone,omitempty"`
+ Error string `json:"error,omitempty"`
+ Login *LoginFlow `json:"login,omitempty"`
+}
+
+type TelegramStatus struct {
+ Bot BotStatus `json:"bot"`
+ Userbot UserbotStatus `json:"userbot"`
+}
+
+type TelegramController interface {
+ Status(ctx context.Context) TelegramStatus
+ StartBot(ctx context.Context) error
+ StopBot(ctx context.Context, force bool) error
+ RestartBot(ctx context.Context, force bool) error
+ StartUserbotLogin(ctx context.Context, phone string) (flowID string, err error)
+ SubmitUserbotCode(ctx context.Context, flowID, code string) error
+ SubmitUserbotPassword(ctx context.Context, flowID, password string) error
+ CancelUserbotLogin(ctx context.Context, flowID string) error
+ LogoutUserbot(ctx context.Context) error
+ ApplyTelegram(ctx context.Context) error
+ RestartProcess()
+}
+
+type TaskStatus = taskstatus.Status
const (
- TaskStatusQueued TaskStatus = "queued"
- TaskStatusRunning TaskStatus = "running"
- TaskStatusCompleted TaskStatus = "completed"
- TaskStatusFailed TaskStatus = "failed"
- TaskStatusCancelled TaskStatus = "cancelled"
+ TaskStatusQueued = taskstatus.Queued
+ TaskStatusRunning = taskstatus.Running
+ TaskStatusCompleted = taskstatus.Completed
+ TaskStatusPartial = taskstatus.Partial
+ TaskStatusFailed = taskstatus.Failed
+ TaskStatusCancelled = taskstatus.Cancelled
)
type CreateTaskRequest struct {
- Type tasktype.TaskType `json:"type"`
- Storage string `json:"storage"`
- Path string `json:"path"`
- Webhook string `json:"webhook,omitempty"`
- Params json.RawMessage `json:"params"`
+ Type tasktype.TaskType `json:"type"`
+ StorageID uint `json:"storage_id"`
+ Path string `json:"path"`
+ Webhook string `json:"webhook,omitempty"`
+ Params json.RawMessage `json:"params"`
}
type CreateTaskResponse struct {
- TaskID string `json:"task_id"`
- Type tasktype.TaskType `json:"type"`
- Status TaskStatus `json:"status"`
- CreatedAt time.Time `json:"created_at"`
+ TaskID string `json:"task_id"`
+ Type tasktype.TaskType `json:"type"`
+ Status TaskStatus `json:"status"`
+ StorageID uint `json:"storage_id"`
+ StorageName string `json:"storage_name"`
+ CreatedAt time.Time `json:"created_at"`
}
type TaskProgress struct {
@@ -43,45 +119,107 @@ type TaskProgress struct {
}
type TaskInfoResponse struct {
- TaskID string `json:"task_id"`
- Type tasktype.TaskType `json:"type"`
- Status TaskStatus `json:"status"`
- Title string `json:"title"`
- Progress *TaskProgress `json:"progress,omitempty"`
- Storage string `json:"storage"`
- Path string `json:"path"`
- Error string `json:"error,omitempty"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
+ TaskID string `json:"task_id"`
+ Type tasktype.TaskType `json:"type"`
+ Status TaskStatus `json:"status"`
+ Title string `json:"title"`
+ Progress *TaskProgress `json:"progress,omitempty"`
+ StorageID uint `json:"storage_id"`
+ StorageName string `json:"storage_name"`
+ Path string `json:"path"`
+ Source string `json:"source,omitempty"`
+ Error string `json:"error,omitempty"`
+ CreatedAt time.Time `json:"created_at"`
+ StartedAt *time.Time `json:"started_at,omitempty"`
+ FinishedAt *time.Time `json:"finished_at,omitempty"`
+ UpdatedAt time.Time `json:"updated_at"`
+ Items []TaskItemResponse `json:"items,omitempty"`
+ FailedItems []TaskItemResponse `json:"failed_items,omitempty"`
+ FailedCount int `json:"failed_count,omitempty"`
+ MetadataAvailable bool `json:"metadata_available,omitempty"`
+}
+
+type TaskItemResponse struct {
+ Index int `json:"index"`
+ Name string `json:"name"`
+ Path string `json:"path,omitempty"`
+ StorageID uint `json:"storage_id,omitempty"`
+ StorageName string `json:"storage_name,omitempty"`
+ Status string `json:"status"`
+ Error string `json:"error,omitempty"`
+ Size int64 `json:"size,omitempty"`
+ SourceChatID int64 `json:"source_chat_id,omitempty"`
+ SourceMessageID int `json:"source_message_id,omitempty"`
+ MetadataAvailable bool `json:"metadata_available,omitempty"`
+}
+
+type TaskMetadataResponse struct {
+ TaskID string `json:"task_id"`
+ Items []TaskMetadataItem `json:"items"`
+}
+
+type TaskMetadataItem struct {
+ Index int `json:"index"`
+ Name string `json:"name,omitempty"`
+ Path string `json:"path,omitempty"`
+ Metadata json.RawMessage `json:"metadata"`
}
type TasksListResponse struct {
Tasks []TaskInfoResponse `json:"tasks"`
- Total int `json:"total"`
+ Total int64 `json:"total"`
+}
+
+type UserStorageFilter struct {
+ StorageIDs []uint `json:"storage_ids"`
+ StorageNames []string `json:"storage_names,omitempty"`
+ Mode storagefilter.Mode `json:"mode"`
}
-type StoragesResponse struct {
- Storages []StorageInfo `json:"storages"`
+type UserResponse struct {
+ ID uint `json:"id"`
+ ChatID int64 `json:"chat_id"`
+ Enabled bool `json:"enabled"`
+ Silent bool `json:"silent"`
+ DefaultStorageID uint `json:"default_storage_id"`
+ DefaultStorageName string `json:"default_storage_name,omitempty"`
+ DefaultDirID uint `json:"default_dir_id"`
+ ApplyRule bool `json:"apply_rule"`
+ FilenameStrategy string `json:"filename_strategy"`
+ FilenameTemplate string `json:"filename_template"`
+ ConflictStrategy string `json:"conflict_strategy"`
+ WatchNotify bool `json:"watch_notify"`
+ StorageFilter UserStorageFilter `json:"storage_filter"`
}
-type StorageInfo struct {
- Name string `json:"name"`
- Type string `json:"type"`
+type UserPatch struct {
+ Enabled *bool `json:"enabled,omitempty"`
+ Silent *bool `json:"silent,omitempty"`
+ DefaultStorageID *uint `json:"default_storage_id,omitempty"`
+ DefaultDirID *uint `json:"default_dir_id,omitempty"`
+ ApplyRule *bool `json:"apply_rule,omitempty"`
+ FilenameStrategy *string `json:"filename_strategy,omitempty"`
+ FilenameTemplate *string `json:"filename_template,omitempty"`
+ ConflictStrategy *string `json:"conflict_strategy,omitempty"`
+ WatchNotify *bool `json:"watch_notify,omitempty"`
+ StorageFilter *UserStorageFilter `json:"storage_filter,omitempty"`
}
type WebhookPayload struct {
TaskID string `json:"task_id"`
Type string `json:"type"`
Status TaskStatus `json:"status"`
- Storage string `json:"storage"`
+ StorageID uint `json:"storage_id"`
+ StorageName string `json:"storage_name"`
Path string `json:"path"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Error string `json:"error,omitempty"`
}
type ErrorResponse struct {
- Error string `json:"error"`
- Message string `json:"message,omitempty"`
+ Error string `json:"error"`
+ Message string `json:"message,omitempty"`
+ Fields map[string]string `json:"fields,omitempty"`
}
type APIError struct {
@@ -90,9 +228,7 @@ type APIError struct {
Message string
}
-func (e *APIError) Error() string {
- return e.Message
-}
+func (e *APIError) Error() string { return e.Message }
func WriteJSON(w http.ResponseWriter, statusCode int, data any) error {
w.Header().Set("Content-Type", "application/json")
@@ -101,10 +237,11 @@ func WriteJSON(w http.ResponseWriter, statusCode int, data any) error {
}
func WriteError(w http.ResponseWriter, statusCode int, errCode, message string) error {
- return WriteJSON(w, statusCode, ErrorResponse{
- Error: errCode,
- Message: message,
- })
+ return WriteJSON(w, statusCode, ErrorResponse{Error: errCode, Message: message})
+}
+
+func WriteFieldErrors(w http.ResponseWriter, statusCode int, message string, fields map[string]string) error {
+ return WriteJSON(w, statusCode, ErrorResponse{Error: "validation_failed", Message: message, Fields: fields})
}
type DirectLinksParams struct {
@@ -126,14 +263,17 @@ type ParsedParams struct {
}
type TransferParams struct {
- SourceStorage string `json:"source_storage"`
- SourcePath string `json:"source_path"`
- TargetStorage string `json:"target_storage"`
- TargetPath string `json:"target_path"`
+ SourceStorageID uint `json:"source_storage_id"`
+ SourcePath string `json:"source_path"`
+ TargetStorageID uint `json:"target_storage_id"`
+ TargetPath string `json:"target_path"`
}
type TGFilesParams struct {
MessageLinks []string `json:"message_links"`
+ ChatID string `json:"chat_id,omitempty"`
+ MessageRange string `json:"message_range,omitempty"`
+ Filter string `json:"filter,omitempty"`
}
type TPHPicsParams struct {
diff --git a/api/update.go b/api/update.go
new file mode 100644
index 00000000..3696c5ae
--- /dev/null
+++ b/api/update.go
@@ -0,0 +1,135 @@
+package api
+
+import (
+ "errors"
+ "net/http"
+ "sync"
+ "time"
+
+ "github.com/blang/semver"
+ "github.com/krau/SaveAny-Bot/common/selfupdate"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/unvgo/ghselfupdate"
+)
+
+var updateMu sync.Mutex
+
+type updateInfoResponse struct {
+ Current string `json:"current"`
+ Latest string `json:"latest,omitempty"`
+ Available bool `json:"available"`
+ Updatable bool `json:"updatable"`
+ Docker bool `json:"docker"`
+ ReleaseNote string `json:"release_notes,omitempty"`
+ URL string `json:"url,omitempty"`
+ PublishedAt *time.Time `json:"published_at,omitempty"`
+ Size int64 `json:"size,omitempty"`
+ Reason string `json:"reason,omitempty"`
+ ReasonCode string `json:"reason_code,omitempty"`
+}
+
+func checkUpdate() (*updateInfoResponse, error) {
+ info := &updateInfoResponse{
+ Current: config.Version,
+ Docker: config.Docker == "true",
+ }
+ current, err := semver.Parse(config.Version)
+ if err != nil {
+ info.Reason = err.Error()
+ info.ReasonCode = "invalid_version"
+ return info, nil
+ }
+ info.Current = current.String()
+ if err := selfupdate.Updatable(); err != nil {
+ info.Reason = err.Error()
+ switch {
+ case errors.Is(err, selfupdate.ErrDocker):
+ info.ReasonCode = "docker"
+ case errors.Is(err, selfupdate.ErrDevBuild):
+ info.ReasonCode = "dev_build"
+ case errors.Is(err, selfupdate.ErrNotWritable):
+ info.ReasonCode = "not_writable"
+ default:
+ info.ReasonCode = "not_updatable"
+ }
+ return info, nil
+ }
+ info.Updatable = true
+ latest, ok, err := ghselfupdate.DetectLatest(config.GitRepo)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ info.ReasonCode = "no_release"
+ info.Reason = "no release found"
+ return info, nil
+ }
+ info.Latest = latest.Version.String()
+ info.ReleaseNote = latest.ReleaseNotes
+ info.URL = latest.AssetURL
+ info.Size = int64(latest.AssetByteSize)
+ info.PublishedAt = latest.PublishedAt
+ info.Available = latest.Version.Major == current.Major && latest.Version.GT(current)
+ if latest.Version.Major > current.Major {
+ info.Reason = "major upgrade required"
+ info.ReasonCode = "major_upgrade_required"
+ } else if !latest.Version.GT(current) {
+ info.Reason = "already on the latest version"
+ info.ReasonCode = "already_latest"
+ }
+ return info, nil
+}
+
+func (s *server) getUpdateInfo(w http.ResponseWriter, r *http.Request) {
+ info, err := checkUpdate()
+ if err != nil {
+ WriteError(w, http.StatusBadGateway, "update_check_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, info)
+}
+
+func (s *server) applyUpdate(w http.ResponseWriter, r *http.Request) {
+ updateMu.Lock()
+ defer updateMu.Unlock()
+
+ current, err := semver.Parse(config.Version)
+ if err != nil {
+ WriteError(w, http.StatusConflict, "not_updatable", err.Error())
+ return
+ }
+ if err := selfupdate.Updatable(); err != nil {
+ WriteError(w, http.StatusConflict, "not_updatable", err.Error())
+ return
+ }
+ latest, ok, err := ghselfupdate.DetectLatest(config.GitRepo)
+ if err != nil {
+ WriteError(w, http.StatusBadGateway, "update_check_failed", err.Error())
+ return
+ }
+ if !ok {
+ WriteError(w, http.StatusNotFound, "no_release", "no release found")
+ return
+ }
+ if latest.Version.Major > current.Major {
+ WriteError(w, http.StatusConflict, "major_upgrade_required", "major upgrade required")
+ return
+ }
+ if !latest.Version.GT(current) {
+ WriteError(w, http.StatusConflict, "already_latest", "already on latest version")
+ return
+ }
+ release, err := selfupdate.Update(current)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "update_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusAccepted, map[string]any{
+ "version": release.Version.String(),
+ "restarting": true,
+ })
+ go func() {
+ time.Sleep(200 * time.Millisecond)
+ s.deps.Telegram.RestartProcess()
+ }()
+}
diff --git a/api/user_automation.go b/api/user_automation.go
new file mode 100644
index 00000000..c00336fd
--- /dev/null
+++ b/api/user_automation.go
@@ -0,0 +1,442 @@
+package api
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "regexp"
+ "strconv"
+ "strings"
+
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/rule"
+)
+
+type userDirResponse struct {
+ ID uint `json:"id"`
+ StorageID uint `json:"storage_id"`
+ StorageName string `json:"storage_name,omitempty"`
+ Path string `json:"path"`
+}
+
+type userRuleResponse struct {
+ ID uint `json:"id"`
+ Type string `json:"type"`
+ Content string `json:"content"`
+ StorageID uint `json:"storage_id"`
+ StorageName string `json:"storage_name,omitempty"`
+ DirPath string `json:"dir_path"`
+}
+
+type userWatchResponse struct {
+ ID uint `json:"id"`
+ ChatID int64 `json:"chat_id"`
+ MessageFilter string `json:"message_filter,omitempty"`
+}
+
+func (s *server) requireUser(w http.ResponseWriter, r *http.Request) (*database.User, bool) {
+ id, err := strconv.ParseUint(r.PathValue("id"), 10, 32)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid user id")
+ return nil, false
+ }
+ user, err := database.GetUserByID(r.Context(), uint(id))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "user_not_found", "user not found")
+ return nil, false
+ }
+ return user, true
+}
+
+func userDirsResponse(dirs []database.Dir, namesByID map[uint]string) []userDirResponse {
+ out := make([]userDirResponse, 0, len(dirs))
+ for _, dir := range dirs {
+ out = append(out, userDirResponse{
+ ID: dir.ID,
+ StorageID: dir.StorageID,
+ StorageName: namesByID[dir.StorageID],
+ Path: dir.Path,
+ })
+ }
+ return out
+}
+
+func (s *server) listUserDirs(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ dirs, err := database.GetUserDirs(r.Context(), user.ID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"dirs": userDirsResponse(dirs, names)})
+}
+
+func (s *server) createUserDir(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ var body struct {
+ StorageID uint `json:"storage_id"`
+ Path string `json:"path"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ body.Path = strings.TrimSpace(body.Path)
+ if body.StorageID == 0 || body.Path == "" {
+ WriteFieldErrors(w, http.StatusBadRequest, "storage_id and path are required", map[string]string{
+ "storage_id": "required",
+ "path": "required",
+ })
+ return
+ }
+ if _, err := database.GetStorageRecord(r.Context(), body.StorageID); err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, "unknown storage", map[string]string{"storage_id": "unknown storage"})
+ return
+ }
+ existing, err := database.GetDirsByUserIDAndStorageID(r.Context(), user.ID, body.StorageID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ for _, dir := range existing {
+ if dir.Path == body.Path {
+ WriteError(w, http.StatusConflict, "dir_exists", "directory already exists")
+ return
+ }
+ }
+ if err := database.CreateDirForUser(r.Context(), user.ID, body.StorageID, body.Path); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ dirs, err := database.GetUserDirs(r.Context(), user.ID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ for _, dir := range dirs {
+ if dir.StorageID == body.StorageID && dir.Path == body.Path {
+ WriteJSON(w, http.StatusCreated, userDirResponse{
+ ID: dir.ID,
+ StorageID: dir.StorageID,
+ StorageName: names[dir.StorageID],
+ Path: dir.Path,
+ })
+ return
+ }
+ }
+ WriteError(w, http.StatusInternalServerError, "internal_error", "directory was not persisted")
+}
+
+func (s *server) deleteUserDir(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ dirID, err := strconv.ParseUint(r.PathValue("dirID"), 10, 32)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid directory id")
+ return
+ }
+ if err := database.DeleteDirByID(r.Context(), uint(dirID), user.ID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if user.DefaultDirID == uint(dirID) {
+ user.DefaultDirID = 0
+ if err := database.UpdateUser(r.Context(), user); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func userRulesResponse(rules []database.Rule, namesByID map[uint]string) []userRuleResponse {
+ out := make([]userRuleResponse, 0, len(rules))
+ for _, rule := range rules {
+ out = append(out, userRuleResponse{
+ ID: rule.ID,
+ Type: rule.Type,
+ Content: rule.Content,
+ StorageID: rule.StorageID,
+ StorageName: namesByID[rule.StorageID],
+ DirPath: rule.DirPath,
+ })
+ }
+ return out
+}
+
+func validateRuleContent(ruleType, content string) error {
+ switch ruleType {
+ case rule.FileNameRegex.String(), rule.MessageRegex.String():
+ if _, err := regexp.Compile(content); err != nil {
+ return fmt.Errorf("invalid regular expression: %w", err)
+ }
+ case rule.IsAlbum.String():
+ if _, err := strconv.ParseBool(content); err != nil {
+ return errors.New("IS-ALBUM content must be true or false")
+ }
+ default:
+ return fmt.Errorf("unsupported rule type: %s", ruleType)
+ }
+ return nil
+}
+
+func normalizeRuleType(raw string) (string, error) {
+ for _, typ := range rule.Values() {
+ if strings.EqualFold(strings.TrimSpace(raw), typ.String()) {
+ return typ.String(), nil
+ }
+ }
+ return "", fmt.Errorf("unsupported rule type: %s", raw)
+}
+
+func (s *server) listUserRules(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ rules, err := database.GetRulesByUserID(r.Context(), user.ID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"rules": userRulesResponse(rules, names)})
+}
+
+func (s *server) createUserRule(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ var body struct {
+ Type string `json:"type"`
+ Content string `json:"content"`
+ StorageID uint `json:"storage_id"`
+ DirPath string `json:"dir_path"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ ruleType, err := normalizeRuleType(body.Type)
+ if err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, err.Error(), map[string]string{"type": err.Error()})
+ return
+ }
+ body.Content = strings.TrimSpace(body.Content)
+ if err := validateRuleContent(ruleType, body.Content); err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, err.Error(), map[string]string{"content": err.Error()})
+ return
+ }
+ if body.StorageID != 0 {
+ if _, err := database.GetStorageRecord(r.Context(), body.StorageID); err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, "unknown storage", map[string]string{"storage_id": "unknown storage"})
+ return
+ }
+ }
+ record := &database.Rule{
+ UserID: user.ID,
+ Type: ruleType,
+ Content: body.Content,
+ StorageID: body.StorageID,
+ DirPath: strings.TrimSpace(body.DirPath),
+ }
+ if err := database.CreateRule(r.Context(), record); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusCreated, userRuleResponse{
+ ID: record.ID, Type: record.Type, Content: record.Content,
+ StorageID: record.StorageID, StorageName: names[record.StorageID],
+ DirPath: record.DirPath,
+ })
+}
+
+func (s *server) deleteUserRule(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ ruleID, err := strconv.ParseUint(r.PathValue("ruleID"), 10, 32)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid rule id")
+ return
+ }
+ if err := database.DeleteRule(r.Context(), uint(ruleID), user.ID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) importUserRulePresets(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ var body struct {
+ StorageID uint `json:"storage_id"`
+ BasePath string `json:"base_path"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if body.StorageID != 0 {
+ if _, err := database.GetStorageRecord(r.Context(), body.StorageID); err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, "unknown storage", map[string]string{"storage_id": "unknown storage"})
+ return
+ }
+ }
+ presets := rule.PresetCategories(strings.TrimSpace(body.BasePath))
+ created := 0
+ for _, preset := range presets {
+ record := &database.Rule{
+ UserID: user.ID,
+ Type: rule.FileNameRegex.String(),
+ Content: preset.Regex,
+ StorageID: body.StorageID,
+ DirPath: preset.Dir,
+ }
+ if err := database.CreateRule(r.Context(), record); err != nil {
+ continue
+ }
+ created++
+ }
+ if created == 0 {
+ WriteError(w, http.StatusInternalServerError, "internal_error", "failed to create preset rules")
+ return
+ }
+ WriteJSON(w, http.StatusCreated, map[string]any{"created": created})
+}
+
+func userWatchesResponse(chats []database.WatchChat) []userWatchResponse {
+ out := make([]userWatchResponse, 0, len(chats))
+ for _, chat := range chats {
+ out = append(out, userWatchResponse{ID: chat.ID, ChatID: chat.ChatID, MessageFilter: chat.MessageFilter})
+ }
+ return out
+}
+
+func normalizeWatchFilter(raw string) (string, error) {
+ raw = strings.TrimSpace(raw)
+ if raw == "" {
+ return "", nil
+ }
+ filterType, filterData, ok := strings.Cut(raw, ":")
+ if !ok || !strings.EqualFold(filterType, "msgre") || filterData == "" {
+ return "", errors.New("filter must be msgre:
")
+ }
+ if _, err := regexp.Compile(filterData); err != nil {
+ return "", fmt.Errorf("invalid filter expression: %w", err)
+ }
+ return "msgre:" + filterData, nil
+}
+
+func (s *server) listUserWatches(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"watches": userWatchesResponse(user.WatchChats)})
+}
+
+func (s *server) createUserWatch(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ var body struct {
+ ChatID int64 `json:"chat_id"`
+ Chat string `json:"chat"`
+ Filter string `json:"filter"`
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if body.ChatID == 0 && strings.TrimSpace(body.Chat) != "" {
+ chatID, err := resolveChatID(r.Context(), strings.TrimSpace(body.Chat))
+ if err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid chat", map[string]string{"chat": err.Error()})
+ return
+ }
+ body.ChatID = chatID
+ }
+ if body.ChatID == 0 {
+ WriteFieldErrors(w, http.StatusBadRequest, "chat is required", map[string]string{"chat": "required"})
+ return
+ }
+ filter, err := normalizeWatchFilter(body.Filter)
+ if err != nil {
+ WriteFieldErrors(w, http.StatusBadRequest, err.Error(), map[string]string{"filter": err.Error()})
+ return
+ }
+ watching, err := user.WatchingChat(r.Context(), body.ChatID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if watching {
+ WriteError(w, http.StatusConflict, "watch_exists", "chat is already watched")
+ return
+ }
+ if err := user.WatchChat(r.Context(), database.WatchChat{
+ UserID: user.ID,
+ ChatID: body.ChatID,
+ MessageFilter: filter,
+ }); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ refreshed, err := database.GetUserByID(r.Context(), user.ID)
+ if err == nil {
+ user = refreshed
+ }
+ WriteJSON(w, http.StatusCreated, userWatchesResponse(user.WatchChats))
+}
+
+func (s *server) deleteUserWatch(w http.ResponseWriter, r *http.Request) {
+ user, ok := s.requireUser(w, r)
+ if !ok {
+ return
+ }
+ chatID, err := strconv.ParseInt(r.PathValue("chatID"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid chat id")
+ return
+ }
+ if err := user.UnwatchChat(r.Context(), chatID); err != nil {
+ WriteError(w, http.StatusNotFound, "watch_not_found", "watched chat not found")
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
diff --git a/api/users.go b/api/users.go
new file mode 100644
index 00000000..314ff743
--- /dev/null
+++ b/api/users.go
@@ -0,0 +1,248 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "strconv"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/storagefilter"
+)
+
+func (s *server) listUsers(w http.ResponseWriter, r *http.Request) {
+ users, err := database.GetAllUsers(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ out := make([]UserResponse, 0, len(users))
+ for i := range users {
+ out = append(out, userResponse(&users[i], names))
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"users": out})
+}
+
+func (s *server) createUser(w http.ResponseWriter, r *http.Request) {
+ var body struct {
+ ChatID int64 `json:"chat_id"`
+ UserPatch
+ }
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if body.ChatID <= 0 {
+ WriteFieldErrors(w, http.StatusBadRequest, "chat_id must be a positive Telegram user ID", map[string]string{"chat_id": "invalid"})
+ return
+ }
+ if _, err := database.GetUserByChatID(r.Context(), body.ChatID); err == nil {
+ WriteError(w, http.StatusConflict, "user_exists", "user already exists")
+ return
+ } else if !database.IsNotFound(err) {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ candidate := &database.User{ChatID: body.ChatID, Enabled: true}
+ if fields := applyUserPatch(r.Context(), candidate, body.UserPatch); len(fields) > 0 {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid user fields", fields)
+ return
+ }
+ if err := database.CreateUser(r.Context(), body.ChatID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ user, err := database.GetUserByChatID(r.Context(), body.ChatID)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if fields := applyUserPatch(r.Context(), user, body.UserPatch); len(fields) > 0 {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid user fields", fields)
+ return
+ }
+ if err := database.UpdateUser(r.Context(), user); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if err := s.deps.Settings.ReloadConfig(r.Context()); err != nil {
+ log.FromContext(r.Context()).Warnf("Failed to reload settings after user creation: %v", err)
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusCreated, userResponse(user, names))
+}
+
+func (s *server) updateUser(w http.ResponseWriter, r *http.Request) {
+ id64, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid user id")
+ return
+ }
+ user, err := database.GetUserByID(r.Context(), uint(id64))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "user_not_found", "user not found")
+ return
+ }
+ var body UserPatch
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&body); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ if fields := applyUserPatch(r.Context(), user, body); len(fields) > 0 {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid user fields", fields)
+ return
+ }
+ if err := database.UpdateUser(r.Context(), user); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if err := s.deps.Settings.ReloadConfig(r.Context()); err != nil {
+ log.FromContext(r.Context()).Warnf("Failed to reload settings after user update: %v", err)
+ }
+ names, err := storageNamesByID(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, userResponse(user, names))
+}
+
+func (s *server) deleteUser(w http.ResponseWriter, r *http.Request) {
+ id64, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid user id")
+ return
+ }
+ user, err := database.GetUserByID(r.Context(), uint(id64))
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "user_not_found", "user not found")
+ return
+ }
+ if err := database.DeleteUser(r.Context(), user); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if err := s.deps.Settings.ReloadConfig(r.Context()); err != nil {
+ log.FromContext(r.Context()).Warnf("Failed to reload settings after user deletion: %v", err)
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+// storageNamesByID resolves names for both enabled and disabled storage
+// records so references remain readable after a storage is disabled.
+func storageNamesByID(ctx context.Context) (map[uint]string, error) {
+ records, err := database.ListStorageRecords(ctx)
+ if err != nil {
+ return nil, err
+ }
+ names := make(map[uint]string, len(records))
+ for _, record := range records {
+ names[record.ID] = record.Name
+ }
+ return names, nil
+}
+
+func userResponse(user *database.User, namesByID map[uint]string) UserResponse {
+ ids := make([]uint, 0)
+ if user.StorageFilterIDsJSON != "" {
+ if err := json.Unmarshal([]byte(user.StorageFilterIDsJSON), &ids); err != nil {
+ log.Warnf("Invalid storage filter for user %d: %v", user.ChatID, err)
+ }
+ }
+ if ids == nil {
+ ids = make([]uint, 0)
+ }
+ names := make([]string, 0, len(ids))
+ for _, id := range ids {
+ names = append(names, namesByID[id])
+ }
+ return UserResponse{
+ ID: user.ID,
+ ChatID: user.ChatID,
+ Enabled: user.Enabled,
+ Silent: user.Silent,
+ DefaultStorageID: user.DefaultStorageID,
+ DefaultStorageName: namesByID[user.DefaultStorageID],
+ DefaultDirID: user.DefaultDirID,
+ ApplyRule: user.ApplyRule,
+ FilenameStrategy: user.FilenameStrategy,
+ FilenameTemplate: user.FilenameTemplate,
+ ConflictStrategy: user.ConflictStrategy,
+ WatchNotify: user.WatchNotify,
+ StorageFilter: UserStorageFilter{StorageIDs: ids, StorageNames: names, Mode: user.StorageFilterMode.Normalize()},
+ }
+}
+
+func applyUserPatch(ctx context.Context, user *database.User, patch UserPatch) map[string]string {
+ fields := make(map[string]string)
+ if patch.Enabled != nil {
+ user.Enabled = *patch.Enabled
+ }
+ if patch.Silent != nil {
+ user.Silent = *patch.Silent
+ }
+ if patch.DefaultStorageID != nil {
+ if *patch.DefaultStorageID != 0 {
+ if _, err := database.GetStorageRecord(ctx, *patch.DefaultStorageID); err != nil {
+ fields["default_storage_id"] = "unknown storage"
+ }
+ }
+ if _, exists := fields["default_storage_id"]; !exists {
+ user.DefaultStorageID = *patch.DefaultStorageID
+ }
+ }
+ if patch.DefaultDirID != nil {
+ user.DefaultDirID = *patch.DefaultDirID
+ }
+ if patch.ApplyRule != nil {
+ user.ApplyRule = *patch.ApplyRule
+ }
+ if patch.FilenameStrategy != nil {
+ user.FilenameStrategy = *patch.FilenameStrategy
+ }
+ if patch.FilenameTemplate != nil {
+ user.FilenameTemplate = *patch.FilenameTemplate
+ }
+ if patch.ConflictStrategy != nil {
+ user.ConflictStrategy = *patch.ConflictStrategy
+ }
+ if patch.WatchNotify != nil {
+ user.WatchNotify = *patch.WatchNotify
+ }
+ if patch.StorageFilter != nil {
+ for _, id := range patch.StorageFilter.StorageIDs {
+ if _, err := database.GetStorageRecord(ctx, id); err != nil {
+ fields["storage_filter.storage_ids"] = "unknown storage"
+ break
+ }
+ }
+ mode := patch.StorageFilter.Mode
+ if mode == "" {
+ mode = storagefilter.Blacklist
+ } else if !mode.Valid() {
+ fields["storage_filter.mode"] = "must be blacklist or whitelist"
+ }
+ storageIDs := patch.StorageFilter.StorageIDs
+ if storageIDs == nil {
+ storageIDs = make([]uint, 0)
+ }
+ raw, err := json.Marshal(storageIDs)
+ if err != nil {
+ fields["storage_filter.storage_ids"] = err.Error()
+ } else {
+ user.StorageFilterIDsJSON = string(raw)
+ user.StorageFilterMode = mode
+ }
+ }
+ return fields
+}
diff --git a/api/webhook.go b/api/webhook.go
index fed12f91..0f90df28 100644
--- a/api/webhook.go
+++ b/api/webhook.go
@@ -87,16 +87,17 @@ func SendWebhook(ctx context.Context, payload *WebhookPayload) {
}()
}
-func CreateWebhookPayload(taskID string, taskType string, status TaskStatus, storage, path string, err error) *WebhookPayload {
+func CreateWebhookPayload(taskID string, taskType string, status TaskStatus, storageID uint, storage, path string, err error) *WebhookPayload {
payload := &WebhookPayload{
- TaskID: taskID,
- Type: taskType,
- Status: status,
- Storage: storage,
- Path: path,
+ TaskID: taskID,
+ Type: taskType,
+ Status: status,
+ StorageID: storageID,
+ StorageName: storage,
+ Path: path,
}
- if status == TaskStatusCompleted || status == TaskStatusFailed {
+ if status.IsTerminal() && status != TaskStatusCancelled {
now := time.Now()
payload.CompletedAt = &now
}
diff --git a/api/workers.go b/api/workers.go
new file mode 100644
index 00000000..7ee5d7f1
--- /dev/null
+++ b/api/workers.go
@@ -0,0 +1,237 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+
+ "github.com/krau/SaveAny-Bot/client/worker"
+ "github.com/krau/SaveAny-Bot/database"
+)
+
+const maskedWorkerToken = "__MASKED__"
+
+type BotWorkerInput struct {
+ Name string `json:"name"`
+ Token string `json:"token"`
+ Enabled bool `json:"enabled"`
+}
+
+type BotWorkerView struct {
+ ID uint `json:"id"`
+ Name string `json:"name"`
+ Enabled bool `json:"enabled"`
+ SessionPath string `json:"session_path,omitempty"`
+ TokenSet bool `json:"token_set"`
+ State string `json:"state"`
+ Username string `json:"username,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+func botWorkerView(record *database.BotWorker) BotWorkerView {
+ status := worker.StatusFor(record.ID)
+ return BotWorkerView{
+ ID: record.ID,
+ Name: record.Name,
+ Enabled: record.Enabled,
+ SessionPath: record.SessionPath,
+ TokenSet: record.Token != "",
+ State: status.State,
+ Username: status.Username,
+ Error: status.Error,
+ }
+}
+
+func (s *server) listBotWorkers(w http.ResponseWriter, r *http.Request) {
+ records, err := database.ListBotWorkers(r.Context())
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ out := make([]BotWorkerView, 0, len(records))
+ for i := range records {
+ out = append(out, botWorkerView(&records[i]))
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"workers": out})
+}
+
+func (s *server) createBotWorker(w http.ResponseWriter, r *http.Request) {
+ var in BotWorkerInput
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&in); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ name := strings.TrimSpace(in.Name)
+ token := strings.TrimSpace(in.Token)
+ if name == "" || len([]rune(name)) > 64 {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid helper bot name", map[string]string{"name": "required, max 64 characters"})
+ return
+ }
+ if token == "" {
+ WriteFieldErrors(w, http.StatusBadRequest, "token is required", map[string]string{"token": "required"})
+ return
+ }
+ sessionPath := worker.NewSessionPath(s.deps.DataDir)
+ if err := os.MkdirAll(filepath.Dir(sessionPath), 0o755); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ record := &database.BotWorker{
+ Name: name,
+ Token: token,
+ SessionPath: sessionPath,
+ Enabled: in.Enabled,
+ }
+ if err := database.CreateBotWorker(r.Context(), record); err != nil {
+ WriteError(w, http.StatusConflict, "worker_create_failed", err.Error())
+ return
+ }
+ if record.Enabled {
+ if _, err := worker.StartWorker(s.deps.Ctx, record.ID); err != nil {
+ WriteError(w, http.StatusInternalServerError, "worker_start_failed", err.Error())
+ return
+ }
+ }
+ WriteJSON(w, http.StatusCreated, botWorkerView(record))
+}
+
+func (s *server) updateBotWorker(w http.ResponseWriter, r *http.Request) {
+ id, ok := botWorkerID(w, r)
+ if !ok {
+ return
+ }
+ record, err := database.GetBotWorker(r.Context(), id)
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "worker_not_found", "helper bot not found")
+ return
+ }
+ var in BotWorkerInput
+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&in); err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
+ return
+ }
+ name := strings.TrimSpace(in.Name)
+ if name == "" || len([]rune(name)) > 64 {
+ WriteFieldErrors(w, http.StatusBadRequest, "invalid helper bot name", map[string]string{"name": "required, max 64 characters"})
+ return
+ }
+ fields := map[string]any{"name": name, "enabled": in.Enabled}
+ if token := strings.TrimSpace(in.Token); token != "" && token != maskedWorkerToken && token != record.Token {
+ worker.StopWorker(id)
+ if err := worker.RemoveSession(record.SessionPath); err != nil {
+ WriteError(w, http.StatusInternalServerError, "worker_session_cleanup_failed", err.Error())
+ return
+ }
+ sessionPath := worker.NewSessionPath(s.deps.DataDir)
+ if err := os.MkdirAll(filepath.Dir(sessionPath), 0o755); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ fields["token"] = token
+ fields["session_path"] = sessionPath
+ }
+ if err := database.UpdateBotWorker(r.Context(), id, fields); err != nil {
+ WriteError(w, http.StatusConflict, "worker_update_failed", err.Error())
+ return
+ }
+ record, err = database.GetBotWorker(r.Context(), id)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if record.Enabled {
+ if _, err := worker.StartWorker(s.deps.Ctx, id); err != nil {
+ WriteError(w, http.StatusInternalServerError, "worker_start_failed", err.Error())
+ return
+ }
+ } else {
+ worker.StopWorker(id)
+ }
+ WriteJSON(w, http.StatusOK, botWorkerView(record))
+}
+
+func (s *server) deleteBotWorker(w http.ResponseWriter, r *http.Request) {
+ id, ok := botWorkerID(w, r)
+ if !ok {
+ return
+ }
+ record, err := database.GetBotWorker(r.Context(), id)
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "worker_not_found", "helper bot not found")
+ return
+ }
+ worker.StopWorker(id)
+ if err := database.DeleteBotWorker(r.Context(), id); err != nil {
+ WriteError(w, http.StatusInternalServerError, "internal_error", err.Error())
+ return
+ }
+ if err := worker.RemoveSession(record.SessionPath); err != nil {
+ WriteError(w, http.StatusInternalServerError, "worker_session_cleanup_failed", err.Error())
+ return
+ }
+ w.WriteHeader(http.StatusNoContent)
+}
+
+func (s *server) startBotWorker(w http.ResponseWriter, r *http.Request) {
+ id, ok := botWorkerID(w, r)
+ if !ok {
+ return
+ }
+ record, err := database.GetBotWorker(r.Context(), id)
+ if err != nil {
+ WriteError(w, http.StatusNotFound, "worker_not_found", "helper bot not found")
+ return
+ }
+ if !record.Enabled {
+ WriteError(w, http.StatusBadRequest, "worker_disabled", "helper bot is disabled")
+ return
+ }
+ status, err := worker.StartWorker(s.deps.Ctx, id)
+ if err != nil {
+ WriteError(w, http.StatusInternalServerError, "worker_start_failed", err.Error())
+ return
+ }
+ WriteJSON(w, http.StatusOK, status)
+}
+
+func (s *server) stopBotWorker(w http.ResponseWriter, r *http.Request) {
+ id, ok := botWorkerID(w, r)
+ if !ok {
+ return
+ }
+ if _, err := database.GetBotWorker(r.Context(), id); err != nil {
+ WriteError(w, http.StatusNotFound, "worker_not_found", "helper bot not found")
+ return
+ }
+ worker.StopWorker(id)
+ WriteJSON(w, http.StatusOK, worker.StatusFor(id))
+}
+
+func (s *server) testBotWorker(w http.ResponseWriter, r *http.Request) {
+ id, ok := botWorkerID(w, r)
+ if !ok {
+ return
+ }
+ if _, err := database.GetBotWorker(r.Context(), id); err != nil {
+ WriteError(w, http.StatusNotFound, "worker_not_found", "helper bot not found")
+ return
+ }
+ status := worker.StatusFor(id)
+ if status.State != worker.StateRunning {
+ WriteJSON(w, http.StatusOK, map[string]any{"ok": false, "error": status.Error, "state": status.State})
+ return
+ }
+ WriteJSON(w, http.StatusOK, map[string]any{"ok": true, "username": status.Username})
+}
+
+func botWorkerID(w http.ResponseWriter, r *http.Request) (uint, bool) {
+ id64, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
+ if err != nil {
+ WriteError(w, http.StatusBadRequest, "invalid_request", "invalid helper bot id")
+ return 0, false
+ }
+ return uint(id64), true
+}
diff --git a/app/app.go b/app/app.go
new file mode 100644
index 00000000..03b36b1b
--- /dev/null
+++ b/app/app.go
@@ -0,0 +1,211 @@
+package app
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/mygotg/ext"
+
+ "github.com/krau/SaveAny-Bot/api"
+ "github.com/krau/SaveAny-Bot/client/bot"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers"
+ "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/client/worker"
+ "github.com/krau/SaveAny-Bot/common/selfupdate"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/core/resume"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/parsers"
+ "github.com/krau/SaveAny-Bot/settings"
+ "github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/SaveAny-Bot/web"
+)
+
+type Options struct {
+ DataDir string
+ DBPath string
+ Listen string
+ NoWeb bool
+}
+
+type App struct {
+ ctx context.Context
+ cancel context.CancelFunc
+ svc *settings.Service
+ opts Options
+ web *web.Server
+
+ applyMu sync.Mutex
+ mu sync.Mutex
+ resumeDone chan struct{}
+ appliedBotCred string
+ appliedUserbotSession string
+ flowMu sync.Mutex
+ flow *loginFlow
+}
+
+func New(parent context.Context, svc *settings.Service, opts Options) *App {
+ ctx, cancel := context.WithCancel(parent)
+ return &App{ctx: ctx, cancel: cancel, svc: svc, opts: opts}
+}
+
+func (a *App) Done() <-chan struct{} { return a.ctx.Done() }
+
+func (a *App) Start() error {
+ bot.SetRestartHandler(a.RequestRestart)
+ if err := database.MarkInterruptedTaskRecords(a.ctx); err != nil {
+ log.Errorf("Failed to mark interrupted tasks: %v", err)
+ }
+ worker.Init(a.ctx, a.opts.DataDir)
+ core.Run(a.ctx)
+
+ cfg := a.svc.Config()
+ if cfg.Parser.PluginEnable {
+ dirs := cfg.Parser.PluginDirs
+ if len(dirs) == 0 {
+ dirs = []string{"plugins"}
+ }
+ for _, dir := range dirs {
+ if err := parsers.LoadPlugins(a.ctx, dir); err != nil {
+ log.Error("Failed to load parser plugins", "dir", dir, "error", err)
+ }
+ }
+ }
+ go handlers.ListenUserbotMediaEvents(a.ctx, func() *ext.Context {
+ if botCtx := bot.ExtContext(); botCtx != nil {
+ return botCtx
+ }
+ return user.GetCtx()
+ })
+
+ if !a.opts.NoWeb && cfg.Web.Enable {
+ webToken := strings.TrimSpace(cfg.Web.APIToken)
+ if webToken == "" {
+ webToken = config.GenerateToken()
+ }
+ a.web = web.New(a.opts.Listen, webToken, api.NewHandler(api.Deps{
+ Ctx: a.ctx,
+ Settings: a.svc,
+ Telegram: a,
+ DataDir: a.opts.DataDir,
+ DBPath: a.opts.DBPath,
+ WebToken: webToken,
+ }))
+ if err := a.web.Start(a.ctx); err != nil {
+ return err
+ }
+ }
+
+ go func() {
+ if err := a.ApplyTelegram(a.ctx); err != nil {
+ log.Errorf("Failed to apply telegram settings: %v", err)
+ }
+ }()
+ a.resumeDone = make(chan struct{})
+ go func() {
+ defer close(a.resumeDone)
+ resume.Run(a.ctx)
+ }()
+ return nil
+}
+
+func (a *App) ApplyTelegram(ctx context.Context) error {
+ a.applyMu.Lock()
+ defer a.applyMu.Unlock()
+ cfg := a.svc.Config()
+ cred := strings.Join([]string{
+ cfg.Telegram.Token,
+ fmt.Sprintf("%d", cfg.Telegram.AppID),
+ fmt.Sprintf("%d", cfg.Telegram.RpcRetry),
+ cfg.Telegram.AppHash,
+ fmt.Sprintf("%t", cfg.Telegram.Proxy.Enable),
+ cfg.Telegram.Proxy.URL,
+ }, "|")
+
+ userbotSession := cfg.Telegram.Userbot.Session
+ a.mu.Lock()
+ changed := cred != a.appliedBotCred
+ userbotSessionChanged := userbotSession != a.appliedUserbotSession
+ a.appliedBotCred = cred
+ a.appliedUserbotSession = userbotSession
+ a.mu.Unlock()
+
+ botStatus := bot.CurrentStatus()
+ switch {
+ case cfg.Telegram.Token == "":
+ if botStatus.State != "stopped" {
+ bot.Stop()
+ }
+ case changed:
+ bot.Stop()
+ go func() {
+ if err := bot.Start(a.ctx); err != nil {
+ log.Errorf("Failed to start bot: %v", err)
+ }
+ }()
+ case botStatus.State == "stopped" || botStatus.State == "error":
+ go func() {
+ if err := bot.Start(a.ctx); err != nil {
+ log.Errorf("Failed to start bot: %v", err)
+ }
+ }()
+ }
+
+ userStatus := user.CurrentStatus()
+ if userbotSessionChanged && (userStatus.State == "running" || userStatus.State == "starting") {
+ user.Stop()
+ userStatus = user.CurrentStatus()
+ }
+ if !cfg.Telegram.Userbot.Enable {
+ if userStatus.State == "running" || userStatus.State == "starting" {
+ user.Stop()
+ }
+ } else if userStatus.State == "stopped" || userStatus.State == "error" || userStatus.State == "login_required" {
+ go func() {
+ if err := user.Start(a.ctx, nil, cfg.Telegram.Userbot.Phone); err != nil && !errors.Is(err, user.ErrLoginRequired) {
+ log.Errorf("Failed to start userbot: %v", err)
+ }
+ }()
+ }
+ return nil
+}
+
+func (a *App) RequestRestart() {
+ selfupdate.RequestRestart()
+ a.cancel()
+}
+
+func (a *App) Stop() {
+ a.cancel()
+ if a.resumeDone != nil {
+ select {
+ case <-a.resumeDone:
+ case <-time.After(5 * time.Second):
+ log.Warn("Timed out waiting for queued task recovery to stop")
+ }
+ }
+ if a.web != nil {
+ stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ if err := a.web.Stop(stopCtx); err != nil {
+ log.Warnf("Failed to stop web server: %v", err)
+ }
+ cancel()
+ }
+ bot.Stop()
+ user.Stop()
+ worker.StopAll()
+ core.Close()
+ if !core.WaitWorkers(10 * time.Second) {
+ log.Warn("Timed out waiting for task workers to stop")
+ }
+ storage.CloseAll()
+ if err := database.Close(); err != nil {
+ log.Warnf("Failed to close database: %v", err)
+ }
+}
diff --git a/app/telegram.go b/app/telegram.go
new file mode 100644
index 00000000..a9e73458
--- /dev/null
+++ b/app/telegram.go
@@ -0,0 +1,222 @@
+package app
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strings"
+
+ "github.com/charmbracelet/log"
+ "github.com/rs/xid"
+
+ "github.com/krau/SaveAny-Bot/api"
+ "github.com/krau/SaveAny-Bot/client/bot"
+ "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/settings"
+)
+
+func (a *App) Status(ctx context.Context) api.TelegramStatus {
+ botStatus := bot.CurrentStatus()
+ userStatus := user.CurrentStatus()
+ cfg := a.svc.Config()
+
+ status := api.TelegramStatus{
+ Bot: api.BotStatus{
+ State: botStatus.State,
+ ID: botStatus.ID,
+ Username: botStatus.Username,
+ Error: botStatus.Error,
+ StartedAt: botStatus.StartedAt,
+ },
+ Userbot: api.UserbotStatus{
+ Enabled: cfg.Telegram.Userbot.Enable,
+ State: userStatus.State,
+ ID: userStatus.ID,
+ Username: userStatus.Username,
+ Phone: userStatus.Phone,
+ Error: userStatus.Error,
+ },
+ }
+ a.flowMu.Lock()
+ flow := a.flow
+ a.flowMu.Unlock()
+ if flow != nil {
+ state, errMsg := flow.status()
+ status.Userbot.Login = &api.LoginFlow{FlowID: flow.id, State: state, Error: errMsg}
+ }
+ return status
+}
+
+func (a *App) StartBot(ctx context.Context) error {
+ if strings.TrimSpace(a.svc.Config().Telegram.Token) == "" {
+ return errors.New("bot token is not configured")
+ }
+ if bot.CurrentStatus().State == "running" || bot.CurrentStatus().State == "starting" {
+ return nil
+ }
+ go func() {
+ if err := bot.Start(a.ctx); err != nil {
+ log.Errorf("Failed to start bot: %v", err)
+ }
+ }()
+ return nil
+}
+
+func (a *App) StopBot(ctx context.Context, force bool) error {
+ if !force && core.ActiveTaskCount() > 0 {
+ return errors.New("tasks are running")
+ }
+ bot.Stop()
+ return nil
+}
+
+func (a *App) RestartBot(ctx context.Context, force bool) error {
+ if !force && core.ActiveTaskCount() > 0 {
+ return errors.New("tasks are running")
+ }
+ bot.Stop()
+ go func() {
+ if err := bot.Start(a.ctx); err != nil {
+ log.Errorf("Failed to restart bot: %v", err)
+ }
+ }()
+ return nil
+}
+
+func (a *App) StartUserbotLogin(ctx context.Context, phone string) (string, error) {
+ if phone == "" {
+ return "", errors.New("phone number is required")
+ }
+ if user.CurrentStatus().State == "running" || user.CurrentStatus().State == "starting" {
+ return "", errors.New("userbot is already running")
+ }
+ cfg := a.svc.Config()
+ if cfg.Telegram.AppID == 0 || cfg.Telegram.AppHash == "" {
+ return "", errors.New("telegram app_id and app_hash are required")
+ }
+
+ a.flowMu.Lock()
+ if a.flow != nil {
+ state, _ := a.flow.status()
+ if state != "failed" && state != "cancelled" && state != "done" {
+ a.flowMu.Unlock()
+ return "", errors.New("a login flow is already active")
+ }
+ }
+ flow := newLoginFlow(a.ctx, xid.New().String(), phone)
+ a.flow = flow
+ a.flowMu.Unlock()
+
+ go func() {
+ err := user.Start(a.ctx, &webConversator{flow: flow}, phone)
+ if err != nil {
+ flow.markFailed(err)
+ return
+ }
+ if flow.ctx.Err() != nil {
+ user.Stop()
+ return
+ }
+ patch, err := json.Marshal(map[string]any{
+ "userbot": map[string]any{"enable": true, "phone": phone},
+ })
+ if err != nil {
+ flow.markFailed(err)
+ user.Stop()
+ return
+ }
+ if _, err := a.svc.UpdateGroup(a.ctx, settings.GroupTelegram, patch); err != nil {
+ flow.markFailed(err)
+ user.Stop()
+ return
+ }
+ if !flow.markDone() {
+ user.Stop()
+ disable, err := json.Marshal(map[string]any{
+ "userbot": map[string]any{"enable": false},
+ })
+ if err != nil {
+ log.Warnf("Failed to encode userbot disable patch: %v", err)
+ return
+ }
+ if _, err := a.svc.UpdateGroup(a.ctx, settings.GroupTelegram, disable); err != nil {
+ log.Warnf("Failed to disable userbot after login flow ended: %v", err)
+ }
+ return
+ }
+ flow.cancel()
+ }()
+ return flow.id, nil
+}
+
+func (a *App) SubmitUserbotCode(ctx context.Context, flowID, code string) error {
+ flow, err := a.getFlow(flowID)
+ if err != nil {
+ return err
+ }
+ return flow.submitCode(code)
+}
+
+func (a *App) SubmitUserbotPassword(ctx context.Context, flowID, password string) error {
+ flow, err := a.getFlow(flowID)
+ if err != nil {
+ return err
+ }
+ return flow.submitPassword(password)
+}
+
+func (a *App) CancelUserbotLogin(ctx context.Context, flowID string) error {
+ flow, err := a.getFlow(flowID)
+ if err != nil {
+ return err
+ }
+ if state, _ := flow.status(); state == "done" {
+ return errors.New("login already completed")
+ }
+ flow.cancel()
+ flow.setState("cancelled", "")
+ user.Stop()
+ a.flowMu.Lock()
+ if a.flow == flow {
+ a.flow = nil
+ }
+ a.flowMu.Unlock()
+ return nil
+}
+
+func (a *App) LogoutUserbot(ctx context.Context) error {
+ a.flowMu.Lock()
+ if a.flow != nil {
+ a.flow.cancel()
+ a.flow = nil
+ }
+ a.flowMu.Unlock()
+ if err := user.Logout(); err != nil {
+ return err
+ }
+ patch, err := json.Marshal(map[string]any{
+ "userbot": map[string]any{"enable": false},
+ })
+ if err != nil {
+ return fmt.Errorf("encode userbot logout settings: %w", err)
+ }
+ if _, err := a.svc.UpdateGroup(ctx, settings.GroupTelegram, patch); err != nil {
+ return fmt.Errorf("failed to persist logout: %w", err)
+ }
+ return nil
+}
+
+func (a *App) RestartProcess() {
+ a.RequestRestart()
+}
+
+func (a *App) getFlow(flowID string) (*loginFlow, error) {
+ a.flowMu.Lock()
+ defer a.flowMu.Unlock()
+ if a.flow == nil || a.flow.id != flowID {
+ return nil, errors.New("unknown login flow")
+ }
+ return a.flow, nil
+}
diff --git a/app/userbot_auth.go b/app/userbot_auth.go
new file mode 100644
index 00000000..7ea779bb
--- /dev/null
+++ b/app/userbot_auth.go
@@ -0,0 +1,140 @@
+package app
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+
+ gotgproto "github.com/krau/mygotg"
+)
+
+type loginFlow struct {
+ id string
+ phone string
+ ctx context.Context
+ cancel context.CancelFunc
+
+ mu sync.Mutex
+ state string
+ errMsg string
+ codeCh chan string
+ pwdCh chan string
+}
+
+func newLoginFlow(parent context.Context, id, phone string) *loginFlow {
+ ctx, cancel := context.WithTimeout(parent, 5*time.Minute)
+ return &loginFlow{
+ id: id,
+ phone: phone,
+ ctx: ctx,
+ cancel: cancel,
+ state: "starting",
+ codeCh: make(chan string, 1),
+ pwdCh: make(chan string, 1),
+ }
+}
+
+func (f *loginFlow) setState(state, errMsg string) {
+ f.mu.Lock()
+ f.state = state
+ f.errMsg = errMsg
+ f.mu.Unlock()
+}
+
+func (f *loginFlow) status() (string, string) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ return f.state, f.errMsg
+}
+
+func (f *loginFlow) markDone() bool {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.state == "cancelled" {
+ return false
+ }
+ f.state = "done"
+ return true
+}
+
+func (f *loginFlow) markFailed(err error) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.state == "cancelled" {
+ return
+ }
+ f.state = "failed"
+ f.errMsg = err.Error()
+}
+
+func (f *loginFlow) submitCode(code string) error {
+ state, _ := f.status()
+ if state != "code_required" {
+ return fmt.Errorf("login flow is not waiting for a code (state %s)", state)
+ }
+ select {
+ case f.codeCh <- code:
+ return nil
+ case <-f.ctx.Done():
+ return f.ctx.Err()
+ }
+}
+
+func (f *loginFlow) submitPassword(password string) error {
+ state, _ := f.status()
+ if state != "password_required" {
+ return fmt.Errorf("login flow is not waiting for a password (state %s)", state)
+ }
+ select {
+ case f.pwdCh <- password:
+ return nil
+ case <-f.ctx.Done():
+ return f.ctx.Err()
+ }
+}
+
+type webConversator struct {
+ flow *loginFlow
+}
+
+func (c *webConversator) AskPhoneNumber() (string, error) { return c.flow.phone, nil }
+
+func (c *webConversator) AskCode() (string, error) {
+ c.flow.setState("code_required", "")
+ select {
+ case <-c.flow.ctx.Done():
+ return "", c.flow.ctx.Err()
+ case code := <-c.flow.codeCh:
+ c.flow.setState("verifying", "")
+ return code, nil
+ }
+}
+
+func (c *webConversator) AskPassword() (string, error) {
+ c.flow.setState("password_required", "")
+ select {
+ case <-c.flow.ctx.Done():
+ return "", c.flow.ctx.Err()
+ case password := <-c.flow.pwdCh:
+ c.flow.setState("verifying", "")
+ return password, nil
+ }
+}
+
+func (c *webConversator) AuthStatus(status gotgproto.AuthStatus) {
+ switch status.Event {
+ case gotgproto.AuthStatusPhoneCodeAsked:
+ c.flow.setState("code_required", "")
+ case gotgproto.AuthStatusPasswordAsked:
+ c.flow.setState("password_required", "")
+ case gotgproto.AuthStatusPhoneCodeRetrial, gotgproto.AuthStatusPasswordRetrial, gotgproto.AuthStatusPhoneRetrial:
+ c.flow.setState("code_required", fmt.Sprintf("incorrect credential, attempts left: %d", status.AttemptsLeft))
+ case gotgproto.AuthStatusPhoneCodeFailed, gotgproto.AuthStatusPasswordFailed:
+ c.flow.setState("failed", "credential verification failed")
+ case gotgproto.AuthStatusSuccess:
+ // The flow is marked done only after the client is fully started and
+ // the enabled flag has been persisted.
+ c.flow.setState("verifying", "")
+ }
+}
diff --git a/boot/boot.go b/boot/boot.go
new file mode 100644
index 00000000..5f9366f8
--- /dev/null
+++ b/boot/boot.go
@@ -0,0 +1,88 @@
+package boot
+
+import (
+ "context"
+ "net/http"
+ "os"
+ "strings"
+
+ "github.com/charmbracelet/log"
+
+ "github.com/krau/SaveAny-Bot/common/cache"
+ "github.com/krau/SaveAny-Bot/common/i18n"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/parsers"
+ "github.com/krau/SaveAny-Bot/settings"
+)
+
+type Result struct {
+ Config config.Config
+ Settings *settings.Service
+ DataDir string
+ DBPath string
+ Listen string
+ NoWeb bool
+}
+
+func Init(ctx context.Context, opts Options) (Result, error) {
+ if err := os.MkdirAll(opts.DataDir, 0o700); err != nil {
+ return Result{}, err
+ }
+ if err := os.Chmod(opts.DataDir, 0o700); err != nil {
+ log.Warnf("Failed to restrict data directory permissions: %v", err)
+ }
+ database.Init(ctx, opts.DBPath)
+ if !settings.LegacyImported(ctx) {
+ if err := settings.ImportLegacy(ctx, opts.ConfigPath, opts.DataDir); err != nil {
+ log.Errorf("Failed to import legacy configuration: %v", err)
+ }
+ }
+ if err := database.MigrateMinioStorageType(ctx); err != nil {
+ log.Errorf("Failed to migrate MinIO storages to S3: %v", err)
+ }
+ svc, err := settings.NewService(ctx, opts.DataDir)
+ if err != nil {
+ return Result{}, err
+ }
+ if tempPath := strings.TrimSpace(svc.Config().Temp.BasePath); tempPath != "" {
+ if err := os.MkdirAll(tempPath, 0o755); err != nil {
+ return Result{}, err
+ }
+ }
+ if opts.Listen != "" {
+ svc.SetListenOverride(opts.Listen)
+ }
+ cfg := svc.Config()
+ if opts.InitWeb && !opts.NoWeb && cfg.Web.Enable {
+ // The generated password is emitted at Warn; force a level that shows it.
+ log.SetLevel(log.InfoLevel)
+ if err := svc.EnsureAdmin(os.Getenv("SAVEANY_ADMIN_PASSWORD")); err != nil {
+ return Result{}, err
+ }
+ }
+ if level, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err == nil {
+ log.SetLevel(level)
+ } else {
+ log.Warn("Invalid log level, using info", "level", cfg.Log.Level)
+ }
+ i18n.Init(cfg.Lang)
+ parsers.Configure()
+ cache.Init()
+ if cfg.Proxy != "" {
+ transport, err := config.ProxyTransport(cfg.Proxy)
+ if err != nil {
+ log.Errorf("Invalid proxy configuration: %v", err)
+ } else {
+ http.DefaultTransport = transport
+ }
+ }
+ return Result{
+ Config: cfg,
+ Settings: svc,
+ DataDir: opts.DataDir,
+ DBPath: opts.DBPath,
+ Listen: cfg.Web.Listen,
+ NoWeb: opts.NoWeb,
+ }, nil
+}
diff --git a/boot/flags.go b/boot/flags.go
new file mode 100644
index 00000000..871c9195
--- /dev/null
+++ b/boot/flags.go
@@ -0,0 +1,73 @@
+package boot
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/charmbracelet/log"
+ "github.com/spf13/cobra"
+)
+
+func RegisterFlags(cmd *cobra.Command) {
+ flags := cmd.PersistentFlags()
+ flags.StringP("config", "c", "", "legacy config file path or URL (imported once)")
+ flags.String("data-dir", "", "data directory (default ./data)")
+ flags.String("db-path", "", "database file path (default /saveany.db)")
+ flags.String("listen", "", "web listen address as host:port (default 127.0.0.1:28080)")
+ flags.Bool("no-web", false, "do not start the web server")
+}
+
+type Options struct {
+ ConfigPath string
+ DataDir string
+ DBPath string
+ Listen string
+ NoWeb bool
+ InitWeb bool
+}
+
+func Parse(cmd *cobra.Command) Options {
+ flags := cmd.Flags()
+ getString := func(name string) string {
+ value, err := flags.GetString(name)
+ if err != nil {
+ log.Warnf("Failed to read --%s: %v", name, err)
+ }
+ return value
+ }
+ getBool := func(name string) bool {
+ value, err := flags.GetBool(name)
+ if err != nil {
+ log.Warnf("Failed to read --%s: %v", name, err)
+ }
+ return value
+ }
+ configPath := getString("config")
+ dataDir := getString("data-dir")
+ dbPath := getString("db-path")
+ listen := getString("listen")
+ noWeb := getBool("no-web")
+
+ if dataDir == "" {
+ dataDir = os.Getenv("SAVEANY_DATA_DIR")
+ }
+ if dataDir == "" {
+ dataDir = "./data"
+ }
+ if dbPath == "" {
+ dbPath = os.Getenv("SAVEANY_DB_PATH")
+ }
+ if dbPath == "" {
+ dbPath = filepath.Join(dataDir, "saveany.db")
+ }
+ if listen == "" {
+ listen = os.Getenv("SAVEANY_LISTEN")
+ }
+ return Options{
+ ConfigPath: configPath,
+ DataDir: dataDir,
+ DBPath: dbPath,
+ Listen: listen,
+ NoWeb: noWeb,
+ }
+}
diff --git a/client/bot/bot.go b/client/bot/bot.go
index 71c1a1bb..d9e04ca3 100644
--- a/client/bot/bot.go
+++ b/client/bot/bot.go
@@ -2,43 +2,99 @@ package bot
import (
"context"
+ "errors"
+ "sync"
"time"
- "github.com/celestix/gotgproto"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
- "github.com/celestix/gotgproto/sessionMaker"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
+ gotgproto "github.com/krau/mygotg"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
+ "github.com/krau/mygotg/session"
+
"github.com/krau/SaveAny-Bot/client/bot/handlers"
"github.com/krau/SaveAny-Bot/client/middleware"
"github.com/krau/SaveAny-Bot/common/i18n"
+ "github.com/krau/SaveAny-Bot/common/selfupdate"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
)
-var ectx *ext.Context
+type Status struct {
+ State string
+ ID int64
+ Username string
+ Error string
+ StartedAt time.Time
+}
+
+type manager struct {
+ mu sync.RWMutex
+ client *gotgproto.Client
+ ectx *ext.Context
+ status Status
+ starting bool
+ generation uint64
+ startCancel context.CancelFunc
+ restartHandler func()
+}
+
+var mgr = &manager{status: Status{State: "stopped"}}
+func SetRestartHandler(fn func()) {
+ mgr.mu.Lock()
+ mgr.restartHandler = fn
+ mgr.mu.Unlock()
+}
+
+func CurrentStatus() Status {
+ mgr.mu.RLock()
+ defer mgr.mu.RUnlock()
+ return mgr.status
+}
+
+// ExtContext returns the bot context, or nil when the bot is not running.
func ExtContext() *ext.Context {
- return ectx
+ mgr.mu.RLock()
+ defer mgr.mu.RUnlock()
+ return mgr.ectx
}
-func Init(ctx context.Context) <-chan struct{} {
- log.FromContext(ctx).Info("Initializing Bot...")
- resultChan := make(chan struct {
+// Start connects the bot using the current configuration. It is idempotent
+// while a client is already running.
+func Start(ctx context.Context) error {
+ mgr.mu.Lock()
+ if mgr.client != nil {
+ mgr.mu.Unlock()
+ return nil
+ }
+ if mgr.starting {
+ mgr.mu.Unlock()
+ return errors.New("bot is already starting")
+ }
+ mgr.starting = true
+ mgr.generation++
+ gen := mgr.generation
+ startCtx, cancelStart := context.WithCancel(ctx)
+ mgr.startCancel = cancelStart
+ mgr.status = Status{State: "starting"}
+ mgr.mu.Unlock()
+
+ logger := log.FromContext(ctx)
+ logger.Info("Initializing Bot...")
+
+ type result struct {
client *gotgproto.Client
err error
- })
- shouldRestart := make(chan struct{})
+ }
+ resultChan := make(chan result, 1)
go func() {
resolver, err := tgutil.NewConfigProxyResolver()
if err != nil {
- resultChan <- struct {
- client *gotgproto.Client
- err error
- }{nil, err}
+ resultChan <- result{nil, err}
return
}
client, err := gotgproto.NewClient(
@@ -46,16 +102,22 @@ func Init(ctx context.Context) <-chan struct{} {
config.C().Telegram.AppHash,
gotgproto.ClientTypeBot(config.C().Telegram.Token),
&gotgproto.ClientOpts{
- Session: sessionMaker.SqlSession(database.GetDialect(config.C().DB.Session)),
- DisableCopyright: true,
- Middlewares: middleware.NewDefaultMiddlewares(ctx, 5*time.Minute),
- Resolver: resolver,
- Context: ctx,
- MaxRetries: config.C().Telegram.RpcRetry,
- AutoFetchReply: true,
+ Session: session.SqlSession(database.GetDialect(config.C().DB.Session)),
+ DisableCopyright: true,
+ DeferUpdateRecovery: true,
+ Middlewares: middleware.NewDefaultMiddlewares(startCtx, 5*time.Minute),
+ Resolver: resolver,
+ Context: startCtx,
+ MaxRetries: config.C().Telegram.RpcRetry,
+ AutoFetchReply: true,
ErrorHandler: func(ctx *ext.Context, u *ext.Update, s string) error {
- if s == "SAVEANTBOT-RESTART" {
- shouldRestart <- struct{}{}
+ if s == selfupdate.RestartSignal {
+ mgr.mu.RLock()
+ handler := mgr.restartHandler
+ mgr.mu.RUnlock()
+ if handler != nil {
+ handler()
+ }
return dispatcher.EndGroups
}
log.FromContext(ctx).Errorf("unhandled error: %s", s)
@@ -63,37 +125,105 @@ func Init(ctx context.Context) <-chan struct{} {
},
},
)
- if err != nil {
- resultChan <- struct {
- client *gotgproto.Client
- err error
- }{nil, err}
- return
- }
- commands := make([]tg.BotCommand, 0, len(handlers.CommandHandlers))
- for _, info := range handlers.CommandHandlers {
- commands = append(commands, tg.BotCommand{Command: info.Cmd, Description: i18n.T(info.Desc)})
- }
- _, err = client.API().BotsSetBotCommands(ctx, &tg.BotsSetBotCommandsRequest{
- Scope: &tg.BotCommandScopeDefault{},
- Commands: commands,
- })
- resultChan <- struct {
- client *gotgproto.Client
- err error
- }{client, err}
+ resultChan <- result{client, err}
}()
+ var res result
select {
- case <-ctx.Done():
- log.FromContext(ctx).Errorf("Bot initialization cancelled: %s", ctx.Err())
- case result := <-resultChan:
- if result.err != nil {
- log.FromContext(ctx).Fatalf("Failed to initialize Bot: %s", result.err)
+ case <-startCtx.Done():
+ mgr.mu.Lock()
+ if mgr.generation == gen {
+ mgr.starting = false
+ mgr.generation++
+ mgr.startCancel = nil
+ mgr.status = Status{State: "stopped"}
+ }
+ mgr.mu.Unlock()
+ return startCtx.Err()
+ case res = <-resultChan:
+ }
+
+ if res.err != nil {
+ mgr.mu.Lock()
+ if mgr.generation == gen {
+ mgr.starting = false
+ mgr.startCancel = nil
+ mgr.status = Status{State: "error", Error: res.err.Error()}
+ }
+ mgr.mu.Unlock()
+ return res.err
+ }
+
+ mgr.mu.RLock()
+ stale := mgr.generation != gen || startCtx.Err() != nil
+ mgr.mu.RUnlock()
+ if stale {
+ res.client.Stop()
+ return errors.New("bot start canceled")
+ }
+
+ ectx := res.client.CreateContext()
+ handlers.Register(res.client.Dispatcher)
+ if err := res.client.StartUpdateRecovery(startCtx); err != nil {
+ res.client.Stop()
+ mgr.mu.Lock()
+ if mgr.generation == gen {
+ mgr.starting = false
+ mgr.startCancel = nil
+ mgr.status = Status{State: "error", Error: err.Error()}
}
- ectx = result.client.CreateContext()
- handlers.Register(result.client.Dispatcher, ectx)
- log.FromContext(ctx).Info("Bot initialization completed.")
+ mgr.mu.Unlock()
+ return err
+ }
+
+ commands := make([]tg.BotCommand, 0, len(handlers.CommandHandlers))
+ for _, info := range handlers.CommandHandlers {
+ commands = append(commands, tg.BotCommand{Command: info.Cmd, Description: i18n.T(info.Desc)})
+ }
+ if _, err := res.client.API().BotsSetBotCommands(startCtx, &tg.BotsSetBotCommandsRequest{
+ Scope: &tg.BotCommandScopeDefault{},
+ Commands: commands,
+ }); err != nil {
+ logger.Errorf("Failed to set bot commands: %v", err)
+ }
+
+ status := Status{State: "running", StartedAt: time.Now()}
+ if res.client.Self != nil {
+ status.ID = res.client.Self.ID
+ status.Username = res.client.Self.Username
+ }
+
+ mgr.mu.Lock()
+ if mgr.generation != gen {
+ mgr.mu.Unlock()
+ res.client.Stop()
+ return errors.New("bot start canceled")
+ }
+ mgr.client = res.client
+ mgr.ectx = ectx
+ mgr.starting = false
+ mgr.startCancel = nil
+ mgr.status = status
+ mgr.mu.Unlock()
+
+ logger.Info("Bot initialization completed.")
+ return nil
+}
+
+func Stop() {
+ mgr.mu.Lock()
+ client := mgr.client
+ mgr.client = nil
+ mgr.ectx = nil
+ mgr.starting = false
+ if mgr.startCancel != nil {
+ mgr.startCancel()
+ mgr.startCancel = nil
+ }
+ mgr.generation++
+ mgr.status = Status{State: "stopped"}
+ mgr.mu.Unlock()
+ if client != nil {
+ client.Stop()
}
- return shouldRestart
}
diff --git a/client/bot/handlers/add_task.go b/client/bot/handlers/add_task.go
index e8338a2f..ecdcf303 100644
--- a/client/bot/handlers/add_task.go
+++ b/client/bot/handlers/add_task.go
@@ -6,8 +6,6 @@ import (
"path"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
@@ -19,6 +17,8 @@ import (
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"gorm.io/gorm"
)
@@ -36,7 +36,7 @@ func handleAddCallback(ctx *ext.Context, update *ext.Update) error {
msgID := update.CallbackQuery.GetMsgID()
userID := update.CallbackQuery.GetUserID()
- selectedStorage, err := storage.GetStorageByUserIDAndName(ctx, userID, data.SelectedStorName)
+ selectedStorage, err := storage.GetStorageByUserIDAndID(ctx, userID, data.SelectedStorageID)
if err != nil {
log.FromContext(ctx).Errorf("Failed to get storage: %s", err)
ctx.AnswerCallback(msgelem.AlertCallbackAnswer(queryID, i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
@@ -44,7 +44,7 @@ func handleAddCallback(ctx *ext.Context, update *ext.Update) error {
})))
return dispatcher.EndGroups
}
- dirs, err := database.GetDirsByUserChatIDAndStorageName(ctx, userID, data.SelectedStorName)
+ dirs, err := database.GetDirsByUserChatIDAndStorageID(ctx, userID, storage.ID(selectedStorage))
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("failed to get user directories: %w", err)
}
@@ -58,7 +58,7 @@ func handleAddCallback(ctx *ext.Context, update *ext.Update) error {
})))
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgCommonPromptSelectDir, nil),
ReplyMarkup: markup,
@@ -83,31 +83,31 @@ func handleAddCallback(ctx *ext.Context, update *ext.Update) error {
switch data.TaskType {
case tasktype.TaskTypeTgfiles:
if data.AsBatch {
- return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userID, selectedStorage, dirPath, data.Files, msgID, data.ConflictStrategy)
+ return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userID, chatID(update), selectedStorage, dirPath, data.Files, msgID, data.ConflictStrategy)
}
- return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userID, selectedStorage, dirPath, data.Files[0], msgID, data.ConflictStrategy)
+ return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userID, chatID(update), selectedStorage, dirPath, data.Files[0], msgID, data.ConflictStrategy)
case tasktype.TaskTypeTphpics:
- return shortcut.CreateAndAddtelegraphWithEdit(ctx, userID, data.TphPageNode, data.TphDirPath, data.TphPics, selectedStorage, msgID)
+ return shortcut.CreateAndAddtelegraphWithEdit(ctx, chatID(update), data.TphPageNode, data.TphDirPath, data.TphPics, selectedStorage, msgID)
case tasktype.TaskTypeParseditem:
if len(data.ParsedItem.Resources) > 1 {
dirPath = path.Join(dirPath, fsutil.NormalizePathname(data.ParsedItem.Title))
}
- shortcut.CreateAndAddParsedTaskWithEdit(ctx, selectedStorage, dirPath, data.ParsedItem, msgID, userID)
+ shortcut.CreateAndAddParsedTaskWithEdit(ctx, selectedStorage, dirPath, data.ParsedItem, msgID, chatID(update))
case tasktype.TaskTypeDirectlinks:
- shortcut.CreateAndAddDirectTaskWithEdit(ctx, selectedStorage, dirPath, data.DirectLinks, msgID, userID)
+ shortcut.CreateAndAddDirectTaskWithEdit(ctx, selectedStorage, dirPath, data.DirectLinks, msgID, chatID(update))
case tasktype.TaskTypeAria2:
- client := GetAria2Client()
- if client == nil {
+ client, err := GetAria2Client()
+ if err != nil {
ctx.AnswerCallback(msgelem.AlertCallbackAnswer(queryID, i18n.T(i18nk.BotMsgAria2ErrorAria2ClientInitFailed, map[string]any{
- "Error": "aria2 client not initialized",
+ "Error": err.Error(),
})))
return dispatcher.EndGroups
}
- shortcut.CreateAndAddAria2TaskWithEdit(ctx, selectedStorage, dirPath, data.Aria2URIs, client, msgID, userID)
+ shortcut.CreateAndAddAria2TaskWithEdit(ctx, selectedStorage, dirPath, data.Aria2URIs, client, msgID, chatID(update))
case tasktype.TaskTypeYtdlp:
- shortcut.CreateAndAddYtdlpTaskWithEdit(ctx, selectedStorage, dirPath, data.YtdlpURLs, data.YtdlpFlags, msgID, userID)
+ shortcut.CreateAndAddYtdlpTaskWithEdit(ctx, selectedStorage, dirPath, data.YtdlpURLs, data.YtdlpFlags, msgID, chatID(update))
case tasktype.TaskTypeTransfer:
- return handleTransferCallback(ctx, userID, selectedStorage, dirPath, data, msgID)
+ return handleTransferCallback(ctx, userID, chatID(update), selectedStorage, dirPath, data, msgID)
default:
return fmt.Errorf("unexcept task type: %s", data.TaskType)
}
diff --git a/client/bot/handlers/cancel_task.go b/client/bot/handlers/cancel_task.go
index e3a03c1b..eef1b540 100644
--- a/client/bot/handlers/cancel_task.go
+++ b/client/bot/handlers/cancel_task.go
@@ -4,14 +4,14 @@ import (
"fmt"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleCancelCallback(ctx *ext.Context, update *ext.Update) error {
@@ -28,31 +28,10 @@ func handleCancelCallback(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
- ctx.EditMessage(update.CallbackQuery.GetUserID(), &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgCancelInfoCancellingTask, nil),
})
return dispatcher.EndGroups
}
-
-func handleCancelCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- args := strings.Fields(update.EffectiveMessage.Text)
- if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCancelUsage, nil)), nil)
- return dispatcher.EndGroups
- }
- taskID := args[1]
- if err := core.CancelTask(ctx, taskID); err != nil {
- logger.Errorf("failed to cancel task %s: %v", taskID, err)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCancelErrorCancelFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return dispatcher.EndGroups
- }
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCancelInfoCancelRequested, map[string]any{
- "TaskID": taskID,
- })), nil)
- return dispatcher.EndGroups
-}
diff --git a/client/bot/handlers/config.go b/client/bot/handlers/config.go
index c8c363dc..343cb1a8 100644
--- a/client/bot/handlers/config.go
+++ b/client/bot/handlers/config.go
@@ -5,40 +5,52 @@ import (
"strings"
"text/template"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
+ "github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/conflictutil"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/enums/fnamest"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
+ "github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleConfigCmd(ctx *ext.Context, update *ext.Update) error {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgConfigPromptSelectOption)), &ext.ReplyOpts{
- Markup: &tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- &tg.KeyboardButtonCallback{
- Text: i18n.T(i18nk.BotMsgConfigButtonFilenameStrategy),
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeConfig, "fnamest"),
- },
- &tg.KeyboardButtonCallback{
- Text: i18n.T(i18nk.BotMsgConfigButtonConflictStrategy),
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeConfig, "conflictst"),
- },
- },
- },
- },
- },
+ ctx.Reply(update, ext.ReplyTextString(configMenuText("")), &ext.ReplyOpts{
+ Markup: configMenuMarkup(),
})
return dispatcher.EndGroups
}
+func configMenuText(extra string) string {
+ text := i18n.T(i18nk.BotMsgConfigMenuTitle) + "\n\n" + i18n.T(i18nk.BotMsgConfigMenuHint)
+ if extra != "" {
+ text += "\n\n" + extra
+ }
+ return text
+}
+
+func configMenuMarkup() *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonFilenameStrategy), fmt.Appendf(nil, "%s fnamest", tcbdata.TypeConfig)),
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonConflictStrategy), fmt.Appendf(nil, "%s conflictst", tcbdata.TypeConfig)),
+ }},
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonSilent), fmt.Appendf(nil, "%s silent", tcbdata.TypeConfig)),
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonDefaultStorage), fmt.Appendf(nil, "%s storage", tcbdata.TypeConfig)),
+ }},
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonTemplate), fmt.Appendf(nil, "%s template", tcbdata.TypeConfig)),
+ }},
+ }}
+}
+
func handleConfigCallback(ctx *ext.Context, update *ext.Update) error {
args := strings.Fields(string(update.CallbackQuery.Data))
invaildDataAnswer := func() error {
@@ -54,15 +66,101 @@ func handleConfigCallback(ctx *ext.Context, update *ext.Update) error {
return invaildDataAnswer()
}
switch args[1] {
+ case "menu":
+ return handleConfigMenuCallback(ctx, update)
case "fnamest":
return handleConfigFnameSTCallback(ctx, update)
case "conflictst":
return handleConfigConflictSTCallback(ctx, update)
+ case "silent":
+ return handleConfigSilentCallback(ctx, update)
+ case "storage":
+ return handleConfigStorageCallback(ctx, update)
+ case "template":
+ return handleConfigTemplateCallback(ctx, update)
default:
return invaildDataAnswer()
}
}
+func handleConfigMenuCallback(ctx *ext.Context, update *ext.Update) error {
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: configMenuText(""),
+ ReplyMarkup: configMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func handleConfigSilentCallback(ctx *ext.Context, update *ext.Update) error {
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ if !user.Silent && user.DefaultStorageID == 0 {
+ ctx.AnswerCallback(msgelem.AlertCallbackAnswer(update.CallbackQuery.GetQueryID(), i18n.T(i18nk.BotMsgCommonErrorDefaultStorageNotSet)))
+ return dispatcher.EndGroups
+ }
+ user.Silent = !user.Silent
+ if err := database.UpdateUser(ctx, user); err != nil {
+ return err
+ }
+ state := i18n.T(i18nk.BotMsgCommonInfoSilentModeOff)
+ if user.Silent {
+ state = i18n.T(i18nk.BotMsgCommonInfoSilentModeOn)
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: configMenuText(state),
+ ReplyMarkup: configMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func handleConfigStorageCallback(ctx *ext.Context, update *ext.Update) error {
+ userID := update.CallbackQuery.GetUserID()
+ storages := storage.GetUserStorages(ctx, userID)
+ if len(storages) == 0 {
+ ctx.AnswerCallback(msgelem.AlertCallbackAnswer(update.CallbackQuery.GetQueryID(), i18n.T(i18nk.BotMsgCommonErrorNoAvailableStorage)))
+ return dispatcher.EndGroups
+ }
+ markup, err := msgelem.BuildSetDefaultStorageMarkup(ctx, storages)
+ if err != nil {
+ return err
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: i18n.T(i18nk.BotMsgCommonPromptSelectDefaultStorage),
+ ReplyMarkup: markup,
+ })
+ return dispatcher.EndGroups
+}
+
+func handleConfigTemplateCallback(ctx *ext.Context, update *ext.Update) error {
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ text := i18n.T(i18nk.BotMsgConfigTemplateHelp)
+ if user.FilenameTemplate != "" {
+ text += "\n\n" + i18n.T(i18nk.BotMsgConfigInfoCurrentTemplatePrefix, map[string]any{
+ "Template": user.FilenameTemplate,
+ })
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: text,
+ ReplyMarkup: &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{
+ Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgConfigButtonBack), fmt.Appendf(nil, "%s menu", tcbdata.TypeConfig)),
+ },
+ }}},
+ })
+ return dispatcher.EndGroups
+}
+
func handleConfigFnameSTCallback(ctx *ext.Context, update *ext.Update) error {
userID := update.CallbackQuery.GetUserID()
user, err := database.GetUserByChatID(ctx, userID)
@@ -80,7 +178,7 @@ func handleConfigFnameSTCallback(ctx *ext.Context, update *ext.Update) error {
if err := database.UpdateUser(ctx, user); err != nil {
return err
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgConfigInfoFilenameStrategySet, map[string]any{
"Strategy": fnamest.GetDisplay(st, config.C().Lang),
@@ -89,14 +187,14 @@ func handleConfigFnameSTCallback(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
opts := fnamest.FnameSTValues()
- buttons := make([]tg.KeyboardButtonClass, 0, len(opts))
+ buttons := make([]tg.KeyboardInlineButton, 0, len(opts))
for _, opt := range opts {
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: fnamest.GetDisplay(opt, config.C().Lang),
- Data: fmt.Appendf(nil, "%s %s %s", tcbdata.TypeConfig, "fnamest", opt),
- })
+ buttons = append(buttons, markup.Callback(
+ fnamest.GetDisplay(opt, config.C().Lang),
+ fmt.Appendf(nil, "%s %s %s", tcbdata.TypeConfig, "fnamest", opt),
+ ))
}
- markup := &tg.ReplyInlineMarkup{Rows: []tg.KeyboardButtonRow{
+ inlineKeyboard := &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{
{Buttons: buttons},
}}
currentStStr := user.FilenameStrategy
@@ -107,12 +205,12 @@ func handleConfigFnameSTCallback(ctx *ext.Context, update *ext.Update) error {
if err != nil {
currentSt = fnamest.Default
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgConfigPromptSelectFilenameStrategy, map[string]any{
"Strategy": fnamest.GetDisplay(currentSt, config.C().Lang),
}),
- ReplyMarkup: markup,
+ ReplyMarkup: inlineKeyboard,
})
return dispatcher.EndGroups
}
@@ -133,7 +231,7 @@ func handleConfigConflictSTCallback(ctx *ext.Context, update *ext.Update) error
if err := database.UpdateUser(ctx, user); err != nil {
return err
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgConfigInfoConflictStrategySet, map[string]any{
"Strategy": conflictutil.Display(selected),
@@ -143,38 +241,38 @@ func handleConfigConflictSTCallback(ctx *ext.Context, update *ext.Update) error
}
opts := tcbdata.ConflictStrategyValues()
- rows := make([]tg.KeyboardButtonRow, 0, len(opts))
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(opts))
for _, opt := range opts {
- rows = append(rows, tg.KeyboardButtonRow{
- Buttons: []tg.KeyboardButtonClass{
- &tg.KeyboardButtonCallback{
- Text: conflictutil.Display(opt),
- Data: fmt.Appendf(nil, "%s %s %s", tcbdata.TypeConfig, "conflictst", opt),
- },
+ rows = append(rows, tg.KeyboardInlineButtonRow{
+ Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(
+ conflictutil.Display(opt),
+ fmt.Appendf(nil, "%s %s %s", tcbdata.TypeConfig, "conflictst", opt),
+ ),
},
})
}
- markup := &tg.ReplyInlineMarkup{Rows: rows}
+ inlineKeyboard := &tg.ReplyInlineMarkup{Rows: rows}
currentSt := conflictutil.EffectiveStrategy(user)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgConfigPromptSelectConflictStrategy, map[string]any{
"Strategy": conflictutil.Display(currentSt),
}),
- ReplyMarkup: markup,
+ ReplyMarkup: inlineKeyboard,
})
return dispatcher.EndGroups
}
-func handleConfigFnameTmpl(ctx *ext.Context, update *ext.Update) error {
- userID := update.GetUserChat().GetID()
+func handleTemplateCmd(ctx *ext.Context, update *ext.Update) error {
+ userID := responsibleUserID(update)
user, err := database.GetUserByChatID(ctx, userID)
if err != nil {
return err
}
args := strings.Fields(string(update.EffectiveMessage.Text))
if len(args) <= 1 {
- text := i18n.T(i18nk.BotMsgConfigFnametmplHelp, nil)
+ text := i18n.T(i18nk.BotMsgConfigTemplateHelp, nil)
if user.FilenameTemplate != "" {
text += "\n\n" + i18n.T(i18nk.BotMsgConfigInfoCurrentTemplatePrefix, map[string]any{
"Template": user.FilenameTemplate,
diff --git a/client/bot/handlers/dir.go b/client/bot/handlers/dir.go
index 85058eb1..3f65bda1 100644
--- a/client/bot/handlers/dir.go
+++ b/client/bot/handlers/dir.go
@@ -1,24 +1,30 @@
package handlers
import (
+ "fmt"
"strconv"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/message/markup"
+ "github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/utils/strutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleDirCmd(ctx *ext.Context, update *ext.Update) error {
logger := log.FromContext(ctx)
- args := strings.Split(update.EffectiveMessage.Text, " ")
- userChatID := update.GetUserChat().GetID()
+ args := strutil.ParseArgsRespectQuotes(update.EffectiveMessage.Text)
+ userChatID := responsibleUserID(update)
dirs, err := database.GetUserDirsByChatID(ctx, userChatID)
if err != nil {
logger.Errorf("Failed to get user directories: %s", err)
@@ -26,37 +32,38 @@ func handleDirCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildDirHelpStyling(dirs)), nil)
+ ctx.Reply(update, ext.ReplyTextString(dirMenuText(dirs)), &ext.ReplyOpts{Markup: dirMenuMarkup(dirs)})
return dispatcher.EndGroups
}
- user, err := database.GetUserByChatID(ctx, update.GetUserChat().GetID())
+ user, err := database.GetUserByChatID(ctx, responsibleUserID(update))
if err != nil {
logger.Errorf("Failed to get user: %s", err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirErrorGetUserFailed)), nil)
return dispatcher.EndGroups
}
switch args[1] {
- case "add":
- // /dir add local1 path/to/dir
+ case "add", "new":
+ // /dirs add local1 path/to/dir
if len(args) < 4 {
ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildDirHelpStyling(dirs)), nil)
return dispatcher.EndGroups
}
- if _, err := storage.GetStorageByUserIDAndName(ctx, user.ChatID, args[2]); err != nil {
+ selectedStorage, err := storage.GetStorageByUserIDAndName(ctx, user.ChatID, args[2])
+ if err != nil {
logger.Errorf("Failed to get storage %q: %s", args[2], err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed,
tgutil.EscapeHTMLTemplateData(map[string]any{"Error": err.Error()}))), nil)
return dispatcher.EndGroups
}
- if err := database.CreateDirForUser(ctx, user.ID, args[2], args[3]); err != nil {
+ if err := database.CreateDirForUser(ctx, user.ID, storage.ID(selectedStorage), args[3]); err != nil {
logger.Errorf("Failed to create directory: %s", err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirErrorCreateDirFailed)), nil)
return dispatcher.EndGroups
}
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirInfoCreateDirSuccess)), nil)
- case "del":
- // /dir del 3
+ case "del", "remove":
+ // /dirs del 3
if len(args) < 3 {
ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildDirHelpStyling(dirs)), nil)
return dispatcher.EndGroups
@@ -66,14 +73,76 @@ func handleDirCmd(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirErrorInvalidDirId)), nil)
return dispatcher.EndGroups
}
- if err := database.DeleteDirByID(ctx, uint(dirID)); err != nil {
+ if err := database.DeleteDirByID(ctx, uint(dirID), user.ID); err != nil {
logger.Errorf("Failed to delete directory: %s", err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirErrorDeleteDirFailed)), nil)
return dispatcher.EndGroups
}
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirInfoDeleteDirSuccess)), nil)
default:
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDirErrorUnknownOperation)), nil)
+ ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildDirHelpStyling(dirs)), nil)
+ }
+ return dispatcher.EndGroups
+}
+
+func dirMenuText(dirs []database.Dir) string {
+ if len(dirs) == 0 {
+ return i18n.T(i18nk.BotMsgDirMenuEmpty) + "\n\n" + i18n.T(i18nk.BotMsgDirMenuHint)
+ }
+ var b strings.Builder
+ b.WriteString(i18n.T(i18nk.BotMsgDirMenuTitle))
+ b.WriteString("\n\n")
+ b.WriteString(i18n.T(i18nk.BotMsgDirMenuHint))
+ for _, dir := range dirs {
+ storageName := config.C().GetStorageNameByID(dir.StorageID)
+ if storageName == "" {
+ storageName = fmt.Sprintf("#%d", dir.StorageID)
+ }
+ fmt.Fprintf(&b, "\n\n#%d %s - %s", dir.ID, storageName, dir.Path)
+ }
+ return b.String()
+}
+
+func dirMenuMarkup(dirs []database.Dir) *tg.ReplyInlineMarkup {
+ if len(dirs) == 0 {
+ return nil
+ }
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(dirs))
+ for _, dir := range dirs {
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgDirButtonDelete), fmt.Appendf(nil, "%s del %d", tcbdata.TypeDir, dir.ID)),
+ }})
+ }
+ return &tg.ReplyInlineMarkup{Rows: rows}
+}
+
+func handleDirCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ if args[1] == "del" && len(args) >= 3 {
+ dirID, err := strconv.Atoi(args[2])
+ if err != nil {
+ return err
+ }
+ if err := database.DeleteDirByID(ctx, uint(dirID), user.ID); err != nil {
+ return err
+ }
+ }
+ dirs, err := database.GetUserDirsByChatID(ctx, userID)
+ if err != nil {
+ return err
}
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: dirMenuText(dirs),
+ ReplyMarkup: dirMenuMarkup(dirs),
+ })
return dispatcher.EndGroups
}
diff --git a/client/bot/handlers/dl.go b/client/bot/handlers/dl.go
deleted file mode 100644
index 29664066..00000000
--- a/client/bot/handlers/dl.go
+++ /dev/null
@@ -1,112 +0,0 @@
-package handlers
-
-import (
- "net/url"
- "strings"
- "sync"
-
- "github.com/celestix/gotgproto/ext"
- "github.com/charmbracelet/log"
- "github.com/duke-git/lancet/v2/slice"
- "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
- "github.com/krau/SaveAny-Bot/common/i18n"
- "github.com/krau/SaveAny-Bot/common/i18n/i18nk"
- "github.com/krau/SaveAny-Bot/config"
- "github.com/krau/SaveAny-Bot/pkg/aria2"
- "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
- "github.com/krau/SaveAny-Bot/pkg/tcbdata"
- "github.com/krau/SaveAny-Bot/storage"
-)
-
-func handleDlCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- args := strings.Split(update.EffectiveMessage.Text, " ")
- if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDlUsage)), nil)
- return nil
- }
- links := args[1:]
- for i, link := range links {
- links[i] = strings.TrimSpace(link)
- u, err := url.Parse(link)
- if err != nil || u.Scheme == "" || u.Host == "" {
- logger.Warn("invaild link", link)
- links[i] = ""
- }
- }
- links = slice.Compact(links)
- if len(links) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDlErrorNoValidLinks)), nil)
- return nil
- }
- markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, update.GetUserChat().GetID()), tcbdata.Add{
- TaskType: tasktype.TaskTypeDirectlinks,
- DirectLinks: links,
- })
- if err != nil {
- return err
- }
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDlInfoFilesSelectStorage, map[string]any{
- "Count": len(links),
- })), &ext.ReplyOpts{
- Markup: markup,
- })
- return nil
-}
-
-var aria2ClientInitOnce sync.Once
-var aria2ClientInitErr error
-var aria2Client *aria2.Client
-
-func GetAria2Client() *aria2.Client {
- return aria2Client
-}
-
-func handleAria2DlCmd(ctx *ext.Context, update *ext.Update) error {
- if !config.C().Aria2.Enable {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgAria2ErrorAria2NotEnabled)), nil)
- return nil
- }
- logger := log.FromContext(ctx)
- args := strings.Split(update.EffectiveMessage.Text, " ")
- if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDlUsage)), nil)
- return nil
- }
- links := args[1:]
- for i, link := range links {
- links[i] = strings.TrimSpace(link)
- }
- links = slice.Compact(links)
- if len(links) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDlErrorNoValidLinks)), nil)
- return nil
- }
- logger.Debug("Preparing aria2 download", "links", links)
-
- // Initialize aria2 client to check connection
- aria2ClientInitOnce.Do(func() {
- aria2Client, aria2ClientInitErr = aria2.NewClient(config.C().Aria2.Url, config.C().Aria2.Secret)
- })
- if aria2ClientInitErr != nil {
- logger.Error("Failed to initialize aria2 client", "error", aria2ClientInitErr)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgAria2ErrorAria2ClientInitFailed, map[string]any{
- "Error": aria2ClientInitErr.Error(),
- })), nil)
- return nil
- }
-
- // don't add to aria2 yet
- markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, update.GetUserChat().GetID()), tcbdata.Add{
- TaskType: tasktype.TaskTypeAria2,
- Aria2URIs: links,
- })
- if err != nil {
- return err
- }
-
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgAria2InfoSelectStorage)), &ext.ReplyOpts{
- Markup: markup,
- })
- return nil
-}
diff --git a/client/bot/handlers/download.go b/client/bot/handlers/download.go
new file mode 100644
index 00000000..f4fa11fc
--- /dev/null
+++ b/client/bot/handlers/download.go
@@ -0,0 +1,191 @@
+package handlers
+
+import (
+ "fmt"
+ "net/url"
+ "strings"
+ "sync"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/message/markup"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
+ "github.com/krau/SaveAny-Bot/common/cache"
+ "github.com/krau/SaveAny-Bot/common/i18n"
+ "github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/aria2"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
+ "github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
+ "github.com/rs/xid"
+)
+
+type downloadOption struct {
+ label i18nk.Key
+ data tcbdata.Add
+}
+
+// handleDownloadCmd parses links once, then lets the user choose the download
+// engine selection.
+func handleDownloadCmd(ctx *ext.Context, update *ext.Update) error {
+ logger := log.FromContext(ctx)
+ args := splitQuotedArgs(update.EffectiveMessage.Text)
+ if len(args) < 2 {
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDownloadUsage)), nil)
+ return dispatcher.EndGroups
+ }
+
+ webLinks, flags := parseYtdlpArgs(logger, args[1:])
+ allLinks := parseDownloadURIs(args[1:], webLinks)
+ if len(webLinks) == 0 && len(allLinks) == 0 {
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDownloadErrorNoValidLinks)), nil)
+ return dispatcher.EndGroups
+ }
+
+ options := make([]downloadOption, 0, 3)
+ if len(webLinks) > 0 && len(flags) == 0 {
+ options = append(options, downloadOption{
+ label: i18nk.BotMsgDownloadButtonDirect,
+ data: tcbdata.Add{
+ TaskType: tasktype.TaskTypeDirectlinks,
+ DirectLinks: webLinks,
+ },
+ })
+ }
+ if config.C().Aria2.Enable && len(allLinks) > 0 {
+ options = append(options, downloadOption{
+ label: i18nk.BotMsgDownloadButtonAria2,
+ data: tcbdata.Add{
+ TaskType: tasktype.TaskTypeAria2,
+ Aria2URIs: allLinks,
+ },
+ })
+ }
+ if len(webLinks) > 0 {
+ options = append(options, downloadOption{
+ label: i18nk.BotMsgDownloadButtonVideo,
+ data: tcbdata.Add{
+ TaskType: tasktype.TaskTypeYtdlp,
+ YtdlpURLs: webLinks,
+ YtdlpFlags: flags,
+ },
+ })
+ }
+ if len(options) == 0 {
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDownloadErrorNoEngine)), nil)
+ return dispatcher.EndGroups
+ }
+
+ buttons := make([]tg.KeyboardInlineButton, 0, len(options))
+ for _, option := range options {
+ dataID := xid.New().String()
+ if err := cache.Set(dataID, option.data); err != nil {
+ return err
+ }
+ buttons = append(buttons, markup.Callback(
+ i18n.T(option.label, nil),
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeEngine, dataID),
+ ))
+ }
+ markup := &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{Buttons: buttons}}}
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgDownloadPromptSelectEngine, map[string]any{
+ "Count": len(allLinks),
+ })), &ext.ReplyOpts{Markup: markup})
+ return dispatcher.EndGroups
+}
+
+func parseDownloadURIs(args, webLinks []string) []string {
+ links := make([]string, 0, len(args))
+ seen := make(map[string]struct{}, len(args))
+ add := func(link string) {
+ if link == "" {
+ return
+ }
+ if _, ok := seen[link]; ok {
+ return
+ }
+ seen[link] = struct{}{}
+ links = append(links, link)
+ }
+ for _, link := range webLinks {
+ add(link)
+ }
+ for _, arg := range args {
+ if arg == "" || strings.HasPrefix(arg, "-") {
+ continue
+ }
+ u, err := url.Parse(arg)
+ if err == nil && u.Scheme != "" {
+ add(arg)
+ }
+ }
+ return links
+}
+
+func handleEngineCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
+ data, err := shortcut.GetCallbackDataWithAnswer[tcbdata.Add](ctx, update, args[1])
+ if err != nil {
+ return err
+ }
+ storages := storage.GetUserStorages(ctx, update.CallbackQuery.GetUserID())
+ if len(storages) == 0 {
+ ctx.AnswerCallback(msgelem.AlertCallbackAnswer(update.CallbackQuery.GetQueryID(), i18n.T(i18nk.BotMsgCommonErrorNoAvailableStorage)))
+ return dispatcher.EndGroups
+ }
+ markup, err := msgelem.BuildAddSelectStorageKeyboard(storages, data)
+ if err != nil {
+ return err
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: i18n.T(i18nk.BotMsgDownloadInfoSelectStorage, map[string]any{"Count": downloadOptionCount(data)}),
+ ReplyMarkup: markup,
+ })
+ return dispatcher.EndGroups
+}
+
+func downloadOptionCount(data tcbdata.Add) int {
+ switch data.TaskType {
+ case tasktype.TaskTypeDirectlinks:
+ return len(data.DirectLinks)
+ case tasktype.TaskTypeAria2:
+ return len(data.Aria2URIs)
+ case tasktype.TaskTypeYtdlp:
+ return len(data.YtdlpURLs)
+ default:
+ return 0
+ }
+}
+
+var aria2ClientCache struct {
+ sync.Mutex
+ key string
+ client *aria2.Client
+}
+
+// GetAria2Client returns a client for the current aria2 settings. The cache is
+// keyed by URL and secret so changing settings does not require a restart.
+func GetAria2Client() (*aria2.Client, error) {
+ cfg := config.C().Aria2
+ key := cfg.Url + "\x00" + cfg.Secret
+ aria2ClientCache.Lock()
+ defer aria2ClientCache.Unlock()
+ if aria2ClientCache.client != nil && aria2ClientCache.key == key {
+ return aria2ClientCache.client, nil
+ }
+ client, err := aria2.NewClient(cfg.Url, cfg.Secret)
+ if err != nil {
+ return nil, err
+ }
+ aria2ClientCache.client = client
+ aria2ClientCache.key = key
+ return client, nil
+}
diff --git a/client/bot/handlers/help.go b/client/bot/handlers/help.go
index 0f18e3bc..2071d235 100644
--- a/client/bot/handlers/help.go
+++ b/client/bot/handlers/help.go
@@ -3,11 +3,11 @@ package handlers
import (
"fmt"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleHelpCmd(ctx *ext.Context, update *ext.Update) error {
diff --git a/client/bot/handlers/link.go b/client/bot/handlers/link.go
index d4369b1e..969cc4a6 100644
--- a/client/bot/handlers/link.go
+++ b/client/bot/handlers/link.go
@@ -1,8 +1,6 @@
package handlers
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/dirutil"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
@@ -11,6 +9,8 @@ import (
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleMessageLink(ctx *ext.Context, update *ext.Update) error {
@@ -19,7 +19,7 @@ func handleMessageLink(ctx *ext.Context, update *ext.Update) error {
return err
}
logger := log.FromContext(ctx)
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
stors := storage.GetUserStorages(ctx, userId)
if len(files) == 1 {
req, err := msgelem.BuildAddOneSelectStorageMessage(ctx, stors, files[0], replied.ID)
@@ -55,9 +55,9 @@ func handleSilentSaveLink(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
if len(files) == 1 {
- return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userId, stor, dirutil.PathFromContext(ctx), files[0], replied.ID)
+ return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userId, chatID(update), stor, dirutil.PathFromContext(ctx), files[0], replied.ID)
}
- return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userId, stor, dirutil.PathFromContext(ctx), files, replied.ID)
+ return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userId, chatID(update), stor, dirutil.PathFromContext(ctx), files, replied.ID)
}
diff --git a/client/bot/handlers/media.go b/client/bot/handlers/media.go
index 6a7e554a..73107d55 100644
--- a/client/bot/handlers/media.go
+++ b/client/bot/handlers/media.go
@@ -1,8 +1,6 @@
package handlers
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/dirutil"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
@@ -12,6 +10,8 @@ import (
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
@@ -22,7 +22,7 @@ func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
return handleGroupMediaMessage(ctx, update, message, groupID)
}
logger.Debugf("Got media: %s", message.Media.TypeName())
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
userDB, err := database.GetUserByChatID(ctx, userId)
if err != nil {
return err
@@ -55,7 +55,7 @@ func handleSilentSaveMedia(ctx *ext.Context, update *ext.Update) error {
return handleGroupMediaMessage(ctx, update, message, groupID)
}
logger.Debugf("Got media: %s", message.Media.TypeName())
- userID := update.GetUserChat().GetID()
+ userID := responsibleUserID(update)
userDB, err := database.GetUserByChatID(ctx, userID)
if err != nil {
return err
@@ -65,5 +65,5 @@ func handleSilentSaveMedia(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userID, stor, dirutil.PathFromContext(ctx), file, msg.ID)
+ return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userID, chatID(update), stor, dirutil.PathFromContext(ctx), file, msg.ID)
}
diff --git a/client/bot/handlers/media_group.go b/client/bot/handlers/media_group.go
index 8230ddd8..9f212367 100644
--- a/client/bot/handlers/media_group.go
+++ b/client/bot/handlers/media_group.go
@@ -4,8 +4,6 @@ import (
"sync"
"time"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
@@ -13,11 +11,14 @@ import (
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/shortcut"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/mediafilter"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
// mediaGroupKey uniquely identifies a media group by chat, sender, and group
@@ -57,11 +58,10 @@ func handleGroupMediaMessage(ctx *ext.Context, update *ext.Update, message *tg.M
mediaGroupHandler.SetupTimeout(max(config.C().Telegram.MediaGroupTimeout, 1))
logger := log.FromContext(ctx)
media := message.Media
- supported := mediautil.IsSupported(media)
- if !supported {
+ if !mediautil.IsSupported(media) || !mediafilter.Allowed(media) {
return dispatcher.EndGroups
}
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
userDB, err := database.GetUserByChatID(ctx, userId)
if err != nil {
return err
@@ -106,7 +106,7 @@ func processMediaGroup(ctx *ext.Context, update *ext.Update, key mediaGroupKey)
}
logger.Debugf("Processing media group %d with %d items", key.groupID, len(items))
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
msg, err := ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgMediaGroupInfoSavingFiles, nil)), nil)
if err != nil {
logger.Errorf("Failed to reply: %s", err)
@@ -116,10 +116,10 @@ func processMediaGroup(ctx *ext.Context, update *ext.Update, key mediaGroupKey)
if stor != nil {
// In silent mode
if len(items) == 1 {
- shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userId, stor, "", items[0], msg.ID)
+ shortcut.CreateAndAddTGFileTaskWithEdit(ctx, userId, key.chatID, stor, "", items[0], msg.ID)
return
}
- shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userId, stor, "", items, msg.ID)
+ shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, userId, key.chatID, stor, "", items, msg.ID)
return
}
@@ -130,7 +130,7 @@ func processMediaGroup(ctx *ext.Context, update *ext.Update, key mediaGroupKey)
})
if err != nil {
logger.Errorf("Failed to build storage selection keyboard: %s", err)
- ctx.EditMessage(userId, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(key.chatID, &tg.MessagesEditMessageRequest{
ID: msg.ID,
Message: i18n.T(i18nk.BotMsgMediaGroupErrorBuildStorageSelectKeyboardFailed, map[string]any{
"Error": err.Error(),
@@ -138,7 +138,7 @@ func processMediaGroup(ctx *ext.Context, update *ext.Update, key mediaGroupKey)
})
return
}
- ctx.EditMessage(userId, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(key.chatID, &tg.MessagesEditMessageRequest{
ID: msg.ID,
Message: i18n.T(i18nk.BotMsgMediaGroupInfoGroupFoundFilesSelectStorage, map[string]any{
"Count": len(items),
diff --git a/client/bot/handlers/middleware.go b/client/bot/handlers/middleware.go
index 678db352..0e3f30f0 100644
--- a/client/bot/handlers/middleware.go
+++ b/client/bot/handlers/middleware.go
@@ -3,25 +3,56 @@ package handlers
import (
"errors"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/duke-git/lancet/v2/slice"
+ "github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/dirutil"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
// responsibleUserID returns the sender's ID. Callback queries carry it
-// natively; message updates resolve it through the entity map.
+// natively; message updates resolve the sender through the entity map, which
+// also covers group and supergroup chats.
func responsibleUserID(u *ext.Update) int64 {
- if u.CallbackQuery != nil {
- return u.CallbackQuery.GetUserID()
+ return tgutil.SenderID(u)
+}
+
+// chatID returns the chat that replies and edits must target. In private chats
+// this equals the sender ID; in groups and supergroups it is the group chat.
+func chatID(u *ext.Update) int64 {
+ return tgutil.ChatID(u)
+}
+
+// isGroupChat reports whether the update belongs to a basic group or a
+// supergroup. Broadcast channels are not group chats.
+func isGroupChat(u *ext.Update) bool {
+ if u == nil {
+ return false
+ }
+ if message := u.EffectiveMessage; message != nil {
+ switch message.PeerID.(type) {
+ case *tg.PeerUser:
+ return false
+ case *tg.PeerChat:
+ return true
+ case *tg.PeerChannel:
+ channel := u.GetChannel()
+ return channel == nil || channel.Megagroup
+ }
+ }
+ if u.GetChat() != nil {
+ return true
}
- return u.GetUserChat().GetID()
+ channel := u.GetChannel()
+ return channel != nil && channel.Megagroup
}
func checkPermission(ctx *ext.Context, update *ext.Update) error {
@@ -29,7 +60,9 @@ func checkPermission(ctx *ext.Context, update *ext.Update) error {
if !slice.Contain(config.C().GetUsersID(), userID) {
if cbq := update.CallbackQuery; cbq != nil {
ctx.AnswerCallback(msgelem.AlertCallbackAnswer(cbq.GetQueryID(), i18n.T(i18nk.BotMsgCommonErrorNoPermission, nil)))
- } else {
+ } else if !isGroupChat(update) {
+ // Stay silent in groups: unaddressed traffic and unauthorized
+ // command attempts must not turn into reply spam.
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorNoPermission, nil)), nil)
}
return dispatcher.EndGroups
@@ -51,9 +84,20 @@ func withPermission(handler func(*ext.Context, *ext.Update) error) func(*ext.Con
}
}
+// skipLinkForMedia lets the media handler save an attached file instead of a
+// link that appears in its caption.
+func skipLinkForMedia(next func(*ext.Context, *ext.Update) error) func(*ext.Context, *ext.Update) error {
+ return func(ctx *ext.Context, update *ext.Update) error {
+ if message := update.EffectiveMessage; message != nil && message.Media != nil && mediautil.IsSupported(message.Media) {
+ return dispatcher.ContinueGroups
+ }
+ return next(ctx, update)
+ }
+}
+
func handleSilentMode(next func(*ext.Context, *ext.Update) error, handler func(*ext.Context, *ext.Update) error) func(*ext.Context, *ext.Update) error {
return func(ctx *ext.Context, update *ext.Update) error {
- userID := update.GetUserChat().GetID()
+ userID := responsibleUserID(update)
user, err := database.GetUserByChatID(ctx, userID)
if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetUserInfoFailed, map[string]any{
@@ -64,28 +108,30 @@ func handleSilentMode(next func(*ext.Context, *ext.Update) error, handler func(*
if !user.Silent {
return next(ctx, update)
}
- if user.DefaultStorage == "" {
+ if user.DefaultStorageID == 0 {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorDefaultStorageNotSet, nil)), nil)
return next(ctx, update)
}
- stor, err := storage.GetStorageByUserIDAndName(ctx, userID, user.DefaultStorage)
+ stor, err := storage.GetStorageByUserIDAndID(ctx, userID, user.DefaultStorageID)
if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
"Error": err.Error(),
})), nil)
return dispatcher.EndGroups
}
- if user.DefaultDir != 0 {
- dir, err := database.GetDirByID(ctx, user.DefaultDir)
+ base := ctx.Context
+ if user.DefaultDirID != 0 {
+ dir, err := database.GetDirByID(ctx, user.DefaultDirID)
if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetDirFailed, map[string]any{
"Error": err.Error(),
})), nil)
return next(ctx, update)
}
- ctx.Context = dirutil.WithContext(ctx.Context, dir)
+ base = dirutil.WithContext(base, dir)
}
- ctx.Context = storage.WithContext(ctx.Context, stor)
- return handler(ctx, update)
+ base = storage.WithContext(base, stor)
+ child := ext.NewContext(base, ctx.Raw, ctx.PeerStorage, ctx.Self, ctx.Sender, ctx.Entities, false)
+ return handler(child, update)
}
}
diff --git a/client/bot/handlers/middleware_test.go b/client/bot/handlers/middleware_test.go
index f752c7bc..09e740ab 100644
--- a/client/bot/handlers/middleware_test.go
+++ b/client/bot/handlers/middleware_test.go
@@ -1,14 +1,12 @@
package handlers
import (
- "os"
- "path/filepath"
"testing"
- "github.com/celestix/gotgproto/ext"
- "github.com/celestix/gotgproto/types"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/mygotg/ext"
+ "github.com/krau/mygotg/types"
)
// Regression: callback queries usually arrive as updateShort without entity
@@ -62,13 +60,9 @@ func TestResponsibleUserID(t *testing.T) {
// it as an error, so every permitted callback was swallowed before the real
// handler ran.
func TestWithPermissionInvokesHandler(t *testing.T) {
- path := filepath.Join(t.TempDir(), "config.toml")
- if err := os.WriteFile(path, []byte("workers = 2\n\n[[users]]\nid = 42\n"), 0o644); err != nil {
- t.Fatal(err)
- }
- if err := config.Init(t.Context(), path); err != nil {
- t.Fatal(err)
- }
+ cfg := config.Default(t.TempDir())
+ cfg.Users = []config.UserConfig{{ID: 42, Enabled: true}}
+ config.Set(cfg)
update := &ext.Update{CallbackQuery: &tg.UpdateBotCallbackQuery{UserID: 42}}
called := false
diff --git a/client/bot/handlers/parse.go b/client/bot/handlers/parse.go
index 907140d8..fbc22816 100644
--- a/client/bot/handlers/parse.go
+++ b/client/bot/handlers/parse.go
@@ -7,8 +7,6 @@ import (
"path"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/dirutil"
@@ -22,6 +20,8 @@ import (
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
@@ -67,7 +67,7 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
return dispatcher.EndGroups
}
logger.Debug("Parsed item from text message", "title", item.Title, "url", item.URL)
- userID := u.GetUserChat().GetID()
+ userID := responsibleUserID(u)
markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, userID), tcbdata.Add{
TaskType: tasktype.TaskTypeParseditem,
ParsedItem: item,
@@ -87,7 +87,7 @@ func handleTextMessage(ctx *ext.Context, u *ext.Update) error {
})), nil)
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(u), &tg.MessagesEditMessageRequest{
Message: text,
ReplyMarkup: markup,
Entities: entities,
@@ -116,7 +116,6 @@ func handleSilentSaveText(ctx *ext.Context, u *ext.Update) error {
return dispatcher.EndGroups
}
logger.Debug("Parsed item from text message", "title", item.Title, "url", item.URL)
- userID := u.GetUserChat().GetID()
text, entities, err := msgelem.BuildParsedTextEntity(*item)
if err != nil {
logger.Errorf("Failed to build parsed text entity: %s", err)
@@ -125,12 +124,12 @@ func handleSilentSaveText(ctx *ext.Context, u *ext.Update) error {
})), nil)
return dispatcher.EndGroups
}
- msg, err := ctx.SendMessage(userID, &tg.MessagesSendMessageRequest{
+ msg, err := ctx.SendMessage(chatID(u), &tg.MessagesSendMessageRequest{
Message: text,
Entities: entities,
ReplyTo: &tg.InputReplyToMessage{
ReplyToMsgID: u.EffectiveMessage.ID,
- ReplyToPeerID: u.GetUserChat().AsInputPeer(),
+ ReplyToPeerID: u.EffectiveChat().GetInputPeer(),
},
})
if err != nil {
@@ -144,5 +143,5 @@ func handleSilentSaveText(ctx *ext.Context, u *ext.Update) error {
if p := dirutil.PathFromContext(ctx); p != "" {
dirPath = path.Join(p, dirPath)
}
- return shortcut.CreateAndAddParsedTaskWithEdit(ctx, stor, dirPath, item, msg.ID, userID)
+ return shortcut.CreateAndAddParsedTaskWithEdit(ctx, stor, dirPath, item, msg.ID, chatID(u))
}
diff --git a/client/bot/handlers/parser.go b/client/bot/handlers/parser.go
index 651e781e..a228ae0a 100644
--- a/client/bot/handlers/parser.go
+++ b/client/bot/handlers/parser.go
@@ -4,13 +4,13 @@ import (
"bytes"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/parsers"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleParserCmd(ctx *ext.Context, u *ext.Update) error {
diff --git a/client/bot/handlers/register.go b/client/bot/handlers/register.go
index 624b8725..c577053f 100644
--- a/client/bot/handlers/register.go
+++ b/client/bot/handlers/register.go
@@ -3,16 +3,15 @@ package handlers
import (
"regexp"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/dispatcher/handlers"
- "github.com/celestix/gotgproto/dispatcher/handlers/filters"
- "github.com/celestix/gotgproto/ext"
+ "github.com/gotd/td/tg"
sabotfilters "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/filters"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/re"
- userclient "github.com/krau/SaveAny-Bot/client/user"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
- "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/dispatcher/handlers"
+ "github.com/krau/mygotg/dispatcher/handlers/filters"
+ "github.com/krau/mygotg/ext"
)
type DescCommandHandler struct {
@@ -23,50 +22,70 @@ type DescCommandHandler struct {
var CommandHandlers = []DescCommandHandler{
{"start", i18nk.BotMsgCmdStart, handleHelpCmd},
- {"silent", i18nk.BotMsgCmdSilent, handleSilentCmd},
- {"storage", i18nk.BotMsgCmdStorage, handleStorageCmd},
- {"dir", i18nk.BotMsgCmdDir, handleDirCmd},
- {"rule", i18nk.BotMsgCmdRule, handleRuleCmd},
{"save", i18nk.BotMsgCmdSave, handleSilentMode(handleSaveCmd, handleSilentSaveReplied)},
- {"dl", i18nk.BotMsgCmdDl, handleDlCmd},
- {"aria2dl", i18nk.BotMsgCmdAria2dl, handleAria2DlCmd},
- {"ytdlp", i18nk.BotMsgCmdYtdlp, handleYtdlpCmd},
+ {"download", i18nk.BotMsgCmdDownload, handleDownloadCmd},
{"transfer", i18nk.BotMsgCmdTransfer, handleTransferCmd},
- {"task", i18nk.BotMsgCmdTask, handleTaskCmd},
- {"cancel", i18nk.BotMsgCmdCancel, handleCancelCmd},
+ {"tasks", i18nk.BotMsgCmdTasks, handleTasksCmd},
{"config", i18nk.BotMsgCmdConfig, handleConfigCmd},
- {"fnametmpl", i18nk.BotMsgCmdFnametmpl, handleConfigFnameTmpl},
- {"help", i18nk.BotMsgCmdHelp, handleHelpCmd},
- {"parser", i18nk.BotMsgCmdParser, handleParserCmd},
+ {"template", i18nk.BotMsgCmdTemplate, handleTemplateCmd},
+ {"dirs", i18nk.BotMsgCmdDirs, handleDirCmd},
+ {"rules", i18nk.BotMsgCmdRules, handleRuleCmd},
{"watch", i18nk.BotMsgCmdWatch, handleWatchCmd},
- {"unwatch", i18nk.BotMsgCmdUnwatch, handleUnwatchCmd},
- {"lswatch", i18nk.BotMsgCmdLswatch, handleLswatchCmd},
- {"syncpeers", i18nk.BotMsgCmdSyncpeers, handleSyncpeersCmd},
+ {"parser", i18nk.BotMsgCmdParser, handleParserCmd},
{"update", i18nk.BotMsgCmdUpdate, handleUpdateCmd},
}
-func Register(disp dispatcher.Dispatcher, botCtx *ext.Context) {
- disp.AddHandler(handlers.NewMessage(filters.Message.ChatType(filters.ChatTypeChannel), func(ctx *ext.Context, u *ext.Update) error {
+// groupGate admits private chats, basic groups, and supergroups. Broadcast
+// channels and updates without a resolvable human sender are rejected.
+func groupGate(_ *ext.Context, u *ext.Update) error {
+ message := u.EffectiveMessage
+ if message == nil || message.Out {
return dispatcher.EndGroups
- }))
- disp.AddHandler(handlers.NewMessage(filters.Message.ChatType(filters.ChatTypeChat), func(ctx *ext.Context, u *ext.Update) error {
+ }
+ switch message.PeerID.(type) {
+ case *tg.PeerUser, *tg.PeerChat:
+ case *tg.PeerChannel:
+ // The channel entity can be absent from short updates. Broadcast
+ // channel posts are rejected by the sender check below because their
+ // sender is the channel, not a user.
+ default:
return dispatcher.EndGroups
- }))
+ }
+ if responsibleUserID(u) == 0 {
+ return dispatcher.EndGroups
+ }
+ return dispatcher.ContinueGroups
+}
+
+func Register(disp dispatcher.Dispatcher) {
+ disp.AddHandler(handlers.NewMessage(filters.Message.All, groupGate))
disp.AddHandler(handlers.NewMessage(filters.Message.All, checkPermission))
for _, info := range CommandHandlers {
disp.AddHandler(handlers.NewCommand(info.Cmd, info.handler))
}
+ // Group chats only react to explicit commands and callback buttons. Plain
+ // messages would otherwise save every album or link posted in the group
+ // once privacy mode is disabled.
+ disp.AddHandler(handlers.NewMessage(filters.Message.All, func(ctx *ext.Context, u *ext.Update) error {
+ if isGroupChat(u) {
+ return dispatcher.EndGroups
+ }
+ return dispatcher.ContinueGroups
+ }))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix("update"), withPermission(handleUpdateCallback)))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeAdd), withPermission(handleAddCallback)))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeSetDefault), withPermission(handleSetDefaultCallback)))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeCancel), withPermission(handleCancelCallback)))
disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeConfig), withPermission(handleConfigCallback)))
- disp.AddHandler(handlers.NewMessage(sabotfilters.RegexUrl(regexp.MustCompile(re.TgMessageLinkRegexString)), handleSilentMode(handleMessageLink, handleSilentSaveLink)))
- disp.AddHandler(handlers.NewMessage(sabotfilters.RegexUrl(regexp.MustCompile(re.TelegraphUrlRegexString)), handleSilentMode(handleTelegraphUrlMessage, handleSilentSaveTelegraph)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeEngine), withPermission(handleEngineCallback)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeTask), withPermission(handleTaskCallback)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeRetry), withPermission(handleRetryCallback)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeWatch), withPermission(handleWatchCallback)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeDir), withPermission(handleDirCallback)))
+ disp.AddHandler(handlers.NewCallbackQuery(filters.CallbackQuery.Prefix(tcbdata.TypeRule), withPermission(handleRuleCallback)))
+ disp.AddHandler(handlers.NewMessage(sabotfilters.RegexUrl(regexp.MustCompile(re.TgMessageLinkRegexString)), skipLinkForMedia(handleSilentMode(handleMessageLink, handleSilentSaveLink))))
+ disp.AddHandler(handlers.NewMessage(sabotfilters.RegexUrl(regexp.MustCompile(re.TelegraphUrlRegexString)), skipLinkForMedia(handleSilentMode(handleTelegraphUrlMessage, handleSilentSaveTelegraph))))
disp.AddHandler(handlers.NewMessage(filters.Message.Media, handleSilentMode(handleMediaMessage, handleSilentSaveMedia)))
disp.AddHandler(handlers.NewMessage(filters.Message.Text, handleSilentMode(handleTextMessage, handleSilentSaveText)))
- if config.C().Telegram.Userbot.Enable {
- go listenMediaMessageEvent(userclient.GetMediaMessageCh(), botCtx)
- }
}
diff --git a/client/bot/handlers/rule.go b/client/bot/handlers/rule.go
index 7d7bf11e..3179c941 100644
--- a/client/bot/handlers/rule.go
+++ b/client/bot/handlers/rule.go
@@ -5,10 +5,10 @@ import (
"strconv"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/duke-git/lancet/v2/slice"
+ "github.com/gotd/td/telegram/message/markup"
+ "github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
@@ -16,12 +16,33 @@ import (
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/rule"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
+// resolveRuleStorage accepts a storage ID or a storage name. IDs avoid the
+// ambiguity of names that contain spaces; CHOSEN keeps the rule storage-less.
+func resolveRuleStorage(userChatID int64, arg string) (uint, error) {
+ if strings.EqualFold(arg, rule.RuleStorNameChosen) {
+ return 0, nil
+ }
+ if id, err := strconv.ParseUint(arg, 10, 32); err == nil {
+ if !config.C().HasStorageID(userChatID, uint(id)) {
+ return 0, fmt.Errorf("storage ID %d not found", id)
+ }
+ return uint(id), nil
+ }
+ if !config.C().HasStorage(userChatID, arg) {
+ return 0, fmt.Errorf("storage not found: %s", arg)
+ }
+ return config.C().GetStorageIDByName(arg), nil
+}
+
func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
logger := log.FromContext(ctx)
args := strutil.ParseArgsRespectQuotes(update.EffectiveMessage.Text)
- userChatID := update.GetUserChat().GetID()
+ userChatID := responsibleUserID(update)
user, err := database.GetUserByChatID(ctx, userChatID)
if err != nil {
logger.Errorf("Failed to get user rules: %s", err)
@@ -29,12 +50,12 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildRuleHelpStyling(user.ApplyRule, user.Rules)), nil)
+ ctx.Reply(update, ext.ReplyTextString(ruleMenuText(user)), &ext.ReplyOpts{Markup: ruleMenuMarkup(user)})
return dispatcher.EndGroups
}
switch args[1] {
case "switch":
- // /rule switch
+ // /rules switch
applyRule := !user.ApplyRule
if err := database.UpdateUserApplyRule(ctx, user.ChatID, applyRule); err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleErrorUpdateUserFailed, nil)), nil)
@@ -46,7 +67,7 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleInfoRuleModeDisabled, nil)), nil)
}
case "add":
- // /rule add
+ // /rules add
if len(args) < 6 {
ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildRuleHelpStyling(user.ApplyRule, user.Rules)), nil)
return dispatcher.EndGroups
@@ -69,15 +90,22 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
}
ruleData := args[3]
- storageName := args[4]
+ storageArg := args[4]
dirPath := args[5]
+ storageID, err := resolveRuleStorage(user.ChatID, storageArg)
+ if err != nil {
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleErrorStorageNotFound, map[string]any{
+ "Storage": storageArg,
+ })), nil)
+ return dispatcher.EndGroups
+ }
rd := &database.Rule{
- Type: ruleType.String(),
- Data: ruleData,
- StorageName: storageName,
- DirPath: dirPath,
- UserID: user.ID,
+ Type: ruleType.String(),
+ Content: ruleData,
+ StorageID: storageID,
+ DirPath: dirPath,
+ UserID: user.ID,
}
if err := database.CreateRule(ctx, rd); err != nil {
logger.Errorf("failed to create rule: %s", err)
@@ -86,15 +114,16 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
}
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleInfoCreateRuleSuccess, nil)), nil)
case "preset":
- // /rule preset [base_path]
+ // /rules preset [base_path]
if len(args) < 3 {
ctx.Reply(update, ext.ReplyTextStyledTextArray(msgelem.BuildRuleHelpStyling(user.ApplyRule, user.Rules)), nil)
return dispatcher.EndGroups
}
- storageName := args[2]
- if !config.C().HasStorage(user.ChatID, storageName) {
+ storageArg := args[2]
+ storageID, err := resolveRuleStorage(user.ChatID, storageArg)
+ if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleErrorStorageNotFound, map[string]any{
- "Storage": storageName,
+ "Storage": storageArg,
})), nil)
return dispatcher.EndGroups
}
@@ -106,11 +135,11 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
imported := 0
for _, p := range presets {
rd := &database.Rule{
- Type: rule.FileNameRegex.String(),
- Data: p.Regex,
- StorageName: storageName,
- DirPath: p.Dir,
- UserID: user.ID,
+ Type: rule.FileNameRegex.String(),
+ Content: p.Regex,
+ StorageID: storageID,
+ DirPath: p.Dir,
+ UserID: user.ID,
}
if err := database.CreateRule(ctx, rd); err != nil {
logger.Errorf("failed to create preset rule %s: %s", p.Name, err)
@@ -126,7 +155,7 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
"Count": imported,
})), nil)
case "del":
- // /rule del
+ // /rules del
if len(args) < 3 {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRulePromptProvideRuleId, nil)), nil)
return dispatcher.EndGroups
@@ -137,7 +166,7 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleErrorInvalidRuleId, nil)), nil)
return dispatcher.EndGroups
}
- if err := database.DeleteRule(ctx, uint(id)); err != nil {
+ if err := database.DeleteRule(ctx, uint(id), user.ID); err != nil {
logger.Errorf("failed to delete rule %d: %s", id, err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgRuleErrorDeleteRuleFailed, nil)), nil)
return dispatcher.EndGroups
@@ -149,3 +178,88 @@ func handleRuleCmd(ctx *ext.Context, update *ext.Update) error {
}
return dispatcher.EndGroups
}
+
+func ruleMenuText(user *database.User) string {
+ var b strings.Builder
+ b.WriteString(i18n.T(i18nk.BotMsgRuleMenuTitle))
+ b.WriteString("\n\n")
+ b.WriteString(i18n.T(i18nk.BotMsgRuleMenuHint))
+ mode := i18nk.BotMsgRuleInfoRuleModeDisabled
+ if user.ApplyRule {
+ mode = i18nk.BotMsgRuleInfoRuleModeEnabled
+ }
+ b.WriteString("\n\n")
+ b.WriteString(i18n.T(mode))
+ if len(user.Rules) == 0 {
+ b.WriteString("\n\n")
+ b.WriteString(i18n.T(i18nk.BotMsgRuleMenuEmpty))
+ return b.String()
+ }
+ for _, r := range user.Rules {
+ storageName := rule.RuleStorNameChosen
+ if r.StorageID != 0 {
+ storageName = config.C().GetStorageNameByID(r.StorageID)
+ if storageName == "" {
+ storageName = fmt.Sprintf("#%d", r.StorageID)
+ }
+ }
+ fmt.Fprintf(&b, "\n\n#%d %s %s %s %s", r.ID, r.Type, r.Content, storageName, r.DirPath)
+ }
+ return b.String()
+}
+
+func ruleMenuMarkup(user *database.User) *tg.ReplyInlineMarkup {
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(user.Rules)+1)
+ label := i18nk.BotMsgRuleButtonEnable
+ if user.ApplyRule {
+ label = i18nk.BotMsgRuleButtonDisable
+ }
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(label), fmt.Appendf(nil, "%s switch", tcbdata.TypeRule)),
+ }})
+ for _, rule := range user.Rules {
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgRuleButtonDelete), fmt.Appendf(nil, "%s del %d", tcbdata.TypeRule, rule.ID)),
+ }})
+ }
+ return &tg.ReplyInlineMarkup{Rows: rows}
+}
+
+func handleRuleCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ switch args[1] {
+ case "switch":
+ if err := database.UpdateUserApplyRule(ctx, user.ChatID, !user.ApplyRule); err != nil {
+ return err
+ }
+ case "del":
+ if len(args) < 3 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
+ ruleID, err := strconv.Atoi(args[2])
+ if err != nil {
+ return err
+ }
+ if err := database.DeleteRule(ctx, uint(ruleID), user.ID); err != nil {
+ return err
+ }
+ }
+ user, err = database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: ruleMenuText(user),
+ ReplyMarkup: ruleMenuMarkup(user),
+ })
+ return dispatcher.EndGroups
+}
diff --git a/client/bot/handlers/save.go b/client/bot/handlers/save.go
index 4500b910..79bd3be5 100644
--- a/client/bot/handlers/save.go
+++ b/client/bot/handlers/save.go
@@ -4,8 +4,6 @@ import (
"regexp"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/duke-git/lancet/v2/validator"
"github.com/gotd/td/tg"
@@ -16,11 +14,14 @@ import (
"github.com/krau/SaveAny-Bot/client/user"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/mediafilter"
"github.com/krau/SaveAny-Bot/common/utils/strutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/krau/SaveAny-Bot/storage"
)
@@ -36,7 +37,7 @@ func handleSaveCmd(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgSaveHelpText)), nil)
return dispatcher.EndGroups
}
- userDB, err := database.GetUserByChatID(ctx, update.GetUserChat().GetID())
+ userDB, err := database.GetUserByChatID(ctx, responsibleUserID(update))
if err != nil {
return err
}
@@ -48,7 +49,7 @@ func handleSaveCmd(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- userId := update.GetUserChat().GetID()
+ userId := responsibleUserID(update)
stors := storage.GetUserStorages(ctx, userId)
req, err := msgelem.BuildAddOneSelectStorageMessage(ctx, stors, file, msg.ID)
if err != nil {
@@ -71,7 +72,7 @@ func handleSilentSaveReplied(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgSaveHelpText)), nil)
return dispatcher.EndGroups
}
- userDB, err := database.GetUserByChatID(ctx, update.GetUserChat().GetID())
+ userDB, err := database.GetUserByChatID(ctx, responsibleUserID(update))
if err != nil {
return err
}
@@ -83,7 +84,7 @@ func handleSilentSaveReplied(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, update.GetUserChat().GetID(), stor, dirutil.PathFromContext(ctx), file, msg.GetID())
+ return shortcut.CreateAndAddTGFileTaskWithEdit(ctx, responsibleUserID(update), chatID(update), stor, dirutil.PathFromContext(ctx), file, msg.GetID())
}
func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error {
@@ -110,7 +111,7 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
if uctx != nil && validator.IsIntStr(chatArg) {
tctx = uctx
}
- chatID, err := tgutil.ParseChatID(tctx, chatArg)
+ sourceChatID, err := tgutil.ParseChatID(tctx, chatArg)
if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorInvalidIdOrUsername, map[string]any{"Error": err.Error()})), nil)
return dispatcher.EndGroups
@@ -122,7 +123,7 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
return dispatcher.EndGroups
}
- msgs, err := tgutil.GetMessagesRange(tctx, chatID, int(startID), int(endID))
+ msgs, err := tgutil.GetMessagesRange(tctx, sourceChatID, int(startID), int(endID))
if err != nil {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetMessagesFailed, map[string]any{"Error": err.Error()})), nil)
return dispatcher.EndGroups
@@ -141,8 +142,7 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
if !ok {
continue
}
- supported := mediautil.IsSupported(media)
- if !supported {
+ if !mediautil.IsSupported(media) || !mediafilter.Allowed(media) {
continue
}
file, err := tfile.FromMediaMessage(media, tctx.Raw, msg, tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*msg)))
@@ -154,8 +154,12 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
sb.Reset()
sb.WriteString(msg.GetMessage())
sb.WriteString(" ")
- fn, _ := tgutil.GetMediaFileName(media)
- sb.WriteString(fn)
+ fn, err := tgutil.GetMediaFileName(media)
+ if err != nil {
+ log.FromContext(ctx).Debugf("Failed to derive media filename for filter: %v", err)
+ } else {
+ sb.WriteString(fn)
+ }
if !filter.MatchString(sb.String()) {
continue
}
@@ -169,7 +173,7 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
stor := storage.FromContext(ctx)
if stor == nil {
// not in silent mode
- stors := storage.GetUserStorages(ctx, update.GetUserChat().GetID())
+ stors := storage.GetUserStorages(ctx, responsibleUserID(update))
markup, err := msgelem.BuildAddSelectStorageKeyboard(stors, tcbdata.Add{
Files: files,
})
@@ -188,5 +192,5 @@ func handleBatchSave(ctx *ext.Context, update *ext.Update, args []string) error
})
return dispatcher.EndGroups
}
- return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, update.GetUserChat().GetID(), stor, "", files, replied.ID)
+ return shortcut.CreateAndAddBatchTGFileTaskWithEdit(ctx, responsibleUserID(update), chatID(update), stor, "", files, replied.ID)
}
diff --git a/client/bot/handlers/silent.go b/client/bot/handlers/silent.go
index f83c5fcb..eec2cd0e 100644
--- a/client/bot/handlers/silent.go
+++ b/client/bot/handlers/silent.go
@@ -4,8 +4,6 @@ import (
"fmt"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/cache"
@@ -14,35 +12,10 @@ import (
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
-func handleSilentCmd(ctx *ext.Context, update *ext.Update) error {
- user, err := database.GetUserByChatID(ctx, update.GetUserChat().GetID())
- if err != nil {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetUserInfoFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return nil
- }
- if !user.Silent && user.DefaultStorage == "" {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorDefaultStorageNotSet, nil)), nil)
- return nil
- }
- user.Silent = !user.Silent
- if err := database.UpdateUser(ctx, user); err != nil {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorUpdateUserInfoFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return nil
- }
- if user.Silent {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonInfoSilentModeOn, nil)), nil)
- } else {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonInfoSilentModeOff, nil)), nil)
- }
- return dispatcher.EndGroups
-}
-
func handleSetDefaultCallback(ctx *ext.Context, update *ext.Update) error {
dataParts := strings.Split(string(update.CallbackQuery.Data), " ")
if len(dataParts) < 2 {
@@ -66,8 +39,8 @@ func handleSetDefaultCallback(ctx *ext.Context, update *ext.Update) error {
}
userID := update.CallbackQuery.GetUserID()
- storageName := data.StorageName
- selectedStorage, err := storage.GetStorageByUserIDAndName(ctx, userID, storageName)
+ storageID := data.StorageID
+ selectedStorage, err := storage.GetStorageByUserIDAndID(ctx, userID, storageID)
if err != nil {
return failedAnswer(i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
"Error": err.Error(),
@@ -86,22 +59,22 @@ func handleSetDefaultCallback(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return failedAnswer(i18n.T(i18nk.BotMsgDirErrorGetUserDirsFailed, nil))
}
- user.DefaultDir = dir.ID
+ user.DefaultDirID = dir.ID
} else {
- dirs, err := database.GetDirsByUserIDAndStorageName(ctx, user.ID, storageName)
+ dirs, err := database.GetDirsByUserIDAndStorageID(ctx, user.ID, storageID)
if err != nil {
return failedAnswer(i18n.T(i18nk.BotMsgCommonErrorGetDirFailed, map[string]any{
"Error": err.Error(),
}))
}
if len(dirs) > 0 {
- markup, err := msgelem.BuildSetDefaultDirMarkup(ctx, storageName, dirs)
+ markup, err := msgelem.BuildSetDefaultDirMarkup(ctx, storageID, dirs)
if err != nil {
return failedAnswer(i18n.T(i18nk.BotMsgCommonErrorBuildDirSelectKeyboardFailed, map[string]any{
"Error": err.Error(),
}))
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgCommonPromptSelectDefaultDir, nil),
ReplyMarkup: markup,
@@ -109,7 +82,7 @@ func handleSetDefaultCallback(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
}
- user.DefaultStorage = selectedStorage.Name()
+ user.DefaultStorageID = storageID
if err := database.UpdateUser(ctx, user); err != nil {
return failedAnswer(i18n.T(i18nk.BotMsgCommonErrorUpdateUserInfoFailed, map[string]any{
"Error": err.Error(),
@@ -124,29 +97,9 @@ func handleSetDefaultCallback(ctx *ext.Context, update *ext.Update) error {
"Dir": strings.TrimPrefix(dir.Path, "/"),
})
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
ID: update.CallbackQuery.GetMsgID(),
Message: msg,
})
return dispatcher.EndGroups
}
-
-func handleStorageCmd(ctx *ext.Context, update *ext.Update) error {
- userID := update.GetUserChat().GetID()
- storages := storage.GetUserStorages(ctx, userID)
- if len(storages) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorNoAvailableStorage, nil)), nil)
- return nil
- }
- markup, err := msgelem.BuildSetDefaultStorageMarkup(ctx, storages)
- if err != nil {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return nil
- }
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonPromptSelectDefaultStorage, nil)), &ext.ReplyOpts{
- Markup: markup,
- })
- return dispatcher.EndGroups
-}
diff --git a/client/bot/handlers/sync_peers.go b/client/bot/handlers/sync_peers.go
index c3f0057f..7cc4b8b8 100644
--- a/client/bot/handlers/sync_peers.go
+++ b/client/bot/handlers/sync_peers.go
@@ -1,62 +1,10 @@
package handlers
import (
- "context"
- "sync"
-
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
- "github.com/celestix/gotgproto/storage"
- "github.com/charmbracelet/log"
- "github.com/gotd/td/telegram/query/dialogs"
- "github.com/krau/SaveAny-Bot/client/user"
- "github.com/krau/SaveAny-Bot/common/i18n"
- "github.com/krau/SaveAny-Bot/common/i18n/i18nk"
- "github.com/krau/SaveAny-Bot/config"
+ userclient "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/mygotg/ext"
)
-var syncpeerMu sync.Mutex
-
-func handleSyncpeersCmd(ctx *ext.Context, u *ext.Update) error {
- if !config.C().Telegram.Userbot.Enable {
- return dispatcher.EndGroups
- }
- syncpeerMu.Lock()
- defer syncpeerMu.Unlock()
- uctx := user.GetCtx()
- if uctx == nil {
- return dispatcher.EndGroups
- }
- ctx.Reply(u, ext.ReplyTextString(i18n.T(i18nk.BotMsgSyncpeersStart)), nil)
- tapi := uctx.Raw
- peerStorage := uctx.PeerStorage
- log.FromContext(ctx).Info("Starting to sync peers...")
- count := 0
- err := dialogs.NewQueryBuilder(tapi).GetDialogs().BatchSize(50).ForEach(ctx, func(ctx context.Context, e dialogs.Elem) error {
- for cid, channel := range e.Entities.Channels() {
- peerStorage.AddPeer(cid, channel.AccessHash, storage.TypeChannel, channel.Username)
- count++
- }
- for uid, user := range e.Entities.Users() {
- peerStorage.AddPeer(uid, user.AccessHash, storage.TypeUser, user.Username)
- count++
- }
- for gid := range e.Entities.Chats() {
- peerStorage.AddPeer(gid, storage.DefaultAccessHash, storage.TypeChat, storage.DefaultUsername)
- count++
- }
- return nil
- })
- if err != nil {
- log.FromContext(ctx).Error("Failed to sync peers", "error", err)
- ctx.Reply(u, ext.ReplyTextString(i18n.T(i18nk.BotMsgSyncpeersFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return dispatcher.EndGroups
- }
- log.FromContext(ctx).Info("Finished syncing peers")
- ctx.Reply(u, ext.ReplyTextString(i18n.T(i18nk.BotMsgSyncpeersSuccess, map[string]any{
- "Count": count,
- })), nil)
- return dispatcher.EndGroups
+func syncPeers(ctx *ext.Context) (int, error) {
+ return userclient.SyncPeers(ctx)
}
diff --git a/client/bot/handlers/tasks.go b/client/bot/handlers/tasks.go
index ac1c3c07..1088223e 100644
--- a/client/bot/handlers/tasks.go
+++ b/client/bot/handlers/tasks.go
@@ -1,117 +1,229 @@
package handlers
import (
+ "fmt"
"strings"
"time"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
- "github.com/gotd/td/telegram/message/styling"
+ "github.com/gotd/td/telegram/message/markup"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
+ userclient "github.com/krau/SaveAny-Bot/client/user"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/core/retry"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/queue"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
-func handleTaskCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- args := strings.Fields(update.EffectiveMessage.Text)
- if len(args) == 1 {
- showRunningTasks(ctx, update)
- return dispatcher.EndGroups
- }
+const taskListLimit = 10
+
+func handleTasksCmd(ctx *ext.Context, update *ext.Update) error {
+ ctx.Reply(update, ext.ReplyTextString(taskMenuText()), &ext.ReplyOpts{
+ Markup: taskMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func taskMenuText() string {
+ return i18n.T(i18nk.BotMsgTasksMenuTitle) + "\n\n" + i18n.T(i18nk.BotMsgTasksMenuHint)
+}
+
+func taskMenuMarkup() *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgTasksMenuRunning), fmt.Appendf(nil, "%s running", tcbdata.TypeTask)),
+ markup.Callback(i18n.T(i18nk.BotMsgTasksMenuQueued), fmt.Appendf(nil, "%s queued", tcbdata.TypeTask)),
+ }},
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgTasksMenuFailed), fmt.Appendf(nil, "%s failed", tcbdata.TypeTask)),
+ }},
+ }}
+}
+func taskBackMarkup() *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{
+ Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgTasksMenuBack), fmt.Appendf(nil, "%s menu", tcbdata.TypeTask)),
+ },
+ }}}
+}
+
+func handleTaskCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
switch args[1] {
- case "running", "run", "r":
- showRunningTasks(ctx, update)
- case "queued", "queue", "q", "waiting":
- showQueuedTasks(ctx, update)
- case "cancel", "c":
- if len(args) < 3 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTasksUsageCancel)), nil)
- return dispatcher.EndGroups
- }
- taskID := args[2]
- if err := core.CancelTask(ctx, taskID); err != nil {
- logger.Errorf("Failed to cancel task %s: %v", taskID, err)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTasksCancelFailed, map[string]any{"Error": err.Error()})), nil)
- return dispatcher.EndGroups
- }
- ctx.Reply(update, ext.ReplyTextStyledTextArray([]styling.StyledTextOption{
- styling.Plain(i18n.T(i18nk.BotMsgTasksCancelRequestedPrefix)),
- styling.Code(taskID),
- }), nil)
+ case "menu":
+ return editTaskMenu(ctx, update)
+ case "running":
+ return editRunningTasks(ctx, update)
+ case "queued":
+ return editQueuedTasks(ctx, update)
+ case "failed":
+ return editFailedTasks(ctx, update)
default:
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTasksUsage)), nil)
+ return editTaskMenu(ctx, update)
}
+}
+
+func editTaskMenu(ctx *ext.Context, update *ext.Update) error {
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: taskMenuText(),
+ ReplyMarkup: taskMenuMarkup(),
+ })
return dispatcher.EndGroups
}
-func showRunningTasks(ctx *ext.Context, update *ext.Update) {
- tasks := core.GetRunningTasks(ctx)
- if len(tasks) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTasksRunningEmpty)), nil)
- return
+type taskListItem struct {
+ ID string
+ Title string
+ Created time.Time
+}
+
+func taskItems(tasks []queue.TaskInfo) []taskListItem {
+ items := make([]taskListItem, 0, len(tasks))
+ for _, task := range tasks {
+ items = append(items, taskListItem{ID: task.ID, Title: task.Title, Created: task.Created})
}
- opts := make([]styling.StyledTextOption, 0, 2+len(tasks)*4)
- opts = append(opts,
- styling.Bold(i18n.T(i18nk.BotMsgTasksRunningTitle)),
- styling.Plain(i18n.T(i18nk.BotMsgTasksTotalPrefix, map[string]any{"Count": len(tasks)})),
- )
- for _, t := range tasks {
- created := t.Created.In(time.Local).Format("2006-01-02 15:04:05")
- status := i18n.T(i18nk.BotMsgTasksStatusRunning)
- if t.Cancelled {
- status = i18n.T(i18nk.BotMsgTasksStatusCancelRequested)
- }
- opts = append(opts,
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldId)),
- styling.Code(t.ID),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldTitle)),
- styling.Code(t.Title),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldCreated)),
- styling.Code(created),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldStatus)),
- styling.Code(status),
+ return items
+}
+
+func taskListText(title string, total int, items []taskListItem) string {
+ var b strings.Builder
+ fmt.Fprintf(&b, "%s (%d)", title, total)
+ limit := min(len(items), taskListLimit)
+ for i := 0; i < limit; i++ {
+ item := items[i]
+ fmt.Fprintf(&b, "\n\n%s: %s\n%s: %s\n%s: %s",
+ i18n.T(i18nk.BotMsgTasksFieldId), item.ID,
+ i18n.T(i18nk.BotMsgTasksFieldTitle), item.Title,
+ i18n.T(i18nk.BotMsgTasksFieldCreated), item.Created.In(time.Local).Format("2006-01-02 15:04:05"),
)
}
- ctx.Reply(update, ext.ReplyTextStyledTextArray(opts), nil)
+ if len(items) > limit {
+ fmt.Fprintf(&b, "\n\n%s", i18n.T(i18nk.BotMsgTasksTruncatedNote, map[string]any{"Count": len(items)}))
+ }
+ return b.String()
}
-func showQueuedTasks(ctx *ext.Context, update *ext.Update) {
+func cancelTaskRows(items []taskListItem) []tg.KeyboardInlineButtonRow {
+ rows := make([]tg.KeyboardInlineButtonRow, 0, min(len(items), taskListLimit)+1)
+ for i := 0; i < min(len(items), taskListLimit); i++ {
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgTasksCancelButton), fmt.Appendf(nil, "%s %s", tcbdata.TypeCancel, items[i].ID)),
+ }})
+ }
+ return rows
+}
+
+func editRunningTasks(ctx *ext.Context, update *ext.Update) error {
+ tasks := core.GetRunningTasks(ctx)
+ var text string
+ markup := taskBackMarkup()
+ if len(tasks) == 0 {
+ text = i18n.T(i18nk.BotMsgTasksRunningEmpty)
+ } else {
+ items := taskItems(tasks)
+ text = taskListText(i18n.T(i18nk.BotMsgTasksRunningTitle), len(items), items)
+ markup.Rows = append(cancelTaskRows(items), markup.Rows...)
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: text,
+ ReplyMarkup: markup,
+ })
+ return dispatcher.EndGroups
+}
+
+func editQueuedTasks(ctx *ext.Context, update *ext.Update) error {
tasks := core.GetQueuedTasks(ctx)
+ var text string
+ markup := taskBackMarkup()
if len(tasks) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTasksQueuedEmpty)), nil)
- return
+ text = i18n.T(i18nk.BotMsgTasksQueuedEmpty)
+ } else {
+ items := taskItems(tasks)
+ text = taskListText(i18n.T(i18nk.BotMsgTasksQueuedTitle), len(items), items)
+ markup.Rows = append(cancelTaskRows(items), markup.Rows...)
}
- opts := make([]styling.StyledTextOption, 0, 2+len(tasks)*3)
- opts = append(opts,
- styling.Bold(i18n.T(i18nk.BotMsgTasksQueuedTitle)),
- styling.Plain(i18n.T(i18nk.BotMsgTasksTotalPrefix, map[string]any{"Count": len(tasks)})),
- )
- const maxShown = 10
- for i, t := range tasks {
- if i >= maxShown {
- break
- }
- created := t.Created.In(time.Local).Format("2006-01-02 15:04:05")
- status := i18n.T(i18nk.BotMsgTasksStatusQueued)
- if t.Cancelled {
- status = i18n.T(i18nk.BotMsgTasksStatusCancelRequested)
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: text,
+ ReplyMarkup: markup,
+ })
+ return dispatcher.EndGroups
+}
+
+func editFailedTasks(ctx *ext.Context, update *ext.Update) error {
+ failed, _, err := database.ListTaskRecords(ctx, database.TaskFilter{Status: "failed", Limit: taskListLimit})
+ if err != nil {
+ return err
+ }
+ partial, _, err := database.ListTaskRecords(ctx, database.TaskFilter{Status: "partial", Limit: taskListLimit})
+ if err != nil {
+ return err
+ }
+ records := append(failed, partial...)
+ if len(records) > taskListLimit {
+ records = records[:taskListLimit]
+ }
+ var text string
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(records)+1)
+ if len(records) == 0 {
+ text = i18n.T(i18nk.BotMsgTasksFailedEmpty)
+ } else {
+ var b strings.Builder
+ fmt.Fprintf(&b, "%s (%d)", i18n.T(i18nk.BotMsgTasksFailedTitle), len(records))
+ for _, record := range records {
+ fmt.Fprintf(&b, "\n\n%s: %s\n%s: %s\n%s: %s",
+ i18n.T(i18nk.BotMsgTasksFieldId), record.TaskID,
+ i18n.T(i18nk.BotMsgTasksFieldTitle), record.Title,
+ i18n.T(i18nk.BotMsgTasksFieldStatus), record.Status,
+ )
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgTasksRetryButton), fmt.Appendf(nil, "%s %s", tcbdata.TypeRetry, record.TaskID)),
+ }})
}
- opts = append(opts,
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldId)),
- styling.Code(t.ID),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldTitle)),
- styling.Code(t.Title),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldCreated)),
- styling.Code(created),
- styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksFieldStatus)),
- styling.Code(status),
- )
+ text = b.String()
+ }
+ rows = append(rows, taskBackMarkup().Rows...)
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: text,
+ ReplyMarkup: &tg.ReplyInlineMarkup{Rows: rows},
+ })
+ return dispatcher.EndGroups
+}
+
+func handleRetryCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
}
- if len(tasks) > maxShown {
- opts = append(opts, styling.Plain("\n"+i18n.T(i18nk.BotMsgTasksTruncatedNote, map[string]any{"Count": len(tasks)})))
+ taskID := args[1]
+ candidates := []*ext.Context{ctx}
+ if uc := userclient.GetCtx(); uc != nil {
+ candidates = append(candidates, uc)
}
- ctx.Reply(update, ext.ReplyTextStyledTextArray(opts), nil)
+ newID, count, err := retry.FailedTask(ctx, candidates, taskID)
+ if err != nil {
+ log.FromContext(ctx).Errorf("failed to retry task %s: %v", taskID, err)
+ ctx.AnswerCallback(msgelem.AlertCallbackAnswer(update.CallbackQuery.GetQueryID(), i18n.T(i18nk.BotMsgRetryErrorFailed, map[string]any{
+ "Error": err.Error(),
+ })))
+ return dispatcher.EndGroups
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: i18n.T(i18nk.BotMsgRetryInfoStarted, map[string]any{"TaskID": newID, "Count": count}),
+ })
+ return dispatcher.EndGroups
}
diff --git a/client/bot/handlers/telegraph.go b/client/bot/handlers/telegraph.go
index 6f30600a..2e7397c6 100644
--- a/client/bot/handlers/telegraph.go
+++ b/client/bot/handlers/telegraph.go
@@ -4,8 +4,6 @@ import (
"fmt"
"path"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
@@ -18,6 +16,8 @@ import (
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
func handleTelegraphUrlMessage(ctx *ext.Context, update *ext.Update) error {
@@ -27,7 +27,7 @@ func handleTelegraphUrlMessage(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- userID := update.GetUserChat().GetID()
+ userID := responsibleUserID(update)
stors := storage.GetUserStorages(ctx, userID)
markup, err := msgelem.BuildAddSelectStorageKeyboard(stors, tcbdata.Add{
TaskType: tasktype.TaskTypeTphpics,
@@ -55,7 +55,7 @@ func handleTelegraphUrlMessage(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
text, entities := eb.Complete()
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
Message: text,
ID: msg.ID,
ReplyMarkup: markup,
@@ -70,11 +70,10 @@ func handleSilentSaveTelegraph(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- userID := update.GetUserChat().GetID()
dirpath := result.TphDir
if p := dirutil.PathFromContext(ctx); p != "" {
dirpath = path.Join(p, dirpath)
}
- return shortcut.CreateAndAddtelegraphWithEdit(ctx, userID, result.Page, dirpath, result.Pics, stor, msg.ID)
+ return shortcut.CreateAndAddtelegraphWithEdit(ctx, chatID(update), result.Page, dirpath, result.Pics, stor, msg.ID)
}
diff --git a/client/bot/handlers/transfer.go b/client/bot/handlers/transfer.go
index f0d0a6e8..0d148039 100644
--- a/client/bot/handlers/transfer.go
+++ b/client/bot/handlers/transfer.go
@@ -3,10 +3,9 @@ package handlers
import (
"fmt"
"regexp"
+ "sort"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
@@ -14,15 +13,36 @@ import (
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/common/utils/strutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/core/tasks/transfer"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
+// splitTransferSource splits "storage:/path" while allowing storage names
+// that contain spaces or colons. Known storage names are matched longest-first.
+func splitTransferSource(userID int64, raw string) (string, string, bool) {
+ names := config.C().GetStorageNamesByUserID(userID)
+ sort.SliceStable(names, func(i, j int) bool { return len(names[i]) > len(names[j]) })
+ for _, name := range names {
+ prefix := name + ":"
+ if after, ok := strings.CutPrefix(raw, prefix); ok {
+ return name, after, true
+ }
+ }
+ parts := strings.SplitN(raw, ":", 2)
+ if len(parts) != 2 || parts[0] == "" {
+ return "", "", false
+ }
+ return parts[0], parts[1], true
+}
+
func handleTransferCmd(ctx *ext.Context, update *ext.Update) error {
logger := log.FromContext(ctx)
args := strutil.ParseArgsRespectQuotes(update.EffectiveMessage.Text)
@@ -32,16 +52,12 @@ func handleTransferCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
- // Parse source: storage_name:/path
- sourceParts := strings.SplitN(args[1], ":", 2)
- if len(sourceParts) != 2 {
+ userID := responsibleUserID(update)
+ sourceStorageName, sourcePath, ok := splitTransferSource(userID, args[1])
+ if !ok {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgTransferErrorInvalidSource, nil)), nil)
return dispatcher.EndGroups
}
- sourceStorageName := sourceParts[0]
- sourcePath := sourceParts[1]
-
- userID := update.GetUserChat().GetID()
sourceStorage, err := storage.GetStorageByUserIDAndName(ctx, userID, sourceStorageName)
if err != nil {
@@ -123,10 +139,10 @@ func handleTransferCmd(ctx *ext.Context, update *ext.Update) error {
}
markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, userID), tcbdata.Add{
- TaskType: tasktype.TaskTypeTransfer,
- TransferSourceStorName: sourceStorageName,
- TransferSourcePath: sourcePath,
- TransferFiles: filePaths,
+ TaskType: tasktype.TaskTypeTransfer,
+ TransferSourceStorageID: storage.ID(sourceStorage),
+ TransferSourcePath: sourcePath,
+ TransferFiles: filePaths,
})
if err != nil {
logger.Errorf("Failed to build storage selection keyboard: %s", err)
@@ -149,38 +165,38 @@ func handleTransferCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
-func handleTransferCallback(ctx *ext.Context, userID int64, targetStorage storage.Storage, dirPath string, data tcbdata.Add, msgID int) error {
+func handleTransferCallback(ctx *ext.Context, userID, chatID int64, targetStorage storage.Storage, dirPath string, data tcbdata.Add, msgID int) error {
logger := log.FromContext(ctx)
- sourceStorage, err := storage.GetStorageByUserIDAndName(ctx, userID, data.TransferSourceStorName)
+ sourceStorage, err := storage.GetStorageByUserIDAndID(ctx, userID, data.TransferSourceStorageID)
if err != nil {
logger.Errorf("Failed to get source storage: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
- Message: i18n.T(i18nk.BotMsgTransferErrorStorageNotFound, map[string]any{"StorageName": data.TransferSourceStorName, "Error": err}),
+ Message: i18n.T(i18nk.BotMsgTransferErrorStorageNotFound, map[string]any{"StorageName": config.C().GetStorageNameByID(data.TransferSourceStorageID), "Error": err}),
})
return dispatcher.EndGroups
}
listable, ok := sourceStorage.(storage.StorageListable)
if !ok {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
- Message: i18n.T(i18nk.BotMsgTransferErrorStorageNotListable, map[string]any{"StorageName": data.TransferSourceStorName}),
+ Message: i18n.T(i18nk.BotMsgTransferErrorStorageNotListable, map[string]any{"StorageName": config.C().GetStorageNameByID(data.TransferSourceStorageID)}),
})
return dispatcher.EndGroups
}
// Re-fetch files to get FileInfo (since we only stored paths)
// This is necessary to get size and other metadata
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgTransferInfoFetchingFiles, nil),
})
allFiles, err := listable.ListFiles(ctx, data.TransferSourcePath)
if err != nil {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgTransferErrorListFilesFailed, map[string]any{"Error": err}),
})
@@ -206,7 +222,7 @@ func handleTransferCallback(ctx *ext.Context, userID int64, targetStorage storag
}
if len(elems) == 0 {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgTransferErrorNoFilesToTransfer, nil),
})
@@ -219,19 +235,19 @@ func handleTransferCallback(ctx *ext.Context, userID int64, targetStorage storag
taskID,
injectCtx,
elems,
- transfer.NewProgressTracker(msgID, userID),
+ transfer.NewProgressTracker(msgID, chatID),
true, // IgnoreErrors
)
if err := core.AddTask(injectCtx, task); err != nil {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgTransferErrorAddTaskFailed, map[string]any{"Error": err}),
})
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgTransferInfoTaskAdded, map[string]any{
"Count": len(elems),
diff --git a/client/bot/handlers/update.go b/client/bot/handlers/update.go
index 30b3515e..50d3895e 100644
--- a/client/bot/handlers/update.go
+++ b/client/bot/handlers/update.go
@@ -1,20 +1,20 @@
package handlers
import (
- "errors"
"regexp"
"strings"
"github.com/blang/semver"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/telegram/message/html"
+ "github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/common/selfupdate"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/unvgo/ghselfupdate"
)
@@ -51,7 +51,7 @@ func handleUpdateCmd(ctx *ext.Context, u *ext.Update) error {
return dispatcher.EndGroups
}
indocker := config.Docker == "true"
- ctx.Sender.To(u.GetUserChat().AsInputPeer()).StyledText(ctx, html.String(nil, func() string {
+ ctx.Sender.To(u.EffectiveChat().GetInputPeer()).StyledText(ctx, html.String(nil, func() string {
md := latest.ReleaseNotes
md = regexp.MustCompile(`(?m)^###\s+ (.+)$`).ReplaceAllString(md, "$1 ")
md = regexp.MustCompile(`(?m)^#####\s+ (.+)$`).ReplaceAllString(md, "$1 ")
@@ -83,18 +83,12 @@ func handleUpdateCmd(ctx *ext.Context, u *ext.Update) error {
"PublishedAt": latest.PublishedAt.Format("2006-01-02 15:04:05"),
})
ctx.Reply(u, ext.ReplyTextString(text), &ext.ReplyOpts{
- Markup: &tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- &tg.KeyboardButtonCallback{
- Text: i18n.T(i18nk.BotMsgUpdateButtonUpgrade, nil),
- Data: []byte("update"),
- },
- },
- },
- },
- },
+ Markup: markup.InlineKeyboard(markup.InlineButtonRow(
+ markup.Callback(
+ i18n.T(i18nk.BotMsgUpdateButtonUpgrade, nil),
+ []byte("update"),
+ ),
+ )),
})
return dispatcher.EndGroups
}
@@ -107,7 +101,7 @@ func handleUpdateCallback(ctx *ext.Context, u *ext.Update) error {
})))
return dispatcher.EndGroups
}
- ctx.EditMessage(u.GetUserChat().GetID(), &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(u), &tg.MessagesEditMessageRequest{
ID: u.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgUpdateInfoUpgradingWithVersion, map[string]any{
"Current": config.Version,
@@ -115,7 +109,7 @@ func handleUpdateCallback(ctx *ext.Context, u *ext.Update) error {
})
latest, err := selfupdate.Update(currentV)
if err != nil {
- ctx.EditMessage(u.GetUserChat().GetID(), &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(u), &tg.MessagesEditMessageRequest{
ID: u.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgUpdateErrorUpgradeFailed, map[string]any{
"Error": err.Error(),
@@ -123,12 +117,12 @@ func handleUpdateCallback(ctx *ext.Context, u *ext.Update) error {
})
return dispatcher.EndGroups
}
- ctx.EditMessage(u.GetUserChat().GetID(), &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID(u), &tg.MessagesEditMessageRequest{
ID: u.CallbackQuery.GetMsgID(),
Message: i18n.T(i18nk.BotMsgUpdateInfoUpgradeSuccess, map[string]any{
"Version": latest.Version.String(),
}),
})
selfupdate.RequestRestart()
- return errors.New("SAVEANTBOT-RESTART")
+ return selfupdate.ErrRestart
}
diff --git a/client/bot/handlers/utils/filters/url.go b/client/bot/handlers/utils/filters/url.go
index 8de50ef1..6ab09e66 100644
--- a/client/bot/handlers/utils/filters/url.go
+++ b/client/bot/handlers/utils/filters/url.go
@@ -4,9 +4,9 @@ import (
"regexp"
"slices"
- "github.com/celestix/gotgproto/dispatcher/handlers/filters"
- "github.com/celestix/gotgproto/types"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/mygotg/dispatcher/handlers/filters"
+ "github.com/krau/mygotg/types"
)
func RegexUrl(r *regexp.Regexp) filters.MessageFilter {
diff --git a/client/bot/handlers/utils/mediautil/media.go b/client/bot/handlers/utils/mediautil/media.go
index 88eaeb3a..8136bd33 100644
--- a/client/bot/handlers/utils/mediautil/media.go
+++ b/client/bot/handlers/utils/mediautil/media.go
@@ -49,36 +49,117 @@ func (f FilenameTemplateData) ToMap() map[string]string {
func TfileOptions(ctx context.Context, user *database.User, message *tg.Message) []tfile.TGFileOption {
opts := make([]tfile.TGFileOption, 0)
- var fnameOpt tfile.TGFileOption
+ opts = append(opts, tfile.WithName(FilenameFor(ctx, user, message)), tfile.WithMessage(message))
+ return opts
+}
+
+// FilenameFor resolves the configured filename strategy for one message.
+func FilenameFor(ctx context.Context, user *database.User, message *tg.Message) string {
switch user.FilenameStrategy {
case fnamest.Message.String():
- fnameOpt = tfile.WithName(tgutil.GenFileNameFromMessage(*message))
+ return tgutil.GenFileNameFromMessage(*message)
case fnamest.Template.String():
if user.FilenameTemplate == "" {
log.FromContext(ctx).Warnf("empty filename template")
- fnameOpt = tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*message))
- break
+ return tgutil.GenFileNameFromMessage(*message)
}
tmpl, err := template.New("filename").Parse(user.FilenameTemplate)
if err != nil {
log.FromContext(ctx).Errorf("failed to parse filename template: %s", err)
- fnameOpt = tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*message))
- break
+ return tgutil.GenFileNameFromMessage(*message)
}
data := BuildFilenameTemplateData(message)
var sb strings.Builder
- err = tmpl.Execute(&sb, data)
- if err != nil {
+ if err := tmpl.Execute(&sb, data); err != nil {
log.FromContext(ctx).Errorf("failed to execute filename template: %s", err)
- fnameOpt = tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*message))
- break
+ return tgutil.GenFileNameFromMessage(*message)
+ }
+ if sb.String() == "" {
+ return tgutil.GenFileNameFromMessage(*message)
}
- fnameOpt = tfile.WithName(sb.String())
+ return sb.String()
default:
- fnameOpt = tfile.WithNameIfEmpty(tgutil.GenFileNameFromMessage(*message))
+ if name := tgutil.GetOriginalFileName(message.Media); name != "" {
+ return name
+ }
+ return tgutil.GenFileNameFromMessage(*message)
}
- opts = append(opts, fnameOpt, tfile.WithMessage(message))
- return opts
+}
+
+// ApplyAlbumCaptions shares one album caption with the items that carry none,
+// so filename templates and message rules see the same text for every file in
+// the group.
+func ApplyAlbumCaptions(ctx context.Context, user *database.User, files []tfile.TGFileMessage) []tfile.TGFileMessage {
+ captions := make(map[int64]string)
+ for _, file := range files {
+ message := file.Message()
+ if message == nil {
+ continue
+ }
+ groupID, grouped := message.GetGroupedID()
+ if !grouped || groupID == 0 {
+ continue
+ }
+ if captions[groupID] == "" && strings.TrimSpace(message.GetMessage()) != "" {
+ captions[groupID] = message.GetMessage()
+ }
+ }
+ if len(captions) == 0 {
+ return files
+ }
+ // Track names already used in each group so a template without a unique
+ // field cannot collapse several album items onto the same path.
+ usedNames := make(map[int64]map[string]struct{})
+ for _, file := range files {
+ message := file.Message()
+ if message == nil {
+ continue
+ }
+ groupID, grouped := message.GetGroupedID()
+ if !grouped || groupID == 0 {
+ continue
+ }
+ if usedNames[groupID] == nil {
+ usedNames[groupID] = make(map[string]struct{})
+ }
+ usedNames[groupID][file.Name()] = struct{}{}
+ }
+
+ result := make([]tfile.TGFileMessage, 0, len(files))
+ for _, file := range files {
+ message := file.Message()
+ if message != nil {
+ if groupID, grouped := message.GetGroupedID(); grouped && groupID != 0 {
+ if caption := captions[groupID]; caption != "" && strings.TrimSpace(message.GetMessage()) == "" {
+ file = applyAlbumCaption(ctx, user, file, caption, usedNames[groupID])
+ }
+ }
+ }
+ result = append(result, file)
+ }
+ return result
+}
+
+type captionOverride struct {
+ tfile.TGFileMessage
+ message *tg.Message
+}
+
+func (f *captionOverride) Message() *tg.Message {
+ return f.message
+}
+
+func applyAlbumCaption(ctx context.Context, user *database.User, file tfile.TGFileMessage, caption string, used map[string]struct{}) tfile.TGFileMessage {
+ message := file.Message()
+ copied := *message
+ copied.Message = caption
+ if name := FilenameFor(ctx, user, &copied); name != "" {
+ if _, exists := used[name]; !exists {
+ used[name] = struct{}{}
+ file.SetName(name)
+ }
+ }
+ return &captionOverride{TGFileMessage: file, message: &copied}
}
func BuildFilenameTemplateData(message *tg.Message) map[string]string {
@@ -99,7 +180,10 @@ func BuildFilenameTemplateData(message *tg.Message) map[string]string {
}(),
MsgGen: tgutil.GenFileNameFromMessage(*message),
OrigName: func() string {
- f, _ := tgutil.GetMediaFileName(message.Media)
+ f, err := tgutil.GetMediaFileName(message.Media)
+ if err != nil {
+ log.FromContext(context.Background()).Debugf("Failed to derive media filename: %v", err)
+ }
return f
}(),
MsgDate: func() string {
@@ -112,26 +196,17 @@ func BuildFilenameTemplateData(message *tg.Message) map[string]string {
}(),
MsgRaw: message.GetMessage(),
ChatID: func() string {
- // 如果消息是频道的(从消息链接中fetch的) 直接使用其chat id,
- // 无论它是否是从其他来源转发的
- if message.GetPost() {
- peer := message.GetPeerID()
- switch p := peer.(type) {
- case *tg.PeerChannel:
- return intToStringOmitZero(p.ChannelID)
- default: // impossible case
- return intToStringOmitZero(tgutil.ChatIdFromPeer(peer))
+ // For channel posts use the channel itself, regardless of the
+ // original forward source. Otherwise prefer the forward source.
+ peer := message.GetPeerID()
+ if !message.GetPost() {
+ if fwdHeader, ok := message.GetFwdFrom(); ok {
+ if fwdFrom, ok := fwdHeader.GetFromID(); ok {
+ peer = fwdFrom
+ }
}
}
- fwdHeader, ok := message.GetFwdFrom()
- if !ok {
- return intToStringOmitZero(tgutil.ChatIdFromPeer(message.GetPeerID()))
- }
- fwdFrom, ok := fwdHeader.GetFromID()
- if !ok {
- return intToStringOmitZero(tgutil.ChatIdFromPeer(message.GetPeerID()))
- }
- return intToStringOmitZero(tgutil.ChatIdFromPeer(fwdFrom))
+ return intToStringOmitZero(tgutil.ChatIdFromPeer(peer))
}(),
}.ToMap()
return data
diff --git a/client/bot/handlers/utils/msgelem/dir.go b/client/bot/handlers/utils/msgelem/dir.go
index c07daaba..def63a10 100644
--- a/client/bot/handlers/utils/msgelem/dir.go
+++ b/client/bot/handlers/utils/msgelem/dir.go
@@ -7,6 +7,7 @@ import (
"github.com/gotd/td/telegram/message/styling"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
)
@@ -27,7 +28,11 @@ func BuildDirHelpStyling(dirs []database.Dir) []styling.StyledTextOption {
var sb strings.Builder
for _, dir := range dirs {
fmt.Fprintf(&sb, "%d: ", dir.ID)
- sb.WriteString(dir.StorageName)
+ storageName := config.C().GetStorageNameByID(dir.StorageID)
+ if storageName == "" {
+ storageName = fmt.Sprintf("#%d", dir.StorageID)
+ }
+ sb.WriteString(storageName)
sb.WriteString(" - ")
sb.WriteString(dir.Path)
sb.WriteString("\n")
diff --git a/client/bot/handlers/utils/msgelem/rule.go b/client/bot/handlers/utils/msgelem/rule.go
index 6c1f6ee4..18f20c5f 100644
--- a/client/bot/handlers/utils/msgelem/rule.go
+++ b/client/bot/handlers/utils/msgelem/rule.go
@@ -7,7 +7,9 @@ import (
"github.com/gotd/td/telegram/message/styling"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
+ rulepkg "github.com/krau/SaveAny-Bot/pkg/rule"
)
func BuildRuleHelpStyling(enabled bool, rules []database.Rule) []styling.StyledTextOption {
@@ -32,7 +34,14 @@ func BuildRuleHelpStyling(enabled bool, rules []database.Rule) []styling.StyledT
styling.Blockquote(func() string {
var sb strings.Builder
for _, rule := range rules {
- ruleText := fmt.Sprintf("%s %s %s %s", rule.Type, rule.Data, rule.StorageName, rule.DirPath)
+ storageName := rulepkg.RuleStorNameChosen
+ if rule.StorageID != 0 {
+ storageName = config.C().GetStorageNameByID(rule.StorageID)
+ if storageName == "" {
+ storageName = fmt.Sprintf("#%d", rule.StorageID)
+ }
+ }
+ ruleText := fmt.Sprintf("%s %s %s %s", rule.Type, rule.Content, storageName, rule.DirPath)
sb.WriteString(fmt.Sprintf("%d: %s\n", rule.ID, ruleText))
}
return sb.String()
diff --git a/client/bot/handlers/utils/msgelem/storage.go b/client/bot/handlers/utils/msgelem/storage.go
index 9a0efb15..4fb90977 100644
--- a/client/bot/handlers/utils/msgelem/storage.go
+++ b/client/bot/handlers/utils/msgelem/storage.go
@@ -6,6 +6,7 @@ import (
"github.com/charmbracelet/log"
"github.com/gotd/td/telegram/message/entity"
+ msgmarkup "github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/cache"
@@ -33,13 +34,13 @@ func BuildAddSelectStorageKeyboard(stors []storage.Storage, adddata tcbdata.Add)
}
}
- buttons := make([]tg.KeyboardButtonClass, 0)
- for _, storage := range stors {
+ buttons := make([]tg.KeyboardInlineButton, 0)
+ for _, st := range stors {
data := tcbdata.Add{
- TaskType: taskType,
- SelectedStorName: storage.Name(),
- SelectedDirPath: adddata.SelectedDirPath,
- ConflictStrategy: adddata.ConflictStrategy,
+ TaskType: taskType,
+ SelectedStorageID: storage.ID(st),
+ SelectedDirPath: adddata.SelectedDirPath,
+ ConflictStrategy: adddata.ConflictStrategy,
Files: adddata.Files,
AsBatch: len(adddata.Files) > 1,
@@ -56,23 +57,23 @@ func BuildAddSelectStorageKeyboard(stors []storage.Storage, adddata tcbdata.Add)
YtdlpURLs: adddata.YtdlpURLs,
YtdlpFlags: adddata.YtdlpFlags,
- TransferSourceStorName: adddata.TransferSourceStorName,
- TransferSourcePath: adddata.TransferSourcePath,
- TransferFiles: adddata.TransferFiles,
+ TransferSourceStorageID: adddata.TransferSourceStorageID,
+ TransferSourcePath: adddata.TransferSourcePath,
+ TransferFiles: adddata.TransferFiles,
}
dataid := xid.New().String()
err := cache.Set(dataid, data)
if err != nil {
return nil, err
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: storage.Name(),
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dataid),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ st.Name(),
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dataid),
+ ))
}
markup := &tg.ReplyInlineMarkup{}
for i := 0; i < len(buttons); i += 3 {
- row := tg.KeyboardButtonRow{}
+ row := tg.KeyboardInlineButtonRow{}
row.Buttons = buttons[i:min(i+3, len(buttons))]
markup.Rows = append(markup.Rows, row)
}
@@ -121,7 +122,7 @@ func BuildConflictStrategyMarkup(adddata tcbdata.Add) (*tg.ReplyInlineMarkup, er
{text: i18n.T(i18nk.BotMsgCommonButtonConflictOverwrite, nil), strategy: tcbdata.ConflictStrategyOverwrite},
{text: i18n.T(i18nk.BotMsgCommonButtonConflictSkip, nil), strategy: tcbdata.ConflictStrategySkip},
}
- buttons := make([]tg.KeyboardButtonClass, 0, len(options))
+ buttons := make([]tg.KeyboardInlineButton, 0, len(options))
for _, opt := range options {
data := adddata
data.ConflictStrategy = opt.strategy
@@ -129,15 +130,15 @@ func BuildConflictStrategyMarkup(adddata tcbdata.Add) (*tg.ReplyInlineMarkup, er
if err := cache.Set(dataid, data); err != nil {
return nil, err
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: opt.text,
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dataid),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ opt.text,
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dataid),
+ ))
}
- rows := make([]tg.KeyboardButtonRow, 0, len(buttons))
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(buttons))
for _, button := range buttons {
- rows = append(rows, tg.KeyboardButtonRow{
- Buttons: []tg.KeyboardButtonClass{button},
+ rows = append(rows, tg.KeyboardInlineButtonRow{
+ Buttons: []tg.KeyboardInlineButton{button},
})
}
return &tg.ReplyInlineMarkup{Rows: rows}, nil
@@ -146,24 +147,24 @@ func BuildConflictStrategyMarkup(adddata tcbdata.Add) (*tg.ReplyInlineMarkup, er
func BuildSetDefaultStorageMarkup(
ctx context.Context,
stors []storage.Storage) (*tg.ReplyInlineMarkup, error) {
- buttons := make([]tg.KeyboardButtonClass, 0)
- for _, storage := range stors {
+ buttons := make([]tg.KeyboardInlineButton, 0)
+ for _, st := range stors {
data := tcbdata.SetDefaultStorage{
- StorageName: storage.Name(),
+ StorageID: storage.ID(st),
}
dataid := xid.New().String()
err := cache.Set(dataid, data)
if err != nil {
return nil, err
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: storage.Name(),
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeSetDefault, dataid),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ st.Name(),
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeSetDefault, dataid),
+ ))
}
markup := &tg.ReplyInlineMarkup{}
for i := 0; i < len(buttons); i += 3 {
- row := tg.KeyboardButtonRow{}
+ row := tg.KeyboardInlineButtonRow{}
row.Buttons = buttons[i:min(i+3, len(buttons))]
markup.Rows = append(markup.Rows, row)
}
@@ -171,27 +172,27 @@ func BuildSetDefaultStorageMarkup(
}
func BuildSetDefaultDirMarkup(ctx context.Context,
- seletedStorage string,
+ storageID uint,
dirs []database.Dir) (*tg.ReplyInlineMarkup, error) {
- buttons := make([]tg.KeyboardButtonClass, 0)
+ buttons := make([]tg.KeyboardInlineButton, 0)
for _, dir := range dirs {
dataid := xid.New().String()
data := tcbdata.SetDefaultStorage{
- StorageName: seletedStorage,
- DirID: dir.ID,
+ StorageID: storageID,
+ DirID: dir.ID,
}
err := cache.Set(dataid, data)
if err != nil {
return nil, err
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: dir.Path,
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeSetDefault, dataid),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ dir.Path,
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeSetDefault, dataid),
+ ))
}
markup := &tg.ReplyInlineMarkup{}
for i := 0; i < len(buttons); i += 3 {
- row := tg.KeyboardButtonRow{}
+ row := tg.KeyboardInlineButtonRow{}
row.Buttons = buttons[i:min(i+3, len(buttons))]
markup.Rows = append(markup.Rows, row)
}
@@ -207,7 +208,7 @@ func BuildSetDirMarkupForAdd(dirs []database.Dir, dataid string) (*tg.ReplyInlin
log.Warnf("Data already has a directory set: %d, %t", data.DirID, data.SettedDir)
return nil, fmt.Errorf("data already has a directory set")
}
- buttons := make([]tg.KeyboardButtonClass, 0)
+ buttons := make([]tg.KeyboardInlineButton, 0)
for _, dir := range dirs {
dirDataId := xid.New().String()
dirData := data
@@ -217,10 +218,10 @@ func BuildSetDirMarkupForAdd(dirs []database.Dir, dataid string) (*tg.ReplyInlin
if err != nil {
return nil, fmt.Errorf("failed to set directory data in cache: %w", err)
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: dir.Path,
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dirDataId),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ dir.Path,
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dirDataId),
+ ))
}
dirDefaultDataId := xid.New().String()
dirDefaultData := data
@@ -230,13 +231,13 @@ func BuildSetDirMarkupForAdd(dirs []database.Dir, dataid string) (*tg.ReplyInlin
if err != nil {
return nil, fmt.Errorf("failed to set default directory data in cache: %w", err)
}
- buttons = append(buttons, &tg.KeyboardButtonCallback{
- Text: i18n.T(i18nk.BotMsgDirButtonDefault, nil),
- Data: fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dirDefaultDataId),
- })
+ buttons = append(buttons, msgmarkup.Callback(
+ i18n.T(i18nk.BotMsgDirButtonDefault, nil),
+ fmt.Appendf(nil, "%s %s", tcbdata.TypeAdd, dirDefaultDataId),
+ ))
markup := &tg.ReplyInlineMarkup{}
for i := 0; i < len(buttons); i += 3 {
- row := tg.KeyboardButtonRow{}
+ row := tg.KeyboardInlineButtonRow{}
row.Buttons = buttons[i:min(i+3, len(buttons))]
markup.Rows = append(markup.Rows, row)
}
diff --git a/client/bot/handlers/utils/ruleutil/rule.go b/client/bot/handlers/utils/ruleutil/rule.go
index 87fa252e..a3691afb 100644
--- a/client/bot/handlers/utils/ruleutil/rule.go
+++ b/client/bot/handlers/utils/ruleutil/rule.go
@@ -6,6 +6,7 @@ import (
"github.com/duke-git/lancet/v2/convertor"
"github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/rule"
"github.com/krau/SaveAny-Bot/pkg/tfile"
@@ -54,9 +55,17 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
}
logger := log.FromContext(ctx)
for _, ur := range rules {
+ storageName := rule.RuleStorNameChosen
+ if ur.StorageID != 0 {
+ storageName = config.C().GetStorageNameByID(ur.StorageID)
+ if storageName == "" {
+ logger.Warnf("Skipping rule %d: unknown storage ID %d", ur.ID, ur.StorageID)
+ continue
+ }
+ }
switch ur.Type {
case rule.FileNameRegex.String():
- ru, err := rule.NewRuleFileNameRegex(ur.StorageName, ur.DirPath, ur.Data)
+ ru, err := rule.NewRuleFileNameRegex(storageName, ur.DirPath, ur.Content)
if err != nil {
logger.Errorf("Failed to create rule: %s", err)
continue
@@ -71,7 +80,7 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
matchedStorageName = matchedStorName(ru.StorageName())
}
case rule.MessageRegex.String():
- ru, err := rule.NewRuleMessageRegex(ur.StorageName, ur.DirPath, ur.Data)
+ ru, err := rule.NewRuleMessageRegex(storageName, ur.DirPath, ur.Content)
if err != nil {
logger.Errorf("Failed to create rule: %s", err)
continue
@@ -86,11 +95,11 @@ func ApplyRule(ctx context.Context, rules []database.Rule, inputs *ruleInput) (m
matchedStorageName = matchedStorName(ru.StorageName())
}
case rule.IsAlbum.String():
- matchAlbum, err := convertor.ToBool(ur.Data)
+ matchAlbum, err := convertor.ToBool(ur.Content)
if err != nil {
matchAlbum = false
}
- ru, err := rule.NewRuleMediaType(ur.StorageName, ur.DirPath, matchAlbum)
+ ru, err := rule.NewRuleMediaType(storageName, ur.DirPath, matchAlbum)
if err != nil {
logger.Errorf("Failed to create rule: %s", err)
continue
diff --git a/client/bot/handlers/utils/shortcut/aria2.go b/client/bot/handlers/utils/shortcut/aria2.go
index 66fda149..1ebab060 100644
--- a/client/bot/handlers/utils/shortcut/aria2.go
+++ b/client/bot/handlers/utils/shortcut/aria2.go
@@ -1,8 +1,6 @@
package shortcut
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/i18n"
@@ -12,10 +10,12 @@ import (
"github.com/krau/SaveAny-Bot/core/tasks/aria2dl"
"github.com/krau/SaveAny-Bot/pkg/aria2"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
-func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, uris []string, aria2Client *aria2.Client, msgID int, userID int64) error {
+func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, uris []string, aria2Client *aria2.Client, msgID int, chatID int64) error {
logger := log.FromContext(ctx)
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
@@ -24,9 +24,9 @@ func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
if len(uris) == 0 {
logger.Error("URIs list is empty")
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
- Message: i18n.T(i18nk.BotMsgDlErrorNoValidLinks, nil),
+ Message: i18n.T(i18nk.BotMsgDownloadErrorNoValidLinks, nil),
})
return dispatcher.EndGroups
}
@@ -34,7 +34,7 @@ func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
gid, err := aria2Client.AddURI(ctx, uris, nil)
if err != nil {
logger.Errorf("Failed to add aria2 download: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgAria2ErrorAddingAria2Download, map[string]any{
"Error": err.Error(),
@@ -44,10 +44,10 @@ func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
}
logger.Infof("Aria2 download added with GID: %s", gid)
- task := aria2dl.NewTask(xid.New().String(), injectCtx, gid, uris, aria2Client, stor, dirPath, aria2dl.NewProgress(msgID, userID))
+ task := aria2dl.NewTask(xid.New().String(), injectCtx, gid, uris, aria2Client, stor, dirPath, aria2dl.NewProgress(msgID, chatID))
if err := core.AddTask(injectCtx, task); err != nil {
logger.Errorf("Failed to add task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -55,7 +55,7 @@ func CreateAndAddAria2TaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
})
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonInfoTaskAdded, nil),
})
diff --git a/client/bot/handlers/utils/shortcut/directlinks.go b/client/bot/handlers/utils/shortcut/directlinks.go
index 9fc0bf3b..34f316bc 100644
--- a/client/bot/handlers/utils/shortcut/directlinks.go
+++ b/client/bot/handlers/utils/shortcut/directlinks.go
@@ -1,8 +1,6 @@
package shortcut
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/i18n"
@@ -11,15 +9,17 @@ import (
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/core/tasks/directlinks"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
-func CreateAndAddDirectTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, links []string, msgID int, userID int64) error {
+func CreateAndAddDirectTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, links []string, msgID int, chatID int64) error {
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
- task := directlinks.NewTask(xid.New().String(), injectCtx, links, stor, dirPath, directlinks.NewProgress(msgID, userID))
+ task := directlinks.NewTask(xid.New().String(), injectCtx, links, stor, dirPath, directlinks.NewProgress(msgID, chatID))
if err := core.AddTask(injectCtx, task); err != nil {
log.FromContext(ctx).Errorf("Failed to add task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -27,7 +27,7 @@ func CreateAndAddDirectTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirP
})
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
Message: i18n.T(i18nk.BotMsgCommonInfoTaskAdded, nil),
})
return dispatcher.EndGroups
diff --git a/client/bot/handlers/utils/shortcut/message.go b/client/bot/handlers/utils/shortcut/message.go
index ef3c5dd6..2b12bc67 100644
--- a/client/bot/handlers/utils/shortcut/message.go
+++ b/client/bot/handlers/utils/shortcut/message.go
@@ -2,14 +2,8 @@
package shortcut
import (
- "encoding/json"
- "fmt"
"net/url"
- "strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
- "github.com/celestix/gotgproto/types"
"github.com/charmbracelet/log"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
@@ -20,12 +14,17 @@ import (
"github.com/krau/SaveAny-Bot/common/cache"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/mediafilter"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/common/utils/tphutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/mediatype"
"github.com/krau/SaveAny-Bot/pkg/telegraph"
"github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
+ "github.com/krau/mygotg/types"
)
func GetFileFromMessageWithReply(ctx *ext.Context, update *ext.Update, message *tg.Message, tfileopts ...tfile.TGFileOption) (replied *types.Message,
@@ -33,10 +32,12 @@ func GetFileFromMessageWithReply(ctx *ext.Context, update *ext.Update, message *
) {
logger := log.FromContext(ctx)
media := message.Media
- supported := mediautil.IsSupported(media)
- if !supported {
+ if !mediautil.IsSupported(media) {
return nil, nil, dispatcher.ContinueGroups
}
+ if !mediafilter.Allowed(media) {
+ return nil, nil, dispatcher.EndGroups
+ }
replied, err = ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonInfoFetchingFileInfo, nil)), nil)
if err != nil {
@@ -77,7 +78,7 @@ func GetFilesFromUpdateLinkMessageWithReplyEdit(ctx *ext.Context, update *ext.Up
logger.Errorf("failed to edit message: %s", err)
}
}
- user, err := database.GetUserByChatID(ctx, update.GetUserChat().GetID())
+ user, err := database.GetUserByChatID(ctx, tgutil.SenderID(update))
if err != nil {
logger.Errorf("failed to get user from db: %s", err)
editReplied(i18n.T(i18nk.BotMsgCommonErrorGetUserInfoFailed, map[string]any{
@@ -96,6 +97,10 @@ func GetFilesFromUpdateLinkMessageWithReplyEdit(ctx *ext.Context, update *ext.Up
logger.Debugf("message %d has no media", msg.GetID())
return
}
+ if !mediautil.IsSupported(media) || !mediafilter.Allowed(media) {
+ logger.Debugf("message %d media type is filtered", msg.GetID())
+ return
+ }
opts := mediautil.TfileOptions(ctx, user, msg)
file, err := tfile.FromMediaMessage(media, client, msg, opts...)
if err != nil {
@@ -169,12 +174,16 @@ type TelegraphResult struct {
func GetTphPicsFromMessageWithReply(ctx *ext.Context, update *ext.Update) (*types.Message, *TelegraphResult, error) {
logger := log.FromContext(ctx)
+ if !mediafilter.Allows(mediatype.Image) {
+ ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorNoSavableFilesFound)), nil)
+ return nil, nil, dispatcher.EndGroups
+ }
tphurl := findTelegraphURL(update.EffectiveMessage.Message)
if tphurl == "" {
logger.Warnf("No telegraph url found but called handleTelegraph")
return nil, nil, dispatcher.ContinueGroups
}
- pagepath, err := parseTelegraphPagePath(tphurl)
+ pagepath, err := tphutil.ParsePagePath(tphurl)
if err != nil {
logger.Errorf("Failed to parse telegraph path: %s", err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorParseTelegraphPathFailed, map[string]any{
@@ -198,32 +207,7 @@ func GetTphPicsFromMessageWithReply(ctx *ext.Context, update *ext.Update) (*type
}
imgs := make([]string, 0)
for _, elem := range page.Content {
- var node telegraph.NodeElement
- data, err := json.Marshal(elem)
- if err != nil {
- logger.Errorf("Failed to marshal element: %s", err)
- continue
- }
- err = json.Unmarshal(data, &node)
- if err != nil {
- logger.Errorf("Failed to unmarshal element: %s", err)
- continue
- }
-
- if len(node.Children) != 0 {
- for _, child := range node.Children {
- imgs = append(imgs, tphutil.GetNodeImages(child)...)
- }
- }
- if node.Tag == "img" {
- if src, ok := node.Attrs["src"]; ok {
- if strings.HasPrefix(src, "/file/") {
- // handle images on telegra.ph server
- src = "https://telegra.ph" + src
- }
- imgs = append(imgs, src)
- }
- }
+ imgs = append(imgs, tphutil.GetNodeImages(elem)...)
}
if len(imgs) == 0 {
logger.Warn("No images found in telegraph page")
@@ -247,7 +231,7 @@ func findTelegraphURL(msg *tg.Message) string {
if firstMatch == "" {
firstMatch = tphurl
}
- if _, err := parseTelegraphPagePath(tphurl); err == nil {
+ if _, err := tphutil.ParsePagePath(tphurl); err == nil {
return tphurl
}
}
@@ -263,26 +247,3 @@ func findTelegraphURL(msg *tg.Message) string {
}
return firstMatch
}
-
-func parseTelegraphPagePath(pageURL string) (string, error) {
- u, err := url.Parse(pageURL)
- if err != nil {
- return "", fmt.Errorf("invalid telegraph URL: %w", err)
- }
- if u.Scheme != "https" || !strings.EqualFold(u.Hostname(), "telegra.ph") {
- return "", fmt.Errorf("invalid telegraph URL host: %s", u.Host)
- }
- pagepath := strings.Trim(u.EscapedPath(), "/")
- if pagepath == "" || strings.Contains(pagepath, "/") {
- return "", fmt.Errorf("invalid telegraph URL path: %s", u.Path)
- }
- pagepath, err = url.PathUnescape(pagepath)
- if err != nil {
- return "", fmt.Errorf("failed to unescape telegraph path: %w", err)
- }
- pagepath = strings.TrimSpace(pagepath)
- if pagepath == "" || strings.Contains(pagepath, "/") {
- return "", fmt.Errorf("invalid telegraph URL path: %s", u.Path)
- }
- return pagepath, nil
-}
diff --git a/client/bot/handlers/utils/shortcut/message_telegraph_test.go b/client/bot/handlers/utils/shortcut/message_telegraph_test.go
index 1567575c..5071a877 100644
--- a/client/bot/handlers/utils/shortcut/message_telegraph_test.go
+++ b/client/bot/handlers/utils/shortcut/message_telegraph_test.go
@@ -4,6 +4,7 @@ import (
"testing"
"github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/common/utils/tphutil"
)
func TestFindTelegraphURL(t *testing.T) {
@@ -145,18 +146,18 @@ func TestParseTelegraphPagePath(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- got, err := parseTelegraphPagePath(tt.pageURL)
+ got, err := tphutil.ParsePagePath(tt.pageURL)
if tt.wantErr {
if err == nil {
- t.Fatalf("parseTelegraphPagePath(%q) returned no error", tt.pageURL)
+ t.Fatalf("tphutil.ParsePagePath(%q) returned no error", tt.pageURL)
}
return
}
if err != nil {
- t.Fatalf("parseTelegraphPagePath(%q) failed: %v", tt.pageURL, err)
+ t.Fatalf("tphutil.ParsePagePath(%q) failed: %v", tt.pageURL, err)
}
if got != tt.want {
- t.Fatalf("parseTelegraphPagePath(%q) = %q, want %q", tt.pageURL, got, tt.want)
+ t.Fatalf("tphutil.ParsePagePath(%q) = %q, want %q", tt.pageURL, got, tt.want)
}
})
}
diff --git a/client/bot/handlers/utils/shortcut/parsed.go b/client/bot/handlers/utils/shortcut/parsed.go
index 12863d9a..d27f71ce 100644
--- a/client/bot/handlers/utils/shortcut/parsed.go
+++ b/client/bot/handlers/utils/shortcut/parsed.go
@@ -1,8 +1,6 @@
package shortcut
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
@@ -13,15 +11,17 @@ import (
parsed "github.com/krau/SaveAny-Bot/core/tasks/parsed"
"github.com/krau/SaveAny-Bot/pkg/parser"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
-func CreateAndAddParsedTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, item *parser.Item, msgID int, userID int64) error {
+func CreateAndAddParsedTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, item *parser.Item, msgID int, chatID int64) error {
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
- task := parsed.NewTask(xid.New().String(), injectCtx, stor, dirPath, item, parsed.NewProgress(msgID, userID))
+ task := parsed.NewTask(xid.New().String(), injectCtx, stor, dirPath, item, parsed.NewProgress(msgID, chatID))
if err := core.AddTask(injectCtx, task); err != nil {
log.FromContext(ctx).Errorf("Failed to add task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -30,7 +30,7 @@ func CreateAndAddParsedTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirP
return dispatcher.EndGroups
}
text, entities := msgelem.BuildTaskAddedEntities(ctx, item.Title, core.GetLength(ctx))
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: text,
Entities: entities,
diff --git a/client/bot/handlers/utils/shortcut/tftask.go b/client/bot/handlers/utils/shortcut/tftask.go
index d6f5d5c0..5ec7e947 100644
--- a/client/bot/handlers/utils/shortcut/tftask.go
+++ b/client/bot/handlers/utils/shortcut/tftask.go
@@ -1,19 +1,20 @@
package shortcut
import (
+ "fmt"
"path"
"strings"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/conflictutil"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/ruleutil"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/core/tasks/batchtfile"
tftask "github.com/krau/SaveAny-Bot/core/tasks/tfile"
@@ -22,16 +23,18 @@ import (
"github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
-func CreateAndAddTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor storage.Storage, dirPath string, file tfile.TGFileMessage, trackMsgID int, conflictStrategy ...string) error {
+func CreateAndAddTGFileTaskWithEdit(ctx *ext.Context, userID, chatID int64, stor storage.Storage, dirPath string, file tfile.TGFileMessage, trackMsgID int, conflictStrategy ...string) error {
logger := log.FromContext(ctx)
strategy := selectedConflictStrategy(conflictStrategy)
user, err := database.GetUserByChatID(ctx, userID)
if err != nil {
logger.Errorf("Failed to get user by chat ID: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorGetUserWithErrFailed, map[string]any{
"Error": err.Error(),
@@ -52,7 +55,7 @@ func CreateAndAddTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor storage
stor, err = storage.GetStorageByUserIDAndName(ctx, user.ChatID, matchedStorageName.String())
if err != nil {
logger.Errorf("Failed to get storage by user ID and name: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
"Error": err.Error(),
@@ -65,12 +68,15 @@ func CreateAndAddTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor storage
startCreateTask:
storagePath := path.Join(dirPath, file.Name())
if strategy == tcbdata.ConflictStrategyAsk || strategy == tcbdata.ConflictStrategySkip {
- exists := stor.Exists(ctx, storagePath)
+ exists, err := stor.Exists(ctx, storagePath)
+ if err != nil {
+ return fmt.Errorf("failed to check storage path %s: %w", storagePath, err)
+ }
if exists && strategy == tcbdata.ConflictStrategyAsk {
- return promptTGFileConflictStrategy(ctx, userID, stor.Name(), dirPath, []tfile.TGFileMessage{file}, false, []string{conflictutil.FormatPath(stor.Name(), storagePath)}, trackMsgID)
+ return promptTGFileConflictStrategy(ctx, chatID, stor.Name(), dirPath, []tfile.TGFileMessage{file}, false, []string{conflictutil.FormatPath(stor.Name(), storagePath)}, trackMsgID)
}
if exists {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonInfoAllConflictFilesSkipped, map[string]any{
"Skipped": file.Name(),
@@ -88,10 +94,10 @@ startCreateTask:
task, err := tftask.NewTGFileTask(taskid, injectCtx, file, stor, storagePath,
tftask.NewProgressTrack(
trackMsgID,
- userID))
+ chatID))
if err != nil {
logger.Errorf("create task failed: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskCreateFailed, map[string]any{
"Error": err.Error(),
@@ -101,7 +107,7 @@ startCreateTask:
}
if err := core.AddTask(injectCtx, task); err != nil {
logger.Errorf("add task failed: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -110,7 +116,7 @@ startCreateTask:
return dispatcher.EndGroups
}
text, entities := msgelem.BuildTaskAddedEntities(ctx, file.Name(), core.GetLength(injectCtx))
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: text,
Entities: entities,
@@ -119,13 +125,13 @@ startCreateTask:
return dispatcher.EndGroups
}
-func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor storage.Storage, dirPath string, files []tfile.TGFileMessage, trackMsgID int, conflictStrategy ...string) error {
+func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID, chatID int64, stor storage.Storage, dirPath string, files []tfile.TGFileMessage, trackMsgID int, conflictStrategy ...string) error {
logger := log.FromContext(ctx)
strategy := selectedConflictStrategy(conflictStrategy)
user, err := database.GetUserByChatID(ctx, userID)
if err != nil {
logger.Errorf("Failed to get user by chat ID: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorGetUserWithErrFailed, map[string]any{
"Error": err.Error(),
@@ -134,6 +140,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
return dispatcher.EndGroups
}
strategy = conflictutil.ResolveStrategy(user, strategy)
+ files = mediautil.ApplyAlbumCaptions(ctx, user, files)
useRule := user.ApplyRule && user.Rules != nil
@@ -168,7 +175,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
fileStor, err = storage.GetStorageByUserIDAndName(ctx, user.ChatID, storName)
if err != nil {
logger.Errorf("Failed to get storage by user ID and name: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorGetStorageFailed, map[string]any{
"Error": err.Error(),
@@ -180,7 +187,10 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
if !matchedDirPath.NeedNewForAlbum() {
storPath := path.Join(matchedDirPath.String(), file.Name())
if strategy == tcbdata.ConflictStrategyAsk || strategy == tcbdata.ConflictStrategySkip {
- exists := fileStor.Exists(ctx, storPath)
+ exists, err := fileStor.Exists(ctx, storPath)
+ if err != nil {
+ return fmt.Errorf("failed to check storage path %s: %w", storPath, err)
+ }
if exists && strategy == tcbdata.ConflictStrategyAsk {
conflicts = append(conflicts, conflictutil.FormatPath(fileStor.Name(), storPath))
continue
@@ -193,7 +203,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
elem, err := batchtfile.NewTaskElement(fileStor, storPath, file)
if err != nil {
logger.Errorf("Failed to create task element: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskCreateFailed, map[string]any{
"Error": err.Error(),
@@ -223,7 +233,35 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
}
}
for _, afiles := range albumFiles {
- if len(afiles) <= 1 {
+ if len(afiles) == 1 {
+ af := afiles[0]
+ storPath := path.Join(af.dirPath, af.file.Name())
+ if strategy == tcbdata.ConflictStrategyAsk || strategy == tcbdata.ConflictStrategySkip {
+ exists, err := af.storage.Exists(ctx, storPath)
+ if err != nil {
+ return fmt.Errorf("failed to check storage path %s: %w", storPath, err)
+ }
+ if exists && strategy == tcbdata.ConflictStrategyAsk {
+ conflicts = append(conflicts, conflictutil.FormatPath(af.storage.Name(), storPath))
+ continue
+ }
+ if exists {
+ skipped = append(skipped, af.file.Name())
+ continue
+ }
+ }
+ elem, err := batchtfile.NewTaskElement(af.storage, storPath, af.file)
+ if err != nil {
+ logger.Errorf("Failed to create task element for album file: %s", err)
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
+ ID: trackMsgID,
+ Message: i18n.T(i18nk.BotMsgCommonErrorTaskCreateFailed, map[string]any{
+ "Error": err.Error(),
+ }),
+ })
+ return dispatcher.EndGroups
+ }
+ elems = append(elems, *elem)
continue
}
// 对于需要新建目录的文件, 将第一个文件的文件名(去除扩展名)作为目录名
@@ -233,7 +271,10 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
for _, af := range afiles {
afstorPath := path.Join(af.dirPath, albumDir, af.file.Name())
if strategy == tcbdata.ConflictStrategyAsk || strategy == tcbdata.ConflictStrategySkip {
- exists := albumStor.Exists(ctx, afstorPath)
+ exists, err := albumStor.Exists(ctx, afstorPath)
+ if err != nil {
+ return fmt.Errorf("failed to check storage path %s: %w", afstorPath, err)
+ }
if exists && strategy == tcbdata.ConflictStrategyAsk {
conflicts = append(conflicts, conflictutil.FormatPath(albumStor.Name(), afstorPath))
continue
@@ -246,7 +287,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
elem, err := batchtfile.NewTaskElement(albumStor, afstorPath, af.file)
if err != nil {
logger.Errorf("Failed to create task element for album file: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskCreateFailed, map[string]any{
"Error": err.Error(),
@@ -259,7 +300,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
}
if strategy == tcbdata.ConflictStrategyAsk && len(conflicts) > 0 {
- return promptTGFileConflictStrategy(ctx, userID, stor.Name(), dirPath, files, true, conflicts, trackMsgID)
+ return promptTGFileConflictStrategy(ctx, chatID, stor.Name(), dirPath, files, true, conflicts, trackMsgID)
}
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
@@ -267,7 +308,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
injectCtx = storage.WithOverwrite(injectCtx)
}
if len(elems) == 0 {
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonInfoAllConflictFilesSkipped, map[string]any{
"Skipped": strings.Join(skipped, "\n"),
@@ -277,10 +318,10 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
return dispatcher.EndGroups
}
taskid := xid.New().String()
- task := batchtfile.NewBatchTGFileTask(taskid, injectCtx, elems, batchtfile.NewProgressTrackerWithSkipped(trackMsgID, userID, skipped), true)
+ task := batchtfile.NewBatchTGFileTask(taskid, injectCtx, elems, batchtfile.NewProgressTrackerWithSkipped(trackMsgID, chatID, skipped), true)
if err := core.AddTask(injectCtx, task); err != nil {
logger.Errorf("Failed to add batch task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -288,7 +329,7 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
})
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: buildBatchAddedMessage(len(elems), skipped),
ReplyMarkup: nil,
@@ -296,19 +337,19 @@ func CreateAndAddBatchTGFileTaskWithEdit(ctx *ext.Context, userID int64, stor st
return dispatcher.EndGroups
}
-func promptTGFileConflictStrategy(ctx *ext.Context, userID int64, storageName, dirPath string, files []tfile.TGFileMessage, asBatch bool, conflicts []string, trackMsgID int) error {
+func promptTGFileConflictStrategy(ctx *ext.Context, chatID int64, storageName, dirPath string, files []tfile.TGFileMessage, asBatch bool, conflicts []string, trackMsgID int) error {
markup, err := msgelem.BuildConflictStrategyMarkup(tcbdata.Add{
- TaskType: tasktype.TaskTypeTgfiles,
- SelectedStorName: storageName,
- SettedDir: true,
- SelectedDirPath: dirPath,
- Files: files,
- AsBatch: asBatch,
+ TaskType: tasktype.TaskTypeTgfiles,
+ SelectedStorageID: config.C().GetStorageIDByName(storageName),
+ SettedDir: true,
+ SelectedDirPath: dirPath,
+ Files: files,
+ AsBatch: asBatch,
})
if err != nil {
return err
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonPromptSelectConflictStrategy, map[string]any{"Files": conflictutil.FormatPaths(conflicts)}),
ReplyMarkup: markup,
diff --git a/client/bot/handlers/utils/shortcut/tphtask.go b/client/bot/handlers/utils/shortcut/tphtask.go
index c2d0d54b..5fd5fd7f 100644
--- a/client/bot/handlers/utils/shortcut/tphtask.go
+++ b/client/bot/handlers/utils/shortcut/tphtask.go
@@ -1,8 +1,6 @@
package shortcut
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
@@ -14,12 +12,14 @@ import (
tphtask "github.com/krau/SaveAny-Bot/core/tasks/telegraph"
"github.com/krau/SaveAny-Bot/pkg/telegraph"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
func CreateAndAddtelegraphWithEdit(
ctx *ext.Context,
- userID int64,
+ chatID int64,
tphpage *telegraph.Page,
dirPath string, // unescaped ph path for file storage
pics []string,
@@ -34,11 +34,11 @@ func CreateAndAddtelegraphWithEdit(
stor,
dirPath,
tphutil.DefaultClient(),
- tphtask.NewProgress(trackMsgID, userID),
+ tphtask.NewProgress(trackMsgID, chatID),
)
if err := core.AddTask(injectCtx, task); err != nil {
log.FromContext(ctx).Errorf("Failed to add task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -47,7 +47,7 @@ func CreateAndAddtelegraphWithEdit(
return dispatcher.EndGroups
}
text, entities := msgelem.BuildTaskAddedEntities(ctx, tphpage.Title, core.GetLength(ctx))
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: trackMsgID,
Message: text,
Entities: entities,
diff --git a/client/bot/handlers/utils/shortcut/ytdlp.go b/client/bot/handlers/utils/shortcut/ytdlp.go
index 2c2325e1..245c16be 100644
--- a/client/bot/handlers/utils/shortcut/ytdlp.go
+++ b/client/bot/handlers/utils/shortcut/ytdlp.go
@@ -1,10 +1,10 @@
package shortcut
import (
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
"github.com/krau/SaveAny-Bot/common/i18n"
@@ -15,15 +15,15 @@ import (
"github.com/krau/SaveAny-Bot/storage"
)
-func CreateAndAddYtdlpTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, urls []string, flags []string, msgID int, userID int64) error {
+func CreateAndAddYtdlpTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPath string, urls []string, flags []string, msgID int, chatID int64) error {
logger := log.FromContext(ctx)
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
if len(urls) == 0 {
logger.Error("URLs list is empty")
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
- Message: i18n.T(i18nk.BotMsgYtdlpErrorNoValidUrls, nil),
+ Message: i18n.T(i18nk.BotMsgDownloadErrorNoValidLinks, nil),
})
return dispatcher.EndGroups
}
@@ -37,12 +37,12 @@ func CreateAndAddYtdlpTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
flags,
stor,
dirPath,
- ytdlp.NewProgress(msgID, userID),
+ ytdlp.NewProgress(msgID, chatID),
)
if err := core.AddTask(injectCtx, task); err != nil {
logger.Errorf("Failed to add yt-dlp task: %s", err)
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonErrorTaskAddFailed, map[string]any{
"Error": err.Error(),
@@ -51,7 +51,7 @@ func CreateAndAddYtdlpTaskWithEdit(ctx *ext.Context, stor storage.Storage, dirPa
return dispatcher.EndGroups
}
- ctx.EditMessage(userID, &tg.MessagesEditMessageRequest{
+ ctx.EditMessage(chatID, &tg.MessagesEditMessageRequest{
ID: msgID,
Message: i18n.T(i18nk.BotMsgCommonInfoTaskAdded, nil),
})
diff --git a/client/bot/handlers/watch.go b/client/bot/handlers/watch.go
index da62720a..16af1e83 100644
--- a/client/bot/handlers/watch.go
+++ b/client/bot/handlers/watch.go
@@ -1,30 +1,37 @@
package handlers
import (
+ "context"
"fmt"
"path"
"regexp"
+ "strconv"
"strings"
"sync"
"text/template"
"time"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/message/markup"
+ "github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/mediautil"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/ruleutil"
userclient "github.com/krau/SaveAny-Bot/client/user"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
+ "github.com/krau/SaveAny-Bot/common/mediafilter"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
coretfile "github.com/krau/SaveAny-Bot/core/tasks/tfile"
"github.com/krau/SaveAny-Bot/database"
"github.com/krau/SaveAny-Bot/pkg/enums/fnamest"
+ "github.com/krau/SaveAny-Bot/pkg/tcbdata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
@@ -32,20 +39,17 @@ func handleWatchCmd(ctx *ext.Context, update *ext.Update) error {
logger := log.FromContext(ctx)
args := strings.Split(update.EffectiveMessage.Text, " ")
if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchHelpText)), nil)
+ ctx.Reply(update, ext.ReplyTextString(watchMenuText("")), &ext.ReplyOpts{Markup: watchMenuMarkup()})
return dispatcher.EndGroups
}
- userChatID := update.GetUserChat().GetID()
+ userChatID := responsibleUserID(update)
user, err := database.GetUserByChatID(ctx, userChatID)
if err != nil {
logger.Errorf("Failed to get user: %s", err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetUserFailed)), nil)
return dispatcher.EndGroups
}
- if args[1] == "notify" {
- return handleWatchNotifyCmd(ctx, update, user, args[2:])
- }
- if user.DefaultStorage == "" {
+ if user.DefaultStorageID == 0 {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorDefaultStorageNotSet)), nil)
return dispatcher.EndGroups
}
@@ -67,9 +71,8 @@ func handleWatchCmd(ctx *ext.Context, update *ext.Update) error {
filter := ""
if len(args) > 2 {
filterArg := strings.Join(args[2:], " ")
- filterType, _, _ := strings.Cut(filterArg, ":")
- filterData := strings.Split(filterArg, ":")[1]
- if filterType == "" || filterData == "" {
+ filterType, filterData, ok := strings.Cut(filterArg, ":")
+ if !ok || filterType == "" || filterData == "" {
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchErrorFilterFormatInvalid)), nil)
return dispatcher.EndGroups
}
@@ -87,9 +90,9 @@ func handleWatchCmd(ctx *ext.Context, update *ext.Update) error {
}
}
if err := user.WatchChat(ctx, database.WatchChat{
- UserID: user.ID,
- ChatID: chatID,
- Filter: filter,
+ UserID: user.ID,
+ ChatID: chatID,
+ MessageFilter: filter,
}); err != nil {
logger.Errorf("Failed to watch chat %d: %s", chatID, err)
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchErrorWatchChatFailed, map[string]any{"Error": err.Error()})), nil)
@@ -99,63 +102,171 @@ func handleWatchCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}
-func handleLswatchCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- userChatID := update.GetUserChat().GetID()
- user, err := database.GetUserByChatID(ctx, userChatID)
- if err != nil {
- logger.Errorf("Failed to get user: %s", err)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetUserFailed)), nil)
- return dispatcher.EndGroups
+func watchMenuText(extra string) string {
+ text := i18n.T(i18nk.BotMsgWatchMenuTitle) + "\n\n" + i18n.T(i18nk.BotMsgWatchMenuHint)
+ if extra != "" {
+ text += "\n\n" + extra
+ }
+ return text
+}
+
+func watchMenuMarkup() *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuAdd), fmt.Appendf(nil, "%s add", tcbdata.TypeWatch)),
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuList), fmt.Appendf(nil, "%s list", tcbdata.TypeWatch)),
+ }},
+ {Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuNotify), fmt.Appendf(nil, "%s notify", tcbdata.TypeWatch)),
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuSync), fmt.Appendf(nil, "%s sync", tcbdata.TypeWatch)),
+ }},
+ }}
+}
+
+func watchBackMarkup() *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{Rows: []tg.KeyboardInlineButtonRow{{
+ Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuBack), fmt.Appendf(nil, "%s menu", tcbdata.TypeWatch)),
+ },
+ }}}
+}
+
+func handleWatchCallback(ctx *ext.Context, update *ext.Update) error {
+ args := strings.Fields(string(update.CallbackQuery.Data))
+ if len(args) < 2 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
}
- chats := user.WatchChats
+ switch args[1] {
+ case "menu":
+ return editWatchMenu(ctx, update)
+ case "add":
+ return editWatchAdd(ctx, update)
+ case "list":
+ return editWatchList(ctx, update)
+ case "notify":
+ return editWatchNotify(ctx, update)
+ case "sync":
+ return editWatchSync(ctx, update)
+ case "rm":
+ return editWatchRemove(ctx, update, args)
+ default:
+ return editWatchMenu(ctx, update)
+ }
+}
+
+func editWatchMenu(ctx *ext.Context, update *ext.Update) error {
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: watchMenuText(""),
+ ReplyMarkup: watchMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func editWatchAdd(ctx *ext.Context, update *ext.Update) error {
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: watchMenuText(i18n.T(i18nk.BotMsgWatchMenuAddHelp)),
+ ReplyMarkup: watchBackMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func watchListText(chats []database.WatchChat) string {
if len(chats) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchInfoWatchListEmpty)), nil)
- return dispatcher.EndGroups
+ return i18n.T(i18nk.BotMsgWatchInfoWatchListEmpty)
}
- var sb strings.Builder
- sb.WriteString(i18n.T(i18nk.BotMsgWatchInfoWatchListHeader))
+ var b strings.Builder
+ b.WriteString(i18n.T(i18nk.BotMsgWatchInfoWatchListHeader))
for _, chat := range chats {
- sb.WriteString("- ")
- sb.WriteString(fmt.Sprintf("%d", chat.ChatID))
- if chat.Filter != "" {
- sb.WriteString(i18n.T(i18nk.BotMsgWatchInfoWatchListFilterPrefix))
- sb.WriteString(chat.Filter)
- sb.WriteString(")")
+ fmt.Fprintf(&b, "\n- %d", chat.ChatID)
+ if chat.MessageFilter != "" {
+ b.WriteString(i18n.T(i18nk.BotMsgWatchInfoWatchListFilterPrefix))
+ b.WriteString(chat.MessageFilter)
+ b.WriteString(")")
}
- sb.WriteString("\n")
}
- ctx.Reply(update, ext.ReplyTextString(sb.String()), nil)
+ return b.String()
+}
+
+func watchListMarkup(chats []database.WatchChat) *tg.ReplyInlineMarkup {
+ rows := make([]tg.KeyboardInlineButtonRow, 0, len(chats)+1)
+ for _, chat := range chats {
+ rows = append(rows, tg.KeyboardInlineButtonRow{Buttons: []tg.KeyboardInlineButton{
+ markup.Callback(i18n.T(i18nk.BotMsgWatchMenuStop), fmt.Appendf(nil, "%s rm %d", tcbdata.TypeWatch, chat.ChatID)),
+ }})
+ }
+ rows = append(rows, watchBackMarkup().Rows...)
+ return &tg.ReplyInlineMarkup{Rows: rows}
+}
+
+func editWatchList(ctx *ext.Context, update *ext.Update) error {
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: watchListText(user.WatchChats),
+ ReplyMarkup: watchListMarkup(user.WatchChats),
+ })
return dispatcher.EndGroups
}
-func handleUnwatchCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- args := strings.Split(update.EffectiveMessage.Text, " ")
- if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchErrorUnwatchNoChatProvided)), nil)
- return dispatcher.EndGroups
+func editWatchNotify(ctx *ext.Context, update *ext.Update) error {
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
}
- userChatID := update.GetUserChat().GetID()
- user, err := database.GetUserByChatID(ctx, userChatID)
+ user.WatchNotify = !user.WatchNotify
+ if err := database.UpdateUser(ctx, user); err != nil {
+ return err
+ }
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: watchMenuText(i18n.T(watchNotifyStateKey(user.WatchNotify))),
+ ReplyMarkup: watchMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func editWatchSync(ctx *ext.Context, update *ext.Update) error {
+ count, err := syncPeers(ctx)
if err != nil {
- logger.Errorf("Failed to get user: %s", err)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorGetUserFailed)), nil)
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: i18n.T(i18nk.BotMsgSyncpeersFailed, map[string]any{"Error": err.Error()}),
+ })
return dispatcher.EndGroups
}
- chatArg := args[1]
- chatID, err := tgutil.ParseChatID(ctx, chatArg)
+ ctx.EditMessage(chatID(update), &tg.MessagesEditMessageRequest{
+ ID: update.CallbackQuery.GetMsgID(),
+ Message: watchMenuText(i18n.T(i18nk.BotMsgSyncpeersSuccess, map[string]any{"Count": count})),
+ ReplyMarkup: watchMenuMarkup(),
+ })
+ return dispatcher.EndGroups
+}
+
+func editWatchRemove(ctx *ext.Context, update *ext.Update, args []string) error {
+ if len(args) < 3 {
+ return fmt.Errorf("invalid callback data: %q", update.CallbackQuery.Data)
+ }
+ chatID, err := strconv.ParseInt(args[2], 10, 64)
if err != nil {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorInvalidIdOrUsername, map[string]any{"Error": err.Error()})), nil)
- return dispatcher.EndGroups
+ return err
+ }
+ userID := update.CallbackQuery.GetUserID()
+ user, err := database.GetUserByChatID(ctx, userID)
+ if err != nil {
+ return err
}
if err := user.UnwatchChat(ctx, chatID); err != nil {
- logger.Errorf("Failed to unwatch chat %d: %s", chatID, err)
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchErrorUnwatchChatFailed, map[string]any{"Error": err.Error()})), nil)
+ ctx.AnswerCallback(msgelem.AlertCallbackAnswer(update.CallbackQuery.GetQueryID(), i18n.T(i18nk.BotMsgWatchErrorUnwatchChatFailed, map[string]any{"Error": err.Error()})))
return dispatcher.EndGroups
}
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchInfoWatchChatStopped, map[string]any{"Chat": chatArg})), nil)
- return dispatcher.EndGroups
+ return editWatchList(ctx, update)
}
type watchMediaGroupHandler struct {
@@ -205,13 +316,24 @@ func (w *watchMediaGroupHandler) addFile(chatID int64, userID uint, file tfile.T
})
}
-func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent, botCtx *ext.Context) {
- if userclient.GetCtx() == nil {
- return
- }
- logger := log.FromContext(userclient.GetCtx())
- for event := range ch {
+// ListenUserbotMediaEvents consumes userbot media events until the channel is
+// closed. resolve returns the client context used for notifications.
+func ListenUserbotMediaEvents(runCtx context.Context, resolve func() *ext.Context) {
+ logger := log.FromContext(runCtx)
+ for event := range userclient.GetMediaMessageCh() {
+ if event.File == nil || event.Ctx == nil {
+ continue
+ }
+ botCtx := resolve()
+ if botCtx == nil {
+ continue
+ }
logger.Debug("Received media message event", "chat_id", event.ChatID, "file_name", event.File.Name())
+ message := event.File.Message()
+ if message == nil || !mediafilter.Allowed(message.Media) {
+ logger.Debug("Skipping filtered media message", "chat_id", event.ChatID, "file_name", event.File.Name())
+ continue
+ }
ctx := event.Ctx
file := event.File
chats, err := database.GetWatchChatsByChatID(ctx, event.ChatID)
@@ -219,10 +341,10 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent, botCtx *ext.C
logger.Errorf("Failed to get watch chats for chat ID %d: %v", event.ChatID, err)
continue
}
- msgText := event.File.Message().GetMessage()
+ msgText := message.GetMessage()
for _, chat := range chats {
- if chat.Filter != "" {
- filter := strings.Split(chat.Filter, ":")
+ if chat.MessageFilter != "" {
+ filter := strings.Split(chat.MessageFilter, ":")
if len(filter) != 2 {
logger.Warnf("Invalid filter format in chat %d, skipping", chat.ChatID)
continue
@@ -246,18 +368,18 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent, botCtx *ext.C
logger.Errorf("Failed to get user by ID %d: %v", chat.UserID, err)
continue
}
- if user.DefaultStorage == "" {
+ if user.DefaultStorageID == 0 {
logger.Warnf("User %d has no default storage set, skipping media message handling", chat.UserID)
continue
}
- stor, err := storage.GetStorageByUserIDAndName(ctx, user.ChatID, user.DefaultStorage)
+ stor, err := storage.GetStorageByUserIDAndID(ctx, user.ChatID, user.DefaultStorageID)
if err != nil {
- logger.Errorf("Failed to get storage by user ID %d and name %s: %v", user.ChatID, user.DefaultStorage, err)
+ logger.Errorf("Failed to get storage ID %d for user %d: %v", user.DefaultStorageID, user.ChatID, err)
continue
}
var defaultDirPath string
- if user.DefaultDir != 0 {
- dir, err := database.GetDirByID(ctx, user.DefaultDir)
+ if user.DefaultDirID != 0 {
+ dir, err := database.GetDirByID(ctx, user.DefaultDirID)
if err != nil {
logger.Warnf("Failed to get default dir for user %d: %v, using root", user.ChatID, err)
} else {
@@ -343,6 +465,7 @@ func processWatchMediaGroup(ctx *ext.Context, botCtx *ext.Context, user *databas
}
useRule := user.ApplyRule && user.Rules != nil
+ files = mediautil.ApplyAlbumCaptions(ctx, user, files)
applyRule := func(file tfile.TGFileMessage) (string, ruleutil.MatchedDirPath) {
if !useRule {
@@ -404,7 +527,19 @@ func processWatchMediaGroup(ctx *ext.Context, botCtx *ext.Context, user *databas
injectCtx := tgutil.ExtWithContext(ctx.Context, ctx)
totalTasks := 0
for groupID, afiles := range albumFiles {
- if len(afiles) <= 1 {
+ if len(afiles) == 1 {
+ af := afiles[0]
+ taskid := xid.New().String()
+ task, err := coretfile.NewTGFileTask(taskid, injectCtx, af.file, af.storage, path.Join(af.dirPath, af.file.Name()), newWatchNotifyProgress(ctx, botCtx, user))
+ if err != nil {
+ logger.Errorf("create task failed for album file: %s", err)
+ continue
+ }
+ if err := core.AddTask(injectCtx, task); err != nil {
+ logger.Errorf("add task failed: %s", err)
+ continue
+ }
+ totalTasks++
continue
}
diff --git a/client/bot/handlers/watch_notify.go b/client/bot/handlers/watch_notify.go
index c1a0c3ec..a1c8c370 100644
--- a/client/bot/handlers/watch_notify.go
+++ b/client/bot/handlers/watch_notify.go
@@ -2,13 +2,11 @@ package handlers
import (
"context"
- "strings"
"sync"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
+ "github.com/krau/mygotg/ext"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
@@ -17,39 +15,6 @@ import (
"github.com/krau/SaveAny-Bot/database"
)
-// handleWatchNotifyCmd toggles the task notifications for watched chats and
-// reports the current state, which is disabled by default.
-func handleWatchNotifyCmd(ctx *ext.Context, update *ext.Update, user *database.User, args []string) error {
- if len(args) > 0 {
- enabled, ok := parseWatchNotifyArg(args[0])
- if !ok {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgWatchErrorNotifyArgInvalid)), nil)
- return dispatcher.EndGroups
- }
- user.WatchNotify = enabled
- if err := database.UpdateUser(ctx, user); err != nil {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorUpdateUserInfoFailed, map[string]any{
- "Error": err.Error(),
- })), nil)
- return dispatcher.EndGroups
- }
- }
- ctx.Reply(update, ext.ReplyTextString(i18n.T(watchNotifyStateKey(user.WatchNotify))), nil)
- return dispatcher.EndGroups
-}
-
-// parseWatchNotifyArg parses the argument of /watch notify.
-func parseWatchNotifyArg(arg string) (enabled bool, ok bool) {
- switch strings.ToLower(arg) {
- case "on":
- return true, true
- case "off":
- return false, true
- default:
- return false, false
- }
-}
-
func watchNotifyStateKey(enabled bool) i18nk.Key {
if enabled {
return i18nk.BotMsgWatchInfoNotifyEnabled
diff --git a/client/bot/handlers/watch_notify_test.go b/client/bot/handlers/watch_notify_test.go
index eddf167e..a4ba5371 100644
--- a/client/bot/handlers/watch_notify_test.go
+++ b/client/bot/handlers/watch_notify_test.go
@@ -7,8 +7,8 @@ import (
"strings"
"testing"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
+ "github.com/krau/mygotg/ext"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
@@ -16,30 +16,6 @@ import (
"github.com/krau/SaveAny-Bot/database"
)
-func TestParseWatchNotifyArg(t *testing.T) {
- tests := []struct {
- arg string
- wantEnabled bool
- wantOK bool
- }{
- {"on", true, true},
- {"ON", true, true},
- {"off", false, true},
- {"Off", false, true},
- {"", false, false},
- {"yes", false, false},
- {"-1002229835658", false, false},
- }
- for _, tt := range tests {
- t.Run(tt.arg, func(t *testing.T) {
- enabled, ok := parseWatchNotifyArg(tt.arg)
- if enabled != tt.wantEnabled || ok != tt.wantOK {
- t.Errorf("parseWatchNotifyArg(%q) = (%v, %v), want (%v, %v)", tt.arg, enabled, ok, tt.wantEnabled, tt.wantOK)
- }
- })
- }
-}
-
type notifyCtxKey struct{}
type notifyTestInfo struct{}
diff --git a/client/bot/handlers/ytdlp.go b/client/bot/handlers/ytdlp.go
index 1f9c8edd..2d235367 100644
--- a/client/bot/handlers/ytdlp.go
+++ b/client/bot/handlers/ytdlp.go
@@ -5,53 +5,9 @@ import (
"strings"
"unicode"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
-
- "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
- "github.com/krau/SaveAny-Bot/common/i18n"
- "github.com/krau/SaveAny-Bot/common/i18n/i18nk"
- "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
- "github.com/krau/SaveAny-Bot/pkg/tcbdata"
- "github.com/krau/SaveAny-Bot/storage"
)
-func handleYtdlpCmd(ctx *ext.Context, update *ext.Update) error {
- logger := log.FromContext(ctx)
- args := splitQuotedArgs(update.EffectiveMessage.Text)
- if len(args) < 2 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgYtdlpUsage)), nil)
- return dispatcher.EndGroups
- }
-
- urls, flags := parseYtdlpArgs(logger, args[1:])
-
- if len(urls) == 0 {
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgYtdlpErrorNoValidUrls)), nil)
- return dispatcher.EndGroups
- }
-
- logger.Debugf("Preparing yt-dlp download for %d URL(s) with %d flag(s)", len(urls), len(flags))
-
- markup, err := msgelem.BuildAddSelectStorageKeyboard(storage.GetUserStorages(ctx, update.GetUserChat().GetID()), tcbdata.Add{
- TaskType: tasktype.TaskTypeYtdlp,
- YtdlpURLs: urls,
- YtdlpFlags: flags,
- })
- if err != nil {
- return err
- }
-
- ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgYtdlpInfoUrlsSelectStorage, map[string]any{
- "Count": len(urls),
- })), &ext.ReplyOpts{
- Markup: markup,
- })
-
- return dispatcher.EndGroups
-}
-
// splitQuotedArgs splits a command line on whitespace, keeping quoted values
// together and removing the quotes. Quotes only delimit a value when they start
// one, so apostrophes inside arguments stay literal.
@@ -84,7 +40,7 @@ func splitQuotedArgs(text string) []string {
return args
}
-// parseYtdlpArgs separates the /ytdlp arguments into URLs and custom yt-dlp
+// parseYtdlpArgs separates download arguments into URLs and custom yt-dlp
// flags. A flag value is kept together with its flag, unless it looks like a URL
// of its own.
func parseYtdlpArgs(logger *log.Logger, args []string) (urls []string, flags []string) {
@@ -110,10 +66,15 @@ func parseYtdlpArgs(logger *log.Logger, args []string) (urls []string, flags []s
}
} else {
u, err := url.Parse(arg)
- if err != nil || u.Scheme == "" || u.Host == "" {
+ if err != nil || u.Scheme == "" {
logger.Warnf("Invalid URL: %s", arg)
continue
}
+ // Magnet and other non-web schemes are valid Aria2 inputs but not
+ // yt-dlp URLs; parseDownloadURIs handles them separately.
+ if u.Host == "" {
+ continue
+ }
urls = append(urls, arg)
}
}
diff --git a/client/bot/handlers/ytdlp_test.go b/client/bot/handlers/ytdlp_test.go
index d9a6ec9a..fccdc221 100644
--- a/client/bot/handlers/ytdlp_test.go
+++ b/client/bot/handlers/ytdlp_test.go
@@ -16,79 +16,79 @@ func TestYtdlpArgumentParsing(t *testing.T) {
}{
{
name: "Single URL without flags",
- input: "/ytdlp https://example.com/video",
+ input: "/download https://example.com/video",
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{},
},
{
name: "Multiple URLs without flags",
- input: "/ytdlp https://example.com/v1 https://example.com/v2",
+ input: "/download https://example.com/v1 https://example.com/v2",
expectedURLs: []string{"https://example.com/v1", "https://example.com/v2"},
expectedFlags: []string{},
},
{
name: "URL with format flag",
- input: "/ytdlp --format best https://example.com/video",
+ input: "/download --format best https://example.com/video",
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"--format", "best"},
},
{
name: "URL with extract-audio flag",
- input: "/ytdlp --extract-audio --audio-format mp3 https://example.com/video",
+ input: "/download --extract-audio --audio-format mp3 https://example.com/video",
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"--extract-audio", "--audio-format", "mp3"},
},
{
name: "Multiple URLs with flags",
- input: "/ytdlp --format best https://example.com/v1 https://example.com/v2",
+ input: "/download --format best https://example.com/v1 https://example.com/v2",
expectedURLs: []string{"https://example.com/v1", "https://example.com/v2"},
expectedFlags: []string{"--format", "best"},
},
{
name: "Flags mixed with URLs",
- input: "/ytdlp https://example.com/v1 --format best https://example.com/v2",
+ input: "/download https://example.com/v1 --format best https://example.com/v2",
expectedURLs: []string{"https://example.com/v1", "https://example.com/v2"},
expectedFlags: []string{"--format", "best"},
},
{
name: "Short flag",
- input: "/ytdlp -f best https://example.com/video",
+ input: "/download -f best https://example.com/video",
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"-f", "best"},
},
{
name: "Boolean flag",
- input: "/ytdlp --extract-audio https://example.com/video",
+ input: "/download --extract-audio https://example.com/video",
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"--extract-audio"},
},
{
name: "Output template flag with spaces",
- input: `/ytdlp -o "%(uploader)s - %(title)s.%(ext)s" https://example.com/video`,
+ input: `/download -o "%(uploader)s - %(title)s.%(ext)s" https://example.com/video`,
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"-o", "%(uploader)s - %(title)s.%(ext)s"},
},
{
name: "Single quoted template",
- input: `/ytdlp -o '%(title)s.%(ext)s' https://example.com/video`,
+ input: `/download -o '%(title)s.%(ext)s' https://example.com/video`,
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"-o", "%(title)s.%(ext)s"},
},
{
name: "Apostrophe inside a URL",
- input: `/ytdlp https://en.wikipedia.org/wiki/O'Brien -f best`,
+ input: `/download https://en.wikipedia.org/wiki/O'Brien -f best`,
expectedURLs: []string{"https://en.wikipedia.org/wiki/O'Brien"},
expectedFlags: []string{"-f", "best"},
},
{
name: "Apostrophe inside a quoted template",
- input: `/ytdlp -o "%(title)s 'live'.%(ext)s" https://example.com/video`,
+ input: `/download -o "%(title)s 'live'.%(ext)s" https://example.com/video`,
expectedURLs: []string{"https://example.com/video"},
expectedFlags: []string{"-o", "%(title)s 'live'.%(ext)s"},
},
{
name: "Invalid URL is dropped",
- input: "/ytdlp not-a-url -f best",
+ input: "/download not-a-url -f best",
expectedURLs: []string{},
expectedFlags: []string{"-f", "best"},
},
diff --git a/client/middleware/floodwait.go b/client/middleware/floodwait.go
deleted file mode 100644
index 4c553c41..00000000
--- a/client/middleware/floodwait.go
+++ /dev/null
@@ -1,19 +0,0 @@
-package middleware
-
-import (
- "time"
-
- "github.com/gotd/contrib/middleware/floodwait"
- "github.com/gotd/contrib/middleware/ratelimit"
- "github.com/gotd/td/telegram"
- "golang.org/x/time/rate"
-)
-
-func NewFloodWaitMiddlewares(maxRetries uint) []telegram.Middleware {
- waiter := floodwait.NewSimpleWaiter().WithMaxRetries(maxRetries)
- ratelimiter := ratelimit.New(rate.Every(time.Millisecond*100), 5)
- return []telegram.Middleware{
- waiter,
- ratelimiter,
- }
-}
diff --git a/client/middleware/retry/retry.go b/client/middleware/retry/retry.go
index b13b0863..026f519d 100644
--- a/client/middleware/retry/retry.go
+++ b/client/middleware/retry/retry.go
@@ -28,11 +28,13 @@ type retry struct {
func (r retry) Handle(next tg.Invoker) telegram.InvokeFunc {
return func(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
retries := 0
+ var lastErr error
for retries < r.max {
if err := next.Invoke(ctx, input, output); err != nil {
if tgerr.Is(err, r.errors...) {
log.FromContext(ctx).Debug("retry middleware", "retries", retries, "error", err)
+ lastErr = err
retries++
continue
}
@@ -42,7 +44,10 @@ func (r retry) Handle(next tg.Invoker) telegram.InvokeFunc {
return nil
}
- return fmt.Errorf("retry limit reached after %d attempts", r.max)
+ if lastErr == nil {
+ return fmt.Errorf("retry limit reached after %d attempts", r.max)
+ }
+ return fmt.Errorf("retry limit reached after %d attempts: %w", r.max, lastErr)
}
}
diff --git a/client/relay/download.go b/client/relay/download.go
new file mode 100644
index 00000000..dfa4635f
--- /dev/null
+++ b/client/relay/download.go
@@ -0,0 +1,417 @@
+package relay
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/constant"
+ "github.com/gotd/td/telegram/downloader"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/worker"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/ext"
+)
+
+var runningClients = worker.RunningClients
+
+// Prepare wraps a Telegram file with a relay-backed downloader when a relay
+// channel and at least two running helpers are available. Any failure leaves
+// the original single-client file in place.
+func Prepare(ctx context.Context, file tfile.TGFile) (tfile.TGFile, func(context.Context)) {
+ return prepare(ctx, file, "")
+}
+
+// PrepareResumable is Prepare with a sidecar state file. The state lets a
+// crashed download reuse its relay message instead of forwarding it again.
+func PrepareResumable(ctx context.Context, file tfile.TGFile, statePath string) (tfile.TGFile, func(context.Context)) {
+ return prepare(ctx, file, statePath)
+}
+
+func prepare(ctx context.Context, file tfile.TGFile, statePath string) (tfile.TGFile, func(context.Context)) {
+ relayChatID := config.C().Telegram.RelayChatID
+ if relayChatID == 0 {
+ return file, nil
+ }
+ if config.C().Stream {
+ log.FromContext(ctx).Debug("Relay download skipped: stream mode is enabled")
+ return file, nil
+ }
+ minSize := int64(config.C().RelayMinSizeMB) << 20
+ if file == nil || file.Size() < minSize {
+ return file, nil
+ }
+ messageFile, ok := file.(tfile.TGFileMessage)
+ if !ok || messageFile.Message() == nil {
+ log.FromContext(ctx).Debug("Relay download skipped: file is not a Telegram message")
+ return file, nil
+ }
+ extCtx := tgutil.ExtFromContext(ctx)
+ if extCtx == nil {
+ return file, nil
+ }
+ workers := runningClients()
+ if len(workers) < 2 {
+ log.FromContext(ctx).Debugf("Relay download skipped: %d running helper bot(s), need at least 2", len(workers))
+ return file, nil
+ }
+ relayPeer, err := extCtx.ResolveInputPeerById(relayChatID)
+ if err != nil {
+ log.FromContext(ctx).Debugf("Relay channel is unavailable, using primary client: %v", err)
+ return file, nil
+ }
+ if _, ok := relayPeer.(*tg.InputPeerChannel); !ok {
+ log.FromContext(ctx).Warn("Relay chat is not a channel, using primary client")
+ return file, nil
+ }
+ message := messageFile.Message()
+ sourceChatID := tgutil.ChatIdFromPeer(message.GetPeerID())
+ if sourceChatID == 0 || message.GetID() == 0 {
+ return file, nil
+ }
+ identity := tfile.Identity(file.Location())
+ keepMessages := config.C().Telegram.RelayKeepMessages
+
+ if statePath != "" {
+ state, stateErr := loadResumeState(statePath)
+ if stateErr != nil {
+ log.FromContext(ctx).Debugf("Relay resume state unavailable: %v", stateErr)
+ } else if state != nil {
+ if state.RelayChatID == relayChatID && state.matches(identity) {
+ relayed := newRelayFile(messageFile, extCtx, relayChatID, state.RelayMessageID, workers, keepMessages, statePath)
+ accessible := relayed.accessibleWorkers(ctx)
+ if len(accessible) >= 2 {
+ relayed.workers = accessible
+ log.FromContext(ctx).Infof(
+ "Relay download reused: file=%q helpers=%s message_id=%d",
+ file.Name(), workerNames(accessible), state.RelayMessageID,
+ )
+ return relayed, relayed.cleanup
+ }
+ log.FromContext(ctx).Warnf("Relay resume message %d is no longer readable; forwarding again", state.RelayMessageID)
+ relayed.cleanup(ctx)
+ } else {
+ log.FromContext(ctx).Debug("Relay resume state belongs to another file; discarding")
+ stale := newRelayFile(messageFile, extCtx, state.RelayChatID, state.RelayMessageID, workers, keepMessages, statePath)
+ stale.cleanup(ctx)
+ }
+ }
+ }
+
+ updates, err := extCtx.ForwardMessages(sourceChatID, relayChatID, &tg.MessagesForwardMessagesRequest{
+ ID: []int{message.GetID()},
+ })
+ if err != nil {
+ log.FromContext(ctx).Debugf("Relay forward failed, using primary client: %v", err)
+ return file, nil
+ }
+ relayMessageID := forwardedMessageID(updates, relayChatID)
+ if relayMessageID == 0 {
+ log.FromContext(ctx).Warn("Relay forward returned no channel message ID, using primary client")
+ return file, nil
+ }
+ relayed := newRelayFile(messageFile, extCtx, relayChatID, relayMessageID, workers, keepMessages, statePath)
+ accessible := relayed.accessibleWorkers(ctx)
+ if len(accessible) < 2 {
+ log.FromContext(ctx).Warnf("Relay download skipped: only %d helper bot(s) can read the relay channel", len(accessible))
+ relayed.cleanup(ctx)
+ return file, nil
+ }
+ relayed.workers = accessible
+ if statePath != "" {
+ state := resumeState{
+ Version: resumeStateVersion,
+ RelayChatID: relayChatID,
+ RelayMessageID: relayMessageID,
+ SourceKind: identity.Kind,
+ SourceID: identity.ID,
+ SourceAccessHash: identity.AccessHash,
+ }
+ if err := saveResumeState(statePath, state); err != nil {
+ log.FromContext(ctx).Warnf("Failed to persist relay resume state: %v", err)
+ relayed.cleanup(ctx)
+ return file, nil
+ }
+ }
+ log.FromContext(ctx).Infof(
+ "Relay download enabled: file=%q size=%d helpers=%s message_id=%d keep_messages=%t",
+ file.Name(), file.Size(), workerNames(accessible), relayMessageID, relayed.keepMessages,
+ )
+ return relayed, relayed.cleanup
+}
+
+func newRelayFile(
+ base tfile.TGFileMessage,
+ primary *ext.Context,
+ relayChatID int64,
+ relayMessageID int,
+ workers []worker.ClientRef,
+ keepMessages bool,
+ statePath string,
+) *relayFile {
+ return &relayFile{
+ base: base,
+ primary: primary,
+ relayChatID: relayChatID,
+ relayMessageID: relayMessageID,
+ workers: workers,
+ keepMessages: keepMessages,
+ statePath: statePath,
+ locations: make(map[uint]tg.InputFileLocationClass, len(workers)),
+ served: make(map[uint]int, len(workers)),
+ }
+}
+
+func (f *relayFile) accessibleWorkers(ctx context.Context) []worker.ClientRef {
+ accessible := make([]worker.ClientRef, 0, len(f.workers))
+ for _, ref := range f.workers {
+ if _, err := f.workerLocation(ctx, ref); err != nil {
+ log.FromContext(ctx).Debugf("Relay worker %s cannot read channel: %v", ref.Name, err)
+ continue
+ }
+ accessible = append(accessible, ref)
+ }
+ return accessible
+}
+
+func workerNames(workers []worker.ClientRef) string {
+ names := make([]string, 0, len(workers))
+ for _, ref := range workers {
+ if ref.Name != "" {
+ names = append(names, ref.Name)
+ }
+ }
+ return strings.Join(names, ", ")
+}
+
+type relayFile struct {
+ base tfile.TGFileMessage
+ primary *ext.Context
+ relayChatID int64
+ relayMessageID int
+ workers []worker.ClientRef
+ keepMessages bool
+ statePath string
+
+ mu sync.Mutex
+ next int
+ locations map[uint]tg.InputFileLocationClass
+ served map[uint]int
+}
+
+func (f *relayFile) Location() tg.InputFileLocationClass { return f.base.Location() }
+func (f *relayFile) Dler() downloader.Client { return &multiClient{file: f, base: f.base.Dler()} }
+func (f *relayFile) Size() int64 { return f.base.Size() }
+func (f *relayFile) Name() string { return f.base.Name() }
+func (f *relayFile) SetName(name string) { f.base.SetName(name) }
+func (f *relayFile) Message() *tg.Message { return f.base.Message() }
+func (f *relayFile) WorkerCount() int { return len(f.workers) }
+
+func (f *relayFile) cleanup(ctx context.Context) {
+ summary := f.servedSummary()
+ if summary != "" {
+ log.FromContext(ctx).Infof("Relay download finished: %s", summary)
+ }
+ if err := removeResumeState(f.statePath); err != nil {
+ log.FromContext(ctx).Warnf("Failed to remove relay resume state: %v", err)
+ }
+ if f.keepMessages {
+ log.FromContext(ctx).Debugf("Relay message %d kept by configuration", f.relayMessageID)
+ return
+ }
+ peer, err := f.primary.ResolveInputPeerById(f.relayChatID)
+ if err != nil {
+ return
+ }
+ cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
+ defer cancel()
+ switch p := peer.(type) {
+ case *tg.InputPeerChannel:
+ _, err = f.primary.Raw.ChannelsDeleteMessages(cleanupCtx, &tg.ChannelsDeleteMessagesRequest{
+ Channel: &tg.InputChannel{ChannelID: p.ChannelID, AccessHash: p.AccessHash},
+ ID: []int{f.relayMessageID},
+ })
+ case *tg.InputPeerChat, *tg.InputPeerUser:
+ _, err = f.primary.Raw.MessagesDeleteMessages(cleanupCtx, &tg.MessagesDeleteMessagesRequest{
+ Revoke: true,
+ ID: []int{f.relayMessageID},
+ })
+ }
+ if err != nil {
+ log.FromContext(ctx).Warnf("Relay message %d cleanup failed: %v", f.relayMessageID, err)
+ }
+}
+
+func (f *relayFile) recordServed(workerID uint) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ if f.served == nil {
+ f.served = make(map[uint]int)
+ }
+ f.served[workerID]++
+}
+
+func (f *relayFile) servedSummary() string {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ parts := make([]string, 0, len(f.workers))
+ for _, ref := range f.workers {
+ if count := f.served[ref.ID]; count > 0 {
+ parts = append(parts, fmt.Sprintf("%s=%d chunk(s)", ref.Name, count))
+ }
+ }
+ return strings.Join(parts, ", ")
+}
+
+type multiClient struct {
+ file *relayFile
+ base downloader.Client
+}
+
+func (c *multiClient) UploadGetFile(ctx context.Context, req *tg.UploadGetFileRequest) (tg.UploadFileClass, error) {
+ for range c.file.workers {
+ ref := c.file.nextWorker()
+ location, err := c.file.workerLocation(ctx, ref)
+ if err != nil {
+ log.FromContext(ctx).Debugf("Relay worker %s location failed: %v", ref.Name, err)
+ continue
+ }
+ workerReq := *req
+ workerReq.Location = location
+ result, err := ref.Ext.Raw.UploadGetFile(ctx, &workerReq)
+ if err == nil {
+ c.file.recordServed(ref.ID)
+ log.FromContext(ctx).Debugf("Relay chunk served: worker=%s offset=%d limit=%d", ref.Name, req.Offset, req.Limit)
+ return result, nil
+ }
+ log.FromContext(ctx).Debugf("Relay worker %s download failed: %v", ref.Name, err)
+ c.file.invalidate(ref.ID)
+ }
+ log.FromContext(ctx).Debugf("Relay chunk fallback to primary: offset=%d limit=%d", req.Offset, req.Limit)
+ return c.base.UploadGetFile(ctx, req)
+}
+
+func (c *multiClient) UploadGetFileHashes(ctx context.Context, req *tg.UploadGetFileHashesRequest) ([]tg.FileHash, error) {
+ return c.base.UploadGetFileHashes(ctx, req)
+}
+
+func (c *multiClient) UploadReuploadCDNFile(ctx context.Context, req *tg.UploadReuploadCDNFileRequest) ([]tg.FileHash, error) {
+ return c.base.UploadReuploadCDNFile(ctx, req)
+}
+
+func (c *multiClient) UploadGetCDNFileHashes(ctx context.Context, req *tg.UploadGetCDNFileHashesRequest) ([]tg.FileHash, error) {
+ return c.base.UploadGetCDNFileHashes(ctx, req)
+}
+
+func (c *multiClient) UploadGetWebFile(ctx context.Context, req *tg.UploadGetWebFileRequest) (*tg.UploadWebFile, error) {
+ return c.base.UploadGetWebFile(ctx, req)
+}
+
+func (f *relayFile) nextWorker() worker.ClientRef {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ ref := f.workers[f.next%len(f.workers)]
+ f.next++
+ return ref
+}
+
+func (f *relayFile) workerLocation(ctx context.Context, ref worker.ClientRef) (tg.InputFileLocationClass, error) {
+ f.mu.Lock()
+ location, ok := f.locations[ref.ID]
+ f.mu.Unlock()
+ if ok {
+ return location, nil
+ }
+ peer, err := ref.Ext.ResolveInputPeerById(f.relayChatID)
+ if err != nil {
+ return nil, err
+ }
+ inputChannel, ok := peer.(*tg.InputPeerChannel)
+ if !ok {
+ return nil, fmt.Errorf("relay chat is not a channel")
+ }
+ result, err := ref.Ext.Raw.ChannelsGetMessages(ctx, &tg.ChannelsGetMessagesRequest{
+ Channel: &tg.InputChannel{ChannelID: inputChannel.ChannelID, AccessHash: inputChannel.AccessHash},
+ ID: []tg.InputMessageClass{&tg.InputMessageID{ID: f.relayMessageID}},
+ })
+ if err != nil {
+ return nil, err
+ }
+ message := firstMessage(result)
+ if message == nil || message.Media == nil {
+ return nil, errors.New("relay message has no media")
+ }
+ relayed, err := tfile.FromMedia(message.Media, ref.Ext.Raw, tfile.WithMessage(message), tfile.WithName(f.base.Name()))
+ if err != nil {
+ return nil, err
+ }
+ location = relayed.Location()
+ f.mu.Lock()
+ f.locations[ref.ID] = location
+ f.mu.Unlock()
+ return location, nil
+}
+
+func (f *relayFile) invalidate(workerID uint) {
+ f.mu.Lock()
+ delete(f.locations, workerID)
+ f.mu.Unlock()
+}
+
+func firstMessage(messages tg.MessagesMessagesClass) *tg.Message {
+ var list []tg.MessageClass
+ switch m := messages.(type) {
+ case *tg.MessagesMessages:
+ list = m.Messages
+ case *tg.MessagesMessagesSlice:
+ list = m.Messages
+ case *tg.MessagesChannelMessages:
+ list = m.Messages
+ default:
+ return nil
+ }
+ for _, item := range list {
+ if message, ok := item.(*tg.Message); ok {
+ return message
+ }
+ }
+ return nil
+}
+
+func forwardedMessageID(updates tg.UpdatesClass, relayChatID int64) int {
+ var list []tg.UpdateClass
+ switch u := updates.(type) {
+ case *tg.Updates:
+ list = u.Updates
+ case *tg.UpdatesCombined:
+ list = u.Updates
+ default:
+ return 0
+ }
+ for _, update := range list {
+ var messageClass tg.MessageClass
+ switch u := update.(type) {
+ case *tg.UpdateNewChannelMessage:
+ messageClass = u.Message
+ case *tg.UpdateNewMessage:
+ messageClass = u.Message
+ default:
+ continue
+ }
+ message, ok := messageClass.(*tg.Message)
+ if !ok {
+ continue
+ }
+ if tgutil.ChatIdFromPeer(message.GetPeerID()) == relayChatID ||
+ tgutil.ChatIdFromPeer(message.GetPeerID()) == constant.TDLibPeerID(relayChatID).ToPlain() {
+ return message.GetID()
+ }
+ }
+ return 0
+}
diff --git a/client/relay/download_test.go b/client/relay/download_test.go
new file mode 100644
index 00000000..cc40cefc
--- /dev/null
+++ b/client/relay/download_test.go
@@ -0,0 +1,126 @@
+package relay
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/gotd/td/bin"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/worker"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/ext"
+ tgstorage "github.com/krau/mygotg/storage"
+)
+
+type relayFakeInvoker struct {
+ mu sync.Mutex
+ forwards int
+ deletes int
+ relayMsg *tg.Message
+ relayDoc *tg.Document
+}
+
+func (f *relayFakeInvoker) Invoke(_ context.Context, input bin.Encoder, output bin.Decoder) error {
+ switch req := input.(type) {
+ case *tg.MessagesForwardMessagesRequest:
+ f.mu.Lock()
+ f.forwards++
+ f.mu.Unlock()
+ if box, ok := output.(*tg.UpdatesBox); ok {
+ box.Updates = &tg.Updates{Updates: []tg.UpdateClass{&tg.UpdateNewChannelMessage{Message: f.relayMsg}}}
+ }
+ case *tg.ChannelsGetMessagesRequest:
+ if box, ok := output.(*tg.MessagesMessagesBox); ok {
+ box.Messages = &tg.MessagesChannelMessages{Messages: []tg.MessageClass{f.relayMsg}}
+ }
+ case *tg.ChannelsDeleteMessagesRequest:
+ f.mu.Lock()
+ f.deletes++
+ f.mu.Unlock()
+ case *tg.UploadGetFileRequest:
+ if box, ok := output.(*tg.UploadFileBox); ok {
+ box.File = &tg.UploadFile{Bytes: make([]byte, req.Limit)}
+ }
+ }
+ return nil
+}
+
+func relayTestExt(inv *relayFakeInvoker, peers []int64) *ext.Context {
+ storage := tgstorage.NewPeerStorage(nil, true)
+ for _, id := range peers {
+ storage.AddPeer(id, 1, tgstorage.TypeChannel, "")
+ }
+ return ext.NewContext(context.Background(), tg.NewClient(inv), storage, &tg.User{ID: 1}, nil, nil, false)
+}
+
+func TestPrepareResumableReusesRelayMessage(t *testing.T) {
+ oldCfg := config.C()
+ cfg := oldCfg
+ cfg.Telegram.RelayChatID = -1000000000500
+ cfg.Telegram.RelayKeepMessages = false
+ config.Set(cfg)
+ defer config.Set(oldCfg)
+
+ const sourceChatID int64 = -1000000000400
+ doc := &tg.Document{ID: 7, AccessHash: 8, FileReference: []byte{1}, Size: 9 << 20, DCID: 2}
+ relayMsg := &tg.Message{ID: 777, PeerID: &tg.PeerChannel{ChannelID: 500}, Media: &tg.MessageMediaDocument{Document: doc}}
+ sourceMsg := &tg.Message{ID: 10, PeerID: &tg.PeerChannel{ChannelID: sourceChatID}, Media: &tg.MessageMediaDocument{Document: doc}}
+ primaryInv := &relayFakeInvoker{relayMsg: relayMsg, relayDoc: doc}
+ primary := relayTestExt(primaryInv, []int64{sourceChatID, 500})
+ worker1 := &relayFakeInvoker{relayMsg: relayMsg, relayDoc: doc}
+ worker2 := &relayFakeInvoker{relayMsg: relayMsg, relayDoc: doc}
+ oldRunning := runningClients
+ runningClients = func() []worker.ClientRef {
+ return []worker.ClientRef{
+ {ID: 1, Name: "one", Ext: relayTestExt(worker1, []int64{500})},
+ {ID: 2, Name: "two", Ext: relayTestExt(worker2, []int64{500})},
+ }
+ }
+ defer func() { runningClients = oldRunning }()
+
+ file, err := tfile.FromMediaMessage(sourceMsg.Media, tg.NewClient(primaryInv), sourceMsg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ ctx := tgutil.ExtWithContext(context.Background(), primary)
+ statePath := ResumeStatePath(t.TempDir() + "/cache")
+
+ first, firstCleanup := PrepareResumable(ctx, file, statePath)
+ if first == nil || firstCleanup == nil {
+ t.Fatal("relay was not prepared")
+ }
+ defer firstCleanup(ctx)
+ primaryInv.mu.Lock()
+ forwards := primaryInv.forwards
+ primaryInv.mu.Unlock()
+ if forwards != 1 {
+ t.Fatalf("forward count = %d, want 1", forwards)
+ }
+ if _, err := loadResumeState(statePath); err != nil {
+ t.Fatalf("state not persisted: %v", err)
+ }
+
+ second, cleanup2 := PrepareResumable(ctx, file, statePath)
+ if second == nil || cleanup2 == nil {
+ t.Fatal("relay state was not reused")
+ }
+ primaryInv.mu.Lock()
+ forwards = primaryInv.forwards
+ primaryInv.mu.Unlock()
+ if forwards != 1 {
+ t.Fatalf("second prepare forwarded again: %d", forwards)
+ }
+ cleanup2(ctx)
+ if state, err := loadResumeState(statePath); err != nil || state != nil {
+ t.Fatalf("relay state after cleanup = %+v, %v", state, err)
+ }
+ primaryInv.mu.Lock()
+ deletes := primaryInv.deletes
+ primaryInv.mu.Unlock()
+ if deletes == 0 {
+ t.Fatal("relay message was not deleted")
+ }
+}
diff --git a/client/relay/state.go b/client/relay/state.go
new file mode 100644
index 00000000..48c51ad7
--- /dev/null
+++ b/client/relay/state.go
@@ -0,0 +1,112 @@
+package relay
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "os"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+)
+
+const resumeStateVersion = 1
+
+type resumeState struct {
+ Version int `json:"version"`
+ RelayChatID int64 `json:"relay_chat_id"`
+ RelayMessageID int `json:"relay_message_id"`
+ SourceKind string `json:"source_kind"`
+ SourceID int64 `json:"source_id"`
+ SourceAccessHash int64 `json:"source_access_hash"`
+}
+
+// ResumeStatePath returns the sidecar path used to keep a relay message alive
+// across a crash. It deliberately lives next to the download cache.
+func ResumeStatePath(cachePath string) string {
+ return cachePath + ".relay"
+}
+
+func loadResumeState(path string) (*resumeState, error) {
+ data, err := os.ReadFile(path)
+ if errors.Is(err, os.ErrNotExist) {
+ return nil, nil
+ }
+ if err != nil {
+ return nil, fmt.Errorf("read relay resume state: %w", err)
+ }
+ var state resumeState
+ if err := json.Unmarshal(data, &state); err != nil {
+ if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
+ return nil, fmt.Errorf("remove invalid relay resume state: %w", removeErr)
+ }
+ return nil, nil
+ }
+ if state.Version != resumeStateVersion || state.RelayMessageID == 0 || state.RelayChatID == 0 {
+ if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
+ return nil, fmt.Errorf("remove incompatible relay resume state: %w", removeErr)
+ }
+ return nil, nil
+ }
+ return &state, nil
+}
+
+func saveResumeState(path string, state resumeState) error {
+ data, err := json.Marshal(state)
+ if err != nil {
+ return fmt.Errorf("marshal relay resume state: %w", err)
+ }
+ tmp := path + ".tmp"
+ if err := os.WriteFile(tmp, data, 0o644); err != nil {
+ return fmt.Errorf("write relay resume state: %w", err)
+ }
+ return os.Rename(tmp, path)
+}
+
+func removeResumeState(path string) error {
+ if path == "" {
+ return nil
+ }
+ if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return err
+ }
+ if err := os.Remove(path + ".tmp"); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return err
+ }
+ return nil
+}
+
+// CleanupResumeState releases a persisted relay message after the cache was
+// already complete, for example if the process stopped between download and
+// cleanup.
+func CleanupResumeState(ctx context.Context, statePath string) {
+ state, err := loadResumeState(statePath)
+ if err != nil || state == nil {
+ return
+ }
+ extCtx := tgutil.ExtFromContext(ctx)
+ if extCtx == nil {
+ if err := removeResumeState(statePath); err != nil {
+ log.FromContext(ctx).Warnf("Failed to remove relay resume state: %v", err)
+ }
+ return
+ }
+ relayed := &relayFile{
+ primary: extCtx,
+ relayChatID: state.RelayChatID,
+ relayMessageID: state.RelayMessageID,
+ keepMessages: config.C().Telegram.RelayKeepMessages,
+ statePath: statePath,
+ }
+ relayed.cleanup(ctx)
+}
+
+func (s *resumeState) matches(identity tfile.FileIdentity) bool {
+ if s == nil {
+ return false
+ }
+ return s.SourceKind == identity.Kind && s.SourceID == identity.ID && s.SourceAccessHash == identity.AccessHash
+}
diff --git a/client/relay/state_test.go b/client/relay/state_test.go
new file mode 100644
index 00000000..4d1bb552
--- /dev/null
+++ b/client/relay/state_test.go
@@ -0,0 +1,129 @@
+package relay
+
+import (
+ "context"
+ "errors"
+ "sync"
+ "testing"
+
+ "github.com/gotd/td/bin"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/worker"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/ext"
+ tgstorage "github.com/krau/mygotg/storage"
+)
+
+type fakeInvoker struct {
+ mu sync.Mutex
+ calls map[uint]int
+ fail map[uint]bool
+ data []byte
+}
+
+func (f *fakeInvoker) Invoke(_ context.Context, input bin.Encoder, output bin.Decoder) error {
+ req, ok := input.(*tg.UploadGetFileRequest)
+ if !ok {
+ return errors.New("unexpected request")
+ }
+ id := req.Location.(*tg.InputDocumentFileLocation).ID
+ f.mu.Lock()
+ f.calls[uint(id)]++
+ fail := f.fail[uint(id)]
+ f.mu.Unlock()
+ if fail {
+ return errors.New("mock worker failure")
+ }
+ box, ok := output.(*tg.UploadFileBox)
+ if !ok {
+ return errors.New("unexpected response")
+ }
+ offset := int(req.Offset)
+ end := offset + req.Limit
+ if end > len(f.data) {
+ end = len(f.data)
+ }
+ box.File = &tg.UploadFile{Bytes: append([]byte(nil), f.data[offset:end]...)}
+ return nil
+}
+
+func testExt(t *testing.T, inv *fakeInvoker) *ext.Context {
+ t.Helper()
+ peers := tgstorage.NewPeerStorage(nil, true)
+ peers.AddPeer(500, 1, tgstorage.TypeChannel, "")
+ return ext.NewContext(context.Background(), tg.NewClient(inv), peers, &tg.User{}, nil, nil, false)
+}
+
+func TestResumeStateRoundTrip(t *testing.T) {
+ path := ResumeStatePath(t.TempDir() + "/cache.bin")
+ identity := tfile.FileIdentity{Kind: "document", ID: 7, AccessHash: 8}
+ state := resumeState{
+ Version: resumeStateVersion, RelayChatID: 500, RelayMessageID: 42,
+ SourceKind: identity.Kind, SourceID: identity.ID, SourceAccessHash: identity.AccessHash,
+ }
+ if err := saveResumeState(path, state); err != nil {
+ t.Fatal(err)
+ }
+ loaded, err := loadResumeState(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if loaded == nil || loaded.RelayMessageID != 42 || !loaded.matches(identity) {
+ t.Fatalf("loaded state = %+v", loaded)
+ }
+ if loaded.matches(tfile.FileIdentity{Kind: "document", ID: 9, AccessHash: 8}) {
+ t.Fatal("state matched a different file")
+ }
+ if err := removeResumeState(path); err != nil {
+ t.Fatal(err)
+ }
+ if loaded, err := loadResumeState(path); err != nil || loaded != nil {
+ t.Fatalf("state after removal = %+v, %v", loaded, err)
+ }
+}
+
+func TestMultiClientFailsOverAndFallsBackToPrimary(t *testing.T) {
+ ctx := context.Background()
+ data := []byte("relay chunk")
+ firstInv := &fakeInvoker{calls: map[uint]int{}, fail: map[uint]bool{1: true}, data: data}
+ secondInv := &fakeInvoker{calls: map[uint]int{}, fail: map[uint]bool{}, data: data}
+ primaryInv := &fakeInvoker{calls: map[uint]int{}, fail: map[uint]bool{}, data: data}
+ location := &tg.InputDocumentFileLocation{ID: 1}
+ file := &relayFile{
+ workers: []worker.ClientRef{
+ {ID: 1, Name: "one", Ext: testExt(t, firstInv)},
+ {ID: 2, Name: "two", Ext: testExt(t, secondInv)},
+ },
+ locations: map[uint]tg.InputFileLocationClass{
+ 1: &tg.InputDocumentFileLocation{ID: 1},
+ 2: &tg.InputDocumentFileLocation{ID: 2},
+ },
+ served: make(map[uint]int),
+ }
+ client := &multiClient{file: file, base: tg.NewClient(primaryInv)}
+ res, err := client.UploadGetFile(ctx, &tg.UploadGetFileRequest{Location: location, Offset: 0, Limit: len(data)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ uploaded, ok := res.(*tg.UploadFile)
+ if !ok || string(uploaded.Bytes) != string(data) {
+ t.Fatalf("response = %#v", res)
+ }
+ if firstInv.calls[1] != 1 || secondInv.calls[2] != 1 {
+ t.Fatalf("worker calls: first=%v second=%v", firstInv.calls, secondInv.calls)
+ }
+
+ // Both helpers fail; the primary client must serve the chunk.
+ firstInv.fail[1] = true
+ secondInv.fail[2] = true
+ res, err = client.UploadGetFile(ctx, &tg.UploadGetFileRequest{Location: location, Offset: 0, Limit: len(data)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if uploads, ok := res.(*tg.UploadFile); !ok || string(uploads.Bytes) != string(data) {
+ t.Fatalf("fallback response = %#v", res)
+ }
+ if primaryInv.calls[1] != 1 {
+ t.Fatalf("primary calls = %v", primaryInv.calls)
+ }
+}
diff --git a/client/user/auth_terminal.go b/client/user/auth_terminal.go
deleted file mode 100644
index 96811cbc..00000000
--- a/client/user/auth_terminal.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package user
-
-import (
- "bufio"
- "fmt"
- "os"
- "strings"
-
- "github.com/celestix/gotgproto"
- "golang.org/x/term"
-)
-
-type terminalAuthConversator struct{}
-
-func readLine(prompt string) (string, error) {
- fmt.Print(prompt)
- reader := bufio.NewReader(os.Stdin)
- text, err := reader.ReadString('\n')
- if err != nil {
- return "", err
- }
- return strings.TrimSpace(text), nil
-}
-
-func (t *terminalAuthConversator) AskPhoneNumber() (string, error) {
- fmt.Println("Your Phone Number (e.g. +44 123456):")
- return readLine("> ")
-}
-
-func (t *terminalAuthConversator) AskCode() (string, error) {
- fmt.Println("Your Code (e.g. 123456):")
- return readLine("> ")
-}
-
-func (t *terminalAuthConversator) AskPassword() (string, error) {
- fmt.Println("Your 2FA Password:")
- fmt.Print("> ")
- bytePwd, err := term.ReadPassword(int(os.Stdin.Fd()))
- fmt.Println()
- if err != nil {
- return "", err
- }
-
- return strings.TrimSpace(string(bytePwd)), nil
-}
-
-func (t *terminalAuthConversator) AuthStatus(authStatus gotgproto.AuthStatus) {
- switch authStatus.Event {
- case gotgproto.AuthStatusPhoneRetrial:
- fmt.Printf("The phone number is incorrect. Attempts left: %d\n", authStatus.AttemptsLeft)
- case gotgproto.AuthStatusPasswordRetrial:
- fmt.Printf("The 2FA password is incorrect. Attempts left: %d\n", authStatus.AttemptsLeft)
- case gotgproto.AuthStatusPhoneCodeRetrial:
- fmt.Printf("The OTP code is incorrect. Attempts left: %d\n", authStatus.AttemptsLeft)
- default:
- }
-}
diff --git a/client/user/sync.go b/client/user/sync.go
new file mode 100644
index 00000000..bc347300
--- /dev/null
+++ b/client/user/sync.go
@@ -0,0 +1,52 @@
+package user
+
+import (
+ "context"
+ "errors"
+ "sync"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/query/dialogs"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/mygotg/storage"
+)
+
+var syncPeersMu sync.Mutex
+
+// SyncPeers imports the userbot's dialogs into the shared peer cache.
+func SyncPeers(ctx context.Context) (int, error) {
+ if !config.C().Telegram.Userbot.Enable {
+ return 0, errors.New("userbot is not enabled")
+ }
+ syncPeersMu.Lock()
+ defer syncPeersMu.Unlock()
+ uctx := GetCtx()
+ if uctx == nil {
+ return 0, errors.New("userbot is not running")
+ }
+ tapi := uctx.Raw
+ peerStorage := uctx.PeerStorage
+ log.FromContext(ctx).Info("Starting to sync peers...")
+ count := 0
+ err := dialogs.NewQueryBuilder(tapi).GetDialogs().BatchSize(50).ForEach(ctx, func(ctx context.Context, e dialogs.Elem) error {
+ for cid, channel := range e.Entities.Channels() {
+ peerStorage.AddPeer(cid, channel.AccessHash, storage.TypeChannel, channel.Username)
+ count++
+ }
+ for uid, user := range e.Entities.Users() {
+ peerStorage.AddPeer(uid, user.AccessHash, storage.TypeUser, user.Username)
+ count++
+ }
+ for gid := range e.Entities.Chats() {
+ peerStorage.AddPeer(gid, storage.DefaultAccessHash, storage.TypeChat, storage.DefaultUsername)
+ count++
+ }
+ return nil
+ })
+ if err != nil {
+ log.FromContext(ctx).Error("Failed to sync peers", "error", err)
+ return count, err
+ }
+ log.FromContext(ctx).Info("Finished syncing peers")
+ return count, nil
+}
diff --git a/client/user/userclient.go b/client/user/userclient.go
index edc9dbc6..774e3224 100644
--- a/client/user/userclient.go
+++ b/client/user/userclient.go
@@ -2,113 +2,264 @@ package user
import (
"context"
+ "errors"
+ "fmt"
+ "os"
+ "runtime/debug"
"sync"
"time"
- "github.com/celestix/gotgproto"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/dispatcher/handlers"
- "github.com/celestix/gotgproto/dispatcher/handlers/filters"
- "github.com/celestix/gotgproto/ext"
- "github.com/celestix/gotgproto/sessionMaker"
-
"github.com/charmbracelet/log"
"github.com/gotd/td/tg"
+ gotgproto "github.com/krau/mygotg"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/dispatcher/handlers"
+ "github.com/krau/mygotg/dispatcher/handlers/filters"
+ gotgerrors "github.com/krau/mygotg/errors"
+ "github.com/krau/mygotg/ext"
+ "github.com/krau/mygotg/session"
+
"github.com/krau/SaveAny-Bot/client/middleware"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/database"
)
-var uc *gotgproto.Client
+var ErrLoginRequired = errors.New("telegram login required")
+
+type Status struct {
+ State string
+ ID int64
+ Username string
+ Phone string
+ Error string
+}
+
+type failConversator struct{}
+
+func (failConversator) AskPhoneNumber() (string, error) { return "", ErrLoginRequired }
+func (failConversator) AskCode() (string, error) { return "", ErrLoginRequired }
+func (failConversator) AskPassword() (string, error) { return "", ErrLoginRequired }
+func (failConversator) AuthStatus(gotgproto.AuthStatus) {}
-// getEctx lazily creates the user-client ext.Context exactly once. Guarded by
-// sync.OnceValue so concurrent GetCtx calls cannot race on ectx creation.
-var getEctx = sync.OnceValue(func() *ext.Context {
- return uc.CreateContext()
-})
+var (
+ ucMu sync.RWMutex
+ uc *gotgproto.Client
+ uctx *ext.Context
+ ustatus = Status{State: "stopped"}
+ starting bool
+ generation uint64
+ startCancel context.CancelFunc
+)
+
+func CurrentStatus() Status {
+ ucMu.RLock()
+ defer ucMu.RUnlock()
+ return ustatus
+}
+// GetCtx returns the userbot context, or nil when the userbot is not running.
func GetCtx() *ext.Context {
- if uc == nil {
- return nil
- }
- return getEctx()
+ ucMu.RLock()
+ defer ucMu.RUnlock()
+ return uctx
}
-func Login(ctx context.Context) (*gotgproto.Client, error) {
- log.FromContext(ctx).Debug("Logging in user client")
+// Start connects the userbot, asking conv for credentials when the session is
+// not authorized. Passing nil uses a conversator that fails with
+// ErrLoginRequired.
+func Start(ctx context.Context, conv gotgproto.AuthConversator, phone string) error {
+ ucMu.Lock()
if uc != nil {
- return uc, nil
+ ucMu.Unlock()
+ return nil
}
- res := make(chan struct {
+ if starting {
+ ucMu.Unlock()
+ return errors.New("userbot is already starting")
+ }
+ starting = true
+ generation++
+ gen := generation
+ startCtx, cancelStart := context.WithCancel(ctx)
+ startCancel = cancelStart
+ ustatus = Status{State: "starting"}
+ ucMu.Unlock()
+
+ autoStart := conv == nil
+ if conv == nil {
+ conv = failConversator{}
+ }
+ logger := log.FromContext(ctx)
+ logger.Debug("Logging in user client")
+
+ type result struct {
client *gotgproto.Client
err error
- })
+ }
+ res := make(chan result, 1)
go func() {
+ sent := false
+ defer func() {
+ if r := recover(); r != nil {
+ logger.Errorf("Userbot client initialization panicked: %v\n%s", r, debug.Stack())
+ if !sent {
+ res <- result{nil, fmt.Errorf("userbot client initialization panicked: %v", r)}
+ }
+ }
+ }()
resolver, err := tgutil.NewConfigProxyResolver()
if err != nil {
- res <- struct {
- client *gotgproto.Client
- err error
- }{nil, err}
+ res <- result{nil, err}
+ sent = true
return
}
tclient, err := gotgproto.NewClient(
config.C().Telegram.AppID,
config.C().Telegram.AppHash,
- gotgproto.ClientTypePhone(""),
+ gotgproto.ClientTypePhone(phone),
&gotgproto.ClientOpts{
- Session: sessionMaker.SqlSession(database.GetDialect(config.C().Telegram.Userbot.Session)),
- AuthConversator: &terminalAuthConversator{},
- Context: ctx,
- DisableCopyright: true,
- Resolver: resolver,
- MaxRetries: config.C().Telegram.RpcRetry,
- AutoFetchReply: true,
- Middlewares: middleware.NewDefaultMiddlewares(ctx, 5*time.Minute),
+ Session: session.SqlSession(database.GetDialect(config.C().Telegram.Userbot.Session)),
+ AuthConversator: conv,
+ NoAutoAuth: autoStart,
+ Context: startCtx,
+ DisableCopyright: true,
+ DeferUpdateRecovery: true,
+ Resolver: resolver,
+ MaxRetries: config.C().Telegram.RpcRetry,
+ AutoFetchReply: true,
+ Middlewares: middleware.NewDefaultMiddlewares(startCtx, 5*time.Minute),
ErrorHandler: func(ctx *ext.Context, u *ext.Update, s string) error {
log.FromContext(ctx).Errorf("Unhandled error: %s", s)
return dispatcher.EndGroups
},
},
)
- if err != nil {
- res <- struct {
- client *gotgproto.Client
- err error
- }{nil, err}
- }
- res <- struct {
- client *gotgproto.Client
- err error
- }(struct {
- client *gotgproto.Client
- err error
- }{tclient, nil})
+ res <- result{tclient, err}
+ sent = true
}()
+ var r result
select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case r := <-res:
- if r.err != nil {
- return nil, r.err
+ case <-startCtx.Done():
+ ucMu.Lock()
+ if generation == gen {
+ starting = false
+ generation++
+ startCancel = nil
+ ustatus = Status{State: "stopped"}
+ }
+ ucMu.Unlock()
+ return startCtx.Err()
+ case r = <-res:
+ }
+ if r.err != nil {
+ state := "error"
+ if errors.Is(r.err, ErrLoginRequired) || (autoStart && errors.Is(r.err, gotgerrors.ErrSessionUnauthorized)) {
+ state = "login_required"
+ }
+ ucMu.Lock()
+ if generation == gen {
+ starting = false
+ startCancel = nil
+ ustatus = Status{State: state, Error: r.err.Error(), Phone: phone}
+ }
+ ucMu.Unlock()
+ return r.err
+ }
+
+ if startCtx.Err() != nil {
+ r.client.Stop()
+ ucMu.Lock()
+ if generation == gen {
+ starting = false
+ startCancel = nil
+ ustatus = Status{State: "stopped"}
+ }
+ ucMu.Unlock()
+ return startCtx.Err()
+ }
+ tclient := r.client
+ tclient.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, func(ctx *ext.Context, u *ext.Update) error {
+ switch u.UpdateClass.(type) {
+ case *tg.UpdateEditChannelMessage, *tg.UpdateEditMessage, *tg.UpdateDeleteChannelMessages, *tg.UpdateDeleteMessages:
+ return dispatcher.EndGroups
+ }
+ chatID := tgutil.ChatIdFromPeer(u.EffectiveMessage.GetPeerID())
+ watchChats, err := database.GetWatchChatsByChatID(ctx, chatID)
+ if err != nil || len(watchChats) == 0 {
+ return dispatcher.EndGroups
+ }
+ return dispatcher.ContinueGroups
+ }))
+ tclient.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, handleMediaMessage))
+ if err := tclient.StartUpdateRecovery(startCtx); err != nil {
+ tclient.Stop()
+ ucMu.Lock()
+ if generation == gen {
+ starting = false
+ startCancel = nil
+ ustatus = Status{State: "error", Error: err.Error(), Phone: phone}
+ }
+ ucMu.Unlock()
+ return err
+ }
+
+ status := Status{State: "running", Phone: phone}
+ if tclient.Self != nil {
+ status.ID = tclient.Self.ID
+ status.Username = tclient.Self.Username
+ }
+ ucMu.Lock()
+ if generation != gen {
+ ucMu.Unlock()
+ tclient.Stop()
+ return errors.New("userbot start canceled")
+ }
+ uc = tclient
+ uctx = tclient.CreateContext()
+ starting = false
+ startCancel = nil
+ ustatus = status
+ ucMu.Unlock()
+
+ if tclient.Self != nil {
+ logger.Infof("User client logged in successfully: %s %s", tclient.Self.FirstName, tclient.Self.LastName)
+ } else {
+ logger.Info("User client logged in successfully")
+ }
+ return nil
+}
+
+func Stop() {
+ ucMu.Lock()
+ client := uc
+ uc = nil
+ uctx = nil
+ starting = false
+ if startCancel != nil {
+ startCancel()
+ startCancel = nil
+ }
+ generation++
+ ustatus = Status{State: "stopped"}
+ ucMu.Unlock()
+ if client != nil {
+ client.Stop()
+ }
+}
+
+// Logout stops the userbot and removes its persisted session.
+func Logout() error {
+ Stop()
+ sessionPath := config.C().Telegram.Userbot.Session
+ if sessionPath == "" {
+ return nil
+ }
+ for _, path := range []string{sessionPath, sessionPath + "-wal", sessionPath + "-shm"} {
+ if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
+ return err
}
- uc = r.client
- uc.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, func(ctx *ext.Context, u *ext.Update) error {
- switch u.UpdateClass.(type) {
- case *tg.UpdateEditChannelMessage, *tg.UpdateEditMessage, *tg.UpdateDeleteChannelMessages, *tg.UpdateDeleteMessages:
- return dispatcher.EndGroups
- }
- chatId := u.EffectiveChat().GetID()
- watchChats, err := database.GetWatchChatsByChatID(ctx, chatId)
- if err != nil || len(watchChats) == 0 {
- return dispatcher.EndGroups
- }
- return dispatcher.ContinueGroups
- }))
- uc.Dispatcher.AddHandler(handlers.NewMessage(filters.Message.Media, handleMediaMessage))
- log.FromContext(ctx).Infof("User client logged in successfully: %s", uc.Self.FirstName+" "+uc.Self.LastName)
- return uc, nil
}
+ return nil
}
diff --git a/client/user/watch.go b/client/user/watch.go
index 5506fb39..94b65d48 100644
--- a/client/user/watch.go
+++ b/client/user/watch.go
@@ -4,11 +4,11 @@ import (
"sync"
"time"
- "github.com/celestix/gotgproto/dispatcher"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
)
type MediaMessageEvent struct {
@@ -51,16 +51,18 @@ func sendMediaMessageEvent(event MediaMessageEvent) {
if timer, exists := mediaMessageHandler.timers[key]; exists {
timer.Stop()
- } else {
- mediaMessageHandler.events[key] = event
}
+ mediaMessageHandler.events[key] = event
mediaMessageHandler.timers[key] = time.AfterFunc(mediaMessageHandler.debounce, func() {
mediaMessageHandler.mu.Lock()
- event := mediaMessageHandler.events[key]
+ event, ok := mediaMessageHandler.events[key]
delete(mediaMessageHandler.events, key)
delete(mediaMessageHandler.timers, key)
mediaMessageHandler.mu.Unlock()
+ if !ok {
+ return
+ }
mediaMessageCh <- event
})
@@ -89,7 +91,7 @@ func handleMediaMessage(ctx *ext.Context, update *ext.Update) error {
if err != nil {
return err
}
- chatId := update.EffectiveChat().GetID()
+ chatId := tgutil.ChatIdFromPeer(update.EffectiveMessage.GetPeerID())
sendMediaMessageEvent(MediaMessageEvent{
Ctx: ctx,
ChatID: chatId,
diff --git a/client/worker/manager.go b/client/worker/manager.go
new file mode 100644
index 00000000..7f3e621f
--- /dev/null
+++ b/client/worker/manager.go
@@ -0,0 +1,375 @@
+package worker
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "sync"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/mygotg"
+ "github.com/krau/mygotg/ext"
+ "github.com/krau/mygotg/session"
+ "github.com/rs/xid"
+
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/database"
+)
+
+const (
+ StateStopped = "stopped"
+ StateStarting = "starting"
+ StateRunning = "running"
+ StateError = "error"
+)
+
+type Status struct {
+ State string `json:"state"`
+ Username string `json:"username,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+type ClientRef struct {
+ ID uint
+ Name string
+ Ext *ext.Context
+}
+
+type runtime struct {
+ id uint
+ name string
+ session string
+ mu sync.RWMutex
+ state string
+ err string
+ username string
+ client *mygotg.Client
+ ext *ext.Context
+ cancel context.CancelFunc
+ gen uint64
+}
+
+type Manager struct {
+ ctx context.Context
+ dataDir string
+ mu sync.RWMutex
+ workers map[uint]*runtime
+ order []uint
+ next int
+}
+
+var manager *Manager
+
+// Init creates the optional helper-bot manager. A manager with no workers is a
+// no-op, preserving the zero-config single-bot setup.
+func Init(ctx context.Context, dataDir string) {
+ m := &Manager{ctx: ctx, dataDir: dataDir, workers: make(map[uint]*runtime)}
+ manager = m
+ if err := m.reload(ctx); err != nil {
+ log.Errorf("Failed to load helper bots: %v", err)
+ }
+}
+
+func (m *Manager) reload(ctx context.Context) error {
+ rows, err := database.ListBotWorkers(ctx)
+ if err != nil {
+ return err
+ }
+ m.mu.Lock()
+ old := m.workers
+ m.workers = make(map[uint]*runtime, len(rows))
+ m.order = m.order[:0]
+ m.next = 0
+ m.mu.Unlock()
+ for _, rt := range old {
+ stopRuntime(rt)
+ }
+ for _, row := range rows {
+ m.mu.Lock()
+ rt := &runtime{id: row.ID, name: row.Name, session: row.SessionPath, state: StateStopped}
+ m.workers[row.ID] = rt
+ m.order = append(m.order, row.ID)
+ m.mu.Unlock()
+ if row.Enabled {
+ m.start(ctx, rt, row.Token)
+ }
+ }
+ return nil
+}
+
+func (m *Manager) start(ctx context.Context, rt *runtime, token string) {
+ if token == "" {
+ rt.mu.Lock()
+ rt.state, rt.err = StateError, "token is empty"
+ rt.mu.Unlock()
+ return
+ }
+ sessionPath := rt.session
+ if sessionPath == "" {
+ sessionPath = NewSessionPath(m.dataDir)
+ if err := database.UpdateBotWorker(ctx, rt.id, map[string]any{"session_path": sessionPath}); err != nil {
+ rt.mu.Lock()
+ rt.state, rt.err = StateError, err.Error()
+ rt.mu.Unlock()
+ return
+ }
+ }
+ if err := os.MkdirAll(filepath.Dir(sessionPath), 0o755); err != nil {
+ rt.mu.Lock()
+ rt.state, rt.err = StateError, err.Error()
+ rt.mu.Unlock()
+ return
+ }
+
+ runCtx, cancel := context.WithCancel(ctx)
+ rt.mu.Lock()
+ rt.session = sessionPath
+ rt.gen++
+ gen := rt.gen
+ rt.cancel = cancel
+ rt.state, rt.err, rt.username = StateStarting, "", ""
+ rt.mu.Unlock()
+
+ go func() {
+ resolver, err := tgutil.NewConfigProxyResolver()
+ if err != nil {
+ m.finishStart(rt, gen, cancel, nil, err, "")
+ return
+ }
+ client, err := mygotg.NewClient(
+ config.C().Telegram.AppID,
+ config.C().Telegram.AppHash,
+ mygotg.ClientTypeBot(token),
+ &mygotg.ClientOpts{
+ Session: session.SqlSession(database.GetDialect(sessionPath)),
+ Context: runCtx,
+ NoUpdates: true,
+ DisableCopyright: true,
+ Resolver: resolver,
+ MaxRetries: config.C().Telegram.RpcRetry,
+ },
+ )
+ if err != nil {
+ m.finishStart(rt, gen, cancel, nil, err, "")
+ return
+ }
+ username := ""
+ if client.Self != nil {
+ username = client.Self.Username
+ }
+ m.finishStart(rt, gen, cancel, client, nil, username)
+ }()
+}
+
+func (m *Manager) finishStart(rt *runtime, gen uint64, cancel context.CancelFunc, client *mygotg.Client, err error, username string) {
+ rt.mu.Lock()
+ defer rt.mu.Unlock()
+ if rt.gen != gen {
+ if client != nil {
+ client.Stop()
+ }
+ cancel()
+ return
+ }
+ if err != nil {
+ rt.state, rt.err = StateError, err.Error()
+ rt.cancel = nil
+ cancel()
+ return
+ }
+ rt.client = client
+ rt.ext = client.CreateContext()
+ rt.state = StateRunning
+ rt.err = ""
+ rt.username = username
+}
+
+// StartWorker starts one persisted worker and returns its current status.
+func StartWorker(ctx context.Context, id uint) (Status, error) {
+ if manager == nil {
+ return Status{}, errors.New("helper bot manager is not initialized")
+ }
+ row, err := database.GetBotWorker(ctx, id)
+ if err != nil {
+ return Status{}, err
+ }
+ if !row.Enabled {
+ return Status{}, errors.New("helper bot is disabled")
+ }
+ manager.mu.Lock()
+ rt := manager.workers[id]
+ if rt == nil {
+ rt = &runtime{id: row.ID, name: row.Name, session: row.SessionPath, state: StateStopped}
+ manager.workers[id] = rt
+ manager.order = append(manager.order, id)
+ }
+ manager.mu.Unlock()
+ rt.mu.RLock()
+ state := rt.state
+ rt.mu.RUnlock()
+ if state == StateRunning || state == StateStarting {
+ return StatusFor(id), nil
+ }
+ rt.mu.Lock()
+ rt.name = row.Name
+ if row.SessionPath != "" {
+ rt.session = row.SessionPath
+ }
+ rt.mu.Unlock()
+ lifecycleCtx := manager.ctx
+ if lifecycleCtx == nil {
+ lifecycleCtx = ctx
+ }
+ manager.start(lifecycleCtx, rt, row.Token)
+ return StatusFor(id), nil
+}
+
+// StopWorker stops one worker while keeping its persisted row.
+func StopWorker(id uint) {
+ if manager == nil {
+ return
+ }
+ manager.mu.RLock()
+ rt := manager.workers[id]
+ manager.mu.RUnlock()
+ if rt != nil {
+ stopRuntime(rt)
+ }
+}
+
+func StopAll() {
+ if manager == nil {
+ return
+ }
+ manager.mu.RLock()
+ workers := make([]*runtime, 0, len(manager.workers))
+ for _, rt := range manager.workers {
+ workers = append(workers, rt)
+ }
+ manager.mu.RUnlock()
+ for _, rt := range workers {
+ stopRuntime(rt)
+ }
+}
+
+func stopRuntime(rt *runtime) {
+ rt.mu.Lock()
+ client := rt.client
+ rt.client = nil
+ rt.ext = nil
+ rt.state, rt.err, rt.username = StateStopped, "", ""
+ rt.gen++
+ cancel := rt.cancel
+ rt.cancel = nil
+ rt.mu.Unlock()
+ if cancel != nil {
+ cancel()
+ }
+ if client != nil {
+ client.Stop()
+ }
+}
+
+// UploadExts returns running helper contexts in round-robin order. Callers can
+// skip helpers that cannot access the intended target chat.
+func UploadExts() []*ext.Context {
+ if manager == nil {
+ return nil
+ }
+ manager.mu.Lock()
+ defer manager.mu.Unlock()
+ if len(manager.order) == 0 {
+ return nil
+ }
+ exts := make([]*ext.Context, 0, len(manager.order))
+ start := manager.next
+ first := -1
+ for i := range manager.order {
+ idx := (start + i) % len(manager.order)
+ rt := manager.workers[manager.order[idx]]
+ if rt == nil {
+ continue
+ }
+ rt.mu.RLock()
+ state, extCtx := rt.state, rt.ext
+ rt.mu.RUnlock()
+ if state == StateRunning && extCtx != nil {
+ if first == -1 {
+ first = idx
+ }
+ exts = append(exts, extCtx)
+ }
+ }
+ if first >= 0 {
+ manager.next = (first + 1) % len(manager.order)
+ }
+ return exts
+}
+
+// UploadExt returns the next running helper context for one Telegram upload,
+// or nil when no helper is configured.
+func UploadExt() *ext.Context {
+ exts := UploadExts()
+ if len(exts) > 0 {
+ return exts[0]
+ }
+ return nil
+}
+
+// RunningClients returns a snapshot of running helpers for download routing.
+func RunningClients() []ClientRef {
+ if manager == nil {
+ return nil
+ }
+ manager.mu.RLock()
+ defer manager.mu.RUnlock()
+ refs := make([]ClientRef, 0, len(manager.order))
+ for _, id := range manager.order {
+ rt := manager.workers[id]
+ if rt == nil {
+ continue
+ }
+ rt.mu.RLock()
+ if rt.state == StateRunning && rt.ext != nil {
+ refs = append(refs, ClientRef{ID: rt.id, Name: rt.name, Ext: rt.ext})
+ }
+ rt.mu.RUnlock()
+ }
+ return refs
+}
+
+// StatusFor returns the runtime status for a worker id.
+func StatusFor(id uint) Status {
+ if manager == nil {
+ return Status{State: StateStopped}
+ }
+ manager.mu.RLock()
+ rt := manager.workers[id]
+ manager.mu.RUnlock()
+ if rt == nil {
+ return Status{State: StateStopped}
+ }
+ rt.mu.RLock()
+ defer rt.mu.RUnlock()
+ return Status{State: rt.state, Username: rt.username, Error: rt.err}
+}
+
+// NewSessionPath returns a unique session database path under data/workers.
+func NewSessionPath(dataDir string) string {
+ return filepath.Join(dataDir, "workers", xid.New().String()+".db")
+}
+
+// RemoveSession deletes a worker session database and its SQLite side files.
+func RemoveSession(path string) error {
+ if path == "" {
+ return nil
+ }
+ for _, suffix := range []string{"", "-wal", "-shm"} {
+ if err := os.Remove(path + suffix); err != nil && !os.IsNotExist(err) {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/cmd/root.go b/cmd/root.go
index 35bb6444..de99aad0 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -2,11 +2,12 @@ package cmd
import (
"context"
- "fmt"
+ "os"
+ "github.com/krau/SaveAny-Bot/boot"
+ "github.com/krau/SaveAny-Bot/cmd/testenv"
"github.com/krau/SaveAny-Bot/cmd/upload"
"github.com/krau/SaveAny-Bot/cmd/watch"
- "github.com/krau/SaveAny-Bot/config"
"github.com/spf13/cobra"
)
@@ -17,13 +18,14 @@ var rootCmd = &cobra.Command{
}
func init() {
- config.RegisterFlags(rootCmd)
+ boot.RegisterFlags(rootCmd)
+ testenv.Register(rootCmd)
upload.Register(rootCmd)
watch.Register(rootCmd)
}
func Execute(ctx context.Context) {
if err := rootCmd.ExecuteContext(ctx); err != nil {
- fmt.Println(err)
+ os.Exit(1)
}
}
diff --git a/cmd/run.go b/cmd/run.go
index cf244567..392abb46 100644
--- a/cmd/run.go
+++ b/cmd/run.go
@@ -7,26 +7,20 @@ import (
"strings"
"time"
- "slices"
-
"github.com/charmbracelet/log"
- "github.com/krau/SaveAny-Bot/api"
- "github.com/krau/SaveAny-Bot/client/bot"
- userclient "github.com/krau/SaveAny-Bot/client/user"
- "github.com/krau/SaveAny-Bot/common/cache"
- "github.com/krau/SaveAny-Bot/common/i18n"
+ "github.com/spf13/cobra"
+
+ "github.com/krau/SaveAny-Bot/app"
+ "github.com/krau/SaveAny-Bot/boot"
"github.com/krau/SaveAny-Bot/common/selfupdate"
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
"github.com/krau/SaveAny-Bot/config"
- "github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/database"
- "github.com/krau/SaveAny-Bot/parsers"
- "github.com/krau/SaveAny-Bot/storage"
- "github.com/spf13/cobra"
)
func Run(cmd *cobra.Command, _ []string) {
ctx, cancel := context.WithCancel(cmd.Context())
+ defer cancel()
logger := log.NewWithOptions(os.Stdout, log.Options{
Level: log.InfoLevel,
ReportTimestamp: true,
@@ -38,34 +32,36 @@ func Run(cmd *cobra.Command, _ []string) {
selfupdate.CleanLeftovers()
- configFile := config.GetConfigFile(cmd)
- if err := config.Init(ctx, configFile); err != nil {
+ opts := boot.Parse(cmd)
+ opts.InitWeb = true
+ res, err := boot.Init(ctx, opts)
+ if err != nil {
logger.Fatal("Init failed", "error", err)
}
-
- level, err := log.ParseLevel(strings.TrimSpace(config.C().Log.Level))
- if err != nil {
- logger.Warn("Invalid log level, fallback to debug", "level", config.C().Log.Level, "error", err)
- level = log.DebugLevel
+ cfg := res.Config
+ if level, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err == nil {
+ logger.SetLevel(level)
}
- logger.SetLevel(level)
- exitChan, err := initAll(ctx)
- if err != nil {
+ if unfinished, err := database.HasUnfinishedTasks(ctx); err != nil {
+ logger.Errorf("Failed to inspect unfinished tasks; keeping cache: %v", err)
+ } else if !unfinished {
+ cleanCache(cfg)
+ }
+ application := app.New(ctx, res.Settings, app.Options{
+ DataDir: res.DataDir,
+ DBPath: res.DBPath,
+ Listen: res.Listen,
+ NoWeb: res.NoWeb,
+ })
+ if err := application.Start(); err != nil {
logger.Fatal("Init failed", "error", err)
}
- go func() {
- <-exitChan
- cancel()
- }()
- core.Run(ctx)
-
- <-ctx.Done()
+ <-application.Done()
logger.Info("Exiting...")
defer logger.Info("Exit complete")
- core.Close()
- cleanCache()
+ application.Stop()
if selfupdate.ShouldRestart() {
logger.Info("Restarting to run the updated version...")
if err := selfupdate.Restart(); err != nil {
@@ -74,54 +70,34 @@ func Run(cmd *cobra.Command, _ []string) {
}
}
-func initAll(ctx context.Context) (<-chan struct{}, error) {
- cache.Init()
- logger := log.FromContext(ctx)
- i18n.Init(config.C().Lang)
- logger.Info("Initializing...")
- database.Init(ctx)
- storage.LoadStorages(ctx)
- if config.C().Parser.PluginEnable {
- for _, dir := range config.C().Parser.PluginDirs {
- if err := parsers.LoadPlugins(ctx, dir); err != nil {
- logger.Error("Failed to load parser plugins", "dir", dir, "error", err)
- } else {
- logger.Debug("Loaded parser plugins from directory", "dir", dir)
- }
- }
- }
- if config.C().Telegram.Userbot.Enable {
- _, err := userclient.Login(ctx)
- if err != nil {
- logger.Fatal("User login failed", "error", err)
- }
- }
- if err := api.Start(ctx); err != nil {
- logger.Fatal("Failed to start API server", "error", err)
- }
- return bot.Init(ctx), nil
-}
-
-func cleanCache() {
- if config.C().NoCleanCache {
+func cleanCache(cfg config.Config) {
+ if cfg.NoCleanCache {
return
}
- if config.C().Temp.BasePath != "" && !config.C().Stream {
- if slices.Contains([]string{"/", ".", "\\", ".."}, filepath.Clean(config.C().Temp.BasePath)) {
- log.Error("Invalid cache directory", "path", config.C().Temp.BasePath)
- return
- }
+ if cfg.Temp.BasePath != "" && !cfg.Stream {
currentDir, err := os.Getwd()
if err != nil {
log.Error("Failed to get working directory", "error", err)
return
}
- cachePath := filepath.Join(currentDir, config.C().Temp.BasePath)
+ cachePath := cfg.Temp.BasePath
+ if !filepath.IsAbs(cachePath) {
+ cachePath = filepath.Join(currentDir, cachePath)
+ }
cachePath, err = filepath.Abs(cachePath)
if err != nil {
log.Error("Failed to get absolute cache path", "error", err)
return
}
+ cachePath = filepath.Clean(cachePath)
+ // Never clean the working directory or one of its ancestors.
+ if rel, err := filepath.Rel(cachePath, currentDir); err != nil || rel == "." || !strings.HasPrefix(rel, "..") {
+ log.Error("Refusing to clean unsafe cache directory", "path", cachePath)
+ return
+ }
+ if _, err := os.Stat(cachePath); os.IsNotExist(err) {
+ return
+ }
log.Info("Cleaning cache directory", "path", cachePath)
if err := fsutil.RemoveAllInDir(cachePath); err != nil {
log.Error("Failed to clean cache directory", "error", err)
diff --git a/cmd/testenv/cmd.go b/cmd/testenv/cmd.go
new file mode 100644
index 00000000..1407bf71
--- /dev/null
+++ b/cmd/testenv/cmd.go
@@ -0,0 +1,225 @@
+package testenv
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/spf13/cobra"
+
+ "github.com/krau/SaveAny-Bot/app"
+ "github.com/krau/SaveAny-Bot/boot"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/settings"
+)
+
+var testenvCmd = &cobra.Command{
+ Use: "testenv",
+ Short: "run a seeded local environment for end-to-end testing",
+ Long: `Run the web UI and task pipeline against a disposable, seeded data directory.
+
+No Telegram credentials are needed. The command creates an admin account, a
+local storage and a whitelisted user, then prints the login details and a
+sample URL that can be used to create a direct-links task.`,
+ RunE: run,
+}
+
+func Register(root *cobra.Command) {
+ flags := testenvCmd.Flags()
+ flags.String("admin-password", "", "web admin password (default saveany-test)")
+ flags.String("storage-name", "test-local", "name of the seeded local storage")
+ flags.Int64("user-chat-id", 10001, "chat ID of the seeded whitelisted user")
+ flags.Bool("keep", false, "keep the generated data directory after exit")
+ root.AddCommand(testenvCmd)
+}
+
+func run(cmd *cobra.Command, _ []string) error {
+ ctx := cmd.Context()
+ opts := boot.Parse(cmd)
+ opts.InitWeb = true
+
+ autoDir := false
+ if !cmd.Flags().Changed("data-dir") && strings.TrimSpace(os.Getenv("SAVEANY_DATA_DIR")) == "" {
+ dir, err := os.MkdirTemp("", "saveany-testenv-")
+ if err != nil {
+ return fmt.Errorf("create test data directory: %w", err)
+ }
+ opts.DataDir = dir
+ opts.DBPath = filepath.Join(dir, "saveany.db")
+ autoDir = true
+ }
+
+ keep, err := cmd.Flags().GetBool("keep")
+ if err != nil {
+ return err
+ }
+ if autoDir && !keep {
+ defer func() {
+ if err := os.RemoveAll(opts.DataDir); err != nil {
+ log.Warn("Failed to remove test data directory", "path", opts.DataDir, "error", err)
+ }
+ }()
+ }
+
+ password, err := cmd.Flags().GetString("admin-password")
+ if err != nil {
+ return err
+ }
+ if password == "" {
+ password = strings.TrimSpace(os.Getenv("SAVEANY_ADMIN_PASSWORD"))
+ }
+ if password == "" {
+ password = "saveany-test"
+ }
+ if err := os.Setenv("SAVEANY_ADMIN_PASSWORD", password); err != nil {
+ return fmt.Errorf("set admin password: %w", err)
+ }
+
+ res, err := boot.Init(ctx, opts)
+ if err != nil {
+ return fmt.Errorf("initialize test environment: %w", err)
+ }
+
+ storageName, err := cmd.Flags().GetString("storage-name")
+ if err != nil {
+ return err
+ }
+ chatID, err := cmd.Flags().GetInt64("user-chat-id")
+ if err != nil {
+ return err
+ }
+ downloadsDir := filepath.Join(opts.DataDir, "downloads")
+ if err := os.MkdirAll(downloadsDir, 0o755); err != nil {
+ return fmt.Errorf("create downloads directory: %w", err)
+ }
+ record, err := res.Settings.CreateStorage(ctx, settings.StorageInput{
+ Name: storageName,
+ Type: "local",
+ Enable: true,
+ Config: map[string]any{"base_path": downloadsDir},
+ })
+ if err != nil && !errors.Is(err, settings.ErrStorageExists) {
+ return fmt.Errorf("seed local storage: %w", err)
+ }
+ if record == nil {
+ records, err := database.ListStorageRecords(ctx)
+ if err != nil {
+ return fmt.Errorf("find seeded storage: %w", err)
+ }
+ for i := range records {
+ if records[i].Name == storageName {
+ record = &records[i]
+ break
+ }
+ }
+ if record == nil {
+ return fmt.Errorf("seeded storage %q not found", storageName)
+ }
+ }
+
+ if _, err := database.GetUserByChatID(ctx, chatID); err != nil {
+ if !database.IsNotFound(err) {
+ return fmt.Errorf("find seeded user: %w", err)
+ }
+ if err := database.CreateUser(ctx, chatID); err != nil {
+ return fmt.Errorf("create seeded user: %w", err)
+ }
+ }
+ user, err := database.GetUserByChatID(ctx, chatID)
+ if err != nil {
+ return fmt.Errorf("load seeded user: %w", err)
+ }
+ user.Enabled = true
+ user.DefaultStorageID = record.ID
+ if err := database.UpdateUser(ctx, user); err != nil {
+ return fmt.Errorf("update seeded user: %w", err)
+ }
+ if err := res.Settings.ReloadConfig(ctx); err != nil {
+ return fmt.Errorf("reload settings: %w", err)
+ }
+
+ sampleServer, sampleURL, err := serveSample(ctx, opts.DataDir)
+ if err != nil {
+ return err
+ }
+ defer func() {
+ shutdownCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
+ defer cancel()
+ if err := sampleServer.Shutdown(shutdownCtx); err != nil {
+ log.Warnf("Failed to shut down sample server: %v", err)
+ }
+ }()
+
+ application := app.New(ctx, res.Settings, app.Options{
+ DataDir: opts.DataDir,
+ DBPath: opts.DBPath,
+ Listen: res.Listen,
+ NoWeb: res.NoWeb,
+ })
+ if err := application.Start(); err != nil {
+ return fmt.Errorf("start test environment: %w", err)
+ }
+ defer application.Stop()
+
+ fmt.Println("Test environment ready")
+ fmt.Printf(" Web UI: %s\n", webURL(res.Listen))
+ fmt.Printf(" Password: %s\n", password)
+ fmt.Printf(" Data dir: %s\n", opts.DataDir)
+ storagePath := downloadsDir
+ if record.ConfigJSON != "" {
+ var cfg map[string]any
+ if err := json.Unmarshal([]byte(record.ConfigJSON), &cfg); err == nil {
+ if path, ok := cfg["base_path"].(string); ok && path != "" {
+ storagePath = path
+ }
+ }
+ }
+ fmt.Printf(" Storage: %s -> %s\n", record.Name, storagePath)
+ fmt.Printf(" Test user: %d\n", chatID)
+ fmt.Printf(" Sample URL: %s\n", sampleURL)
+ fmt.Println("Press Ctrl+C to stop.")
+
+ <-ctx.Done()
+ log.Info("Stopping test environment")
+ return nil
+}
+
+func serveSample(_ context.Context, dataDir string) (*http.Server, string, error) {
+ dir := filepath.Join(dataDir, "sample")
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ return nil, "", fmt.Errorf("create sample directory: %w", err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "sample.txt"), []byte("SaveAny-Bot end-to-end sample\n"), 0o644); err != nil {
+ return nil, "", fmt.Errorf("write sample file: %w", err)
+ }
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ return nil, "", fmt.Errorf("listen for sample server: %w", err)
+ }
+ server := &http.Server{Handler: http.FileServer(http.Dir(dir)), ReadHeaderTimeout: 5 * time.Second}
+ go func() {
+ if err := server.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
+ log.Error("Sample server error", "error", err)
+ }
+ }()
+ return server, "http://" + ln.Addr().String() + "/sample.txt", nil
+}
+
+func webURL(listen string) string {
+ host, port, err := net.SplitHostPort(listen)
+ if err != nil {
+ return "http://" + listen
+ }
+ if host == "" || host == "0.0.0.0" || host == "::" {
+ host = "127.0.0.1"
+ }
+ return "http://" + net.JoinHostPort(host, port) + "/"
+}
diff --git a/cmd/upload/cmd.go b/cmd/upload/cmd.go
index 9648c595..9add90d0 100644
--- a/cmd/upload/cmd.go
+++ b/cmd/upload/cmd.go
@@ -1,31 +1,28 @@
package upload
import (
- "context"
"fmt"
- "io"
"os"
"path"
"path/filepath"
+ "time"
"github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/boot"
"github.com/krau/SaveAny-Bot/client/bot"
- "github.com/krau/SaveAny-Bot/common/cache"
- "github.com/krau/SaveAny-Bot/common/i18n"
- "github.com/krau/SaveAny-Bot/common/utils/ioutil"
+ "github.com/krau/SaveAny-Bot/common/upload"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
- "github.com/krau/SaveAny-Bot/database"
- "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
stortype "github.com/krau/SaveAny-Bot/pkg/enums/storage"
"github.com/krau/SaveAny-Bot/storage"
"github.com/spf13/cobra"
)
var uploadCmd = &cobra.Command{
- Use: "upload",
- Short: "upload local files to storage",
- RunE: Upload,
+ Use: "upload",
+ Short: "upload local files to storage",
+ SilenceUsage: true,
+ RunE: Upload,
}
func Register(root *cobra.Command) {
@@ -57,71 +54,63 @@ func Upload(cmd *cobra.Command, args []string) error {
}
ctx := cmd.Context()
- log := log.FromContext(ctx)
- configFile := config.GetConfigFile(cmd)
- if err := config.Init(ctx, configFile); err != nil {
+ logger := log.FromContext(ctx)
+ opts := boot.Parse(cmd)
+ opts.InitWeb = false
+ if _, err := boot.Init(ctx, opts); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
- i18n.Init(config.C().Lang)
- cache.Init()
- database.Init(ctx)
stor, err := storage.GetStorageByName(ctx, storname)
if err != nil {
- log.Fatal("Failed to get storage", "error", err)
+ return fmt.Errorf("failed to get storage %q: %w", storname, err)
}
switch stor.Type() {
case stortype.Telegram:
- bot.Init(ctx)
- default:
- // placeholder for other storage types that may need special initialization
+ if err := bot.Start(ctx); err != nil {
+ return fmt.Errorf("failed to start bot: %w", err)
+ }
+ defer bot.Stop()
}
- file, err := os.Open(filepath.Clean(fp))
+ fileInfo, err := os.Stat(filepath.Clean(fp))
if err != nil {
- log.Fatal("Failed to open file", "error", err)
+ return fmt.Errorf("failed to stat file %q: %w", fp, err)
}
- defer file.Close()
- fileInfo, err := file.Stat()
- if err != nil {
- log.Fatal("Failed to get file info", "error", err)
+ if fileInfo.IsDir() {
+ return fmt.Errorf("%q is a directory", fp)
}
- fileName := fileInfo.Name()
- fileSize := fileInfo.Size()
-
- uploadPath := path.Join(dirPath, fileName)
- ctx = context.WithValue(ctx, ctxkey.ContentLength, fileSize)
+ uploadPath := path.Join(dirPath, fileInfo.Name())
ctx = tgutil.ExtWithContext(ctx, bot.ExtContext())
- var reader io.Reader
var progressUI *UploadProgress
- log.Info("Uploading file...", "file", fp, "to", storname, "as", uploadPath)
-
- if !noProgress && fileSize > 0 {
- progressUI = NewUploadProgress(fileName, fileSize)
+ if !noProgress && fileInfo.Size() > 0 {
+ progressUI = NewUploadProgress(fileInfo.Name(), fileInfo.Size())
progressUI.Start()
-
- reader = ioutil.NewProgressReader(file, fileSize, func(read int64, total int64) {
- if total > 0 {
- progressUI.UpdateProgress(float64(read) / float64(total))
- }
- })
- } else {
- reader = file
}
-
- if err := stor.Save(ctx, reader, uploadPath); err != nil {
+ onProgress := func(uploaded, total int64) {
+ if progressUI != nil && total > 0 {
+ progressUI.UpdateProgress(float64(uploaded) / float64(total))
+ }
+ }
+ logger.Info("Uploading file...", "file", fp, "to", storname, "as", uploadPath)
+ err = upload.SaveFile(ctx, filepath.Clean(fp), uploadPath, upload.Options{
+ Storage: stor,
+ Retries: max(config.C().Retry, 1),
+ RetryDelay: 3 * time.Second,
+ OnProgress: onProgress,
+ })
+ if err != nil {
if progressUI != nil {
progressUI.SetError(err)
}
- log.Fatal("Failed to upload file", "error", err)
+ return err
}
-
if progressUI != nil {
progressUI.Done()
}
- log.Info("File uploaded successfully")
+ logger.Info("File uploaded successfully")
return nil
}
diff --git a/cmd/version.go b/cmd/version.go
index 46bd8aef..c8357fa3 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -1,6 +1,7 @@
package cmd
import (
+ "errors"
"fmt"
"runtime"
@@ -16,8 +17,9 @@ var VersionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "Print the version number of saveany-bot",
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", config.Version, runtime.GOOS, runtime.GOARCH, config.BuildTime, config.GitCommit)
+ return nil
},
}
@@ -25,42 +27,37 @@ var upgradeCmd = &cobra.Command{
Use: "upgrade",
Aliases: []string{"up"},
Short: "Upgrade saveany-bot to the latest version",
- Run: func(cmd *cobra.Command, args []string) {
+ RunE: func(cmd *cobra.Command, args []string) error {
v, err := semver.Parse(config.Version)
if err != nil {
- fmt.Println("Currently in development version or version info injection failed:", err)
- return
+ return fmt.Errorf("current build has no release version: %w", err)
}
if err := selfupdate.Updatable(); err != nil {
- fmt.Println("This build cannot update itself:", err)
- return
+ return fmt.Errorf("this build cannot update itself: %w", err)
}
latest, found, err := ghselfupdate.DetectLatest(config.GitRepo)
if err != nil {
- fmt.Println("Error occurred while detecting latest version:", err)
- return
+ return fmt.Errorf("detect latest release: %w", err)
}
if !found {
- fmt.Println("No releases found")
- return
+ return errors.New("no releases found")
}
if latest.Version.Major != v.Major {
- fmt.Printf("Major version upgrade detected: %s -> %s. Please manually download the latest version and check the migration guide.\n", v, latest.Version)
- return
+ return fmt.Errorf("major upgrade %s -> %s requires a manual download and migration review", v, latest.Version)
}
if latest.Version.Equals(v) || latest.Version.LT(v) {
fmt.Println("Current binary is the latest version", config.Version)
- return
+ return nil
}
fmt.Printf("Updating to version %s...\n", latest.Version)
latest, err = selfupdate.Update(v)
if err != nil {
- fmt.Println("Update failed:", err)
- return
+ return fmt.Errorf("update failed: %w", err)
}
fmt.Println("Successfully updated to version", latest.Version)
fmt.Println("Release note:\n", latest.ReleaseNotes)
fmt.Println("Restart saveany-bot to use it.")
+ return nil
},
}
diff --git a/cmd/watch/cmd.go b/cmd/watch/cmd.go
index aee8b88a..88cfa3e3 100644
--- a/cmd/watch/cmd.go
+++ b/cmd/watch/cmd.go
@@ -5,12 +5,10 @@ import (
"time"
"github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/boot"
"github.com/krau/SaveAny-Bot/client/bot"
- "github.com/krau/SaveAny-Bot/common/cache"
- "github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
- "github.com/krau/SaveAny-Bot/database"
stortype "github.com/krau/SaveAny-Bot/pkg/enums/storage"
"github.com/krau/SaveAny-Bot/storage"
"github.com/spf13/cobra"
@@ -84,13 +82,11 @@ func runWatch(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
logger := log.FromContext(ctx)
- configFile := config.GetConfigFile(cmd)
- if err := config.Init(ctx, configFile); err != nil {
+ opts := boot.Parse(cmd)
+ opts.InitWeb = false
+ if _, err := boot.Init(ctx, opts); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
- i18n.Init(config.C().Lang)
- cache.Init()
- database.Init(ctx)
stor, err := storage.GetStorageByName(ctx, storName)
if err != nil {
@@ -99,7 +95,10 @@ func runWatch(cmd *cobra.Command, _ []string) error {
// Telegram storage needs the bot client and its ext context injected into ctx.
if stor.Type() == stortype.Telegram {
- bot.Init(ctx)
+ if err := bot.Start(ctx); err != nil {
+ return fmt.Errorf("failed to start bot: %w", err)
+ }
+ defer bot.Stop()
ctx = tgutil.ExtWithContext(ctx, bot.ExtContext())
}
diff --git a/cmd/watch/uploader.go b/cmd/watch/uploader.go
index 66c06c10..2cb177f3 100644
--- a/cmd/watch/uploader.go
+++ b/cmd/watch/uploader.go
@@ -10,7 +10,7 @@ import (
"time"
"github.com/charmbracelet/log"
- "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
+ uploadpkg "github.com/krau/SaveAny-Bot/common/upload"
"github.com/krau/SaveAny-Bot/storage"
)
@@ -126,7 +126,7 @@ func (u *Uploader) worker(ctx context.Context) {
}
func (u *Uploader) process(ctx context.Context, job uploadJob) {
- if err := u.uploadWithRetry(ctx, job); err != nil {
+ if err := u.upload(ctx, job); err != nil {
if ctx.Err() != nil {
u.clearInflight(job.localPath)
return
@@ -160,40 +160,8 @@ func (u *Uploader) clearInflight(localPath string) {
u.mu.Unlock()
}
-func (u *Uploader) uploadWithRetry(ctx context.Context, job uploadJob) error {
- var lastErr error
- for attempt := 1; attempt <= u.retry; attempt++ {
- if ctx.Err() != nil {
- return ctx.Err()
- }
- err := u.upload(ctx, job)
- if err == nil {
- return nil
- }
- if ctx.Err() != nil {
- return ctx.Err()
- }
- lastErr = err
- u.logger.Warnf("upload %s failed (attempt %d/%d): %v", job.localPath, attempt, u.retry, err)
- if attempt < u.retry {
- select {
- case <-time.After(u.retryDelay):
- case <-ctx.Done():
- return ctx.Err()
- }
- }
- }
- return lastErr
-}
-
func (u *Uploader) upload(ctx context.Context, job uploadJob) error {
- file, err := os.Open(filepath.Clean(job.localPath))
- if err != nil {
- return fmt.Errorf("failed to open file: %w", err)
- }
- defer file.Close()
-
- info, err := file.Stat()
+ info, err := os.Stat(filepath.Clean(job.localPath))
if err != nil {
return fmt.Errorf("failed to stat file: %w", err)
}
@@ -204,16 +172,25 @@ func (u *Uploader) upload(ctx context.Context, job uploadJob) error {
// Keep the relative directory structure on the storage side.
storagePath := path.Join(u.destDir, filepath.ToSlash(job.relPath))
- uploadCtx := context.WithValue(ctx, ctxkey.ContentLength, info.Size())
- if u.overwrite {
- uploadCtx = storage.WithOverwrite(uploadCtx)
- } else if u.stor.Exists(uploadCtx, storagePath) {
- u.logger.Infof("skip existing file: %s", storagePath)
- return nil
+ uploadCtx := ctx
+ if !u.overwrite {
+ exists, checkErr := u.stor.Exists(uploadCtx, storagePath)
+ if checkErr != nil {
+ return fmt.Errorf("failed to check existing file: %w", checkErr)
+ }
+ if exists {
+ u.logger.Infof("skip existing file: %s", storagePath)
+ return nil
+ }
}
u.logger.Infof("uploading %s -> %s (%d bytes)", job.localPath, storagePath, info.Size())
- if err := u.stor.Save(uploadCtx, file, storagePath); err != nil {
+ if err := uploadpkg.SaveFile(uploadCtx, job.localPath, storagePath, uploadpkg.Options{
+ Storage: u.stor,
+ Overwrite: u.overwrite,
+ Retries: u.retry,
+ RetryDelay: u.retryDelay,
+ }); err != nil {
return fmt.Errorf("failed to save to storage: %w", err)
}
u.logger.Infof("uploaded %s", storagePath)
diff --git a/common/i18n/i18n.go b/common/i18n/i18n.go
index 48c3e25c..55b0c235 100644
--- a/common/i18n/i18n.go
+++ b/common/i18n/i18n.go
@@ -2,6 +2,7 @@ package i18n
import (
"embed"
+ "sync"
"maps"
@@ -15,40 +16,51 @@ import (
var localesFS embed.FS
var (
+ mu sync.RWMutex
bundle *i18n.Bundle
localizer *i18n.Localizer
)
func Init(lang string) {
- bundle = i18n.NewBundle(language.SimplifiedChinese)
- bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
+ newBundle := i18n.NewBundle(language.SimplifiedChinese)
+ newBundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
files, err := localesFS.ReadDir("locale")
if err != nil {
panic("failed to read locale directory: " + err.Error())
}
for _, file := range files {
- if _, err := bundle.LoadMessageFileFS(localesFS, "locale/"+file.Name()); err != nil {
+ if _, err := newBundle.LoadMessageFileFS(localesFS, "locale/"+file.Name()); err != nil {
panic("failed to load message file: " + err.Error())
}
}
if lang == "" {
lang = "zh-Hans"
}
- localizer = i18n.NewLocalizer(bundle, lang)
- if localizer == nil {
+ newLocalizer := i18n.NewLocalizer(newBundle, lang)
+ if newLocalizer == nil {
panic("failed to create localizer, check your config for valid language setting")
}
+ mu.Lock()
+ bundle = newBundle
+ localizer = newLocalizer
+ mu.Unlock()
}
func T(key i18nk.Key, templateData ...map[string]any) string {
- if localizer == nil || bundle == nil {
- Init("zh-Hans")
+ mu.RLock()
+ loc := localizer
+ mu.RUnlock()
+ if loc == nil {
+ Init("")
+ mu.RLock()
+ loc = localizer
+ mu.RUnlock()
}
templateDataMap := make(map[string]any)
for _, data := range templateData {
maps.Copy(templateDataMap, data)
}
- msg, err := localizer.Localize(&i18n.LocalizeConfig{
+ msg, err := loc.Localize(&i18n.LocalizeConfig{
MessageID: string(key),
TemplateData: templateDataMap,
})
diff --git a/common/i18n/i18nk/keys.go b/common/i18n/i18nk/keys.go
index 7ef3714c..506b2720 100644
--- a/common/i18n/i18nk/keys.go
+++ b/common/i18n/i18nk/keys.go
@@ -11,31 +11,19 @@ const (
BotMsgAria2InfoAria2DownloadAdded Key = "bot.msg.aria2.info_aria2_download_added"
BotMsgAria2InfoSelectStorage Key = "bot.msg.aria2.info_select_storage"
BotMsgCancelErrorCancelFailed Key = "bot.msg.cancel.error_cancel_failed"
- BotMsgCancelInfoCancelRequested Key = "bot.msg.cancel.info_cancel_requested"
BotMsgCancelInfoCancellingTask Key = "bot.msg.cancel.info_cancelling_task"
- BotMsgCancelUsage Key = "bot.msg.cancel.usage"
- BotMsgCmdAria2dl Key = "bot.msg.cmd.aria2dl"
- BotMsgCmdCancel Key = "bot.msg.cmd.cancel"
BotMsgCmdConfig Key = "bot.msg.cmd.config"
- BotMsgCmdDir Key = "bot.msg.cmd.dir"
- BotMsgCmdDl Key = "bot.msg.cmd.dl"
- BotMsgCmdFnametmpl Key = "bot.msg.cmd.fnametmpl"
- BotMsgCmdHelp Key = "bot.msg.cmd.help"
- BotMsgCmdImport Key = "bot.msg.cmd.import"
- BotMsgCmdLswatch Key = "bot.msg.cmd.lswatch"
+ BotMsgCmdDirs Key = "bot.msg.cmd.dirs"
+ BotMsgCmdDownload Key = "bot.msg.cmd.download"
BotMsgCmdParser Key = "bot.msg.cmd.parser"
- BotMsgCmdRule Key = "bot.msg.cmd.rule"
+ BotMsgCmdRules Key = "bot.msg.cmd.rules"
BotMsgCmdSave Key = "bot.msg.cmd.save"
- BotMsgCmdSilent Key = "bot.msg.cmd.silent"
BotMsgCmdStart Key = "bot.msg.cmd.start"
- BotMsgCmdStorage Key = "bot.msg.cmd.storage"
- BotMsgCmdSyncpeers Key = "bot.msg.cmd.syncpeers"
- BotMsgCmdTask Key = "bot.msg.cmd.task"
+ BotMsgCmdTasks Key = "bot.msg.cmd.tasks"
+ BotMsgCmdTemplate Key = "bot.msg.cmd.template"
BotMsgCmdTransfer Key = "bot.msg.cmd.transfer"
- BotMsgCmdUnwatch Key = "bot.msg.cmd.unwatch"
BotMsgCmdUpdate Key = "bot.msg.cmd.update"
BotMsgCmdWatch Key = "bot.msg.cmd.watch"
- BotMsgCmdYtdlp Key = "bot.msg.cmd.ytdlp"
BotMsgCommonButtonConflictOverwrite Key = "bot.msg.common.button_conflict_overwrite"
BotMsgCommonButtonConflictRename Key = "bot.msg.common.button_conflict_rename"
BotMsgCommonButtonConflictSkip Key = "bot.msg.common.button_conflict_skip"
@@ -84,23 +72,30 @@ const (
BotMsgCommonPromptSelectDefaultDir Key = "bot.msg.common.prompt_select_default_dir"
BotMsgCommonPromptSelectDefaultStorage Key = "bot.msg.common.prompt_select_default_storage"
BotMsgCommonPromptSelectDir Key = "bot.msg.common.prompt_select_dir"
+ BotMsgConfigButtonBack Key = "bot.msg.config.button_back"
BotMsgConfigButtonConflictStrategy Key = "bot.msg.config.button_conflict_strategy"
+ BotMsgConfigButtonDefaultStorage Key = "bot.msg.config.button_default_storage"
BotMsgConfigButtonFilenameStrategy Key = "bot.msg.config.button_filename_strategy"
+ BotMsgConfigButtonSilent Key = "bot.msg.config.button_silent"
+ BotMsgConfigButtonTemplate Key = "bot.msg.config.button_template"
BotMsgConfigConflictStrategyAsk Key = "bot.msg.config.conflict_strategy_ask"
BotMsgConfigConflictStrategyOverwrite Key = "bot.msg.config.conflict_strategy_overwrite"
BotMsgConfigConflictStrategyRename Key = "bot.msg.config.conflict_strategy_rename"
BotMsgConfigConflictStrategySkip Key = "bot.msg.config.conflict_strategy_skip"
BotMsgConfigErrorInvalidCallbackData Key = "bot.msg.config.error_invalid_callback_data"
BotMsgConfigErrorInvalidTemplate Key = "bot.msg.config.error_invalid_template"
- BotMsgConfigFnametmplHelp Key = "bot.msg.config.fnametmpl_help"
BotMsgConfigInfoConflictStrategySet Key = "bot.msg.config.info_conflict_strategy_set"
BotMsgConfigInfoCurrentTemplatePrefix Key = "bot.msg.config.info_current_template_prefix"
BotMsgConfigInfoFilenameStrategySet Key = "bot.msg.config.info_filename_strategy_set"
BotMsgConfigInfoTemplateUpdated Key = "bot.msg.config.info_template_updated"
+ BotMsgConfigMenuHint Key = "bot.msg.config.menu_hint"
+ BotMsgConfigMenuTitle Key = "bot.msg.config.menu_title"
BotMsgConfigPromptSelectConflictStrategy Key = "bot.msg.config.prompt_select_conflict_strategy"
BotMsgConfigPromptSelectFilenameStrategy Key = "bot.msg.config.prompt_select_filename_strategy"
BotMsgConfigPromptSelectOption Key = "bot.msg.config.prompt_select_option"
+ BotMsgConfigTemplateHelp Key = "bot.msg.config.template_help"
BotMsgDirButtonDefault Key = "bot.msg.dir.button_default"
+ BotMsgDirButtonDelete Key = "bot.msg.dir.button_delete"
BotMsgDirErrorCreateDirFailed Key = "bot.msg.dir.error_create_dir_failed"
BotMsgDirErrorDeleteDirFailed Key = "bot.msg.dir.error_delete_dir_failed"
BotMsgDirErrorGetUserDirsFailed Key = "bot.msg.dir.error_get_user_dirs_failed"
@@ -118,9 +113,17 @@ const (
BotMsgDirHelpUsage Key = "bot.msg.dir.help_usage"
BotMsgDirInfoCreateDirSuccess Key = "bot.msg.dir.info_create_dir_success"
BotMsgDirInfoDeleteDirSuccess Key = "bot.msg.dir.info_delete_dir_success"
- BotMsgDlErrorNoValidLinks Key = "bot.msg.dl.error_no_valid_links"
- BotMsgDlInfoFilesSelectStorage Key = "bot.msg.dl.info_files_select_storage"
- BotMsgDlUsage Key = "bot.msg.dl.usage"
+ BotMsgDirMenuEmpty Key = "bot.msg.dir.menu_empty"
+ BotMsgDirMenuHint Key = "bot.msg.dir.menu_hint"
+ BotMsgDirMenuTitle Key = "bot.msg.dir.menu_title"
+ BotMsgDownloadButtonAria2 Key = "bot.msg.download.button_aria2"
+ BotMsgDownloadButtonDirect Key = "bot.msg.download.button_direct"
+ BotMsgDownloadButtonVideo Key = "bot.msg.download.button_video"
+ BotMsgDownloadErrorNoEngine Key = "bot.msg.download.error_no_engine"
+ BotMsgDownloadErrorNoValidLinks Key = "bot.msg.download.error_no_valid_links"
+ BotMsgDownloadInfoSelectStorage Key = "bot.msg.download.info_select_storage"
+ BotMsgDownloadPromptSelectEngine Key = "bot.msg.download.prompt_select_engine"
+ BotMsgDownloadUsage Key = "bot.msg.download.usage"
BotMsgHelpTextFmt Key = "bot.msg.help_text_fmt"
BotMsgMediaGroupErrorBuildStorageSelectKeyboardFailed Key = "bot.msg.media_group.error_build_storage_select_keyboard_failed"
BotMsgMediaGroupInfoGroupFoundFilesSelectStorage Key = "bot.msg.media_group.info_group_found_files_select_storage"
@@ -152,6 +155,7 @@ const (
BotMsgProgressAvgSpeedPrefix Key = "bot.msg.progress.avg_speed_prefix"
BotMsgProgressBatchCanceled Key = "bot.msg.progress.batch_canceled"
BotMsgProgressBatchDone Key = "bot.msg.progress.batch_done"
+ BotMsgProgressBatchDoneWithFailed Key = "bot.msg.progress.batch_done_with_failed"
BotMsgProgressBatchDoneWithSkipped Key = "bot.msg.progress.batch_done_with_skipped"
BotMsgProgressBatchFailedGroup Key = "bot.msg.progress.batch_failed_group"
BotMsgProgressBatchFailedItem Key = "bot.msg.progress.batch_failed_item"
@@ -219,6 +223,11 @@ const (
BotMsgProgressYtdlpDone Key = "bot.msg.progress.ytdlp_done"
BotMsgProgressYtdlpDownloading Key = "bot.msg.progress.ytdlp_downloading"
BotMsgProgressYtdlpStart Key = "bot.msg.progress.ytdlp_start"
+ BotMsgRetryErrorFailed Key = "bot.msg.retry.error_failed"
+ BotMsgRetryInfoStarted Key = "bot.msg.retry.info_started"
+ BotMsgRuleButtonDelete Key = "bot.msg.rule.button_delete"
+ BotMsgRuleButtonDisable Key = "bot.msg.rule.button_disable"
+ BotMsgRuleButtonEnable Key = "bot.msg.rule.button_enable"
BotMsgRuleErrorCreateRuleFailed Key = "bot.msg.rule.error_create_rule_failed"
BotMsgRuleErrorDeleteRuleFailed Key = "bot.msg.rule.error_delete_rule_failed"
BotMsgRuleErrorGetUserRulesFailed Key = "bot.msg.rule.error_get_user_rules_failed"
@@ -240,6 +249,9 @@ const (
BotMsgRuleInfoPresetImported Key = "bot.msg.rule.info_preset_imported"
BotMsgRuleInfoRuleModeDisabled Key = "bot.msg.rule.info_rule_mode_disabled"
BotMsgRuleInfoRuleModeEnabled Key = "bot.msg.rule.info_rule_mode_enabled"
+ BotMsgRuleMenuEmpty Key = "bot.msg.rule.menu_empty"
+ BotMsgRuleMenuHint Key = "bot.msg.rule.menu_hint"
+ BotMsgRuleMenuTitle Key = "bot.msg.rule.menu_title"
BotMsgRulePromptProvideRuleId Key = "bot.msg.rule.prompt_provide_rule_id"
BotMsgRulePromptProvideStorageName Key = "bot.msg.rule.prompt_provide_storage_name"
BotMsgSaveErrorInvalidIdOrUsername Key = "bot.msg.save.error_invalid_id_or_username"
@@ -249,8 +261,11 @@ const (
BotMsgSyncpeersFailed Key = "bot.msg.syncpeers.failed"
BotMsgSyncpeersStart Key = "bot.msg.syncpeers.start"
BotMsgSyncpeersSuccess Key = "bot.msg.syncpeers.success"
+ BotMsgTasksCancelButton Key = "bot.msg.tasks.cancel_button"
BotMsgTasksCancelFailed Key = "bot.msg.tasks.cancel_failed"
BotMsgTasksCancelRequestedPrefix Key = "bot.msg.tasks.cancel_requested_prefix"
+ BotMsgTasksFailedEmpty Key = "bot.msg.tasks.failed_empty"
+ BotMsgTasksFailedTitle Key = "bot.msg.tasks.failed_title"
BotMsgTasksFieldCreated Key = "bot.msg.tasks.field_created"
BotMsgTasksFieldId Key = "bot.msg.tasks.field_id"
BotMsgTasksFieldStatus Key = "bot.msg.tasks.field_status"
@@ -259,8 +274,15 @@ const (
BotMsgTasksInfoAddedToQueuePrefix Key = "bot.msg.tasks.info_added_to_queue_prefix"
BotMsgTasksInfoFilenamePrefix Key = "bot.msg.tasks.info_filename_prefix"
BotMsgTasksInfoQueueLengthPrefix Key = "bot.msg.tasks.info_queue_length_prefix"
+ BotMsgTasksMenuBack Key = "bot.msg.tasks.menu_back"
+ BotMsgTasksMenuFailed Key = "bot.msg.tasks.menu_failed"
+ BotMsgTasksMenuHint Key = "bot.msg.tasks.menu_hint"
+ BotMsgTasksMenuQueued Key = "bot.msg.tasks.menu_queued"
+ BotMsgTasksMenuRunning Key = "bot.msg.tasks.menu_running"
+ BotMsgTasksMenuTitle Key = "bot.msg.tasks.menu_title"
BotMsgTasksQueuedEmpty Key = "bot.msg.tasks.queued_empty"
BotMsgTasksQueuedTitle Key = "bot.msg.tasks.queued_title"
+ BotMsgTasksRetryButton Key = "bot.msg.tasks.retry_button"
BotMsgTasksRunningEmpty Key = "bot.msg.tasks.running_empty"
BotMsgTasksRunningTitle Key = "bot.msg.tasks.running_title"
BotMsgTasksStatusCancelRequested Key = "bot.msg.tasks.status_cancel_requested"
@@ -268,8 +290,6 @@ const (
BotMsgTasksStatusRunning Key = "bot.msg.tasks.status_running"
BotMsgTasksTotalPrefix Key = "bot.msg.tasks.total_prefix"
BotMsgTasksTruncatedNote Key = "bot.msg.tasks.truncated_note"
- BotMsgTasksUsage Key = "bot.msg.tasks.usage"
- BotMsgTasksUsageCancel Key = "bot.msg.tasks.usage_cancel"
BotMsgTelegraphErrorBuildStorageSelectKeyboardFailed Key = "bot.msg.telegraph.error_build_storage_select_keyboard_failed"
BotMsgTelegraphInfoPicCountPrefix Key = "bot.msg.telegraph.info_pic_count_prefix"
BotMsgTelegraphInfoPromptSelectStorage Key = "bot.msg.telegraph.info_prompt_select_storage"
@@ -303,9 +323,7 @@ const (
BotMsgUpdateInfoUpgradingWithVersion Key = "bot.msg.update.info_upgrading_with_version"
BotMsgWatchErrorFilterFormatInvalid Key = "bot.msg.watch.error_filter_format_invalid"
BotMsgWatchErrorFilterTypeUnsupported Key = "bot.msg.watch.error_filter_type_unsupported"
- BotMsgWatchErrorNotifyArgInvalid Key = "bot.msg.watch.error_notify_arg_invalid"
BotMsgWatchErrorUnwatchChatFailed Key = "bot.msg.watch.error_unwatch_chat_failed"
- BotMsgWatchErrorUnwatchNoChatProvided Key = "bot.msg.watch.error_unwatch_no_chat_provided"
BotMsgWatchErrorWatchChatFailed Key = "bot.msg.watch.error_watch_chat_failed"
BotMsgWatchInfoAlreadyWatchingChat Key = "bot.msg.watch.info_already_watching_chat"
BotMsgWatchInfoNotifyDisabled Key = "bot.msg.watch.info_notify_disabled"
@@ -315,13 +333,16 @@ const (
BotMsgWatchInfoWatchListEmpty Key = "bot.msg.watch.info_watch_list_empty"
BotMsgWatchInfoWatchListFilterPrefix Key = "bot.msg.watch.info_watch_list_filter_prefix"
BotMsgWatchInfoWatchListHeader Key = "bot.msg.watch.info_watch_list_header"
+ BotMsgWatchMenuAdd Key = "bot.msg.watch.menu_add"
+ BotMsgWatchMenuAddHelp Key = "bot.msg.watch.menu_add_help"
+ BotMsgWatchMenuBack Key = "bot.msg.watch.menu_back"
+ BotMsgWatchMenuHint Key = "bot.msg.watch.menu_hint"
+ BotMsgWatchMenuList Key = "bot.msg.watch.menu_list"
+ BotMsgWatchMenuNotify Key = "bot.msg.watch.menu_notify"
+ BotMsgWatchMenuStop Key = "bot.msg.watch.menu_stop"
+ BotMsgWatchMenuSync Key = "bot.msg.watch.menu_sync"
+ BotMsgWatchMenuTitle Key = "bot.msg.watch.menu_title"
BotMsgWatchNotifyDetected Key = "bot.msg.watch.notify_detected"
- BotMsgWatchHelpText Key = "bot.msg.watch_help_text"
- BotMsgYtdlpErrorDownloadFailed Key = "bot.msg.ytdlp.error_download_failed"
- BotMsgYtdlpErrorNoValidUrls Key = "bot.msg.ytdlp.error_no_valid_urls"
- BotMsgYtdlpInfoDownloading Key = "bot.msg.ytdlp.info_downloading"
- BotMsgYtdlpInfoUrlsSelectStorage Key = "bot.msg.ytdlp.info_urls_select_storage"
- BotMsgYtdlpUsage Key = "bot.msg.ytdlp.usage"
ConfigErrDuplicateStorageName Key = "config.err.duplicate_storage_name"
ConfigErrInvalidCacheDir Key = "config.err.invalid_cache_dir"
ErrCleanCacheFailed Key = "err.clean_cache_failed"
diff --git a/common/i18n/locale/en.yaml b/common/i18n/locale/en.yaml
index dd50608a..25a15d4b 100644
--- a/common/i18n/locale/en.yaml
+++ b/common/i18n/locale/en.yaml
@@ -24,48 +24,33 @@ bot:
Version: %s , Commit: %s
Commands:
- /start - Start using the bot
- /help - Show help
- /silent - Toggle silent mode
- /storage - Set default storage
- /save [custom filename] - Save file
- /import [channel_id] [filter] - Import files from storage to Telegram
- /dir - Manage storage directories
- /rule - Manage rules
- /config - Modify configuration
- /fnametmpl - Set custom filename template
+ /start - Open help and command list
+ /save - Save replied media or batch messages
+ /download - Download links (direct, Aria2, or video)
+ /transfer - Transfer files between storages
+ /tasks - Manage tasks
+ /config - User settings (storage, silent mode, naming)
+ /template - Set filename template
+ /dirs - Manage storage directories
+ /rules - Manage storage rules
+ /watch - Watch chats and notifications (UserBot)
/parser - Manage parser plugins
- /task - Manage task queue
- /watch - Watch chats and auto save (UserBot)
- /unwatch - Stop watching chats (UserBot)
- /lswatch - List watched chats (UserBot)
- /syncpeers - Sync peer chats (UserBot)
- /update - Check and upgrade to latest version
+ /update - Check for updates
Usage guide: https://sabot.unv.app/usage
cmd:
- start: "Start using"
- silent: "Toggle silent mode"
- storage: "Set default storage"
- dir: "Manage storage directories"
- rule: "Manage auto-save rules"
+ start: "Open help"
save: "Save files"
- dl: "Download files from given links"
- aria2dl: "Download files using Aria2"
- ytdlp: "Download video/audio using yt-dlp"
- import: "Import files from storage to Telegram"
+ download: "Download links (direct, Aria2, video)"
transfer: "Transfer files between storages"
- task: "Manage task queue"
- cancel: "Cancel task"
- watch: "Watch chats (UserBot)"
- unwatch: "Stop watching chats (UserBot)"
- lswatch: "List watched chats (UserBot)"
- config: "Modify configuration"
- fnametmpl: "Set filename template"
- help: "Show help"
+ tasks: "Manage tasks"
+ config: "User settings"
+ template: "Set filename template"
+ dirs: "Manage storage directories"
+ rules: "Manage auto-save rules"
+ watch: "Watch chats and notifications (UserBot)"
parser: "Manage parsers"
update: "Check for updates"
- syncpeers: "Sync peer chats (UserBot)"
save_help_text: |
Usage:
@@ -76,22 +61,6 @@ bot:
2. After setting default storage, send /save to batch save files. Rules will be applied; if no rule matches, default storage will be used.
Example:
/save @acherkrau 114-514
- watch_help_text: |
- Use /watch to watch messages in a chat and automatically save them to the default storage, following storage rules.
-
- Syntax:
- /watch [filter]
-
- Parameters:
- - : Chat ID or username
- - [filter]: Optional, format is filter_type:expression , see docs for all supported filters
-
- Example:
- /watch -1002229835658 msgre:.*plana.*
-
- This will watch chat with ID -1002229835658 and save all media messages containing "plana".
-
- Notifications for saved messages are off by default, toggle them with /watch notify on|off.
common:
cancel_button_text: "Cancel"
error_invalid_regex: "Invalid regex: {{.Error}}"
@@ -106,7 +75,7 @@ bot:
info_found_files_select_storage: "Found {{.Count}} files, please select storage"
error_get_user_failed: "Failed to get user"
error_get_user_with_err_failed: "Failed to get user: {{.Error}}"
- error_default_storage_not_set: "Please set a default storage first with /storage"
+ error_default_storage_not_set: "Please set a default storage first in /config"
error_no_available_storage: "No available storage"
error_get_storage_failed: "Failed to get storage: {{.Error}}"
prompt_select_default_storage: "Please select a storage to set as default"
@@ -146,6 +115,18 @@ bot:
save:
error_invalid_id_or_username: "Invalid ID or username: {{.Error}}"
watch:
+ menu_title: "Watch manager"
+ menu_hint: "Manage watched chats, notifications, and peer sync."
+ menu_add: "Add watch"
+ menu_list: "Watched chats"
+ menu_notify: "Notifications"
+ menu_sync: "Sync peers"
+ menu_stop: "Stop"
+ menu_back: "Back"
+ menu_add_help: |
+ Send /watch [filter] to start watching.
+ Example:
+ /watch -1002229835658 msgre:.*plana.*
error_filter_format_invalid: "Invalid filter format, please use :"
error_filter_type_unsupported: "Unsupported filter type, please see the docs"
error_watch_chat_failed: "Failed to watch chat: {{.Error}}"
@@ -154,16 +135,22 @@ bot:
info_watch_list_empty: "No chats are being watched currently"
info_watch_list_header: "Currently watched chats:\n"
info_watch_list_filter_prefix: " (filter: "
- error_unwatch_no_chat_provided: "Please provide a chat ID or username to unwatch"
error_unwatch_chat_failed: "Failed to unwatch chat: {{.Error}}"
info_watch_chat_stopped: "Stopped watching chat: {{.Chat}}"
- error_notify_arg_invalid: "Invalid argument, usage: /watch notify on|off"
- info_notify_enabled: "Task notifications are enabled for every chat you watch: each saved message and its result is reported here.\n\nUsage: /watch notify on|off"
- info_notify_disabled: "Task notifications are disabled for every chat you watch.\n\nUsage: /watch notify on|off"
+ info_notify_enabled: "Task notifications are enabled for every chat you watch: each saved message and its result is reported here."
+ info_notify_disabled: "Task notifications are disabled for every chat you watch."
notify_detected: "🔔 Watched message detected \n{{.Name}}\nProgress will be reported here."
tasks:
- usage_cancel: "Usage: /tasks cancel "
- usage: "Usage: /tasks [running|queued|cancel ]"
+ menu_title: "Task manager"
+ menu_hint: "Inspect running, queued, and failed tasks."
+ menu_running: "Running"
+ menu_queued: "Queued"
+ menu_failed: "Failed"
+ menu_back: "Back"
+ failed_title: "Failed tasks"
+ failed_empty: "No failed tasks"
+ retry_button: "Retry"
+ cancel_button: "Cancel"
cancel_failed: "Failed to cancel task: {{.Error}}"
cancel_requested_prefix: "Cancel requested for task: "
running_empty: "No running tasks"
@@ -184,6 +171,12 @@ bot:
info_filename_prefix: "Filename: "
info_queue_length_prefix: "\nCurrent queued tasks: "
rule:
+ menu_title: "Storage rules"
+ menu_hint: "Use /rules add to add a rule."
+ menu_empty: "No rules configured"
+ button_enable: "Enable rules"
+ button_disable: "Disable rules"
+ button_delete: "Delete"
error_get_user_rules_failed: "Failed to get user rules"
error_update_user_failed: "Failed to update user"
info_rule_mode_enabled: "Rule mode enabled"
@@ -195,19 +188,23 @@ bot:
error_invalid_rule_id: "Invalid rule ID"
error_delete_rule_failed: "Failed to delete rule"
info_delete_rule_success: "Rule deleted successfully"
- help_usage: "Usage: /rule "
+ help_usage: "Usage: /rules "
help_current_mode_enabled: "\nRule mode is currently enabled"
help_current_mode_disabled: "\nRule mode is currently disabled"
help_available_ops: "\n\nAvailable operations:\n"
help_switch_suffix: " - Toggle rule mode\n"
- help_add_suffix: " - Add rule\n"
+ help_add_suffix: " - Add rule\n"
help_del_suffix: " - Delete rule\n"
- help_preset_suffix: " [base_path] - Import built-in filetype rules (video/image/audio/document/archive)\n"
+ help_preset_suffix: " [base_path] - Import built-in filetype rules (video/image/audio/document/archive)\n"
help_existing_rules_prefix: "\nCurrent rules:\n"
prompt_provide_storage_name: "Please provide a storage name"
error_storage_not_found: "Storage not found: {{.Storage}}"
info_preset_imported: "Imported {{.Count}} built-in classification rules into storage {{.Storage}}"
dir:
+ menu_title: "Storage directories"
+ menu_hint: "Use /dirs add to add a directory."
+ menu_empty: "No directories configured"
+ button_delete: "Delete"
error_get_user_dirs_failed: "Failed to get user directories"
error_get_user_failed: "Failed to get user"
error_create_dir_failed: "Failed to create directory"
@@ -216,14 +213,14 @@ bot:
error_delete_dir_failed: "Failed to delete directory"
info_delete_dir_success: "Directory deleted successfully"
error_unknown_operation: "Unknown operation"
- help_usage: "Usage: /dir "
+ help_usage: "Usage: /dirs "
help_available_ops: "\n\nAvailable operations:\n"
help_add_suffix: " - Add path\n"
help_del_suffix: " - Delete path\n"
help_add_example_prefix: "\nAdd path example:\n"
- help_add_example_cmd: "/dir add local1 path/to/dir"
+ help_add_example_cmd: "/dirs add local1 path/to/dir"
help_del_example_prefix: "\n\nDelete path example:\n"
- help_del_example_cmd: "/dir del 3"
+ help_del_example_cmd: "/dirs del 3"
help_existing_dirs_prefix: "\n\nCurrent paths:\n"
button_default: "Default"
parser:
@@ -282,7 +279,13 @@ bot:
info_upgrade_success: "Upgraded to version {{.Version}}\nIf the bot did not restart automatically, please start it manually"
button_upgrade: "Upgrade"
config:
+ menu_title: "User configuration"
+ menu_hint: "Choose a setting to change."
prompt_select_option: "Please select an option to configure"
+ button_silent: "Silent mode"
+ button_default_storage: "Default storage"
+ button_template: "Filename template"
+ button_back: "Back"
button_filename_strategy: "Filename strategy"
button_conflict_strategy: "Duplicate file strategy"
error_invalid_callback_data: "Invalid callback data"
@@ -295,9 +298,9 @@ bot:
conflict_strategy_ask: "Ask every time"
conflict_strategy_overwrite: "Always overwrite"
conflict_strategy_skip: "Always skip"
- fnametmpl_help: |-
- Use this command to set filename template, for example:
- /fnametmpl Image_{{"{{.msgid}}"}}_{{"{{.msgdate}}"}}.jpg
+ template_help: |-
+ Use /template to set a filename template, for example:
+ /template Image_{{"{{.msgid}}"}}_{{"{{.msgdate}}"}}.jpg
Available variables:
- {{"{{.msgid}}"}}: Message ID
@@ -312,16 +315,21 @@ bot:
If template parsing fails, it will fall back to default filename.
info_template_updated: "Filename template updated"
info_current_template_prefix: "Current template: {{.Template}}"
- dl:
- usage: "Usage: /dl ..."
+ download:
+ usage: |
+ Usage: /download [yt-dlp options]
+ Choose direct download, Aria2, or video extraction.
+ Examples:
+ /download https://example.com/file.zip
+ /download https://www.youtube.com/watch?v=...
+ /download --format best https://www.youtube.com/watch?v=...
+ prompt_select_engine: "Found {{.Count}} link(s), choose a download engine"
+ button_direct: "Direct"
+ button_aria2: "Aria2"
+ button_video: "Video (yt-dlp)"
+ info_select_storage: "Choose storage for {{.Count}} link(s)"
error_no_valid_links: "No valid links to download"
- info_files_select_storage: "Total {{.Count}} files, please select storage"
- ytdlp:
- usage: "Usage: /ytdlp [OPTIONS] [URL2] ...\nExamples:\n /ytdlp https://example.com/video\n /ytdlp --format best https://example.com/video\n /ytdlp --extract-audio --audio-format mp3 https://example.com/video"
- error_no_valid_urls: "No valid URLs"
- info_urls_select_storage: "Found {{.Count}} links, please select storage"
- info_downloading: "Downloading via yt-dlp..."
- error_download_failed: "yt-dlp download failed: {{.Error}}"
+ error_no_engine: "No available download engine for these links"
transfer:
usage: |
Usage: /transfer :/ [filter]
@@ -344,10 +352,11 @@ bot:
start_stats: "Total files: {{.Count}}\nTotal size: {{.SizeMB}} MB"
info_files_select_storage: "Total {{.Count}} files ({{.SizeMB}} MB), please select target storage"
error_build_storage_select_keyboard_failed: "Failed to build storage selection keyboard: {{.Error}}"
+ retry:
+ info_started: "Retry task created: {{.TaskID}} ({{.Count}} files)"
+ error_failed: "Failed to retry task: {{.Error}}"
cancel:
- usage: "Usage: /cancel "
error_cancel_failed: "Failed to cancel task: {{.Error}}"
- info_cancel_requested: "Cancel requested for task: {{.TaskID}}"
info_cancelling_task: "Cancelling task..."
media_group:
info_saving_files: "Saving files..."
@@ -371,6 +380,7 @@ bot:
batch_summary_skipped: "⏭️ Skipped: {{.Count}}"
batch_done: "✅ Completed \n\nFiles: {{.Count}}\nTotal size: {{.Size}}"
batch_done_with_skipped: "⚠️ Completed \n\nSucceeded: {{.Success}}\nSkipped: {{.Skipped}}\nTotal size: {{.Size}}"
+ batch_done_with_failed: "⚠️ Completed with failures \n\nSucceeded: {{.Success}}\nFailed: {{.Failed}}\nSkipped: {{.Skipped}}\nTotal size: {{.Size}}{{.List}}"
batch_canceled: "🚫 Task canceled \n\nFiles: {{.Total}}\nCompleted: {{.Completed}}\nIncomplete: {{.Incomplete}}\nSkipped: {{.Skipped}}"
batch_failed_item: "❌ Processing failed \n\nFailed file: {{.Index}}. {{.Name}}\nStage: {{.Stage}}\nProgress: {{.Progress}}\nSpeed before failure: {{.Speed}}\nReason: {{.Reason}}\n\n✅ Completed: {{.Completed}}\n❌ Failed: {{.Failed}}\n⏹️ Incomplete: {{.Incomplete}}"
batch_failed_group: "❌ Batch upload failed \n\nAffected files: {{.Affected}}\nReason: {{.Reason}}\n\n✅ Completed: {{.Completed}}\n❌ Batch failed: {{.Failed}}\n⏹️ Incomplete: {{.Incomplete}}"
diff --git a/common/i18n/locale/zh-Hans.yaml b/common/i18n/locale/zh-Hans.yaml
index b8057dce..ff1d0cd8 100644
--- a/common/i18n/locale/zh-Hans.yaml
+++ b/common/i18n/locale/zh-Hans.yaml
@@ -24,75 +24,43 @@ bot:
版本: %s , 提交: %s
命令:
- /start - 开始使用
- /help - 显示帮助
- /silent - 开关静默模式
- /storage - 设置默认存储位置
- /save [自定义文件名] - 保存文件
- /dl <链接1> <链接2> ... - 下载给定链接的文件
- /import <存储名> <目录路径> [频道ID] [过滤器] - 从存储端导入文件到 Telegram
- /dir - 管理存储目录
- /rule - 管理规则
- /config - 修改配置
- /fnametmpl - 设置文件自定义命名模板
+ /start - 打开帮助和命令列表
+ /save - 保存回复的媒体或批量消息
+ /download - 下载链接(直链、Aria2 或视频)
+ /transfer - 在存储端之间传输文件
+ /tasks - 管理任务
+ /config - 用户设置(存储、静默模式、命名)
+ /template - 设置文件名模板
+ /dirs - 管理存储目录
+ /rules - 管理存储规则
+ /watch - 监听聊天和通知(UserBot)
/parser - 管理解析器插件
- /task - 管理任务队列
- /watch - 监听聊天并自动保存 (UserBot)
- /unwatch - 取消监听聊天 (UserBot)
- /lswatch - 列出正在监听的聊天 (UserBot)
- /syncpeers - 同步对话列表 (UserBot)
- /update - 检查更新并升级
+ /update - 检查更新
使用帮助: https://sabot.unv.app/usage
cmd:
- start: "开始使用"
- silent: "切换静默模式"
- storage: "设置默认存储端"
- dir: "管理存储文件夹"
- rule: "管理自动存储规则"
+ start: "打开帮助"
save: "保存文件"
- dl: "下载给定链接的文件"
- aria2dl: "使用 Aria2 下载给定链接的文件"
- ytdlp: "使用 yt-dlp 下载视频/音频"
- import: "从存储端导入文件到 Telegram"
+ download: "下载链接(直链、Aria2、视频)"
transfer: "在存储端之间传输文件"
- task: "管理任务队列"
- cancel: "取消任务"
- watch: "监听聊天(UserBot)"
- unwatch: "取消监听聊天(UserBot)"
- lswatch: "列出监听的聊天(UserBot)"
- syncpeers: "同步对话列表(UserBot)"
- config: "修改配置"
- fnametmpl: "设置文件命名模板"
- help: "显示帮助"
+ tasks: "管理任务"
+ config: "用户设置"
+ template: "设置文件名模板"
+ dirs: "管理存储目录"
+ rules: "管理自动存储规则"
+ watch: "监听聊天和通知(UserBot)"
parser: "管理解析器"
update: "检查更新"
save_help_text: |
使用方法:
- 1. 使用该命令回复要保存的文件, 可选文件名参数.
+ 1. 回复要保存的文件消息,可选指定文件名。
示例:
/save custom_file_name.mp4
- 2. 设置默认存储后, 发送 /save <频道ID/用户名> <消息ID范围> 来批量保存文件. 遵从存储规则, 若未匹配到任何规则则使用默认存储.
+ 2. 设置默认存储后,发送 /save <频道ID/用户名> <消息ID范围> 批量保存文件。优先匹配存储规则,未命中时使用默认存储。
示例:
/save @acherkrau 114-514
- watch_help_text: |
- 使用 /watch 命令监听一个聊天的消息, 并自动保存到默认存储中, 遵从存储规则.
-
- 命令语法:
- /watch [filter]
-
- 参数:
- - : 聊天的 ID 或用户名
- - [filter]: 可选, 格式为 过滤器类型:表达式 , 所有支持类型的过滤器请查看文档
-
- 命令示例:
- /watch -1002229835658 msgre:.*plana.*
-
- 这将监听 ID 为 -1002229835658 的聊天, 并转存所有包含 "plana" 的媒体消息
-
- 监听任务的通知默认关闭, 可使用 /watch notify on|off 开启或关闭.
common:
cancel_button_text: "取消任务"
error_invalid_regex: "无效的正则表达式: {{.Error}}"
@@ -100,14 +68,14 @@ bot:
error_invalid_id_or_username: "无效的ID或用户名: {{.Error}}"
error_get_messages_failed: "获取消息失败: {{.Error}}"
info_fetching_messages: "正在获取消息..."
- error_no_messages_in_range: "没有找到指定范围内的消息"
- error_no_savable_messages_in_range: "没有找到指定范围内的可保存消息"
+ error_no_messages_in_range: "未找到指定范围内的消息"
+ error_no_savable_messages_in_range: "指定范围内未找到可保存的消息"
error_build_storage_select_message_failed: "构建存储选择消息失败: {{.Error}}"
error_build_storage_select_keyboard_failed: "构建存储选择键盘失败: {{.Error}}"
info_found_files_select_storage: "找到 {{.Count}} 个文件, 请选择存储位置"
error_get_user_failed: "获取用户失败"
error_get_user_with_err_failed: "获取用户失败: {{.Error}}"
- error_default_storage_not_set: "请先设置默认存储, 使用 /storage 命令"
+ error_default_storage_not_set: "请先在 /config 中设置默认存储"
error_no_available_storage: "无可用的存储"
error_get_storage_failed: "获取存储失败: {{.Error}}"
prompt_select_default_storage: "请选择要设为默认的存储位置"
@@ -135,39 +103,57 @@ bot:
info_silent_mode_off: "已关闭静默模式"
error_get_file_failed: "获取文件失败: {{.Error}}"
info_fetching_file_info: "正在获取文件信息..."
- error_no_savable_files_found: "没有找到可保存的文件"
+ error_no_savable_files_found: "未找到可保存的文件"
error_parse_telegraph_path_failed: "解析 telegraph 路径失败: {{.Error}}"
info_fetching_telegraph_page: "正在获取 telegraph 页面..."
error_get_telegraph_page_failed: "获取 telegraph 页面失败: {{.Error}}"
- error_no_images_in_telegraph_page: "在 telegraph 页面中未找到图片"
+ error_no_images_in_telegraph_page: "Telegraph 页面中未找到图片"
error_build_dir_select_keyboard_failed: "构建目录选择键盘失败: {{.Error}}"
error_no_permission: |
- 您不在白名单中, 无法使用此 Bot.
- 您可以部署自己的实例: https://github.com/krau/SaveAny-Bot
+ 当前账号未在白名单中,无法使用此 Bot。
+ 可自行部署实例: https://github.com/krau/SaveAny-Bot
save:
error_invalid_id_or_username: "无效的ID或用户名: {{.Error}}"
watch:
+ menu_title: "监听管理"
+ menu_hint: "管理监听聊天、通知和对话同步。"
+ menu_add: "添加监听"
+ menu_list: "监听列表"
+ menu_notify: "通知开关"
+ menu_sync: "同步对话"
+ menu_stop: "停止"
+ menu_back: "返回"
+ menu_add_help: |
+ 发送 /watch <聊天ID/用户名> [过滤器] 开始监听。
+ 示例:
+ /watch -1002229835658 msgre:.*plana.*
error_filter_format_invalid: "过滤器格式错误, 请使用 <过滤器类型>:<表达式>"
error_filter_type_unsupported: "不支持的过滤器类型, 请参阅文档"
error_watch_chat_failed: "监听聊天失败: {{.Error}}"
info_watch_chat_started: "已开始监听聊天: {{.Chat}}"
info_already_watching_chat: "已经在监听此聊天"
- info_watch_list_empty: "当前没有监听任何聊天"
+ info_watch_list_empty: "当前未监听任何聊天"
info_watch_list_header: "当前监听的聊天:\n"
info_watch_list_filter_prefix: " (过滤器: "
- error_unwatch_no_chat_provided: "请提供要取消监听的聊天ID或用户名"
error_unwatch_chat_failed: "取消监听聊天失败: {{.Error}}"
info_watch_chat_stopped: "已取消监听聊天: {{.Chat}}"
- error_notify_arg_invalid: "无效的参数, 用法: /watch notify on|off"
- info_notify_enabled: "已为你监听的所有聊天开启任务通知: 监听到的消息及其保存结果会在此处反馈.\n\n用法: /watch notify on|off"
- info_notify_disabled: "已为你监听的所有聊天关闭任务通知.\n\n用法: /watch notify on|off"
- notify_detected: "🔔 监听到新消息 \n{{.Name}}\n进度将在此处更新"
+ info_notify_enabled: "已开启任务通知,监听到的消息及保存结果将在此反馈。"
+ info_notify_disabled: "已关闭任务通知。"
+ notify_detected: "🔔 监听到新消息 \n{{.Name}}"
tasks:
- usage_cancel: "用法: /tasks cancel "
- usage: "用法: /tasks [running|queued|cancel ]"
+ menu_title: "任务管理"
+ menu_hint: "查看运行中、排队中和失败任务。"
+ menu_running: "运行中"
+ menu_queued: "排队中"
+ menu_failed: "失败任务"
+ menu_back: "返回"
+ failed_title: "失败任务"
+ failed_empty: "暂无失败任务"
+ retry_button: "重试"
+ cancel_button: "取消"
cancel_failed: "取消任务失败: {{.Error}}"
cancel_requested_prefix: "已请求取消任务: "
- running_empty: "当前没有正在运行的任务"
+ running_empty: "暂无运行中的任务"
running_title: "当前正在运行的任务:"
total_prefix: "总数: {{.Count}}\n"
field_id: "ID: "
@@ -177,7 +163,7 @@ bot:
status_running: "运行中"
status_queued: "排队中"
status_cancel_requested: "已请求取消"
- queued_empty: "当前没有排队中的任务"
+ queued_empty: "暂无排队中的任务"
queued_title: "当前排队中的任务:"
truncated_note: "...\n只显示前 10 个任务, 共 {{.Count}} 个任务"
info_added_to_queue_full: "已添加到任务队列\n文件名: {{.Filename}}\n当前排队任务数: {{.QueueLength}}"
@@ -185,6 +171,12 @@ bot:
info_filename_prefix: "文件名: "
info_queue_length_prefix: "\n当前排队任务数: "
rule:
+ menu_title: "存储规则"
+ menu_hint: "使用 /rules add <类型> <内容> <存储> <路径> 添加规则。"
+ menu_empty: "尚未配置规则"
+ button_enable: "启用规则"
+ button_disable: "停用规则"
+ button_delete: "删除"
error_get_user_rules_failed: "获取用户规则失败"
error_update_user_failed: "更新用户失败"
info_rule_mode_enabled: "已启用规则模式"
@@ -196,19 +188,23 @@ bot:
error_invalid_rule_id: "无效的规则ID"
error_delete_rule_failed: "删除规则失败"
info_delete_rule_success: "删除规则成功"
- help_usage: "使用方法: /rule <操作> <参数...>"
+ help_usage: "使用方法: /rules <操作> <参数...>"
help_current_mode_enabled: "\n当前已启用规则模式"
help_current_mode_disabled: "\n当前已禁用规则模式"
help_available_ops: "\n\n可用操作:\n"
help_switch_suffix: " - 开关规则模式\n"
- help_add_suffix: " <类型> <数据> <存储名> <路径> - 添加规则\n"
+ help_add_suffix: " <类型> <数据> <存储ID|名称> <路径> - 添加规则\n"
help_del_suffix: " <规则ID> - 删除规则\n"
- help_preset_suffix: " <存储名> [基础路径] - 导入内置文件类型分类规则(视频/图片/音频/文档/压缩包)\n"
+ help_preset_suffix: " <存储ID|名称> [基础路径] - 导入内置文件类型分类规则(视频/图片/音频/文档/压缩包)\n"
help_existing_rules_prefix: "\n当前已添加的规则:\n"
prompt_provide_storage_name: "请提供存储名称"
error_storage_not_found: "未找到存储: {{.Storage}}"
info_preset_imported: "已导入 {{.Count}} 条内置分类规则到存储 {{.Storage}}"
dir:
+ menu_title: "存储目录"
+ menu_hint: "使用 /dirs add <存储> <路径> 添加目录。"
+ menu_empty: "尚未配置目录"
+ button_delete: "删除"
error_get_user_dirs_failed: "获取用户文件夹失败"
error_get_user_failed: "获取用户失败"
error_create_dir_failed: "创建文件夹失败"
@@ -217,14 +213,14 @@ bot:
error_delete_dir_failed: "删除文件夹失败"
info_delete_dir_success: "文件夹删除成功"
error_unknown_operation: "未知操作"
- help_usage: "使用方法: /dir <操作> <参数...>"
+ help_usage: "使用方法: /dirs <操作> <参数...>"
help_available_ops: "\n\n可用操作:\n"
help_add_suffix: " <存储名> <路径> - 添加路径\n"
help_del_suffix: " <路径ID> - 删除路径\n"
help_add_example_prefix: "\n添加路径示例:\n"
- help_add_example_cmd: "/dir add local1 path/to/dir"
+ help_add_example_cmd: "/dirs add local1 path/to/dir"
help_del_example_prefix: "\n\n删除路径示例:\n"
- help_del_example_cmd: "/dir del 3"
+ help_del_example_cmd: "/dirs del 3"
help_existing_dirs_prefix: "\n\n当前已添加的路径:\n"
button_default: "默认"
parser:
@@ -261,14 +257,14 @@ bot:
update:
error_version_var_invalid: "当前处于开发版本或版本信息注入失败: {{.Error}}"
error_check_latest_failed: "检测最新版本失败: {{.Error}}"
- error_no_release_found: "没有找到版本信息"
- info_major_upgrade_required: "检测到大版本更新: {{.Current}} -> {{.Latest}} , 请前往 GitHub 手动下载最新版本并查看迁移指南"
- info_already_latest: "当前已经是最新版本: {{.Version}}"
+ error_no_release_found: "未找到版本信息"
+ info_major_upgrade_required: "检测到大版本更新: {{.Current}} -> {{.Latest}},请前往 GitHub 查看迁移指南并手动升级"
+ info_already_latest: "当前已是最新版本: {{.Version}}"
info_new_version_in_docker: |-
发现新版本: {{.Latest}}
当前版本: {{.Current}}
发布时间: {{.PublishedAt}}
- 由于您正在使用 Docker 部署, 请自行在部署平台上执行更新命令
+ 当前为 Docker 部署,请拉取新镜像完成更新
info_new_version_prompt_upgrade: |-
发现新版本: {{.Latest}}
当前版本: {{.Current}}
@@ -277,13 +273,19 @@ bot:
下载链接: {{.URL}}
发布时间: {{.PublishedAt}}
- 升级将重启 Bot , 是否升级?
+ 升级将重启 Bot,是否继续?
info_upgrading_with_version: "正在升级中, 当前版本: {{.Current}}"
error_upgrade_failed: "升级失败: {{.Error}}"
- info_upgrade_success: "已升级至版本 {{.Version}}\n若 Bot 未自动重启请手动启动"
+ info_upgrade_success: "已升级至版本 {{.Version}}\n若未自动重启,请手动启动"
button_upgrade: "升级"
config:
+ menu_title: "用户设置"
+ menu_hint: "选择要修改的设置。"
prompt_select_option: "请选择要配置的选项"
+ button_silent: "静默模式"
+ button_default_storage: "默认存储"
+ button_template: "文件名模板"
+ button_back: "返回"
button_filename_strategy: "文件名策略"
button_conflict_strategy: "重名文件保存策略"
error_invalid_callback_data: "无效的回调数据"
@@ -296,9 +298,9 @@ bot:
conflict_strategy_ask: "每次询问"
conflict_strategy_overwrite: "始终覆盖"
conflict_strategy_skip: "始终跳过"
- fnametmpl_help: |-
+ template_help: |-
使用该命令设置文件名模板, 示例:
- /fnametmpl 图片_{{"{{.msgid}}"}}_{{"{{.msgdate}}"}}.jpg
+ /template 图片_{{"{{.msgid}}"}}_{{"{{.msgdate}}"}}.jpg
可用变量:
- {{"{{.msgid}}"}}: 消息ID
@@ -309,46 +311,52 @@ bot:
- {{"{{.origname}}"}}: 媒体的原始文件名 (如果有)
- {{"{{.chatid}}"}}: 消息的聊天ID
- 模板仅在文件名策略设置为 '自定义模板' 时生效,
- 且模板解析错误时会回退到默认文件名
+ 模板仅在文件名策略为“自定义模板”时生效;
+ 解析错误时将回退到默认文件名
info_template_updated: "已更新文件名模板"
info_current_template_prefix: "当前模板: {{.Template}}"
- dl:
- usage: "用法: /dl <链接1> <链接2> ..."
- error_no_valid_links: "没有有效的链接可供下载"
- info_files_select_storage: "共 {{.Count}} 个文件, 请选择存储位置"
- ytdlp:
- usage: "用法: /ytdlp [选项] [URL2] ...\n示例:\n /ytdlp https://example.com/video\n /ytdlp --format best https://example.com/video\n /ytdlp --extract-audio --audio-format mp3 https://example.com/video"
- error_no_valid_urls: "没有有效的 URL"
- info_urls_select_storage: "共 {{.Count}} 个链接, 请选择存储位置"
- info_downloading: "正在通过 yt-dlp 下载..."
- error_download_failed: "yt-dlp 下载失败: {{.Error}}"
+ download:
+ usage: |
+ 用法: /download <链接...> [yt-dlp 选项]
+ 可选择直接下载、Aria2 或视频解析。
+ 示例:
+ /download https://example.com/file.zip
+ /download https://www.youtube.com/watch?v=...
+ /download --format best https://www.youtube.com/watch?v=...
+ prompt_select_engine: "找到 {{.Count}} 个链接,请选择下载方式"
+ button_direct: "直接下载"
+ button_aria2: "Aria2"
+ button_video: "视频(yt-dlp)"
+ info_select_storage: "请为 {{.Count}} 个链接选择存储位置"
+ error_no_valid_links: "未找到有效的下载链接"
+ error_no_engine: "无可用的下载方式"
transfer:
usage: |
用法: /transfer :/ [filter]
示例:
/transfer local1:/downloads
/transfer alist1:/media/photos
- /transfer webdav1:/files ".*\.mp4$"
+ /transfer webdav1:/videos ".*\.mp4$"
error_invalid_source: "源路径格式无效,应为: storage_name:/path"
error_invalid_target: "目标路径格式无效,应为: storage_name:/path"
- error_storage_not_found: "存储端 '{{.StorageName}}' 不存在或您无权访问: {{.Error}}"
- error_storage_not_listable: "存储端 '{{.StorageName}}' 不支持列举文件功能"
- error_storage_not_readable: "存储端 '{{.StorageName}}' 不支持读取文件功能"
- error_target_not_found: "目标存储端 '{{.StorageName}}' 不存在或您无权访问: {{.Error}}"
+ error_storage_not_found: "存储端“{{.StorageName}}”不存在或无权访问: {{.Error}}"
+ error_storage_not_listable: "存储端“{{.StorageName}}”不支持列举文件"
+ error_storage_not_readable: "存储端“{{.StorageName}}”不支持读取文件"
+ error_target_not_found: "目标存储端“{{.StorageName}}”不存在或无权访问: {{.Error}}"
info_fetching_files: "正在获取文件列表..."
error_list_files_failed: "获取文件列表失败: {{.Error}}"
error_invalid_regex: "正则表达式无效: {{.Error}}"
- error_no_files_to_transfer: "目录中没有可传输的文件"
+ error_no_files_to_transfer: "目录中无待传输文件"
error_add_task_failed: "添加任务失败: {{.Error}}"
info_task_added: "已添加 {{.Count}} 个文件到传输队列\n总大小: {{.SizeMB}} MB\n任务 ID: {{.TaskID}}"
start_stats: "总文件数: {{.Count}}\n总大小: {{.SizeMB}} MB"
info_files_select_storage: "共 {{.Count}} 个文件 (总大小: {{.SizeMB}} MB),请选择目标存储位置"
error_build_storage_select_keyboard_failed: "构建存储选择键盘失败: {{.Error}}"
+ retry:
+ info_started: "已创建重试任务: {{.TaskID}}({{.Count}} 个文件)"
+ error_failed: "重试任务失败: {{.Error}}"
cancel:
- usage: "用法: /cancel "
error_cancel_failed: "取消任务失败: {{.Error}}"
- info_cancel_requested: "已请求取消任务: {{.TaskID}}"
info_cancelling_task: "正在取消任务..."
media_group:
info_saving_files: "正在保存文件..."
@@ -365,13 +373,14 @@ bot:
batch_item_transferring_unknown: "↕️ {{.Index}}/{{.Total}} 传输中 \n{{.Name}}\n速度:{{.Speed}}\n大小:{{.Current}} / 未知 "
batch_item_uploading: "⬆️ {{.Index}}/{{.Total}} 上传中 \n{{.Name}}\n{{.Bar}} {{.Progress}}%\n速度:{{.Speed}}\n大小:{{.Current}} / {{.Size}} "
batch_item_retrying: "🔁 {{.Index}}/{{.Total}} 上传重试 \n{{.Name}}\n{{.Bar}} {{.Progress}}%\n重试次数:{{.Attempt}}/{{.Limit}}\n失败前速度:{{.Speed}}\n大小:{{.Current}} / {{.Size}}\n原因:{{.Reason}} "
- batch_item_confirming: "⏳ {{.Index}}/{{.Total}} 等待中 \n{{.Name}}\n{{.Bar}} {{.Progress}}%\n文件已上传,正在等待远端确认 "
+ batch_item_confirming: "⏳ {{.Index}}/{{.Total}} 等待中 \n{{.Name}}\n{{.Bar}} {{.Progress}}%\n已上传,等待远端确认 "
batch_summary_hidden_active: "🔄 另有 {{.Count}} 个文件正在处理"
batch_summary_confirming: "☁️ 已上传,等待整组发送:{{.Count}}"
batch_summary_failed: "❌ 失败:{{.Count}}"
batch_summary_skipped: "⏭️ 已跳过:{{.Count}}"
batch_done: "✅ 处理完成 \n\n文件数: {{.Count}}\n总大小: {{.Size}}"
batch_done_with_skipped: "⚠️ 处理完成 \n\n成功: {{.Success}}\n已跳过: {{.Skipped}}\n总大小: {{.Size}}"
+ batch_done_with_failed: "⚠️ 处理完成(部分失败) \n\n成功: {{.Success}}\n失败: {{.Failed}}\n已跳过: {{.Skipped}}\n总大小: {{.Size}}{{.List}}"
batch_canceled: "🚫 任务已取消 \n\n文件数: {{.Total}}\n已完成: {{.Completed}}\n未完成: {{.Incomplete}}\n已跳过: {{.Skipped}}"
batch_failed_item: "❌ 处理失败 \n\n失败文件: {{.Index}}. {{.Name}}\n失败阶段: {{.Stage}}\n失败进度: {{.Progress}}\n失败前速度: {{.Speed}}\n原因: {{.Reason}}\n\n✅ 已完成: {{.Completed}}\n❌ 失败: {{.Failed}}\n⏹️ 未完成: {{.Incomplete}}"
batch_failed_group: "❌ 批量上传失败 \n\n受影响文件: {{.Affected}} 个\n原因: {{.Reason}}\n\n✅ 已完成: {{.Completed}}\n❌ 批次失败: {{.Failed}}\n⏹️ 未完成: {{.Incomplete}}"
@@ -441,4 +450,4 @@ bot:
info_adding_aria2_download: "正在添加 Aria2 下载任务..."
error_adding_aria2_download: "添加 Aria2 下载任务失败: {{.Error}}"
info_aria2_download_added: "Aria2 下载任务已添加, GID: {{.GID}}"
- info_select_storage: "请选择存储位置, 选择后将添加到 Aria2 下载队列"
+ info_select_storage: "请选择存储位置,确认后将加入 Aria2 下载队列"
diff --git a/common/mediafilter/mediafilter.go b/common/mediafilter/mediafilter.go
new file mode 100644
index 00000000..5d6147c2
--- /dev/null
+++ b/common/mediafilter/mediafilter.go
@@ -0,0 +1,36 @@
+// Package mediafilter applies the instance-wide allowed media type setting.
+package mediafilter
+
+import (
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/mediatype"
+)
+
+// Allowed reports whether media should be processed under the current
+// allowlist. An empty allowlist permits every supported media object.
+func Allowed(media tg.MessageMediaClass) bool {
+ allowed := config.C().Media.AllowedMediaTypes
+ if len(allowed) == 0 {
+ return true
+ }
+ typ, ok := mediatype.Classify(media)
+ if !ok {
+ return false
+ }
+ return Allows(typ)
+}
+
+// Allows reports whether a known media category is enabled.
+func Allows(typ mediatype.Type) bool {
+ allowed := config.C().Media.AllowedMediaTypes
+ if len(allowed) == 0 {
+ return true
+ }
+ for _, value := range allowed {
+ if value == string(typ) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/common/selfupdate/selfupdate.go b/common/selfupdate/selfupdate.go
index 112f4c4c..63304d28 100644
--- a/common/selfupdate/selfupdate.go
+++ b/common/selfupdate/selfupdate.go
@@ -12,16 +12,25 @@ import (
"sync/atomic"
"github.com/blang/semver"
+ "github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/config"
"github.com/unvgo/ghselfupdate"
)
var (
- ErrDevBuild = errors.New("not a release build")
- ErrDocker = errors.New("built into a Docker image")
- ErrNotWritable = errors.New("the binary's folder is not writable")
+ ErrDevBuild = errors.New("not a release build")
+ ErrDocker = errors.New("built into a Docker image")
+ ErrNotWritable = errors.New("the binary's folder is not writable")
+ ErrRestart = errors.New(RestartSignal)
+ ErrNoRelease = errors.New("no release found")
+ ErrMajorUpgrade = errors.New("major upgrade requires a manual download and migration review")
+ ErrAlreadyLatest = errors.New("already on the latest version")
)
+// RestartSignal marks a handler error that asks the client to restart the
+// process after an update.
+const RestartSignal = "saveanybot-restart"
+
var (
restarting atomic.Bool
@@ -74,15 +83,39 @@ func Updatable() error {
return nil
}
+// CheckTarget validates that latest can be installed by this build.
+func CheckTarget(current, latest semver.Version) error {
+ if latest.Major > current.Major {
+ return fmt.Errorf("%w: %s -> %s", ErrMajorUpgrade, current, latest)
+ }
+ if !latest.GT(current) {
+ return fmt.Errorf("%w: %s", ErrAlreadyLatest, current)
+ }
+ return nil
+}
+
func Update(current semver.Version) (*ghselfupdate.Release, error) {
if err := Updatable(); err != nil {
return nil, err
}
- rel, err := ghselfupdate.UpdateSelf(current, config.GitRepo)
+ latest, found, err := ghselfupdate.DetectLatest(config.GitRepo)
if err != nil {
+ return nil, fmt.Errorf("failed to detect latest release: %w", err)
+ }
+ if !found {
+ return nil, ErrNoRelease
+ }
+ if err := CheckTarget(current, latest.Version); err != nil {
+ return nil, err
+ }
+ exe, err := SelfPath()
+ if err != nil {
+ return nil, err
+ }
+ if err := ghselfupdate.DefaultUpdater().UpdateTo(latest, exe); err != nil {
return nil, fmt.Errorf("failed to update: %w", err)
}
- return rel, nil
+ return latest, nil
}
func Restart() error {
@@ -96,6 +129,7 @@ func Restart() error {
func CleanLeftovers() {
exe, err := SelfPath()
if err != nil {
+ log.Warnf("Failed to resolve executable path for cleanup: %v", err)
return
}
cleanLeftovers(exe)
@@ -103,10 +137,18 @@ func CleanLeftovers() {
func cleanLeftovers(exe string) {
dir, name := filepath.Split(exe)
- _ = os.Remove(filepath.Join(dir, "."+name+".old"))
+ if err := os.Remove(filepath.Join(dir, "."+name+".old")); err != nil && !errors.Is(err, os.ErrNotExist) {
+ log.Warnf("Failed to remove stale update file: %v", err)
+ }
staged := filepath.Join(dir, "."+name+".new")
- if same, err := sameContent(exe, staged); err == nil && same {
- _ = os.Remove(staged)
+ if same, err := sameContent(exe, staged); err != nil {
+ if !errors.Is(err, os.ErrNotExist) {
+ log.Warnf("Failed to compare staged update: %v", err)
+ }
+ } else if same {
+ if err := os.Remove(staged); err != nil {
+ log.Warnf("Failed to remove staged update: %v", err)
+ }
}
}
@@ -115,8 +157,14 @@ func writable(dir string) bool {
if err != nil {
return false
}
- f.Close()
- os.Remove(f.Name())
+ if err := f.Close(); err != nil {
+ log.Warnf("Failed to close update probe: %v", err)
+ return false
+ }
+ if err := os.Remove(f.Name()); err != nil {
+ log.Warnf("Failed to remove update probe: %v", err)
+ return false
+ }
return true
}
diff --git a/common/tdler/dler.go b/common/tdler/dler.go
index f965bcee..f73b7056 100644
--- a/common/tdler/dler.go
+++ b/common/tdler/dler.go
@@ -5,16 +5,41 @@ import (
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/relay"
"github.com/krau/SaveAny-Bot/common/utils/dlutil"
+ "github.com/krau/SaveAny-Bot/common/utils/rpclimit"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/pkg/consts/tglimit"
"github.com/krau/SaveAny-Bot/pkg/tfile"
)
-func NewDownloader(file tfile.TGFile) *downloader.Builder {
+func NewDownloader(ctx context.Context, file tfile.TGFile) (*downloader.Builder, func(context.Context)) {
+ file, cleanup := relay.Prepare(ctx, file)
+ return newDownloader(file), cleanup
+}
+
+func newDownloader(file tfile.TGFile) *downloader.Builder {
return downloader.NewDownloader().WithPartSize(tglimit.MaxPartSize).
- Download(eofAwareClient{Client: file.Dler(), size: file.Size()}, file.Location()).
- WithThreads(dlutil.BestThreads(file.Size(), config.C().Threads))
+ Download(eofAwareClient{Client: rpclimit.WrapDownloader(file.Dler()), size: file.Size()}, file.Location()).
+ WithThreads(downloadThreads(file))
+}
+
+func downloadThreads(file tfile.TGFile) int {
+ cfg := config.C()
+ if cfg.DownloadThreads > 0 {
+ return cfg.DownloadThreads
+ }
+ threads := dlutil.BestThreads(file.Size(), cfg.Threads)
+ if scalable, ok := file.(interface{ WorkerCount() int }); ok {
+ // A relay file distributes chunks across helpers. Give each helper the
+ // same per-worker concurrency; the global transfer budget still caps the
+ // real number of in-flight RPCs in rpclimit.
+ threads *= max(scalable.WorkerCount(), 1)
+ }
+ if cfg.MaxDownloadThreads > 0 {
+ threads = min(threads, cfg.MaxDownloadThreads)
+ }
+ return max(threads, 1)
}
// eofAwareClient answers upload.getFile requests at or past the end of the
diff --git a/common/tdler/download_cache.go b/common/tdler/download_cache.go
new file mode 100644
index 00000000..dc17debc
--- /dev/null
+++ b/common/tdler/download_cache.go
@@ -0,0 +1,125 @@
+package tdler
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+
+ "github.com/charmbracelet/log"
+
+ "github.com/krau/SaveAny-Bot/client/relay"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+)
+
+// DownloadToCache downloads file into cachePath, reporting download progress
+// through wrap. A complete cache file is reused as-is (e.g. when the previous
+// run was interrupted during upload). Downloads with a known size resume from
+// a partial .part file tracked by a resume bitmap, which is removed once the
+// download is finalized. Files with an unknown size (e.g. photos) are fetched
+// with the plain downloader.
+func DownloadToCache(
+ ctx context.Context,
+ file tfile.TGFile,
+ cachePath string,
+ wrap func(io.WriterAt) io.WriterAt,
+) error {
+ if cachePath == "" {
+ return fmt.Errorf("empty cache path")
+ }
+ logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", file.Name()))
+ if file.Size() > 0 {
+ if stat, err := os.Stat(cachePath); err == nil && stat.Size() == file.Size() {
+ logger.Info("Cache file already complete, skipping download")
+ if err := os.Remove(cachePath + ".part"); err != nil && !errors.Is(err, os.ErrNotExist) {
+ logger.Warnf("Failed to remove stale part file: %v", err)
+ }
+ relay.CleanupResumeState(ctx, relay.ResumeStatePath(cachePath))
+ if err := RemoveResumeState(ResumeStatePath(cachePath + ".part")); err != nil {
+ logger.Warnf("Failed to remove stale resume state: %v", err)
+ }
+ return nil
+ }
+ }
+ if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
+ return fmt.Errorf("failed to create cache directory: %w", err)
+ }
+ var cleanup func(context.Context)
+ if file.Size() > 0 {
+ file, cleanup = relay.PrepareResumable(ctx, file, relay.ResumeStatePath(cachePath))
+ } else {
+ file, cleanup = relay.Prepare(ctx, file)
+ }
+ if file.Size() <= 0 {
+ if cleanup != nil {
+ defer cleanup(ctx)
+ }
+ // Unknown size (e.g. photos) cannot be resumed; use the plain downloader.
+ localFile, err := os.Create(cachePath)
+ if err != nil {
+ return fmt.Errorf("failed to create cache file: %w", err)
+ }
+ defer func() {
+ if err := localFile.Close(); err != nil {
+ logger.Warnf("Failed to close cache file: %v", err)
+ }
+ }()
+ if _, err := newDownloader(file).Parallel(ctx, wrap(localFile)); err != nil {
+ return fmt.Errorf("download failed: %w", err)
+ }
+ return nil
+ }
+ partPath := cachePath + ".part"
+ // 不截断已存在的 .part: 位图标记的已完成块依赖既有字节。
+ partFile, err := os.OpenFile(partPath, os.O_CREATE|os.O_RDWR, 0o644)
+ if err != nil {
+ return fmt.Errorf("failed to open cache file: %w", err)
+ }
+ downloadErr := DownloadResumable(
+ ctx, file, wrap(partFile),
+ downloadThreads(file),
+ ResumeStatePath(partPath),
+ )
+ if downloadErr != nil {
+ // Keep the relay message and sidecar so a later run can reuse them.
+ return errors.Join(downloadErr, partFile.Close())
+ }
+ if cleanup != nil {
+ defer cleanup(ctx)
+ }
+ // 落盘后再关闭并 rename: 确保位图标记的字节确实持久, 掉电后不会
+ // 出现 "位图完整但数据页丢失" 的零字节洞。
+ if err := partFile.Sync(); err != nil {
+ return errors.Join(fmt.Errorf("failed to sync cache file: %w", err), partFile.Close())
+ }
+ closeErr := partFile.Close()
+ if closeErr != nil {
+ return fmt.Errorf("failed to close cache file: %w", closeErr)
+ }
+ stat, err := os.Stat(partPath)
+ if err != nil {
+ return fmt.Errorf("failed to stat downloaded file: %w", err)
+ }
+ if stat.Size() > file.Size() {
+ if err := os.Truncate(partPath, file.Size()); err != nil {
+ return fmt.Errorf("truncate downloaded file: %w", err)
+ }
+ stat, err = os.Stat(partPath)
+ if err != nil {
+ return fmt.Errorf("failed to stat downloaded file: %w", err)
+ }
+ }
+ if stat.Size() != file.Size() {
+ return fmt.Errorf("downloaded size %d does not match expected %d", stat.Size(), file.Size())
+ }
+ if err := os.Rename(partPath, cachePath); err != nil {
+ return fmt.Errorf("failed to finalize download: %w", err)
+ }
+ // 清理位图是尽力而为: 下载已完成, 清理失败不应使任务失败。
+ if err := RemoveResumeState(ResumeStatePath(partPath)); err != nil {
+ logger.Warnf("Failed to remove resume state: %v", err)
+ }
+ return nil
+}
diff --git a/common/tdler/resume.go b/common/tdler/resume.go
new file mode 100644
index 00000000..9ff2e4a1
--- /dev/null
+++ b/common/tdler/resume.go
@@ -0,0 +1,454 @@
+package tdler
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "math/bits"
+ "net"
+ "os"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/downloader"
+ "github.com/gotd/td/tg"
+ "github.com/gotd/td/tgerr"
+ "golang.org/x/sync/errgroup"
+
+ "github.com/krau/SaveAny-Bot/common/utils/rpclimit"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/pkg/consts/tglimit"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+)
+
+const maxChunkRetries = 20
+
+// chunkRetryLimit returns the configured timeout retry limit for one chunk.
+func chunkRetryLimit() int {
+ limit := config.C().ChunkRetries
+ if limit < 1 {
+ return maxChunkRetries
+ }
+ return limit
+}
+
+// errCDNRedirect reports a chunk served from a CDN. Raw upload.getFile
+// requests cannot follow CDN redirects (they need gotd's re-hash dance), so
+// the download falls back to the plain downloader, which handles them.
+var errCDNRedirect = errors.New("CDN redirect")
+
+func partPathOf(bitmapPath string) string {
+ return strings.TrimSuffix(bitmapPath, ".bitmap")
+}
+
+// resumeBitmap records which partSize-aligned blocks of a download have been
+// durably written, so an interrupted download can continue after a restart.
+// The bitmap file is rewritten atomically after every completed block.
+type resumeBitmap struct {
+ PartSize int `json:"part_size"`
+ Size int64 `json:"size"`
+ Blocks []uint64 `json:"blocks"`
+ SourceKind string `json:"source_kind,omitempty"`
+ SourceID int64 `json:"source_id,omitempty"`
+ SourceAccessHash int64 `json:"source_access_hash,omitempty"`
+
+ mu sync.Mutex
+ dirty int
+ lastSave time.Time
+}
+
+func newResumeBitmap(size int64, identity tfile.FileIdentity) *resumeBitmap {
+ b := &resumeBitmap{
+ PartSize: tglimit.MaxPartSize,
+ Size: size,
+ SourceKind: identity.Kind,
+ SourceID: identity.ID,
+ SourceAccessHash: identity.AccessHash,
+ }
+ b.ensureBlocks()
+ return b
+}
+
+func (b *resumeBitmap) matches(identity tfile.FileIdentity) bool {
+ if b.SourceKind == "" && b.SourceID == 0 && b.SourceAccessHash == 0 {
+ // State written by an earlier version without identity metadata.
+ return true
+ }
+ return b.SourceKind == identity.Kind && b.SourceID == identity.ID && b.SourceAccessHash == identity.AccessHash
+}
+
+func (b *resumeBitmap) ensureBlocks() {
+ if need := (b.blockCount() + 63) / 64; len(b.Blocks) < need {
+ b.Blocks = make([]uint64, need)
+ }
+}
+
+func (b *resumeBitmap) blockCount() int {
+ return int((b.Size + int64(b.PartSize) - 1) / int64(b.PartSize))
+}
+
+func (b *resumeBitmap) isDone(block int) bool {
+ return b.Blocks[block/64]&(1< highest {
+ highest = block
+ }
+ }
+ if highest < 0 {
+ return 0
+ }
+ covered := int64(highest+1) * int64(b.PartSize)
+ return min(covered, b.Size)
+}
+
+// maxResumableSize bounds the bitmap allocation derived from a (possibly
+// corrupt) size field: 1 TiB needs ~16 KiB of bitmap words, far above any
+// real Telegram file.
+const maxResumableSize int64 = 1 << 40
+
+func loadResumeBitmap(path string) (*resumeBitmap, error) {
+ data, err := os.ReadFile(path)
+ if errors.Is(err, os.ErrNotExist) {
+ return nil, nil
+ }
+ if err != nil {
+ return nil, fmt.Errorf("read resume bitmap: %w", err)
+ }
+ var b resumeBitmap
+ if err := json.Unmarshal(data, &b); err != nil {
+ // 无法解析的位图 (外部损坏): 删除并视为不存在, 全量重下自愈。
+ if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
+ return nil, fmt.Errorf("remove invalid resume bitmap: %w", removeErr)
+ }
+ return nil, nil
+ }
+ // 位图只写 PartSize=MaxPartSize; 其他值或超大 size 都是损坏数据,
+ // 在按 size 分配内存前拒绝, 防止构造的位图触发巨量分配。
+ if b.PartSize != tglimit.MaxPartSize || b.Size > maxResumableSize {
+ if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
+ return nil, fmt.Errorf("remove incompatible resume bitmap: %w", removeErr)
+ }
+ return nil, nil
+ }
+ b.ensureBlocks()
+ return &b, nil
+}
+
+func (b *resumeBitmap) save(path string) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.saveLocked(path)
+}
+
+func (b *resumeBitmap) saveLocked(path string) error {
+ data, err := json.Marshal(b)
+ if err != nil {
+ return fmt.Errorf("marshal resume bitmap: %w", err)
+ }
+ tmp := path + ".tmp"
+ if err := os.WriteFile(tmp, data, 0o644); err != nil {
+ return fmt.Errorf("write resume bitmap: %w", err)
+ }
+ return os.Rename(tmp, path)
+}
+
+func (b *resumeBitmap) markAndSave(block int, path string) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ b.markDone(block)
+ b.dirty++
+ // Persist at most once per second or every 16 completed blocks. A crash
+ // between saves only re-downloads the unwritten tail; it cannot mark
+ // missing data as complete.
+ if b.dirty < 16 && !b.lastSave.IsZero() && time.Since(b.lastSave) < time.Second {
+ return nil
+ }
+ if err := b.saveLocked(path); err != nil {
+ return err
+ }
+ b.dirty = 0
+ b.lastSave = time.Now()
+ return nil
+}
+
+func (b *resumeBitmap) flush(path string) error {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ if b.dirty == 0 {
+ return nil
+ }
+ if err := b.saveLocked(path); err != nil {
+ return err
+ }
+ b.dirty = 0
+ b.lastSave = time.Now()
+ return nil
+}
+
+func isRetryableChunkError(ctx context.Context, err error) bool {
+ if err == nil || ctx.Err() != nil {
+ return false
+ }
+ if tgerr.Is(err, tg.ErrTimeout) ||
+ errors.Is(err, context.DeadlineExceeded) ||
+ errors.Is(err, context.Canceled) {
+ return true
+ }
+ var netErr net.Error
+ if errors.As(err, &netErr) && (netErr.Timeout() || netErr.Temporary()) {
+ return true
+ }
+ if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, net.ErrClosed) {
+ return true
+ }
+ // gotd's RPC engine closes in-flight requests when it reconnects. The
+ // wrapped engine error has no exported type, so match its stable text.
+ message := strings.ToLower(err.Error())
+ for _, fragment := range []string{
+ "engine forcibly closed",
+ "connection reset by peer",
+ "connection closed",
+ "broken pipe",
+ "retryuntilack",
+ "retry limit reached",
+ "use of closed network connection",
+ } {
+ if strings.Contains(message, fragment) {
+ return true
+ }
+ }
+ return false
+}
+
+func waitChunkRetry(ctx context.Context, attempt int) error {
+ delay := 100 * time.Millisecond
+ if attempt > 1 {
+ delay = min(100*time.Millisecond< limit {
+ return nil, fmt.Errorf("get chunk at %d: server returned %d bytes, limit %d", offset, len(r.Bytes), limit)
+ }
+ return r.Bytes, nil
+ case *tg.UploadFileCDNRedirect:
+ return nil, fmt.Errorf("%w (dc %d)", errCDNRedirect, r.DCID)
+ default:
+ return nil, fmt.Errorf("unexpected upload.getFile response %T", res)
+ }
+ }
+ if flood, ferr := tgerr.FloodWait(ctx, err); ferr != nil {
+ if flood {
+ // FloodWait already slept; retry.
+ continue
+ }
+ if isRetryableChunkError(ctx, ferr) {
+ retries++
+ if retries >= retryLimit {
+ return nil, fmt.Errorf("get chunk at %d: retry limit reached: %w", offset, ferr)
+ }
+ log.FromContext(ctx).Debugf(
+ "Retrying chunk at %d after transient error (%d/%d): %v",
+ offset, retries, retryLimit, ferr,
+ )
+ if werr := waitChunkRetry(ctx, retries); werr != nil {
+ return nil, fmt.Errorf("get chunk at %d: %w", offset, werr)
+ }
+ continue
+ }
+ return nil, fmt.Errorf("get chunk at %d: %w", offset, ferr)
+ }
+ }
+}
+
+// DownloadResumable downloads file to w in partSize chunks, skipping blocks
+// already recorded as complete in bitmapPath and persisting every completed
+// block so an interrupted download can resume. A missing or incompatible
+// bitmap starts a full download. Requires a known, non-zero file size.
+func DownloadResumable(
+ ctx context.Context,
+ file tfile.TGFile,
+ w io.WriterAt,
+ threads int,
+ bitmapPath string,
+) error {
+ if file.Size() <= 0 {
+ return fmt.Errorf("resumable download requires a known size")
+ }
+ if threads < 1 {
+ threads = 1
+ }
+ bm, err := loadResumeBitmap(bitmapPath)
+ if err != nil {
+ return err
+ }
+ if bm != nil && !bm.matches(tfile.Identity(file.Location())) {
+ if err := os.Remove(bitmapPath); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return fmt.Errorf("remove stale resume bitmap: %w", err)
+ }
+ bm = nil
+ }
+ // 位图描述的数据文件 (bitmapPath 去掉 .bitmap 后缀) 必须存在且非空,
+ // 且至少覆盖位图标记为完成的最大块范围: 若缺失、为空或更短,
+ // 已标记完成的块字节已丢失, 必须重置位图全量重下 (否则 WriteAt 会
+ // 在文件中留下零字节洞, 而最终尺寸校验恰好通过, 造成静默损坏)。
+ if bm != nil {
+ if stat, err := os.Stat(partPathOf(bitmapPath)); err != nil || stat.Size() < bm.coveredBytes() {
+ if err := os.Remove(bitmapPath); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return fmt.Errorf("reset stale resume bitmap: %w", err)
+ }
+ bm = nil
+ }
+ }
+ if bm == nil || bm.PartSize != tglimit.MaxPartSize || bm.Size != file.Size() {
+ // 位图缺失或不兼容: 截断旧的部分文件, 避免残留尾部字节导致最终大小校验失败。
+ if err := os.Truncate(partPathOf(bitmapPath), 0); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return fmt.Errorf("truncate stale part file: %w", err)
+ }
+ bm = newResumeBitmap(file.Size(), tfile.Identity(file.Location()))
+ if err := bm.save(bitmapPath); err != nil {
+ return err
+ }
+ }
+ missing := bm.missingBlocks()
+ if len(missing) == 0 {
+ return nil
+ }
+
+ client := rpclimit.WrapDownloader(file.Dler())
+ location := file.Location()
+ var writeMu sync.Mutex
+ eg, gctx := errgroup.WithContext(ctx)
+ eg.SetLimit(threads)
+ for _, block := range missing {
+ if err := gctx.Err(); err != nil {
+ break
+ }
+ eg.Go(func() error {
+ offset := int64(block) * int64(bm.PartSize)
+ data, err := fetchChunk(gctx, client, location, offset, bm.PartSize)
+ if err != nil {
+ return err
+ }
+ want := int64(bm.PartSize)
+ if offset+want > bm.Size {
+ want = bm.Size - offset
+ }
+ if int64(len(data)) != want {
+ return fmt.Errorf("chunk at offset %d has size %d, want %d", offset, len(data), want)
+ }
+ writeMu.Lock()
+ defer writeMu.Unlock()
+ n, err := w.WriteAt(data, offset)
+ if err != nil {
+ return fmt.Errorf("write chunk at offset %d: %w", offset, err)
+ }
+ if n != len(data) {
+ return fmt.Errorf("write chunk at offset %d: short write %d/%d", offset, n, len(data))
+ }
+ return bm.markAndSave(block, bitmapPath)
+ })
+ }
+ err = eg.Wait()
+ if flushErr := bm.flush(bitmapPath); flushErr != nil {
+ return flushErr
+ }
+ if err != nil {
+ if errors.Is(err, errCDNRedirect) {
+ // CDN 托管的文件无法用裸 upload.getFile 续传, 回退到 gotd 下载器
+ // (它内部处理 CDN 重定向), 全量重下本文件。
+ log.FromContext(ctx).Warn("File served from CDN, falling back to plain downloader")
+ if _, err := newDownloader(file).Parallel(ctx, w); err != nil {
+ return fmt.Errorf("plain download after CDN redirect: %w", err)
+ }
+ return nil
+ }
+ return err
+ }
+ if !bm.complete() {
+ return fmt.Errorf("download finished with missing blocks")
+ }
+ return nil
+}
+
+// RemoveResumeState deletes the bitmap file of a completed download.
+func RemoveResumeState(bitmapPath string) error {
+ if err := os.Remove(bitmapPath); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return fmt.Errorf("remove resume bitmap: %w", err)
+ }
+ if err := os.Remove(bitmapPath + ".tmp"); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return fmt.Errorf("remove resume bitmap temp: %w", err)
+ }
+ return nil
+}
+
+// ResumeStatePath returns the bitmap path for a download cache file.
+func ResumeStatePath(cachePath string) string {
+ return cachePath + ".bitmap"
+}
diff --git a/common/tdler/resume_identity_test.go b/common/tdler/resume_identity_test.go
new file mode 100644
index 00000000..3e7b8932
--- /dev/null
+++ b/common/tdler/resume_identity_test.go
@@ -0,0 +1,23 @@
+package tdler
+
+import (
+ "testing"
+
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+)
+
+func TestResumeBitmapFileIdentity(t *testing.T) {
+ a := tfile.FileIdentity{Kind: "document", ID: 1, AccessHash: 2}
+ b := tfile.FileIdentity{Kind: "document", ID: 3, AccessHash: 2}
+ bm := newResumeBitmap(2*1024*1024, a)
+ if !bm.matches(a) {
+ t.Fatal("bitmap did not match its source file")
+ }
+ if bm.matches(b) {
+ t.Fatal("bitmap matched a different source file")
+ }
+ legacy := &resumeBitmap{PartSize: 1024, Size: 1}
+ if !legacy.matches(b) {
+ t.Fatal("legacy bitmap without identity should be accepted")
+ }
+}
diff --git a/common/upload/upload.go b/common/upload/upload.go
new file mode 100644
index 00000000..95d61c53
--- /dev/null
+++ b/common/upload/upload.go
@@ -0,0 +1,115 @@
+// Package upload provides the shared local-file upload path used by the CLI
+// commands. It keeps retries, cancellation, and confirmed progress consistent
+// across one-shot and watch uploads.
+package upload
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/common/utils/ioutil"
+ "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+type Options struct {
+ Storage storage.Storage
+ Overwrite bool
+ Retries int
+ RetryDelay time.Duration
+ OnProgress func(uploaded, total int64)
+}
+
+func (o Options) validate() error {
+ if o.Storage == nil {
+ return errors.New("upload: storage is required")
+ }
+ if o.Retries < 1 {
+ return errors.New("upload: retries must be at least 1")
+ }
+ if o.RetryDelay < 0 {
+ return errors.New("upload: retry delay must not be negative")
+ }
+ return nil
+}
+
+// SaveFile uploads a local file, rewinding it for every retry.
+func SaveFile(ctx context.Context, localPath, storagePath string, opts Options) (retErr error) {
+ if err := opts.validate(); err != nil {
+ return err
+ }
+ file, err := os.Open(localPath)
+ if err != nil {
+ return fmt.Errorf("upload: open %s: %w", localPath, err)
+ }
+ defer func() {
+ retErr = errors.Join(retErr, file.Close())
+ }()
+
+ info, err := file.Stat()
+ if err != nil {
+ return fmt.Errorf("upload: stat %s: %w", localPath, err)
+ }
+ if info.IsDir() {
+ return fmt.Errorf("upload: %s is a directory", localPath)
+ }
+ return Save(ctx, file, info.Size(), storagePath, opts)
+}
+
+// Save uploads a seekable reader. The reader is rewound before each attempt so
+// a retry never sends a partially consumed stream.
+func Save(ctx context.Context, reader io.ReadSeeker, size int64, storagePath string, opts Options) error {
+ if err := opts.validate(); err != nil {
+ return err
+ }
+ uploadCtx := context.WithValue(ctx, ctxkey.ContentLength, size)
+ if opts.Overwrite {
+ uploadCtx = storage.WithOverwrite(uploadCtx)
+ }
+
+ var lastErr error
+ for attempt := 0; attempt < opts.Retries; attempt++ {
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+ if _, err := reader.Seek(0, io.SeekStart); err != nil {
+ return fmt.Errorf("upload: rewind: %w", err)
+ }
+ err := saveOnce(uploadCtx, reader, size, storagePath, opts)
+ if err == nil {
+ return nil
+ }
+ if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
+ return err
+ }
+ lastErr = err
+ if attempt+1 >= opts.Retries {
+ break
+ }
+ if opts.RetryDelay > 0 {
+ timer := time.NewTimer(opts.RetryDelay)
+ select {
+ case <-ctx.Done():
+ timer.Stop()
+ return ctx.Err()
+ case <-timer.C:
+ }
+ }
+ }
+ return fmt.Errorf("upload failed after %d attempt(s): %w", opts.Retries, lastErr)
+}
+
+func saveOnce(ctx context.Context, reader io.ReadSeeker, size int64, storagePath string, opts Options) error {
+ if progressSaver, ok := opts.Storage.(storage.StorageProgressSaver); ok {
+ return progressSaver.SaveWithProgress(ctx, reader, storagePath, opts.OnProgress)
+ }
+ if opts.OnProgress == nil {
+ return opts.Storage.Save(ctx, reader, storagePath)
+ }
+ progressReader := ioutil.NewProgressReader(reader, size, opts.OnProgress)
+ return opts.Storage.Save(ctx, progressReader, storagePath)
+}
diff --git a/common/upload/upload_test.go b/common/upload/upload_test.go
new file mode 100644
index 00000000..bc342973
--- /dev/null
+++ b/common/upload/upload_test.go
@@ -0,0 +1,55 @@
+package upload
+
+import (
+ "context"
+ "errors"
+ "io"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/config/storage"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+)
+
+func TestSaveRetriesAndRewinds(t *testing.T) {
+ stor := &retryStorage{failures: 1}
+ err := Save(context.Background(), strings.NewReader("payload"), 7, "file.txt", Options{
+ Storage: stor,
+ Retries: 2,
+ RetryDelay: time.Millisecond,
+ })
+ if err != nil {
+ t.Fatalf("Save: %v", err)
+ }
+ if stor.attempts != 2 {
+ t.Fatalf("attempts = %d, want 2", stor.attempts)
+ }
+ if string(stor.data) != "payload" {
+ t.Fatalf("uploaded data = %q, want payload", stor.data)
+ }
+}
+
+type retryStorage struct {
+ attempts int
+ failures int
+ data []byte
+}
+
+func (s *retryStorage) Init(context.Context, storage.StorageConfig) error { return nil }
+func (s *retryStorage) Type() storenum.StorageType { return storenum.Local }
+func (s *retryStorage) Name() string { return "test" }
+func (s *retryStorage) Exists(context.Context, string) (bool, error) { return false, nil }
+
+func (s *retryStorage) Save(_ context.Context, r io.Reader, _ string) error {
+ s.attempts++
+ if s.attempts <= s.failures {
+ return errors.New("transient failure")
+ }
+ data, err := io.ReadAll(r)
+ if err != nil {
+ return err
+ }
+ s.data = data
+ return nil
+}
diff --git a/common/utils/fsutil/fs.go b/common/utils/fsutil/fs.go
index 0295d081..c1b1a18c 100644
--- a/common/utils/fsutil/fs.go
+++ b/common/utils/fsutil/fs.go
@@ -3,6 +3,7 @@ package fsutil
import (
"errors"
"os"
+ "path"
"path/filepath"
"strings"
"unicode"
@@ -60,6 +61,18 @@ func CreateFile(fp string) (*File, error) {
return &File{File: file}, nil
}
+// SafeBaseName reduces a remote-provided name to one safe filename component.
+func SafeBaseName(name string) string {
+ name = strings.ReplaceAll(name, "\\", "/")
+ name = path.Base(name)
+ name = NormalizePathname(name)
+ switch name {
+ case "", ".", "..":
+ return "file"
+ }
+ return name
+}
+
func NormalizePathname(s string) string {
specials := `\/:*?"<>|` + "\n\r\t"
var builder strings.Builder
diff --git a/common/utils/fsutil/unique.go b/common/utils/fsutil/unique.go
index f8b98d7c..b817d9a6 100644
--- a/common/utils/fsutil/unique.go
+++ b/common/utils/fsutil/unique.go
@@ -9,19 +9,28 @@ import (
)
// UniquePath returns a non-taken path under basePath: the name itself, then
-// numbered variants, then a random suffix.
-func UniquePath(basePath, name string, exists func(candidate string) bool, maxAttempts int) string {
+// numbered variants, then a random suffix. The exists callback may return an
+// error; callers must not treat a failed check as "not taken".
+func UniquePath(basePath, name string, exists func(candidate string) (bool, error), maxAttempts int) (string, error) {
candidate := path.Join(basePath, name)
- if !exists(candidate) {
- return candidate
+ taken, err := exists(candidate)
+ if err != nil {
+ return "", err
+ }
+ if !taken {
+ return candidate, nil
}
ext := path.Ext(name)
stem := strings.TrimSuffix(name, ext)
for i := 1; i <= maxAttempts; i++ {
candidate = path.Join(basePath, fmt.Sprintf("%s_%d%s", stem, i, ext))
- if !exists(candidate) {
- return candidate
+ taken, err = exists(candidate)
+ if err != nil {
+ return "", err
+ }
+ if !taken {
+ return candidate, nil
}
}
- return path.Join(basePath, fmt.Sprintf("%s_%s%s", stem, xid.New().String(), ext))
+ return path.Join(basePath, fmt.Sprintf("%s_%s%s", stem, xid.New().String(), ext)), nil
}
diff --git a/common/utils/netutil/proxy.go b/common/utils/netutil/proxy.go
index 23cd95c7..b7eee094 100644
--- a/common/utils/netutil/proxy.go
+++ b/common/utils/netutil/proxy.go
@@ -14,65 +14,83 @@ import (
"golang.org/x/net/proxy"
)
-func NewProxyHTTPClient(proxyUrl string) (*http.Client, error) {
- if proxyUrl == "" {
- return http.DefaultClient, nil
- }
- transport, err := NewProxyTransport(proxyUrl)
+func NewProxyHTTPClient(proxyURL string) (*http.Client, error) {
+ transport, err := NewProxyTransport(proxyURL)
if err != nil {
return nil, err
}
- return &http.Client{
- Transport: transport,
- }, nil
+ return &http.Client{Transport: transport}, nil
}
-var (
- defaultProxyHttpClient *http.Client
- onceLoadDefaultProxyHttpClient sync.Once
-)
+var defaultParserClient = struct {
+ sync.Mutex
+ key string
+ client *http.Client
+}{}
+// DefaultParserHTTPClient returns a parser client for the current parser proxy
+// setting. The client is rebuilt when the setting changes so a settings reload
+// does not leave requests pinned to the old proxy.
func DefaultParserHTTPClient() *http.Client {
- onceLoadDefaultProxyHttpClient.Do(func() {
- client, err := NewProxyHTTPClient(config.C().Parser.Proxy)
- if err != nil {
- log.Warn("Failed to create default proxy HTTP client, using http.DefaultClient", "error", err)
- defaultProxyHttpClient = http.DefaultClient
- } else {
- defaultProxyHttpClient = client
+ key := config.C().Parser.Proxy
+ defaultParserClient.Lock()
+ defer defaultParserClient.Unlock()
+ if defaultParserClient.client != nil && defaultParserClient.key == key {
+ return defaultParserClient.client
+ }
+ client, err := NewProxyHTTPClient(key)
+ if err != nil {
+ log.Warn("Failed to create default proxy HTTP client, using direct client", "error", err)
+ fallback, fallbackErr := NewProxyHTTPClient("")
+ if fallbackErr != nil {
+ log.Errorf("Failed to create direct parser HTTP client: %v", fallbackErr)
+ fallback = http.DefaultClient
}
- })
- return defaultProxyHttpClient
+ client = fallback
+ }
+ if defaultParserClient.client != nil {
+ if transport, ok := defaultParserClient.client.Transport.(*http.Transport); ok {
+ transport.CloseIdleConnections()
+ }
+ }
+ defaultParserClient.client = client
+ defaultParserClient.key = key
+ return client
}
func NewProxyTransport(proxyStr string) (*http.Transport, error) {
- proxyURL, err := url.Parse(proxyStr)
- if err != nil {
- return nil, err
- }
transport := &http.Transport{
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
+ ResponseHeaderTimeout: 30 * time.Second,
+ }
+ if proxyStr == "" {
+ return transport, nil
+ }
+ proxyURL, err := url.Parse(proxyStr)
+ if err != nil {
+ return nil, err
}
switch proxyURL.Scheme {
case "http", "https":
transport.Proxy = http.ProxyURL(proxyURL)
-
case "socks5", "socks5h":
dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
if err != nil {
return nil, err
}
+ contextDialer, ok := dialer.(proxy.ContextDialer)
+ if !ok {
+ return nil, fmt.Errorf("proxy %s does not support context cancellation", proxyURL.Scheme)
+ }
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
- return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr)
+ return contextDialer.DialContext(ctx, network, addr)
}
-
default:
return nil, fmt.Errorf("unsupported proxy type: %s", proxyURL.Scheme)
}
-
return transport, nil
}
diff --git a/common/utils/netutil/safehttp.go b/common/utils/netutil/safehttp.go
new file mode 100644
index 00000000..fb055f94
--- /dev/null
+++ b/common/utils/netutil/safehttp.go
@@ -0,0 +1,60 @@
+package netutil
+
+import (
+ "context"
+ "fmt"
+ "net"
+ "net/http"
+ "time"
+)
+
+// PublicOnlyHTTPClient returns a client that refuses to connect to loopback,
+// private, link-local, or unspecified addresses. Use it for URLs taken from
+// untrusted content.
+func PublicOnlyHTTPClient(timeout time.Duration) *http.Client {
+ return &http.Client{
+ Timeout: timeout,
+ Transport: &http.Transport{
+ DialContext: publicDialContext,
+ ForceAttemptHTTP2: true,
+ MaxIdleConns: 10,
+ IdleConnTimeout: 30 * time.Second,
+ },
+ }
+}
+
+func publicDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
+ host, port, err := net.SplitHostPort(addr)
+ if err != nil {
+ return nil, err
+ }
+ addresses, err := net.DefaultResolver.LookupIPAddr(ctx, host)
+ if err != nil {
+ return nil, fmt.Errorf("netutil: resolve %s: %w", host, err)
+ }
+ dialer := &net.Dialer{Timeout: 10 * time.Second}
+ var lastErr error
+ for _, address := range addresses {
+ if !isPublicIP(address.IP) {
+ continue
+ }
+ conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(address.IP.String(), port))
+ if err == nil {
+ return conn, nil
+ }
+ lastErr = err
+ }
+ if lastErr != nil {
+ return nil, fmt.Errorf("netutil: no reachable public address for %s: %w", host, lastErr)
+ }
+ return nil, fmt.Errorf("netutil: %s resolves only to non-public addresses", host)
+}
+
+func isPublicIP(ip net.IP) bool {
+ return ip.IsGlobalUnicast() &&
+ !ip.IsPrivate() &&
+ !ip.IsLoopback() &&
+ !ip.IsLinkLocalUnicast() &&
+ !ip.IsLinkLocalMulticast() &&
+ !ip.IsUnspecified()
+}
diff --git a/common/utils/rpclimit/rpclimit.go b/common/utils/rpclimit/rpclimit.go
new file mode 100644
index 00000000..c744d0db
--- /dev/null
+++ b/common/utils/rpclimit/rpclimit.go
@@ -0,0 +1,126 @@
+// Package rpclimit bounds the number of concurrent Telegram file-part RPCs.
+//
+// Per-file threads alone do not bound concurrency: every task worker can run
+// several elements, and each element opens its own set of part requests, so
+// the real load is workers × elements × threads. Downloads and uploads share
+// one budget derived from the configuration, which is re-read on every
+// acquire so runtime setting changes apply without a restart.
+package rpclimit
+
+import (
+ "context"
+ "sync"
+
+ "github.com/gotd/td/telegram/downloader"
+ "github.com/gotd/td/telegram/uploader"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/config"
+)
+
+var transfer = &limiter{notify: make(chan struct{}, 1)}
+
+// Acquire reserves one transfer slot. It blocks until a slot is free or ctx is
+// done. Every successful Acquire must be paired with Release.
+func Acquire(ctx context.Context) error {
+ return transfer.acquire(ctx, config.TransferBudget())
+}
+
+// Release returns a previously acquired slot.
+func Release() {
+ transfer.release()
+}
+
+// WrapDownloader limits UploadGetFile calls made through the returned client.
+func WrapDownloader(client downloader.Client) downloader.Client {
+ if client == nil {
+ return nil
+ }
+ return limitedDownloader{Client: client}
+}
+
+// WrapUploader limits save-part calls made through the returned uploader
+// client.
+func WrapUploader(client uploader.Client) uploader.Client {
+ if client == nil {
+ return nil
+ }
+ return limitedUploader{Client: client}
+}
+
+type limitedDownloader struct {
+ downloader.Client
+}
+
+func (c limitedDownloader) UploadGetFile(ctx context.Context, req *tg.UploadGetFileRequest) (tg.UploadFileClass, error) {
+ if err := Acquire(ctx); err != nil {
+ return nil, err
+ }
+ defer Release()
+ return c.Client.UploadGetFile(ctx, req)
+}
+
+type limitedUploader struct {
+ uploader.Client
+}
+
+func (c limitedUploader) UploadSaveFilePart(ctx context.Context, req *tg.UploadSaveFilePartRequest) (bool, error) {
+ if err := Acquire(ctx); err != nil {
+ return false, err
+ }
+ defer Release()
+ return c.Client.UploadSaveFilePart(ctx, req)
+}
+
+func (c limitedUploader) UploadSaveBigFilePart(ctx context.Context, req *tg.UploadSaveBigFilePartRequest) (bool, error) {
+ if err := Acquire(ctx); err != nil {
+ return false, err
+ }
+ defer Release()
+ return c.Client.UploadSaveBigFilePart(ctx, req)
+}
+
+// limiter is a context-aware counting semaphore with a dynamic limit.
+type limiter struct {
+ mu sync.Mutex
+ inFlight int
+ notify chan struct{}
+}
+
+func (l *limiter) acquire(ctx context.Context, limit int) error {
+ limit = max(limit, 1)
+ for {
+ l.mu.Lock()
+ if l.inFlight < limit {
+ l.inFlight++
+ // Hand one wake-up to another waiter when capacity remains.
+ if l.inFlight < limit {
+ l.signalLocked()
+ }
+ l.mu.Unlock()
+ return nil
+ }
+ l.mu.Unlock()
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-l.notify:
+ }
+ }
+}
+
+func (l *limiter) release() {
+ l.mu.Lock()
+ if l.inFlight > 0 {
+ l.inFlight--
+ }
+ l.signalLocked()
+ l.mu.Unlock()
+}
+
+// signalLocked wakes at most one waiter.
+func (l *limiter) signalLocked() {
+ select {
+ case l.notify <- struct{}{}:
+ default:
+ }
+}
diff --git a/common/utils/strutil/string_test.go b/common/utils/strutil/string_test.go
index 99c096a3..a751c6fe 100644
--- a/common/utils/strutil/string_test.go
+++ b/common/utils/strutil/string_test.go
@@ -107,13 +107,13 @@ func TestParseArgsRespectQuotes(t *testing.T) {
}{
{
name: "simple split",
- input: `/rule add FILENAME-REGEX (?i)\.(mp4|mkv)$ "我的 Alist" /视频`,
- want: []string{"/rule", "add", "FILENAME-REGEX", "(?i)\\.(mp4|mkv)$", "我的 Alist", "/视频"},
+ input: `/rules add FILENAME-REGEX (?i)\.(mp4|mkv)$ "我的 Alist" /视频`,
+ want: []string{"/rules", "add", "FILENAME-REGEX", "(?i)\\.(mp4|mkv)$", "我的 Alist", "/视频"},
},
{
name: "escaped quotes",
- input: `/rule add "My \"Awesome\" Folder"`,
- want: []string{"/rule", "add", `My "Awesome" Folder`},
+ input: `/rules add "My \"Awesome\" Folder"`,
+ want: []string{"/rules", "add", `My "Awesome" Folder`},
},
{
name: "escaped backslash",
diff --git a/common/utils/tgutil/chatinfo.go b/common/utils/tgutil/chatinfo.go
new file mode 100644
index 00000000..cd0eed7f
--- /dev/null
+++ b/common/utils/tgutil/chatinfo.go
@@ -0,0 +1,140 @@
+package tgutil
+
+import (
+ "sync"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/constant"
+ "github.com/gotd/td/tg"
+ "github.com/krau/mygotg/ext"
+)
+
+// ChatInfo is the normalized display identity of a Telegram peer.
+type ChatInfo struct {
+ ID int64 `json:"id"`
+ Title string `json:"title,omitempty"`
+ Username string `json:"username,omitempty"`
+}
+
+var chatInfoCache sync.Map // map[int64]ChatInfo
+
+// ChatInfoFromExt resolves a peer's title and username from update entities,
+// then from the peer cache/API. Empty fields are left empty.
+func ChatInfoFromExt(ctx *ext.Context, peer tg.PeerClass) ChatInfo {
+ id := ChatIdFromPeer(peer)
+ if id == 0 {
+ return ChatInfo{}
+ }
+ if cached, ok := chatInfoCache.Load(id); ok {
+ return cached.(ChatInfo)
+ }
+ info := ChatInfo{ID: id}
+ if ctx != nil {
+ plain := constant.TDLibPeerID(id).ToPlain()
+ if ctx.Entities != nil {
+ switch {
+ case ctx.Entities.Channels[plain] != nil:
+ channel := ctx.Entities.Channels[plain]
+ info.Title, info.Username = channel.Title, channel.Username
+ case ctx.Entities.Chats[plain] != nil:
+ info.Title = ctx.Entities.Chats[plain].Title
+ case ctx.Entities.Users[plain] != nil:
+ user := ctx.Entities.Users[plain]
+ info.Title, info.Username = displayName(user), user.Username
+ }
+ }
+ if info.Title == "" && ctx.Raw != nil {
+ info.Title, info.Username = fetchChatInfo(ctx, peer, plain)
+ }
+ }
+ if info.Title != "" || info.Username != "" {
+ chatInfoCache.Store(id, info)
+ }
+ return info
+}
+
+func displayName(user *tg.User) string {
+ if user == nil {
+ return ""
+ }
+ if user.LastName != "" {
+ return user.FirstName + " " + user.LastName
+ }
+ return user.FirstName
+}
+
+func fetchChatInfo(ctx *ext.Context, peer tg.PeerClass, plainID int64) (string, string) {
+ switch peer.(type) {
+ case *tg.PeerChannel:
+ return fetchChannelInfo(ctx, plainID)
+ case *tg.PeerChat:
+ return fetchGroupInfo(ctx, plainID)
+ case *tg.PeerUser:
+ return fetchUserInfo(ctx, plainID)
+ }
+ return "", ""
+}
+
+func fetchChannelInfo(ctx *ext.Context, channelID int64) (string, string) {
+ peer, err := ctx.ResolveInputPeerById(MarkedChannelID(channelID))
+ if err != nil {
+ log.Debugf("Failed to resolve channel %d: %v", channelID, err)
+ return "", ""
+ }
+ channelPeer, ok := peer.(*tg.InputPeerChannel)
+ if !ok {
+ return "", ""
+ }
+ result, err := ctx.Raw.ChannelsGetChannels(ctx, []tg.InputChannelClass{
+ &tg.InputChannel{ChannelID: channelID, AccessHash: channelPeer.AccessHash},
+ })
+ if err != nil {
+ log.Debugf("Failed to fetch channel %d: %v", channelID, err)
+ return "", ""
+ }
+ for _, item := range result.GetChats() {
+ if channel, ok := item.(*tg.Channel); ok && channel.ID == channelID {
+ return channel.Title, channel.Username
+ }
+ }
+ return "", ""
+}
+
+func fetchGroupInfo(ctx *ext.Context, chatID int64) (string, string) {
+ result, err := ctx.Raw.MessagesGetFullChat(ctx, chatID)
+ if err != nil {
+ log.Debugf("Failed to fetch chat %d: %v", chatID, err)
+ return "", ""
+ }
+ for _, item := range result.GetChats() {
+ if chat, ok := item.(*tg.Chat); ok && chat.ID == chatID {
+ return chat.Title, ""
+ }
+ }
+ return "", ""
+}
+
+func fetchUserInfo(ctx *ext.Context, userID int64) (string, string) {
+ peer, err := ctx.ResolveInputPeerById(userID)
+ if err != nil {
+ log.Debugf("Failed to resolve user %d: %v", userID, err)
+ return "", ""
+ }
+ userPeer, ok := peer.(*tg.InputPeerUser)
+ if !ok {
+ return "", ""
+ }
+ users, err := ctx.Raw.UsersGetUsers(ctx, []tg.InputUserClass{
+ &tg.InputUser{UserID: userID, AccessHash: userPeer.AccessHash},
+ })
+ if err != nil {
+ log.Debugf("Failed to fetch user %d: %v", userID, err)
+ return "", ""
+ }
+ for _, item := range users {
+ if user, ok := item.(*tg.User); ok && user.ID == userID {
+ return displayName(user), user.Username
+ }
+ }
+ return "", ""
+}
diff --git a/common/utils/tgutil/context.go b/common/utils/tgutil/context.go
index a0ce1f41..27759794 100644
--- a/common/utils/tgutil/context.go
+++ b/common/utils/tgutil/context.go
@@ -3,7 +3,7 @@ package tgutil
import (
"context"
- "github.com/celestix/gotgproto/ext"
+ "github.com/krau/mygotg/ext"
)
type contextKey struct{}
diff --git a/common/utils/tgutil/media.go b/common/utils/tgutil/media.go
index 33fb5ea4..1efb8fe7 100644
--- a/common/utils/tgutil/media.go
+++ b/common/utils/tgutil/media.go
@@ -2,11 +2,29 @@ package tgutil
import (
"fmt"
+ "strings"
"github.com/gabriel-vasile/mimetype"
"github.com/gotd/td/tg"
)
+func GetOriginalFileName(media tg.MessageMediaClass) string {
+ v, ok := media.(*tg.MessageMediaDocument)
+ if !ok {
+ return ""
+ }
+ f, ok := v.Document.AsNotEmpty()
+ if !ok {
+ return ""
+ }
+ for _, attribute := range f.Attributes {
+ if name, ok := attribute.(*tg.DocumentAttributeFilename); ok {
+ return strings.TrimSpace(name.GetFileName())
+ }
+ }
+ return ""
+}
+
func GetMediaFileName(media tg.MessageMediaClass) (string, error) {
switch v := media.(type) {
case *tg.MessageMediaPhoto:
diff --git a/common/utils/tgutil/message.go b/common/utils/tgutil/message.go
index 8890e2e7..1832dcae 100644
--- a/common/utils/tgutil/message.go
+++ b/common/utils/tgutil/message.go
@@ -8,7 +8,6 @@ import (
"unicode"
"unicode/utf16"
- "github.com/celestix/gotgproto/ext"
"github.com/duke-git/lancet/v2/maputil"
"github.com/duke-git/lancet/v2/mathutil"
"github.com/duke-git/lancet/v2/slice"
@@ -16,11 +15,13 @@ import (
"github.com/duke-git/lancet/v2/validator"
"github.com/gabriel-vasile/mimetype"
"github.com/gotd/td/constant"
+ "github.com/gotd/td/telegram/message/markup"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/cache"
"github.com/krau/SaveAny-Bot/common/i18n"
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
"github.com/krau/SaveAny-Bot/common/utils/strutil"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
)
@@ -95,10 +96,18 @@ func GenFileNameFromMessage(message tg.Message) string {
return filename + ext
}
-func BuildCancelButton(taskID string) tg.KeyboardButtonClass {
- return &tg.KeyboardButtonCallback{
- Text: i18n.T(i18nk.BotMsgCommonCancelButtonText, nil),
- Data: fmt.Appendf(nil, "cancel %s", taskID),
+func BuildCancelButton(taskID string) tg.KeyboardInlineButton {
+ return markup.Callback(
+ i18n.T(i18nk.BotMsgCommonCancelButtonText, nil),
+ fmt.Appendf(nil, "cancel %s", taskID),
+ )
+}
+
+func BuildCancelMarkup(taskID string) *tg.ReplyInlineMarkup {
+ return &tg.ReplyInlineMarkup{
+ Rows: []tg.KeyboardInlineButtonRow{{
+ Buttons: []tg.KeyboardInlineButton{BuildCancelButton(taskID)},
+ }},
}
}
@@ -192,10 +201,12 @@ func getMessagesRange(ctx *ext.Context, chatID int64, minId, maxId int) ([]*tg.M
return result, nil
}
-func getMessageByID(ctx *ext.Context, chatID int64, msgID int) (*tg.Message, error) {
+func getMessageByID(ctx *ext.Context, chatID int64, msgID int, useCache bool) (*tg.Message, error) {
key := fmt.Sprintf("tgmsg:%d:%d:%d", ctx.Self.ID, chatID, msgID)
- if msg, ok := cache.Get[*tg.Message](key); ok {
- return msg, nil
+ if useCache {
+ if msg, ok := cache.Get[*tg.Message](key); ok {
+ return msg, nil
+ }
}
msgs, err := ctx.GetMessages(chatID, []tg.InputMessageClass{
&tg.InputMessageID{ID: msgID},
@@ -216,25 +227,57 @@ func getMessageByID(ctx *ext.Context, chatID int64, msgID int) (*tg.Message, err
}
func GetMessageByID(ctx *ext.Context, chatID int64, msgID int) (*tg.Message, error) {
- // we don't know what the input chatID is bot api style(e.g. channel with -100 prefix) or plain tdlib style(no any prefix and every id is positive)
- if msg, err := getMessageByID(ctx, chatID, msgID); err == nil {
+ return lookupMessageByID(ctx, chatID, msgID, true)
+}
+
+// GetMessageByIDFresh fetches the message from Telegram instead of the cache.
+// Use it when the caller needs current file references, for example when
+// retrying a failed download.
+func GetMessageByIDFresh(ctx *ext.Context, chatID int64, msgID int) (*tg.Message, error) {
+ return lookupMessageByID(ctx, chatID, msgID, false)
+}
+
+// GetMessageByIDFreshAny tries each client context in order and returns the
+// message with the client that fetched it. It bypasses the message cache so
+// file references are current.
+func GetMessageByIDFreshAny(ctxs []*ext.Context, chatID int64, msgID int) (*tg.Message, *ext.Context, error) {
+ var lastErr error
+ for _, ctx := range ctxs {
+ if ctx == nil {
+ continue
+ }
+ msg, err := GetMessageByIDFresh(ctx, chatID, msgID)
+ if err == nil {
+ return msg, ctx, nil
+ }
+ lastErr = err
+ }
+ if lastErr == nil {
+ lastErr = fmt.Errorf("no telegram client available")
+ }
+ return nil, nil, lastErr
+}
+
+func lookupMessageByID(ctx *ext.Context, chatID int64, msgID int, useCache bool) (*tg.Message, error) {
+ // we don't know if the input chatID is bot api style(e.g. channel with -100 prefix) or plain tdlib style(no any prefix and every id is positive)
+ if msg, err := getMessageByID(ctx, chatID, msgID, useCache); err == nil {
return msg, nil
}
in := constant.TDLibPeerID(chatID)
plain := in.ToPlain()
var channel constant.TDLibPeerID
channel.Channel(plain)
- if msg, err := getMessageByID(ctx, int64(channel), msgID); err == nil {
+ if msg, err := getMessageByID(ctx, int64(channel), msgID, useCache); err == nil {
return msg, nil
}
var chat constant.TDLibPeerID
chat.Chat(plain)
- if msg, err := getMessageByID(ctx, int64(chat), msgID); err == nil {
+ if msg, err := getMessageByID(ctx, int64(chat), msgID, useCache); err == nil {
return msg, nil
}
var userID constant.TDLibPeerID
userID.User(plain)
- if msg, err := getMessageByID(ctx, int64(userID), msgID); err == nil {
+ if msg, err := getMessageByID(ctx, int64(userID), msgID, useCache); err == nil {
return msg, nil
}
diff --git a/common/utils/tgutil/peer.go b/common/utils/tgutil/peer.go
index ddf0ea82..f8e02ce1 100644
--- a/common/utils/tgutil/peer.go
+++ b/common/utils/tgutil/peer.go
@@ -1,15 +1,47 @@
package tgutil
-import "github.com/gotd/td/tg"
+import (
+ "github.com/gotd/td/constant"
+ "github.com/gotd/td/tg"
+ tgstorage "github.com/krau/mygotg/storage"
+)
+// MarkedChannelID converts a plain channel ID to the Bot API style
+// -100 form used across the database and HTTP API.
+func MarkedChannelID(id int64) int64 {
+ var peerID constant.TDLibPeerID
+ peerID.Channel(id)
+ return int64(peerID)
+}
+
+// MarkedChatID converts a plain basic-group ID to its negative Bot API form.
+func MarkedChatID(id int64) int64 {
+ var peerID constant.TDLibPeerID
+ peerID.Chat(id)
+ return int64(peerID)
+}
+
+// MarkedPeerID converts a plain peer ID to the Bot API style form for its type.
+func MarkedPeerID(id int64, typ int) int64 {
+ switch tgstorage.EntityType(typ) {
+ case tgstorage.TypeChannel:
+ return MarkedChannelID(id)
+ case tgstorage.TypeChat:
+ return MarkedChatID(id)
+ default:
+ return id
+ }
+}
+
+// ChatIdFromPeer returns the Bot API style ID of a message or callback peer.
func ChatIdFromPeer(peer tg.PeerClass) int64 {
- switch peer := peer.(type) {
+ switch p := peer.(type) {
case *tg.PeerChannel:
- return peer.ChannelID
- case *tg.PeerUser:
- return peer.UserID
+ return MarkedChannelID(p.ChannelID)
case *tg.PeerChat:
- return peer.ChatID
+ return MarkedChatID(p.ChatID)
+ case *tg.PeerUser:
+ return p.UserID
default:
return 0
}
diff --git a/common/utils/tgutil/progress_edit.go b/common/utils/tgutil/progress_edit.go
index 5648b7b0..a9352bfc 100644
--- a/common/utils/tgutil/progress_edit.go
+++ b/common/utils/tgutil/progress_edit.go
@@ -16,16 +16,38 @@ type ProgressSender func(ctx context.Context, chatID int64, req *tg.MessagesEdit
const progressEditInterval = time.Second
-var progressEditLimiter = rate.NewLimiter(rate.Every(progressEditInterval), 1)
+var progressEditLimiters = struct {
+ sync.Mutex
+ byChat map[int64]*rate.Limiter
+}{byChat: make(map[int64]*rate.Limiter)}
+
+func progressEditLimiter(chatID int64) *rate.Limiter {
+ progressEditLimiters.Lock()
+ defer progressEditLimiters.Unlock()
+ limiter := progressEditLimiters.byChat[chatID]
+ if limiter == nil {
+ limiter = rate.NewLimiter(rate.Every(progressEditInterval), 1)
+ progressEditLimiters.byChat[chatID] = limiter
+ }
+ return limiter
+}
func SendProgressEdit(ctx context.Context, chatID int64, req *tg.MessagesEditMessageRequest) error {
- if err := progressEditLimiter.Wait(context.Background()); err != nil {
+ if err := progressEditLimiter(chatID).Wait(ctx); err != nil {
return err
}
ext := ExtFromContext(ctx)
if ext == nil {
return nil
}
+ if ext.Context != nil && ext.Context.Err() != nil {
+ // The final progress edit can run after the task context is canceled.
+ // Keep the Telegram client and other context values, but detach the
+ // cancellation signal so EditMessage can still be sent.
+ clone := *ext
+ clone.Context = context.WithoutCancel(ext.Context)
+ ext = &clone
+ }
if _, err := ext.EditMessage(chatID, req); err != nil {
return err
}
@@ -53,6 +75,12 @@ func NewProgressEditQueue(sender ProgressSender) *ProgressEditQueue {
}
func (q *ProgressEditQueue) Submit(ctx context.Context, chatID int64, req *tg.MessagesEditMessageRequest, final bool) {
+ if final {
+ // The final edit may run after the task context is canceled by the
+ // queue. Keep its values (including the Telegram client) but not the
+ // cancellation signal, otherwise the message stays in an old phase.
+ ctx = context.WithoutCancel(ctx)
+ }
job := progressEditJob{ctx: ctx, chatID: chatID, req: req, final: final}
q.start.Do(func() { go q.run() })
for {
diff --git a/common/utils/tgutil/resolve.go b/common/utils/tgutil/resolve.go
index b5e975c0..00b74de9 100644
--- a/common/utils/tgutil/resolve.go
+++ b/common/utils/tgutil/resolve.go
@@ -6,24 +6,42 @@ import (
"strconv"
"strings"
- "github.com/celestix/gotgproto/ext"
"github.com/duke-git/lancet/v2/validator"
+ "github.com/gotd/td/constant"
"github.com/gotd/td/tg"
+ "github.com/krau/mygotg/ext"
)
func ParseChatID(ctx *ext.Context, idOrUsername string) (int64, error) {
- idOrUsername = strings.TrimPrefix(idOrUsername, "@")
+ idOrUsername = strings.TrimPrefix(strings.TrimSpace(idOrUsername), "@")
+ if idOrUsername == "" {
+ return 0, fmt.Errorf("chat ID is empty")
+ }
if validator.IsIntStr(idOrUsername) {
- chatID, err := strconv.Atoi(idOrUsername)
+ chatID, err := strconv.ParseInt(idOrUsername, 10, 64)
if err != nil {
return 0, err
}
- return int64(chatID), nil
+ if chatID == 0 {
+ return 0, fmt.Errorf("chat ID must not be zero")
+ }
+ if chatID < 0 {
+ peerID := constant.TDLibPeerID(chatID)
+ if !peerID.IsChannel() && !peerID.IsChat() {
+ return 0, fmt.Errorf("invalid negative chat ID: %d", chatID)
+ }
+ return chatID, nil
+ }
+ // A plain ID is a user ID unless the peer cache already knows that
+ // this plain ID belongs to a channel or basic group.
+ if peer := ctx.PeerStorage.GetPeerById(chatID); peer != nil && peer.ID != 0 {
+ return MarkedPeerID(peer.ID, peer.Type), nil
+ }
+ return chatID, nil
}
username := idOrUsername
- peer := ctx.PeerStorage.GetPeerByUsername(username)
- if peer != nil && peer.ID != 0 {
- return peer.ID, nil
+ if peer := ctx.PeerStorage.GetPeerByUsername(username); peer != nil && peer.ID != 0 {
+ return MarkedPeerID(peer.ID, peer.Type), nil
}
chat, err := ctx.ResolveUsername(username)
if err != nil {
@@ -36,7 +54,29 @@ func ParseChatID(ctx *ext.Context, idOrUsername string) (int64, error) {
if chatID == 0 {
return 0, fmt.Errorf("chat ID is zero for username: %s", idOrUsername)
}
- return chatID, nil
+ switch {
+ case chat.IsAChannel():
+ return MarkedChannelID(chatID), nil
+ case chat.IsAChat():
+ return MarkedChatID(chatID), nil
+ default:
+ return chatID, nil
+ }
+}
+
+// ParseChannelID parses a t.me/c/ channel reference as a Bot API style ID.
+func ParseChannelID(_ *ext.Context, value string) (int64, error) {
+ value = strings.TrimSpace(value)
+ if id, err := strconv.ParseInt(value, 10, 64); err == nil {
+ if id == 0 {
+ return 0, fmt.Errorf("channel ID must not be zero")
+ }
+ if id < 0 {
+ return id, nil
+ }
+ return MarkedChannelID(id), nil
+ }
+ return 0, fmt.Errorf("invalid channel ID: %s", value)
}
// return: ChatID, MessageID, error
@@ -91,7 +131,13 @@ func ParseMessageLink(ctx *ext.Context, link string) (int64, int, error) {
if paths[0] != "c" {
chatPart = paths[0]
}
- chatID, err := ParseChatID(ctx, chatPart)
+ var chatID int64
+ var err error
+ if paths[0] == "c" {
+ chatID, err = ParseChannelID(ctx, chatPart)
+ } else {
+ chatID, err = ParseChatID(ctx, chatPart)
+ }
if err != nil {
return 0, 0, fmt.Errorf("failed to parse chat ID: %w", err)
}
@@ -105,7 +151,7 @@ func ParseMessageLink(ctx *ext.Context, link string) (int64, int, error) {
if paths[0] != "c" {
return 0, 0, fmt.Errorf("invalid message link format: %s", link)
}
- chatID, err := ParseChatID(ctx, paths[1])
+ chatID, err := ParseChannelID(ctx, paths[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to parse chat ID: %w", err)
}
diff --git a/common/utils/tgutil/sender.go b/common/utils/tgutil/sender.go
new file mode 100644
index 00000000..2239914a
--- /dev/null
+++ b/common/utils/tgutil/sender.go
@@ -0,0 +1,56 @@
+package tgutil
+
+import (
+ "github.com/gotd/td/tg"
+ "github.com/krau/mygotg/ext"
+)
+
+// SenderID returns the user responsible for the update. Callback queries carry
+// it natively; message updates resolve it from the sender peer.
+func SenderID(u *ext.Update) int64 {
+ if u == nil {
+ return 0
+ }
+ if u.CallbackQuery != nil {
+ return u.CallbackQuery.GetUserID()
+ }
+ message := u.EffectiveMessage
+ // Outgoing updates in private chats belong to the chat peer, not to the
+ // bot that sent them.
+ if message != nil && message.Out {
+ return u.GetUserChat().GetID()
+ }
+ if user := u.EffectiveUser(); user != nil {
+ return user.ID
+ }
+ if message != nil {
+ if from, ok := message.GetFromID(); ok {
+ if peer, ok := from.(*tg.PeerUser); ok {
+ return peer.UserID
+ }
+ }
+ }
+ return u.GetUserChat().GetID()
+}
+
+// ChatID returns the chat that replies and edits must target. In private chats
+// this equals the sender ID; in groups and supergroups it is the group chat.
+// Peer IDs are used directly so callback updates without entity maps still
+// resolve their target chat.
+func ChatID(u *ext.Update) int64 {
+ if u == nil {
+ return 0
+ }
+ if message := u.EffectiveMessage; message != nil {
+ if id := ChatIdFromPeer(message.PeerID); id != 0 {
+ return id
+ }
+ }
+ if cb := u.CallbackQuery; cb != nil {
+ if id := ChatIdFromPeer(cb.Peer); id != 0 {
+ return id
+ }
+ return cb.GetUserID()
+ }
+ return u.EffectiveChat().GetID()
+}
diff --git a/common/utils/tphutil/tph.go b/common/utils/tphutil/tph.go
index d2647661..525bc1ae 100644
--- a/common/utils/tphutil/tph.go
+++ b/common/utils/tphutil/tph.go
@@ -1,41 +1,53 @@
package tphutil
import (
+ "context"
"encoding/json"
+ "errors"
+ "fmt"
+ "net/url"
"strings"
"sync"
+ "github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/pkg/telegraph"
)
-var (
- tphClient *telegraph.Client
- once sync.Once
-)
+var defaultClient = struct {
+ sync.Mutex
+ key string
+ client *telegraph.Client
+}{}
func DefaultClient() *telegraph.Client {
- once.Do(func() {
- tphClient = initDefault()
- })
- return tphClient
-}
-
-func initDefault() *telegraph.Client {
- var client *telegraph.Client
- if config.C().Telegram.Proxy.Enable && config.C().Telegram.Proxy.URL != "" {
- proxyUrl := config.C().Telegram.Proxy.URL
- var err error
- client, err = telegraph.NewClientWithProxy(proxyUrl)
- if err != nil {
- client = telegraph.NewClient()
- }
- } else {
+ cfg := config.C().Telegram.Proxy
+ key := "direct"
+ if cfg.Enable && cfg.URL != "" {
+ key = cfg.URL
+ }
+ defaultClient.Lock()
+ defer defaultClient.Unlock()
+ if defaultClient.client != nil && defaultClient.key == key {
+ return defaultClient.client
+ }
+ client, err := newClient(key)
+ if err != nil {
+ log.Warn("Failed to create Telegraph client with proxy; using direct client", "error", err)
client = telegraph.NewClient()
}
+ defaultClient.client = client
+ defaultClient.key = key
return client
}
+func newClient(key string) (*telegraph.Client, error) {
+ if key == "direct" {
+ return telegraph.NewClient(), nil
+ }
+ return telegraph.NewClientWithProxy(key)
+}
+
func GetNodeImages(node telegraph.Node) []string {
var srcs []string
@@ -62,3 +74,55 @@ func GetNodeImages(node telegraph.Node) []string {
}
return srcs
}
+
+// ParsePagePath validates a Telegraph page URL and returns its path. Bot
+// messages require HTTPS; the HTTP API also accepts legacy HTTP links.
+func ParsePagePath(pageURL string) (string, error) {
+ return parsePagePath(pageURL, false)
+}
+
+func parsePagePath(pageURL string, allowHTTP bool) (string, error) {
+ u, err := url.Parse(pageURL)
+ if err != nil {
+ return "", fmt.Errorf("invalid telegraph URL: %w", err)
+ }
+ if u.Scheme != "https" && !(allowHTTP && u.Scheme == "http") {
+ return "", fmt.Errorf("invalid telegraph URL scheme: %s", u.Scheme)
+ }
+ if !strings.EqualFold(u.Hostname(), "telegra.ph") && !strings.EqualFold(u.Hostname(), "telegraph.co") {
+ return "", fmt.Errorf("invalid telegraph URL host: %s", u.Host)
+ }
+ pagePath := strings.Trim(u.EscapedPath(), "/")
+ if pagePath == "" || strings.Contains(pagePath, "/") {
+ return "", fmt.Errorf("invalid telegraph URL path: %s", u.Path)
+ }
+ pagePath, err = url.PathUnescape(pagePath)
+ if err != nil {
+ return "", fmt.Errorf("failed to unescape telegraph path: %w", err)
+ }
+ pagePath = strings.TrimSpace(pagePath)
+ if pagePath == "" || strings.Contains(pagePath, "/") {
+ return "", fmt.Errorf("invalid telegraph URL path: %s", u.Path)
+ }
+ return pagePath, nil
+}
+
+// ExtractImages fetches a Telegraph page and returns its image URLs and path.
+func ExtractImages(ctx context.Context, pageURL string) ([]string, string, error) {
+ pagePath, err := parsePagePath(pageURL, true)
+ if err != nil {
+ return nil, "", err
+ }
+ page, err := DefaultClient().GetPage(ctx, pagePath)
+ if err != nil {
+ return nil, "", fmt.Errorf("failed to get telegraph page: %w", err)
+ }
+ images := make([]string, 0)
+ for _, elem := range page.Content {
+ images = append(images, GetNodeImages(elem)...)
+ }
+ if len(images) == 0 {
+ return nil, "", errors.New("no images found in telegraph page")
+ }
+ return images, pagePath, nil
+}
diff --git a/config.example.toml b/config.example.toml
deleted file mode 100644
index 9c07f3ab..00000000
--- a/config.example.toml
+++ /dev/null
@@ -1,135 +0,0 @@
-# 创建文件时,若需要保留中文注释,请务必确保本文件编码为 UTF-8 ,否则会无法读取。
-# When creating this file, ensure the encoding is UTF-8 to preserve Chinese comments, otherwise it may fail to read.
-# 更详细的配置请在 https://sabot.unv.app/deployment/configuration 查看
-# For more detailed configuration, see https://sabot.unv.app/deployment/configuration
-workers = 4 # 同时下载文件数 / Number of concurrent downloads
-retry = 3 # 下载失败重试次数 / Number of retries on download failure
-threads = 4 # 单个任务下载使用的最大线程数 / Max threads per download task
-stream = false # 使用流式传输模式, 建议仅在硬盘空间十分有限时使用. / Stream mode, recommended only when disk space is very limited.
-
-[log]
-# 日志级别, 可选: debug, info, warn, error, fatal / Log level, options: debug, info, warn, error, fatal
-level = "debug"
-
-[telegram]
-# Bot Token
-# 更换 Bot Token 后请删除会话数据库文件 (默认路径为 data/session.db )
-# After changing Bot Token, delete the session database file (default path: data/session.db)
-token = ""
-# Telegram API 配置, 若不配置也可运行, 将使用默认的 API ID 和 API HASH
-# Telegram API config, optional - defaults will be used if not set
-# 推荐使用自己的 API ID 和 API HASH (https://my.telegram.org)
-# Recommended: use your own API ID and API HASH (https://my.telegram.org)
-# app_id = 1025907
-# app_hash = "452b0359b988148995f22ff0f4229750"
-[telegram.proxy]
-# 启用代理连接 telegram / Enable proxy for Telegram connection
-enable = false
-url = "socks5://127.0.0.1:7890"
-
-# Aria2 配置 / Aria2 configuration
-[aria2]
-# 启用 Aria2 下载支持 / Enable Aria2 download support
-enable = false
-# Aria2 RPC URL
-url = "http://localhost:6800/jsonrpc"
-# Aria2 RPC Secret (如果配置了 rpc-secret) / Aria2 RPC Secret (if rpc-secret is configured)
-secret = ""
-# 转存完成后删除 Aria2 下载的本地文件 / Delete local files downloaded by Aria2 after transfer
-remove_after_transfer = true
-
-# yt-dlp 视频下载配置 / yt-dlp video download configuration
-[ytdlp]
-# 默认下载的最高视频清晰度 (按高度限制), 如 1080, 720, 480; 0 表示不限制 (下载最佳画质)
-# Max video resolution by height, e.g. 1080, 720, 480; 0 means no limit (best available)
-# 仅在使用 /ytdlp 命令且未手动指定任何参数时生效
-# Only applies when using the /ytdlp command without manually specified flags
-max_height = 1080
-# 直接指定 yt-dlp format 选择表达式, 留空则使用 max_height
-# Raw yt-dlp format selector; leave empty to use max_height
-# 设置后优先级高于 max_height, 例如: "bv*[height<=720]+ba/b"
-# Takes precedence over max_height when set, e.g. "bv*[height<=720]+ba/b"
-format = ""
-# 下载后转封装的视频容器格式, 留空则不转封装. 默认 mp4
-# Target video container to recode into after download; empty disables recoding. Default mp4
-recode = "mp4"
-# 下载文件名模板 (yt-dlp output template), 默认使用视频标题
-# 目录始终由 bot 管理, 模板中的路径相对于 bot 的临时下载目录
-# Filename template for downloads (yt-dlp output template), defaults to the video title
-# The download directory is always managed by the bot; paths inside the template are relative to its temp directory
-# 例如 / e.g. "%(uploader)s - %(title)s.%(ext)s"
-filename_template = "%(title)s.%(ext)s"
-# 将文件名限制为 ASCII 字符 (yt-dlp --restrict-filenames), 会丢弃标题中的非拉丁字符、空格和 &
-# Restrict file names to ASCII characters (yt-dlp --restrict-filenames): drops non-latin characters, spaces and & from titles
-restrict_filenames = false
-
-# 解析器配置 / Parser configuration
-[parser]
-# 启用 JS 解析器插件 (Go 内置解析器默认启用)
-# Enable JS parser plugins (built-in Go parsers are enabled by default)
-plugin_enable = false
-# 插件目录, 可以是多个目录 / Plugin directories, multiple allowed
-plugin_dirs = ["./plugins"]
-# 解析器默认代理 / Default proxy for parsers
-proxy = ""
-
-# Twitter/X 解析器配置 / Twitter/X parser configuration
-[parser.twitter]
-# 自定义 API 域名 / Custom API domain
-api_domain = "api.fxtwitter.com"
-# 单独为此解析器指定代理 (留空则使用 [parser] 中的 proxy)
-# Proxy for this parser only (leave empty to use the proxy from [parser])
-# proxy = "http://127.0.0.1:7890"
-
-# Kemono 解析器配置 (暂无可配置项, 留空即可)
-# Kemono parser configuration (no options yet, leave empty)
-[parser.kemono]
-
-# HTTP API 配置 / HTTP API configuration
-[api]
-# 启用 HTTP API / Enable HTTP API
-enable = false
-# 监听地址 / Listen address
-host = "0.0.0.0"
-# 监听端口 / Listen port
-port = 8080
-# 认证 Token (必需) / Auth token (required)
-token = ""
-
-# 存储列表 / Storage list
-[[storages]]
-# 标识名, 需要唯一 / Identifier name, must be unique
-name = "本机1"
-# 存储类型, 目前可用: local, alist, webdav, s3, telegram
-# Storage type, available: local, alist, webdav, s3, telegram
-type = "local"
-# 启用存储 / Enable storage
-enable = true
-# 文件保存根路径 / Root path for saving files
-base_path = "./downloads"
-
-[[storages]]
-name = "MyWebdav"
-type = "webdav"
-enable = false
-base_path = '/path/telegram'
-url = 'https://example.com/dav'
-username = 'username'
-password = 'password'
-
-# 用户列表 / User list
-[[users]]
-# telegram user id
-id = 114514
-# 存储过滤列表, 元素为存储标识名. / Storage filter list, elements are storage identifier names.
-# 将该列表留空并开启黑名单过滤模式以允许使用所有存储,此处示例为黑名单模式,用户 114514 可使用所有存储
-# Leave empty with blacklist mode enabled to allow all storages; this example uses blacklist mode, user 114514 can use all storages
-storages = []
-# 使用列表过滤黑名单模式,反之则为白名单,白名单请在列表中指定可用的存储.
-# Blacklist filter mode; set false for whitelist mode and specify allowed storages in the list.
-blacklist = true
-
-[[users]]
-id = 123456
-storages = ["本机1"]
-blacklist = false # 使用白名单模式,此时,用户 123456 仅可使用标识名为 '本地1' 的存储 / Whitelist mode: user 123456 can only use the storage named '本机1'
diff --git a/config/aria2.go b/config/aria2.go
new file mode 100644
index 00000000..fc1b96cb
--- /dev/null
+++ b/config/aria2.go
@@ -0,0 +1,8 @@
+package config
+
+type aria2Config struct {
+ Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
+ Url string `toml:"url" mapstructure:"url" json:"url"`
+ Secret string `toml:"secret" mapstructure:"secret" json:"secret"`
+ KeepFile bool `toml:"keep_file" mapstructure:"keep_file" json:"keep_file"`
+}
diff --git a/config/config.go b/config/config.go
new file mode 100644
index 00000000..8c8367b0
--- /dev/null
+++ b/config/config.go
@@ -0,0 +1,286 @@
+package config
+
+import (
+ "context"
+ "crypto/rand"
+ "encoding/base64"
+ "fmt"
+ "net"
+ "net/http"
+ "net/url"
+ "path/filepath"
+ "sync/atomic"
+ "time"
+
+ "github.com/krau/SaveAny-Bot/config/storage"
+ "golang.org/x/net/proxy"
+)
+
+type Config struct {
+ Lang string `toml:"lang" mapstructure:"lang" json:"lang"`
+ Workers int `toml:"workers" mapstructure:"workers" json:"workers"`
+ Retry int `toml:"retry" mapstructure:"retry" json:"retry"`
+ NoCleanCache bool `toml:"no_clean_cache" mapstructure:"no_clean_cache" json:"no_clean_cache"`
+ Threads int `toml:"threads" mapstructure:"threads" json:"threads"`
+ // DownloadThreads overrides automatic per-file chunk concurrency when > 0.
+ DownloadThreads int `toml:"download_threads" mapstructure:"download_threads" json:"download_threads"`
+ // MaxDownloadThreads caps automatic per-file chunk concurrency.
+ MaxDownloadThreads int `toml:"max_download_threads" mapstructure:"max_download_threads" json:"max_download_threads"`
+ // TransferBudget caps concurrent Telegram part RPCs; 0 derives workers * threads.
+ TransferBudget int `toml:"transfer_budget" mapstructure:"transfer_budget" json:"transfer_budget"`
+ // RelayMinSizeMB controls the smallest file eligible for helper downloads.
+ RelayMinSizeMB int `toml:"relay_min_size_mb" mapstructure:"relay_min_size_mb" json:"relay_min_size_mb"`
+ // ChunkRetries is the retry count for transient errors on one file chunk.
+ ChunkRetries int `toml:"chunk_retries" mapstructure:"chunk_retries" json:"chunk_retries"`
+ Stream bool `toml:"stream" mapstructure:"stream" json:"stream"`
+ Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
+ Log logConfig `toml:"log" mapstructure:"log" json:"log"`
+ Aria2 aria2Config `toml:"aria2" mapstructure:"aria2" json:"aria2"`
+ Cache cacheConfig `toml:"cache" mapstructure:"cache" json:"cache"`
+ Users []UserConfig `toml:"users" mapstructure:"users" json:"-"`
+ Temp tempConfig `toml:"temp" mapstructure:"temp" json:"temp"`
+ DB dbConfig `toml:"db" mapstructure:"db" json:"db"`
+ Telegram telegramConfig `toml:"telegram" mapstructure:"telegram" json:"telegram"`
+ Storages []storage.StorageConfig `toml:"-" mapstructure:"-" json:"-"`
+ Parser parserConfig `toml:"parser" mapstructure:"parser" json:"parser"`
+ Hook hookConfig `toml:"hook" mapstructure:"hook" json:"hook"`
+ Ytdlp YtdlpConfig `toml:"ytdlp" mapstructure:"ytdlp" json:"ytdlp"`
+ Web webConfig `toml:"web" mapstructure:"web" json:"web"`
+ Metadata metadataConfig `toml:"metadata" mapstructure:"metadata" json:"metadata"`
+ Media mediaConfig `toml:"media" mapstructure:"media" json:"media"`
+}
+
+type metadataConfig struct {
+ Enabled bool `toml:"enabled" mapstructure:"enabled" json:"enabled"`
+ IncludeText bool `toml:"include_text" mapstructure:"include_text" json:"include_text"`
+ Sidecar bool `toml:"sidecar" mapstructure:"sidecar" json:"sidecar"`
+}
+
+type mediaConfig struct {
+ // AllowedMediaTypes is empty when all supported media types are allowed.
+ AllowedMediaTypes []string `toml:"allowed_media_types" mapstructure:"allowed_media_types" json:"allowed_media_types"`
+}
+
+type webConfig struct {
+ Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
+ Listen string `toml:"listen" mapstructure:"listen" json:"listen"`
+ APIToken string `toml:"api_token" mapstructure:"api_token" json:"api_token"`
+ SecureCookie bool `toml:"secure_cookie" mapstructure:"secure_cookie" json:"secure_cookie"`
+}
+
+const (
+ defaultBotSessionFile = "bot_session.db"
+ defaultUserbotSessionFile = "userbot_session.db"
+ defaultWebListen = "127.0.0.1:28080"
+)
+
+var current atomic.Pointer[Config]
+
+// DefaultBotSessionPath returns the v1 bot session database path.
+func DefaultBotSessionPath(dataDir string) string {
+ return filepath.Join(dataDir, defaultBotSessionFile)
+}
+
+// DefaultUserbotSessionPath returns the v1 userbot session database path.
+func DefaultUserbotSessionPath(dataDir string) string {
+ return filepath.Join(dataDir, defaultUserbotSessionFile)
+}
+
+func init() {
+ cfg := Default("./data")
+ current.Store(&cfg)
+}
+
+// C returns a snapshot of the current configuration. Callers must not mutate it.
+func C() Config {
+ return *current.Load()
+}
+
+// TransferBudget is the upper bound for concurrent Telegram file-part RPCs
+// (download and upload combined). It keeps the per-file thread setting from
+// multiplying with task and element concurrency.
+func TransferBudget() int {
+ cfg := current.Load()
+ if cfg.TransferBudget > 0 {
+ return cfg.TransferBudget
+ }
+ return max(cfg.Workers, 1) * max(cfg.Threads, 1)
+}
+
+// Set replaces the current configuration snapshot.
+func Set(cfg Config) {
+ current.Store(&cfg)
+}
+
+func DefaultsMap(dataDir string) map[string]any {
+ return map[string]any{
+ "lang": "zh-Hans",
+ "workers": 3,
+ "retry": 3,
+ "no_clean_cache": false,
+ "threads": 4,
+ "download_threads": 0,
+ "max_download_threads": 16,
+ "transfer_budget": 0,
+ "relay_min_size_mb": 8,
+ "chunk_retries": 20,
+ "log.level": "debug",
+
+ "cache.ttl": 86400,
+ "cache.num_counters": 1e5,
+ "cache.max_cost": 1e6,
+
+ "telegram.app_id": 1025907,
+ "telegram.app_hash": "452b0359b988148995f22ff0f4229750",
+ "telegram.rpc_retry": 5,
+ "telegram.userbot.enable": false,
+ "telegram.userbot.session": DefaultUserbotSessionPath(dataDir),
+ "telegram.relay_chat_id": 0,
+ "telegram.relay_keep_messages": true,
+
+ "temp.base_path": "cache/",
+
+ "db.session": DefaultBotSessionPath(dataDir),
+
+ "web.enable": true,
+ "web.listen": defaultWebListen,
+ "web.api_token": "",
+ "web.secure_cookie": false,
+
+ "ytdlp.recode": "mp4",
+ "ytdlp.filename_template": DefaultYtdlpFilenameTemplate,
+ "ytdlp.max_height": 0,
+ "ytdlp.format": "",
+ "ytdlp.restrict_filenames": false,
+
+ "metadata.enabled": false,
+ "metadata.include_text": true,
+ "metadata.sidecar": false,
+ "media.allowed_media_types": []string{},
+ }
+}
+
+func Default(dataDir string) Config {
+ return Config{
+ Lang: "zh-Hans",
+ Workers: 3,
+ Retry: 3,
+ Threads: 4,
+ DownloadThreads: 0,
+ MaxDownloadThreads: 16,
+ TransferBudget: 0,
+ RelayMinSizeMB: 8,
+ ChunkRetries: 20,
+ Log: logConfig{Level: "debug"},
+ Cache: cacheConfig{
+ TTL: 86400,
+ NumCounters: 1e5,
+ MaxCost: 1e6,
+ },
+ Temp: tempConfig{BasePath: "cache/"},
+ DB: dbConfig{Session: DefaultBotSessionPath(dataDir)},
+ Telegram: telegramConfig{
+ AppID: 1025907,
+ AppHash: "452b0359b988148995f22ff0f4229750",
+ RpcRetry: 5,
+ Userbot: userbotConfig{Session: DefaultUserbotSessionPath(dataDir)},
+ RelayKeepMessages: true,
+ },
+ Ytdlp: YtdlpConfig{
+ Recode: "mp4",
+ FilenameTemplate: DefaultYtdlpFilenameTemplate,
+ },
+ Web: webConfig{
+ Enable: true,
+ Listen: defaultWebListen,
+ },
+ Metadata: metadataConfig{IncludeText: true},
+ Media: mediaConfig{},
+ }
+}
+
+func (c Config) GetStorageByName(name string) storage.StorageConfig {
+ for _, st := range c.Storages {
+ if st.GetName() == name {
+ return st
+ }
+ }
+ return nil
+}
+
+func (c Config) GetStorageIDByName(name string) uint {
+ if st := c.GetStorageByName(name); st != nil {
+ return st.GetID()
+ }
+ return 0
+}
+
+func (c Config) GetStorageByID(id uint) storage.StorageConfig {
+ if id == 0 {
+ return nil
+ }
+ for _, st := range c.Storages {
+ if st.GetID() == id {
+ return st
+ }
+ }
+ return nil
+}
+
+func (c Config) GetStorageNameByID(id uint) string {
+ if id == 0 {
+ return ""
+ }
+ if st := c.GetStorageByID(id); st != nil {
+ return st.GetName()
+ }
+ return ""
+}
+
+// ProxyTransport builds the HTTP transport used for the configured proxy.
+func ProxyTransport(proxyStr string) (*http.Transport, error) {
+ proxyURL, err := url.Parse(proxyStr)
+ if err != nil {
+ return nil, err
+ }
+ transport := &http.Transport{
+ ForceAttemptHTTP2: true,
+ MaxIdleConns: 100,
+ IdleConnTimeout: 90 * time.Second,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ExpectContinueTimeout: 1 * time.Second,
+ }
+ switch proxyURL.Scheme {
+ case "http", "https":
+ transport.Proxy = http.ProxyURL(proxyURL)
+ case "socks5", "socks5h":
+ dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
+ if err != nil {
+ return nil, err
+ }
+ transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr)
+ }
+ default:
+ return nil, fmt.Errorf("unsupported proxy type: %s", proxyURL.Scheme)
+ }
+ return transport, nil
+}
+
+// GeneratePassword returns a URL-safe random password with ~120 bits of entropy.
+func GeneratePassword() string {
+ return randomBase64(15)
+}
+
+// GenerateToken returns a URL-safe token with 256 bits of entropy.
+func GenerateToken() string {
+ return randomBase64(32)
+}
+
+func randomBase64(size int) string {
+ buf := make([]byte, size)
+ if _, err := rand.Read(buf); err != nil {
+ panic(fmt.Sprintf("failed to read random bytes: %v", err))
+ }
+ return base64.RawURLEncoding.EncodeToString(buf)
+}
diff --git a/config/db.go b/config/db.go
index ba7b2a60..2b80686b 100644
--- a/config/db.go
+++ b/config/db.go
@@ -1,6 +1,5 @@
package config
type dbConfig struct {
- Path string `toml:"path" mapstructure:"path"`
- Session string `toml:"session" mapstructure:"session"`
+ Session string `toml:"session" mapstructure:"session" json:"session"`
}
diff --git a/config/flags.go b/config/flags.go
deleted file mode 100644
index e0bc0b3f..00000000
--- a/config/flags.go
+++ /dev/null
@@ -1,75 +0,0 @@
-package config
-
-import (
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
-)
-
-func RegisterFlags(cmd *cobra.Command) {
- flags := cmd.Flags()
-
- flags.StringP("config", "c", "", "config file path")
- flags.StringP("lang", "l", "", "language (e.g., zh-Hans, en)")
- flags.IntP("workers", "w", 0, "number of workers")
- flags.Int("retry", 0, "retry times")
- flags.Int("threads", 0, "number of threads")
- flags.Bool("stream", false, "enable stream mode")
- flags.Bool("no-clean-cache", false, "do not clean cache on exit")
- flags.String("proxy", "", "proxy URL (http, https, socks5, socks5h)")
- flags.String("log-level", "", "log level (trace/debug, info, warn, error, fatal)")
-
- flags.String("telegram-token", "", "telegram bot token")
- flags.Int("telegram-app-id", 0, "telegram app id")
- flags.String("telegram-app-hash", "", "telegram app hash")
- flags.Int("telegram-rpc-retry", 0, "telegram rpc retry times")
- flags.Bool("telegram-userbot-enable", false, "enable userbot")
- flags.String("telegram-userbot-session", "", "userbot session path")
- flags.Bool("telegram-proxy-enable", false, "enable telegram proxy")
- flags.String("telegram-proxy-url", "", "telegram proxy URL")
-
- flags.String("db-path", "", "database path")
- flags.String("db-session", "", "session database path")
-
- flags.String("temp-base-path", "", "temp directory base path")
-
- flags.Bool("parser-plugin-enable", false, "enable parser plugins")
- flags.StringSlice("parser-plugin-dirs", nil, "parser plugin directories")
- flags.String("parser-proxy", "", "parser proxy URL")
-
- bindFlags(cmd)
-}
-
-func bindFlags(cmd *cobra.Command) {
- flags := cmd.Flags()
-
- viper.BindPFlag("lang", flags.Lookup("lang"))
- viper.BindPFlag("workers", flags.Lookup("workers"))
- viper.BindPFlag("retry", flags.Lookup("retry"))
- viper.BindPFlag("threads", flags.Lookup("threads"))
- viper.BindPFlag("stream", flags.Lookup("stream"))
- viper.BindPFlag("no_clean_cache", flags.Lookup("no-clean-cache"))
- viper.BindPFlag("proxy", flags.Lookup("proxy"))
- viper.BindPFlag("log.level", flags.Lookup("log-level"))
-
- viper.BindPFlag("telegram.token", flags.Lookup("telegram-token"))
- viper.BindPFlag("telegram.app_id", flags.Lookup("telegram-app-id"))
- viper.BindPFlag("telegram.app_hash", flags.Lookup("telegram-app-hash"))
- viper.BindPFlag("telegram.rpc_retry", flags.Lookup("telegram-rpc-retry"))
- viper.BindPFlag("telegram.userbot.enable", flags.Lookup("telegram-userbot-enable"))
- viper.BindPFlag("telegram.userbot.session", flags.Lookup("telegram-userbot-session"))
- viper.BindPFlag("telegram.proxy.enable", flags.Lookup("telegram-proxy-enable"))
- viper.BindPFlag("telegram.proxy.url", flags.Lookup("telegram-proxy-url"))
-
- viper.BindPFlag("db.path", flags.Lookup("db-path"))
- viper.BindPFlag("db.session", flags.Lookup("db-session"))
- viper.BindPFlag("temp.base_path", flags.Lookup("temp-base-path"))
-
- viper.BindPFlag("parser.plugin_enable", flags.Lookup("parser-plugin-enable"))
- viper.BindPFlag("parser.plugin_dirs", flags.Lookup("parser-plugin-dirs"))
- viper.BindPFlag("parser.proxy", flags.Lookup("parser-proxy"))
-}
-
-func GetConfigFile(cmd *cobra.Command) string {
- configFile, _ := cmd.Flags().GetString("config")
- return configFile
-}
diff --git a/config/parser.go b/config/parser.go
index b7920955..e4118b0d 100644
--- a/config/parser.go
+++ b/config/parser.go
@@ -4,7 +4,7 @@ type parserConfig struct {
PluginEnable bool `toml:"plugin_enable" mapstructure:"plugin_enable" json:"plugin_enable"`
PluginDirs []string `toml:"plugin_dirs" mapstructure:"plugin_dirs" json:"plugin_dirs"`
Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
- ParserCfgs map[string]map[string]any `mapstructure:",remain"`
+ ParserCfgs map[string]map[string]any `mapstructure:",remain" json:"configs"`
}
func (c Config) GetParserConfigByName(name string) map[string]any {
diff --git a/config/storage/factory.go b/config/storage/factory.go
index 8deeab4b..920ad167 100644
--- a/config/storage/factory.go
+++ b/config/storage/factory.go
@@ -1,70 +1,81 @@
package storage
import (
+ "encoding/json"
"fmt"
- "reflect"
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
- "github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
-var storageFactories = map[storenum.StorageType]func(cfg *BaseConfig) (StorageConfig, error){
- storenum.Local: createStorageConfig(&LocalStorageConfig{}),
- storenum.Alist: createStorageConfig(&AlistStorageConfig{}),
- storenum.Webdav: createStorageConfig(&WebdavStorageConfig{}),
- storenum.Minio: createStorageConfig(&MinioStorageConfig{}),
- storenum.S3: createStorageConfig(&S3StorageConfig{}),
- storenum.Telegram: createStorageConfig(&TelegramStorageConfig{}),
- storenum.Rclone: createStorageConfig(&RcloneStorageConfig{}),
-}
-
-func createStorageConfig(configType StorageConfig) func(cfg *BaseConfig) (StorageConfig, error) {
- return func(cfg *BaseConfig) (StorageConfig, error) {
- configValue := reflect.New(reflect.TypeOf(configType).Elem()).Interface().(StorageConfig)
+// NewStorageConfigFromJSON builds a typed storage config from JSON-encoded
+// type-specific fields. Only the legacy importer still needs Viper; runtime
+// storage settings come from the database as JSON.
+func NewStorageConfigFromJSON(id uint, name, typ string, enable bool, raw []byte) (StorageConfig, error) {
+ st, err := storenum.ParseStorageType(typ)
+ if err != nil {
+ return nil, fmt.Errorf("invalid storage type %s: %w", typ, err)
+ }
- reflect.ValueOf(configValue).Elem().FieldByName("BaseConfig").Set(reflect.ValueOf(*cfg))
+ base := BaseConfig{ID: id, Name: name, Type: typ, Enable: enable}
+ cfg, err := newStorageConfig(st, base)
+ if err != nil {
+ return nil, err
+ }
- if err := mapstructure.Decode(cfg.RawConfig, configValue); err != nil {
- return nil, fmt.Errorf("failed to decode %s storage config: %w", cfg.Type, err)
+ if len(raw) > 0 {
+ if err := json.Unmarshal(raw, cfg); err != nil {
+ return nil, fmt.Errorf("failed to decode %s storage config: %w", typ, err)
}
+ }
+ // The database owns identity and enablement; type-specific JSON must not
+ // override them.
+ setBaseConfig(cfg, base)
+ return cfg, nil
+}
- return configValue, nil
+func newStorageConfig(st storenum.StorageType, base BaseConfig) (StorageConfig, error) {
+ switch st {
+ case storenum.Local:
+ return &LocalStorageConfig{BaseConfig: base}, nil
+ case storenum.Alist:
+ return &AlistStorageConfig{BaseConfig: base}, nil
+ case storenum.Webdav:
+ return &WebdavStorageConfig{BaseConfig: base}, nil
+ case storenum.S3:
+ return &S3StorageConfig{BaseConfig: base}, nil
+ case storenum.Telegram:
+ return &TelegramStorageConfig{BaseConfig: base}, nil
+ case storenum.Rclone:
+ return &RcloneStorageConfig{BaseConfig: base}, nil
+ default:
+ return nil, fmt.Errorf("unsupported storage type: %s", st)
}
}
-func LoadStorageConfigs(v *viper.Viper) ([]StorageConfig, error) {
+func setBaseConfig(cfg StorageConfig, base BaseConfig) {
+ switch typed := cfg.(type) {
+ case *LocalStorageConfig:
+ typed.BaseConfig = base
+ case *AlistStorageConfig:
+ typed.BaseConfig = base
+ case *WebdavStorageConfig:
+ typed.BaseConfig = base
+ case *S3StorageConfig:
+ typed.BaseConfig = base
+ case *TelegramStorageConfig:
+ typed.BaseConfig = base
+ case *RcloneStorageConfig:
+ typed.BaseConfig = base
+ }
+}
+
+// LoadAllBaseConfigs unmarshals every legacy storage entry, including disabled
+// ones. It is only used by the one-time legacy importer.
+func LoadAllBaseConfigs(v *viper.Viper) ([]BaseConfig, error) {
var baseConfigs []BaseConfig
if err := v.UnmarshalKey("storages", &baseConfigs); err != nil {
return nil, fmt.Errorf("failed to unmarshal storage configs: %w", err)
}
-
- var configs []StorageConfig
- for _, baseCfg := range baseConfigs {
- if !baseCfg.Enable {
- continue
- }
- st, err := storenum.ParseStorageType(baseCfg.Type)
- if err != nil {
- return nil, fmt.Errorf("invalid storage type %s for %s: %w", baseCfg.Type, baseCfg.Name, err)
- }
-
- factory, ok := storageFactories[st]
- if !ok {
- return nil, fmt.Errorf("unsupported storage type: %s", baseCfg.Type)
- }
-
- cfg, err := factory(&baseCfg)
- if err != nil {
- return nil, fmt.Errorf("failed to create storage config for %s: %w", baseCfg.Name, err)
- }
-
- if err := cfg.Validate(); err != nil {
- return nil, fmt.Errorf("invalid storage config for %s: %w", baseCfg.Name, err)
- }
-
- configs = append(configs, cfg)
- }
-
- return configs, nil
+ return baseConfigs, nil
}
diff --git a/config/storage/minio.go b/config/storage/minio.go
deleted file mode 100644
index 8e9cd20a..00000000
--- a/config/storage/minio.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package storage
-
-import (
- "fmt"
-
- storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
-)
-
-type MinioStorageConfig struct {
- BaseConfig
- Endpoint string `toml:"endpoint" mapstructure:"endpoint" json:"endpoint"`
- AccessKeyID string `toml:"access_key_id" mapstructure:"access_key_id" json:"access_key_id"`
- SecretAccessKey string `toml:"secret_access_key" mapstructure:"secret_access_key" json:"secret_access_key"`
- BucketName string `toml:"bucket_name" mapstructure:"bucket_name" json:"bucket_name"`
- UseSSL bool `toml:"use_ssl" mapstructure:"use_ssl" json:"use_ssl"`
- BasePath string `toml:"base_path" mapstructure:"base_path" json:"base_path"`
-}
-
-func (m *MinioStorageConfig) Validate() error {
- if m.Endpoint == "" {
- return fmt.Errorf("endpoint is required for minio storage")
- }
- if m.AccessKeyID == "" || m.SecretAccessKey == "" {
- return fmt.Errorf("access_key_id and secret_access_key are required for minio storage")
- }
- if m.BucketName == "" {
- return fmt.Errorf("bucket_name is required for minio storage")
- }
- if m.BasePath == "" {
- return fmt.Errorf("base_path is required for minio storage")
- }
- return nil
-}
-
-func (m *MinioStorageConfig) GetType() storenum.StorageType {
- return storenum.Minio
-}
-
-func (m *MinioStorageConfig) GetName() string {
- return m.Name
-}
diff --git a/config/storage/s3.go b/config/storage/s3.go
index 31e7a921..6df39ed7 100644
--- a/config/storage/s3.go
+++ b/config/storage/s3.go
@@ -12,7 +12,6 @@ type S3StorageConfig struct {
AccessKeyID string `toml:"access_key_id" mapstructure:"access_key_id" json:"access_key_id"`
SecretAccessKey string `toml:"secret_access_key" mapstructure:"secret_access_key" json:"secret_access_key"`
BucketName string `toml:"bucket_name" mapstructure:"bucket_name" json:"bucket_name"`
- UseSSL bool `toml:"use_ssl" mapstructure:"use_ssl" json:"use_ssl"`
BasePath string `toml:"base_path" mapstructure:"base_path" json:"base_path"`
Region string `toml:"region" mapstructure:"region" json:"region"`
VirtualHost bool `toml:"virtual_host" mapstructure:"virtual_host" json:"virtual_host"`
diff --git a/config/storage/telegram.go b/config/storage/telegram.go
index c897f800..a64e9d81 100644
--- a/config/storage/telegram.go
+++ b/config/storage/telegram.go
@@ -3,6 +3,7 @@ package storage
import (
"fmt"
+ "github.com/gotd/td/constant"
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
)
@@ -19,12 +20,21 @@ type TelegramStorageConfig struct {
// only effective when SkipLarge is false
// use zip when splitting non-video files or when lossless video splitting is disabled/unavailable
SplitSizeMB int64 `toml:"split_size_mb" mapstructure:"split_size_mb" json:"split_size_mb"`
+ // Use metadata text as the caption when the live source caption is absent.
+ UseMetadataCaption bool `toml:"use_metadata_caption" mapstructure:"use_metadata_caption" json:"use_metadata_caption"`
+ // Use grouped_id from metadata to preserve source media groups when the live
+ // source group key is absent.
+ UseMetadataAlbum bool `toml:"use_metadata_album" mapstructure:"use_metadata_album" json:"use_metadata_album"`
}
func (m *TelegramStorageConfig) Validate() error {
if m.ChatID == 0 {
return fmt.Errorf("chat_id is required for telegram storage")
}
+ peerID := constant.TDLibPeerID(m.ChatID)
+ if !peerID.IsUser() && !peerID.IsChat() && !peerID.IsChannel() {
+ return fmt.Errorf("chat_id must be a Bot API style user or chat ID")
+ }
if m.RateLimit < 0 || m.RateBurst < 0 {
return fmt.Errorf("rate_limit and rate_burst must be greater than 0 for telegram storage")
}
diff --git a/config/storage/types.go b/config/storage/types.go
index 73d3bdc5..06915abb 100644
--- a/config/storage/types.go
+++ b/config/storage/types.go
@@ -6,13 +6,17 @@ import (
type StorageConfig interface {
Validate() error
+ GetID() uint
GetType() storenum.StorageType
GetName() string
}
type BaseConfig struct {
+ ID uint `toml:"-" mapstructure:"-" json:"-"`
Name string `toml:"name" mapstructure:"name" json:"name"`
Type string `toml:"type" mapstructure:"type" json:"type"`
Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
RawConfig map[string]any `toml:"-" mapstructure:",remain"`
}
+
+func (b BaseConfig) GetID() uint { return b.ID }
diff --git a/config/storage/validate.go b/config/storage/validate.go
new file mode 100644
index 00000000..c84e0b62
--- /dev/null
+++ b/config/storage/validate.go
@@ -0,0 +1,29 @@
+package storage
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+)
+
+// ValidateJSON rejects unknown or mistyped keys before a storage record is
+// persisted. Keeping this check at the boundary makes the stored JSON a
+// versioned, typed contract instead of an arbitrary map.
+func ValidateJSON(typ string, raw []byte) error {
+ st, err := storenum.ParseStorageType(typ)
+ if err != nil {
+ return err
+ }
+ cfg, err := newStorageConfig(st, BaseConfig{})
+ if err != nil {
+ return err
+ }
+ decoder := json.NewDecoder(bytes.NewReader(raw))
+ decoder.DisallowUnknownFields()
+ if err := decoder.Decode(cfg); err != nil {
+ return fmt.Errorf("invalid %s storage config: %w", st, err)
+ }
+ return nil
+}
diff --git a/config/tg.go b/config/tg.go
index 9b26e403..4b5973a1 100644
--- a/config/tg.go
+++ b/config/tg.go
@@ -1,21 +1,24 @@
package config
type telegramConfig struct {
- Token string `toml:"token" mapstructure:"token"`
+ Token string `toml:"token" mapstructure:"token" json:"token"`
AppID int `toml:"app_id" mapstructure:"app_id" json:"app_id"`
AppHash string `toml:"app_hash" mapstructure:"app_hash" json:"app_hash"`
- Proxy tgProxyConfig `toml:"proxy" mapstructure:"proxy"`
+ Proxy tgProxyConfig `toml:"proxy" mapstructure:"proxy" json:"proxy"`
RpcRetry int `toml:"rpc_retry" mapstructure:"rpc_retry" json:"rpc_retry"`
Userbot userbotConfig `toml:"userbot" mapstructure:"userbot" json:"userbot"`
MediaGroupTimeout int `toml:"media_group_timeout" mapstructure:"media_group_timeout" json:"media_group_timeout"`
+ RelayChatID int64 `toml:"relay_chat_id" mapstructure:"relay_chat_id" json:"relay_chat_id"`
+ RelayKeepMessages bool `toml:"relay_keep_messages" mapstructure:"relay_keep_messages" json:"relay_keep_messages"`
}
type userbotConfig struct {
- Enable bool `toml:"enable" mapstructure:"enable"`
- Session string `toml:"session" mapstructure:"session"`
+ Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
+ Session string `toml:"session" mapstructure:"session" json:"session"`
+ Phone string `toml:"phone" mapstructure:"phone" json:"phone"`
}
type tgProxyConfig struct {
- Enable bool `toml:"enable" mapstructure:"enable"`
- URL string `toml:"url" mapstructure:"url"`
+ Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
+ URL string `toml:"url" mapstructure:"url" json:"url"`
}
diff --git a/config/user.go b/config/user.go
index ac97e0e2..57fe5b37 100644
--- a/config/user.go
+++ b/config/user.go
@@ -2,34 +2,97 @@ package config
import (
"github.com/duke-git/lancet/v2/slice"
+
+ "github.com/krau/SaveAny-Bot/pkg/enums/storagefilter"
)
-type userConfig struct {
- ID int64 `toml:"id" mapstructure:"id" json:"id"` // telegram user id
- Storages []string `toml:"storages" mapstructure:"storages" json:"storages"` // storage names
- Blacklist bool `toml:"blacklist" mapstructure:"blacklist" json:"blacklist"` // 黑名单模式, storage names 中的存储将不会被使用, 默认为白名单模式
+type UserConfig struct {
+ ID int64 `toml:"id" mapstructure:"id" json:"id"` // telegram user id
+ Enabled bool `toml:"enabled" mapstructure:"enabled" json:"enabled"`
+ DefaultStorageID uint `toml:"default_storage_id" mapstructure:"default_storage_id" json:"default_storage_id"`
+ StorageFilterIDs []uint `toml:"storage_filter_ids" mapstructure:"storage_filter_ids" json:"storage_filter_ids"`
+ StorageFilterNames []string `toml:"storages" mapstructure:"storages" json:"-"` // legacy config import only
+ StorageFilterMode storagefilter.Mode `toml:"storage_filter_mode" mapstructure:"storage_filter_mode" json:"storage_filter_mode"`
+ // LegacyBlacklist is only used while importing a v0 config file. New
+ // database-backed settings always use StorageFilterMode.
+ LegacyBlacklist bool `toml:"blacklist" mapstructure:"blacklist" json:"-"`
}
-var userIDs []int64
-var storages []string
-var userStorages = make(map[int64][]string)
+// FilterMode resolves the storage filter mode, including the v0 blacklist
+// flag used during config import.
+func (u UserConfig) FilterMode() storagefilter.Mode {
+ if u.StorageFilterMode.Valid() {
+ return u.StorageFilterMode
+ }
+ if u.LegacyBlacklist {
+ return storagefilter.Blacklist
+ }
+ return storagefilter.Whitelist
+}
-func (c Config) GetStorageNamesByUserID(userID int64) []string {
- us, ok := userStorages[userID]
- if ok {
- return us
+func (u UserConfig) filterIDs(c Config) []uint {
+ if len(u.StorageFilterIDs) > 0 || len(u.StorageFilterNames) == 0 {
+ return u.StorageFilterIDs
+ }
+ ids := make([]uint, 0, len(u.StorageFilterNames))
+ for _, name := range u.StorageFilterNames {
+ if id := c.GetStorageIDByName(name); id != 0 {
+ ids = append(ids, id)
+ }
+ }
+ return ids
+}
+
+// GetStorageIDsByUserID returns the storage IDs accessible to a user.
+func (c Config) GetStorageIDsByUserID(userID int64) []uint {
+ for _, u := range c.Users {
+ if u.ID != userID || !u.Enabled {
+ continue
+ }
+ selected := u.filterIDs(c)
+ if u.FilterMode() == storagefilter.Whitelist {
+ return selected
+ }
+ selectedSet := make(map[uint]struct{}, len(selected))
+ for _, id := range selected {
+ selectedSet[id] = struct{}{}
+ }
+ ids := make([]uint, 0, len(c.Storages))
+ for _, st := range c.Storages {
+ if _, denied := selectedSet[st.GetID()]; !denied {
+ ids = append(ids, st.GetID())
+ }
+ }
+ return ids
}
return nil
}
+func (c Config) GetStorageNamesByUserID(userID int64) []string {
+ ids := c.GetStorageIDsByUserID(userID)
+ names := make([]string, 0, len(ids))
+ for _, id := range ids {
+ if name := c.GetStorageNameByID(id); name != "" {
+ names = append(names, name)
+ }
+ }
+ return names
+}
+
func (c Config) GetUsersID() []int64 {
- return userIDs
+ ids := make([]int64, 0, len(c.Users))
+ for _, u := range c.Users {
+ if u.Enabled {
+ ids = append(ids, u.ID)
+ }
+ }
+ return ids
+}
+
+func (c Config) HasStorageID(userID int64, storageID uint) bool {
+ return slice.Contain(c.GetStorageIDsByUserID(userID), storageID)
}
func (c Config) HasStorage(userID int64, storageName string) bool {
- us, ok := userStorages[userID]
- if !ok {
- return false
- }
- return slice.Contain(us, storageName)
+ return slice.Contain(c.GetStorageNamesByUserID(userID), storageName)
}
diff --git a/config/viper.go b/config/viper.go
deleted file mode 100644
index ad5b0fe2..00000000
--- a/config/viper.go
+++ /dev/null
@@ -1,236 +0,0 @@
-package config
-
-import (
- "context"
- "fmt"
- "net"
- "net/http"
- "net/url"
- "strings"
- "time"
-
- "github.com/charmbracelet/log"
- "github.com/duke-git/lancet/v2/slice"
- "github.com/krau/SaveAny-Bot/config/storage"
- "github.com/spf13/viper"
- "golang.org/x/net/proxy"
-)
-
-type Config struct {
- Lang string `toml:"lang" mapstructure:"lang" json:"lang"`
- Workers int `toml:"workers" mapstructure:"workers"`
- Retry int `toml:"retry" mapstructure:"retry"`
- NoCleanCache bool `toml:"no_clean_cache" mapstructure:"no_clean_cache" json:"no_clean_cache"`
- Threads int `toml:"threads" mapstructure:"threads" json:"threads"`
- Stream bool `toml:"stream" mapstructure:"stream" json:"stream"`
- Proxy string `toml:"proxy" mapstructure:"proxy" json:"proxy"`
- Log logConfig `toml:"log" mapstructure:"log" json:"log"`
- Aria2 aria2Config `toml:"aria2" mapstructure:"aria2" json:"aria2"`
- API apiConfig `toml:"api" mapstructure:"api" json:"api"`
-
- Cache cacheConfig `toml:"cache" mapstructure:"cache" json:"cache"`
- Users []userConfig `toml:"users" mapstructure:"users" json:"users"`
- Temp tempConfig `toml:"temp" mapstructure:"temp"`
- DB dbConfig `toml:"db" mapstructure:"db"`
- Telegram telegramConfig `toml:"telegram" mapstructure:"telegram"`
- Storages []storage.StorageConfig `toml:"-" mapstructure:"-" json:"storages"`
- Parser parserConfig `toml:"parser" mapstructure:"parser" json:"parser"`
- Hook hookConfig `toml:"hook" mapstructure:"hook" json:"hook"`
- Ytdlp YtdlpConfig `toml:"ytdlp" mapstructure:"ytdlp" json:"ytdlp"`
-}
-
-type aria2Config struct {
- Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
- Url string `toml:"url" mapstructure:"url" json:"url"`
- Secret string `toml:"secret" mapstructure:"secret" json:"secret"`
- KeepFile bool `toml:"keep_file" mapstructure:"keep_file" json:"keep_file"`
-}
-
-type apiConfig struct {
- Enable bool `toml:"enable" mapstructure:"enable" json:"enable"`
- Host string `toml:"host" mapstructure:"host" json:"host"`
- Port int `toml:"port" mapstructure:"port" json:"port"`
- Token string `toml:"token" mapstructure:"token" json:"token"`
-}
-
-var cfg = &Config{}
-
-func C() Config {
- return *cfg
-}
-
-func (c Config) GetStorageByName(name string) storage.StorageConfig {
- for _, storage := range c.Storages {
- if storage.GetName() == name {
- return storage
- }
- }
- return nil
-}
-
-func Init(ctx context.Context, configFile ...string) error {
- logger := log.FromContext(ctx)
-
- // Reset side tables for re-init.
- storages = nil
- userIDs = nil
- userStorages = make(map[int64][]string)
-
- viper.SetConfigType("toml")
- viper.SetEnvPrefix("SAVEANY")
- viper.AutomaticEnv()
- replacer := strings.NewReplacer(".", "_")
- viper.SetEnvKeyReplacer(replacer)
-
- // 配置文件支持传入一个 http(s) URL 地址
- loadedFromURL := false
- if len(configFile) > 0 && configFile[0] != "" {
- cfg := configFile[0]
- if strings.HasPrefix(cfg, "http://") || strings.HasPrefix(cfg, "https://") {
- client := &http.Client{Timeout: 30 * time.Second}
- resp, err := client.Get(cfg)
- if err != nil {
- return fmt.Errorf("failed to fetch remote config file: %w", err)
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("failed to fetch remote config file: status code %d", resp.StatusCode)
- }
- if err := viper.ReadConfig(resp.Body); err != nil {
- return fmt.Errorf("failed to read remote config file: %w", err)
- }
- loadedFromURL = true
- } else {
- viper.SetConfigFile(cfg)
- }
- } else {
- viper.SetConfigName("config")
- viper.AddConfigPath(".")
- viper.AddConfigPath("/etc/saveany/")
- }
-
- defaultConfigs := map[string]any{
- "lang": "zh-Hans",
- "workers": 3,
- "retry": 3,
- "threads": 4,
- "log.level": "debug",
-
- "cache.ttl": 86400,
- "cache.num_counters": 1e5,
- "cache.max_cost": 1e6,
-
- "telegram.app_id": 1025907,
- "telegram.app_hash": "452b0359b988148995f22ff0f4229750",
- "telegram.rpc_retry": 5,
- "telegram.userbot.enable": false,
- "telegram.userbot.session": "data/usersession.db",
-
- "temp.base_path": "cache/",
-
- "db.path": "data/saveany.db",
- "db.session": "data/session.db",
-
- "api.enable": false,
- "api.host": "0.0.0.0",
- "api.port": 8080,
- "api.token": "",
-
- "ytdlp.recode": "mp4",
- "ytdlp.filename_template": DefaultYtdlpFilenameTemplate,
- "ytdlp.max_height": 0,
- "ytdlp.format": "",
- "ytdlp.restrict_filenames": false,
- }
-
- for key, value := range defaultConfigs {
- viper.SetDefault(key, value)
- }
-
- if !loadedFromURL {
- if err := viper.ReadInConfig(); err != nil {
- logger.Errorf("Error reading config file: %v", err)
- return err
- }
- }
-
- if err := viper.Unmarshal(cfg); err != nil {
- logger.Errorf("Error unmarshalling config file: %v", err)
- return err
- }
-
- storagesConfig, err := storage.LoadStorageConfigs(viper.GetViper())
- if err != nil {
- return fmt.Errorf("error loading storage configs: %w", err)
- }
- cfg.Storages = storagesConfig
-
- storageNames := make(map[string]struct{})
- for _, storage := range cfg.Storages {
- if _, ok := storageNames[storage.GetName()]; ok {
- return fmt.Errorf("duplicate storage name: %s", storage.GetName())
- }
- storageNames[storage.GetName()] = struct{}{}
- }
-
- if cfg.Workers < 1 {
- cfg.Workers = 1
- }
- if cfg.Threads < 1 {
- cfg.Threads = 1
- }
- if cfg.Retry < 1 {
- cfg.Retry = 1
- }
-
- for _, storage := range cfg.Storages {
- storages = append(storages, storage.GetName())
- }
- for _, user := range cfg.Users {
- userIDs = append(userIDs, user.ID)
- if user.Blacklist {
- userStorages[user.ID] = slice.Compact(slice.Difference(storages, user.Storages))
- } else {
- userStorages[user.ID] = user.Storages
- }
- }
- if cfg.Proxy != "" {
- http.DefaultTransport, err = newProxyTransport(cfg.Proxy)
- if err != nil {
- return fmt.Errorf("failed to create proxy transport: %w", err)
- }
- }
- return nil
-}
-
-func newProxyTransport(proxyStr string) (*http.Transport, error) {
- proxyURL, err := url.Parse(proxyStr)
- if err != nil {
- return nil, err
- }
- transport := &http.Transport{
- ForceAttemptHTTP2: true,
- MaxIdleConns: 100,
- IdleConnTimeout: 90 * time.Second,
- TLSHandshakeTimeout: 10 * time.Second,
- ExpectContinueTimeout: 1 * time.Second,
- }
- switch proxyURL.Scheme {
- case "http", "https":
- transport.Proxy = http.ProxyURL(proxyURL)
-
- case "socks5", "socks5h":
- dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
- if err != nil {
- return nil, err
- }
- transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
- return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr)
- }
-
- default:
- return nil, fmt.Errorf("unsupported proxy type: %s", proxyURL.Scheme)
- }
-
- return transport, nil
-}
diff --git a/core/core.go b/core/core.go
index e10d0862..6574f6a9 100644
--- a/core/core.go
+++ b/core/core.go
@@ -2,19 +2,29 @@ package core
import (
"context"
+ "encoding/json"
"errors"
+ "fmt"
"sync"
+ "time"
"github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/taskstatus"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
"github.com/krau/SaveAny-Bot/pkg/queue"
"github.com/krau/SaveAny-Bot/pkg/taskevent"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
)
var (
queueOnce sync.Once
queueInstance *queue.TaskQueue[Executable]
+ workerWG sync.WaitGroup
+
+ taskSinkFactoryMu sync.RWMutex
+ taskSinkFactory func(Executable) taskevent.Sink
)
// initQueue lazily creates the shared task queue.
@@ -25,6 +35,12 @@ func initQueue() *queue.TaskQueue[Executable] {
return queueInstance
}
+const (
+ TaskSourceBot = "bot"
+ TaskSourceAPI = "api"
+ TaskSourceRetry = "retry"
+)
+
type Executable interface {
Type() tasktype.TaskType
Title() string
@@ -32,43 +48,144 @@ type Executable interface {
Execute(ctx context.Context) error
}
+// DestinationInfo lets task records include the storage target for every
+// creation path, including tasks started from Telegram handlers.
+type DestinationInfo interface {
+ StorageName() string
+ StoragePath() string
+}
+
+// ParamSource exposes the creation parameters that a queued task needs to be
+// rebuilt after a restart.
+type ParamSource interface {
+ TaskParams() (json.RawMessage, error)
+}
+
func worker(ctx context.Context, qe *queue.TaskQueue[Executable], semaphore chan struct{}) {
+ defer workerWG.Done()
logger := log.FromContext(ctx)
execHooks := config.C().Hook.Exec
for {
semaphore <- struct{}{}
qtask, err := qe.Get()
if err != nil {
- logger.Error("Failed to get task from queue:", err)
+ if !errors.Is(err, queue.ErrQueueClosed) {
+ logger.Error("Failed to get task from queue:", err)
+ }
break // queue closed and empty
}
exe := qtask.Data
taskCtx := qtask.Context()
+ recordCtx := context.WithoutCancel(taskCtx)
logger.Infof("Processing task: %s", exe.TaskID())
taskevent.Emit(taskCtx, taskevent.Event{TaskID: exe.TaskID(), Phase: taskevent.PhaseStart})
if err := ExecCommandString(taskCtx, execHooks.TaskBeforeStart); err != nil {
logger.Errorf("Failed to execute before start hook for task %s: %v", exe.TaskID(), err)
}
+ startedAt := time.Now()
+ if dbErr := database.UpdateTaskRecord(recordCtx, exe.TaskID(), map[string]any{
+ "status": taskstatus.Running.String(),
+ "started_at": startedAt,
+ }); dbErr != nil {
+ logger.Errorf("Failed to update task record for %s: %v", exe.TaskID(), dbErr)
+ }
err = exe.Execute(taskCtx)
+ items := collectItemResults(exe)
+ failedItems := countItems(items, ItemStatusFailed)
+ status := taskstatus.Completed.String()
if err != nil {
if errors.Is(taskCtx.Err(), context.Canceled) {
- logger.Infof("Task %s was canceled", exe.TaskID())
- if err := ExecCommandString(ctx, execHooks.TaskCancel); err != nil {
- logger.Errorf("Failed to execute cancel hook for task %s: %v", exe.TaskID(), err)
+ current, dbErr := database.GetTaskRecord(recordCtx, exe.TaskID())
+ if dbErr == nil && current.Status == taskstatus.Cancelled.String() {
+ status = taskstatus.Cancelled.String()
+ logger.Infof("Task %s was canceled", exe.TaskID())
+ if err := ExecCommandString(ctx, execHooks.TaskCancel); err != nil {
+ logger.Errorf("Failed to execute cancel hook for task %s: %v", exe.TaskID(), err)
+ }
+ } else {
+ // Process shutdown cancels task contexts; leave the record queued
+ // so the next start can resume it from persisted cache.
+ status = taskstatus.Queued.String()
+ logger.Infof("Task %s interrupted; it will resume after restart", exe.TaskID())
}
} else {
+ status = taskstatus.Failed.String()
logger.Errorf("Failed to execute task %s: %v", exe.TaskID(), err)
if err := ExecCommandString(ctx, execHooks.TaskFail); err != nil {
logger.Errorf("Failed to execute fail hook for task %s: %v", exe.TaskID(), err)
}
}
+ } else if failedItems > 0 {
+ status = taskstatus.Partial.String()
+ logger.Warnf("Task %s finished with %d failed files", exe.TaskID(), failedItems)
+ if err := ExecCommandString(ctx, execHooks.TaskFail); err != nil {
+ logger.Errorf("Failed to execute fail hook for task %s: %v", exe.TaskID(), err)
+ }
} else {
logger.Infof("Task %s completed successfully", exe.TaskID())
if err := ExecCommandString(ctx, execHooks.TaskSuccess); err != nil {
logger.Errorf("Failed to execute success hook for task %s: %v", exe.TaskID(), err)
}
}
- taskevent.Emit(taskCtx, taskevent.Event{TaskID: exe.TaskID(), Phase: taskevent.PhaseDone, Err: err})
+ if len(items) > 0 && status != taskstatus.Queued.String() {
+ records := make([]database.TaskItemRecord, 0, len(items))
+ for _, item := range items {
+ records = append(records, database.TaskItemRecord{
+ Index: item.Index,
+ Name: item.Name,
+ Path: item.Path,
+ StorageName: item.StorageName,
+ StorageID: item.StorageID,
+ Status: string(item.Status),
+ Error: item.Error,
+ Size: item.Size,
+ SourceChatID: item.SourceChatID,
+ SourceMsgID: item.SourceMessageID,
+ })
+ }
+ if dbErr := database.ReplaceTaskItems(recordCtx, exe.TaskID(), records); dbErr != nil {
+ logger.Errorf("Failed to store task items for %s: %v", exe.TaskID(), dbErr)
+ }
+ metadataRecords := make([]database.TaskItemMetadata, 0, len(items))
+ for _, item := range items {
+ if item.MetadataJSON == "" {
+ continue
+ }
+ metadataRecords = append(metadataRecords, database.TaskItemMetadata{
+ ItemIndex: item.Index,
+ Version: item.MetadataVersion,
+ SourceChatID: item.SourceChatID,
+ SourceMessageID: item.SourceMessageID,
+ JSON: item.MetadataJSON,
+ })
+ }
+ if dbErr := database.ReplaceTaskItemMetadata(recordCtx, exe.TaskID(), metadataRecords); dbErr != nil {
+ logger.Errorf("Failed to store task metadata for %s: %v", exe.TaskID(), dbErr)
+ }
+ }
+ recordFields := map[string]any{"status": status}
+ if status == taskstatus.Queued.String() {
+ recordFields["started_at"] = nil
+ recordFields["finished_at"] = nil
+ recordFields["error"] = ""
+ } else {
+ recordFields["finished_at"] = time.Now()
+ }
+ if status == taskstatus.Partial.String() {
+ recordFields["error"] = partialError(items)
+ } else if err != nil && status != taskstatus.Cancelled.String() {
+ recordFields["error"] = err.Error()
+ }
+ if dbErr := database.UpdateTaskRecord(recordCtx, exe.TaskID(), recordFields); dbErr != nil {
+ logger.Errorf("Failed to finish task record for %s: %v", exe.TaskID(), dbErr)
+ }
+ taskevent.Emit(taskCtx, taskevent.Event{
+ TaskID: exe.TaskID(),
+ Phase: taskevent.PhaseDone,
+ Err: err,
+ Partial: failedItems > 0 && err == nil,
+ Cancelled: status == taskstatus.Cancelled.String(),
+ })
qe.Done(qtask.ID)
<-semaphore
}
@@ -78,10 +195,11 @@ func Run(ctx context.Context) {
log.FromContext(ctx).Info("Start processing tasks...")
semaphore := make(chan struct{}, config.C().Workers)
q := initQueue()
- for range config.C().Workers {
+ workers := config.C().Workers
+ workerWG.Add(workers)
+ for range workers {
go worker(ctx, q, semaphore)
}
-
}
// Close stops the queue and unblocks workers in Get.
@@ -91,19 +209,180 @@ func Close() {
}
}
+// WaitWorkers waits up to timeout for all worker goroutines to exit.
+func WaitWorkers(timeout time.Duration) bool {
+ done := make(chan struct{})
+ go func() {
+ workerWG.Wait()
+ close(done)
+ }()
+ select {
+ case <-done:
+ return true
+ case <-time.After(timeout):
+ return false
+ }
+}
+
func AddTask(ctx context.Context, task Executable) error {
+ return AddTaskWithSource(ctx, task, TaskSourceBot)
+}
+
+// SetTaskSinkFactory installs the optional observer used for tasks started
+// outside the API. The API task path registers its own sink with webhook data.
+func SetTaskSinkFactory(factory func(Executable) taskevent.Sink) {
+ taskSinkFactoryMu.Lock()
+ taskSinkFactory = factory
+ taskSinkFactoryMu.Unlock()
+}
+
+func taskSinkFor(task Executable, source string) taskevent.Sink {
+ if source == TaskSourceAPI {
+ return nil
+ }
+ taskSinkFactoryMu.RLock()
+ factory := taskSinkFactory
+ taskSinkFactoryMu.RUnlock()
+ if factory == nil {
+ return nil
+ }
+ return factory(task)
+}
+
+// AddTaskWithSource records the task before enqueueing it so every execution
+// has a matching database row.
+func AddTaskWithSource(ctx context.Context, task Executable, source string) error {
+ logger := log.FromContext(ctx)
+ sink := taskSinkFor(task, source)
+ if sink != nil {
+ ctx = taskevent.WithSink(ctx, sink)
+ }
+ record := &database.TaskRecord{
+ TaskID: task.TaskID(),
+ Type: task.Type().String(),
+ Title: task.Title(),
+ Status: taskstatus.Queued.String(),
+ Source: source,
+ ParamsVersion: taskspec.Version,
+ }
+ if destination, ok := task.(DestinationInfo); ok {
+ record.StorageName = destination.StorageName()
+ record.StorageID = config.C().GetStorageIDByName(record.StorageName)
+ record.Path = destination.StoragePath()
+ }
+ if source, ok := task.(ParamSource); ok {
+ params, err := source.TaskParams()
+ if err != nil {
+ logger.Warnf("Failed to serialize params for task %s: %v", task.TaskID(), err)
+ } else {
+ record.Params = string(params)
+ }
+ }
+ if err := database.CreateTaskRecord(ctx, record); err != nil {
+ logger.Errorf("Failed to create task record for %s: %v", task.TaskID(), err)
+ }
+ if err := initQueue().Add(queue.NewTask(ctx, task.TaskID(), task.Title(), task)); err != nil {
+ if sink != nil {
+ sink.Emit(taskevent.Event{TaskID: task.TaskID(), Phase: taskevent.PhaseDone, Err: err})
+ }
+ if dbErr := database.DeleteTaskRecord(ctx, task.TaskID()); dbErr != nil {
+ logger.Errorf("Failed to remove task record for %s: %v", task.TaskID(), dbErr)
+ }
+ return err
+ }
+ return nil
+}
+
+func EnrichTaskRecord(ctx context.Context, taskID, storageName, storagePath string) error {
+ return database.UpdateTaskRecord(ctx, taskID, map[string]any{
+ "storage_name": storageName,
+ "storage_id": config.C().GetStorageIDByName(storageName),
+ "path": storagePath,
+ })
+}
+
+// IsTaskRunning reports whether the task is still being executed, including
+// the window between cancellation and the worker persisting its results.
+func IsTaskRunning(id string) bool {
+ return initQueue().IsRunning(id)
+}
+
+// HasTask reports whether the task is known to the queue, queued or running.
+func HasTask(id string) bool {
+ return initQueue().Has(id)
+}
+
+// EnqueueExisting adds a rebuilt task without creating another record. The
+// progress sink factory is attached so the web console still tracks it.
+func EnqueueExisting(ctx context.Context, task Executable) error {
+ if sink := taskSinkFor(task, TaskSourceBot); sink != nil {
+ ctx = taskevent.WithSink(ctx, sink)
+ }
return initQueue().Add(queue.NewTask(ctx, task.TaskID(), task.Title(), task))
}
func CancelTask(ctx context.Context, id string) error {
- err := queueInstance.CancelTask(id)
- return err
+ q := initQueue()
+ if q.Has(id) {
+ task, _ := q.GetTask(id)
+ // Persist the cancellation before canceling the context so the worker
+ // can distinguish it from a process shutdown and does not requeue it.
+ if dbErr := database.UpdateTaskRecord(context.WithoutCancel(ctx), id, map[string]any{
+ "status": taskstatus.Cancelled.String(),
+ "finished_at": time.Now(),
+ }); dbErr != nil {
+ log.FromContext(ctx).Errorf("Failed to cancel task record for %s: %v", id, dbErr)
+ }
+ err := q.CancelTask(id)
+ if err == nil && task != nil {
+ taskevent.Emit(task.Context(), taskevent.Event{
+ TaskID: id,
+ Phase: taskevent.PhaseDone,
+ Cancelled: true,
+ })
+ }
+ return err
+ }
+ // A queued record may not be in the queue yet while restart recovery waits
+ // for a Telegram client.
+ record, err := database.GetTaskRecord(ctx, id)
+ if err != nil {
+ return fmt.Errorf("task %s does not exist", id)
+ }
+ if isTerminalTaskStatus(record.Status) {
+ return fmt.Errorf("task %s is not active", id)
+ }
+ if dbErr := database.UpdateTaskRecord(context.WithoutCancel(ctx), id, map[string]any{
+ "status": taskstatus.Cancelled.String(),
+ "finished_at": time.Now(),
+ }); dbErr != nil {
+ return dbErr
+ }
+ if q.Has(id) {
+ return q.CancelTask(id)
+ }
+ return nil
+}
+
+func isTerminalTaskStatus(status string) bool {
+ return taskstatus.Status(status).IsTerminal()
}
func GetLength(ctx context.Context) int {
+ if queueInstance == nil {
+ return 0
+ }
return queueInstance.ActiveLength()
}
+// ActiveTaskCount includes queued and running tasks regardless of cancellation.
+func ActiveTaskCount() int {
+ if queueInstance == nil {
+ return 0
+ }
+ return queueInstance.ActiveCount()
+}
+
func GetRunningTasks(ctx context.Context) []queue.TaskInfo {
return queueInstance.RunningTasks()
}
diff --git a/core/result.go b/core/result.go
new file mode 100644
index 00000000..67f881de
--- /dev/null
+++ b/core/result.go
@@ -0,0 +1,59 @@
+package core
+
+import "fmt"
+
+// ItemStatus is the final state of one file inside a task.
+type ItemStatus string
+
+const (
+ ItemStatusCompleted ItemStatus = "completed"
+ ItemStatusFailed ItemStatus = "failed"
+ ItemStatusSkipped ItemStatus = "skipped"
+ ItemStatusCancelled ItemStatus = "cancelled"
+)
+
+// ItemResult describes the outcome of one file in a task. Tasks that handle
+// multiple files implement ResultReporter so the outcome survives after the
+// task leaves memory.
+type ItemResult struct {
+ Index int
+ Name string
+ Path string
+ StorageName string
+ StorageID uint
+ Status ItemStatus
+ Error string
+ Size int64
+ SourceChatID int64
+ SourceMessageID int
+ MetadataJSON string
+ MetadataVersion int
+}
+
+// ResultReporter exposes per-file outcomes after execution.
+type ResultReporter interface {
+ ItemResults() []ItemResult
+}
+
+func collectItemResults(task Executable) []ItemResult {
+ reporter, ok := task.(ResultReporter)
+ if !ok {
+ return nil
+ }
+ return reporter.ItemResults()
+}
+
+func countItems(items []ItemResult, status ItemStatus) int {
+ count := 0
+ for _, item := range items {
+ if item.Status == status {
+ count++
+ }
+ }
+ return count
+}
+
+func partialError(items []ItemResult) string {
+ failed := countItems(items, ItemStatusFailed)
+ return fmt.Sprintf("%d of %d files failed", failed, len(items))
+}
diff --git a/core/resume/resume.go b/core/resume/resume.go
new file mode 100644
index 00000000..227f7c11
--- /dev/null
+++ b/core/resume/resume.go
@@ -0,0 +1,429 @@
+// Package resume rebuilds tasks that were queued or running when the process
+// stopped. Task creation parameters are persisted with the task record, so a
+// task can be re-enqueued without resubmitting the original request.
+package resume
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/client/bot"
+ userclient "github.com/krau/SaveAny-Bot/client/user"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/core/tasks/aria2dl"
+ "github.com/krau/SaveAny-Bot/core/tasks/batchtfile"
+ "github.com/krau/SaveAny-Bot/core/tasks/directlinks"
+ "github.com/krau/SaveAny-Bot/core/tasks/parsed"
+ "github.com/krau/SaveAny-Bot/core/tasks/telegraph"
+ "github.com/krau/SaveAny-Bot/core/tasks/tfile"
+ "github.com/krau/SaveAny-Bot/core/tasks/transfer"
+ "github.com/krau/SaveAny-Bot/core/tasks/ytdlp"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/aria2"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+ "github.com/krau/SaveAny-Bot/pkg/enums/taskstatus"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/storagetypes"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+ tphclient "github.com/krau/SaveAny-Bot/pkg/telegraph"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/ext"
+)
+
+const retryInterval = 15 * time.Second
+
+// ErrNeedsClient means the task needs a Telegram client that is not running
+// yet. The record stays queued and is retried later.
+var ErrNeedsClient = errors.New("telegram client not available")
+
+// ErrAlreadyDone means every file in the task was uploaded before the
+// process stopped. The record only needs to be marked completed.
+var ErrAlreadyDone = errors.New("task already completed")
+
+var scanMu sync.Mutex
+
+// Run resumes queued tasks and keeps retrying while some of them are waiting
+// for a Telegram client.
+func Run(ctx context.Context) {
+ for {
+ pending := runOnce(ctx)
+ if pending == 0 {
+ return
+ }
+ select {
+ case <-ctx.Done():
+ return
+ case <-time.After(retryInterval):
+ }
+ }
+}
+
+func runOnce(ctx context.Context) (pending int) {
+ scanMu.Lock()
+ defer scanMu.Unlock()
+
+ logger := log.FromContext(ctx).WithPrefix("resume")
+ records, err := database.ListQueuedTaskRecords(ctx)
+ if err != nil {
+ logger.Errorf("Failed to list queued tasks: %v", err)
+ return 1
+ }
+ for i := range records {
+ taskID := records[i].TaskID
+ if core.HasTask(taskID) {
+ continue
+ }
+ record, err := database.GetTaskRecord(ctx, taskID)
+ if err != nil || record.Status != taskstatus.Queued.String() {
+ continue
+ }
+ task, taskCtx, err := rebuild(ctx, record)
+ switch {
+ case errors.Is(err, ErrNeedsClient):
+ pending++
+ logger.Debugf("Task %s waits for a telegram client", taskID)
+ continue
+ case errors.Is(err, ErrAlreadyDone):
+ if dbErr := database.UpdateTaskRecord(ctx, taskID, map[string]any{
+ "status": taskstatus.Completed.String(),
+ "error": "",
+ "finished_at": time.Now(),
+ }); dbErr != nil {
+ logger.Errorf("Failed to mark completed task %s: %v", taskID, dbErr)
+ } else {
+ logger.Infof("Task %s was already completed before restart", taskID)
+ }
+ continue
+ case err != nil:
+ logger.Warnf("Cannot resume task %s: %v", taskID, err)
+ if dbErr := database.UpdateTaskRecord(ctx, taskID, map[string]any{
+ "status": taskstatus.Failed.String(),
+ "error": "cannot resume: " + err.Error(),
+ "finished_at": time.Now(),
+ }); dbErr != nil {
+ logger.Errorf("Failed to mark task %s as failed: %v", taskID, dbErr)
+ }
+ continue
+ }
+ if err := core.EnqueueExisting(taskCtx, task); err != nil {
+ logger.Errorf("Failed to enqueue resumed task %s: %v", taskID, err)
+ pending++
+ continue
+ }
+ // The task may have been cancelled while it was being rebuilt.
+ if current, err := database.GetTaskRecord(ctx, taskID); err == nil && current.Status == taskstatus.Cancelled.String() {
+ if err := core.CancelTask(ctx, taskID); err != nil {
+ logger.Errorf("Failed to cancel resumed task %s: %v", taskID, err)
+ }
+ continue
+ }
+ logger.Infof("Resumed queued task %s", taskID)
+ }
+ return pending
+}
+
+func rebuild(ctx context.Context, record *database.TaskRecord) (core.Executable, context.Context, error) {
+ if record.Params == "" {
+ return nil, ctx, errors.New("missing task parameters")
+ }
+ if record.ParamsVersion > taskspec.Version {
+ return nil, ctx, fmt.Errorf("unsupported task parameter version %d", record.ParamsVersion)
+ }
+ switch tasktype.TaskType(record.Type) {
+ case tasktype.TaskTypeDirectlinks:
+ var spec taskspec.DirectLinks
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ stor, err := resolveStorage(ctx, record.StorageName, record.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ taskCtx, err := attachTelegramClient(ctx, stor)
+ if err != nil {
+ return nil, ctx, err
+ }
+ target := strings.TrimSuffix(record.Path, "/")
+ if spec.Path != nil {
+ target = *spec.Path
+ }
+ return directlinks.NewTask(record.TaskID, taskCtx, spec.URLs, stor, target, nil), taskCtx, nil
+ case tasktype.TaskTypeYtdlp:
+ var spec taskspec.YTDLP
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ stor, err := resolveStorage(ctx, record.StorageName, record.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ taskCtx, err := attachTelegramClient(ctx, stor)
+ if err != nil {
+ return nil, ctx, err
+ }
+ return ytdlp.NewTask(record.TaskID, taskCtx, spec.URLs, spec.Flags, stor, record.Path, nil), taskCtx, nil
+ case tasktype.TaskTypeAria2:
+ var spec taskspec.Aria2
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ stor, err := resolveStorage(ctx, record.StorageName, record.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ taskCtx, err := attachTelegramClient(ctx, stor)
+ if err != nil {
+ return nil, ctx, err
+ }
+ cfg := config.C().Aria2
+ if !cfg.Enable {
+ return nil, ctx, errors.New("aria2 is not enabled")
+ }
+ aria2Client, err := aria2.NewClient(cfg.Url, cfg.Secret)
+ if err != nil {
+ return nil, ctx, fmt.Errorf("failed to create aria2 client: %w", err)
+ }
+ gid := spec.GID
+ if gid == "" {
+ gid, err = aria2Client.AddURI(ctx, spec.URIs, nil)
+ if err != nil {
+ return nil, ctx, fmt.Errorf("failed to add aria2 task: %w", err)
+ }
+ } else if _, err := aria2Client.TellStatus(ctx, gid); err != nil {
+ gid, err = aria2Client.AddURI(ctx, spec.URIs, nil)
+ if err != nil {
+ return nil, ctx, fmt.Errorf("failed to add aria2 task: %w", err)
+ }
+ }
+ return aria2dl.NewTask(record.TaskID, taskCtx, gid, spec.URIs, aria2Client, stor, record.Path, nil), taskCtx, nil
+ case tasktype.TaskTypeParseditem:
+ var spec taskspec.Parsed
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ if spec.Item == nil {
+ return nil, ctx, errors.New("missing parsed item")
+ }
+ stor, err := resolveStorage(ctx, record.StorageName, record.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ taskCtx, err := attachTelegramClient(ctx, stor)
+ if err != nil {
+ return nil, ctx, err
+ }
+ return parsed.NewTask(record.TaskID, taskCtx, stor, record.Path, spec.Item, nil), taskCtx, nil
+ case tasktype.TaskTypeTphpics:
+ var spec taskspec.Telegraph
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ if len(spec.Pics) == 0 {
+ return nil, ctx, errors.New("missing telegraph images")
+ }
+ stor, err := resolveStorage(ctx, record.StorageName, record.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ taskCtx, err := attachTelegramClient(ctx, stor)
+ if err != nil {
+ return nil, ctx, err
+ }
+ return telegraph.NewTask(record.TaskID, taskCtx, spec.PhPath, spec.Pics, stor, record.Path, tphclient.NewClient(), nil), taskCtx, nil
+ case tasktype.TaskTypeTransfer:
+ var spec taskspec.Transfer
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ elems := make([]transfer.TaskElement, 0, len(spec.Items))
+ storages := make([]storage.Storage, 0, len(spec.Items)*2)
+ for _, item := range spec.Items {
+ sourceStor, err := resolveStorage(ctx, item.SourceStorage, item.SourceStorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ targetStor, err := resolveStorage(ctx, item.TargetStorage, item.TargetStorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ elem := transfer.NewTaskElement(sourceStor, storagetypes.FileInfo{
+ Name: item.Name,
+ Path: item.SourcePath,
+ Size: item.Size,
+ ModTime: item.ModTime,
+ }, targetStor, item.TargetPath)
+ elems = append(elems, *elem)
+ storages = append(storages, sourceStor, targetStor)
+ }
+ if len(elems) == 0 {
+ return nil, ctx, errors.New("missing transfer files")
+ }
+ taskCtx, err := attachTelegramClient(ctx, storages...)
+ if err != nil {
+ return nil, ctx, err
+ }
+ return transfer.NewTransferTask(record.TaskID, taskCtx, elems, nil, true), taskCtx, nil
+ case tasktype.TaskTypeTgfiles:
+ return rebuildTG(ctx, record)
+ default:
+ return nil, ctx, fmt.Errorf("unsupported task type %q", record.Type)
+ }
+}
+
+func rebuildTG(ctx context.Context, record *database.TaskRecord) (core.Executable, context.Context, error) {
+ var spec taskspec.TGFile
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return nil, ctx, fmt.Errorf("invalid parameters: %w", err)
+ }
+ if len(spec.Items) == 0 {
+ return nil, ctx, errors.New("missing telegram file items")
+ }
+ done := make(map[string]struct{}, len(spec.Done))
+ for _, id := range spec.Done {
+ done[id] = struct{}{}
+ }
+ remaining := 0
+ for _, item := range spec.Items {
+ if _, ok := done[item.ID]; item.ID == "" || !ok {
+ remaining++
+ }
+ }
+ if remaining == 0 {
+ return nil, ctx, ErrAlreadyDone
+ }
+ candidates := make([]*ext.Context, 0, 2)
+ for _, candidate := range []*ext.Context{bot.ExtContext(), userclient.GetCtx()} {
+ if candidate != nil {
+ candidates = append(candidates, candidate)
+ }
+ }
+ if len(candidates) == 0 {
+ return nil, ctx, ErrNeedsClient
+ }
+ elems := make([]batchtfile.TaskElement, 0, len(spec.Items))
+ var taskExt *ext.Context
+ for _, item := range spec.Items {
+ if _, ok := done[item.ID]; item.ID != "" && ok {
+ continue
+ }
+ stor, err := resolveStorage(ctx, item.Storage, item.StorageID)
+ if err != nil {
+ return nil, ctx, err
+ }
+ message, clientCtx, err := tgutil.GetMessageByIDFreshAny(candidates, item.ChatID, item.MessageID)
+ if err != nil {
+ if clientPending() {
+ return nil, ctx, ErrNeedsClient
+ }
+ return nil, ctx, fmt.Errorf("failed to fetch source message %d: %w", item.MessageID, err)
+ }
+ if message.Media == nil {
+ return nil, ctx, fmt.Errorf("source message %d has no media", item.MessageID)
+ }
+ file, err := tfilepkg.FromMediaMessage(message.Media, clientCtx.Raw, message)
+ if err != nil {
+ return nil, ctx, fmt.Errorf("failed to rebuild file %s: %w", item.Name, err)
+ }
+ file.SetName(item.Name)
+ var elem *batchtfile.TaskElement
+ if item.ID != "" {
+ elem, err = batchtfile.NewTaskElementWithID(item.ID, stor, item.Path, file)
+ } else {
+ elem, err = batchtfile.NewTaskElement(stor, item.Path, file)
+ }
+ if err != nil {
+ return nil, ctx, fmt.Errorf("failed to create task element for %s: %w", item.Name, err)
+ }
+ elems = append(elems, *elem)
+ if taskExt == nil {
+ taskExt = clientCtx
+ }
+ }
+ taskCtx := tgutil.ExtWithContext(ctx, taskExt)
+ if spec.Overwrite {
+ taskCtx = storage.WithOverwrite(taskCtx)
+ }
+ if len(spec.Items) == 1 {
+ task, err := tfile.NewTGFileTask(record.TaskID, taskCtx, elems[0].File, elems[0].Storage, elems[0].Path, nil)
+ if err != nil {
+ return nil, ctx, err
+ }
+ task.ElementID = elems[0].ID
+ task.Done = spec.Done
+ return task, taskCtx, nil
+ }
+ task := batchtfile.NewBatchTGFileTask(record.TaskID, taskCtx, elems, nil, true)
+ task.Done = spec.Done
+ return task, taskCtx, nil
+}
+
+// activeClient returns the first running Telegram client.
+func activeClient() *ext.Context {
+ if botCtx := bot.ExtContext(); botCtx != nil {
+ return botCtx
+ }
+ return userclient.GetCtx()
+}
+
+// attachTelegramClient adds a client context when the task writes to Telegram.
+// A Telegram destination without a running client must wait for one.
+func attachTelegramClient(ctx context.Context, storages ...storage.Storage) (context.Context, error) {
+ needsTelegram := false
+ for _, stor := range storages {
+ if stor != nil && stor.Type() == storenum.Telegram {
+ needsTelegram = true
+ break
+ }
+ }
+ client := activeClient()
+ if client == nil {
+ if needsTelegram {
+ return ctx, ErrNeedsClient
+ }
+ return ctx, nil
+ }
+ return tgutil.ExtWithContext(ctx, client), nil
+}
+
+// clientPending reports whether a configured Telegram client is still starting
+// or not logged in, so a fetch failure should be retried instead of failing
+// the task permanently.
+func clientPending() bool {
+ cfg := config.C().Telegram
+ if cfg.Token != "" && bot.CurrentStatus().State != "running" {
+ return true
+ }
+ if cfg.Userbot.Enable && userclient.CurrentStatus().State != "running" {
+ return true
+ }
+ return false
+}
+
+func resolveStorage(ctx context.Context, name string, id uint) (storage.Storage, error) {
+ if id != 0 {
+ stor, err := storage.GetStorageByID(ctx, id)
+ if err == nil {
+ return stor, nil
+ }
+ if name == "" {
+ return nil, err
+ }
+ }
+ if name == "" {
+ return nil, errors.New("storage is empty")
+ }
+ stor, ok := storage.GetStorage(name)
+ if !ok {
+ return nil, fmt.Errorf("storage %q is not available", name)
+ }
+ return stor, nil
+}
diff --git a/core/retry/retry.go b/core/retry/retry.go
new file mode 100644
index 00000000..0b47b622
--- /dev/null
+++ b/core/retry/retry.go
@@ -0,0 +1,172 @@
+// Package retry re-creates a task from its persisted item records, so files
+// that failed (for example because the disk was full) can be downloaded again
+// without resubmitting the original message.
+package retry
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "path"
+
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/core/tasks/batchtfile"
+ "github.com/krau/SaveAny-Bot/core/tasks/tfile"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/taskstatus"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+ "github.com/krau/mygotg/ext"
+ "github.com/rs/xid"
+)
+
+var (
+ ErrTaskActive = errors.New("task is still active")
+ ErrNothingToRetry = errors.New("task has no failed files to retry")
+ ErrUnsupportedType = errors.New("retry is only supported for telegram file tasks")
+ ErrStorageMissing = errors.New("target storage is no longer available")
+ ErrSourceUnavailable = errors.New("failed file has no source message to retry")
+ ErrClientUnavailable = errors.New("no telegram client available")
+)
+
+// FailedTask enqueues a new task containing the failed (or unfinished) files
+// of taskID and returns the new task ID. Messages are fetched fresh so their
+// file references are current. extCtxs are tried in order (bot first, then
+// userbot) to match the client that can see the source chat.
+func FailedTask(ctx context.Context, extCtxs []*ext.Context, taskID string) (string, int, error) {
+ record, err := database.GetTaskRecord(ctx, taskID)
+ if err != nil {
+ return "", 0, fmt.Errorf("task not found: %w", err)
+ }
+ if core.IsTaskRunning(taskID) {
+ return "", 0, ErrTaskActive
+ }
+ if !isTerminalStatus(record.Status) {
+ return "", 0, ErrTaskActive
+ }
+ if record.Type != tasktype.TaskTypeTgfiles.String() {
+ return "", 0, ErrUnsupportedType
+ }
+ items, err := database.ListTaskItems(ctx, taskID)
+ if err != nil {
+ return "", 0, fmt.Errorf("failed to load task items: %w", err)
+ }
+ selected := retryableItems(items)
+ if len(selected) == 0 {
+ return "", 0, ErrNothingToRetry
+ }
+ candidates := make([]*ext.Context, 0, len(extCtxs))
+ for _, candidate := range extCtxs {
+ if candidate != nil {
+ candidates = append(candidates, candidate)
+ }
+ }
+ if len(candidates) == 0 {
+ return "", 0, ErrClientUnavailable
+ }
+
+ elems := make([]batchtfile.TaskElement, 0, len(selected))
+ var taskExt *ext.Context
+ for _, item := range selected {
+ if item.SourceChatID == 0 || item.SourceMsgID == 0 {
+ return "", 0, fmt.Errorf("%w: %s", ErrSourceUnavailable, item.Name)
+ }
+ msg, clientCtx, err := tgutil.GetMessageByIDFreshAny(candidates, item.SourceChatID, item.SourceMsgID)
+ if err != nil {
+ return "", 0, fmt.Errorf("%w: %s: %v", ErrSourceUnavailable, item.Name, err)
+ }
+ if taskExt == nil {
+ taskExt = clientCtx
+ }
+ if msg.Media == nil {
+ return "", 0, fmt.Errorf("%w: %s", ErrSourceUnavailable, item.Name)
+ }
+ stor, err := itemStorage(ctx, item, record)
+ if err != nil {
+ return "", 0, err
+ }
+ file, err := tfilepkg.FromMediaMessage(msg.Media, clientCtx.Raw, msg)
+ if err != nil {
+ return "", 0, fmt.Errorf("failed to rebuild file %s: %w", item.Name, err)
+ }
+ file.SetName(item.Name)
+ target := item.Path
+ if target == "" {
+ target = path.Join(record.Path, item.Name)
+ }
+ elem, err := batchtfile.NewTaskElement(stor, target, file)
+ if err != nil {
+ return "", 0, fmt.Errorf("failed to create task element for %s: %w", item.Name, err)
+ }
+ elems = append(elems, *elem)
+ }
+
+ newID := xid.New().String()
+ // A failed item may have left a partial file at its destination; the retry
+ // must replace it instead of writing a renamed duplicate.
+ taskCtx := storage.WithOverwrite(tgutil.ExtWithContext(ctx, taskExt))
+ var task core.Executable
+ if len(elems) == 1 {
+ single, err := tfile.NewTGFileTask(newID, taskCtx, elems[0].File, elems[0].Storage, elems[0].Path, nil)
+ if err != nil {
+ return "", 0, fmt.Errorf("failed to create retry task: %w", err)
+ }
+ task = single
+ } else {
+ task = batchtfile.NewBatchTGFileTask(newID, taskCtx, elems, nil, true)
+ }
+ if err := core.AddTaskWithSource(taskCtx, task, core.TaskSourceRetry); err != nil {
+ return "", 0, fmt.Errorf("failed to enqueue retry task: %w", err)
+ }
+ return newID, len(elems), nil
+}
+
+func itemStorage(ctx context.Context, item database.TaskItemRecord, record *database.TaskRecord) (storage.Storage, error) {
+ id := item.StorageID
+ if id == 0 {
+ id = record.StorageID
+ }
+ if id != 0 {
+ if stor, err := storage.GetStorageByID(ctx, id); err == nil {
+ return stor, nil
+ }
+ }
+ name := item.StorageName
+ if name == "" {
+ name = record.StorageName
+ }
+ stor, ok := storage.GetStorage(name)
+ if !ok {
+ return nil, fmt.Errorf("%w: %s", ErrStorageMissing, name)
+ }
+ return stor, nil
+}
+
+// retryableItems prefers failed files; if a task failed before per-item
+// outcomes were recorded, unfinished items are retried instead.
+func retryableItems(items []database.TaskItemRecord) []database.TaskItemRecord {
+ failed := make([]database.TaskItemRecord, 0, len(items))
+ for _, item := range items {
+ if item.Status == string(core.ItemStatusFailed) {
+ failed = append(failed, item)
+ }
+ }
+ if len(failed) > 0 {
+ return failed
+ }
+ for _, item := range items {
+ switch item.Status {
+ case string(core.ItemStatusCompleted), string(core.ItemStatusSkipped):
+ continue
+ default:
+ failed = append(failed, item)
+ }
+ }
+ return failed
+}
+
+func isTerminalStatus(status string) bool {
+ return taskstatus.Status(status).IsTerminal()
+}
diff --git a/core/task_params.go b/core/task_params.go
new file mode 100644
index 00000000..38f0b386
--- /dev/null
+++ b/core/task_params.go
@@ -0,0 +1,50 @@
+package core
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "sync"
+
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var taskParamsMu sync.Mutex
+
+// MarkTaskElementDone records an uploaded Telegram file element in the task's
+// persisted parameters so a restart can skip it.
+func MarkTaskElementDone(ctx context.Context, taskID, elementID string) error {
+ if elementID == "" {
+ return errors.New("empty task element ID")
+ }
+ taskParamsMu.Lock()
+ defer taskParamsMu.Unlock()
+
+ record, err := database.GetTaskRecord(ctx, taskID)
+ if err != nil {
+ return err
+ }
+ if record.Type != tasktype.TaskTypeTgfiles.String() {
+ return errors.New("task is not a telegram file task")
+ }
+ if record.Params == "" {
+ return errors.New("task has no persisted parameters")
+ }
+ var spec taskspec.TGFile
+ if err := json.Unmarshal([]byte(record.Params), &spec); err != nil {
+ return err
+ }
+ for _, id := range spec.Done {
+ if id == elementID {
+ return nil
+ }
+ }
+ spec.Done = append(spec.Done, elementID)
+ raw, err := json.Marshal(spec)
+ if err != nil {
+ return err
+ }
+ return database.UpdateTaskRecord(ctx, taskID, map[string]any{"params": string(raw)})
+}
diff --git a/core/tasks/aria2dl/execute.go b/core/tasks/aria2dl/execute.go
index 1dfdd492..1739f241 100644
--- a/core/tasks/aria2dl/execute.go
+++ b/core/tasks/aria2dl/execute.go
@@ -49,7 +49,9 @@ func (t *Task) Execute(ctx context.Context) error {
t.Progress.OnDone(ctx, t, nil)
}
- if _, err := t.Aria2Client.RemoveDownloadResult(context.Background(), t.GID); err != nil {
+ cleanupCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ if _, err := t.Aria2Client.RemoveDownloadResult(cleanupCtx, t.GID); err != nil {
logger.Warnf("Failed to remove aria2 download result: %v", err)
}
diff --git a/core/tasks/aria2dl/params.go b/core/tasks/aria2dl/params.go
new file mode 100644
index 00000000..2f257de1
--- /dev/null
+++ b/core/tasks/aria2dl/params.go
@@ -0,0 +1,14 @@
+package aria2dl
+
+import (
+ "encoding/json"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ return json.Marshal(taskspec.Aria2{GID: t.GID, URIs: t.URIs})
+}
diff --git a/core/tasks/aria2dl/progress.go b/core/tasks/aria2dl/progress.go
index 6a254675..37e5b076 100644
--- a/core/tasks/aria2dl/progress.go
+++ b/core/tasks/aria2dl/progress.go
@@ -56,23 +56,27 @@ func (p *Progress) OnStart(ctx context.Context, task *Task) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(task.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(task.TaskID()))
ext.EditMessage(p.chatID, req)
}
// OnProgress implements ProgressTracker.
func (p *Progress) OnProgress(ctx context.Context, task *Task, status *aria2.Status) {
- totalLength, _ := strconv.ParseInt(status.TotalLength, 10, 64)
- completedLength, _ := strconv.ParseInt(status.CompletedLength, 10, 64)
- downloadSpeed, _ := strconv.ParseInt(status.DownloadSpeed, 10, 64)
+ totalLength, err := strconv.ParseInt(status.TotalLength, 10, 64)
+ if err != nil {
+ log.FromContext(ctx).Warnf("Invalid aria2 total length %q: %v", status.TotalLength, err)
+ return
+ }
+ completedLength, err := strconv.ParseInt(status.CompletedLength, 10, 64)
+ if err != nil {
+ log.FromContext(ctx).Warnf("Invalid aria2 completed length %q: %v", status.CompletedLength, err)
+ return
+ }
+ downloadSpeed, err := strconv.ParseInt(status.DownloadSpeed, 10, 64)
+ if err != nil {
+ log.FromContext(ctx).Warnf("Invalid aria2 download speed %q: %v", status.DownloadSpeed, err)
+ return
+ }
if totalLength == 0 {
return
@@ -109,15 +113,7 @@ func (p *Progress) OnProgress(ctx context.Context, task *Task, status *aria2.Sta
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(task.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(task.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.chatID, req)
diff --git a/core/tasks/aria2dl/task.go b/core/tasks/aria2dl/task.go
index 0e09be16..01e88d14 100644
--- a/core/tasks/aria2dl/task.go
+++ b/core/tasks/aria2dl/task.go
@@ -11,6 +11,7 @@ import (
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
ID string
@@ -28,6 +29,17 @@ func (t *Task) Title() string {
return fmt.Sprintf("[%s](Aria2 GID:%s->%s:%s)", t.Type(), t.GID, t.Storage.Name(), t.StorPath)
}
+func (t *Task) StorageName() string {
+ if t.Storage == nil {
+ return ""
+ }
+ return t.Storage.Name()
+}
+
+func (t *Task) StoragePath() string {
+ return t.StorPath
+}
+
// Type implements core.Executable.
func (t *Task) Type() tasktype.TaskType {
return tasktype.TaskTypeAria2
diff --git a/core/tasks/aria2dl/task_test.go b/core/tasks/aria2dl/task_test.go
index 7a0728ea..0811932d 100644
--- a/core/tasks/aria2dl/task_test.go
+++ b/core/tasks/aria2dl/task_test.go
@@ -34,8 +34,8 @@ func (m *mockStorage) Save(ctx context.Context, reader io.Reader, path string) e
return nil
}
-func (m *mockStorage) Exists(ctx context.Context, path string) bool {
- return false
+func (m *mockStorage) Exists(ctx context.Context, path string) (bool, error) {
+ return false, nil
}
func (m *mockStorage) JoinStoragePath(path string) string {
diff --git a/core/tasks/batchtfile/execute.go b/core/tasks/batchtfile/execute.go
index 99dd51c0..0a744098 100644
--- a/core/tasks/batchtfile/execute.go
+++ b/core/tasks/batchtfile/execute.go
@@ -16,6 +16,7 @@ import (
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
"github.com/krau/SaveAny-Bot/pkg/taskevent"
@@ -87,7 +88,8 @@ func (t *Task) executionGroups() []executionGroup {
for i := 0; i < len(t.elems); {
elem := &t.elems[i]
batchSaver, batchCapable := elem.Storage.(storage.StorageBatchSaver)
- if !batchCapable || elem.sourceGroupKey == "" {
+ key := elem.executionGroupKey()
+ if !batchCapable || key == "" {
groups = append(groups, executionGroup{elems: []*TaskElement{elem}})
i++
continue
@@ -96,7 +98,7 @@ func (t *Task) executionGroups() []executionGroup {
end := i + 1
for end < len(t.elems) {
next := &t.elems[end]
- if next.Storage != elem.Storage || next.sourceGroupKey != elem.sourceGroupKey {
+ if next.Storage != elem.Storage || next.executionGroupKey() != key {
break
}
end++
@@ -120,7 +122,7 @@ func (t *Task) processElements(ctx context.Context, elems []*TaskElement) error
return err
}
defer t.unmarkProcessing(elem.ID)
- err := t.processElement(gctx, *elem)
+ err := t.processElement(gctx, elem)
if err != nil && t.IgnoreErrors && !errors.Is(err, context.Canceled) {
// Per-item failure: keep siblings running.
log.FromContext(ctx).Warnf("Element %s failed (ignored): %v", elem.ID, err)
@@ -133,8 +135,9 @@ func (t *Task) processElements(ctx context.Context, elems []*TaskElement) error
}
func (t *Task) processBatch(ctx context.Context, group executionGroup) error {
+ var cleanup []*TaskElement
defer func() {
- for _, elem := range group.elems {
+ for _, elem := range cleanup {
if err := os.Remove(elem.localPath); err != nil && !os.IsNotExist(err) {
log.FromContext(ctx).Warnf("Failed to cleanup batch cache file %s: %v", elem.localPath, err)
}
@@ -206,18 +209,29 @@ func (t *Task) processBatch(ctx context.Context, group executionGroup) error {
}
openFiles = append(openFiles, file)
items = append(items, storagetypes.BatchItem{
- Reader: file,
- StoragePath: elem.Path,
- Size: stat.Size(),
- SourceGroupKey: elem.sourceGroupKey,
- Caption: elem.sourceCaption,
- PreserveCaption: elem.preserveCaption,
+ Reader: file,
+ StoragePath: elem.Path,
+ Size: stat.Size(),
+ SourceGroupKey: elem.sourceGroupKey,
+ Caption: elem.sourceCaption,
+ PreserveCaption: elem.preserveCaption,
+ MetadataJSON: elem.metadataJSON,
+ MetadataGroupKey: elem.metadataGroupKey,
})
}
for index, item := range items {
t.recordDownloadComplete(successElems[index].ID, item.Size)
}
- return t.saveBatchItems(ctx, successElems, items)
+ if err := t.saveBatchItems(ctx, successElems, items); err != nil {
+ return err
+ }
+ for _, elem := range successElems {
+ if err := core.MarkTaskElementDone(ctx, t.ID, elem.ID); err != nil {
+ return fmt.Errorf("failed to persist element completion: %w", err)
+ }
+ }
+ cleanup = successElems
+ return nil
}
func (t *Task) saveBatchItems(ctx context.Context, successElems []*TaskElement, items []storagetypes.BatchItem) error {
@@ -287,37 +301,37 @@ func (t *Task) unmarkProcessing(id string) {
func (t *Task) downloadElement(ctx context.Context, elem *TaskElement) error {
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", elem.File.Name()))
logger.Info("Starting file download")
- localFile, err := fsutil.CreateFile(elem.localPath)
+ if err := tdler.DownloadToCache(ctx, elem.File, elem.localPath, func(w io.WriterAt) io.WriterAt {
+ return ioutil.NewProgressWriterAt(w, func(n int) {
+ t.recordItemDownload(elem.ID, int64(n), time.Now())
+ downloaded := t.downloaded.Add(int64(n))
+ t.notifyProgress(ctx)
+ taskevent.Emit(ctx, taskevent.Event{
+ TaskID: t.ID,
+ Phase: taskevent.PhaseProgress,
+ TotalBytes: t.totalSize,
+ DownloadedBytes: downloaded,
+ })
+ })
+ }); err != nil {
+ t.markItemFailed(ctx, elem.ID, FailureStageDownload, err)
+ t.notifyStateChange(ctx)
+ return fmt.Errorf("failed to download file: %w", err)
+ }
+ logger.Info("File downloaded successfully")
+ fileStat, err := os.Stat(elem.localPath)
if err != nil {
t.markItemFailed(ctx, elem.ID, FailureStageCache, err)
t.notifyStateChange(ctx)
- return fmt.Errorf("failed to create local file: %w", err)
- }
- wrAt := ioutil.NewProgressWriterAt(localFile, func(n int) {
- t.recordItemDownload(elem.ID, int64(n), time.Now())
- downloaded := t.downloaded.Add(int64(n))
- t.notifyProgress(ctx)
- taskevent.Emit(ctx, taskevent.Event{
- TaskID: t.ID,
- Phase: taskevent.PhaseProgress,
- TotalBytes: t.totalSize,
- DownloadedBytes: downloaded,
- })
- })
- _, downloadErr := tdler.NewDownloader(elem.File).Parallel(ctx, wrAt)
- closeErr := localFile.Close()
- if downloadErr != nil {
- t.markItemFailed(ctx, elem.ID, FailureStageDownload, downloadErr)
- t.notifyStateChange(ctx)
- return fmt.Errorf("failed to download file: %w", downloadErr)
+ return fmt.Errorf("failed to get file stat: %w", err)
}
- if closeErr != nil {
- t.markItemFailed(ctx, elem.ID, FailureStageCache, closeErr)
+ if elem.File.Size() > 0 && fileStat.Size() == 0 {
+ err := fmt.Errorf("downloaded file is empty (expected %d bytes)", elem.File.Size())
+ t.markItemFailed(ctx, elem.ID, FailureStageDownload, err)
t.notifyStateChange(ctx)
- return fmt.Errorf("failed to close cache file: %w", closeErr)
+ return err
}
- logger.Info("File downloaded successfully")
- if path.Ext(elem.FileName()) == "" {
+ if path.Ext(elem.FileName()) == "" && path.Ext(elem.Path) == "" {
if ext := fsutil.DetectFileExt(elem.localPath); ext != "" {
elem.Path += ext
}
@@ -327,14 +341,20 @@ func (t *Task) downloadElement(ctx context.Context, elem *TaskElement) error {
return nil
}
-func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
+func (t *Task) processElement(ctx context.Context, elem *TaskElement) error {
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", elem.File.Name()))
if elem.stream {
pr, pw := io.Pipe()
defer pr.Close()
errg, uploadCtx := errgroup.WithContext(ctx)
+ uploadCtx = storagetypes.WithMessageMetadata(uploadCtx, elem.metadataJSON)
+ if elem.preserveCaption {
+ uploadCtx = storagetypes.WithSourceCaption(uploadCtx, elem.sourceCaption)
+ }
+ savedPath := elem.Path
errg.Go(func() error {
- err := elem.Storage.Save(uploadCtx, pr, elem.Path)
+ var err error
+ savedPath, err = storage.SaveWithPath(uploadCtx, elem.Storage, pr, elem.Path)
if err != nil {
t.markItemFailed(uploadCtx, elem.ID, FailureStageUpload, err)
t.notifyStateChange(ctx)
@@ -355,7 +375,11 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
errg.Go(func() error {
defer pw.Close()
logger.Info("Starting file download in stream mode")
- _, err := tdler.NewDownloader(elem.File).Stream(uploadCtx, wr)
+ downloader, cleanup := tdler.NewDownloader(uploadCtx, elem.File)
+ if cleanup != nil {
+ defer cleanup(uploadCtx)
+ }
+ _, err := downloader.Stream(uploadCtx, wr)
if err != nil {
logger.Errorf("Failed to download file: %v", err)
t.markItemFailed(uploadCtx, elem.ID, FailureStageDownload, err)
@@ -374,35 +398,30 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
})
t.recordDownloadComplete(elem.ID, streamedBytes)
t.markItemCompleted(elem.ID)
+ if err := core.MarkTaskElementDone(ctx, t.ID, elem.ID); err != nil {
+ t.markItemFailed(ctx, elem.ID, FailureStageUpload, err)
+ t.notifyStateChange(ctx)
+ return fmt.Errorf("failed to persist element completion: %w", err)
+ }
+ elem.saveMetadataSidecar(ctx, savedPath)
t.notifyStateChange(ctx)
logger.Info("File downloaded successfully in stream mode")
return nil
}
logger.Info("Starting file download")
- localFile, err := fsutil.CreateFile(elem.localPath)
- if err != nil {
- t.markItemFailed(ctx, elem.ID, FailureStageCache, err)
- t.notifyStateChange(ctx)
- return fmt.Errorf("failed to create local file: %w", err)
- }
- defer func() {
- if err := localFile.CloseAndRemove(); err != nil {
- logger.Errorf("Failed to close local file: %v", err)
- }
- }()
- wrAt := ioutil.NewProgressWriterAt(localFile, func(n int) {
- t.recordItemDownload(elem.ID, int64(n), time.Now())
- downloaded := t.downloaded.Add(int64(n))
- t.notifyProgress(ctx)
- taskevent.Emit(ctx, taskevent.Event{
- TaskID: t.ID,
- Phase: taskevent.PhaseProgress,
- TotalBytes: t.totalSize,
- DownloadedBytes: downloaded,
+ if err := tdler.DownloadToCache(ctx, elem.File, elem.localPath, func(w io.WriterAt) io.WriterAt {
+ return ioutil.NewProgressWriterAt(w, func(n int) {
+ t.recordItemDownload(elem.ID, int64(n), time.Now())
+ downloaded := t.downloaded.Add(int64(n))
+ t.notifyProgress(ctx)
+ taskevent.Emit(ctx, taskevent.Event{
+ TaskID: t.ID,
+ Phase: taskevent.PhaseProgress,
+ TotalBytes: t.totalSize,
+ DownloadedBytes: downloaded,
+ })
})
- })
- _, err = tdler.NewDownloader(elem.File).Parallel(ctx, wrAt)
- if err != nil {
+ }); err != nil {
t.markItemFailed(ctx, elem.ID, FailureStageDownload, err)
t.notifyStateChange(ctx)
return fmt.Errorf("failed to download file: %w", err)
@@ -414,20 +433,30 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
elem.Path = elem.Path + ext
}
}
- var fileStat os.FileInfo
- fileStat, err = os.Stat(elem.localPath)
+ fileStat, err := os.Stat(elem.localPath)
if err != nil {
t.markItemFailed(ctx, elem.ID, FailureStageCache, err)
t.notifyStateChange(ctx)
return fmt.Errorf("failed to get file stat: %w", err)
}
+ if elem.File.Size() > 0 && fileStat.Size() == 0 {
+ err := fmt.Errorf("downloaded file is empty (expected %d bytes)", elem.File.Size())
+ t.markItemFailed(ctx, elem.ID, FailureStageDownload, err)
+ t.notifyStateChange(ctx)
+ return err
+ }
t.recordDownloadComplete(elem.ID, fileStat.Size())
vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size())
+ vctx = storagetypes.WithMessageMetadata(vctx, elem.metadataJSON)
+ if elem.preserveCaption {
+ vctx = storagetypes.WithSourceCaption(vctx, elem.sourceCaption)
+ }
t.startUpload(vctx)
onProgress := t.uploadCallback(vctx, elem.ID)
attempt := 0
retryLimit := int(config.C().Retry)
lastFailureStage := FailureStageUpload
+ savedPath := elem.Path
err = retry.Retry(func() error {
attempt++
var file *os.File
@@ -442,8 +471,9 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
onProgress(0, fileStat.Size())
if progressSaver, ok := elem.Storage.(storage.StorageProgressSaver); ok {
err = progressSaver.SaveWithProgress(vctx, file, elem.Path, onProgress)
+ savedPath = elem.Path
} else {
- err = elem.Storage.Save(vctx, ioutil.NewProgressReader(file, fileStat.Size(), onProgress), elem.Path)
+ savedPath, err = storage.SaveWithPath(vctx, elem.Storage, ioutil.NewProgressReader(file, fileStat.Size(), onProgress), elem.Path)
}
if err != nil {
logger.Errorf("Failed to save file: %s, retrying...", err)
@@ -457,7 +487,16 @@ func (t *Task) processElement(ctx context.Context, elem TaskElement) error {
if err == nil {
onProgress(fileStat.Size(), fileStat.Size())
t.markItemCompleted(elem.ID)
+ if err := core.MarkTaskElementDone(vctx, t.ID, elem.ID); err != nil {
+ t.markItemFailed(vctx, elem.ID, FailureStageUpload, err)
+ t.notifyStateChange(vctx)
+ return fmt.Errorf("failed to persist element completion: %w", err)
+ }
+ elem.saveMetadataSidecar(vctx, savedPath)
t.notifyStateChange(vctx)
+ if err := os.Remove(elem.localPath); err != nil {
+ logger.Errorf("Failed to remove cache file: %v", err)
+ }
} else {
t.markItemFailed(vctx, elem.ID, lastFailureStage, err)
t.notifyStateChange(vctx)
diff --git a/core/tasks/batchtfile/params.go b/core/tasks/batchtfile/params.go
new file mode 100644
index 00000000..bbe6d7fc
--- /dev/null
+++ b/core/tasks/batchtfile/params.go
@@ -0,0 +1,41 @@
+package batchtfile
+
+import (
+ "encoding/json"
+ "errors"
+
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ items := make([]taskspec.TGFileItem, 0, len(t.elems))
+ for i := range t.elems {
+ elem := &t.elems[i]
+ messageFile, ok := elem.File.(tfile.TGFileMessage)
+ if !ok || messageFile.Message() == nil {
+ return nil, errors.New("task element has no source message")
+ }
+ message := messageFile.Message()
+ items = append(items, taskspec.TGFileItem{
+ ID: elem.ID,
+ ChatID: tgutil.ChatIdFromPeer(message.GetPeerID()),
+ MessageID: message.GetID(),
+ Name: elem.File.Name(),
+ Storage: elem.Storage.Name(),
+ StorageID: storage.ID(elem.Storage),
+ Path: elem.Path,
+ })
+ }
+ overwrite := false
+ if t.ctx != nil {
+ overwrite, _ = t.ctx.Value(ctxkey.OverwriteExisting).(bool)
+ }
+ return json.Marshal(taskspec.TGFile{Items: items, Overwrite: overwrite, Done: t.Done})
+}
diff --git a/core/tasks/batchtfile/progress.go b/core/tasks/batchtfile/progress.go
index a74649bd..1eebee30 100644
--- a/core/tasks/batchtfile/progress.go
+++ b/core/tasks/batchtfile/progress.go
@@ -123,9 +123,7 @@ func buildBatchEditMessageRequest(messageID int, taskID string, message rendered
req.SetEntities(message.Entities)
}
if cancellable {
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{Rows: []tg.KeyboardButtonRow{{
- Buttons: []tg.KeyboardButtonClass{tgutil.BuildCancelButton(taskID)},
- }}})
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(taskID))
}
return req
}
@@ -191,12 +189,21 @@ func buildBatchDoneMarkup(ctx context.Context, info TaskInfo, skipped []string,
}
if err == nil {
completed, _, _, failed := itemCounts(items)
- // Report per-element failures instead of full completion.
- totalSkipped := len(skipped) + failed
- if totalSkipped > 0 {
+ // Keep real failures out of the skipped bucket so partial results are
+ // never reported as a full success.
+ if failed > 0 {
+ return localizedProgressMarkup(i18nk.BotMsgProgressBatchDoneWithFailed, map[string]any{
+ "Success": completed,
+ "Failed": failed,
+ "Skipped": len(skipped),
+ "Size": dlutil.FormatSize(totalSize),
+ "List": failedItemsList(items, 5),
+ })
+ }
+ if len(skipped) > 0 {
return localizedProgressMarkup(i18nk.BotMsgProgressBatchDoneWithSkipped, map[string]any{
"Success": completed,
- "Skipped": totalSkipped,
+ "Skipped": len(skipped),
"Size": dlutil.FormatSize(totalSize),
})
}
@@ -450,6 +457,38 @@ func firstError(items []TaskItemProgress) string {
return ""
}
+func failedItemsList(items []TaskItemProgress, limit int) string {
+ failed := make([]TaskItemProgress, 0, len(items))
+ for _, item := range items {
+ if item.Phase == ItemPhaseFailed {
+ failed = append(failed, item)
+ }
+ }
+ if len(failed) == 0 {
+ return ""
+ }
+ var builder strings.Builder
+ builder.WriteString("\n\n")
+ for i, item := range failed {
+ if i >= limit {
+ builder.WriteString(fmt.Sprintf("\n… +%d", len(failed)-limit))
+ break
+ }
+ builder.WriteString("\n❌ ")
+ builder.WriteString(item.Name)
+ if stage := failureStageLabel(item.FailureStage); stage != "" {
+ builder.WriteString(" (")
+ builder.WriteString(stage)
+ builder.WriteString(")")
+ }
+ if reason := truncateRunes(item.Error, 60); reason != "" {
+ builder.WriteString(": ")
+ builder.WriteString(reason)
+ }
+ }
+ return builder.String()
+}
+
func failureStageLabel(stage FailureStage) string {
switch stage {
case FailureStageDownload:
diff --git a/core/tasks/batchtfile/results.go b/core/tasks/batchtfile/results.go
new file mode 100644
index 00000000..5a9c3f94
--- /dev/null
+++ b/core/tasks/batchtfile/results.go
@@ -0,0 +1,85 @@
+package batchtfile
+
+import (
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ResultReporter = (*Task)(nil)
+
+// ItemResults reports the final state of every element. The worker persists
+// these rows so failed files stay visible and retryable after the task ends.
+func (t *Task) ItemResults() []core.ItemResult {
+ states := t.Items()
+ byID := make(map[string]TaskItemProgress, len(states))
+ for _, state := range states {
+ byID[state.ID] = state
+ }
+
+ results := make([]core.ItemResult, 0, len(t.elems))
+ for i := range t.elems {
+ elem := &t.elems[i]
+ state, ok := byID[elem.ID]
+ result := core.ItemResult{
+ Index: i + 1,
+ Name: elem.File.Name(),
+ Path: elem.Path,
+ StorageName: elem.Storage.Name(),
+ StorageID: storage.ID(elem.Storage),
+ Size: state.Size,
+ }
+ if !ok {
+ result.Status = core.ItemStatusSkipped
+ } else {
+ result.Index = state.Index
+ switch state.Phase {
+ case ItemPhaseCompleted:
+ result.Status = core.ItemStatusCompleted
+ case ItemPhaseFailed:
+ result.Status = core.ItemStatusFailed
+ result.Error = failureSummary(state)
+ case ItemPhaseStopped:
+ result.Status = core.ItemStatusCancelled
+ default:
+ result.Status = core.ItemStatusSkipped
+ }
+ }
+ if messageFile, ok := elem.File.(tfile.TGFileMessage); ok && messageFile.Message() != nil {
+ msg := messageFile.Message()
+ result.SourceChatID = tgutil.ChatIdFromPeer(msg.GetPeerID())
+ result.SourceMessageID = msg.GetID()
+ result.MetadataJSON = elem.metadataJSON
+ result.MetadataVersion = elem.metadataVersion
+ }
+ results = append(results, result)
+ }
+ return results
+}
+
+func failureSummary(state TaskItemProgress) string {
+ stage := failureStageToken(state.FailureStage)
+ if state.Error == "" {
+ return stage
+ }
+ return stage + ": " + state.Error
+}
+
+// failureStageToken returns a stable, non-localized stage name for storage.
+func failureStageToken(stage FailureStage) string {
+ switch stage {
+ case FailureStageDownload:
+ return "download"
+ case FailureStageCache:
+ return "local cache"
+ case FailureStageUpload:
+ return "upload"
+ case FailureStageConfirm:
+ return "remote confirmation"
+ case FailureStageBatchUpload:
+ return "batch upload"
+ default:
+ return "internal task"
+ }
+}
diff --git a/core/tasks/batchtfile/task.go b/core/tasks/batchtfile/task.go
index 82c4bbd1..36761541 100644
--- a/core/tasks/batchtfile/task.go
+++ b/core/tasks/batchtfile/task.go
@@ -2,36 +2,45 @@ package batchtfile
import (
"context"
+ "encoding/json"
"fmt"
"path/filepath"
"sync"
"sync/atomic"
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/common/utils/fsutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/metadata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
"github.com/rs/xid"
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type TaskElement struct {
- ID string
- Storage storage.Storage
- Path string
- File tfile.TGFile
- localPath string
- stream bool
- sourceGroupKey string
- sourceCaption string
- preserveCaption bool
+ ID string
+ Storage storage.Storage
+ Path string
+ File tfile.TGFile
+ localPath string
+ stream bool
+ sourceGroupKey string
+ sourceCaption string
+ preserveCaption bool
+ metadataJSON string
+ metadataVersion int
+ metadataGroupKey string
}
type Task struct {
ID string
+ Done []string
ctx context.Context
elems []TaskElement
Progress ProgressTracker
@@ -63,11 +72,19 @@ func NewTaskElement(
path string,
file tfile.TGFile,
) (*TaskElement, error) {
- id := xid.New().String()
+ return NewTaskElementWithID(xid.New().String(), stor, path, file)
+}
+
+func NewTaskElementWithID(
+ id string,
+ stor storage.Storage,
+ path string,
+ file tfile.TGFile,
+) (*TaskElement, error) {
groupKey, caption, preserveCaption := sourceMetadata(file)
_, ok := stor.(storage.StorageCannotStream)
if !config.C().Stream || ok {
- cachePath, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, fmt.Sprintf("%s_%s", id, file.Name())))
+ cachePath, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, fmt.Sprintf("%s_%s", id, fsutil.SafeBaseName(file.Name()))))
if err != nil {
return nil, fmt.Errorf("failed to get absolute path for cache: %w", err)
}
@@ -136,5 +153,58 @@ func NewBatchTGFileTask(
IgnoreErrors: ignoreErrors,
processingMu: sync.RWMutex{},
}
+ if config.C().Metadata.Enabled {
+ for i := range task.elems {
+ task.elems[i].metadataJSON, task.elems[i].metadataVersion = buildElementMetadata(ctx, task.elems[i].File)
+ task.elems[i].metadataGroupKey = metadataGroupKey(task.elems[i].metadataJSON)
+ }
+ }
return task
}
+
+func buildElementMetadata(ctx context.Context, file tfile.TGFile) (string, int) {
+ messageFile, ok := file.(tfile.TGFileMessage)
+ if !ok || messageFile.Message() == nil {
+ return "", 0
+ }
+ metaJSON, version, err := metadata.BuildJSON(ctx, messageFile.Message(), file.Name(), file.Size(), config.C().Metadata.IncludeText)
+ if err != nil {
+ log.FromContext(ctx).Warnf("Failed to build task metadata: %v", err)
+ return "", 0
+ }
+ return metaJSON, version
+}
+
+func (e *TaskElement) saveMetadataSidecar(ctx context.Context, actualPath string) {
+ if !config.C().Metadata.Sidecar || e.metadataJSON == "" || actualPath == "" {
+ return
+ }
+ if err := storage.WriteMetadataSidecar(ctx, e.Storage, actualPath, []byte(e.metadataJSON)); err != nil {
+ log.FromContext(ctx).Warnf("Failed to save metadata sidecar for %s: %v", actualPath, err)
+ }
+}
+
+func metadataGroupKey(raw string) string {
+ if raw == "" {
+ return ""
+ }
+ var meta struct {
+ Message struct {
+ GroupedID int64 `json:"grouped_id"`
+ Chat struct {
+ ID int64 `json:"id"`
+ } `json:"chat"`
+ } `json:"message"`
+ }
+ if err := json.Unmarshal([]byte(raw), &meta); err != nil || meta.Message.GroupedID == 0 {
+ return ""
+ }
+ return fmt.Sprintf("metadata:%d:%d", meta.Message.Chat.ID, meta.Message.GroupedID)
+}
+
+func (e *TaskElement) executionGroupKey() string {
+ if e.sourceGroupKey != "" {
+ return e.sourceGroupKey
+ }
+ return e.metadataGroupKey
+}
diff --git a/core/tasks/batchtfile/taskinfo.go b/core/tasks/batchtfile/taskinfo.go
index c99d0eff..7b7ce4bf 100644
--- a/core/tasks/batchtfile/taskinfo.go
+++ b/core/tasks/batchtfile/taskinfo.go
@@ -50,9 +50,25 @@ func (t *Task) Count() int {
}
func (t *Task) Processing() []TaskElementInfo {
+ t.processingMu.RLock()
+ defer t.processingMu.RUnlock()
processing := make([]TaskElementInfo, 0, len(t.elems))
for _, elem := range t.processing {
processing = append(processing, elem)
}
return processing
}
+
+func (t *Task) StorageName() string {
+ if len(t.elems) == 0 {
+ return ""
+ }
+ return t.elems[0].Storage.Name()
+}
+
+func (t *Task) StoragePath() string {
+ if len(t.elems) == 0 {
+ return ""
+ }
+ return t.elems[0].Path
+}
diff --git a/core/tasks/directlinks/execute.go b/core/tasks/directlinks/execute.go
index f3511fb8..6978fa02 100644
--- a/core/tasks/directlinks/execute.go
+++ b/core/tasks/directlinks/execute.go
@@ -56,6 +56,7 @@ func (t *Task) Execute(ctx context.Context) error {
if file.Name == "" {
return fmt.Errorf("failed to determine filename for %s: Content-Disposition header is empty and URL does not contain a valid filename", file.URL)
}
+ file.Name = fsutil.SafeBaseName(file.Name)
return nil
})
diff --git a/core/tasks/directlinks/params.go b/core/tasks/directlinks/params.go
new file mode 100644
index 00000000..b167249b
--- /dev/null
+++ b/core/tasks/directlinks/params.go
@@ -0,0 +1,19 @@
+package directlinks
+
+import (
+ "encoding/json"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ urls := make([]string, 0, len(t.files))
+ for _, file := range t.files {
+ urls = append(urls, file.URL)
+ }
+ path := t.StorPath
+ return json.Marshal(taskspec.DirectLinks{URLs: urls, Path: &path})
+}
diff --git a/core/tasks/directlinks/progress.go b/core/tasks/directlinks/progress.go
index 708d451e..12e45527 100644
--- a/core/tasks/directlinks/progress.go
+++ b/core/tasks/directlinks/progress.go
@@ -145,15 +145,7 @@ func (p *Progress) OnProgress(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.chatID, req)
@@ -187,15 +179,7 @@ func (p *Progress) OnStart(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext.EditMessage(p.chatID, req)
}
diff --git a/core/tasks/directlinks/task.go b/core/tasks/directlinks/task.go
index 350fa4ff..a951ce01 100644
--- a/core/tasks/directlinks/task.go
+++ b/core/tasks/directlinks/task.go
@@ -28,6 +28,7 @@ func (f *File) FileSize() int64 {
}
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
ID string
@@ -110,7 +111,12 @@ func NewTask(
_, ok := stor.(storage.StorageCannotStream)
stream := config.C().Stream && !ok
files := make([]*File, 0, len(links))
+ seen := make(map[string]struct{}, len(links))
for _, link := range links {
+ if _, duplicate := seen[link]; duplicate {
+ continue
+ }
+ seen[link] = struct{}{}
files = append(files, &File{
URL: link,
})
diff --git a/core/tasks/parsed/execute.go b/core/tasks/parsed/execute.go
index 18bf3b5c..fd339f32 100644
--- a/core/tasks/parsed/execute.go
+++ b/core/tasks/parsed/execute.go
@@ -94,11 +94,12 @@ func (t *Task) processResource(ctx context.Context, resource parser.Resource) er
}
return resp.ContentLength
}())
+ filename := fsutil.SafeBaseName(resource.Filename)
if t.stream {
- return t.Stor.Save(ctx, resp.Body, path.Join(t.StorPath, resource.Filename))
+ return t.Stor.Save(ctx, resp.Body, path.Join(t.StorPath, filename))
}
cacheFile, err := fsutil.CreateFile(filepath.Join(config.C().Temp.BasePath,
- fmt.Sprintf("resource_%s_%s", t.ID, resource.Filename)))
+ fmt.Sprintf("resource_%s_%s", t.ID, filename)))
if err != nil {
return fmt.Errorf("failed to create cache file for resource %s: %w", resource.URL, err)
}
@@ -137,7 +138,7 @@ func (t *Task) processResource(ctx context.Context, resource parser.Resource) er
if err != nil {
return fmt.Errorf("failed to seek cache file for resource %s: %w", resource.URL, err)
}
- return t.Stor.Save(ctx, cacheFile, path.Join(t.StorPath, resource.Filename))
+ return t.Stor.Save(ctx, cacheFile, path.Join(t.StorPath, filename))
}, retry.Context(ctx), retry.RetryTimes(uint(config.C().Retry)))
if ctx.Err() != nil {
return ctx.Err()
diff --git a/core/tasks/parsed/params.go b/core/tasks/parsed/params.go
new file mode 100644
index 00000000..1c438d85
--- /dev/null
+++ b/core/tasks/parsed/params.go
@@ -0,0 +1,18 @@
+package parsed
+
+import (
+ "encoding/json"
+ "errors"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ if t.item == nil {
+ return nil, errors.New("task has no parsed item")
+ }
+ return json.Marshal(taskspec.Parsed{Item: t.item})
+}
diff --git a/core/tasks/parsed/progress.go b/core/tasks/parsed/progress.go
index 8c2b0aed..a732e09d 100644
--- a/core/tasks/parsed/progress.go
+++ b/core/tasks/parsed/progress.go
@@ -57,15 +57,7 @@ func (p *Progress) OnStart(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.ChatID, req)
@@ -116,15 +108,7 @@ func (p *Progress) OnProgress(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.ChatID, req)
diff --git a/core/tasks/parsed/task.go b/core/tasks/parsed/task.go
index d52e88a5..dc268329 100644
--- a/core/tasks/parsed/task.go
+++ b/core/tasks/parsed/task.go
@@ -16,6 +16,7 @@ import (
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
ID string
diff --git a/core/tasks/parsed/taskinfo.go b/core/tasks/parsed/taskinfo.go
index cba7f7bf..d24eec2b 100644
--- a/core/tasks/parsed/taskinfo.go
+++ b/core/tasks/parsed/taskinfo.go
@@ -1,5 +1,7 @@
package parsed
+import "maps"
+
type TaskInfo interface {
TaskID() string
Site() string
@@ -42,7 +44,11 @@ func (t *Task) DownloadedBytes() int64 {
func (t *Task) Processing() map[string]ResourceInfo {
t.processingMu.RLock()
defer t.processingMu.RUnlock()
- return t.processing
+ // Return a snapshot: callers render it while resource goroutines mutate the
+ // live map.
+ snapshot := make(map[string]ResourceInfo, len(t.processing))
+ maps.Copy(snapshot, t.processing)
+ return snapshot
}
type ResourceInfo interface {
diff --git a/core/tasks/telegraph/execute.go b/core/tasks/telegraph/execute.go
index d6d891a6..6b7c6402 100644
--- a/core/tasks/telegraph/execute.go
+++ b/core/tasks/telegraph/execute.go
@@ -66,7 +66,7 @@ func (t *Task) processPic(ctx context.Context, picUrl string, index int) error {
return fmt.Errorf("failed to download picture %s: %w", picUrl, err)
}
defer body.Close()
- filename := fmt.Sprintf("%d%s", index+1, path.Ext(picUrl))
+ filename := fsutil.SafeBaseName(fmt.Sprintf("%d%s", index+1, path.Ext(picUrl)))
if t.cannotStream {
cacheFile, err := fsutil.CreateFile(filepath.Join(config.C().Temp.BasePath,
fmt.Sprintf("tph_%s_%s", t.TaskID(), filename),
diff --git a/core/tasks/telegraph/params.go b/core/tasks/telegraph/params.go
new file mode 100644
index 00000000..66edcc9b
--- /dev/null
+++ b/core/tasks/telegraph/params.go
@@ -0,0 +1,14 @@
+package telegraph
+
+import (
+ "encoding/json"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ return json.Marshal(taskspec.Telegraph{PhPath: t.PhPath, Pics: t.Pics})
+}
diff --git a/core/tasks/telegraph/progress.go b/core/tasks/telegraph/progress.go
index 2613e321..2ed4cae6 100644
--- a/core/tasks/telegraph/progress.go
+++ b/core/tasks/telegraph/progress.go
@@ -44,15 +44,7 @@ func (p *Progress) OnStart(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.ChatID, req)
@@ -80,15 +72,7 @@ func (p *Progress) OnProgress(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.ChatID, req)
diff --git a/core/tasks/telegraph/task.go b/core/tasks/telegraph/task.go
index c1b5586a..2a5e9d4d 100644
--- a/core/tasks/telegraph/task.go
+++ b/core/tasks/telegraph/task.go
@@ -12,6 +12,7 @@ import (
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
ID string
diff --git a/core/tasks/tfile/execute.go b/core/tasks/tfile/execute.go
index 8eea959c..2a6fa2b2 100644
--- a/core/tasks/tfile/execute.go
+++ b/core/tasks/tfile/execute.go
@@ -13,13 +13,15 @@ import (
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
"github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
)
-func (t *Task) Execute(ctx context.Context) error {
+func (t *Task) Execute(ctx context.Context) (err error) {
+ defer func() { t.execErr = err }()
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", t.File.Name()))
if t.Progress != nil {
t.Progress.OnStart(ctx, t)
@@ -29,42 +31,36 @@ func (t *Task) Execute(ctx context.Context) error {
}
logger.Info("Starting file download")
- localFile, err := fsutil.CreateFile(t.localPath)
- if err != nil {
- return fmt.Errorf("failed to create local file: %w", err)
- }
- defer func() {
- if err := localFile.CloseAndRemove(); err != nil {
- logger.Errorf("Failed to close local file: %v", err)
- }
- }()
- wrAt := newWriterAt(ctx, localFile, t.Progress, t)
-
defer func() {
if t.Progress != nil {
t.Progress.OnDone(ctx, t, err)
}
}()
- _, err = tdler.NewDownloader(t.File).Parallel(ctx, wrAt)
- if err != nil {
+ if err := tdler.DownloadToCache(ctx, t.File, t.localPath, func(w io.WriterAt) io.WriterAt {
+ return newWriterAt(ctx, w, t.Progress, t)
+ }); err != nil {
return fmt.Errorf("failed to download file: %w", err)
}
logger.Infof("File downloaded successfully")
- if path.Ext(t.File.Name()) == "" {
+ if path.Ext(t.File.Name()) == "" && path.Ext(t.Path) == "" {
ext := fsutil.DetectFileExt(t.localPath)
if ext != "" {
t.Path = t.Path + ext
}
}
- var fileStat os.FileInfo
- fileStat, err = os.Stat(t.localPath)
+ fileStat, err := os.Stat(t.localPath)
if err != nil {
return fmt.Errorf("failed to get file stat: %w", err)
}
+ if t.File.Size() > 0 && fileStat.Size() == 0 {
+ return fmt.Errorf("downloaded file is empty (expected %d bytes)", t.File.Size())
+ }
vctx := context.WithValue(ctx, ctxkey.ContentLength, fileStat.Size())
+ vctx = storagetypes.WithMessageMetadata(vctx, t.metadataJSON)
if caption, ok := sourceCaption(t.File); ok {
vctx = storagetypes.WithSourceCaption(vctx, caption)
}
+ savedPath := t.Path
err = retry.Retry(func() error {
file, err := os.Open(t.localPath)
if err != nil {
@@ -73,7 +69,7 @@ func (t *Task) Execute(ctx context.Context) error {
defer file.Close()
uploadProgress, tracksUpload := t.Progress.(UploadProgressTracker)
if !tracksUpload {
- if err = t.Storage.Save(vctx, file, t.Path); err != nil {
+ if savedPath, err = storage.SaveWithPath(vctx, t.Storage, file, t.Path); err != nil {
return fmt.Errorf("failed to save file: %w", err)
}
return nil
@@ -85,9 +81,10 @@ func (t *Task) Execute(ctx context.Context) error {
}
if progressSaver, ok := t.Storage.(storage.StorageProgressSaver); ok {
err = progressSaver.SaveWithProgress(vctx, file, t.Path, onProgress)
+ savedPath = t.Path
} else {
var reader io.Reader = ioutil.NewProgressReader(file, fileStat.Size(), onProgress)
- err = t.Storage.Save(vctx, reader, t.Path)
+ savedPath, err = storage.SaveWithPath(vctx, t.Storage, reader, t.Path)
}
if err != nil {
return fmt.Errorf("failed to save file: %w", err)
@@ -97,6 +94,13 @@ func (t *Task) Execute(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to save file after retries: %w", err)
}
+ if err := core.MarkTaskElementDone(ctx, t.ID, t.ElementID); err != nil {
+ return fmt.Errorf("failed to persist task completion: %w", err)
+ }
+ t.saveMetadataSidecar(ctx, savedPath)
+ if err := os.Remove(t.localPath); err != nil {
+ logger.Errorf("Failed to remove cache file: %v", err)
+ }
return nil
}
diff --git a/core/tasks/tfile/params.go b/core/tasks/tfile/params.go
new file mode 100644
index 00000000..81bf729a
--- /dev/null
+++ b/core/tasks/tfile/params.go
@@ -0,0 +1,40 @@
+package tfile
+
+import (
+ "encoding/json"
+ "errors"
+
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ messageFile, ok := t.File.(tfilepkg.TGFileMessage)
+ if !ok || messageFile.Message() == nil {
+ return nil, errors.New("task has no source message")
+ }
+ message := messageFile.Message()
+ overwrite := false
+ if t.Ctx != nil {
+ overwrite, _ = t.Ctx.Value(ctxkey.OverwriteExisting).(bool)
+ }
+ return json.Marshal(taskspec.TGFile{
+ Overwrite: overwrite,
+ Done: t.Done,
+ Items: []taskspec.TGFileItem{{
+ ID: t.ElementID,
+ ChatID: tgutil.ChatIdFromPeer(message.GetPeerID()),
+ MessageID: message.GetID(),
+ Name: t.File.Name(),
+ Storage: t.Storage.Name(),
+ StorageID: storage.ID(t.Storage),
+ Path: t.Path,
+ }},
+ })
+}
diff --git a/core/tasks/tfile/progress.go b/core/tasks/tfile/progress.go
index 240f1dd5..cc9f52f7 100644
--- a/core/tasks/tfile/progress.go
+++ b/core/tasks/tfile/progress.go
@@ -39,6 +39,7 @@ type Progress struct {
lastUpdatePercent atomic.Int32
lastUpdateAt atomic.Int64
updateMu sync.Mutex
+ edits *tgutil.ProgressEditQueue
uploadAttempt int
uploadedBytes int64
actualSize int64
@@ -220,9 +221,12 @@ func (p *Progress) editMessage(ctx context.Context, taskID string, message rende
}
req := buildSingleEditMessageRequest(p.MessageID, taskID, message, cancellable)
if ext := tgutil.ExtFromContext(ctx); ext != nil {
- if _, err := ext.EditMessage(p.ChatID, req); err != nil {
- log.FromContext(ctx).Errorf("Failed to edit file progress message: %v", err)
+ if p.edits == nil {
+ p.edits = tgutil.NewProgressEditQueue(tgutil.SendProgressEdit)
}
+ // Queue the edit so a FLOOD_WAIT or a slow RPC never blocks the
+ // transfer goroutine that is holding the progress mutex.
+ p.edits.Submit(ctx, p.ChatID, req, !cancellable)
}
}
@@ -233,9 +237,7 @@ func buildSingleEditMessageRequest(messageID int, taskID string, message rendere
req.SetEntities(message.Entities)
}
if cancellable {
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{Rows: []tg.KeyboardButtonRow{{
- Buttons: []tg.KeyboardButtonClass{tgutil.BuildCancelButton(taskID)},
- }}})
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(taskID))
}
return req
}
diff --git a/core/tasks/tfile/results.go b/core/tasks/tfile/results.go
new file mode 100644
index 00000000..b3150380
--- /dev/null
+++ b/core/tasks/tfile/results.go
@@ -0,0 +1,40 @@
+package tfile
+
+import (
+ "context"
+ "errors"
+
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+ "github.com/krau/SaveAny-Bot/core"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ResultReporter = (*Task)(nil)
+
+func (t *Task) ItemResults() []core.ItemResult {
+ result := core.ItemResult{
+ Index: 1,
+ Name: t.File.Name(),
+ Path: t.Path,
+ StorageName: t.Storage.Name(),
+ StorageID: storage.ID(t.Storage),
+ Size: t.File.Size(),
+ Status: core.ItemStatusCompleted,
+ }
+ if t.execErr != nil {
+ result.Status = core.ItemStatusFailed
+ if errors.Is(t.execErr, context.Canceled) {
+ result.Status = core.ItemStatusCancelled
+ }
+ result.Error = t.execErr.Error()
+ }
+ if messageFile, ok := t.File.(tfilepkg.TGFileMessage); ok && messageFile.Message() != nil {
+ msg := messageFile.Message()
+ result.SourceChatID = tgutil.ChatIdFromPeer(msg.GetPeerID())
+ result.SourceMessageID = msg.GetID()
+ result.MetadataJSON = t.metadataJSON
+ result.MetadataVersion = t.metadataVersion
+ }
+ return []core.ItemResult{result}
+}
diff --git a/core/tasks/tfile/stream.go b/core/tasks/tfile/stream.go
index 881c31e3..960cd9fd 100644
--- a/core/tasks/tfile/stream.go
+++ b/core/tasks/tfile/stream.go
@@ -7,6 +7,8 @@ import (
"github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/common/tdler"
+ "github.com/krau/SaveAny-Bot/pkg/storagetypes"
+ "github.com/krau/SaveAny-Bot/storage"
"golang.org/x/sync/errgroup"
)
@@ -16,14 +18,25 @@ func executeStream(ctx context.Context, task *Task) error {
pr, pw := io.Pipe()
defer pr.Close()
errg, uploadCtx := errgroup.WithContext(ctx)
+ uploadCtx = storagetypes.WithMessageMetadata(uploadCtx, task.metadataJSON)
+ if caption, ok := sourceCaption(task.File); ok {
+ uploadCtx = storagetypes.WithSourceCaption(uploadCtx, caption)
+ }
+ savedPath := task.Path
errg.Go(func() error {
- return task.Storage.Save(uploadCtx, pr, task.Path)
+ var err error
+ savedPath, err = storage.SaveWithPath(uploadCtx, task.Storage, pr, task.Path)
+ return err
})
wr := newWriter(ctx, pw, task.Progress, task)
errg.Go(func() error {
defer pw.Close()
logger.Info("Starting file download in stream mode")
- _, err := tdler.NewDownloader(task.File).Stream(uploadCtx, wr)
+ downloader, cleanup := tdler.NewDownloader(ctx, task.File)
+ if cleanup != nil {
+ defer cleanup(ctx)
+ }
+ _, err := downloader.Stream(uploadCtx, wr)
if err != nil {
logger.Errorf("Failed to download file: %v", err)
pw.CloseWithError(err)
@@ -40,5 +53,6 @@ func executeStream(ctx context.Context, task *Task) error {
return err
}
logger.Info("File downloaded successfully in stream mode")
+ task.saveMetadataSidecar(ctx, savedPath)
return nil
}
diff --git a/core/tasks/tfile/tftask.go b/core/tasks/tfile/tftask.go
index d2cafa8d..ae1c50bd 100644
--- a/core/tasks/tfile/tftask.go
+++ b/core/tasks/tfile/tftask.go
@@ -5,24 +5,33 @@ import (
"fmt"
"path/filepath"
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/common/utils/fsutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core"
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/metadata"
"github.com/krau/SaveAny-Bot/pkg/tfile"
"github.com/krau/SaveAny-Bot/storage"
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
- ID string
- Ctx context.Context
- File tfile.TGFile
- Storage storage.Storage
- Path string
- Progress ProgressTracker
- stream bool
- localPath string
+ ID string
+ ElementID string
+ Done []string
+ Ctx context.Context
+ File tfile.TGFile
+ Storage storage.Storage
+ Path string
+ Progress ProgressTracker
+ stream bool
+ localPath string
+ execErr error
+ metadataJSON string
+ metadataVersion int
}
// Title implements core.Exectable.
@@ -42,31 +51,63 @@ func NewTGFileTask(
path string,
progress ProgressTracker,
) (*Task, error) {
+ metaJSON, metaVersion := buildFileMetadata(ctx, file)
_, ok := stor.(storage.StorageCannotStream)
if !config.C().Stream || ok {
- cachePath, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, fmt.Sprintf("%s_%s", id, file.Name())))
+ cachePath, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, fmt.Sprintf("%s_%s", id, fsutil.SafeBaseName(file.Name()))))
if err != nil {
return nil, fmt.Errorf("failed to get absolute path for cache: %w", err)
}
tfile := &Task{
- ID: id,
- Ctx: ctx,
- File: file,
- Storage: stor,
- Path: path,
- Progress: progress,
- localPath: cachePath,
+ ID: id,
+ ElementID: id,
+ Ctx: ctx,
+ File: file,
+ Storage: stor,
+ Path: path,
+ Progress: progress,
+ localPath: cachePath,
+ metadataJSON: metaJSON,
+ metadataVersion: metaVersion,
}
return tfile, nil
}
tfileTask := &Task{
- ID: id,
- Ctx: ctx,
- File: file,
- Storage: stor,
- Path: path,
- Progress: progress,
- stream: true,
+ ID: id,
+ ElementID: id,
+ Ctx: ctx,
+ File: file,
+ Storage: stor,
+ Path: path,
+ Progress: progress,
+ stream: true,
+ metadataJSON: metaJSON,
+ metadataVersion: metaVersion,
}
return tfileTask, nil
}
+
+func buildFileMetadata(ctx context.Context, file tfile.TGFile) (string, int) {
+ if !config.C().Metadata.Enabled {
+ return "", 0
+ }
+ messageFile, ok := file.(tfile.TGFileMessage)
+ if !ok || messageFile.Message() == nil {
+ return "", 0
+ }
+ metaJSON, version, err := metadata.BuildJSON(ctx, messageFile.Message(), file.Name(), file.Size(), config.C().Metadata.IncludeText)
+ if err != nil {
+ log.FromContext(ctx).Warnf("Failed to build task metadata: %v", err)
+ return "", 0
+ }
+ return metaJSON, version
+}
+
+func (t *Task) saveMetadataSidecar(ctx context.Context, actualPath string) {
+ if !config.C().Metadata.Sidecar || t.metadataJSON == "" || actualPath == "" {
+ return
+ }
+ if err := storage.WriteMetadataSidecar(ctx, t.Storage, actualPath, []byte(t.metadataJSON)); err != nil {
+ log.FromContext(ctx).Warnf("Failed to save metadata sidecar for %s: %v", actualPath, err)
+ }
+}
diff --git a/core/tasks/tfile/writer.go b/core/tasks/tfile/writer.go
index 09fc1c59..2b8a1056 100644
--- a/core/tasks/tfile/writer.go
+++ b/core/tasks/tfile/writer.go
@@ -5,34 +5,21 @@ import (
"io"
"sync/atomic"
+ "github.com/krau/SaveAny-Bot/common/utils/ioutil"
"github.com/krau/SaveAny-Bot/pkg/taskevent"
)
type ProgressWriterAt struct {
ctx context.Context
- wrAt io.WriterAt
progress ProgressTracker
downloaded *atomic.Int64
total int64
info TaskInfo
+ writer *ioutil.ProgressWriterAt
}
func (w *ProgressWriterAt) WriteAt(p []byte, off int64) (int, error) {
- at, err := w.wrAt.WriteAt(p, off)
- if err != nil {
- return 0, err
- }
- downloaded := w.downloaded.Add(int64(at))
- if w.progress != nil {
- w.progress.OnProgress(w.ctx, w.info, downloaded, w.total)
- }
- taskevent.Emit(w.ctx, taskevent.Event{
- TaskID: w.info.TaskID(),
- Phase: taskevent.PhaseProgress,
- TotalBytes: w.total,
- DownloadedBytes: downloaded,
- })
- return at, nil
+ return w.writer.WriteAt(p, off)
}
func newWriterAt(
@@ -41,31 +28,19 @@ func newWriterAt(
progress ProgressTracker,
taskInfo TaskInfo,
) *ProgressWriterAt {
- return &ProgressWriterAt{
+ w := &ProgressWriterAt{
ctx: ctx,
progress: progress,
downloaded: &atomic.Int64{},
total: taskInfo.FileSize(),
- wrAt: wrAt,
info: taskInfo,
}
+ w.writer = ioutil.NewProgressWriterAt(wrAt, w.record)
+ return w
}
-type ProgressWriter struct {
- ctx context.Context
- wrAt io.Writer
- progress ProgressTracker
- downloaded *atomic.Int64
- total int64
- info TaskInfo
-}
-
-func (w *ProgressWriter) Write(p []byte) (int, error) {
- at, err := w.wrAt.Write(p)
- if err != nil {
- return 0, err
- }
- downloaded := w.downloaded.Add(int64(at))
+func (w *ProgressWriterAt) record(n int) {
+ downloaded := w.downloaded.Add(int64(n))
if w.progress != nil {
w.progress.OnProgress(w.ctx, w.info, downloaded, w.total)
}
@@ -75,7 +50,19 @@ func (w *ProgressWriter) Write(p []byte) (int, error) {
TotalBytes: w.total,
DownloadedBytes: downloaded,
})
- return at, nil
+}
+
+type ProgressWriter struct {
+ ctx context.Context
+ progress ProgressTracker
+ downloaded *atomic.Int64
+ total int64
+ info TaskInfo
+ writer *ioutil.ProgressWriter
+}
+
+func (w *ProgressWriter) Write(p []byte) (int, error) {
+ return w.writer.Write(p)
}
func newWriter(
@@ -84,12 +71,26 @@ func newWriter(
progress ProgressTracker,
taskInfo TaskInfo,
) *ProgressWriter {
- return &ProgressWriter{
+ w := &ProgressWriter{
ctx: ctx,
progress: progress,
downloaded: &atomic.Int64{},
total: taskInfo.FileSize(),
- wrAt: wr,
info: taskInfo,
}
+ w.writer = ioutil.NewProgressWriter(wr, w.record)
+ return w
+}
+
+func (w *ProgressWriter) record(n int) {
+ downloaded := w.downloaded.Add(int64(n))
+ if w.progress != nil {
+ w.progress.OnProgress(w.ctx, w.info, downloaded, w.total)
+ }
+ taskevent.Emit(w.ctx, taskevent.Event{
+ TaskID: w.info.TaskID(),
+ Phase: taskevent.PhaseProgress,
+ TotalBytes: w.total,
+ DownloadedBytes: downloaded,
+ })
}
diff --git a/core/tasks/transfer/params.go b/core/tasks/transfer/params.go
new file mode 100644
index 00000000..7f18e607
--- /dev/null
+++ b/core/tasks/transfer/params.go
@@ -0,0 +1,30 @@
+package transfer
+
+import (
+ "encoding/json"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ items := make([]taskspec.TransferItem, 0, len(t.elems))
+ for i := range t.elems {
+ elem := &t.elems[i]
+ items = append(items, taskspec.TransferItem{
+ SourceStorage: elem.SourceStorage.Name(),
+ SourceStorageID: storage.ID(elem.SourceStorage),
+ SourcePath: elem.SourcePath,
+ TargetStorage: elem.TargetStorage.Name(),
+ TargetStorageID: storage.ID(elem.TargetStorage),
+ TargetPath: elem.TargetPath,
+ Name: elem.FileInfo.Name,
+ Size: elem.FileInfo.Size,
+ ModTime: elem.FileInfo.ModTime,
+ })
+ }
+ return json.Marshal(taskspec.Transfer{Items: items})
+}
diff --git a/core/tasks/transfer/progress.go b/core/tasks/transfer/progress.go
index bd538b3e..b96aee7a 100644
--- a/core/tasks/transfer/progress.go
+++ b/core/tasks/transfer/progress.go
@@ -64,15 +64,7 @@ func (p *Progress) OnStart(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- },
- })
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
@@ -143,15 +135,7 @@ func (p *Progress) OnProgress(ctx context.Context, info TaskInfo) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(info.TaskID()),
- },
- },
- },
- })
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(info.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
diff --git a/core/tasks/transfer/results.go b/core/tasks/transfer/results.go
new file mode 100644
index 00000000..4a0e06fa
--- /dev/null
+++ b/core/tasks/transfer/results.go
@@ -0,0 +1,36 @@
+package transfer
+
+import (
+ "path"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/storage"
+)
+
+var _ core.ResultReporter = (*Task)(nil)
+
+// ItemResults reports per-file outcomes so a transfer with ignored failures is
+// recorded as partial instead of a full success.
+func (t *Task) ItemResults() []core.ItemResult {
+ t.processingMu.RLock()
+ defer t.processingMu.RUnlock()
+ results := make([]core.ItemResult, 0, len(t.elems))
+ for i := range t.elems {
+ elem := &t.elems[i]
+ result := core.ItemResult{
+ Index: i + 1,
+ Name: elem.FileInfo.Name,
+ Path: path.Join(elem.TargetPath, elem.FileInfo.Name),
+ StorageName: elem.TargetStorage.Name(),
+ StorageID: storage.ID(elem.TargetStorage),
+ Size: elem.FileInfo.Size,
+ Status: core.ItemStatusCompleted,
+ }
+ if err := t.failed[elem.ID]; err != nil {
+ result.Status = core.ItemStatusFailed
+ result.Error = err.Error()
+ }
+ results = append(results, result)
+ }
+ return results
+}
diff --git a/core/tasks/transfer/task.go b/core/tasks/transfer/task.go
index 85e2bdef..728aeb57 100644
--- a/core/tasks/transfer/task.go
+++ b/core/tasks/transfer/task.go
@@ -14,6 +14,7 @@ import (
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type TaskElement struct {
ID string
diff --git a/core/tasks/transfer/taskinfo.go b/core/tasks/transfer/taskinfo.go
index cf5a3546..1cc79a17 100644
--- a/core/tasks/transfer/taskinfo.go
+++ b/core/tasks/transfer/taskinfo.go
@@ -70,3 +70,17 @@ func (t *Task) FailedFiles() []string {
}
return result
}
+
+func (t *Task) StorageName() string {
+ if len(t.elems) == 0 {
+ return ""
+ }
+ return t.elems[0].TargetStorage.Name()
+}
+
+func (t *Task) StoragePath() string {
+ if len(t.elems) == 0 {
+ return ""
+ }
+ return t.elems[0].TargetPath
+}
diff --git a/core/tasks/ytdlp/execute.go b/core/tasks/ytdlp/execute.go
index d554f569..484f0c8d 100644
--- a/core/tasks/ytdlp/execute.go
+++ b/core/tasks/ytdlp/execute.go
@@ -87,6 +87,9 @@ func (t *Task) Execute(ctx context.Context) error {
// task. The bot owns the output directory: the configured template roots every
// output in tempDir, and custom -o/--output templates are re-rooted there.
func buildDownloadCommand(cfg config.YtdlpConfig, tempDir string, flags []string) (*ytdlp.Command, []string, error) {
+ if err := validateCustomFlags(flags); err != nil {
+ return nil, nil, err
+ }
flags, err := rewriteOutputTemplates(flags, tempDir)
if err != nil {
return nil, nil, err
diff --git a/core/tasks/ytdlp/output.go b/core/tasks/ytdlp/output.go
index 05e9d68f..ba07fd88 100644
--- a/core/tasks/ytdlp/output.go
+++ b/core/tasks/ytdlp/output.go
@@ -14,6 +14,47 @@ import (
// and their position, so repeated templates still override each other the way
// yt-dlp expects. A -o/--output without a template is rejected, since yt-dlp
// would consume the following argument (a URL or another flag) as the template.
+// forbiddenYtdlpFlags can execute programs, load configuration, or read and
+// write arbitrary files, so they are never accepted from user input.
+var forbiddenYtdlpFlags = map[string]struct{}{
+ "--exec": {},
+ "--exec-before-download": {},
+ "--config-location": {},
+ "--config-locations": {},
+ "--load-info-json": {},
+ "--batch-file": {},
+ "--downloader": {},
+ "--external-downloader": {},
+ "--paths": {},
+ "--cache-dir": {},
+ "--postprocessor-args": {},
+ "--ppa": {},
+ "--netrc": {},
+ "--netrc-cmd": {},
+ "--proxy": {},
+ "--cookies": {},
+ "--cookies-from-browser": {},
+ "--plugin-dirs": {},
+ "--update": {},
+ "--update-to": {},
+}
+
+func validateCustomFlags(flags []string) error {
+ for _, flag := range flags {
+ name := flag
+ if before, _, ok := strings.Cut(flag, "="); ok {
+ name = before
+ }
+ if _, forbidden := forbiddenYtdlpFlags[name]; forbidden {
+ return fmt.Errorf("yt-dlp flag %s is not allowed", name)
+ }
+ if name == "-P" || strings.HasPrefix(name, "-P") || name == "-U" {
+ return fmt.Errorf("yt-dlp flag %s is not allowed", name)
+ }
+ }
+ return nil
+}
+
func rewriteOutputTemplates(flags []string, dir string) ([]string, error) {
rewritten := make([]string, 0, len(flags))
for i := 0; i < len(flags); i++ {
@@ -93,7 +134,7 @@ func splitTypePrefix(template string) (prefix, rest string) {
if !ok {
return "", template
}
- for _, k := range strings.Split(key, "+") {
+ for k := range strings.SplitSeq(key, "+") {
if _, known := ytdlpOutputTypes[k]; !known {
return "", template
}
diff --git a/core/tasks/ytdlp/params.go b/core/tasks/ytdlp/params.go
new file mode 100644
index 00000000..5aaf334e
--- /dev/null
+++ b/core/tasks/ytdlp/params.go
@@ -0,0 +1,14 @@
+package ytdlp
+
+import (
+ "encoding/json"
+
+ "github.com/krau/SaveAny-Bot/core"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+)
+
+var _ core.ParamSource = (*Task)(nil)
+
+func (t *Task) TaskParams() (json.RawMessage, error) {
+ return json.Marshal(taskspec.YTDLP{URLs: t.URLs, Flags: t.Flags})
+}
diff --git a/core/tasks/ytdlp/progress.go b/core/tasks/ytdlp/progress.go
index f2ba6924..c31d71c6 100644
--- a/core/tasks/ytdlp/progress.go
+++ b/core/tasks/ytdlp/progress.go
@@ -60,15 +60,7 @@ func (p *Progress) OnStart(ctx context.Context, task *Task) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(task.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(task.TaskID()))
ext.EditMessage(p.chatID, req)
}
@@ -102,15 +94,7 @@ func (p *Progress) OnProgress(ctx context.Context, task *Task, status string) {
}
req.SetMessage(text)
req.SetEntities(entities)
- req.SetReplyMarkup(&tg.ReplyInlineMarkup{
- Rows: []tg.KeyboardButtonRow{
- {
- Buttons: []tg.KeyboardButtonClass{
- tgutil.BuildCancelButton(task.TaskID()),
- },
- },
- }},
- )
+ req.SetReplyMarkup(tgutil.BuildCancelMarkup(task.TaskID()))
ext := tgutil.ExtFromContext(ctx)
if ext != nil {
ext.EditMessage(p.chatID, req)
diff --git a/core/tasks/ytdlp/task.go b/core/tasks/ytdlp/task.go
index ef945c04..bbbb9751 100644
--- a/core/tasks/ytdlp/task.go
+++ b/core/tasks/ytdlp/task.go
@@ -10,6 +10,7 @@ import (
)
var _ core.Executable = (*Task)(nil)
+var _ core.DestinationInfo = (*Task)(nil)
type Task struct {
ID string
@@ -30,6 +31,17 @@ func (t *Task) Title() string {
return fmt.Sprintf("[%s](%d URLs->%s:%s)", t.Type(), urlCount, t.Storage.Name(), t.StorPath)
}
+func (t *Task) StorageName() string {
+ if t.Storage == nil {
+ return ""
+ }
+ return t.Storage.Name()
+}
+
+func (t *Task) StoragePath() string {
+ return t.StorPath
+}
+
// Type implements core.Executable.
func (t *Task) Type() tasktype.TaskType {
return tasktype.TaskTypeYtdlp
diff --git a/database/bot_worker.go b/database/bot_worker.go
new file mode 100644
index 00000000..f56d098c
--- /dev/null
+++ b/database/bot_worker.go
@@ -0,0 +1,31 @@
+package database
+
+import "context"
+
+func CreateBotWorker(ctx context.Context, worker *BotWorker) error {
+ return db.WithContext(ctx).Create(worker).Error
+}
+
+func ListBotWorkers(ctx context.Context) ([]BotWorker, error) {
+ var workers []BotWorker
+ if err := db.WithContext(ctx).Order("id ASC").Find(&workers).Error; err != nil {
+ return nil, err
+ }
+ return workers, nil
+}
+
+func GetBotWorker(ctx context.Context, id uint) (*BotWorker, error) {
+ var worker BotWorker
+ if err := db.WithContext(ctx).First(&worker, id).Error; err != nil {
+ return nil, err
+ }
+ return &worker, nil
+}
+
+func UpdateBotWorker(ctx context.Context, id uint, fields map[string]any) error {
+ return db.WithContext(ctx).Model(&BotWorker{}).Where("id = ?", id).Updates(fields).Error
+}
+
+func DeleteBotWorker(ctx context.Context, id uint) error {
+ return db.WithContext(ctx).Unscoped().Delete(&BotWorker{}, id).Error
+}
diff --git a/database/chat.go b/database/chat.go
index 6e6c6ae2..d8136a2a 100644
--- a/database/chat.go
+++ b/database/chat.go
@@ -1,6 +1,8 @@
package database
-import "context"
+import (
+ "context"
+)
func (user *User) WatchChat(ctx context.Context, chat WatchChat) error {
if len(user.WatchChats) == 0 {
diff --git a/database/db.go b/database/db.go
index 3c4300e3..c20b025d 100644
--- a/database/db.go
+++ b/database/db.go
@@ -8,20 +8,19 @@ import (
"time"
"github.com/charmbracelet/log"
- "github.com/krau/SaveAny-Bot/config"
"gorm.io/gorm"
glogger "gorm.io/gorm/logger"
)
var db *gorm.DB
-func Init(ctx context.Context) {
+func Init(ctx context.Context, path string) {
logger := log.FromContext(ctx)
- if err := os.MkdirAll(filepath.Dir(config.C().DB.Path), 0755); err != nil {
- logger.Fatal("Failed to create data directory: ", err)
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ logger.Fatalf("Failed to create data directory: %v", err)
}
var err error
- db, err = gorm.Open(GetDialect(config.C().DB.Path), &gorm.Config{
+ db, err = gorm.Open(GetDialect(path), &gorm.Config{
Logger: glogger.New(logger, glogger.Config{
Colorful: true,
SlowThreshold: time.Second * 5,
@@ -32,53 +31,29 @@ func Init(ctx context.Context) {
PrepareStmt: true,
})
if err != nil {
- logger.Fatal("Failed to open database: ", err)
+ logger.Fatalf("Failed to open database: %v", err)
}
- logger.Debug("Database connected")
- if err := db.AutoMigrate(&User{}, &Dir{}, &Rule{}, &WatchChat{}); err != nil {
- logger.Fatal("Database migration failed; if upgrading from an old version, try deleting the database file and retrying", "error", err)
+ if err := os.Chmod(path, 0o600); err != nil {
+ logger.Warnf("Failed to restrict database permissions: %v", err)
}
- if err := syncUsers(ctx); err != nil {
- logger.Fatal("Failed to sync users:", err)
+ logger.Debug("Database connected")
+ if err := migrate(ctx); err != nil {
+ logger.Fatalf("Database migration failed: %v", err)
}
- logger.Debug("Database migrated")
logger.Info("Database initialized")
}
-func syncUsers(ctx context.Context) error {
- logger := log.FromContext(ctx)
- dbUsers, err := GetAllUsers(ctx)
- if err != nil {
- return fmt.Errorf("failed to get users: %w", err)
+func Close() error {
+ if db == nil {
+ return nil
}
-
- dbUserMap := make(map[int64]User)
- for _, u := range dbUsers {
- dbUserMap[u.ChatID] = u
- }
-
- cfgUserMap := make(map[int64]struct{})
- for _, u := range config.C().Users {
- cfgUserMap[u.ID] = struct{}{}
- }
-
- for cfgID := range cfgUserMap {
- if _, exists := dbUserMap[cfgID]; !exists {
- if err := CreateUser(ctx, cfgID); err != nil {
- return fmt.Errorf("failed to create user %d: %w", cfgID, err)
- }
- logger.Infof("Created user from config: %d", cfgID)
- }
- }
-
- for dbID, dbUser := range dbUserMap {
- if _, exists := cfgUserMap[dbID]; !exists {
- if err := DeleteUser(ctx, &dbUser); err != nil {
- return fmt.Errorf("failed to delete user %d: %w", dbID, err)
- }
- logger.Infof("Deleted user not present in config: %d", dbID)
- }
+ sqlDB, err := db.DB()
+ if err != nil {
+ return fmt.Errorf("failed to get sql db: %w", err)
}
+ return sqlDB.Close()
+}
- return nil
+func Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error {
+ return db.WithContext(ctx).Transaction(fn)
}
diff --git a/database/dir.go b/database/dir.go
index 3ae65dda..fed93248 100644
--- a/database/dir.go
+++ b/database/dir.go
@@ -2,11 +2,11 @@ package database
import "context"
-func CreateDirForUser(ctx context.Context, userID uint, storageName, path string) error {
+func CreateDirForUser(ctx context.Context, userID, storageID uint, path string) error {
dir := Dir{
- UserID: userID,
- StorageName: storageName,
- Path: path,
+ UserID: userID,
+ StorageID: storageID,
+ Path: path,
}
return db.WithContext(ctx).Create(&dir).Error
}
@@ -34,24 +34,26 @@ func GetUserDirsByChatID(ctx context.Context, chatID int64) ([]Dir, error) {
return GetUserDirs(ctx, user.ID)
}
-func GetDirsByUserIDAndStorageName(ctx context.Context, userID uint, storageName string) ([]Dir, error) {
+func GetDirsByUserIDAndStorageID(ctx context.Context, userID, storageID uint) ([]Dir, error) {
var dirs []Dir
- err := db.WithContext(ctx).Where("user_id = ? AND storage_name = ?", userID, storageName).Find(&dirs).Error
+ err := db.WithContext(ctx).Where("user_id = ? AND storage_id = ?", userID, storageID).Find(&dirs).Error
return dirs, err
}
-func GetDirsByUserChatIDAndStorageName(ctx context.Context, chatID int64, storageName string) ([]Dir, error) {
+func GetDirsByUserChatIDAndStorageID(ctx context.Context, chatID int64, storageID uint) ([]Dir, error) {
user, err := GetUserByChatID(ctx, chatID)
if err != nil {
return nil, err
}
- return GetDirsByUserIDAndStorageName(ctx, user.ID, storageName)
+ return GetDirsByUserIDAndStorageID(ctx, user.ID, storageID)
}
-func DeleteDirForUser(ctx context.Context, userID uint, storageName, path string) error {
- return db.WithContext(ctx).Unscoped().Where("user_id = ? AND storage_name = ? AND path = ?", userID, storageName, path).Delete(&Dir{}).Error
+func DeleteDirForUser(ctx context.Context, userID, storageID uint, path string) error {
+ return db.WithContext(ctx).Unscoped().Where("user_id = ? AND storage_id = ? AND path = ?", userID, storageID, path).Delete(&Dir{}).Error
}
-func DeleteDirByID(ctx context.Context, id uint) error {
- return db.WithContext(ctx).Unscoped().Delete(&Dir{}, id).Error
+func DeleteDirByID(ctx context.Context, id uint, userID uint) error {
+ return db.WithContext(ctx).Unscoped().
+ Where("id = ? AND user_id = ?", id, userID).
+ Delete(&Dir{}).Error
}
diff --git a/database/driver.go b/database/driver.go
index d06744eb..f38e1f7b 100644
--- a/database/driver.go
+++ b/database/driver.go
@@ -8,5 +8,5 @@ import (
)
func GetDialect(dsn string) gorm.Dialector {
- return gormlite.Open(dsn)
+ return gormlite.Open(dsnWithPragmas(dsn))
}
diff --git a/database/driver_glebarez.go b/database/driver_glebarez.go
index 7e38b8ea..6ca9805a 100644
--- a/database/driver_glebarez.go
+++ b/database/driver_glebarez.go
@@ -8,5 +8,5 @@ import (
)
func GetDialect(dsn string) gorm.Dialector {
- return sqlite.Open(dsn)
+ return sqlite.Open(dsnWithPragmas(dsn))
}
diff --git a/database/dsn.go b/database/dsn.go
new file mode 100644
index 00000000..ac62c79b
--- /dev/null
+++ b/database/dsn.go
@@ -0,0 +1,25 @@
+package database
+
+import (
+ "net/url"
+ "path/filepath"
+ "strings"
+)
+
+func dsnWithPragmas(dsn string) string {
+ if strings.Contains(dsn, "_pragma=") {
+ return dsn
+ }
+ if dsn == ":memory:" || strings.HasPrefix(dsn, "file:") {
+ return dsn
+ }
+ absolute, err := filepath.Abs(dsn)
+ if err != nil {
+ return dsn
+ }
+ values := url.Values{}
+ values.Add("_pragma", "foreign_keys(1)")
+ values.Add("_pragma", "journal_mode(WAL)")
+ values.Add("_pragma", "busy_timeout(5000)")
+ return (&url.URL{Scheme: "file", Path: filepath.ToSlash(absolute), RawQuery: values.Encode()}).String()
+}
diff --git a/database/migration.go b/database/migration.go
new file mode 100644
index 00000000..7bb99b2a
--- /dev/null
+++ b/database/migration.go
@@ -0,0 +1,73 @@
+package database
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "gorm.io/gorm"
+)
+
+// CurrentSchemaVersion is the database schema expected by this build.
+// Bump it together with a new entry in schemaMigrations.
+const CurrentSchemaVersion = 1
+
+type schemaMigration struct {
+ version int
+ up func(tx *gorm.DB) error
+}
+
+var schemaMigrations = []schemaMigration{
+ {version: 1, up: baselineSchema},
+}
+
+func baselineSchema(tx *gorm.DB) error {
+ return tx.AutoMigrate(
+ &User{},
+ &Dir{},
+ &Rule{},
+ &WatchChat{},
+ &Setting{},
+ &StorageRecord{},
+ &TaskRecord{},
+ &TaskItemRecord{},
+ &WebSession{},
+ &BotWorker{},
+ &TaskItemMetadata{},
+ )
+}
+
+func migrate(ctx context.Context) error {
+ if err := db.WithContext(ctx).Exec(`
+CREATE TABLE IF NOT EXISTS schema_migrations (
+ version INTEGER PRIMARY KEY,
+ applied_at DATETIME NOT NULL
+)`).Error; err != nil {
+ return fmt.Errorf("create schema_migrations: %w", err)
+ }
+
+ var applied int
+ if err := db.WithContext(ctx).Raw("SELECT COALESCE(MAX(version), 0) FROM schema_migrations").Scan(&applied).Error; err != nil {
+ return fmt.Errorf("read schema version: %w", err)
+ }
+ if applied > CurrentSchemaVersion {
+ return fmt.Errorf("database schema version %d is newer than supported version %d", applied, CurrentSchemaVersion)
+ }
+ for _, migration := range schemaMigrations {
+ if migration.version <= applied {
+ continue
+ }
+ if err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := migration.up(tx); err != nil {
+ return err
+ }
+ return tx.Exec(
+ "INSERT INTO schema_migrations (version, applied_at) VALUES (?, ?)",
+ migration.version, time.Now().UTC(),
+ ).Error
+ }); err != nil {
+ return fmt.Errorf("apply schema migration %d: %w", migration.version, err)
+ }
+ }
+ return nil
+}
diff --git a/database/model.go b/database/model.go
index f3e68ead..77c86453 100644
--- a/database/model.go
+++ b/database/model.go
@@ -1,44 +1,150 @@
package database
import (
+ "time"
+
"gorm.io/gorm"
+
+ "github.com/krau/SaveAny-Bot/pkg/enums/storagefilter"
)
type User struct {
gorm.Model
- ChatID int64 `gorm:"uniqueIndex;not null"`
- Silent bool
- DefaultStorage string
- DefaultDir uint // Dir.ID
- Dirs []Dir
- ApplyRule bool
- Rules []Rule
- WatchChats []WatchChat
- WatchNotify bool
- FilenameStrategy string
- FilenameTemplate string
- ConflictStrategy string
+ ChatID int64 `gorm:"uniqueIndex;not null"`
+ Silent bool
+ DefaultStorageID uint // StorageRecord.ID
+ DefaultDirID uint // Dir.ID
+ Dirs []Dir
+ ApplyRule bool
+ Rules []Rule
+ WatchChats []WatchChat
+ WatchNotify bool
+ FilenameStrategy string
+ FilenameTemplate string
+ ConflictStrategy string
+ Enabled bool `gorm:"not null;default:true"`
+ StorageFilterIDsJSON string `gorm:"column:storage_filter_ids;type:text"` // JSON array of storage IDs
+ StorageFilterMode storagefilter.Mode `gorm:"size:16;not null;default:blacklist"` // blacklist or whitelist
}
type WatchChat struct {
gorm.Model
- UserID uint // User's database ID (not chat ID)
- ChatID int64
- Filter string
+ UserID uint `gorm:"uniqueIndex:idx_watch_user_chat"`
+ ChatID int64 `gorm:"uniqueIndex:idx_watch_user_chat"`
+ MessageFilter string // filter type and expression, for example msgre:foo
}
type Dir struct {
gorm.Model
- UserID uint
- StorageName string
- Path string
+ UserID uint `gorm:"uniqueIndex:idx_dir_user_storage_path"`
+ StorageID uint `gorm:"uniqueIndex:idx_dir_user_storage_path"` // StorageRecord.ID
+ Path string `gorm:"uniqueIndex:idx_dir_user_storage_path"`
}
type Rule struct {
gorm.Model
- UserID uint
- Type string
- Data string
- StorageName string
- DirPath string
+ UserID uint
+ Type string
+ Content string // rule content interpreted according to Type
+ StorageID uint // StorageRecord.ID
+ DirPath string
+}
+
+type Setting struct {
+ Key string `gorm:"primaryKey;size:64"`
+ Value string `gorm:"type:text;not null"`
+ UpdatedAt time.Time
+}
+
+type StorageRecord struct {
+ gorm.Model
+ Name string `gorm:"uniqueIndex;size:64;not null"`
+ Type string `gorm:"size:32;not null"`
+ Enable bool `gorm:"not null"`
+ ConfigJSON string `gorm:"column:config;type:text;not null"` // JSON storage configuration
+ ConfigVersion int `gorm:"not null;default:1"`
+}
+
+func (StorageRecord) TableName() string { return "storages" }
+
+// BotWorker is an optional helper bot used for Telegram storage uploads.
+type BotWorker struct {
+ gorm.Model
+ Name string `gorm:"uniqueIndex;size:64;not null"`
+ Token string `gorm:"type:text;not null"`
+ SessionPath string `gorm:"size:255"`
+ Enabled bool `gorm:"not null"`
+}
+
+type TaskRecord struct {
+ ID uint `gorm:"primaryKey"`
+ TaskID string `gorm:"uniqueIndex;size:64;not null"`
+ Type string `gorm:"size:32;not null"`
+ Title string
+ Status string `gorm:"size:16;index;not null"`
+ StorageName string
+ StorageID uint
+ Path string
+ Source string `gorm:"size:16;not null"`
+ Error string `gorm:"type:text"`
+ Params string `gorm:"type:text"`
+ ParamsVersion int `gorm:"not null;default:1"`
+ CreatedAt time.Time
+ StartedAt *time.Time
+ FinishedAt *time.Time
+ UpdatedAt time.Time
+}
+
+// TaskItemRecord is the persisted outcome of one file inside a task. It keeps
+// failed items addressable after the task leaves memory, so they can be
+// reported and retried.
+type TaskItemRecord struct {
+ ID uint `gorm:"primaryKey"`
+ TaskID string `gorm:"index:idx_task_item_task_index,unique;size:64;not null"`
+ Index int `gorm:"index:idx_task_item_task_index,unique;not null"`
+ Name string
+ Path string
+ StorageName string
+ StorageID uint
+ Status string `gorm:"size:16;index;not null"`
+ Error string `gorm:"type:text"`
+ Size int64
+ SourceChatID int64
+ SourceMsgID int
+}
+
+func (TaskItemRecord) TableName() string {
+ return "task_items"
+}
+
+type WebSession struct {
+ ID string `gorm:"primaryKey;size:64"`
+ CSRF string `gorm:"size:64;not null"`
+ CreatedAt time.Time
+ ExpiresAt time.Time `gorm:"index"`
+}
+
+type TaskFilter struct {
+ Status string
+ Type string
+ Limit int
+ Offset int
+}
+
+// TaskItemMetadata stores the normalized Telegram message metadata for one
+// saved file. It is separate from TaskItemRecord so task lists do not load
+// potentially large JSON payloads.
+type TaskItemMetadata struct {
+ ID uint `gorm:"primaryKey"`
+ TaskID string `gorm:"uniqueIndex:idx_task_metadata_item;size:64;not null"`
+ ItemIndex int `gorm:"uniqueIndex:idx_task_metadata_item;not null"`
+ Version int `gorm:"not null;default:1"`
+ SourceChatID int64 `gorm:"index"`
+ SourceMessageID int `gorm:"index"`
+ JSON string `gorm:"column:metadata;type:text;not null"`
+ CreatedAt time.Time
+}
+
+func (TaskItemMetadata) TableName() string {
+ return "task_item_metadata"
}
diff --git a/database/rule.go b/database/rule.go
index 937e4abb..7dc5355b 100644
--- a/database/rule.go
+++ b/database/rule.go
@@ -6,14 +6,22 @@ func CreateRule(ctx context.Context, rule *Rule) error {
return db.WithContext(ctx).Create(rule).Error
}
-func DeleteRule(ctx context.Context, ruleID uint) error {
- return db.WithContext(ctx).Unscoped().Delete(&Rule{}, ruleID).Error
+func DeleteRule(ctx context.Context, ruleID uint, userID uint) error {
+ return db.WithContext(ctx).Unscoped().
+ Where("id = ? AND user_id = ?", ruleID, userID).
+ Delete(&Rule{}).Error
}
func UpdateUserApplyRule(ctx context.Context, chatID int64, applyRule bool) error {
return db.WithContext(ctx).Model(&User{}).Where("chat_id = ?", chatID).Update("apply_rule", applyRule).Error
}
+func GetRulesByUserID(ctx context.Context, userID uint) ([]Rule, error) {
+ var rules []Rule
+ err := db.WithContext(ctx).Where("user_id = ?", userID).Find(&rules).Error
+ return rules, err
+}
+
func GetRulesByUserChatID(ctx context.Context, chatID int64) ([]Rule, error) {
var rules []Rule
err := db.WithContext(ctx).Where("user_id = (SELECT id FROM users WHERE chat_id = ?)", chatID).Find(&rules).Error
diff --git a/database/setting.go b/database/setting.go
new file mode 100644
index 00000000..762d28cb
--- /dev/null
+++ b/database/setting.go
@@ -0,0 +1,53 @@
+package database
+
+import (
+ "context"
+
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+)
+
+func GetSetting(ctx context.Context, key string) (string, bool, error) {
+ var setting Setting
+ err := db.WithContext(ctx).Where("key = ?", key).First(&setting).Error
+ if err != nil {
+ if err == gorm.ErrRecordNotFound {
+ return "", false, nil
+ }
+ return "", false, err
+ }
+ return setting.Value, true, nil
+}
+
+func SetSetting(ctx context.Context, key, value string) error {
+ return db.WithContext(ctx).Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "key"}},
+ DoUpdates: clause.AssignmentColumns([]string{"value", "updated_at"}),
+ }).Create(&Setting{Key: key, Value: value}).Error
+}
+
+func SetSettingsTx(ctx context.Context, values map[string]string) error {
+ return Transaction(ctx, func(tx *gorm.DB) error {
+ for key, value := range values {
+ if err := tx.Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "key"}},
+ DoUpdates: clause.AssignmentColumns([]string{"value", "updated_at"}),
+ }).Create(&Setting{Key: key, Value: value}).Error; err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+}
+
+func ListSettings(ctx context.Context) (map[string]string, error) {
+ var settings []Setting
+ if err := db.WithContext(ctx).Find(&settings).Error; err != nil {
+ return nil, err
+ }
+ out := make(map[string]string, len(settings))
+ for _, s := range settings {
+ out[s.Key] = s.Value
+ }
+ return out, nil
+}
diff --git a/database/storage_record.go b/database/storage_record.go
new file mode 100644
index 00000000..87807ace
--- /dev/null
+++ b/database/storage_record.go
@@ -0,0 +1,105 @@
+package database
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "slices"
+ "strings"
+
+ "gorm.io/gorm"
+)
+
+func CreateStorageRecord(ctx context.Context, record *StorageRecord) error {
+ return db.WithContext(ctx).Create(record).Error
+}
+
+func ListStorageRecords(ctx context.Context) ([]StorageRecord, error) {
+ var records []StorageRecord
+ err := db.WithContext(ctx).Order("name").Find(&records).Error
+ return records, err
+}
+
+func GetStorageRecord(ctx context.Context, id uint) (*StorageRecord, error) {
+ var record StorageRecord
+ if err := db.WithContext(ctx).First(&record, id).Error; err != nil {
+ return nil, err
+ }
+ return &record, nil
+}
+
+func UpdateStorageRecord(ctx context.Context, record *StorageRecord) error {
+ return db.WithContext(ctx).Save(record).Error
+}
+
+func DeleteStorageRecord(ctx context.Context, id uint) error {
+ return db.WithContext(ctx).Unscoped().Delete(&StorageRecord{}, id).Error
+}
+
+// MigrateMinioStorageType converts legacy MinIO storage records to S3 and
+// normalizes their endpoint to the scheme-based form used by the S3 backend.
+func MigrateMinioStorageType(ctx context.Context) error {
+ var records []StorageRecord
+ if err := db.WithContext(ctx).Where("type = ?", "minio").Find(&records).Error; err != nil {
+ return err
+ }
+ for _, record := range records {
+ config := make(map[string]any)
+ if record.ConfigJSON != "" {
+ if err := json.Unmarshal([]byte(record.ConfigJSON), &config); err != nil {
+ return fmt.Errorf("decode legacy MinIO storage %s: %w", record.Name, err)
+ }
+ }
+ if endpoint, ok := config["endpoint"].(string); ok && endpoint != "" && !strings.Contains(endpoint, "://") {
+ scheme := "http"
+ if useSSL, _ := config["use_ssl"].(bool); useSSL {
+ scheme = "https"
+ }
+ config["endpoint"] = scheme + "://" + endpoint
+ }
+ delete(config, "use_ssl")
+ raw, err := json.Marshal(config)
+ if err != nil {
+ return fmt.Errorf("encode S3 storage %s: %w", record.Name, err)
+ }
+ if err := db.WithContext(ctx).Model(&StorageRecord{}).
+ Where("id = ?", record.ID).
+ Updates(map[string]any{"type": "s3", "config": string(raw)}).Error; err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+// StorageReferences returns how many entities still reference the storage ID.
+func StorageReferences(ctx context.Context, storageID uint) (users, dirs, rules int64, err error) {
+ var allUsers []User
+ if err = db.WithContext(ctx).Find(&allUsers).Error; err != nil {
+ return 0, 0, 0, err
+ }
+ for _, u := range allUsers {
+ if u.DefaultStorageID == storageID {
+ users++
+ continue
+ }
+ var ids []uint
+ if u.StorageFilterIDsJSON != "" {
+ if err := json.Unmarshal([]byte(u.StorageFilterIDsJSON), &ids); err != nil {
+ return 0, 0, 0, fmt.Errorf("decode storage filter for user %d: %w", u.ChatID, err)
+ }
+ }
+ if slices.Contains(ids, storageID) {
+ users++
+ }
+ }
+ if err = db.WithContext(ctx).Model(&Dir{}).Where("storage_id = ?", storageID).Count(&dirs).Error; err != nil {
+ return
+ }
+ if err = db.WithContext(ctx).Model(&Rule{}).Where("storage_id = ?", storageID).Count(&rules).Error; err != nil {
+ return
+ }
+ return users, dirs, rules, nil
+}
+
+func IsNotFound(err error) bool { return errors.Is(err, gorm.ErrRecordNotFound) }
diff --git a/database/task_item_metadata.go b/database/task_item_metadata.go
new file mode 100644
index 00000000..df48f767
--- /dev/null
+++ b/database/task_item_metadata.go
@@ -0,0 +1,53 @@
+package database
+
+import (
+ "context"
+
+ "gorm.io/gorm"
+)
+
+// ReplaceTaskItemMetadata stores normalized metadata for a task, replacing any
+// rows left by an earlier execution of the same task.
+func ReplaceTaskItemMetadata(ctx context.Context, taskID string, records []TaskItemMetadata) error {
+ return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Unscoped().Where("task_id = ?", taskID).Delete(&TaskItemMetadata{}).Error; err != nil {
+ return err
+ }
+ if len(records) == 0 {
+ return nil
+ }
+ for i := range records {
+ records[i].TaskID = taskID
+ }
+ return tx.CreateInBatches(records, 100).Error
+ })
+}
+
+func ListTaskItemMetadata(ctx context.Context, taskID string) ([]TaskItemMetadata, error) {
+ var records []TaskItemMetadata
+ err := db.WithContext(ctx).Where("task_id = ?", taskID).Order("item_index ASC").Find(&records).Error
+ return records, err
+}
+
+func CountTaskMetadata(ctx context.Context, taskIDs []string) (map[string]int, error) {
+ counts := make(map[string]int, len(taskIDs))
+ if len(taskIDs) == 0 {
+ return counts, nil
+ }
+ var rows []struct {
+ TaskID string
+ Count int
+ }
+ err := db.WithContext(ctx).Model(&TaskItemMetadata{}).
+ Select("task_id, COUNT(*) AS count").
+ Where("task_id IN ?", taskIDs).
+ Group("task_id").
+ Scan(&rows).Error
+ if err != nil {
+ return nil, err
+ }
+ for _, row := range rows {
+ counts[row.TaskID] = row.Count
+ }
+ return counts, nil
+}
diff --git a/database/task_record.go b/database/task_record.go
new file mode 100644
index 00000000..f34259e7
--- /dev/null
+++ b/database/task_record.go
@@ -0,0 +1,171 @@
+package database
+
+import (
+ "context"
+
+ "gorm.io/gorm"
+
+ "github.com/krau/SaveAny-Bot/pkg/enums/taskstatus"
+)
+
+func CreateTaskRecord(ctx context.Context, record *TaskRecord) error {
+ return db.WithContext(ctx).Create(record).Error
+}
+
+func UpdateTaskRecord(ctx context.Context, taskID string, fields map[string]any) error {
+ return db.WithContext(ctx).Model(&TaskRecord{}).Where("task_id = ?", taskID).Updates(fields).Error
+}
+
+func GetTaskRecord(ctx context.Context, taskID string) (*TaskRecord, error) {
+ var record TaskRecord
+ if err := db.WithContext(ctx).Where("task_id = ?", taskID).First(&record).Error; err != nil {
+ return nil, err
+ }
+ return &record, nil
+}
+
+func ListTaskRecords(ctx context.Context, filter TaskFilter) ([]TaskRecord, int64, error) {
+ query := db.WithContext(ctx).Model(&TaskRecord{})
+ if filter.Status != "" {
+ query = query.Where("status = ?", filter.Status)
+ }
+ if filter.Type != "" {
+ query = query.Where("type = ?", filter.Type)
+ }
+ var total int64
+ if err := query.Count(&total).Error; err != nil {
+ return nil, 0, err
+ }
+ limit := filter.Limit
+ if limit <= 0 || limit > 200 {
+ limit = 50
+ }
+ var records []TaskRecord
+ err := query.Order("created_at DESC").Limit(limit).Offset(filter.Offset).Find(&records).Error
+ return records, total, err
+}
+
+// ListQueuedTaskRecords returns every task that is queued for execution,
+// including tasks requeued after an interrupted run, oldest first.
+func ListQueuedTaskRecords(ctx context.Context) ([]TaskRecord, error) {
+ var records []TaskRecord
+ err := db.WithContext(ctx).
+ Where("status = ?", taskstatus.Queued.String()).
+ Order("created_at ASC").
+ Find(&records).Error
+ return records, err
+}
+
+// HasUnfinishedTasks reports whether any task is queued or running.
+func HasUnfinishedTasks(ctx context.Context) (bool, error) {
+ var count int64
+ err := db.WithContext(ctx).Model(&TaskRecord{}).
+ Where("status IN ?", []string{taskstatus.Queued.String(), taskstatus.Running.String()}).
+ Count(&count).Error
+ return count > 0, err
+}
+
+func DeleteTaskRecord(ctx context.Context, taskID string) error {
+ return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Unscoped().Where("task_id = ?", taskID).Delete(&TaskItemMetadata{}).Error; err != nil {
+ return err
+ }
+ if err := tx.Unscoped().Where("task_id = ?", taskID).Delete(&TaskItemRecord{}).Error; err != nil {
+ return err
+ }
+ return tx.Unscoped().Where("task_id = ?", taskID).Delete(&TaskRecord{}).Error
+ })
+}
+
+// ReplaceTaskItems stores the final outcome of every file in a task,
+// replacing any previous rows for the same task.
+func ReplaceTaskItems(ctx context.Context, taskID string, items []TaskItemRecord) error {
+ if len(items) == 0 {
+ return nil
+ }
+ return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Unscoped().Where("task_id = ?", taskID).Delete(&TaskItemRecord{}).Error; err != nil {
+ return err
+ }
+ for i := range items {
+ items[i].TaskID = taskID
+ }
+ return tx.CreateInBatches(items, 100).Error
+ })
+}
+
+func CountFailedTaskItems(ctx context.Context, taskIDs []string) (map[string]int, error) {
+ counts := make(map[string]int, len(taskIDs))
+ if len(taskIDs) == 0 {
+ return counts, nil
+ }
+ var rows []struct {
+ TaskID string
+ Count int
+ }
+ err := db.WithContext(ctx).Model(&TaskItemRecord{}).
+ Select("task_id, COUNT(*) AS count").
+ Where("task_id IN ? AND status = ?", taskIDs, taskstatus.Failed.String()).
+ Group("task_id").
+ Scan(&rows).Error
+ if err != nil {
+ return nil, err
+ }
+ for _, row := range rows {
+ counts[row.TaskID] = row.Count
+ }
+ return counts, nil
+}
+
+func ListTaskItems(ctx context.Context, taskID string) ([]TaskItemRecord, error) {
+ var items []TaskItemRecord
+ if err := db.WithContext(ctx).Where("task_id = ?", taskID).Order("id ASC").Find(&items).Error; err != nil {
+ return nil, err
+ }
+ return items, nil
+}
+
+// ListFailedTaskItems returns failed items for the given task IDs, capped per
+// task so a large batch cannot blow up a list response.
+func ListFailedTaskItems(ctx context.Context, taskIDs []string, perTask int) (map[string][]TaskItemRecord, error) {
+ out := make(map[string][]TaskItemRecord, len(taskIDs))
+ if len(taskIDs) == 0 {
+ return out, nil
+ }
+ if perTask <= 0 {
+ perTask = 20
+ }
+ // Cap rows per task in SQL so a task with a large number of failures
+ // cannot make every list request load all of them.
+ const query = `
+SELECT * FROM (
+ SELECT task_items.*, ROW_NUMBER() OVER (PARTITION BY task_id ORDER BY id) AS row_num
+ FROM task_items
+ WHERE task_id IN ? AND status = ?
+) WHERE row_num <= ?`
+ var items []TaskItemRecord
+ if err := db.WithContext(ctx).Raw(query, taskIDs, taskstatus.Failed.String(), perTask).Scan(&items).Error; err != nil {
+ return nil, err
+ }
+ for _, item := range items {
+ group := out[item.TaskID]
+ if len(group) >= perTask {
+ continue
+ }
+ out[item.TaskID] = append(group, item)
+ }
+ return out, nil
+}
+
+// MarkInterruptedTaskRecords requeues tasks that were running when the
+// process stopped so they can be rebuilt from their persisted parameters.
+func MarkInterruptedTaskRecords(ctx context.Context) error {
+ return db.WithContext(ctx).Model(&TaskRecord{}).
+ Where("status = ?", taskstatus.Running.String()).
+ Updates(map[string]any{
+ "status": taskstatus.Queued.String(),
+ "error": "",
+ "started_at": nil,
+ "finished_at": nil,
+ }).Error
+}
diff --git a/database/user.go b/database/user.go
index 7f631b5e..4279403a 100644
--- a/database/user.go
+++ b/database/user.go
@@ -4,13 +4,15 @@ import (
"context"
"gorm.io/gorm/clause"
+
+ "github.com/krau/SaveAny-Bot/pkg/enums/storagefilter"
)
func CreateUser(ctx context.Context, chatID int64) error {
if _, err := GetUserByChatID(ctx, chatID); err == nil {
return nil
}
- return db.Create(&User{ChatID: chatID}).Error
+ return db.Create(&User{ChatID: chatID, StorageFilterMode: storagefilter.Blacklist}).Error
}
func GetAllUsers(ctx context.Context) ([]User, error) {
diff --git a/database/websession.go b/database/websession.go
new file mode 100644
index 00000000..deca8395
--- /dev/null
+++ b/database/websession.go
@@ -0,0 +1,30 @@
+package database
+
+import (
+ "context"
+ "time"
+)
+
+func CreateWebSession(ctx context.Context, session *WebSession) error {
+ return db.WithContext(ctx).Create(session).Error
+}
+
+func GetWebSession(ctx context.Context, id string) (*WebSession, error) {
+ var session WebSession
+ if err := db.WithContext(ctx).Where("id = ? AND expires_at > ?", id, time.Now()).First(&session).Error; err != nil {
+ return nil, err
+ }
+ return &session, nil
+}
+
+func DeleteWebSession(ctx context.Context, id string) error {
+ return db.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&WebSession{}).Error
+}
+
+func DeleteOtherWebSessions(ctx context.Context, keepID string) error {
+ return db.WithContext(ctx).Unscoped().Where("id <> ?", keepID).Delete(&WebSession{}).Error
+}
+
+func DeleteExpiredWebSessions(ctx context.Context) error {
+ return db.WithContext(ctx).Unscoped().Where("expires_at <= ?", time.Now()).Delete(&WebSession{}).Error
+}
diff --git a/docker-compose.local.yml b/docker-compose.local.yml
index 5659d1e7..3bcf458d 100644
--- a/docker-compose.local.yml
+++ b/docker-compose.local.yml
@@ -4,12 +4,18 @@ services:
container_name: saveany-bot
restart: unless-stopped
ports:
- - "8080:8080"
+ - "28080:28080"
+ # Web UI 默认仅监听 127.0.0.1:28080;带访问 Token 的地址会打印在日志中
+ # The Web UI binds to 127.0.0.1:28080 by default; the tokenized URL is printed in the log
+ # 如需对外开放,取消注释并设置 SAVEANY_LISTEN
+ # To expose it, uncomment SAVEANY_LISTEN
+ # environment:
+ # SAVEANY_ADMIN_PASSWORD: change-me
+ # SAVEANY_LISTEN: 0.0.0.0:28080
volumes:
- ./data:/app/data
- - ./config.toml:/app/config.toml
- ./downloads:/app/downloads
- ./cache:/app/cache
# 使用 host 模式以便访问宿主机服务 (如代理)
# 如果你对 Docker 网络模式熟悉, 可以自行修改
- network_mode: host
\ No newline at end of file
+ network_mode: host
diff --git a/docker-compose.yml b/docker-compose.yml
index 2e358555..ba7374a9 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -3,13 +3,17 @@ services:
image: ghcr.io/krau/saveany-bot:latest
container_name: saveany-bot
restart: unless-stopped
- ports:
- - "8080:8080"
+ # Web UI 默认仅监听 127.0.0.1:28080;带访问 Token 的地址会打印在日志中
+ # The Web UI binds to 127.0.0.1:28080 by default; the tokenized URL is printed in the log
+ # 如需对外开放,取消注释并设置 SAVEANY_LISTEN
+ # To expose it, uncomment SAVEANY_LISTEN
+ # environment:
+ # SAVEANY_ADMIN_PASSWORD: change-me
+ # SAVEANY_LISTEN: 0.0.0.0:28080
volumes:
- ./data:/app/data
- - ./config.toml:/app/config.toml
- ./downloads:/app/downloads
- ./cache:/app/cache
# 使用 host 模式以便访问宿主机服务 (如代理)
# 如果你对 Docker 网络模式熟悉, 可以自行修改
- network_mode: host
\ No newline at end of file
+ network_mode: host
diff --git a/docs/content/en/contribute/_index.md b/docs/content/en/contribute/_index.md
index e7f84d5a..21240092 100644
--- a/docs/content/en/contribute/_index.md
+++ b/docs/content/en/contribute/_index.md
@@ -12,6 +12,7 @@ Here are some guidelines and suggestions for contributing code. You don't have t
- **Open an issue before adding new features**, so we can discuss design and implementation details and avoid work that doesn't fit the project design.
- **Use modern development tools**, format your code before committing, and keep the style consistent.
- **Use [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)**, and avoid vague or overly simple commit messages.
+- **See [Testing](testing) for the seeded environment and end-to-end suites.**
## Contributing New Storage Backend
diff --git a/docs/content/en/contribute/testing.md b/docs/content/en/contribute/testing.md
new file mode 100644
index 00000000..cb6deb27
--- /dev/null
+++ b/docs/content/en/contribute/testing.md
@@ -0,0 +1,80 @@
+---
+title: "Testing"
+weight: 10
+---
+
+# Testing
+
+The project ships a seeded local environment and two end-to-end suites that do
+not need Telegram credentials.
+
+## Seeded environment
+
+```bash
+make testenv
+```
+
+The command creates a disposable data directory and prints:
+
+- the Web UI URL
+- the admin password (`saveany-test` by default)
+- a seeded local storage and whitelisted user
+- a sample URL that can be used as a direct-links task
+
+Use `--keep` to retain the generated data directory, or pass `--data-dir` to
+reuse an existing one. Telegram is left stopped, so the web console and the
+non-Telegram task pipeline are available immediately.
+
+## Backend end-to-end tests
+
+```bash
+make e2e-api
+```
+
+The suite builds the binary, starts the seeded environment on an ephemeral
+port and drives the public API with a real HTTP client. It covers authentication
+and CSRF, onboarding, storage and user lifecycle, settings updates, the SPA
+asset fallback, and a direct-links task that downloads from a local HTTP server
+into the seeded storage.
+
+The tests live under `e2e/` behind the `e2e` build tag, so `go test ./...` does
+not start the environment.
+
+## Telegram end-to-end tests
+
+```bash
+make e2e-telegram
+```
+
+This suite runs the real bot handlers, dispatcher, task queue, and storage
+backends against an in-memory `tg.Invoker`. It covers `/start`, group admission,
+saving a Telegram document to local storage (including progress edits), and
+uploading it to a Telegram storage backend. No Telegram account or network is
+used: gotd's client, sender, downloader, and uploader run unchanged against the
+fake RPC layer.
+
+## Web end-to-end tests
+
+```bash
+make e2e-web-setup # once, downloads the Chromium build
+make e2e-web
+```
+
+The suite starts the seeded environment and drives the built web UI with
+Playwright. It covers login, storage testing and creation, and creating a
+direct-links task that reaches the completed state. Test artifacts are written
+to `tmp/e2e-web-results/`.
+
+Playwright is a dev dependency of `web/ui` and its version is locked by
+`pnpm-lock.yaml`. `make e2e-web-setup` installs the matching Chromium build.
+
+## Combined
+
+```bash
+make e2e
+```
+
+Runs the API, Telegram, and browser suites after building the web UI. The fake
+Telegram layer covers commands, downloads, uploads, and group admission without
+an account. Real Telegram server semantics, userbot login, and live rate limits
+still need a test account.
diff --git a/docs/content/en/deployment/configuration/_index.md b/docs/content/en/deployment/configuration/_index.md
index 65387045..9923da53 100644
--- a/docs/content/en/deployment/configuration/_index.md
+++ b/docs/content/en/deployment/configuration/_index.md
@@ -1,270 +1,106 @@
---
-title: "Configuration Guide"
+title: "Configuration"
+weight: 11
---
-# Configuration Guide
-
-SaveAnyBot uses the toml format for its configuration files. You can learn more about toml syntax on the [TOML official website](https://toml.io/).
-
-SaveAnyBot needs to read a `config.toml` file in the working directory as its configuration file. If this file is missing, a default file will be created, and the bot will attempt to load configuration from environment variables.
-
-Here is an example of a minimal configuration file:
-
-```toml
-[telegram]
-token = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
-[[users]]
-# telegram user id
-id = 777000
-blacklist = true
-
-[[storages]]
-name = "Local Storage"
-type = "local"
-enable = true
-base_path = "./downloads"
-```
-
-## Detailed Configuration
-
-### Global Configuration
-
-- `lang`: The language used by the Bot, default is `zh-CN` (Simplified Chinese). `en` is used for English.
-- `stream`: Whether to enable Stream mode, default is `false`. When enabled, the Bot will stream files directly to storage endpoints (if supported), without downloading them locally.
-{{< hint warning >}}
-Stream mode is very useful for deployment environments with limited disk space, but it also has some drawbacks:
-
-
-Cannot use multi-threading to download files from Telegram, resulting in slower speeds.
-Higher task failure rate when the network is unstable.
-Cannot process files in the middle layer, such as automatic file type identification.
-Not supported by all storage endpoints; unsupported endpoints may downgrade to normal mode or fail to upload.
-
-{{< /hint >}}
-- `workers`: Number of tasks to process simultaneously, default is 3.
-- `threads`: Number of threads used when downloading files, default is 4. Only effective when Stream mode is not enabled.
-- `retry`: Number of retries when a task fails, default is 3.
-- `proxy`: Global proxy configuration. After setting this, all network connections inside the program will try to use this proxy. Optional.
-
-```toml
-lang = "en"
-stream = false
-workers = 3
-threads = 4
-retry = 3
-proxy = "socks5://127.0.0.1:7890"
-```
-
-### Telegram Configuration
-
-- `token`: Your Telegram Bot Token, which can be obtained by creating a Bot through [BotFather](https://t.me/botfather).
-- `app_id`, `app_hash`: Telegram API ID & Hash, obtained by creating an application at [Telegram API](https://my.telegram.org/apps). Default values will be used if not provided.
-- `flood_retry`: Number of retries for flood control, default is 5.
-- `rpc_retry`: Number of retries for RPC requests, default is 5.
-- `proxy`: Proxy configuration, optional.
- - `enable`: Whether to enable the proxy.
- - `url`: Proxy address
-- `userbot`: Userbot configuration, optional.
- - `enable`: Enable userbot integration. Requires logging in with a user account; you should use your own API ID & Hash when enabling this.
- - `session`: Path to the userbot session file, default is `data/usersession.db`.
-
-{{< hint warning >}}
-After enabling userbot integration, the bot can download files from private channels and groups, but there is an unavoidable risk of the account being banned.
-
-On the first start after enabling userbot, you need to input phone number, 2FA and verification code in the terminal.
-
-If you deploy with Docker, please run the container with `-it` for an interactive environment, then perform the login.
-{{< /hint >}}
-
-```toml
-[telegram]
-token = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-app_id = 1025907
-app_hash = "452b0359b988148995f22ff0f4229750"
-flood_retry = 5
-rpc_retry = 5
-[telegram.proxy]
-enable = false
-url = "socks5://127.0.0.1:7890"
-[telegram.userbot]
-enable = false
-session = "data/usersession.db"
-```
-
-### Aria2 Configuration
-
-Aria2 is a powerful download manager that supports HTTP/HTTPS, FTP, BitTorrent, and other protocols. When enabled, the bot can use the `/aria2dl` command to download files via Aria2.
-
-- `enable`: Whether to enable Aria2 support, default is `false`
-- `url`: Aria2 RPC address, typically `http://localhost:6800/jsonrpc`
-- `secret`: Aria2 RPC secret, if you configured `rpc-secret` in Aria2, you need to fill it in here
-- `remove_after_transfer`: Whether to remove local files downloaded by Aria2 after transfer, default is `true`
-
-{{< hint info >}}
-Aria2 needs to be installed and running separately. You can refer to the [Aria2 official documentation](https://aria2.github.io/) to learn how to install and configure Aria2.
-{{< /hint >}}
-
-```toml
-[aria2]
-enable = true
-url = "http://localhost:6800/jsonrpc"
-secret = "your-rpc-secret"
-remove_after_transfer = true
-```
-
-### yt-dlp Configuration
-
-Configures the behavior of the `/ytdlp` command and the `ytdlp` HTTP-API task type when no custom flags are passed.
-
-- `max_height`: Default maximum video resolution by height in pixels (e.g. `1080`, `720`). `0` means no limit (best available). Ignored when `format` is set.
-- `format`: A raw yt-dlp format selector (`-f`). When set, it takes precedence over `max_height` and gives you full control, e.g. `bv*[height<=720]+ba/b`.
-- `recode`: The target video container yt-dlp recodes into after download (e.g. `mp4`). Leave empty to disable recoding.
-- `filename_template`: yt-dlp output template for downloaded file names, default `%(title)s.%(ext)s`. Directories in the template are created below the bot's download directory and kept in the storage path.
-- `restrict_filenames`: Restrict file names to ASCII characters (yt-dlp `--restrict-filenames`), which also drops spaces and `&`. Disabled by default.
-
-{{< hint info >}}
-`max_height`, `format` and `recode` only apply when using the `/ytdlp` command (or the `ytdlp` API task type) without passing any custom flags. `filename_template` and `restrict_filenames` also apply when other flags are passed, unless you pass the corresponding flag yourself (`-o/--output`, `--restrict-filenames` or `--no-restrict-filenames`).
-{{< /hint >}}
-
-```toml
-[ytdlp]
-max_height = 1080
-format = "" # e.g. "bv*[height<=720]+ba/b"
-recode = "mp4" # empty disables recoding
-filename_template = "%(title)s.%(ext)s" # e.g. "%(uploader)s - %(title)s.%(ext)s"
-restrict_filenames = false
-```
-
-### HTTP API Configuration
-
-When enabled, SaveAny-Bot exposes an HTTP API for creating/querying/canceling tasks programmatically. See [HTTP API](../../usage/api) for the full endpoint reference.
-
-- `enable`: Whether to enable the HTTP API server, default is `false`.
-- `host`: Bind address, default `0.0.0.0`.
-- `port`: Listen port, default `8080`.
-- `token`: Authentication token. **Strongly recommended** — if empty, the API is exposed without any authentication.
-
-```toml
-[api]
-enable = false
-host = "0.0.0.0"
-port = 8080
-token = "your-token"
-```
-
-### Log Configuration
-
-- `level`: Log level. One of `debug`, `info`, `warn`, `error`, `fatal`. Default is `info`.
-
-```toml
-[log]
-level = "info"
-```
-
-### Storage Endpoints List
-
-The storage endpoints list is used to define the storage locations supported by the Bot. Each storage endpoint needs to specify a name, type, and related configuration, using the double bracket syntax `[[storages]]`.
-
-Each storage endpoint requires at least the following fields:
-
-- `name`: Storage endpoint name, used for identification in the Bot, must be unique.
-- `enable`: Whether to enable this storage endpoint, default is `true`.
-- `type`: Storage endpoint type, currently supports the following types:
- - `local`: Local disk
- - `alist`: Alist
- - `webdav`: WebDAV
- - `s3`: aws S3 and other S3 compatible services
- - `rclone`: Uses rclone to implement uploads
- - `telegram`: Upload to Telegram
-
-Example, this is a configuration that includes local storage and webdav storage:
-
-```toml
-[[storages]]
-name = "Local Storage"
-type = "local"
-enable = true
-# Custom configuration for local type storage
-base_path = "./downloads"
-
-[[storages]]
-name = "WebDAV"
-type = "webdav"
-enable = true
-# Custom configuration for webdav type storage
-url = "https://example.com/webdav"
-base_path = "/path/to/webdav"
-username = "your_username"
-password = "your_password"
-```
-
-For custom configuration items for all storage endpoints, see [Storage Configuration](./storages)
-
-### User List
-
-The user list is used to define access control for storage endpoints. Each user needs to specify a Telegram User ID, defined using the double bracket syntax `[[users]]`.
-
-- `id`: The user's Telegram User ID
-- `storages`: Filtered list of storage endpoints, defined by storage endpoint names, default is whitelist mode (i.e., only allows access to storage endpoints in the list)
-- `blacklist`: Whether to enable blacklist mode, default is `false`. If blacklist mode is enabled, the user is allowed to access only storage endpoints that are **not** in the list.
-
-Example, this is a configuration containing three users: user `123123` can only access local storage, user `456456` can only access storage other than WebDAV, and user `789789` has blacklist mode enabled but no storage endpoints specified, so they can access all storage:
+# Configuration
-```toml
-[[users]]
-id = 123123
-storages = ["Local Storage"]
-
-[[users]]
-id = 456456
-storages = ["WebDAV"]
-blacklist = true
-
-[[users]]
-id = 789789
-storages = []
-blacklist = true
-```
+Settings are stored in the SQLite database under the data directory (default `data/saveany.db`) and managed from the web console. The first start uses defaults and prints a direct web URL with an access token; set `SAVEANY_ADMIN_PASSWORD` to choose the fallback password.
-### Events
-
-Event hooks allow you to run custom commands based on task status while the bot is processing tasks. Currently only arbitrary command execution is supported, configured via `[hook.exec]`.
+## Configuration pages
-Supported event types:
+| Page | Contents |
+|---|---|
+| Guide | First-run checklist and everyday usage |
+| Telegram | Bot token, API ID/hash, proxy, Userbot login |
+| Storages | Local disk, WebDAV, Alist, S3, Rclone, Telegram, and more |
+| Users | Telegram user IDs, default storage, directories, rules, and storage access |
+| Automation | Per-user directories, rules, watched chats, and peer sync |
+| Settings | Task concurrency, language, proxy, media type filter, Aria2, yt-dlp, parsers, cache, logging, web, and API |
+| Parsers | Install JavaScript parser plugins |
+| System | Runtime information, restart, version checks, and updates |
-- `task_before_start`: Before a task starts
-- `task_success`: After a task completes successfully
-- `task_fail`: After a task fails
-- `task_cancel`: After a task is cancelled
+Some settings require a process restart, including task concurrency, global proxy, database session path, cache settings, plugins, hooks, and the web listen address. The page marks fields that need a restart.
-The configured value must be a full shell command line. The bot will execute this command when the event occurs. Example:
+### Automation
-```toml
-[hook.exec]
-task_before_start = "echo 'task is about to start'"
-task_success = "bash /path/to/success_script.sh"
-task_fail = "curl -X POST https://example.com/api/notify -d 'task failed'"
-task_cancel = "bash /path/to/cancel_script.sh"
-```
+The Automation page manages a selected user's directories, rules, watched chats, and Userbot peer sync. Directories can be marked as the default destination; rules can be enabled, disabled, added, deleted, or imported from presets.
### Parsers
-Parsers give the bot the ability to handle non-Telegram files, such as downloading files from other websites. Configure them via `[parsers]`.
+The Parsers page installs JavaScript parser plugins when parser plugins are enabled in Settings. Upload a `.js` plugin file or paste its code; it becomes available without a restart.
-```toml
-[parsers]
-plugin_enable = true # Whether to enable parser plugins
-plugin_dirs = ["./plugins"] # Plugin directories, can be multiple
-```
+### Telegram
-The above settings only control JavaScript-based parser plugins. The bot also has built-in parsers implemented in Go, which are enabled by default.
+Create a bot with [@BotFather](https://t.me/BotFather). `API ID` and `API Hash` have defaults and usually need no changes; use your own [Telegram API](https://my.telegram.org/apps) credentials when enabling the Userbot.
-### Miscellaneous
+Saving bot settings starts the connection automatically. Userbot login happens on the Telegram page, with no terminal interaction inside the container.
-```toml
-no_clean_cache = false # Whether not to clear the cache folder when exiting
-# Temporary download folder configuration
-[temp]
-base_path = "./cache"
-```
\ No newline at end of file
+### Helper bots
+
+Helper bots are optional. Add them in the **Helper bots** section of the **Telegram** page with a BotFather token, then add each bot to the destination Telegram chat with permission to send files. Telegram storage uploads are distributed across running helpers that can access the destination; helpers without access are skipped and uploads fall back to the primary bot. Helper bots do not handle commands.
+
+To split large file downloads across helpers, set a private **Relay channel ID** on the **Telegram** page, make sure the primary bot can post there, and add at least two helper bots to that channel. The primary bot forwards incoming files to the relay channel, and each helper obtains its own file reference before downloading assigned chunks. This requires non-stream mode and files at or above the configurable **Relay minimum file size** (8 MB by default), and falls back to the single-client downloader when the relay or helper pool is unavailable. Forwarded relay messages are kept by default; disable **Keep forwarded channel messages** to delete them after download.
+
+Transfer concurrency can be tuned under **Settings → Core**:
+
+| Setting | Default | Meaning |
+|---|---|---|
+| `Download threads per file` | 4 | Base threads used by automatic scheduling |
+| `Download thread override` | 0 | 0 uses automatic sizing; a positive value is the exact total thread count |
+| `Automatic thread cap` | 16 | Maximum threads when automatic sizing is used |
+| `Global transfer budget` | 0 | 0 uses `workers × threads`; downloads and uploads share this cap |
+| `Relay minimum file size (MB)` | 8 | Smaller files skip helper relays |
+| `Chunk retry count` | 20 | Retries for timeouts or connection failures on one chunk |
+
+### Media filter
+
+Use **Settings → Media filter** to restrict the bot to selected Telegram media types. Enter one value per line under **Allowed media types**; leave the field empty to allow everything:
+
+| Value | Matches |
+|---|---|
+| `image` | Telegram photos, image documents, stickers, and animated images |
+| `video` | Telegram video documents, including video messages |
+| `audio` | Telegram audio and voice messages |
+| `document` | Other document files |
+
+Filtering happens before download tasks are created. Non-matching media is not downloaded and does not appear in the task list. Media groups keep only matching items; a group with no matching items is skipped silently. This is an instance-wide setting and takes effect immediately.
+
+The filter currently covers Telegram media entry points: direct and forwarded media, media groups, batch `/save`, message links, watched chats, and Telegraph images. Parser results, direct HTTP links, and yt-dlp output are not filtered. Storage rules only change the destination path; they do not skip files.
+
+### Message metadata
+
+Enable **Settings → Message metadata** to store normalized Telegram message data for new Telegram file tasks. The local database keeps source chat, message ID, date, sender, text or caption, reply/forward references, album grouping, and media attributes. The task details API and Web console can export the stored JSON.
+
+**Store message text** controls whether the message body and extracted tags are kept. Access hashes, tokens, raw Telegram objects, and old task data are not stored. Metadata applies only to files saved after the setting is enabled.
+
+**Write sidecar JSON** writes `.meta.json` next to each saved file. It is off by default. Telegram storage does not upload the sidecar; it can instead consume the metadata to restore captions or media groups using the Telegram storage options.
+
+### Storages
+
+Add and enable storages on the **Storages** page. Local disk needs only a base path:
+
+- In Docker, usually `/app/downloads`.
+- For a binary deployment, use `./downloads` or an absolute path.
+
+See [Storage configuration](./storages) for backend fields.
+
+### Users
+
+Only enabled Telegram user IDs on the **Users** page can use the bot. A new user needs at least the numeric user ID; the default storage can be set later.
+
+## Environment variables
+
+| Variable | Purpose |
+|---|---|
+| `SAVEANY_DATA_DIR` | Data directory, default `./data` |
+| `SAVEANY_DB_PATH` | SQLite file path, default `/saveany.db` |
+| `SAVEANY_LISTEN` | Web listen address, default `127.0.0.1:28080`; use `0.0.0.0:28080` to expose it |
+| `SAVEANY_ADMIN_PASSWORD` | Set the fallback admin password on first start; otherwise one is generated and logged |
+
+## Security
+
+- The web console binds to `127.0.0.1:28080` by default. Expose it publicly only through a reverse proxy with HTTPS.
+- The `Access token` setting enables direct Web UI access through `/?token=...` and Bearer authentication for `/api/v1`. When empty, a token is generated on every start and printed in the log.
+- Change the generated fallback admin password immediately after the first password login.
+- Never expose `data/`, bot tokens, Userbot sessions, or access tokens.
diff --git a/docs/content/en/deployment/configuration/storages.md b/docs/content/en/deployment/configuration/storages.md
index 44d7780f..4771fad5 100644
--- a/docs/content/en/deployment/configuration/storages.md
+++ b/docs/content/en/deployment/configuration/storages.md
@@ -4,7 +4,9 @@ title: "Storage Configuration"
# Storage Configuration
-Please first read the [Configuration Guide](../) to understand the basic format of the configuration file.
+Add storages on the **Storages** page in the web console and fill in the fields below.
+
+Storage names can contain spaces, Unicode, and symbols (up to 64 characters, excluding control characters). Quote names containing spaces when using bot commands such as `/dirs` or `/transfer`. Rename a storage on the same page at any time; user defaults, directories, rules, and storage filters reference the stable storage ID, so they are unaffected by the name change.
## Alist
@@ -12,7 +14,7 @@ Please first read the [Configuration Guide](../) to understand the basic format
Stream mode is not supported.
-```toml
+```text
url = "https://alist.example.com" # URL of Alist
username = "your_username" # Username for Alist
password = "your_password" # Password for Alist
@@ -27,14 +29,14 @@ token = "your_token"
`type=local`
-```toml
+```text
base_path = "./downloads" # Base path for local storage, all files will be stored under this path
```
## WebDAV
`type=webdav`
-```toml
+```text
url = "https://webdav.example.com" # URL of WebDAV
username = "your_username" # Username for WebDAV
password = "your_password" # Password for WebDAV
@@ -45,8 +47,8 @@ base_path = "/path/to/webdav" # Base path in WebDAV, all files will be stored un
`type=s3`
-```toml
-endpoint = "s3.example.com" # Endpoint for S3, defaults to AWS S3 endpoint if not set
+```text
+endpoint = "https://s3.example.com" # Endpoint for S3, defaults to AWS S3 endpoint if not set
region = "us-east-1" # Region for S3
access_key_id = "your_access_key_id" # Access key ID for S3
secret_access_key = "your_secret_access_key" # Secret access key for S3
@@ -75,21 +77,25 @@ If you are using a third-party S3-compatible service, it usually uses path-style
Stream mode is not supported.
-```toml
-chat_id = "123456789" # Telegram chat ID, the bot will send files to this chat
+```text
+chat_id = "-1001234567890" # Bot API chat ID (positive for users, negative for basic groups, -100... for channels/supergroups)
force_file = false # Force sending as file, default is false
skip_large = false # Skip large files, default is false. If enabled, files exceeding Telegram's limit will not be uploaded.
split_large_video = false # Losslessly split oversized videos into one album of up to 10 independently playable parts. Falls back to ZIP parts on failure or when more than 10 parts are required.
split_size_mb = 0 # Split size in MB. 0 uses the uploader account limit: 2000 MB for bots/regular users and 4000 MB for Premium users. Oversized non-video files use ZIP parts. Ignored when skip_large is true.
+use_metadata_caption = false # Use metadata text as caption when the live source caption is absent
+use_metadata_album = false # Use metadata grouped_id to preserve source media groups when live grouping is absent
```
+When message metadata is enabled, Telegram storage consumes it directly instead of uploading a `.meta.json` file. `use_metadata_caption` restores the original message text as the caption when the live source caption is unavailable; `use_metadata_album` uses metadata `grouped_id` to send the files as a media group when live grouping is unavailable. Both options are off by default. For albums, the caption is attached only to the first item, matching Telegram client behavior.
+
## Rclone
`type=rclone`
Supports multiple cloud storage services through the [rclone](https://rclone.org/) command-line tool. You need to install rclone and configure remote storage first.
-```toml
+```text
# Remote name configured in rclone, can be any remote defined in rclone.conf
remote = "mydrive"
# Base path in the remote storage, all files will be stored under this path
@@ -117,7 +123,7 @@ rclone supports many cloud storage services, including but not limited to:
After configuring Google Drive, you can configure the storage like this:
-```toml
+```text
[[storages]]
name = "GoogleDrive"
type = "rclone"
@@ -128,7 +134,7 @@ base_path = "/SaveAnyBot"
If using a custom rclone config file:
-```toml
+```text
[[storages]]
name = "MyRemote"
type = "rclone"
diff --git a/docs/content/en/deployment/installation.md b/docs/content/en/deployment/installation.md
index 89e986f7..f40dc357 100644
--- a/docs/content/en/deployment/installation.md
+++ b/docs/content/en/deployment/installation.md
@@ -1,30 +1,55 @@
---
-title: "Installation and Updates"
+title: "Installation and updates"
+weight: 10
---
-# Installation and Updates
+# Installation and updates
-## Deploy from Pre-compiled Binary (Recommended)
+Start with an empty data directory, then configure the bot, storage, and users in the web console.
-Download the binary file for your platform from the [Release](https://github.com/krau/SaveAny-Bot/releases) page.
+## Binary deployment
-Create a `config.toml` file in the extracted directory, refer to the [Configuration Guide](../configuration) to edit the configuration file.
-
-Run:
+Download and extract a release for your platform:
```bash
chmod +x saveany-bot
./saveany-bot
```
-### Daemon
+The default data directory is `data/` under the current working directory. The web UI listens on `127.0.0.1:28080` by default. At startup it prints a direct URL with an access token:
+
+```text
+Web UI available at http://127.0.0.1:28080/?token=...
+```
+
+Open that URL; the token creates an authenticated session. Unless an access token is set under **Settings → Web**, a new token is generated on every start.
+
+To expose the web UI on another interface or port, pass the listen address on the command line:
+
+```bash
+./saveany-bot --listen 0.0.0.0:28080
+```
+
+The equivalent environment variable is `SAVEANY_LISTEN`:
+
+```bash
+SAVEANY_LISTEN='0.0.0.0:28080' ./saveany-bot
+```
+
+The generated admin password remains available for password login; set `SAVEANY_ADMIN_PASSWORD` to choose it. It is printed only on the first start. After signing in, change it from the sidebar **Change password** dialog. Open the logged URL and follow the **Guide** page:
+
+1. Enter the bot token on the Telegram page.
+2. Add at least one storage on the Storages page.
+3. Add your Telegram user ID on the Users page.
+
+### Process supervision
{{< tabs "daemon" >}}
-{{< tab "systemd (Regular Linux)" >}}
+{{< tab "systemd (Linux)" >}}
-Create a file /etc/systemd/system/saveany-bot.service and write the following content:
+Create `/etc/systemd/system/saveany-bot.service`:
-{{< codeblock >}}
+```ini
[Unit]
Description=SaveAnyBot
After=systemd-user-sessions.service
@@ -37,135 +62,97 @@ Restart=always
[Install]
WantedBy=multi-user.target
-{{< /codeblock >}}
-
-Enable startup on boot and start the service:
+```
-{{< codeblock >}}
+```bash
systemctl enable --now saveany-bot
-{{< /codeblock >}}
+```
{{< /tab >}}
-
{{< tab "procd (OpenWrt)" >}}
-Add Boot Autostart Service
-
-Create a file /etc/init.d/saveanybot, refer to wrt_init and modify as needed:
+Create `/etc/init.d/saveanybot`, based on [wrt_init](https://github.com/krau/SaveAny-Bot/blob/main/docs/confs/wrt_init), and adjust the working directory and executable path:
-{{< codeblock >}}
+```sh
#!/bin/sh /etc/rc.common
-
-#This is the OpenWRT init.d script for SaveAnyBot
-
-START=99
+START=99
STOP=10
description="SaveAnyBot"
-
WORKING_DIR="/mnt/mmc1-1/SaveAnyBot"
EXEC_PATH="$WORKING_DIR/saveany-bot"
-start() {
- echo "Starting SaveAnyBot..."
- cd $WORKING_DIR
- $EXEC_PATH &
-}
-stop() {
- echo "Stopping SaveAnyBot..."
- killall saveany-bot
-}
-reload() {
- stop
- start
-}
-
-{{< /codeblock >}}
-
-Set permissions:
-
-{{< codeblock >}}
-chmod +x /etc/init.d/saveanybot
-{{< /codeblock >}}
-
-Then copy the file to /etc/rc.d and rename it to S99saveanybot, also set permissions:
-
-{{< codeblock >}}
-chmod +x /etc/rc.d/S99saveanybot
-{{< /codeblock >}}
-
-Add Shortcut Commands
-
-Create a file /usr/bin/sabot, refer to wrt_bin and modify as needed. Note that the file encoding here only supports ANSI 936.
-
-Then set permissions:
-
-{{< codeblock >}}
-chmod +x /usr/bin/sabot
-{{< /codeblock >}}
+start() { cd $WORKING_DIR && $EXEC_PATH & }
+stop() { killall saveany-bot; }
+```
-Usage: sudo sabot start|stop|restart|status|enable|disable
+```bash
+chmod +x /etc/init.d/saveanybot
+ln -s /etc/init.d/saveanybot /etc/rc.d/S99saveanybot
+```
{{< /tab >}}
{{< /tabs >}}
-
-## Deploy Using Docker
+## Docker deployment
### Docker Compose
-Download the [docker-compose.yml](https://github.com/krau/SaveAny-Bot/blob/main/docker-compose.yml) file, create a new `config.toml` file in the same directory, refer to [config.example.toml](https://github.com/krau/SaveAny-Bot/blob/main/config.example.toml) to edit the configuration file.
-
-Start:
+Download [docker-compose.yml](https://github.com/krau/SaveAny-Bot/blob/main/docker-compose.yml), adjust ports and paths if needed, then start:
```bash
docker compose up -d
+docker compose logs -f saveany-bot
+```
+
+Default mounts:
+
+- `./data:/app/data` — settings, task history, and sessions
+- `./downloads:/app/downloads` — default local storage
+- `./cache:/app/cache` — download cache
+
+Set an admin password, or read the generated one from the log. Set the listen address for bridge networking:
+
+```yaml
+environment:
+ SAVEANY_ADMIN_PASSWORD: change-me
+ SAVEANY_LISTEN: 0.0.0.0:28080
```
### Docker
-```shell
+```bash
docker run -d --name saveany-bot \
- -v /path/to/config.toml:/app/config.toml \
- -v /path/to/downloads:/app/downloads \
+ -p 28080:28080 \
+ -e SAVEANY_LISTEN=0.0.0.0:28080 \
+ -v ./data:/app/data \
+ -v ./downloads:/app/downloads \
+ -v ./cache:/app/cache \
ghcr.io/krau/saveany-bot:latest
```
-{{< hint info >}}
-About Docker image variants
-
-
-Default: Includes all features and dependencies, larger in size. Use this if you don't have special requirements.
-micro: Slimmed-down image with some optional dependencies removed, smaller in size.
-pico: Minimal image containing only core features, smallest in size.
-
-You can pull different variants by specifying tags, for example: ghcr.io/krau/saveany-bot:micro
-
-For more details about the variants, see the Dockerfile in the project root.
-{{< /hint >}}
+Read the tokenized URL from the logs and open it. For local storage in Docker, use `/app/downloads` as the base path.
## Updates
-If you deployed from pre-compiled binaries, use the following CLI command to update:
+### Binary
```bash
./saveany-bot up
```
-(`upgrade` is also available as an alias.)
-
-The command replaces the binary in place; a running bot keeps the old version until it is restarted. You can also send `/update` to the bot: it downloads the new version, replaces its own binary and restarts itself in place (on Windows it exits instead, and the service manager starts the new version).
+You can also send `/update` in Telegram. The updated process restarts itself; on Windows, restart it from your service manager.
-If you deployed with Docker, use the following commands to update:
-
-docker:
+### Docker
```bash
docker pull ghcr.io/krau/saveany-bot:latest
docker restart saveany-bot
```
-docker compose:
+### Docker Compose
```bash
docker compose pull
-docker compose restart
-```
\ No newline at end of file
+docker compose up -d
+```
+
+Back up `data/` before updating and keep the Docker volume.
diff --git a/docs/content/en/deployment/migration.md b/docs/content/en/deployment/migration.md
new file mode 100644
index 00000000..b4d20563
--- /dev/null
+++ b/docs/content/en/deployment/migration.md
@@ -0,0 +1,132 @@
+---
+title: "Migrate from v0"
+weight: 15
+---
+
+# Migrate from v0
+
+v1 stores configuration in a local SQLite database. On first startup, it imports a legacy v0 `config.toml` once, then all settings are managed from the web console.
+
+v1 does not migrate Telegram sessions. The v0 `session.db` and `usersession.db` files were created by gotgproto and are no longer read. Migration imports the legacy configuration; v1 uses a new database. The bot signs in again with the configured token, and an enabled Userbot must sign in again from the web console. Back up all existing files before upgrading.
+
+## 1. Stop and back up
+
+Stop v0 and back up the configuration, data, and downloads:
+
+```bash
+docker compose down
+cp -a config.toml data downloads /path/to/backup/
+```
+
+For a binary deployment, stop the process and back up the same directories.
+
+Do not run v0 and v1 at the same time, and never share one `data/` directory between two instances.
+
+## 2. Keep the data directory
+
+v1 does not reuse the v0 database. Keep the old file as a backup and let v1 create a new `data/saveany.db`:
+
+| v0 file | v1 default | Purpose |
+|---|---|---|
+| `data/saveany.db` | not reused | Keep as a backup; v1 creates a new database |
+
+Do not point v1 at the v0 database with `--db-path` or `SAVEANY_DB_PATH`; the v0 schema is not migrated. Move or rename the old `data/saveany.db` out of the data directory before the first v1 start; v1 must create a new database at the default path. Keep the old file as a backup.
+
+The v0 `data/session.db` and `data/usersession.db` are ignored; delete them after confirming they are backed up. v1 uses new session files:
+
+| File | Purpose |
+|---|---|
+| `data/bot_session.db` | Bot session |
+| `data/userbot_session.db` | Userbot session |
+
+The bot signs in again with the configured token. If the Userbot is enabled, sign in again on the Telegram page later.
+
+## 3. Import the configuration on first startup
+
+v1 must be able to read the legacy `config.toml` during its first startup. In Docker, mount it only for that run:
+
+```yaml
+services:
+ saveany-bot:
+ image: ghcr.io/krau/saveany-bot:latest
+ restart: unless-stopped
+ ports:
+ - "28080:28080"
+ environment:
+ SAVEANY_LISTEN: 0.0.0.0:28080
+ volumes:
+ - ./data:/app/data
+ - ./config.toml:/app/config.toml:ro
+ - ./downloads:/app/downloads
+ - ./cache:/app/cache
+```
+
+```bash
+docker compose up -d
+docker compose logs -f saveany-bot
+```
+
+For a binary deployment:
+
+```bash
+./saveany-bot --config /path/to/config.toml
+```
+
+Without `--config`, v1 checks `./config.toml` and then `/etc/saveany/config.toml`.
+
+After the import log appears, remove the `config.toml` mount and restart. v1 writes an import marker to the database and will not import it again. The legacy `db.session` and `telegram.userbot.session` values are ignored; v1 always uses the new session file paths.
+
+## 4. Sign in and verify
+
+The startup log prints a direct Web UI URL with an access token; open it to sign in. If `SAVEANY_ADMIN_PASSWORD` was not set on the first run, the fallback admin password is printed in the log as well:
+
+```bash
+docker compose logs saveany-bot | grep -E "Web UI available at|Generated web admin password"
+```
+
+Open **Guide** after signing in and verify:
+
+- The bot has reconnected with the current token.
+- If the Userbot is enabled, it has signed in again and shows its account.
+- At least one storage is enabled and has no initialization error.
+- The Users list contains your old accounts. Old users that were not present in the v0 config are disabled; enable them manually if needed.
+- Storages, users, and settings imported from `config.toml` are present.
+- Recreate directories, rules, and watched chats in v1; v0 database rows are not migrated.
+
+Then send a file or Telegram message link to the bot and confirm a task completes.
+
+## 5. After migration
+
+- Later changes to `config.toml` are **ignored**. Use the web console.
+- MinIO storage entries are converted to `s3` on startup; review their endpoint, credentials, and bucket in the web console.
+- Back up the entire `data/` directory before updates and keep the Docker volume.
+- Update Docker with `docker compose pull && docker compose up -d`.
+- If you change the Bot token, delete `data/bot_session.db` before restarting to avoid an old session mismatch.
+
+## Troubleshooting
+
+**Nothing was imported**
+
+Check that the config path is correct and the file is valid TOML. The import marker is written only after a successful import. Existing storage names and settings groups are not overwritten.
+
+**A legacy session was not migrated**
+
+v1 does not read the old gotgproto `session.db` or `usersession.db`. The bot signs in again with the current token; if the Userbot is enabled, sign in again on the Telegram page. Keep the old files in your backup and delete them when they are no longer needed.
+
+**The old database is not migrated**
+
+v1 does not reuse the v0 database or its `[db] path`. Keep the old file as a backup and let v1 create a new database.
+
+**Local storage paths stopped working**
+
+v0 resolved local storage paths relative to the process working directory. In Docker this is usually `/app`; make sure `downloads/` is still mounted at the same path.
+
+**The admin password is unknown**
+
+Find `Generated web admin password` in the first startup log. Only a hash is stored, so the password cannot be recovered from the database. If the log is gone, back up the database, delete the `auth` setting, and restart to generate a new password.
+
+## Related
+
+- [Installation and updates](../installation)
+- [Web console configuration](../configuration/)
+- [Usage](../../usage/)
diff --git a/docs/content/en/usage/_index.md b/docs/content/en/usage/_index.md
index 0a0932e5..62a39f32 100644
--- a/docs/content/en/usage/_index.md
+++ b/docs/content/en/usage/_index.md
@@ -7,10 +7,38 @@ weight: 10
This page introduces some of Save Any Bot's features and basic usage. If you can't find what you need here, please also see the [Configuration Guide](../deployment/configuration) or ask in GitHub [Discussions](https://github.com/krau/SaveAny-Bot/discussions).
+## Bot Commands
+
+| Command | Purpose |
+|---|---|
+| `/start` | Open help and the command list |
+| `/save` | Save a replied media message, or batch-save a message range |
+| `/download` | Download links using Direct, Aria2, or Video |
+| `/transfer` | Transfer files between storages |
+| `/tasks` | Inspect and manage running, queued, and failed tasks |
+| `/config` | User settings: filename and conflict strategy, silent mode, default storage |
+| `/template` | Set the custom filename template |
+| `/dirs` | Manage storage directories |
+| `/rules` | Manage storage rules |
+| `/watch` | Watch chats and manage notifications/peer sync (UserBot) |
+| `/parser` | Manage parser plugins |
+| `/update` | Check for updates |
+
+
## File Transfer
To use the bot's Telegram file saving feature, you need to send or forward the following types of messages to the bot:
1. File or media messages, such as images, videos, documents, etc.
2. Telegram message links, for example: `https://t.me/acherkrau/1097`. **Even if the channel prohibits forwarding and saving, the bot can still download its files.**
-3. Telegra.ph article links. The bot will download all images in the article.
\ No newline at end of file
+3. Telegra.ph article links. The bot will download all images in the article.
+
+## Group Chats
+
+The bot works in basic groups and supergroups. Broadcast channels are not supported.
+
+- In groups the bot only reacts to explicit commands and button callbacks; ordinary media and link messages are not saved automatically.
+- To save a file from the group, reply to it with `/save`, or use `/save@YourBot` in groups. Other commands such as `/download` work as usual.
+- Only whitelisted users can use the bot. Messages from other members are ignored silently, and their button clicks receive a permission alert.
+- Messages sent by anonymous administrators in supergroups have no identifiable user and are ignored.
+- With the default Privacy Mode, the bot only receives commands addressed to it, mentions, and replies to its own messages. To let it receive all group messages, use `/setprivacy` in [@BotFather](https://t.me/BotFather) to disable Privacy Mode and re-add the bot to the group. Even then, the bot only reacts to commands and buttons in groups.
\ No newline at end of file
diff --git a/docs/content/en/usage/api.md b/docs/content/en/usage/api.md
index 740158f3..936f7e13 100644
--- a/docs/content/en/usage/api.md
+++ b/docs/content/en/usage/api.md
@@ -9,41 +9,29 @@ SaveAny-Bot provides an HTTP API that allows you to programmatically create down
## Enabling the API
-Add or modify the following section in `config.toml`:
-
-```toml
-[api]
-enable = true
-host = "0.0.0.0" # Bind address, default 0.0.0.0
-port = 8080 # Listen port, default 8080
-token = "your-token" # Auth token — strongly recommended
-```
-
-You can also override these settings with environment variables (prefix `SAVEANY_`):
+The API shares the web console listener and port; there is nothing separate to enable. The web console listens on `127.0.0.1:28080` by default. Set an access token under **Settings → Web**:
-| Environment Variable | Config Key |
-|---|---|
-| `SAVEANY_API_ENABLE` | `api.enable` |
-| `SAVEANY_API_HOST` | `api.host` |
-| `SAVEANY_API_PORT` | `api.port` |
-| `SAVEANY_API_TOKEN` | `api.token` |
+- `Access token`: used for direct Web UI access through `/?token=...` and with `Authorization: Bearer ` for the API.
+- `Web listen address`: shared by the console and API; use `0.0.0.0:28080` when it must be reachable from other hosts.
-{{< hint warning >}}
-If `token` is empty, the API server will be accessible **without any authentication**, which is a security risk.
-{{< /hint >}}
+All endpoints live under `/api/v1`.
## Authentication
-When `token` is configured, all API requests must include a Bearer token in the HTTP header:
+Set an access token under **Settings → Web**, then send it in the request header:
```
Authorization: Bearer
```
-On authentication failure, the server returns `401`:
+If no access token is configured, a random token is generated on every start and printed in the log as part of the direct Web UI URL. That value also works as a Bearer token for the current process.
+
+You can also use the web console login cookie. Non-GET requests made with a cookie also require the `X-CSRF-Token` header.
+
+Authentication failures return `401`:
```json
-{ "error": "unauthorized", "message": "invalid token" }
+{ "error": "unauthorized", "message": "authentication required" }
```
## Error Response Format
@@ -62,7 +50,6 @@ Common error codes:
| Error Code | HTTP Status | Meaning |
|---|---|---|
| `unauthorized` | 401 | Authentication failed |
-| `method_not_allowed` | 405 | Wrong HTTP method |
| `invalid_request` | 400 | Malformed request body or parameters |
| `task_creation_failed` | 400 | Failed to create task |
| `task_not_found` | 404 | Task ID does not exist |
@@ -80,7 +67,7 @@ No authentication required.
**Response `200 OK`:**
```json
-{ "status": "ok" }
+{ "status": "ok", "version": "1.0.0" }
```
---
@@ -94,14 +81,44 @@ Returns all currently loaded storage backends.
```json
{
"storages": [
- { "name": "local", "type": "local" },
- { "name": "MyMinio", "type": "s3" }
+ {
+ "storage_id": 1,
+ "name": "local",
+ "type": "local",
+ "enable": true,
+ "config": { "base_path": "/app/downloads" },
+ "config_version": 1
+ }
]
}
```
---
+### GET /api/v1/meta — API and Capability Metadata
+
+Returns the frozen v1 capability vocabulary and the database schema version.
+
+```json
+{
+ "api_version": "v1",
+ "version": "1.0.0",
+ "schema_version": 1,
+ "chat_id_format": "bot_api",
+ "task_types": ["tgfiles", "tphpics", "parseditem", "directlinks", "aria2", "ytdlp", "transfer"],
+ "task_statuses": ["queued", "running", "completed", "partial", "failed", "cancelled"],
+ "task_item_statuses": ["completed", "failed", "skipped", "cancelled"],
+ "storage_types": ["local", "webdav", "alist", "telegram", "s3", "rclone"],
+ "rule_types": ["FILENAME-REGEX", "MESSAGE-REGEX", "IS-ALBUM"],
+ "filename_strategies": ["default", "message", "template"],
+ "conflict_strategies": ["rename", "ask", "overwrite", "skip"]
+}
+```
+
+Chat IDs use the Bot API form: positive IDs for users, negative IDs for basic groups, and `-100...` IDs for supergroups and channels.
+
+---
+
### GET /api/v1/task-types — List Supported Task Types
**Response `200 OK`:**
@@ -109,13 +126,8 @@ Returns all currently loaded storage backends.
```json
{
"types": [
- "directlinks",
- "ytdlp",
- "aria2",
- "parseditem",
- "tgfiles",
- "tphpics",
- "transfer"
+ { "type": "directlinks", "label": "directlinks" },
+ { "type": "tgfiles", "label": "tgfiles" }
]
}
```
@@ -129,7 +141,7 @@ Inspect a Telegram message link, direct media URL, or other yt-dlp-supported med
Request:
```bash
-curl -X GET "http://localhost:8080/api/v1/media-metadata?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxxx" \
+curl -X GET "http://127.0.0.1:28080/api/v1/media-metadata?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxxx" \
-H "Authorization: Bearer YOUR_TOKEN"
```
@@ -163,7 +175,7 @@ Authorization: Bearer
```json
{
"type": "",
- "storage": "",
+ "storage_id": 1,
"path": "",
"webhook": "",
"params": { }
@@ -173,7 +185,7 @@ Authorization: Bearer
| Field | Type | Required | Description |
|---|---|---|---|
| `type` | string | Yes | Task type — see below |
-| `storage` | string | Yes | Target storage name, must match a name in your config |
+| `storage_id` | uint | Yes | Target storage ID |
| `path` | string | No | Subdirectory path within the storage |
| `webhook` | string | No | Callback URL invoked when the task reaches a terminal state |
| `params` | object | Yes | Type-specific parameters — see below |
@@ -182,9 +194,11 @@ Authorization: Bearer
```json
{
- "task_id": "abc123xyz",
- "type": "directlinks",
- "status": "queued",
+ "task_id": "abc123xyz",
+ "type": "directlinks",
+ "status": "queued",
+ "storage_id": 1,
+ "storage_name": "local",
"created_at": "2026-03-11T10:00:00Z"
}
```
@@ -198,7 +212,7 @@ Download one or more files from direct HTTP/HTTPS URLs.
```json
{
"type": "directlinks",
- "storage": "local",
+ "storage_id": 1,
"path": "downloads",
"params": {
"urls": [
@@ -224,7 +238,7 @@ Download videos or audio via yt-dlp, supporting YouTube, Bilibili, and 1000+ oth
```json
{
"type": "ytdlp",
- "storage": "local",
+ "storage_id": 1,
"path": "videos",
"params": {
"urls": ["https://www.youtube.com/watch?v=xxx"],
@@ -241,7 +255,7 @@ Download videos or audio via yt-dlp, supporting YouTube, Bilibili, and 1000+ oth
##### aria2 — Aria2 Download
{{< hint warning >}}
-Requires Aria2 to be enabled and configured (RPC) in the config file.
+Requires Aria2 to be enabled and configured (RPC) under **Settings → Aria2**.
{{< /hint >}}
Download files via the Aria2 download manager, supporting HTTP/HTTPS, FTP, BitTorrent (magnet links, torrent files), and more.
@@ -249,7 +263,7 @@ Download files via the Aria2 download manager, supporting HTTP/HTTPS, FTP, BitTo
```json
{
"type": "aria2",
- "storage": "local",
+ "storage_id": 1,
"path": "downloads",
"params": {
"urls": ["magnet:?xt=urn:btih:..."],
@@ -270,7 +284,7 @@ Hand a URL off to a registered JS plugin or built-in parser for processing and d
```json
{
"type": "parseditem",
- "storage": "local",
+ "storage_id": 1,
"path": "parsed",
"params": {
"url": "https://some-site.com/page"
@@ -298,7 +312,7 @@ If the message is part of a media group (album), all files in the group are down
```json
{
"type": "tgfiles",
- "storage": "local",
+ "storage_id": 1,
"path": "telegram",
"params": {
"message_links": [
@@ -311,7 +325,10 @@ If the message is part of a media group (album), all files in the group are down
| params field | Type | Required | Description |
|---|---|---|---|
-| `message_links` | []string | Yes | List of Telegram message links, at least 1 |
+| `message_links` | []string | No | List of Telegram message links; required unless `chat_id` and `message_range` are used |
+| `chat_id` | string | No | Bot API chat ID or username used with `message_range` |
+| `message_range` | string | No | Inclusive message range, for example `100-120` |
+| `filter` | string | No | Optional regular expression matching message text or file names |
##### tphpics — Telegraph Article Images
@@ -322,7 +339,7 @@ Supported URL prefixes: `https://telegra.ph/`, `http://telegra.ph/`, `https://te
```json
{
"type": "tphpics",
- "storage": "local",
+ "storage_id": 1,
"path": "telegraph",
"params": {
"telegraph_url": "https://telegra.ph/Some-Article-01-01"
@@ -339,34 +356,34 @@ Supported URL prefixes: `https://telegra.ph/`, `http://telegra.ph/`, `https://te
Transfer files directly between two storage backends without going through Telegram. The source storage must support both listing and reading.
{{< hint info >}}
-For `transfer` tasks, the top-level `storage` field is still required for validation, but the actual storages used are determined by `source_storage` and `target_storage` inside `params`.
+For `transfer` tasks, the top-level `storage_id` field is still required for validation, but the actual storages used are determined by `source_storage_id` and `target_storage_id` inside `params`.
{{< /hint >}}
```json
{
"type": "transfer",
- "storage": "local",
+ "storage_id": 1,
"params": {
- "source_storage": "MyS3",
- "source_path": "backups/",
- "target_storage": "LocalDisk",
- "target_path": "restored/"
+ "source_storage_id": 2,
+ "source_path": "backups/",
+ "target_storage_id": 1,
+ "target_path": "restored/"
}
}
```
| params field | Type | Required | Description |
|---|---|---|---|
-| `source_storage` | string | Yes | Source storage name |
+| `source_storage_id` | uint | Yes | Source storage ID |
| `source_path` | string | Yes | Path within the source storage; must contain at least one file |
-| `target_storage` | string | Yes | Target storage name |
+| `target_storage_id` | uint | Yes | Target storage ID |
| `target_path` | string | Yes | Destination path within the target storage |
---
### GET /api/v1/tasks — List All Tasks
-Returns all tasks created via the API, newest first (by creation time). Task records are stored in memory only and are cleared on restart. Tasks that have reached a terminal state are also evicted 24 hours after their last update; the sweep runs every 10 minutes.
+Returns tasks newest first (by creation time). Task records and their creation parameters are persisted in SQLite; queued and interrupted tasks are resumed after a restart. Terminal records remain until deleted through the API.
**Response `200 OK`:**
@@ -378,7 +395,8 @@ Returns all tasks created via the API, newest first (by creation time). Task rec
"type": "directlinks",
"status": "running",
"title": "file.zip",
- "storage": "local",
+ "storage_id": 1,
+ "storage_name": "local",
"path": "downloads",
"error": "",
"created_at": "2026-03-11T10:00:00Z",
@@ -411,25 +429,113 @@ The `progress` field is included when either `total_bytes` or `total_files` is g
- `400 invalid_request` — no task ID in path
- `404 task_not_found` — task does not exist
+Task objects include `metadata_available` when at least one item has stored message metadata.
+
---
-### DELETE /api/v1/tasks/{task_id} — Cancel Task
+### GET /api/v1/tasks/{task_id}/metadata — Get Message Metadata
-**Path parameter:** `task_id`
+Returns the normalized Telegram message metadata recorded for every saved file in a Telegram file task. Enable **Settings → Message metadata** before creating the task. Metadata is always stored in the local database. The optional **Write sidecar JSON** setting also writes `.meta.json` beside the saved file; Telegram storage skips sidecars.
+
+**Path parameter:** `task_id` — the task whose metadata should be returned.
**Response `200 OK`:**
```json
-{ "message": "task cancelled successfully" }
+{
+ "task_id": "abc123xyz",
+ "items": [
+ {
+ "index": 1,
+ "name": "saved-video.mp4",
+ "path": "downloads/saved-video.mp4",
+ "metadata": {
+ "version": 1,
+ "message": {
+ "id": 123,
+ "chat": { "id": -1001234567890, "title": "Channel" },
+ "sender": { "id": 123456789, "name": "Alice" },
+ "text": "caption",
+ "grouped_id": 999
+ },
+ "file": {
+ "name": "saved-video.mp4",
+ "media_type": "document",
+ "mime_type": "video/mp4",
+ "size": 12345678,
+ "width": 1920,
+ "height": 1080,
+ "duration": 123.4
+ }
+ }
+ }
+ ]
+}
```
+Message text is omitted when **Store message text** is disabled. Access hashes, tokens, and raw Telegram objects are never stored.
+
**Error responses:**
- `400 invalid_request` — no task ID in path
- `404 task_not_found` — task does not exist
+- `404 metadata_not_found` — the task has no recorded metadata
+
+---
+
+### POST /api/v1/tasks/{task_id}/retry — Retry Failed Files
+
+Creates a new task from the failed files of a terminal task. The bot fetches the original messages again, which also refreshes their file references. Supported for Telegram file tasks (`tgfiles`).
+
+**Path parameter:** `task_id` — the task to retry.
+
+**Response `202 Accepted`:**
+
+```json
+{ "task_id": "d4f7u1crvimcs4n4j8e1", "status": "queued", "file_count": 2 }
+```
+
+**Error responses:**
+- `409 task_active` — the task is still queued or running
+- `409 nothing_to_retry` — the task has no failed or unfinished files
+- `409 storage_missing` / `409 source_unavailable`
+- `400 retry_unsupported` — the task type cannot be retried
+- `503 client_unavailable` — no Telegram client is running
+
+Task responses include per-file outcomes: list responses carry the total in `failed_count` plus up to five failed items per task in `failed_items`, and the single-task response carries every recorded file in `items`.
+
+---
+
+### POST /api/v1/tasks/{task_id}/cancel — Cancel Task
+
+Cancels a queued or running task.
+
+**Path parameter:** `task_id` — the task to cancel.
+
+**Response `202 Accepted`:**
+
+```json
+{ "task_id": "abc123xyz", "status": "cancelled" }
+```
+
+**Error responses:**
+- `409 task_not_active` — the task is already terminal
+- `404 task_not_found` — task does not exist
- `500 cancel_failed` — cancellation failed
---
+### DELETE /api/v1/tasks/{task_id} — Delete Task
+
+Deletes a terminal task and its recorded items. Use `POST /api/v1/tasks/{task_id}/cancel` to cancel a queued or running task.
+
+**Path parameter:** `task_id`
+
+**Response `204 No Content`:**
+- `409 task_active` — cancel the task before deleting it
+- `404 task_not_found` — task does not exist
+
+---
+
## Task Statuses
| Status | Meaning |
@@ -437,17 +543,20 @@ The `progress` field is included when either `total_bytes` or `total_files` is g
| `queued` | Task is queued and waiting to run |
| `running` | Task is currently executing |
| `completed` | Task finished successfully |
+| `partial` | Task finished, but one or more files failed |
| `failed` | Task encountered an error |
-| `cancelled` | Task was cancelled via the DELETE endpoint |
+| `cancelled` | Task was cancelled through the cancel endpoint |
+
+Queued tasks and tasks interrupted while running keep their creation parameters and are re-enqueued after a restart. Telegram downloads resume from the partial cache; files whose upload already completed are skipped.
---
## Webhook Callbacks
-When a `webhook` URL is provided in the create request, SaveAny-Bot sends a `POST` request to that URL once the task reaches `completed` or `failed`.
+When a `webhook` URL is provided in the create request, SaveAny-Bot sends a `POST` request to that URL once the task reaches `completed`, `partial`, or `failed`.
{{< hint info >}}
-Cancelled tasks do **not** trigger a webhook — cancellation is already known to whoever issued the DELETE. Each task fires at most one callback.
+Cancelled tasks do **not** trigger a webhook — cancellation is already known to whoever requested it. Each task fires at most one callback.
{{< /hint >}}
The callback is sent asynchronously, so the task is not delayed by a slow or unreachable endpoint.
@@ -466,13 +575,14 @@ User-Agent: SaveAny-Bot/1.0
"task_id": "abc123xyz",
"type": "directlinks",
"status": "completed",
- "storage": "local",
+ "storage_id": 1,
+ "storage_name": "local",
"path": "downloads",
"completed_at": "2026-03-11T10:01:00Z",
"error": ""
}
```
-`completed_at` is only present when status is `completed` or `failed`. `error` is only present when non-empty.
+`completed_at` is only present when status is `completed`, `partial`, or `failed`. `error` is only present when non-empty.
**Retry policy:** Up to 3 attempts. A non-2xx response or a transport error triggers a retry after 100ms, then 400ms. Each request has a 30-second timeout.
diff --git a/docs/content/en/usage/aria2.md b/docs/content/en/usage/aria2.md
index eead799b..21e8f90b 100644
--- a/docs/content/en/usage/aria2.md
+++ b/docs/content/en/usage/aria2.md
@@ -1,41 +1,37 @@
---
-title: "Aria2 Download"
+title: "Aria2 Downloads"
weight: 6
---
-# Aria2 Download
+# Aria2 Downloads
{{< hint warning >}}
-This feature requires enabling Aria2 in the configuration file and configuring the RPC connection.
+This feature requires enabling Aria2 under **Settings → Aria2** and configuring the RPC connection.
{{< /hint >}}
-Use the `/aria2dl` command to download files via the Aria2 download manager, supporting HTTP/HTTPS, FTP, BitTorrent, and other protocols.
+Aria2 downloads are available through `/download`. Send links, then choose **Aria2**. It supports HTTP/HTTPS, FTP, BitTorrent, and magnet links.
```bash
-/aria2dl [uri2] [uri3] ...
+/download [uri2] [uri3] ...
```
Examples:
```bash
-# Download HTTP link
-/aria2dl https://example.com/file.zip
+# Download an HTTP link
+/download https://example.com/file.zip
-# Download magnet link
-/aria2dl magnet:?xt=urn:btih:...
+# Download a magnet link
+/download magnet:?xt=urn:btih:...
-# Download torrent file (need to upload .torrent file first)
-/aria2dl https://example.com/file.torrent
+# Download a torrent file
+/download https://example.com/file.torrent
```
-Configure Aria2:
+Choose **Aria2**, then select the target storage. When only Aria2 supports the supplied links, it is the only engine available.
-Add to `config.toml`:
+Configure Aria2 under **Settings → Aria2** in the web console:
-```toml
-[aria2]
-enable = true
-url = "http://localhost:6800/jsonrpc"
-secret = "your-rpc-secret" # If rpc-secret is configured
-remove_after_transfer = true # Remove local files after transfer
-```
+- `URL`: Aria2 RPC address, for example `http://localhost:6800/jsonrpc`
+- `Secret`: required when `rpc-secret` is configured
+- `Keep file`: keep the local file downloaded by Aria2 after the transfer
diff --git a/docs/content/en/usage/cli.md b/docs/content/en/usage/cli.md
index 216096e9..b5c14621 100644
--- a/docs/content/en/usage/cli.md
+++ b/docs/content/en/usage/cli.md
@@ -7,7 +7,7 @@ weight: 21
Besides running the Telegram bot with `./saveany-bot` (no subcommand), the binary exposes two helper subcommands for moving local files into a storage backend: `upload` (one-shot) and `watch` (continuous).
-These subcommands load the same `config.toml` as the bot, initialize the database and caches, then perform their task. They do **not** start the Telegram bot itself, although storages of type `telegram` will spin up the bot client just for the upload.
+These subcommands load the same database settings as the bot, initialize the database and caches, then perform their task. They do **not** start the Telegram bot itself, although storages of type `telegram` will spin up the bot client just for the upload.
## `upload` — Upload a Single File
@@ -20,7 +20,7 @@ Flags:
| Flag | Required | Description |
|---|---|---|
| `-f, --file` | Yes | Path to the local file to upload |
-| `-s, --storage` | Yes | Target storage name (must exist in `config.toml`) |
+| `-s, --storage` | Yes | Target storage name (must exist in the web console storage list) |
| `-d, --dir` | No | Destination directory within the storage. Defaults to the storage's `base_path` |
| `--no-progress` | No | Disable the terminal progress bar |
@@ -82,7 +82,7 @@ Examples:
- Relative directory structure is preserved under the destination directory. A file written to `./inbox/sub/file.txt` with `--path ./inbox` is uploaded to `/sub/file.txt`.
- `watch` runs until interrupted (e.g. `Ctrl-C` / `SIGINT`); in-flight uploads are drained before exit.
-- Retries follow the global `retry` value from `config.toml`, with `--retry-delay` between attempts.
+- Retries follow the global `retry` value from Settings, with `--retry-delay` between attempts.
- Telegram-type storages will start the bot client automatically to perform uploads.
{{< hint warning >}}
diff --git a/docs/content/en/usage/config.md b/docs/content/en/usage/config.md
index db95eaf9..4c720ff1 100644
--- a/docs/content/en/usage/config.md
+++ b/docs/content/en/usage/config.md
@@ -5,14 +5,17 @@ weight: 11
# File Naming & Conflict Strategies
-SaveAny-Bot lets you customize how saved files are named and how collisions with existing files are resolved, directly in Telegram via the `/config` and `/fnametmpl` commands.
+SaveAny-Bot lets you customize how saved files are named and how collisions with existing files are resolved, directly in Telegram with `/config` and `/template`.
-## `/config` — User Configuration
+## `/config` — User Settings
-The `/config` command opens an inline menu where you can change two per-user settings:
+The `/config` command contains the following per-user settings:
- **Filename strategy** — how the saved file is named
- **Duplicate file strategy** — what happens when a file with the same name already exists in the target storage
+- **Silent mode** — save incoming files directly to the default location
+- **Default storage** — choose the storage (and directory) used by silent mode and unmatched rules
+- **Filename template** — view the current template and the `/template` usage
Settings are stored per user and apply to all of that user's subsequent save/transfer tasks.
@@ -22,7 +25,7 @@ Settings are stored per user and apply to all of that user's subsequent save/tra
|---|---|
| `Default` | Use the original media filename, or a generated name when no original filename is available |
| `Gen From Msg First` | Generate the filename from the message content (e.g. caption, text) and prefer that over the original filename |
-| `Template` | Render the filename from a custom template you define with `/fnametmpl` |
+| `Template` | Render the filename from a custom template you define with `/template` |
### Duplicate file strategy
@@ -37,15 +40,15 @@ Settings are stored per user and apply to all of that user's subsequent save/tra
The conflict strategy only kicks in for storage backends that can detect the existence of a file. Backends that do not support existence checks will fall back to overwriting.
{{< /hint >}}
-## `/fnametmpl` — Custom Filename Template
+## `/template` — Custom Filename Template
-When the filename strategy is set to `Template`, SaveAny-Bot renders each saved file's name using the template configured via `/fnametmpl`.
+When the filename strategy is set to `Template`, SaveAny-Bot renders each saved file's name using the template configured via `/template`.
```
-/fnametmpl [template]
+/template [template]
```
-- Running `/fnametmpl` without arguments shows your current template and the help text.
+- Running `/template` without arguments shows your current template and the help text.
- Running it with a template string sets that template as your filename template.
The template uses Go [`text/template`](https://pkg.go.dev/text/template) syntax. The available variables are:
@@ -64,12 +67,16 @@ Examples:
```
# Fixed prefix + message id + date
-/fnametmpl Image_{{.msgid}}_{{.msgdate}}.jpg
+/template Image_{{.msgid}}_{{.msgdate}}.jpg
# Use original name if available, otherwise a generated name
-/fnametmpl {{.origname}}
+/template {{.origname}}
```
+{{< hint info >}}
+When an album is saved as a batch (media groups sent to the bot, and watched albums matched by a `NEW-FOR-ALBUM` rule), files without their own caption use the album's caption for template variables and message rules, so `{{.msgtags}}` and `{{.msgraw}}` are populated for every item. If the template produces the same name for several items, later items keep their generated name.
+{{< /hint >}}
+
{{< hint warning >}}
The template only takes effect when the filename strategy is set to `Template`. If template parsing fails, SaveAny-Bot falls back to the default filename naming logic.
{{< /hint >}}
\ No newline at end of file
diff --git a/docs/content/en/usage/directlinks.md b/docs/content/en/usage/directlinks.md
index 56f04ba5..5f610f8e 100644
--- a/docs/content/en/usage/directlinks.md
+++ b/docs/content/en/usage/directlinks.md
@@ -1,21 +1,25 @@
---
-title: "Direct Download Links"
+title: "URL Downloads"
weight: 5
---
-# Direct Download Links
+# URL Downloads
-Use the `/dl` command to directly download one or more HTTP/HTTPS files to storage.
+Use the `/download` command with one or more links and select an engine:
+
+- **Direct** — plain HTTP/HTTPS download, best for ordinary file links
+- **Aria2** — when Aria2 is enabled, including magnet and torrent links
+- **Video** — when yt-dlp is available, for supported video sites
```bash
-/dl [url2] [url3] ...
+/download [url2] [url3] ...
```
Examples:
```bash
-/dl https://example.com/file.zip
-/dl https://example.com/file1.zip https://example.com/file2.zip
+/download https://example.com/file.zip
+/download https://example.com/file1.zip https://example.com/file2.zip
```
-The bot will validate the link format and then ask you to select the target storage location.
+Choose the engine, then select the target storage. For video sites, yt-dlp options such as `-f best` or `--extract-audio` can be appended to the same command.
diff --git a/docs/content/en/usage/parsers.md b/docs/content/en/usage/parsers.md
index e256ad02..0196e8e1 100644
--- a/docs/content/en/usage/parsers.md
+++ b/docs/content/en/usage/parsers.md
@@ -9,7 +9,34 @@ Besides files on Telegram, the bot can also save files from other websites via J
> See the [Contributing Parsers](../contribute) document for details.
-Just send links that match the requirements of a parser to the bot. Currently built-in parsers include:
+Send a link that matches a parser. Built-in parsers:
- Twitter
- Kemono
+
+## JavaScript plugin API
+
+JavaScript plugins register one parser with:
+
+```js
+registerParser({
+ metadata: {
+ name: 'example',
+ version: '1.0.0',
+ apiVersion: '1',
+ description: 'Example plugin',
+ author: 'you',
+ },
+ canHandle(url) { return url.includes('example.com') },
+ parse(url) {
+ return {
+ site: 'example',
+ url,
+ title: 'Example',
+ resources: [{ url: 'https://cdn.example.com/file.zip', filename: 'file.zip' }],
+ }
+ },
+})
+```
+
+`apiVersion` is the plugin ABI version and is independent of the plugin's own `version`. The current ABI is `"1"`; plugins with another value are rejected. The available globals are `console`, `ghttp` (`get`, `getJSON`, `head`), and `playwright`. `parse` returns an `Item` with `resources` containing `url`, `filename`, optional `mime_type`, `extension`, `size`, `hash`, `headers`, and `extra`.
diff --git a/docs/content/en/usage/rules.md b/docs/content/en/usage/rules.md
index 442b8230..9533868e 100644
--- a/docs/content/en/usage/rules.md
+++ b/docs/content/en/usage/rules.md
@@ -9,6 +9,8 @@ Storage rules allow you to define redirection rules when the bot uploads files t
See: #28
+Send `/rules` to view the current mode and rules, enable or disable rule matching, and delete rules. Add rules and import presets with the commands below.
+
Currently supported rule types:
1. FILENAME-REGEX
@@ -17,39 +19,40 @@ Currently supported rule types:
Basic syntax for adding rules:
-"RuleType RuleContent StorageName Path"
+"RuleType RuleContent StorageID|StorageName Path"
-Pay attention to spaces; the bot can only parse correctly formatted syntax. Below is an example of a valid rule command:
+A numeric storage argument is treated as a storage ID (shown on the Storages page of the web console); anything else is treated as a storage name. Use an ID when the name contains spaces, or quote the name. Below is an example of a valid rule command:
```
-/rule add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /videos
+/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ 3 /videos
+/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ "My Alist" /videos
```
-In addition, if `CHOSEN` is used as the storage name in the rule, it means files will be stored under the path of the storage you selected by clicking the inline button.
+In addition, if `CHOSEN` is used as the storage in the rule, it means files will be stored under the path of the storage you selected by clicking the inline button.
-You can also toggle whether rules are applied with `/rule switch`. When rule mode is off, all files go to the default storage.
+You can also toggle whether rules are applied with `/rules switch`. When rule mode is off, all files go to the default storage.
## Preset Rules
Manually writing regex rules for common file types is tedious, so the bot ships a built-in set of preset categories (video, image, audio, document, archive) that you can import in one command:
```
-/rule preset [base_path]
+/rules preset [base_path]
```
Parameters:
-- `storage`: Target storage name (must exist and be accessible to you)
+- `storage`: Target storage ID or name (must exist and be accessible to you)
- `base_path`: Optional. Each preset category's subdirectory is created under this path. If omitted, the default category directory names are used directly.
Examples:
```
# Import preset rules into "MyAlist" with the default directory layout
-/rule preset MyAlist
+/rules preset MyAlist
# Import preset rules with a custom base path "downloads/sorted"
-/rule preset MyAlist downloads/sorted
+/rules preset MyAlist downloads/sorted
```
This will create `FILENAME-REGEX` rules for each category, routing matched files to the corresponding subdirectory under `base_path`:
@@ -63,7 +66,7 @@ This will create `FILENAME-REGEX` rules for each category, routing matched files
| archive | zip, rar, 7z, tar, gz, bz2, xz, ... | `压缩包` |
{{< hint info >}}
-Preset rules are regular `FILENAME-REGEX` rules once imported. You can view, edit, or delete them individually with `/rule` and `/rule del ` like any other rule.
+Preset rules are regular `FILENAME-REGEX` rules once imported. You can view or delete them individually from `/rules` like any other rule.
{{< /hint >}}
Rule types:
@@ -76,7 +79,7 @@ Matches based on filename regex. The rule content must be a valid regular expres
FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /videos
```
-This means files with extensions mp4, mkv, ts, avi, flv will be saved to the `/videos` directory in the storage named `MyAlist` (also affected by the `base_path` in the configuration file).
+This means files with extensions mp4, mkv, ts, avi, flv will be saved to the `/videos` directory in the storage named `MyAlist` (also affected by the `base_path` in storage settings).
## MESSAGE-REGEX
diff --git a/docs/content/en/usage/silent.md b/docs/content/en/usage/silent.md
index fcd84c9d..53ccf34e 100644
--- a/docs/content/en/usage/silent.md
+++ b/docs/content/en/usage/silent.md
@@ -3,12 +3,10 @@ title: "Silent Mode"
weight: 2
---
-# Silent Mode (silent)
+# Silent Mode
-Use the `/silent` command to toggle silent mode.
+Silent mode is configured in `/config`. Toggle **Silent mode** there.
-By default, silent mode is off, and the bot will ask you for the save location of each file.
+By default, silent mode is off, and the bot asks for the save location of each file.
-When silent mode is enabled, the bot will save files directly to the default location without confirmation.
-
-Before enabling silent mode, you need to set the default save location using the `/storage` command.
+When silent mode is enabled, the bot saves files directly to the default location without confirmation. Set the default storage and directory in `/config` before enabling it.
diff --git a/docs/content/en/usage/watch.md b/docs/content/en/usage/watch.md
index 2af3d0fc..bc7b2bc3 100644
--- a/docs/content/en/usage/watch.md
+++ b/docs/content/en/usage/watch.md
@@ -11,18 +11,21 @@ This feature requires enabling UserBot integration.
You can watch messages in a specific chat and automatically save them to the default storage, following storage rules. You can also add filters so that only matching messages are saved.
-Watch a chat:
+Send `/watch` to manage watched chats:
-```
-/watch [filter]
-```
+- **Add watch** — shows the syntax for adding a chat
+- **Watched chats** — lists watched chats and lets you stop each one
+- **Notifications** — toggles task notifications for all watched chats
+- **Sync peers** — imports the Userbot dialog list into the peer cache
-Stop watching:
+To start watching a chat, send:
```
-/unwatch
+/watch [filter]
```
+To stop watching, open **Watched chats** from `/watch` and select the chat.
+
Filter types:
## msgre
@@ -30,19 +33,13 @@ Filter types:
Regex-match the message text. For example:
```
-/watch 12345678 msgre:.*hello.*
+/watch -1002229835658 msgre:.*hello.*
```
-This will watch the chat with ID `12345678`, and only save messages whose text contains `hello`.
+This will watch the channel with ID `-1002229835658`, and only save messages whose text contains `hello`. Use the Bot API form: positive IDs for users, negative IDs for basic groups, and `-100...` for supergroups and channels.
## Task notifications
-Task notifications are disabled by default for every user. Enable them to get a message for every message the bot picks up from a watched chat, updated with the download progress and the final result:
-
-```
-/watch notify on
-```
-
-Turn them off again with `/watch notify off`, and query the current state with `/watch notify`.
+Task notifications are disabled by default for every user. Enable them with `/watch` → **Notifications** to get a message for every message the bot picks up from a watched chat, updated with the download progress and the final result. Disable them the same way.
-Notifications are sent by the bot to your private chat with it. The toggle is per user, not per watched chat: `/watch notify off` stops notifications for every chat that user watches.
+Notifications are sent by the bot to your private chat with it. The toggle is per user, not per watched chat.
diff --git a/docs/content/en/usage/ytdlp.md b/docs/content/en/usage/ytdlp.md
index c1504ed1..faee2a08 100644
--- a/docs/content/en/usage/ytdlp.md
+++ b/docs/content/en/usage/ytdlp.md
@@ -1,32 +1,32 @@
---
-title: "yt-dlp Video Download"
+title: "Video Downloads (yt-dlp)"
weight: 7
---
-# yt-dlp Video Download
+# Video Downloads (yt-dlp)
{{< hint warning >}}
This feature requires the yt-dlp command-line tool installed on your system.
{{< /hint >}}
-Use the `/ytdlp` command to download videos and audio from supported video websites, including YouTube, Bilibili, Twitter, and 1000+ other sites.
+Video extraction is available through `/download`. Send a supported video URL, then choose **Video (yt-dlp)**. It supports YouTube, Bilibili, Twitter, and 1000+ other sites.
```bash
-/ytdlp [url2] [flags...]
+/download [url2] [flags...]
```
Examples:
```bash
# Basic download
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ
# Download multiple videos
-/ytdlp https://www.youtube.com/watch?v=video1 https://www.youtube.com/watch?v=video2
+/download https://www.youtube.com/watch?v=video1 https://www.youtube.com/watch?v=video2
# Use custom parameters
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ -f best
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ --extract-audio --audio-format mp3
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -f best
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ --extract-audio --audio-format mp3
```
Common parameters:
@@ -38,12 +38,14 @@ Common parameters:
- `--write-sub`: Download subtitles
- `--write-thumbnail`: Download thumbnail
+For safety, flags that execute programs, load external configuration, or read and write arbitrary files are rejected: `--exec`, `--exec-before-download`, `--config-location`, `--load-info-json`, `--batch-file`, `--downloader`, `--external-downloader`, `--paths`, `--cache-dir`, `--postprocessor-args`, `--netrc-cmd`, `--plugin-dirs`, `--update` and similar.
+
## File names
Downloaded files are named after the video title (`%(title)s.%(ext)s`) and saved into the storage directory you selected. Pass a yt-dlp output template to customize the name:
```bash
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
```
Only the template part of `-o/--output` is used: the download directory is always managed by the bot, and any relative path in the template is created below it and kept in the storage path. To change the default for every download, set `filename_template` in the [`[ytdlp]` config](../deployment/configuration); `restrict_filenames` there keeps file names ASCII-only, which drops non-latin characters, spaces and `&`.
diff --git a/docs/content/zh/_index.md b/docs/content/zh/_index.md
index a8df9052..41e2c3eb 100644
--- a/docs/content/zh/_index.md
+++ b/docs/content/zh/_index.md
@@ -9,21 +9,21 @@ title: 介绍


-把 Telegram 上的文件转存到多种存储端.
+把 Telegram 上的文件转存到多种存储端。
-## 🎯 特性
+## 特性
-- 支持文档/视频/图片/贴纸…甚至还有 [Telegraph](https://telegra.ph/)
-- 破解禁止保存的文件
+- 支持文档、视频、图片、贴纸及 [Telegraph](https://telegra.ph/) 图集
+- 支持转存受限频道中禁止转发与保存的内容
- 批量下载
- 流式传输
-- 多用户使用
-- 基于存储规则的自动整理
-- 监听并自动转存指定聊天的消息, 支持过滤
-- 在不同存储端之间转存文件
-- 集成 yt-dlp, 从所支持的网站下载并转存媒体文件
-- 集成 Aria2, 支持直链/磁力下载和转存
-- 使用 js 编写解析器插件以转存任意网站的文件
+- 多用户支持
+- 基于规则自动分类整理
+- 监听指定聊天并按过滤器自动转存
+- 存储端之间互相传输文件
+- 集成 yt-dlp,下载视频并转存
+- 集成 Aria2,支持直链与磁力链接转存
+- 支持编写 JavaScript 解析器插件扩展站点
- 存储端支持:
- Alist
- S3
diff --git a/docs/content/zh/contribute/_index.md b/docs/content/zh/contribute/_index.md
index 95970a3e..82bd5dbc 100644
--- a/docs/content/zh/contribute/_index.md
+++ b/docs/content/zh/contribute/_index.md
@@ -5,30 +5,31 @@ weight: 20
# 参与开发
-在开始之前, 请 Fork 本项目, 并克隆到本地, 安装好 Go 开发环境.
+在开始之前,请 Fork 本项目并克隆到本地,准备好 Go 开发环境。
-以下是一些贡献代码的指南或建议, 你不必完全遵守, 但将有助于快速 review 并合并你的提交:
+贡献代码建议遵循以下约定,以便快速合并:
-- **新功能请先提交 Issue**, 以便讨论设计和实现细节, 并避免因与项目设计不符而被拒绝.
-- **使用现代开发工具**, 确保提交前格式化代码, 并保持风格一致.
-- **使用[语义化提交](https://www.conventionalcommits.org/zh-hans/v1.0.0/)**, 避免提交消息模糊或过于简单.
+- **新功能请先提交 Issue**:便于提前讨论设计细节,避免因与整体规划不符而被拒绝。
+- **代码风格**:提交前请格式化代码并保持项目整体风格一致。
+- **提交规范**:遵循[语义化提交](https://www.conventionalcommits.org/zh-hans/v1.0.0/)规范。
+- **测试验证**:参阅[测试文档](testing),运行测试验证改动。
## 贡献新存储端
-1. 在 `pkg/enums/storage/storages.go` 中添加新的存储端类型, 并运行代码生成
-2. 在 `config/storage` 目录下定义存储端配置, 并添加到 `config/storage/factory.go` 中
-3. 在 `storage` 目录下新建一个包, 编写存储端实现, 然后在 `storage/storage.go` 中导入并添加它
-4. 更新文档, 添加配置说明
+1. 在 `pkg/enums/storage/storages.go` 中添加新的存储端类型,并运行代码生成
+2. 在 `config/storage` 目录下定义存储端配置,并添加到 `config/storage/factory.go` 中
+3. 在 `storage` 目录下新建一个包编写存储端实现,然后在 `storage/storage.go` 中导入注册
+4. 更新文档,添加配置说明
## 贡献新解析器
-你可以选择使用 Go 编写原生的解析器实现(推荐), 或是使用 JavaScript 以插件的方式实现.
+解析器可通过 Go 原生编写(推荐),也可编写为 JavaScript 插件。
-如果使用 Go 编写, 请:
+若使用 Go 编写:
-1. 在 `parsers` 目录下新建一个包, 编写解析器实现
+1. 在 `parsers` 目录下新建包编写解析器实现
2. 在 `parsers/parser.go` 的 `init` 中注册解析器
-如果使用 JavaScript 编写, 请参考 `plugins/example_parser_basic.js` 的实现, 并在该文件夹下新建一个 js 文件, 实现你的解析逻辑.
+若使用 JavaScript 编写,请参考 `plugins/example_parser_basic.js` 并在该文件夹下新建 `.js` 文件实现解析逻辑。
-需要注意, `plugins` 目录下解析器默认不会被编译到二进制文件中, 用户需要手动下载它们并放到本地指定目录下以启用它们.
\ No newline at end of file
+注意:`plugins` 目录下的解析器不打包进二进制文件,需放置于配置指定的插件目录中加载。
\ No newline at end of file
diff --git a/docs/content/zh/contribute/testing.md b/docs/content/zh/contribute/testing.md
new file mode 100644
index 00000000..ba7c88a5
--- /dev/null
+++ b/docs/content/zh/contribute/testing.md
@@ -0,0 +1,60 @@
+---
+title: "测试"
+weight: 10
+---
+
+# 测试
+
+项目提供预置测试数据的本地环境,以及无需 Telegram 凭据的端到端测试。
+
+## 预置环境
+
+```bash
+make testenv
+```
+
+命令会创建临时数据目录并输出:
+
+- Web UI 地址
+- 管理员密码(默认为 `saveany-test`)
+- 预置的本地存储和白名单用户
+- 用于创建直链下载任务的示例 URL
+
+使用 `--keep` 保留生成的数据目录,或传入 `--data-dir` 复用已有目录。Telegram 保持停止状态,可直接测试 Web 控制台及非 Telegram 任务流程。
+
+## 后端端到端测试
+
+```bash
+make e2e-api
+```
+
+测试会构建二进制,在随机端口启动预置环境并调用公开 API。覆盖认证与 CSRF、新手引导、存储与用户生命周期、设置更新、SPA 静态资源回退,以及下载直链文件到预置存储的任务。
+
+测试位于 `e2e/`,使用 `e2e` build tag,普通 `go test ./...` 不会触发。
+
+## Telegram 端到端测试
+
+```bash
+make e2e-telegram
+```
+
+该测试让 Bot handler、dispatcher、任务队列和存储实现运行在内存 `tg.Invoker` 之上。覆盖 `/start`、群聊准入、将 Telegram 文件保存到本地存储(含进度编辑)及上传到 Telegram 存储。全程无需真实 Telegram 账号或外部网络。
+
+## 前端端到端测试
+
+```bash
+make e2e-web-setup # 首次运行, 下载 Chromium
+make e2e-web
+```
+
+启动预置环境并使用 Playwright 驱动 Web UI。覆盖登录、存储配置测试与创建,以及直链任务创建与完成状态校验。测试产物写入 `tmp/e2e-web-results/`。
+
+Playwright 为 `web/ui` 的开发依赖,版本由 `pnpm-lock.yaml` 锁定。`make e2e-web-setup` 会自动安装匹配的 Chromium。
+
+## 完整测试
+
+```bash
+make e2e
+```
+
+依次运行 API、Telegram 和前端浏览器测试(前端测试前会自动构建 Web UI)。假 Telegram 层覆盖了命令、下载、上传与群聊准入;若需测试线上 Telegram 真实行为、Userbot 登录及限流,仍需使用测试账号。
diff --git a/docs/content/zh/deployment/configuration/_index.md b/docs/content/zh/deployment/configuration/_index.md
index dccc99c1..47fd7e41 100644
--- a/docs/content/zh/deployment/configuration/_index.md
+++ b/docs/content/zh/deployment/configuration/_index.md
@@ -1,270 +1,106 @@
---
title: "配置说明"
+weight: 11
---
# 配置说明
-SaveAnyBot 的配置文件使用 toml 格式, 你可以在 [TOML 官方网站](https://toml.io/) 上了解更多关于 toml 的语法.
-
-SaveAnyBot 需要读取工作目录下的 `config.toml` 文件作为配置文件, 若缺少该文件则会创建默认文件, 并尝试从环境变量中加载配置.
-
-以下是一个最简的配置文件示例:
-
-```toml
-[telegram]
-token = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
-[[users]]
-# telegram user id
-id = 777000
-blacklist = true
-
-[[storages]]
-name = "本机存储"
-type = "local"
-enable = true
-base_path = "./downloads"
-```
-
-## 详细配置
-
-### 全局配置
-
-- `lang`: Bot 使用的语言, 默认为 `zh-CN` (简体中文), 设为 `en` 则使用英语.
-- `stream`: 是否启用 Stream 模式, 默认为 `false`. 启用后 Bot 将直接将文件流式传输到存储端(若存储端支持), 不需要下载到本地
-{{< hint warning >}}
-Stream 模式对于磁盘空间有限的部署环境十分有用, 但也有一些弊端:
-
-
-无法使用多线程从 Telegram 下载文件, 速度较慢.
-网络不稳定时, 任务失败率高.
-无法在中间层对文件进行处理, 例如自动文件类型识别.
-并非支持所有存储端, 不支持的存储端可能会降级为普通模式或无法上传.
-
-{{< /hint >}}
-- `workers`: 同时处理任务数量, 默认为 3
-- `threads`: 下载文件时使用的线程数, 默认为 4. 仅在未启用 Stream 模式时生效.
-- `retry`: 任务失败时的重试次数, 默认为 3.
-- `proxy`: 全局代理配置, 配置后程序内一切网络连接将会尝试使用该代理, 可选.
-
-```toml
-lang = "zh-CN"
-stream = false
-workers = 3
-threads = 4
-retry = 3
-proxy = "socks5://127.0.0.1:7890"
-```
-
-### Telegram 配置
-
-- `token`: 你的 Telegram Bot Token, 可以通过 [BotFather](https://t.me/botfather) 创建 Bot 并获取 Token.
-- `app_id`, `app_hash`: Telegram API ID & Hash, 在 [Telegram API](https://my.telegram.org/apps) 创建应用获取, 若不提供则使用默认值.
-- `flood_retry`: Flood 控制重试次数, 默认为 5.
-- `rpc_retry`: RPC 请求重试次数, 默认为 5.
-- `proxy`: 代理配置, 可选.
- - `enable`: 是否启用代理.
- - `url`: 代理地址
-- `userbot`: userbot 配置, 可选.
- - `enable`: 启用 userbot 集成, 需要登录用户账号, 此时请务必使用自己的 api id & hash.
- - `session`: userbot 会话文件路径, 默认为 `data/usersession.db`.
-
-{{< hint warning >}}
-启用 userbot 集成后, bot 可以下载私密频道和群组的文件, 但具有无法避免的账号被封禁的风险.
-
-开启 userbot 集成后第一次启动 bot 时需要通过终端交互输入手机号, 2FA 和验证码.
-
-如果你使用 docker 部署, 请使用 -it 参数为容器提供交互式环境, 然后执行登录操作.
-{{< /hint >}}
-
-```toml
-[telegram]
-token = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-app_id = 1025907
-app_hash = "452b0359b988148995f22ff0f4229750"
-flood_retry = 5
-rpc_retry = 5
-[telegram.proxy]
-enable = false
-url = "socks5://127.0.0.1:7890"
-[telegram.userbot]
-enable = false
-session = "data/usersession.db"
-```
-
-### Aria2 配置
-
-Aria2 是一个强大的下载管理器,支持 HTTP/HTTPS、FTP、BitTorrent 等多种协议。启用后,Bot 可以使用 `/aria2dl` 命令通过 Aria2 下载文件。
-
-- `enable`: 是否启用 Aria2 支持,默认为 `false`
-- `url`: Aria2 RPC 地址,通常为 `http://localhost:6800/jsonrpc`
-- `secret`: Aria2 RPC 密钥,如果你在 Aria2 中配置了 `rpc-secret`,需要在此填写
-- `remove_after_transfer`: 转存完成后是否删除 Aria2 下载的本地文件,默认为 `true`
-
-{{< hint info >}}
-Aria2 需要单独安装和运行。你可以参考 [Aria2 官方文档](https://aria2.github.io/) 了解如何安装和配置 Aria2。
-{{< /hint >}}
-
-```toml
-[aria2]
-enable = true
-url = "http://localhost:6800/jsonrpc"
-secret = "your-rpc-secret"
-remove_after_transfer = true
-```
-
-### yt-dlp 配置
-
-用于配置 `/ytdlp` 命令以及 HTTP API 中 `ytdlp` 任务类型在未传自定义参数时的默认行为.
-
-- `max_height`: 默认下载的最高视频清晰度 (按高度限制), 如 `1080`, `720`, `480`; `0` 表示不限制 (下载最佳画质). 当设置了 `format` 时此项被忽略.
-- `format`: 直接指定 yt-dlp format 选择表达式, 设置后优先级高于 `max_height`, 例如 `bv*[height<=720]+ba/b`.
-- `recode`: 下载后转封装的视频容器格式 (如 `mp4`), 留空则不转封装.
-- `filename_template`: 下载文件名模板 (yt-dlp output template), 默认 `%(title)s.%(ext)s`. 模板中的目录会创建在 bot 的下载目录下, 并保留到存储路径中.
-- `restrict_filenames`: 将文件名限制为 ASCII 字符 (yt-dlp `--restrict-filenames`), 同时会去掉空格和 `&`. 默认关闭.
-
-{{< hint info >}}
-`max_height`、`format` 和 `recode` 仅在使用 `/ytdlp` 命令 (或 API 的 `ytdlp` 任务类型) 且未传任何自定义参数时生效. `filename_template` 和 `restrict_filenames` 在传了其他参数时依然生效, 除非自己传了对应的参数 (`-o/--output`、`--restrict-filenames` 或 `--no-restrict-filenames`).
-{{< /hint >}}
-
-```toml
-[ytdlp]
-max_height = 1080
-format = "" # 例如 "bv*[height<=720]+ba/b"
-recode = "mp4" # 留空则不转封装
-filename_template = "%(title)s.%(ext)s" # 例如 "%(uploader)s - %(title)s.%(ext)s"
-restrict_filenames = false
-```
-
-### HTTP API 配置
-
-启用后, SaveAny-Bot 会暴露一套 HTTP API, 用于以编程方式创建/查询/取消任务. 完整的接口说明见 [HTTP API](../../usage/api).
-
-- `enable`: 是否启用 HTTP API 服务, 默认为 `false`.
-- `host`: 监听地址, 默认 `0.0.0.0`.
-- `port`: 监听端口, 默认 `8080`.
-- `token`: 鉴权 Token, **强烈建议设置** — 若为空, API 将在无任何鉴权的情况下暴露.
-
-```toml
-[api]
-enable = false
-host = "0.0.0.0"
-port = 8080
-token = "your-token"
-```
-
-### 日志配置
-
-- `level`: 日志级别, 可选 `debug`, `info`, `warn`, `error`, `fatal`. 默认为 `info`.
-
-```toml
-[log]
-level = "info"
-```
-
-### 存储端列表
-
-存储端列表用于定义 Bot 支持的存储位置, 每个存储端需要指定名称、类型和相关配置, 使用双中括号语法 `[[storages]]` 定义.
-
-每一个存储端至少需要以下字段:
-
-- `name`: 存储端名称, 用于在 Bot 中识别, 需要唯一
-- `enable`: 是否启用该存储端, 默认为 `true`
-- `type`: 存储端类型, 目前支持以下类型:
- - `local`: 本地磁盘
- - `alist`: Alist
- - `webdav`: WebDAV
- - `s3`: aws S3 及其他兼容 S3 的服务
- - `rclone`: 调用 rclone 实现上传
- - `telegram`: 上传到 Telegram
-
-示例, 这是一个包含本地存储和 webdav 存储的配置:
-
-```toml
-[[storages]]
-name = "本地存储"
-type = "local"
-enable = true
-# 以下是 local 类型存储的自定义配置
-base_path = "./downloads"
-
-[[storages]]
-name = "WebDAV"
-type = "webdav"
-enable = true
-# 以下是 webdav 类型存储的自定义配置
-url = "https://example.com/webdav"
-base_path = "/path/to/webdav"
-username = "your_username"
-password = "your_password"
-```
-
-所有存储端的自定义配置项可查看 [存储端配置](./storages)
-
-### 用户列表
-
-用户列表用于定义对存储端的访问控制, 每个用户需要指定 Telegram 上的用户 ID, 使用双中括号语法 `[[users]]` 定义.
-
-- `id`: 用户的 Telegram User ID
-- `storages`: 过滤的存储端列表, 使用存储端名称定义, 默认为白名单模式 (即只允许访问列表中的存储端)
-- `blacklist`: 是否启用黑名单模式, 默认为 `false`. 若启用黑名单模式, 则仅允许访问**没有**在列表中的存储端.
-
-示例, 这是一个包含三个用户的配置, 用户 `123123` 只能访问本地存储, 用户 `456456` 只能访问除 WebDAV 以外的存储, 用户 `789789` 启用黑名单模式但没有指定存储端, 因此可以访问所有存储:
+配置保存在数据目录下的 SQLite 数据库(默认 `data/saveany.db`),通过 Web 控制台统一管理。首次启动采用默认设置,并在日志中输出带 Token 的直达链接;可通过 `SAVEANY_ADMIN_PASSWORD` 预设管理员密码。
-```toml
-[[users]]
-id = 123123
-storages = ["本地存储"]
-
-[[users]]
-id = 456456
-storages = ["WebDAV"]
-blacklist = true
-
-[[users]]
-id = 789789
-storages = []
-blacklist = true
-```
+## 配置页面
-### 事件触发
-
-事件触发提供了在 Bot 处理任务时根据任务状态执行自定义操作的能力, 目前仅支持任意命令执行. 使用 `[hook.exec]` 配置.
+| 页面 | 内容 |
+|---|---|
+| 指南 | 首次配置检查清单和日常使用说明 |
+| Telegram | Bot Token、API ID/Hash、代理、Userbot 登录 |
+| 存储 | 本地磁盘、WebDAV、Alist、S3、Rclone、Telegram 等存储端 |
+| 用户 | Telegram 用户 ID、默认存储、目录、规则和存储访问权限 |
+| 自动化 | 按用户管理目录、规则、监听聊天和对话同步 |
+| 设置 | 任务并发、语言、代理、媒体类型过滤、Aria2、yt-dlp、解析器、缓存、日志、Web 和 API |
+| 解析器 | 安装 JavaScript 解析器插件 |
+| 系统 | 运行信息、重启、版本检查和更新 |
-目前具有以下几种事件类型:
+部分配置需重启进程生效(如任务并发数、全局代理、缓存、插件、Hook 及 Web 监听地址),保存时页面会明确提示。
-- `task_before_start`: 任务即将开始前
-- `task_success`: 任务成功完成后
-- `task_fail`: 任务失败后
-- `task_cancel`: 任务被取消后
+### 自动化
-提供的配置值需要为完整的命令行命令, Bot 会在事件发生时执行该命令. 示例:
-
-```toml
-[hook.exec]
-task_before_start = "echo '任务即将开始'"
-task_success = "bash /path/to/success_script.sh"
-task_fail = "curl -X POST https://example.com/api/notify -d '任务失败'"
-task_cancel = "bash /path/to/cancel_script.sh"
-```
+自动化页面按用户分别管理目录、规则、监听聊天及 Userbot 对话同步。可设置默认目录,并支持规则的启停、增删与预设导入。
### 解析器
-解析器为 Bot 提供了处理非 Telegram 文件的能力, 例如从其他网站下载文件. 使用 `[parsers]` 配置.
+解析器页面用于安装 JavaScript 解析器插件(需在设置中启用解析器插件)。支持上传 `.js` 文件或直接粘贴代码,安装后立即可用,无需重启。
+
+### Telegram
+
+Bot Token 在 [@BotFather](https://t.me/BotFather) 创建。`API ID` 和 `API Hash` 已提供默认值,通常无需修改;启用 Userbot 时建议使用自己的 [Telegram API](https://my.telegram.org/apps) 凭据。
+
+保存 Bot 配置后会自动建立连接。Userbot 的登录在 Telegram 页面完成,无需进入终端。
+
+### 辅助 Bot
+
+配置辅助 Bot 可提升 Telegram 文件的传输效率。在 **Telegram** 页面的 **辅助 Bot** 区域填入 Token,将辅助 Bot 加入目标 Telegram 聊天并授予发文件权限。Telegram 存储上传将自动分配给可用且在群内的辅助 Bot;若辅助 Bot 无法访问目标聊天,上传自动回退到主 Bot。辅助 Bot 不响应命令。
+
+若需通过多个辅助 Bot 分块加速下载大文件,可在 **Telegram** 页面填写私有的 **辅助下载频道 ID**。需确保主 Bot 可向该频道发帖,且频道内已加入至少两个辅助 Bot。主 Bot 会把收到的文件转发到辅助下载频道,各辅助 Bot 刷新文件引用后下载各自的分块。该功能仅在非流式模式下对达到 **Relay 最小文件大小**(默认 8 MB)的文件生效;频道或辅助 Bot 异常时自动回退为单客户端下载。默认保留转发到频道的消息;关闭“保留频道转发消息”将在下载完成后自动清理。
+
+传输并发可在 **设置 → 核心** 中调整:
+
+| 设置 | 默认值 | 说明 |
+|---|---|---|
+| `单文件下载线程数` | 4 | 自动调度的基础线程数 |
+| `下载线程覆盖` | 0 | 0 表示自动;大于 0 时直接使用该总线程数 |
+| `自动线程上限` | 16 | 自动调度时的总线程上限 |
+| `全局传输并发上限` | 0 | 0 表示 `workers × threads`;下载与上传共享 |
+| `Relay 最小文件大小 (MB)` | 8 | 更小的文件不使用辅助 Bot |
+| `分块重试次数` | 20 | 单个分块超时或连接中断的最大重试次数 |
+
+### 媒体过滤
+
+在 **设置 → 媒体过滤** 中可以限制 Bot 只处理指定类型的 Telegram 媒体。`允许的媒体类型` 每行填写一个值,留空表示全部允许:
+
+| 值 | 匹配内容 |
+|---|---|
+| `image` | Telegram 图片、图片文件、贴纸和动态图片 |
+| `video` | Telegram 视频文件(含视频消息) |
+| `audio` | Telegram 音频和语音消息 |
+| `document` | 其余文档文件 |
+
+过滤在创建下载任务之前执行,不匹配的媒体不会下载,也不会出现在任务列表。媒体组只保留匹配项;如果整组都被过滤,则静默跳过。该设置是实例级配置,修改后立即生效。
+
+目前只覆盖 Telegram 媒体入口:直接发送/转发媒体、媒体组、`/save` 批量保存、消息链接、监听聊天和 Telegraph 图片。解析器、HTTP 直链和 yt-dlp 等非 Telegram 媒体不参与该过滤。存储规则只负责改变归档位置,不会跳过文件。
+
+### 消息元数据
+
+在 **设置 → 消息元数据** 中启用后,新的 Telegram 文件任务会保存规范化后的消息数据。本地数据库记录来源聊天、消息 ID、时间、发送者、正文或 caption、回复/转发引用、相册分组及媒体属性;任务详情 API 和 Web 控制台可导出对应 JSON。
+
+“保存消息文本”控制是否保存正文与标签。该选项不保存 access hash、Token 或原始 Telegram 对象,且仅对启用后创建的新任务生效。
+
+“写入旁车 JSON”会在实际文件旁写入 `<文件路径>.meta.json`,默认关闭。Telegram 存储不会上传旁车文件,而是可用存储配置中的选项消费元数据来还原 caption 或媒体组。
+
+### 存储
+
+存储端在 **存储** 页面添加并启用。本地磁盘只需要一个基础路径:
+
+- Docker 部署通常填写 `/app/downloads`。
+- 二进制部署可以填写 `./downloads` 或绝对路径。
+
+存储端字段说明见 [存储端配置](./storages)。
+
+### 用户
+
+只有 **用户** 页面中启用的 Telegram User ID 能使用 Bot。新建用户时至少填写数字 User ID;默认存储可以留空,之后再设置。
-```toml
-[parsers]
-plugin_enable = true # 是否启用解析器插件
-plugin_dirs = ["./plugins"] # 插件目录, 可以是多个目录
-```
+## 环境变量
-上述两个配置项只用于控制以 JavaScript 编写的解析器插件, Bot 还有内置的使用 Go 实现的解析器, 目前默认开启.
+| 变量 | 说明 |
+|---|---|
+| `SAVEANY_DATA_DIR` | 数据目录,默认 `./data` |
+| `SAVEANY_DB_PATH` | SQLite 文件路径,默认 `/saveany.db` |
+| `SAVEANY_LISTEN` | Web 监听地址,默认 `127.0.0.1:28080`;需要对外开放时使用 `0.0.0.0:28080` |
+| `SAVEANY_ADMIN_PASSWORD` | 首次启动时设置备用管理员密码;未设置则自动生成并打印 |
-### 杂项
+## 安全
-```toml
-no_clean_cache = false # 是否在退出时不清空缓存文件夹
-# 临时下载文件夹配置
-[temp]
-base_path = "./cache"
-```
\ No newline at end of file
+- Web 控制台默认仅监听 `127.0.0.1:28080`;若需对外开放,请使用反向代理和 HTTPS。
+- 设置中的 `访问 Token` 可用于 `/?token=...` 直接登录 Web UI,也用于 `/api/v1` 的 Bearer 认证。留空时,每次启动自动生成并打印到日志。
+- 首次使用密码登录后,立即修改自动生成的备用管理员密码。
+- 不要公开 `data/`、Bot Token、Userbot 会话或访问 Token。
diff --git a/docs/content/zh/deployment/configuration/storages.md b/docs/content/zh/deployment/configuration/storages.md
index 4dce9462..0159a746 100644
--- a/docs/content/zh/deployment/configuration/storages.md
+++ b/docs/content/zh/deployment/configuration/storages.md
@@ -4,15 +4,17 @@ title: "存储端配置"
# 存储端配置
-请先阅读 [配置说明](../) 了解配置文件的基本格式.
+在 Web 控制台的 **存储** 页面添加存储时填写以下字段。
+
+存储名称支持空格、中文与常用符号(最多 64 字符,不可含控制字符)。在 `/dirs`、`/transfer` 等命令中引用含空格的名称时需加双引号。名称可在 Web 控制台随时更改,用户默认存储、目录及规则均基于内部存储 ID 关联,不受重命名影响。
## Alist
`type=alist`
-不支持 Stream 模式.
+不支持流式下载(Stream 模式)。
-```toml
+```text
url = "https://alist.example.com" # Alist 的 URL
username = "your_username" # Alist 的用户名
password = "your_password" # Alist 的密码
@@ -27,14 +29,14 @@ token = "your_token"
`type=local`
-```toml
+```text
base_path = "./downloads" # 本地存储的基础路径, 所有文件将存储在此路径下
```
## WebDAV
`type=webdav`
-```toml
+```text
url = "https://webdav.example.com" # WebDAV 的 URL
username = "your_username" # WebDAV
password = "your_password" # WebDAV 的密码
@@ -45,8 +47,8 @@ base_path = "/path/to/webdav" # WebDAV 中的基础路径, 所有文件将存储
`type=s3`
-```toml
-endpoint = "s3.example.com" # S3 的端点, 默认为 aws S3 的端点
+```text
+endpoint = "https://s3.example.com" # S3 的端点, 默认为 aws S3 的端点
region = "us-east-1" # S3 的区域
access_key_id = "your_access_key_id" # S3 的访问密钥 ID
secret_access_key = "your_secret_access_key" # S3 的秘密访问密钥
@@ -67,36 +69,42 @@ https://your_bucket_name.s3.example.com/path/to/s3/your_file
https://s3.example.com/your_bucket_name/path/to/s3/your_file
```
-如果你使用的是第三方的兼容 S3 的服务, 一般使用的是路径风格的 URL. 而 AWS S3 则通常使用虚拟主机风格的 URL. 详情请参考你所使用的 S3 兼容服务的文档.
+第三方 S3 兼容服务通常采用路径风格 URL,AWS S3 则默认使用虚拟主机风格。具体请参考对应服务文档。
## Telegram
`type=telegram`
-不支持 Stream 模式.
+不支持流式下载(Stream 模式)。
-```toml
+```text
# Telegram 聊天 ID, Bot 将把文件发送到这个聊天
-chat_id = "123456789"
+chat_id = "-1001234567890" # Bot API 聊天 ID(用户为正数,普通群为负数,超级群/频道使用 -100...)
# 是否强制使用文件方式发送, 默认为 false
force_file = false
# 是否跳过大文件, 默认为 false. 如果启用, 超过 Telegram 限制的文件将不会上传.
skip_large = false
-# 超限视频是否使用 FFmpeg 无损分割成一个媒体组(最多 10 个)内可独立播放的小视频;失败或需要超过 10 段时回退到 ZIP 分卷.
+# 超限视频是否使用 FFmpeg 分割为单组(最多 10 个)独立播放的小视频;失败或超过 10 段时回退为 ZIP 分卷.
split_large_video = false
# 分卷大小, 单位 MB. 设为 0 时使用实际上传账号的限制:Bot/普通用户为 2000 MB,Premium 用户为 4000 MB.
# 超过该大小的文件将被分割成多个部分上传;非视频使用 ZIP 格式.
# 当 skip_large 启用时, 该选项无效.
split_size_mb = 0
+# 没有实时 caption 时,用元数据中的消息文本作为发送 caption
+use_metadata_caption = false
+# 没有实时分组信息时,用元数据 grouped_id 将文件作为媒体组发送
+use_metadata_album = false
```
+启用消息元数据后,Telegram 存储会直接消费元数据,不会把 `.meta.json` 作为文件上传。`use_metadata_caption` 在没有实时 caption 时用原消息文本还原 caption;`use_metadata_album` 在没有实时分组信息时用元数据 `grouped_id` 将文件作为媒体组发送。两项默认关闭。媒体组只会在第一个文件上添加 caption,以符合 Telegram 客户端行为。
+
## Rclone
`type=rclone`
通过 [rclone](https://rclone.org/) 命令行工具支持多种云存储服务. 需要先安装 rclone 并配置好远程存储.
-```toml
+```text
# rclone 配置的远程名称, 可以是任何在 rclone.conf 中配置的远程
remote = "mydrive"
# 在远程存储中的基础路径, 所有文件将存储在此路径下
@@ -109,22 +117,15 @@ flags = ["--transfers", "4", "--checkers", "8"]
### 配置 rclone 远程
-首先需要配置 rclone 远程, 运行 `rclone config` 命令进行交互式配置, 或直接编辑 `rclone.conf` 文件.
+首先需要配置 rclone 远程,运行 `rclone config` 进行交互式配置,或直接编辑 `rclone.conf` 文件。
-rclone 支持多种云存储服务, 包括但不限于:
-- Google Drive
-- Dropbox
-- OneDrive
-- Amazon S3 及兼容服务
-- SFTP
-- FTP
-- 更多服务请参考 [rclone 官方文档](https://rclone.org/overview/)
+支持 Google Drive、Dropbox、OneDrive、S3 兼容存储、SFTP/FTP 等服务,详见 [rclone 官方文档](https://rclone.org/overview/)。
### 使用示例
配置 Google Drive 后, 可以这样配置存储:
-```toml
+```text
[[storages]]
name = "GoogleDrive"
type = "rclone"
@@ -135,7 +136,7 @@ base_path = "/SaveAnyBot"
如果使用自定义的 rclone 配置文件:
-```toml
+```text
[[storages]]
name = "MyRemote"
type = "rclone"
diff --git a/docs/content/zh/deployment/installation.md b/docs/content/zh/deployment/installation.md
index f52fe31f..0e85b593 100644
--- a/docs/content/zh/deployment/installation.md
+++ b/docs/content/zh/deployment/installation.md
@@ -1,30 +1,55 @@
---
title: "安装与更新"
+weight: 10
---
# 安装与更新
-## 从预编译文件部署(推荐)
+准备空数据目录即可启动,随后在 Web 控制台完成 Bot、存储和用户配置。
-在 [Release](https://github.com/krau/SaveAny-Bot/releases) 页面下载对应平台的二进制文件.
+## 从预编译文件部署
-在解压后目录新建 `config.toml` 文件, 参考 [配置说明](../configuration) 编辑配置文件
-
-运行:
+在 [Release](https://github.com/krau/SaveAny-Bot/releases) 页面下载对应平台的二进制文件并解压:
```bash
chmod +x saveany-bot
./saveany-bot
```
+程序默认使用当前目录下的 `data/` 保存数据库和会话。Web UI 默认监听 `127.0.0.1:28080`,启动时会输出带访问 Token 的直达地址:
+
+```text
+Web UI available at http://127.0.0.1:28080/?token=...
+```
+
+打开该地址即可登录。未在“设置 → Web”中指定访问 Token 时,每次启动均会重新生成。
+
+如需在其他网卡或端口访问,通过命令行指定监听地址:
+
+```bash
+./saveany-bot --listen 0.0.0.0:28080
+```
+
+对应的环境变量为 `SAVEANY_LISTEN`:
+
+```bash
+SAVEANY_LISTEN='0.0.0.0:28080' ./saveany-bot
+```
+
+自动生成的管理员密码用于首次登录,也可通过环境变量 `SAVEANY_ADMIN_PASSWORD` 预设。初始密码仅在首次启动时打印,登录后可在侧栏修改密码。打开日志中的直达地址后,参照指南页面完成初始化:
+
+1. 在 Telegram 页面填写 Bot Token。
+2. 在存储页面添加至少一个存储。
+3. 在用户页面添加你的 Telegram User ID。
+
### 进程守护
{{< tabs "daemon" >}}
{{< tab "systemd (常规 Linux)" >}}
-创建文件 /etc/systemd/system/saveany-bot.service 并写入以下内容:
+创建 `/etc/systemd/system/saveany-bot.service`:
-{{< codeblock >}}
+```ini
[Unit]
Description=SaveAnyBot
After=systemd-user-sessions.service
@@ -37,133 +62,97 @@ Restart=always
[Install]
WantedBy=multi-user.target
-{{< /codeblock >}}
-
-设为开机启动并启动服务:
+```
-{{< codeblock >}}
+```bash
systemctl enable --now saveany-bot
-{{< /codeblock >}}
+```
{{< /tab >}}
-
{{< tab "procd (OpenWrt)" >}}
-添加开机自启动服务
-
-创建文件 /etc/init.d/saveanybot ,参考 wrt_init 并自行修改:
+创建 `/etc/init.d/saveanybot`,参考 [wrt_init](https://github.com/krau/SaveAny-Bot/blob/main/docs/confs/wrt_init) 并修改工作目录与程序路径:
-{{< codeblock >}}
+```sh
#!/bin/sh /etc/rc.common
-
-#This is the OpenWRT init.d script for SaveAnyBot
-
-START=99
+START=99
STOP=10
description="SaveAnyBot"
-
WORKING_DIR="/mnt/mmc1-1/SaveAnyBot"
EXEC_PATH="$WORKING_DIR/saveany-bot"
-start() {
- echo "Starting SaveAnyBot..."
- cd $WORKING_DIR
- $EXEC_PATH &
-}
-stop() {
- echo "Stopping SaveAnyBot..."
- killall saveany-bot
-}
-reload() {
- stop
- start
-}
-
-{{< /codeblock >}}
-
-赋予权限:
-
-{{< codeblock >}}
-chmod +x /etc/init.d/saveanybot
-{{< /codeblock >}}
-
-然后将文件复制到 /etc/rc.d 并重命名为 S99saveanybot, 同样赋予权限:
-
-{{< codeblock >}}
-chmod +x /etc/rc.d/S99saveanybot
-{{< /codeblock >}}
-
-添加快捷指令
-
-创建文件 /usr/bin/sabot ,参考 wrt_bin 并自行修改,注意此处文件编码仅支持 ANSI 936 .
-
-随后赋予权限:
-
-{{< codeblock >}}
-chmod +x /usr/bin/sabot
-{{< /codeblock >}}
+start() { cd $WORKING_DIR && $EXEC_PATH & }
+stop() { killall saveany-bot; }
+```
-使用: sudo sabot start|stop|restart|status|enable|disable
+```bash
+chmod +x /etc/init.d/saveanybot
+ln -s /etc/init.d/saveanybot /etc/rc.d/S99saveanybot
+```
{{< /tab >}}
{{< /tabs >}}
-
## 使用 Docker 部署
### Docker Compose
-下载 [docker-compose.yml](https://github.com/krau/SaveAny-Bot/blob/main/docker-compose.yml) 文件, 在同目录下新建 `config.toml` 文件, 参考 [config.example.toml](https://github.com/krau/SaveAny-Bot/blob/main/config.example.toml) 编辑配置文件.
-
-启动:
+下载 [docker-compose.yml](https://github.com/krau/SaveAny-Bot/blob/main/docker-compose.yml),按需修改端口和数据目录,然后启动:
```bash
docker compose up -d
+docker compose logs -f saveany-bot
+```
+
+默认挂载:
+
+- `./data:/app/data` — 设置、任务记录和会话
+- `./downloads:/app/downloads` — 本地存储的默认目录
+- `./cache:/app/cache` — 下载缓存
+
+可指定管理员密码;未指定时从首启日志读取生成的密码。使用桥接网络时需将监听地址设为 0.0.0.0:
+
+```yaml
+environment:
+ SAVEANY_ADMIN_PASSWORD: change-me
+ SAVEANY_LISTEN: 0.0.0.0:28080
```
### Docker
-```shell
+```bash
docker run -d --name saveany-bot \
- -v /path/to/config.toml:/app/config.toml \
- -v /path/to/downloads:/app/downloads \
+ -p 28080:28080 \
+ -e SAVEANY_LISTEN=0.0.0.0:28080 \
+ -v ./data:/app/data \
+ -v ./downloads:/app/downloads \
+ -v ./cache:/app/cache \
ghcr.io/krau/saveany-bot:latest
```
-{{< hint info >}}
-关于 docker 镜像的变体版本
-
-
-默认版本: 包含所有功能和依赖, 体积较大. 如果没有特殊需要, 请使用此版本
-micro: 精简版本, 去除部分可选依赖, 体积较小
-pico: 极简版本, 仅包含核心功能, 体积最小
-
-你可以根据需要, 通过指定不同的标签来拉取合适的版本, 例如: ghcr.io/krau/saveany-bot:micro
-
-关于变体版本的更详细的区别, 请参考项目根目录下的 Dockerfile 文件.
-{{< /hint >}}
+打开日志中输出的直达链接。若使用本地存储,容器内路径可设为 `/app/downloads`。
## 更新
-若使用预编译二进制文件部署, 使用以下 CLI 命令更新:
+### 预编译二进制
```bash
./saveany-bot up
```
-该命令会就地替换二进制文件; 已运行的 Bot 会在重启后才使用新版本. 也可以在 Telegram 中发送 `/update`, Bot 会自行下载新版本、替换自身二进制并就地重启进程 (Windows 上无法就地重启, 进程会退出, 由服务管理器启动新版本).
+或在 Telegram 中发送 `/update`。更新后进程会自动重启(Windows 环境需手动或由服务管理器重启)。
-如果是 Docker 部署, 使用以下命令更新:
-
-docker:
+### Docker
```bash
docker pull ghcr.io/krau/saveany-bot:latest
docker restart saveany-bot
```
-docker compose:
+### Docker Compose
```bash
docker compose pull
-docker compose restart
-```
\ No newline at end of file
+docker compose up -d
+```
+
+更新前请备份 `data/`,并保留 Docker 数据卷。
diff --git a/docs/content/zh/deployment/migration.md b/docs/content/zh/deployment/migration.md
new file mode 100644
index 00000000..780d5eca
--- /dev/null
+++ b/docs/content/zh/deployment/migration.md
@@ -0,0 +1,132 @@
+---
+title: "从 v0 升级"
+weight: 15
+---
+
+# 从 v0 升级
+
+v1 使用本地 SQLite 数据库保存配置。首次启动若检测到 v0 的 `config.toml`,会自动导入数据库;此后所有配置均在 Web 控制台管理。
+
+v1 不迁移 Telegram 会话。旧版 `session.db` 与 `usersession.db` 已弃用,迁移仅导入旧版配置并写入新数据库。Bot 将使用现有 Token 重新登录,Userbot 则需在 Web 控制台重新登录。升级前请完整备份数据。
+
+## 1. 停止并备份
+
+先停止 v0,再备份配置、数据和下载目录:
+
+```bash
+docker compose down
+cp -a config.toml data downloads /path/to/backup/
+```
+
+如果使用二进制部署,直接停止进程后备份相同目录即可。
+
+不要同时运行 v0 和 v1,也不要让两个实例共用同一个 `data/` 目录。
+
+## 2. 保留数据目录
+
+v1 不复用 v0 数据库。请保留旧文件作为备份,由 v1 创建新的 `data/saveany.db`:
+
+| v0 文件 | v1 默认路径 | 说明 |
+|---|---|---|
+| `data/saveany.db` | 不复用 | 保留作为备份;v1 创建新数据库 |
+
+不要使用 `--db-path` 或 `SAVEANY_DB_PATH` 指向 v0 数据库(旧 schema 不兼容)。请在启动 v1 前将旧的 `data/saveany.db` 移出或重命名备份,由 v1 在默认路径创建新数据库。
+
+v0 的 `data/session.db` 和 `data/usersession.db` 会被忽略,确认已备份后可删除。v1 使用新的会话文件:
+
+| 文件 | 说明 |
+|---|---|
+| `data/bot_session.db` | Bot 会话 |
+| `data/userbot_session.db` | Userbot 会话 |
+
+Bot 使用当前 Token 重新登录。启用 Userbot 时,请稍后在 Telegram 页面重新登录。
+
+## 3. 首次启动时导入配置
+
+首次启动必须让 v1 能读到旧版 `config.toml`。Docker 只在这时挂载配置文件:
+
+```yaml
+services:
+ saveany-bot:
+ image: ghcr.io/krau/saveany-bot:latest
+ restart: unless-stopped
+ ports:
+ - "28080:28080"
+ environment:
+ SAVEANY_LISTEN: 0.0.0.0:28080
+ volumes:
+ - ./data:/app/data
+ - ./config.toml:/app/config.toml:ro
+ - ./downloads:/app/downloads
+ - ./cache:/app/cache
+```
+
+```bash
+docker compose up -d
+docker compose logs -f saveany-bot
+```
+
+二进制部署:
+
+```bash
+./saveany-bot --config /path/to/config.toml
+```
+
+省略 `--config` 时,v1 会依次查找工作目录下的 `./config.toml` 和 `/etc/saveany/config.toml`。
+
+日志中出现配置导入完成后,即可移除 `config.toml` 挂载并重启。v1 在数据库中写入导入标记,不会重复导入。`config.toml` 中的 `db.session` 和 `telegram.userbot.session` 会被忽略,v1 始终使用新的会话文件路径。
+
+## 4. 登录并核对
+
+启动日志会打印带访问 Token 的 Web UI 直达地址,直接打开即可登录。如果首次启动没有设置 `SAVEANY_ADMIN_PASSWORD`,备用管理员密码也会打印在日志中:
+
+```bash
+docker compose logs saveany-bot | grep -E "Web UI available at|Generated web admin password"
+```
+
+登录后打开 **指南**,确认:
+
+- Bot 已使用当前 Token 重新连接。
+- 若启用了 Userbot,已重新登录并显示账号。
+- 存储已启用且没有初始化错误。
+- 用户列表包含原有账号。v0 配置中不存在的旧用户会被禁用,需要时手动启用。
+- 从 `config.toml` 导入的存储、用户和设置仍然存在。
+- 目录、规则和监听需要在 v1 中重新创建;v0 数据库行不会迁移。
+
+然后向 Bot 发送一个文件或 Telegram 消息链接,确认任务能正常完成。
+
+## 5. 迁移之后
+
+- 修改 `config.toml` 不再生效,请直接在 Web 控制台调整设置。
+- MinIO 存储会在启动时转换为 `s3`,请在 Web 控制台核对 Endpoint、密钥和 Bucket。
+- 更新前备份整个 `data/` 目录,并保留 Docker 数据卷。
+- Docker 更新使用 `docker compose pull && docker compose up -d`。
+- 若更换 Bot Token,建议删除 `data/bot_session.db` 后重启,避免旧会话不匹配。
+
+## 常见问题
+
+### 未导入配置
+
+确认配置文件路径正确、内容仍是合法 TOML,并检查启动日志。导入标记只会在导入成功后写入;已有同名存储或配置组不会被覆盖。
+
+### 旧会话未迁移
+
+v1 不读取旧版 gotgproto 的 `session.db` 或 `usersession.db`。Bot 会使用当前 Token 自动重新登录;启用 Userbot 时,在 Telegram 页面重新登录即可。旧文件可以保留在备份中,确认不再需要后删除。
+
+### 旧数据库未迁移
+
+v1 不复用 v0 数据库或其 `[db] path`。请保留旧文件作为备份,由 v1 创建新数据库。
+
+### 本地存储路径失效
+
+v0 的本地存储路径相对于进程工作目录。Docker 中通常是 `/app`,请确认 `downloads/` 仍挂载到同一路径。
+
+### 忘记登录密码
+
+查看首次启动日志中的 `Generated web admin password`。密码只保存哈希,无法从数据库还原;若日志已丢失,请先备份数据库,再清空其中的 `auth` 设置并重启,重新生成密码。
+
+## 相关文档
+
+- [安装与更新](../installation)
+- [Web 控制台配置](../configuration/)
+- [使用帮助](../../usage/)
diff --git a/docs/content/zh/help/_index.md b/docs/content/zh/help/_index.md
index c1ff08e0..a3ec640a 100644
--- a/docs/content/zh/help/_index.md
+++ b/docs/content/zh/help/_index.md
@@ -5,14 +5,14 @@ weight: 15
# 常见问题
-## 上传 alist 失败也会显示成功
+## 上传 Alist 失败但界面显示成功
-在 alist 管理页面适当调整上传分片大小, 为 alist 使用更稳定的网络环境部署, 都可以减少这种情况的发生.
+可尝试在 Alist 管理页面调小上传分片大小,或改善 Alist 部署环境的网络质量。
-## Bot 提示下载成功但是 alist 未显示
+## Bot 提示下载成功但 Alist 未显示文件
-alist 缓存了目录结构, 参考 文档 可以调整缓存时间
+Alist 缓存了目录结构,可参考 [Alist 缓存文档](https://alist.nn.ci/zh/guide/drivers/common.html#缓存过期) 缩短缓存过期时间或手动刷新目录。
-## docker部署配置了代理后仍无法连接 telegram (初始化客户端超时)
+## Docker 部署配置代理后无法连接 Telegram(初始化客户端超时)
-docker 不能直接访问宿主机网络, 如果你不熟悉其用法, 请将容器设为 host 模式.
\ No newline at end of file
+容器默认无法直接通过 `127.0.0.1` 访问宿主机网络。可将代理地址设为宿主机真实局域网 IP,或将容器网络模式改为 `host`。
\ No newline at end of file
diff --git a/docs/content/zh/usage/_index.md b/docs/content/zh/usage/_index.md
index 93e906cb..459ff77b 100644
--- a/docs/content/zh/usage/_index.md
+++ b/docs/content/zh/usage/_index.md
@@ -5,12 +5,39 @@ weight: 10
# 使用帮助
-这里介绍 Save Any Bot 的一些功能和使用方法, 如果你没有在这里找到你需要的内容, 另请参阅 [配置说明](../deployment/configuration) 或前往 Github [Discussions](https://github.com/krau/SaveAny-Bot/discussions) 提问.
+介绍 Save Any Bot 的常用功能与命令操作。如需部署及高级配置,请参阅[配置说明](../deployment/configuration);其他问题可在 GitHub [Discussions](https://github.com/krau/SaveAny-Bot/discussions) 交流。
+
+## Bot 命令
+
+| 命令 | 用途 |
+|---|---|
+| `/start` | 打开帮助和命令列表 |
+| `/save` | 保存回复的媒体消息,或按消息范围批量保存 |
+| `/download` | 使用直接下载、Aria2 或视频解析下载链接 |
+| `/transfer` | 在存储端之间传输文件 |
+| `/tasks` | 查看和管理运行中、排队中和失败任务 |
+| `/config` | 用户设置:文件名和重名策略、静默模式、默认存储 |
+| `/template` | 设置自定义文件名模板 |
+| `/dirs` | 管理存储目录 |
+| `/rules` | 管理存储规则 |
+| `/watch` | 监听聊天并管理通知/对话同步 (UserBot) |
+| `/parser` | 管理解析器插件 |
+| `/update` | 检查更新 |
## 转存文件
-要使用 Bot 的转存 Telegram 文件功能, 需要向 Bot 发送或转发以下类型的消息.
+向 Bot 发送或转发以下内容即可触发转存:
+
+1. 图片、视频、音频、文档等媒体文件
+2. Telegram 消息链接(如 `https://t.me/acherkrau/1097`),支持转存开启了防转发/防保存保护的频道文件
+3. Telegra.ph 文章链接,自动提取并下载其中的所有图片
+
+## 在群组中使用
+
+Bot 支持普通群组和超级群组,不支持广播频道。
-1. 文件或媒体消息, 如图片, 视频, 文档等
-2. Telegram 消息链接, 例如: `https://t.me/acherkrau/1097`. **即使频道禁止了转发和保存, Bot 依然可以下载其文件.**
-3. Telegra.ph 的文章链接, Bot 将下载其中的所有图片
\ No newline at end of file
+- 群组中仅响应显式命令和按钮回调,不会自动保存群内的普通媒体或普通文本消息。
+- 转存群内文件时,回复目标消息并发送 `/save`(群组中亦可使用 `/save@YourBot` 格式);其他命令(如 `/download`)用法相同。
+- 仅白名单用户可以使用。非白名单成员的消息会被忽略,点击内联按钮将提示无权限。
+- 超级群中以匿名管理员身份发送的消息因无法识别具体用户,会被直接忽略。
+- 默认的 Privacy Mode 下,Bot 仅能接收针对它的命令、@提及以及对其消息的回复。若需接收群内全部消息,可在 [@BotFather](https://t.me/BotFather) 使用 `/setprivacy` 关闭 Privacy Mode;Bot 在群组中依然仅响应命令和交互按钮。
\ No newline at end of file
diff --git a/docs/content/zh/usage/api.md b/docs/content/zh/usage/api.md
index 3a9cfb4e..fdda8c46 100644
--- a/docs/content/zh/usage/api.md
+++ b/docs/content/zh/usage/api.md
@@ -5,45 +5,33 @@ weight: 20
# HTTP API
-SaveAny-Bot 提供了一套 HTTP API,允许你通过程序化方式创建下载/转存任务、查询任务状态、取消任务等,无需通过 Telegram 操作。
+SaveAny-Bot 提供 HTTP API,支持通过脚本或第三方程序创建下载/转存任务、查询状态及取消任务。
## 启用 API
-在 `config.toml` 中添加或修改以下配置:
+API 与 Web 控制台共用监听地址和端口,不需要单独启用。Web 控制台默认监听 `127.0.0.1:28080`。在 **设置 → Web** 中配置访问 Token:
-```toml
-[api]
-enable = true
-host = "0.0.0.0" # 监听地址,默认 0.0.0.0
-port = 8080 # 监听端口,默认 8080
-token = "your-token" # 鉴权 Token,强烈建议设置
-```
-
-也可通过环境变量覆盖(前缀 `SAVEANY_`):
+- `访问 Token`:用于 `/?token=...` 直接登录 Web UI,也用于 API 的 `Authorization: Bearer `。
+- `监听地址`:控制台与 API 共用;需要从其他主机访问时使用 `0.0.0.0:28080`。
-| 环境变量 | 对应配置项 |
-|---|---|
-| `SAVEANY_API_ENABLE` | `api.enable` |
-| `SAVEANY_API_HOST` | `api.host` |
-| `SAVEANY_API_PORT` | `api.port` |
-| `SAVEANY_API_TOKEN` | `api.token` |
+所有接口位于 `/api/v1` 下。
-{{< hint warning >}}
-若 `token` 为空,API 服务将**不进行任何鉴权**即可访问,存在安全风险。
-{{< /hint >}}
+## 认证
-## 鉴权
-
-当配置了 `token` 时,所有 API 请求均需在 HTTP 请求头中携带 Bearer Token:
+在 **设置 → Web** 中配置访问 Token,然后在请求头中携带:
```
Authorization: Bearer
```
+未配置访问 Token 时,每次启动都会生成随机 Token,并作为 Web UI 直达地址的一部分打印到日志。该 Token 在当前进程内同样可用于 Bearer 认证。
+
+也可以使用 Web 控制台登录 Cookie。使用 Cookie 的非 GET 请求还需要携带 `X-CSRF-Token`。
+
鉴权失败时返回 `401`:
```json
-{ "error": "unauthorized", "message": "invalid token" }
+{ "error": "unauthorized", "message": "authentication required" }
```
## 错误响应格式
@@ -62,7 +50,6 @@ Authorization: Bearer
| 错误码 | HTTP 状态 | 含义 |
|---|---|---|
| `unauthorized` | 401 | 鉴权失败 |
-| `method_not_allowed` | 405 | HTTP 方法不正确 |
| `invalid_request` | 400 | 请求体/参数非法 |
| `task_creation_failed` | 400 | 任务创建失败 |
| `task_not_found` | 404 | 任务 ID 不存在 |
@@ -80,7 +67,7 @@ Authorization: Bearer
**响应 `200 OK`:**
```json
-{ "status": "ok" }
+{ "status": "ok", "version": "1.0.0" }
```
---
@@ -94,14 +81,44 @@ Authorization: Bearer
```json
{
"storages": [
- { "name": "local", "type": "local" },
- { "name": "MyMinio", "type": "s3" }
+ {
+ "storage_id": 1,
+ "name": "local",
+ "type": "local",
+ "enable": true,
+ "config": { "base_path": "/app/downloads" },
+ "config_version": 1
+ }
]
}
```
---
+### GET /api/v1/meta — API 与能力元数据
+
+返回冻结的 v1 能力词表和数据库 schema 版本。
+
+```json
+{
+ "api_version": "v1",
+ "version": "1.0.0",
+ "schema_version": 1,
+ "chat_id_format": "bot_api",
+ "task_types": ["tgfiles", "tphpics", "parseditem", "directlinks", "aria2", "ytdlp", "transfer"],
+ "task_statuses": ["queued", "running", "completed", "partial", "failed", "cancelled"],
+ "task_item_statuses": ["completed", "failed", "skipped", "cancelled"],
+ "storage_types": ["local", "webdav", "alist", "telegram", "s3", "rclone"],
+ "rule_types": ["FILENAME-REGEX", "MESSAGE-REGEX", "IS-ALBUM"],
+ "filename_strategies": ["default", "message", "template"],
+ "conflict_strategies": ["rename", "ask", "overwrite", "skip"]
+}
+```
+
+聊天 ID 使用 Bot API 形式:用户为正数,普通群为负数,超级群和频道使用 `-100...`。
+
+---
+
### GET /api/v1/task-types — 列出支持的任务类型
**响应 `200 OK`:**
@@ -109,13 +126,8 @@ Authorization: Bearer
```json
{
"types": [
- "directlinks",
- "ytdlp",
- "aria2",
- "parseditem",
- "tgfiles",
- "tphpics",
- "transfer"
+ { "type": "directlinks", "label": "directlinks" },
+ { "type": "tgfiles", "label": "tgfiles" }
]
}
```
@@ -124,12 +136,12 @@ Authorization: Bearer
### GET /api/v1/media-metadata — 媒体元数据查询
-对 Telegram 消息链接、直链音视频地址或其他 yt-dlp 支持的媒体链接进行探测,在不完整下载文件的情况下返回标题、缩略图、上传者和音视频时长等可用元数据。
+探测 Telegram 消息链接、直链音视频或 yt-dlp 支持的媒体链接,无需完整下载即可提取标题、缩略图、上传者和时长等元数据。
请求:
```bash
-curl -X GET "http://localhost:8080/api/v1/media-metadata?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxxx" \
+curl -X GET "http://127.0.0.1:28080/api/v1/media-metadata?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dxxx" \
-H "Authorization: Bearer YOUR_TOKEN"
```
@@ -163,7 +175,7 @@ Authorization: Bearer
```json
{
"type": "<任务类型>",
- "storage": "<存储名>",
+ "storage_id": 1,
"path": "<子目录>",
"webhook": "<回调URL>",
"params": { }
@@ -173,7 +185,7 @@ Authorization: Bearer
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `type` | string | 是 | 任务类型,见下文 |
-| `storage` | string | 是 | 目标存储名,须与配置中的存储名一致 |
+| `storage_id` | uint | 是 | 目标存储 ID |
| `path` | string | 否 | 存储内的子目录路径 |
| `webhook` | string | 否 | 任务完成/失败时的回调地址 |
| `params` | object | 是 | 各任务类型的专属参数,见下文 |
@@ -182,9 +194,11 @@ Authorization: Bearer
```json
{
- "task_id": "abc123xyz",
- "type": "directlinks",
- "status": "queued",
+ "task_id": "abc123xyz",
+ "type": "directlinks",
+ "status": "queued",
+ "storage_id": 1,
+ "storage_name": "local",
"created_at": "2026-03-11T10:00:00Z"
}
```
@@ -198,7 +212,7 @@ Authorization: Bearer
```json
{
"type": "directlinks",
- "storage": "local",
+ "storage_id": 1,
"path": "downloads",
"params": {
"urls": [
@@ -219,12 +233,12 @@ Authorization: Bearer
需要在系统中安装 yt-dlp。
{{< /hint >}}
-通过 yt-dlp 下载视频/音频,支持 YouTube、Bilibili 等 1000+ 网站。
+通过 yt-dlp 下载音视频,支持主流视频网站。
```json
{
"type": "ytdlp",
- "storage": "local",
+ "storage_id": 1,
"path": "videos",
"params": {
"urls": ["https://www.youtube.com/watch?v=xxx"],
@@ -241,7 +255,7 @@ Authorization: Bearer
##### aria2 — Aria2 下载
{{< hint warning >}}
-需要在配置文件中启用并配置 Aria2 RPC。
+需要在 **设置 → Aria2** 中启用并配置 Aria2 RPC。
{{< /hint >}}
通过 Aria2 下载管理器下载文件,支持 HTTP/HTTPS、FTP、BitTorrent(磁力链接、种子)等协议。
@@ -249,7 +263,7 @@ Authorization: Bearer
```json
{
"type": "aria2",
- "storage": "local",
+ "storage_id": 1,
"path": "downloads",
"params": {
"urls": ["magnet:?xt=urn:btih:..."],
@@ -270,7 +284,7 @@ Authorization: Bearer
```json
{
"type": "parseditem",
- "storage": "local",
+ "storage_id": 1,
"path": "parsed",
"params": {
"url": "https://some-site.com/page"
@@ -298,7 +312,7 @@ Authorization: Bearer
```json
{
"type": "tgfiles",
- "storage": "local",
+ "storage_id": 1,
"path": "telegram",
"params": {
"message_links": [
@@ -311,7 +325,10 @@ Authorization: Bearer
| params 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
-| `message_links` | []string | 是 | Telegram 消息链接列表,至少 1 条 |
+| `message_links` | []string | 否 | Telegram 消息链接列表;与 `chat_id` + `message_range` 二选一 |
+| `chat_id` | string | 否 | 与消息范围配合使用的 Bot API 聊天 ID 或用户名 |
+| `message_range` | string | 否 | 消息范围,例如 `100-120` |
+| `filter` | string | 否 | 可选正则,匹配消息文本或文件名 |
##### tphpics — Telegraph 文章图片下载
@@ -322,7 +339,7 @@ Authorization: Bearer
```json
{
"type": "tphpics",
- "storage": "local",
+ "storage_id": 1,
"path": "telegraph",
"params": {
"telegraph_url": "https://telegra.ph/Some-Article-01-01"
@@ -339,34 +356,34 @@ Authorization: Bearer
在两个存储后端之间直接传输文件,无需经过 Telegram。源存储须支持列举(list)和读取(read)操作。
{{< hint info >}}
-`transfer` 任务中,顶层的 `storage` 字段仍然必须填写(用于通过参数校验),但实际使用的存储由 `params` 中的 `source_storage` 和 `target_storage` 决定。
+`transfer` 任务中,顶层的 `storage_id` 字段仍然必须填写(用于通过参数校验),但实际使用的存储由 `params` 中的 `source_storage_id` 和 `target_storage_id` 决定。
{{< /hint >}}
```json
{
"type": "transfer",
- "storage": "local",
+ "storage_id": 1,
"params": {
- "source_storage": "MyS3",
- "source_path": "backups/",
- "target_storage": "LocalDisk",
- "target_path": "restored/"
+ "source_storage_id": 2,
+ "source_path": "backups/",
+ "target_storage_id": 1,
+ "target_path": "restored/"
}
}
```
| params 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
-| `source_storage` | string | 是 | 源存储名 |
+| `source_storage_id` | uint | 是 | 源存储 ID |
| `source_path` | string | 是 | 源存储中的路径,须包含至少一个文件 |
-| `target_storage` | string | 是 | 目标存储名 |
+| `target_storage_id` | uint | 是 | 目标存储 ID |
| `target_path` | string | 是 | 目标存储中的路径 |
---
### GET /api/v1/tasks — 列出所有任务
-返回所有 API 创建的任务,按创建时间由新到旧排序(仅在内存中保留,重启后清空)。已进入终态的任务在最后一次更新 24 小时后也会被清理,清理任务每 10 分钟执行一次。
+返回所有任务,按创建时间由新到旧排序。任务记录和创建参数持久化在 SQLite 中;排队中和中断的任务会在重启后恢复。终态记录保留到通过 API 删除。
**响应 `200 OK`:**
@@ -378,7 +395,8 @@ Authorization: Bearer
"type": "directlinks",
"status": "running",
"title": "file.zip",
- "storage": "local",
+ "storage_id": 1,
+ "storage_name": "local",
"path": "downloads",
"error": "",
"created_at": "2026-03-11T10:00:00Z",
@@ -411,25 +429,113 @@ Authorization: Bearer
- `400 invalid_request` — 路径中未提供 task_id
- `404 task_not_found` — 任务不存在
+任务对象在至少一个文件保存了消息元数据时会返回 `metadata_available`。
+
---
-### DELETE /api/v1/tasks/{task_id} — 取消任务
+### GET /api/v1/tasks/{task_id}/metadata — 查询消息元数据
-**路径参数:** `task_id`
+返回 Telegram 文件任务中每个已保存文件的消息元数据。需要先在 **设置 → 消息元数据** 中启用;元数据始终保存在本地数据库。启用“写入旁车 JSON”时还会在实际文件旁写入 `<文件路径>.meta.json`,Telegram 存储会跳过旁车。
+
+**路径参数:** `task_id` — 要查询的任务。
**响应 `200 OK`:**
```json
-{ "message": "task cancelled successfully" }
+{
+ "task_id": "abc123xyz",
+ "items": [
+ {
+ "index": 1,
+ "name": "saved-video.mp4",
+ "path": "downloads/saved-video.mp4",
+ "metadata": {
+ "version": 1,
+ "message": {
+ "id": 123,
+ "chat": { "id": -1001234567890, "title": "Channel" },
+ "sender": { "id": 123456789, "name": "Alice" },
+ "text": "caption",
+ "grouped_id": 999
+ },
+ "file": {
+ "name": "saved-video.mp4",
+ "media_type": "document",
+ "mime_type": "video/mp4",
+ "size": 12345678,
+ "width": 1920,
+ "height": 1080,
+ "duration": 123.4
+ }
+ }
+ }
+ ]
+}
```
+关闭“保存消息文本”时不会保存消息正文。不会保存 access hash、Token 或原始 Telegram 对象。
+
**错误响应:**
- `400 invalid_request` — 路径中未提供 task_id
- `404 task_not_found` — 任务不存在
+- `404 metadata_not_found` — 该任务没有已记录的元数据
+
+---
+
+### POST /api/v1/tasks/{task_id}/retry — 重试失败文件
+
+使用已完成任务中失败的文件创建新任务。Bot 会重新拉取原始消息,同时刷新文件引用。仅支持 Telegram 文件任务(`tgfiles`)。
+
+**路径参数:** `task_id` — 要重试的任务。
+
+**响应 `202 Accepted`:**
+
+```json
+{ "task_id": "d4f7u1crvimcs4n4j8e1", "status": "queued", "file_count": 2 }
+```
+
+**错误响应:**
+- `409 task_active` — 任务仍在排队或运行中
+- `409 nothing_to_retry` — 没有失败或未完成的文件
+- `409 storage_missing` / `409 source_unavailable`
+- `400 retry_unsupported` — 该任务类型不支持重试
+- `503 client_unavailable` — 没有运行中的 Telegram 客户端
+
+任务响应包含单文件结果:列表响应返回失败总数(`failed_count`)以及每个任务最多 5 个失败项(`failed_items`),单任务查询返回全部记录(`items`)。
+
+---
+
+### POST /api/v1/tasks/{task_id}/cancel — 取消任务
+
+取消排队中或运行中的任务。
+
+**路径参数:** `task_id` — 要取消的任务。
+
+**响应 `202 Accepted`:**
+
+```json
+{ "task_id": "abc123xyz", "status": "cancelled" }
+```
+
+**错误响应:**
+- `409 task_not_active` — 任务已经结束
+- `404 task_not_found` — 任务不存在
- `500 cancel_failed` — 取消操作失败
---
+### DELETE /api/v1/tasks/{task_id} — 删除任务
+
+删除已结束的任务及其记录。取消排队中或运行中的任务请使用 `POST /api/v1/tasks/{task_id}/cancel`。
+
+**路径参数:** `task_id`
+
+**响应 `204 No Content`:**
+- `409 task_active` — 请先取消任务
+- `404 task_not_found` — 任务不存在
+
+---
+
## 任务状态
| 状态值 | 含义 |
@@ -437,17 +543,20 @@ Authorization: Bearer
| `queued` | 已入队,等待执行 |
| `running` | 正在执行 |
| `completed` | 已成功完成 |
+| `partial` | 已完成,但部分文件失败 |
| `failed` | 执行失败 |
-| `cancelled` | 已通过 DELETE 接口取消 |
+| `cancelled` | 已通过取消接口取消 |
+
+排队中的任务和进程停止时正在运行的任务都会保存创建参数,重启后自动重新入队。Telegram 下载会从部分缓存继续,已完成上传的文件会被跳过。
---
## Webhook 回调
-创建任务时可设置 `webhook` 字段。当任务进入 `completed` 或 `failed` 状态时,Bot 会向该地址发送一个 `POST` 请求。
+创建任务时可设置 `webhook` 字段。当任务进入 `completed`、`partial` 或 `failed` 状态时,Bot 会向该地址发送一个 `POST` 请求。
{{< hint info >}}
-被取消的任务**不会**触发 Webhook——取消操作的发起方已经知晓结果。每个任务最多只会发送一次回调。
+已取消的任务不会触发 Webhook,每个任务最多发送一次回调。
{{< /hint >}}
回调为异步发送,不会因为回调地址响应缓慢或不可达而阻塞任务。
@@ -466,13 +575,14 @@ User-Agent: SaveAny-Bot/1.0
"task_id": "abc123xyz",
"type": "directlinks",
"status": "completed",
- "storage": "local",
+ "storage_id": 1,
+ "storage_name": "local",
"path": "downloads",
"completed_at": "2026-03-11T10:01:00Z",
"error": ""
}
```
-`completed_at` 仅在状态为 `completed` 或 `failed` 时出现。`error` 仅在有错误时出现。
+`completed_at` 仅在状态为 `completed`、`partial` 或 `failed` 时出现。`error` 仅在有错误时出现。
**重试机制:** 最多尝试 3 次。响应非 2xx 或请求出错时会重试,间隔依次为 100 毫秒、400 毫秒。每次请求超时为 30 秒。
diff --git a/docs/content/zh/usage/aria2.md b/docs/content/zh/usage/aria2.md
index d4cd9e58..33723360 100644
--- a/docs/content/zh/usage/aria2.md
+++ b/docs/content/zh/usage/aria2.md
@@ -6,36 +6,32 @@ weight: 6
# Aria2 下载
{{< hint warning >}}
-该功能需要在配置文件中启用 Aria2 并配置 RPC 连接.
+需要在 **设置 → Aria2** 中启用 Aria2 并配置 RPC 连接。
{{< /hint >}}
-使用 `/aria2dl` 命令可以通过 Aria2 下载管理器下载文件, 支持 HTTP/HTTPS、FTP、BitTorrent 等多种协议.
+可通过 `/download` 使用 Aria2 下载,支持 HTTP/HTTPS、FTP、BitTorrent 种子与磁力链接。发送链接后选择 **Aria2** 即可。
```bash
-/aria2dl [uri2] [uri3] ...
+/download <链接1> [链接2] [链接3] ...
```
-示例:
+示例:
```bash
# 下载 HTTP 链接
-/aria2dl https://example.com/file.zip
+/download https://example.com/file.zip
# 下载磁力链接
-/aria2dl magnet:?xt=urn:btih:...
+/download magnet:?xt=urn:btih:...
-# 下载种子文件 (需要先上传 .torrent 文件)
-/aria2dl https://example.com/file.torrent
+# 下载种子文件
+/download https://example.com/file.torrent
```
-配置 Aria2:
+选择 **Aria2** 后指定目标存储位置。若链接仅 Aria2 支持,将直接进入 Aria2 下载流程。
-在 `config.toml` 中添加:
+在 Web 控制台的 **设置 → Aria2** 中配置:
-```toml
-[aria2]
-enable = true
-url = "http://localhost:6800/jsonrpc"
-secret = "your-rpc-secret" # 如果配置了 rpc-secret
-remove_after_transfer = true # 转存完成后删除本地文件
-```
+- `URL`: Aria2 RPC 地址,例如 `http://localhost:6800/jsonrpc`
+- `Secret`: 配置了 `rpc-secret` 时填写
+- `保留文件`: 转存完成后保留 Aria2 下载的本地文件
diff --git a/docs/content/zh/usage/cli.md b/docs/content/zh/usage/cli.md
index 05b925d2..2b4a5361 100644
--- a/docs/content/zh/usage/cli.md
+++ b/docs/content/zh/usage/cli.md
@@ -5,9 +5,9 @@ weight: 21
# 命令行子命令
-除了直接运行 `./saveany-bot` (不带子命令) 启动 Telegram Bot 外, 这个二进制文件还提供两个把本地文件上传到存储后端的辅助子命令: `upload` (一次性) 和 `watch` (持续监听).
+除直接运行 `./saveany-bot` 启动 Telegram Bot 外,二进制文件还提供两个辅助子命令用于上传本地文件:`upload`(一次性上传)和 `watch`(持续监听目录并上传)。
-这些子命令会读取与 Bot 相同的 `config.toml`, 初始化数据库和缓存, 然后执行任务. 它们**不会**启动 Telegram Bot 本身, 但 `telegram` 类型的存储会在需要上传时临时启动 Bot 客户端来执行上传.
+子命令读取相同的数据库配置与缓存,但不会启动 Bot 服务。当目标存储为 `telegram` 时,会在上传期间临时启动 Bot 客户端。
## `upload` — 上传单个文件
@@ -20,7 +20,7 @@ saveany-bot upload -f <文件> -s <存储名> [-d <目录>] [--no-progress]
| 参数 | 必填 | 说明 |
|---|---|---|
| `-f, --file` | 是 | 待上传的本地文件路径 |
-| `-s, --storage` | 是 | 目标存储名 (必须存在于 `config.toml`) |
+| `-s, --storage` | 是 | 目标存储名 (必须在 Web 控制台的存储页面中存在) |
| `-d, --dir` | 否 | 存储中的目标目录, 默认使用存储的 `base_path` |
| `--no-progress` | 否 | 关闭终端进度条 |
@@ -82,9 +82,9 @@ saveany-bot watch -p <路径> -s <存储名> [-d <目录>] [选项]
- 相对子目录结构会被保留: 以 `--path ./inbox` 为例, 写入 `./inbox/sub/file.txt` 的文件会被上传到 `<目标目录>/sub/file.txt`.
- `watch` 会一直运行直到被中断 (如 `Ctrl-C` / `SIGINT`), 退出前会等待所有进行中的上传完成.
-- 重试次数遵循 `config.toml` 中的全局 `retry` 值, 各次重试之间间隔 `--retry-delay`.
+- 重试次数遵循 设置中的全局 `retry` 值, 各次重试之间间隔 `--retry-delay`.
- `telegram` 类型的存储会自动启动 Bot 客户端来执行上传.
{{< hint warning >}}
-`watch` 子命令与 Bot 内的 `/watch` 命令 (监听 Telegram 聊天) 无关. 本子命令监听的是**本地文件系统目录**, 不依赖 Telegram.
+`watch` 子命令用于监听本地文件目录,与 Telegram 聊天的 `/watch` 命令无关。
{{< /hint >}}
\ No newline at end of file
diff --git a/docs/content/zh/usage/config.md b/docs/content/zh/usage/config.md
index de8996b5..62dde828 100644
--- a/docs/content/zh/usage/config.md
+++ b/docs/content/zh/usage/config.md
@@ -5,47 +5,50 @@ weight: 11
# 文件命名与重名策略
-SaveAny-Bot 支持在 Telegram 中通过 `/config` 和 `/fnametmpl` 命令自定义保存文件的命名方式, 以及处理与已存在文件重名时的冲突策略.
+可通过 `/config` 和 `/template` 自定义文件命名方式与重名冲突策略。
## `/config` — 用户配置
-`/config` 命令会弹出一个内联菜单, 你可以在其中修改以下两项用户级设置:
+`/config` 命令包含以下设置:
-- **文件名策略** — 保存文件的命名方式
-- **重名文件保存策略** — 目标存储中已存在同名文件时的处理方式
+- **文件名策略**:保存文件的命名方式
+- **重名文件保存策略**:目标存储中已存在同名文件时的处理方式
+- **静默模式**:直接保存到默认位置,不再每次询问
+- **默认存储**:选择静默模式和无规则匹配时使用的默认存储及目录
+- **文件名模板**:查看当前模板与 `/template` 用法
-设置按用户分别保存, 对该用户后续所有的保存/转存任务生效.
+设置按用户分别保存,对该用户后续所有的保存与转存任务生效。
### 文件名策略
| 选项 | 行为 |
|---|---|
-| `默认` | 使用媒体原始文件名, 没有原始文件名时使用生成名 |
-| `优先从消息生成` | 优先根据消息内容 (如 caption、文本) 生成文件名, 而非原始文件名 |
-| `自定义模板` | 使用 `/fnametmpl` 设置的自定义模板渲染文件名 |
+| `默认` | 使用媒体原始文件名,无原始文件名时使用生成名称 |
+| `优先从消息生成` | 优先根据消息内容(如 caption、文本)生成文件名,而非原始文件名 |
+| `自定义模板` | 使用 `/template` 设置的自定义模板渲染文件名 |
### 重名文件保存策略
| 选项 | 行为 |
|---|---|
-| `始终重命名` (默认) | 保留已有文件, 将新文件以另一个名字保存 |
-| `每次询问` | 每次遇到重名时通过内联按钮提示你选择 |
+| `始终重命名` (默认) | 保留已有文件,新文件自动追加后缀保存 |
+| `每次询问` | 每次遇到重名时通过内联按钮提示选择 |
| `始终覆盖` | 用新文件替换已有文件 |
-| `始终跳过` | 对重名文件不做处理 |
+| `始终跳过` | 跳过不保存 |
{{< hint info >}}
重名策略仅在能够检测文件是否已存在的存储后端生效. 不支持检测文件是否存在的存储后端会退化为覆盖行为.
{{< /hint >}}
-## `/fnametmpl` — 自定义文件名模板
+## `/template` — 自定义文件名模板
-当文件名策略设置为 `自定义模板` 时, SaveAny-Bot 会用 `/fnametmpl` 配置的模板来渲染所保存文件的文件名.
+当文件名策略设置为 `自定义模板` 时, SaveAny-Bot 会用 `/template` 配置的模板来渲染所保存文件的文件名.
```
-/fnametmpl [模板]
+/template [模板]
```
-- 不带参数运行 `/fnametmpl` 会显示当前模板以及帮助说明
+- 不带参数运行 `/template` 会显示当前模板以及帮助说明
- 带模板字符串运行则会把它设为你的文件名模板
模板使用 Go [`text/template`](https://pkg.go.dev/text/template) 语法. 可用变量如下:
@@ -64,12 +67,16 @@ SaveAny-Bot 支持在 Telegram 中通过 `/config` 和 `/fnametmpl` 命令自定
```
# 固定前缀 + 消息 ID + 日期
-/fnametmpl 图片_{{.msgid}}_{{.msgdate}}.jpg
+/template 图片_{{.msgid}}_{{.msgdate}}.jpg
# 优先使用原始文件名, 没有则用生成名
-/fnametmpl {{.origname}}
+/template {{.origname}}
```
+{{< hint info >}}
+当媒体组按批次保存时 (发送给 Bot 的媒体组, 以及命中 `NEW-FOR-ALBUM` 规则的监听媒体组), 没有自身文本的文件会使用媒体组的文本填充模板变量和消息规则, 因此每个文件都能取得 `{{.msgtags}}` 与 `{{.msgraw}}`. 若模板对多个文件产生相同名称, 后续文件会保留自动生成的名称.
+{{< /hint >}}
+
{{< hint warning >}}
模板仅在文件名策略设置为 `自定义模板` 时生效. 如果模板解析失败, SaveAny-Bot 会回退到默认的文件名生成逻辑.
{{< /hint >}}
\ No newline at end of file
diff --git a/docs/content/zh/usage/directlinks.md b/docs/content/zh/usage/directlinks.md
index dd41f682..532b0ab7 100644
--- a/docs/content/zh/usage/directlinks.md
+++ b/docs/content/zh/usage/directlinks.md
@@ -1,21 +1,25 @@
---
-title: "直接下载链接"
+title: "链接下载"
weight: 5
---
-# 直接下载链接
+# 链接下载
-使用 `/dl` 命令可以直接下载一个或多个 HTTP/HTTPS 链接的文件到存储中.
+使用 `/download` 命令发送一个或多个链接,并选择下载方式:
+
+- **直接下载** — 普通 HTTP/HTTPS 文件链接
+- **Aria2** — 启用 Aria2 后可用,也支持磁力和种子链接
+- **视频** — 安装 yt-dlp 后可用,用于支持的视频网站
```bash
-/dl [url2] [url3] ...
+/download <链接1> [链接2] [链接3] ...
```
-示例:
+示例:
```bash
-/dl https://example.com/file.zip
-/dl https://example.com/file1.zip https://example.com/file2.zip
+/download https://example.com/file.zip
+/download https://example.com/file1.zip https://example.com/file2.zip
```
-Bot 会验证链接格式, 然后让你选择目标存储位置.
+选择下载方式后,再选择目标存储位置。视频链接可以在同一命令后追加 yt-dlp 参数,例如 `-f best` 或 `--extract-audio`。
diff --git a/docs/content/zh/usage/parsers.md b/docs/content/zh/usage/parsers.md
index a44d65b1..3a1a27f3 100644
--- a/docs/content/zh/usage/parsers.md
+++ b/docs/content/zh/usage/parsers.md
@@ -5,11 +5,38 @@ weight: 9
# 转存 Telegram 之外的文件
-除了 Telegram 上的文件, Bot 还可通过 JavaScript 插件或内置解析器来支持转存其他网站的文件.
+除 Telegram 文件外,Bot 还可通过内置解析器或 JavaScript 插件转存第三方网站内容。
-> 查看[贡献解析器](../contribute)文档了解详情
+> 编写与贡献解析器请参阅[相关文档](../contribute)。
-只需向 Bot 发送符合解析器要求的链接即可使用, 当前内置的解析器:
+向 Bot 发送符合解析器要求的链接。内置解析器:
- Twitter
- Kemono
+
+## JavaScript 插件 API
+
+JavaScript 插件通过以下结构注册解析器:
+
+```js
+registerParser({
+ metadata: {
+ name: 'example',
+ version: '1.0.0',
+ apiVersion: '1',
+ description: '示例插件',
+ author: 'you',
+ },
+ canHandle(url) { return url.includes('example.com') },
+ parse(url) {
+ return {
+ site: 'example',
+ url,
+ title: 'Example',
+ resources: [{ url: 'https://cdn.example.com/file.zip', filename: 'file.zip' }],
+ }
+ },
+})
+```
+
+`apiVersion` 是插件 ABI 版本,与插件自身的 `version` 独立。当前 ABI 为 `"1"`,其他值会被拒绝。可用全局对象为 `console`、`ghttp`(`get`、`getJSON`、`head`)和 `playwright`。`parse` 返回 `Item`,其中 `resources` 支持 `url`、`filename`,以及可选的 `mime_type`、`extension`、`size`、`hash`、`headers`、`extra`。
diff --git a/docs/content/zh/usage/rules.md b/docs/content/zh/usage/rules.md
index 4ce6ace4..c5862134 100644
--- a/docs/content/zh/usage/rules.md
+++ b/docs/content/zh/usage/rules.md
@@ -5,51 +5,54 @@ weight: 3
# 存储规则
-允许你为 Bot 在上传文件到存储时设置一些重定向规则, 用于自动整理所保存的文件.
+通过设置重定向规则,自动按文件名、消息内容或相册将保存的文件分类归档。
见: #28
+发送 `/rules` 可查看现有规则、开关规则匹配或删除规则。添加规则与导入预设命令如下:
+
目前支持的规则类型:
1. FILENAME-REGEX
2. MESSAGE-REGEX
3. IS-ALBUM
-添加规则的基本语法:
+添加规则语法:
-"规则类型 规则内容 存储名 路径"
+`/rules add <规则类型> <匹配内容> <存储ID|存储名> <路径>`
-注意空格的使用, 语法正确 bot 才能解析, 以下是一条合法的添加规则命令:
+存储参数为纯数字时按存储 ID 解析(可在 Web 控制台的存储页面查看),否则按存储名解析。存储名包含空格时请使用 ID 或为名称加双引号。示例:
```
-/rule add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /视频
+/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ 3 /视频
+/rules add FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ "我的 Alist" /视频
```
-此外, 规则中的存储名若使用 "CHOSEN" , 则表示存储到点击按钮选择的存储端的路径下
+若存储填为 `CHOSEN`,则表示保存到所选存储端的指定路径下。
-你也可以使用 `/rule switch` 来开关规则模式. 关闭规则模式时, 所有文件都将保存到默认存储.
+可通过 `/rules switch` 开关规则模式。关闭时所有文件直接存入默认存储。
## 预设规则
-为常见文件类型手动编写正则规则比较繁琐, 因此 Bot 内置了一组预设分类 (视频、图片、音频、文档、压缩包), 可以通过一条命令批量导入:
+Bot 内置了常见文件类型(视频、图片、音频、文档、压缩包)的分类规则,可一键导入:
```
-/rule preset <存储名> [基础路径]
+/rules preset <存储ID|存储名> [基础路径]
```
参数:
-- `存储名`: 目标存储名 (必须存在且你有权访问)
+- `存储`: 目标存储 ID 或名称 (必须存在且你有权访问)
- `基础路径`: 可选. 各预设分类的子目录会创建在此路径下; 若不填则直接使用默认分类目录名
示例:
```
# 导入预设规则到 "MyAlist", 使用默认目录布局
-/rule preset MyAlist
+/rules preset MyAlist
# 在自定义基础路径 "downloads/sorted" 下导入预设规则
-/rule preset MyAlist downloads/sorted
+/rules preset MyAlist downloads/sorted
```
此命令会为每个分类创建 `FILENAME-REGEX` 规则, 将匹配的文件路由到 `基础路径` 下对应的子目录:
@@ -63,7 +66,7 @@ weight: 3
| 压缩包 | zip, rar, 7z, tar, gz, bz2, xz, ... | `压缩包` |
{{< hint info >}}
-导入后的预设规则就是普通的 `FILENAME-REGEX` 规则. 你可以像其他规则一样通过 `/rule` 查看或用 `/rule del ` 单独删除/编辑它们.
+导入后的预设规则就是普通的 `FILENAME-REGEX` 规则. 你可以像其他规则一样通过 `/rules` 查看或单独删除它们.
{{< /hint >}}
规则类型:
@@ -76,17 +79,17 @@ weight: 3
FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /视频
```
-表示将文件名后缀为 mp4,mkv,ts,avi,flv 的文件放到名为 MyAlist 存储下的 /视频 目录内 (同时受配置文件中的 `base_path` 影响)
+表示将文件名后缀为 mp4,mkv,ts,avi,flv 的文件放到名为 MyAlist 存储下的 /视频 目录内 (同时受存储设置中的 `base_path` 影响)
## MESSAGE-REGEX
-同上, 但是是根据消息本身的文本内容正则匹配
+根据消息正文或说明文字(caption)进行正则表达式匹配。
## IS-ALBUM
-匹配相册消息 (media group), 规则内容只能为 `true` 或 `false`.
+匹配相册消息(media group),规则内容为 `true` 或 `false`。
-规则中的路径若使用 "NEW-FOR-ALBUM" , 则表示为该组消息新建一个文件夹来存储它们. 见: https://github.com/krau/SaveAny-Bot/issues/87
+若路径指定为 `NEW-FOR-ALBUM`,则会为整组消息自动新建文件夹并统一保存(参考 issue [#87](https://github.com/krau/SaveAny-Bot/issues/87))。
例如:
@@ -94,4 +97,4 @@ FILENAME-REGEX (?i)\.(mp4|mkv|ts|avi|flv)$ MyAlist /视频
IS-ALBUM true MyWebdav NEW-FOR-ALBUM
```
-这将会把以 media group 形式发送的消息保存到名为 MyWebdav 的存储下, 并为每个相册新建一个文件夹(由第一个文件生成)来存储它们.
+这将把媒体组消息保存到 MyWebdav 存储下,并为每个相册以首个文件的名称新建子目录归档。
diff --git a/docs/content/zh/usage/silent.md b/docs/content/zh/usage/silent.md
index 4361b707..5acdbfe7 100644
--- a/docs/content/zh/usage/silent.md
+++ b/docs/content/zh/usage/silent.md
@@ -3,12 +3,11 @@ title: "静默模式"
weight: 2
---
-# 静默模式 (silent)
+# 静默模式
-使用 `/silent` 命令可以开关静默模式.
+静默模式可在 `/config` 中开启或关闭。
-默认情况下不开启静默模式, Bot 会询问你每个文件的保存位置.
+- **关闭(默认)**:Bot 接收文件时会弹出选择菜单,每次询问保存位置。
+- **开启**:文件将自动保存至预设的默认存储和目录,不再弹出询问提示。
-开启静默模式后, Bot 会直接保存文件到默认位置, 无需确认.
-
-在开启静默模式之前, 需要使用 `/storage` 命令设置默认保存位置.
+> 开启前请确保已在 `/config` 中设置默认存储。
diff --git a/docs/content/zh/usage/transfer.md b/docs/content/zh/usage/transfer.md
index 2041e696..dc7c007a 100644
--- a/docs/content/zh/usage/transfer.md
+++ b/docs/content/zh/usage/transfer.md
@@ -33,18 +33,17 @@ weight: 8
/transfer local1:/pictures "(?i)\.(jpg|png|gif)$"
```
-Bot 会:
+操作流程:
1. 列出源路径下的所有文件
-2. 应用过滤器 (如果提供)
-3. 显示文件数量和总大小
-4. 让你选择目标存储
-5. 让你选择目标目录 (如果该存储配置了目录)
-6. 开始传输任务
-
-注意:
-
-- 源存储必须支持列举和读取功能
-- 目标存储必须支持写入功能
-- 传输过程显示实时进度
-- 支持取消正在进行的传输任务
+2. 应用过滤器(若提供)
+3. 显示文件数量与总大小
+4. 选择目标存储
+5. 选择目标目录(若该存储配置了目录)
+6. 开始执行传输
+
+说明:
+
+- 源存储必须支持列举与读取
+- 目标存储必须支持写入
+- 传输过程展示实时进度,支持随时取消
diff --git a/docs/content/zh/usage/watch.md b/docs/content/zh/usage/watch.md
index e5bc78ba..4408de69 100644
--- a/docs/content/zh/usage/watch.md
+++ b/docs/content/zh/usage/watch.md
@@ -6,43 +6,38 @@ weight: 4
# 监听聊天
{{< hint warning >}}
-该功能需开启 UserBot 集成.
+需要启用 UserBot 集成。
{{< /hint >}}
-监听指定聊天的消息, 并自动保存到默认存储中, 遵从存储规则, 并且可以设置过滤器来只保存匹配的消息.
+可监听指定聊天中的新消息,支持按过滤器筛选并依照存储规则自动保存。
-监听聊天:
+发送 `/watch` 管理监听聊天:
-```
-/watch [filter]
-```
+- **添加监听** — 显示添加聊天的语法
+- **监听列表** — 列出正在监听的聊天,可逐项停止
+- **通知开关** — 开关所有监听聊天的任务通知
+- **同步对话** — 将 UserBot 的对话列表同步到 peer 缓存
-取消监听:
+开始监听:
```
-/unwatch
+/watch <聊天ID/用户名> [过滤器]
```
+停止监听:在 `/watch` 的“监听列表”中点击对应聊天即可停止。
+
过滤器类型:
## msgre
-正则匹配消息文本, 例如:
+按消息文本正则匹配,例如:
```
-/watch 12345678 msgre:.*hello.*
+/watch -1002229835658 msgre:.*hello.*
```
-这将会监听 ID 为 12345678 的聊天, 并且只保存消息文本中包含 "hello" 的消息.
+这会监听 ID 为 `-1002229835658` 的频道,只保存文本包含 `hello` 的消息。用户私聊使用正数 ID,普通群使用负数 ID,超级群和频道使用 `-100...`。
## 任务通知
-所有用户的监听任务通知默认关闭. 开启后, 机器人从被监听聊天中保存的每条消息都会在此处收到通知, 并随下载进度更新, 最终显示保存结果:
-
-```
-/watch notify on
-```
-
-使用 `/watch notify off` 关闭, 使用 `/watch notify` 查询当前状态.
-
-通知由机器人发送到你与它的私聊中. 该开关按用户生效, 而非按聊天: `/watch notify off` 会关闭该用户所有被监听聊天的通知.
+任务通知默认关闭。在 `/watch` 中切换“通知开关”开启后,Bot 会在私聊中实时推送监听到的消息进度与保存结果。此开关按用户生效,作用于该用户的所有监听聊天。再次点击即可关闭。
diff --git a/docs/content/zh/usage/ytdlp.md b/docs/content/zh/usage/ytdlp.md
index cc14abb0..4ac0afb9 100644
--- a/docs/content/zh/usage/ytdlp.md
+++ b/docs/content/zh/usage/ytdlp.md
@@ -1,51 +1,53 @@
---
-title: "yt-dlp 视频下载"
+title: "视频下载 (yt-dlp)"
weight: 7
---
-# yt-dlp 视频下载
+# 视频下载 (yt-dlp)
{{< hint warning >}}
-该功能需要在系统中安装 yt-dlp 命令行工具.
+需要系统安装 yt-dlp 命令行工具。
{{< /hint >}}
-使用 `/ytdlp` 命令可以下载支持的视频网站的视频和音频, 支持 YouTube、Bilibili、Twitter 等 1000+ 个网站.
+视频解析可通过 `/download` 使用。发送支持的视频链接后,选择 **视频(yt-dlp)**。支持 YouTube、Bilibili、Twitter 等主流音视频站点。
```bash
-/ytdlp [url2] [flags...]
+/download <链接1> [链接2] [参数...]
```
-示例:
+示例:
```bash
# 基本下载
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ
# 下载多个视频
-/ytdlp https://www.youtube.com/watch?v=video1 https://www.youtube.com/watch?v=video2
+/download https://www.youtube.com/watch?v=video1 https://www.youtube.com/watch?v=video2
# 使用自定义参数
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ -f best
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ --extract-audio --audio-format mp3
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -f best
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ --extract-audio --audio-format mp3
```
-常用参数:
+常用参数:
-- `-f `: 指定下载格式 (如 `best`, `worst`, `bestvideo+bestaudio`)
-- `-o `: 文件名模板 (如 `%(uploader)s - %(title)s.%(ext)s`)
+- `-f `: 指定下载格式(如 `best`、`worst`、`bestvideo+bestaudio`)
+- `-o `: 文件名模板(如 `%(uploader)s - %(title)s.%(ext)s`)
- `--extract-audio`: 提取音频
-- `--audio-format `: 音频格式 (如 `mp3`, `m4a`, `wav`)
+- `--audio-format `: 音频格式(如 `mp3`、`m4a`、`wav`)
- `--write-sub`: 下载字幕
- `--write-thumbnail`: 下载缩略图
-## 文件名
+出于安全考虑,以下会执行程序、加载外部配置或读写任意文件的参数会被拒绝:`--exec`、`--exec-before-download`、`--config-location`、`--load-info-json`、`--batch-file`、`--downloader`、`--external-downloader`、`--paths`、`--cache-dir`、`--postprocessor-args`、`--netrc-cmd`、`--plugin-dirs`、`--update` 等。
-下载的文件以视频标题命名 (`%(title)s.%(ext)s`), 并保存到你所选择的存储目录中. 可以通过 yt-dlp 的 output template 自定义文件名:
+## 文件命名
+
+下载文件默认按视频标题命名(`%(title)s.%(ext)s`),保存到你选择的存储目录。可以传入 yt-dlp 输出模板自定义文件名:
```bash
-/ytdlp https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
+/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
```
-`-o/--output` 只有模板部分会被使用: 下载目录始终由 bot 管理, 模板中的相对路径会创建在该目录下, 并保留到存储路径中. 如需修改所有下载的默认命名, 请在 [`[ytdlp]` 配置](../deployment/configuration)中设置 `filename_template`; 其中的 `restrict_filenames` 会将文件名限制为 ASCII, 去掉非拉丁字符、空格和 `&`.
+只使用 `-o/--output` 的模板部分:下载目录始终由 Bot 管理,模板中的相对路径会创建在该目录下,并保留到存储路径中。如需修改所有下载的默认命名,请在 [`[ytdlp]` 配置](../deployment/configuration)中设置 `filename_template`;其中的 `restrict_filenames` 会将文件名限制为 ASCII,去掉非拉丁字符、空格和 `&`。
更多参数请参考 [yt-dlp 文档](https://github.com/yt-dlp/yt-dlp#usage-and-options).
diff --git a/docs/http_api.md b/docs/http_api.md
deleted file mode 100644
index f2cbae52..00000000
--- a/docs/http_api.md
+++ /dev/null
@@ -1,612 +0,0 @@
-# SaveAny-Bot HTTP API
-
-This document describes all currently registered HTTP API endpoints in SaveAny-Bot.
-
-## Base URL
-
-- Default: `http://0.0.0.0:8080`
-- Config source: `[api]` section in `config.toml` (`host`, `port`, `enable`, `token`)
-
-## Authentication
-
-When `[api].token` is non-empty, all endpoints require:
-
-```http
-Authorization: Bearer
-```
-
-If token is empty, authentication middleware is not applied.
-
-A missing header, a header that is not `Bearer `, or a wrong token all
-return `401 Unauthorized`:
-
-```json
-{
- "error": "unauthorized",
- "message": "invalid token"
-}
-```
-
-## Common Response Format
-
-### Success
-
-- `Content-Type: application/json`
-- Status code depends on endpoint
-
-### Error
-
-```json
-{
- "error": "error_code",
- "message": "human readable message"
-}
-```
-
-## Task Status Values
-
-- `queued`
-- `running`
-- `completed`
-- `failed`
-- `cancelled`
-
-## Endpoints
-
-## 1) Health Check
-
-- **Path**: `/health`
-- **Method**: `GET`
-- **Description**: Check API service health.
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "status": "ok"
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/health" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 2) Create Task
-
-- **Path**: `/api/v1/tasks`
-- **Method**: `POST`
-- **Description**: Create and enqueue a task.
-
-### Request
-
-- Headers:
- - `Content-Type: application/json`
- - `Authorization: Bearer ` (only if API token is configured)
-
-### Request Body
-
-```json
-{
- "type": "directlinks",
- "storage": "local-main",
- "path": "downloads/movies",
- "webhook": "https://example.com/webhook",
- "params": {}
-}
-```
-
-Field details:
-
-- `type` (string, required):
- - `directlinks`
- - `ytdlp`
- - `aria2`
- - `parseditem`
- - `tgfiles`
- - `tphpics`
- - `transfer`
-- `storage` (string, required): target storage name from configured storages
-- `path` (string, optional): target path on storage
-- `webhook` (string, optional): callback URL for task completion/failure
-- `params` (object, required): type-specific payload
-
-#### `params` by `type`
-
-1. `directlinks`
-
-```json
-{
- "urls": ["https://example.com/file.zip"]
-}
-```
-
-2. `ytdlp`
-
-```json
-{
- "urls": ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
- "flags": ["--format", "bestvideo+bestaudio"]
-}
-```
-
-3. `aria2`
-
-```json
-{
- "urls": ["https://example.com/large-file.iso"],
- "options": {
- "max-connection-per-server": "8"
- }
-}
-```
-
-4. `parseditem`
-
-```json
-{
- "url": "https://example-parser-site.com/post/123"
-}
-```
-
-5. `tgfiles`
-
-```json
-{
- "message_links": [
- "https://t.me/channel_name/100",
- "https://t.me/channel_name/101"
- ]
-}
-```
-
-6. `tphpics`
-
-```json
-{
- "telegraph_url": "https://telegra.ph/example-page-01-01"
-}
-```
-
-7. `transfer`
-
-```json
-{
- "source_storage": "s3-source",
- "source_path": "backup/2026-03-01",
- "target_storage": "local-main",
- "target_path": "restored"
-}
-```
-
-### Response
-
-- `201 Created`
-
-```json
-{
- "task_id": "d4f7u1crvimcs4n4j8d0",
- "type": "directlinks",
- "status": "queued",
- "created_at": "2026-03-10T10:00:00Z"
-}
-```
-
-### Error Examples
-
-- `400 Bad Request` invalid JSON:
-
-```json
-{
- "error": "invalid_request",
- "message": "failed to decode request body: ..."
-}
-```
-
-- `400 Bad Request` unsupported type/storage/params:
-
-```json
-{
- "error": "task_creation_failed",
- "message": "unsupported task type: unknown"
-}
-```
-
-### Example
-
-```bash
-curl -X POST "http://localhost:8080/api/v1/tasks" \
- -H "Authorization: Bearer YOUR_TOKEN" \
- -H "Content-Type: application/json" \
- -d '{
- "type": "directlinks",
- "storage": "local-main",
- "path": "downloads",
- "params": {
- "urls": ["https://example.com/file.zip"]
- }
- }'
-```
-
----
-
-## 3) List Tasks
-
-- **Path**: `/api/v1/tasks`
-- **Method**: `GET`
-- **Description**: List all tracked API tasks, newest first (by creation time).
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "tasks": [
- {
- "task_id": "d4f7u1crvimcs4n4j8d0",
- "type": "directlinks",
- "status": "running",
- "title": "Direct Links Download",
- "progress": {
- "total_bytes": 104857600,
- "downloaded_bytes": 52428800,
- "total_files": 3,
- "downloaded_files": 1,
- "percent": 50,
- "speed_mbps": 12.5
- },
- "storage": "local-main",
- "path": "downloads",
- "created_at": "2026-03-10T10:00:00Z",
- "updated_at": "2026-03-10T10:03:00Z"
- }
- ],
- "total": 1
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/api/v1/tasks" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 4) Get Task By ID
-
-- **Path**: `/api/v1/tasks/{task_id}`
-- **Method**: `GET`
-- **Description**: Retrieve one task's metadata and progress.
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Path parameter:
- - `task_id` (required)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "task_id": "d4f7u1crvimcs4n4j8d0",
- "type": "directlinks",
- "status": "running",
- "title": "Direct Links Download",
- "progress": {
- "total_bytes": 104857600,
- "downloaded_bytes": 52428800,
- "total_files": 3,
- "downloaded_files": 1,
- "percent": 50,
- "speed_mbps": 12.5
- },
- "storage": "local-main",
- "path": "downloads",
- "created_at": "2026-03-10T10:00:00Z",
- "updated_at": "2026-03-10T10:03:00Z"
-}
-```
-
-### Error Examples
-
-- `404 Not Found`
-
-```json
-{
- "error": "task_not_found",
- "message": "task not found: d4f7u1crvimcs4n4j8d0"
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/api/v1/tasks/d4f7u1crvimcs4n4j8d0" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 5) Cancel Task
-
-- **Path**: `/api/v1/tasks/{task_id}`
-- **Method**: `DELETE`
-- **Description**: Cancel a queued/running task.
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Path parameter:
- - `task_id` (required)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "message": "task cancelled successfully"
-}
-```
-
-### Error Examples
-
-- `404 Not Found`
-
-```json
-{
- "error": "task_not_found",
- "message": "task not found: d4f7u1crvimcs4n4j8d0"
-}
-```
-
-- `500 Internal Server Error`
-
-```json
-{
- "error": "cancel_failed",
- "message": "failed to cancel task: ..."
-}
-```
-
-### Example
-
-```bash
-curl -X DELETE "http://localhost:8080/api/v1/tasks/d4f7u1crvimcs4n4j8d0" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 6) List Storages
-
-- **Path**: `/api/v1/storages`
-- **Method**: `GET`
-- **Description**: List loaded storage backends.
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "storages": [
- {
- "name": "local-main",
- "type": "local"
- },
- {
- "name": "my-s3",
- "type": "s3"
- }
- ]
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/api/v1/storages" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 7) List Supported Task Types
-
-- **Path**: `/api/v1/task-types`
-- **Method**: `GET`
-- **Description**: List supported task type values for task creation.
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Body: none
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "types": [
- "directlinks",
- "ytdlp",
- "aria2",
- "parseditem",
- "tgfiles",
- "tphpics",
- "transfer"
- ]
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/api/v1/task-types" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## 8) Get Media Metadata
-
-- **Path**: `/api/v1/media-metadata`
-- **Method**: `GET`
-- **Description**: Inspect a supported media URL and return available metadata such as title, thumbnail, uploader, and audio/video duration.
-
-Supported inputs:
-
-- Telegram message links such as `https://t.me/username/123` and `https://t.me/c/123456789/123`
-- Direct audio/video file URLs that `ffprobe` can inspect
-- Other media page URLs supported by `yt-dlp`
-
-### Request
-
-- Headers:
- - `Authorization: Bearer ` (only if API token is configured)
-- Query parameters:
- - `url` (required): Telegram message link, direct media URL, or media page URL supported by yt-dlp
-
-### Response
-
-- `200 OK`
-
-```json
-{
- "url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
- "title": "Rick Astley - Never Gonna Give You Up (Official Video)",
- "thumbnail": "https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg",
- "uploader": "Rick Astley",
- "duration_seconds": 213.0
-}
-```
-
-`title`, `thumbnail` and `uploader` are omitted when the source does not
-expose them. `duration_seconds` is always present.
-
-### Error Examples
-
-- `400 Bad Request` missing URL:
-
-```json
-{
- "error": "invalid_request",
- "message": "url query parameter is required"
-}
-```
-
-- `400 Bad Request` unsupported or unreadable URL:
-
-```json
-{
- "error": "metadata_extraction_failed",
- "message": "failed to inspect media: ..."
-}
-```
-
-### Example
-
-```bash
-curl -X GET "http://localhost:8080/api/v1/media-metadata?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdQw4w9WgXcQ" \
- -H "Authorization: Bearer YOUR_TOKEN"
-```
-
----
-
-## Webhook Callbacks
-
-When `webhook` is set on a create-task request, a `POST` is sent to that URL
-once the task reaches `completed` or `failed`. Cancelled tasks do not fire a
-callback, and each task fires at most one. Delivery is asynchronous, so a slow
-endpoint never blocks the task.
-
-Headers:
-
-```http
-Content-Type: application/json
-User-Agent: SaveAny-Bot/1.0
-```
-
-Body:
-
-```json
-{
- "task_id": "d4f7u1crvimcs4n4j8d0",
- "type": "directlinks",
- "status": "completed",
- "storage": "local-main",
- "path": "downloads",
- "completed_at": "2026-03-10T10:05:00Z",
- "error": ""
-}
-```
-
-`completed_at` is present only for `completed` and `failed`. `error` is present
-only when non-empty.
-
-Up to 3 attempts are made. A non-2xx response or a transport error retries
-after 100ms, then 400ms. Each request times out after 30 seconds.
-
----
-
-## Task Retention
-
-Task records live in memory only and are lost on restart. A task that has
-reached a terminal state is also evicted 24 hours after its last update; the
-sweep runs every 10 minutes.
-
-## Notes
-
-- Unknown endpoint returns:
-
-```json
-{
- "error": "not_found",
- "message": "endpoint not found: /your/path"
-}
-```
-
-- Wrong method on an endpoint returns:
-
-```json
-{
- "error": "method_not_allowed",
- "message": "method not allowed: PUT"
-}
-```
diff --git a/docs/hugo.toml b/docs/hugo.toml
index 71205a51..4df99846 100644
--- a/docs/hugo.toml
+++ b/docs/hugo.toml
@@ -12,7 +12,7 @@ path = 'github.com/alex-shpak/hugo-book'
[params]
BookTheme = 'auto'
BookToC = true
-BookLogo = 'logo.png'
+BookLogo = 'logo.svg'
BookSection = '*'
BookRepo = 'https://github.com/krau/saveany-bot'
BookCommitPath = 'commit'
diff --git a/docs/static/logo.svg b/docs/static/logo.svg
new file mode 100644
index 00000000..c8de904b
--- /dev/null
+++ b/docs/static/logo.svg
@@ -0,0 +1,24 @@
+
+ Save Any Bot
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/e2e/api_test.go b/e2e/api_test.go
new file mode 100644
index 00000000..db3360f4
--- /dev/null
+++ b/e2e/api_test.go
@@ -0,0 +1,267 @@
+//go:build e2e
+
+package e2e
+
+import (
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func TestHealthAndAuth(t *testing.T) {
+ status, body := (&apiClient{t: t, base: serverURL, http: http.DefaultClient}).request(http.MethodGet, "/health", nil, false)
+ if status != http.StatusOK || !strings.Contains(string(body), `"status":"ok"`) {
+ t.Fatalf("health = %d %s", status, body)
+ }
+
+ anon := &apiClient{t: t, base: serverURL, http: http.DefaultClient}
+ if status, _ := anon.request(http.MethodPost, "/api/v1/auth/login", map[string]string{"password": "wrong"}, false); status != http.StatusUnauthorized {
+ t.Fatalf("wrong password status = %d", status)
+ }
+
+ client := newClient(t)
+ var me struct {
+ Authenticated bool `json:"authenticated"`
+ CSRF string `json:"csrf_token"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/auth/me", nil, false, &me); status != http.StatusOK || !me.Authenticated {
+ t.Fatalf("me = %d %+v", status, me)
+ }
+
+ if status := client.do(http.MethodPost, "/api/v1/auth/logout", nil, true, nil); status != http.StatusNoContent {
+ t.Fatalf("logout status = %d", status)
+ }
+ if status := client.do(http.MethodGet, "/api/v1/auth/me", nil, false, nil); status != http.StatusUnauthorized {
+ t.Fatalf("me after logout = %d", status)
+ }
+}
+
+func TestCSRFAndSettingsRoundTrip(t *testing.T) {
+ client := sharedClient(t)
+
+ var settings map[string]map[string]any
+ if status := client.do(http.MethodGet, "/api/v1/settings", nil, false, &settings); status != http.StatusOK {
+ t.Fatalf("get settings = %d", status)
+ }
+ core := settings["core"]
+ originalLang, _ := core["lang"].(string)
+ nextLang := "en"
+ if originalLang == nextLang {
+ nextLang = "zh-Hans"
+ }
+ defer func() {
+ client.do(http.MethodPut, "/api/v1/settings/core", map[string]any{"lang": originalLang}, true, nil)
+ }()
+
+ if status := client.do(http.MethodPut, "/api/v1/settings/core", map[string]any{"lang": nextLang}, false, nil); status != http.StatusForbidden {
+ t.Fatalf("settings update without CSRF = %d, want 403", status)
+ }
+ if status := client.do(http.MethodPut, "/api/v1/settings/core", map[string]any{"lang": nextLang}, true, nil); status != http.StatusOK {
+ t.Fatalf("settings update with CSRF = %d", status)
+ }
+ if status := client.do(http.MethodGet, "/api/v1/settings", nil, false, &settings); status != http.StatusOK {
+ t.Fatalf("get settings after update = %d", status)
+ }
+ if got, _ := settings["core"]["lang"].(string); got != nextLang {
+ t.Fatalf("lang = %q, want %q", got, nextLang)
+ }
+}
+
+type storageSummary struct {
+ StorageID uint `json:"storage_id"`
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Enable bool `json:"enable"`
+ Config map[string]any `json:"config"`
+}
+
+func storageIDByName(t *testing.T, client *apiClient, name string) uint {
+ t.Helper()
+ var storages struct {
+ Storages []storageSummary `json:"storages"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/storages", nil, false, &storages); status != http.StatusOK {
+ t.Fatalf("list storages = %d", status)
+ }
+ for _, storage := range storages.Storages {
+ if storage.Name != name {
+ continue
+ }
+ if storage.StorageID == 0 {
+ t.Fatalf("storage %q has no storage_id", name)
+ }
+ return storage.StorageID
+ }
+ t.Fatalf("storage %q not found in %+v", name, storages.Storages)
+ return 0
+}
+
+func TestSeededEnvironment(t *testing.T) {
+ client := sharedClient(t)
+
+ var storages struct {
+ Storages []storageSummary `json:"storages"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/storages", nil, false, &storages); status != http.StatusOK {
+ t.Fatalf("list storages = %d", status)
+ }
+ if len(storages.Storages) != 1 || storages.Storages[0].Name != "e2e-local" {
+ t.Fatalf("seeded storages = %+v", storages.Storages)
+ }
+ storageID := storages.Storages[0].StorageID
+
+ var testResult struct {
+ OK bool `json:"ok"`
+ Error string `json:"error"`
+ }
+ if status := client.do(http.MethodPost, "/api/v1/storages/"+strconv.FormatUint(uint64(storageID), 10)+"/test", nil, true, &testResult); status != http.StatusOK || !testResult.OK {
+ t.Fatalf("storage test = %d %+v", status, testResult)
+ }
+
+ var users struct {
+ Users []struct {
+ ChatID int64 `json:"chat_id"`
+ Enabled bool `json:"enabled"`
+ DefaultStorageID uint `json:"default_storage_id"`
+ } `json:"users"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/users", nil, false, &users); status != http.StatusOK {
+ t.Fatalf("list users = %d", status)
+ }
+ if len(users.Users) != 1 || users.Users[0].ChatID != 10001 || !users.Users[0].Enabled || users.Users[0].DefaultStorageID != storageID {
+ t.Fatalf("seeded users = %+v", users.Users)
+ }
+
+ var onboarding struct {
+ Steps struct {
+ BotConfigured bool `json:"bot_configured"`
+ StorageReady bool `json:"storage_ready"`
+ UserReady bool `json:"user_ready"`
+ } `json:"steps"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/onboarding", nil, false, &onboarding); status != http.StatusOK {
+ t.Fatalf("onboarding = %d", status)
+ }
+ if onboarding.Steps.BotConfigured || !onboarding.Steps.StorageReady || !onboarding.Steps.UserReady {
+ t.Fatalf("onboarding steps = %+v", onboarding.Steps)
+ }
+}
+
+func TestStorageLifecycle(t *testing.T) {
+ client := sharedClient(t)
+ basePath := t.TempDir()
+
+ var created struct {
+ StorageID uint `json:"storage_id"`
+ }
+ status := client.do(http.MethodPost, "/api/v1/storages", map[string]any{
+ "name": "e2e-temp", "type": "local", "enable": true, "config": map[string]any{"base_path": basePath},
+ }, true, &created)
+ if status != http.StatusCreated || created.StorageID == 0 {
+ t.Fatalf("create storage = %d %+v", status, created)
+ }
+
+ var testResult struct {
+ OK bool `json:"ok"`
+ }
+ if status := client.do(http.MethodPost, "/api/v1/storages/"+strconv.FormatUint(uint64(created.StorageID), 10)+"/test", nil, true, &testResult); status != http.StatusOK || !testResult.OK {
+ t.Fatalf("test storage = %d %+v", status, testResult)
+ }
+
+ if status := client.do(http.MethodPut, "/api/v1/storages/"+strconv.FormatUint(uint64(created.StorageID), 10), map[string]any{
+ "name": "e2e-temp", "type": "local", "enable": false, "config": map[string]any{"base_path": basePath},
+ }, true, nil); status != http.StatusOK {
+ t.Fatalf("update storage = %d", status)
+ }
+
+ if status := client.do(http.MethodDelete, "/api/v1/storages/"+strconv.FormatUint(uint64(created.StorageID), 10), nil, true, nil); status != http.StatusNoContent {
+ t.Fatalf("delete storage = %d", status)
+ }
+}
+
+func TestUserLifecycle(t *testing.T) {
+ client := sharedClient(t)
+
+ var created struct {
+ ID uint `json:"id"`
+ ChatID int64 `json:"chat_id"`
+ }
+ status := client.do(http.MethodPost, "/api/v1/users", map[string]any{
+ "chat_id": 20002, "enabled": true,
+ }, true, &created)
+ if status != http.StatusCreated || created.ID == 0 {
+ t.Fatalf("create user = %d %+v", status, created)
+ }
+
+ if status := client.do(http.MethodPut, "/api/v1/users/"+strconv.FormatUint(uint64(created.ID), 10), map[string]any{"silent": true}, true, nil); status != http.StatusOK {
+ t.Fatalf("update user = %d", status)
+ }
+
+ if status := client.do(http.MethodDelete, "/api/v1/users/"+strconv.FormatUint(uint64(created.ID), 10), nil, true, nil); status != http.StatusNoContent {
+ t.Fatalf("delete user = %d", status)
+ }
+
+ var users struct {
+ Users []struct {
+ ChatID int64 `json:"chat_id"`
+ } `json:"users"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/users", nil, false, &users); status != http.StatusOK {
+ t.Fatalf("list users = %d", status)
+ }
+ for _, user := range users.Users {
+ if user.ChatID == 20002 {
+ t.Fatal("deleted user still listed")
+ }
+ }
+}
+
+func TestViewEndpoints(t *testing.T) {
+ client := sharedClient(t)
+ paths := []string{
+ "/api/v1/settings",
+ "/api/v1/storages",
+ "/api/v1/users",
+ "/api/v1/telegram/status",
+ "/api/v1/telegram/workers",
+ "/api/v1/tasks",
+ "/api/v1/task-types",
+ "/api/v1/system/info",
+ "/api/v1/onboarding",
+ "/api/v1/filesystem?path=" + dataDir,
+ }
+ for _, path := range paths {
+ if status := client.do(http.MethodGet, path, nil, false, nil); status != http.StatusOK {
+ t.Errorf("GET %s = %d, want 200", path, status)
+ }
+ }
+}
+
+func TestSPAServing(t *testing.T) {
+ index, err := os.ReadFile(filepath.Join(repoRoot, "web", "dist", "index.html"))
+ if err != nil {
+ t.Skip("web UI is not built; run make web")
+ }
+ client := newClient(t)
+ status, body := client.request(http.MethodGet, "/", nil, false)
+ if status != http.StatusOK || !strings.Contains(string(body), `id="app"`) {
+ t.Fatalf("GET / = %d, body length %d", status, len(body))
+ }
+ status, body = client.request(http.MethodGet, "/tasks", nil, false)
+ if status != http.StatusOK || !strings.Contains(string(body), `id="app"`) {
+ t.Fatalf("SPA fallback = %d, body length %d", status, len(body))
+ }
+ if strings.Contains(string(index), "/assets/") {
+ start := strings.Index(string(index), "/assets/")
+ end := strings.Index(string(index[start:]), `"`)
+ if end > 0 {
+ asset := string(index[start : start+end])
+ if status, _ := client.request(http.MethodGet, asset, nil, false); status != http.StatusOK {
+ t.Fatalf("GET %s = %d", asset, status)
+ }
+ }
+ }
+}
diff --git a/e2e/main_test.go b/e2e/main_test.go
new file mode 100644
index 00000000..6c98a1cb
--- /dev/null
+++ b/e2e/main_test.go
@@ -0,0 +1,284 @@
+//go:build e2e
+
+package e2e
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net"
+ "net/http"
+ "net/http/cookiejar"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+var (
+ serverURL string
+ dataDir string
+ repoRoot string
+ testBinary string
+)
+
+func TestMain(m *testing.M) {
+ os.Exit(runTests(m))
+}
+
+func runTests(m *testing.M) int {
+ var err error
+ repoRoot, err = filepath.Abs("..")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "resolve repo root:", err)
+ return 1
+ }
+ binDir, err := os.MkdirTemp("", "saveany-e2e-bin-")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "create binary directory:", err)
+ return 1
+ }
+ defer os.RemoveAll(binDir)
+ dataDir, err = os.MkdirTemp("", "saveany-e2e-data-")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "create data directory:", err)
+ return 1
+ }
+ defer os.RemoveAll(dataDir)
+
+ bin := filepath.Join(binDir, "saveany-bot")
+ if runtime.GOOS == "windows" {
+ bin += ".exe"
+ }
+ testBinary = bin
+ build := exec.Command("go", "build", "-o", bin, ".")
+ build.Dir = repoRoot
+ build.Stdout = os.Stderr
+ build.Stderr = os.Stderr
+ if err := build.Run(); err != nil {
+ fmt.Fprintln(os.Stderr, "build test binary:", err)
+ return 1
+ }
+
+ port := freePort()
+ serverURL = "http://127.0.0.1:" + port
+ logFile, err := os.CreateTemp("", "saveany-e2e-*.log")
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "create log file:", err)
+ return 1
+ }
+ defer func() {
+ name := logFile.Name()
+ if err := logFile.Close(); err != nil {
+ fmt.Fprintln(os.Stderr, "close testenv log:", err)
+ }
+ if os.Getenv("E2E_KEEP_LOG") == "" {
+ if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
+ fmt.Fprintln(os.Stderr, "remove testenv log:", err)
+ }
+ } else {
+ fmt.Fprintln(os.Stderr, "server log:", name)
+ }
+ }()
+
+ cmd := exec.Command(bin,
+ "testenv",
+ "--data-dir", dataDir,
+ "--listen", "127.0.0.1:"+port,
+ "--admin-password", "test-admin",
+ "--storage-name", "e2e-local",
+ "--user-chat-id", "10001",
+ )
+ cmd.Dir = dataDir
+ cmd.Stdout = logFile
+ cmd.Stderr = logFile
+ cmd.Env = append(os.Environ(), "SAVEANY_ADMIN_PASSWORD=test-admin")
+ if err := cmd.Start(); err != nil {
+ fmt.Fprintln(os.Stderr, "start test environment:", err)
+ return 1
+ }
+
+ stopped := make(chan struct{})
+ go func() {
+ if err := cmd.Wait(); err != nil {
+ fmt.Fprintln(os.Stderr, "testenv wait:", err)
+ }
+ close(stopped)
+ }()
+ waitCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+ if err := waitForHealth(waitCtx); err != nil {
+ select {
+ case <-stopped:
+ default:
+ if err := cmd.Process.Kill(); err != nil {
+ fmt.Fprintln(os.Stderr, "kill testenv:", err)
+ }
+ }
+ if err := logFile.Sync(); err != nil {
+ fmt.Fprintln(os.Stderr, "sync testenv log:", err)
+ }
+ data, readErr := os.ReadFile(logFile.Name())
+ if readErr != nil {
+ fmt.Fprintln(os.Stderr, "read testenv log:", readErr)
+ }
+ fmt.Fprintf(os.Stderr, "test environment did not become ready: %v\n%s\n", err, data)
+ return 1
+ }
+
+ code := m.Run()
+ if err := cmd.Process.Signal(os.Interrupt); err != nil {
+ fmt.Fprintln(os.Stderr, "signal testenv:", err)
+ }
+ select {
+ case <-stopped:
+ case <-time.After(20 * time.Second):
+ if err := cmd.Process.Kill(); err != nil {
+ fmt.Fprintln(os.Stderr, "kill testenv:", err)
+ }
+ <-stopped
+ }
+ return code
+}
+
+func freePort() string {
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ panic(err)
+ }
+ defer func() {
+ if err := ln.Close(); err != nil {
+ panic(fmt.Sprintf("close free port listener: %v", err))
+ }
+ }()
+ _, port, err := net.SplitHostPort(ln.Addr().String())
+ if err != nil {
+ panic(err)
+ }
+ return port
+}
+
+func waitForHealth(ctx context.Context) error {
+ return waitForHealthAt(ctx, serverURL)
+}
+
+func waitForHealthAt(ctx context.Context, base string) error {
+ client := &http.Client{Timeout: 2 * time.Second}
+ for {
+ req, _ := http.NewRequestWithContext(ctx, http.MethodGet, base+"/health", nil)
+ resp, err := client.Do(req)
+ if err == nil {
+ if closeErr := resp.Body.Close(); closeErr != nil {
+ fmt.Fprintln(os.Stderr, "close health response:", closeErr)
+ }
+ if resp.StatusCode == http.StatusOK {
+ return nil
+ }
+ }
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-time.After(100 * time.Millisecond):
+ }
+ }
+}
+
+var shared *apiClient
+
+// sharedClient logs in once for tests that only need an authenticated session.
+// The login limiter allows a small burst per address, so creating a fresh
+// session in every test would exhaust it.
+func sharedClient(t *testing.T) *apiClient {
+ t.Helper()
+ if shared == nil {
+ shared = newClient(t)
+ }
+ c := *shared
+ c.t = t
+ return &c
+}
+
+type apiClient struct {
+ t *testing.T
+ http *http.Client
+ csrf string
+ base string
+ authed bool
+}
+
+func newClient(t *testing.T) *apiClient {
+ return newClientAt(t, serverURL)
+}
+
+func newClientAt(t *testing.T, base string) *apiClient {
+ t.Helper()
+ jar, err := cookiejar.New(nil)
+ if err != nil {
+ t.Fatalf("create cookie jar: %v", err)
+ }
+ c := &apiClient{t: t, base: base, http: &http.Client{Jar: jar, Timeout: 10 * time.Second}}
+ status, body := c.request(http.MethodPost, "/api/v1/auth/login", map[string]string{"password": "test-admin"}, false)
+ if status != http.StatusOK {
+ t.Fatalf("login status = %d, body = %s", status, body)
+ }
+ var login struct {
+ CSRF string `json:"csrf_token"`
+ }
+ if err := json.Unmarshal(body, &login); err != nil || login.CSRF == "" {
+ t.Fatalf("invalid login response: %s", body)
+ }
+ c.csrf = login.CSRF
+ c.authed = true
+ return c
+}
+
+func (c *apiClient) request(method, path string, body any, withCSRF bool) (int, []byte) {
+ c.t.Helper()
+ var reader io.Reader
+ if body != nil {
+ raw, err := json.Marshal(body)
+ if err != nil {
+ c.t.Fatalf("marshal request: %v", err)
+ }
+ reader = bytes.NewReader(raw)
+ }
+ req, err := http.NewRequest(method, c.base+path, reader)
+ if err != nil {
+ c.t.Fatalf("create request: %v", err)
+ }
+ if body != nil {
+ req.Header.Set("Content-Type", "application/json")
+ }
+ if withCSRF {
+ req.Header.Set("X-CSRF-Token", c.csrf)
+ }
+ resp, err := c.http.Do(req)
+ if err != nil {
+ c.t.Fatalf("%s %s: %v", method, path, err)
+ }
+ defer func() {
+ if err := resp.Body.Close(); err != nil {
+ c.t.Errorf("close response body: %v", err)
+ }
+ }()
+ data, err := io.ReadAll(resp.Body)
+ if err != nil {
+ c.t.Fatalf("read %s %s response: %v", method, path, err)
+ }
+ return resp.StatusCode, data
+}
+
+func (c *apiClient) do(method, path string, body any, withCSRF bool, out any) int {
+ c.t.Helper()
+ status, data := c.request(method, path, body, withCSRF)
+ if out != nil && len(data) > 0 {
+ if err := json.Unmarshal(data, out); err != nil {
+ c.t.Fatalf("%s %s returned invalid JSON: %v\n%s", method, path, err, data)
+ }
+ }
+ return status
+}
diff --git a/e2e/resume_process_test.go b/e2e/resume_process_test.go
new file mode 100644
index 00000000..3d9d03ad
--- /dev/null
+++ b/e2e/resume_process_test.go
@@ -0,0 +1,190 @@
+//go:build e2e
+
+package e2e
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "syscall"
+ "testing"
+ "time"
+)
+
+func TestGracefulRestartResumesTask(t *testing.T) {
+ restartResume(t, os.Interrupt)
+}
+
+func TestSIGTERMRestartResumesTask(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("SIGTERM process test is POSIX-only")
+ }
+ restartResume(t, syscall.SIGTERM)
+}
+
+// restartResume kills a real testenv process and starts a new process against
+// the same data directory.
+func restartResume(t *testing.T, sig os.Signal) {
+ dir := t.TempDir()
+ port := freePort()
+ base := "http://127.0.0.1:" + port
+
+ content := bytes.Repeat([]byte("restart-resume\n"), (16*1024*1024)/15+1)[:16*1024*1024]
+ source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/slow.bin" {
+ http.NotFound(w, r)
+ return
+ }
+ w.Header().Set("Content-Length", fmt.Sprintf("%d", len(content)))
+ for offset := 0; offset < len(content); offset += 256 * 1024 {
+ end := offset + 256*1024
+ if end > len(content) {
+ end = len(content)
+ }
+ if _, err := w.Write(content[offset:end]); err != nil {
+ return
+ }
+ w.(http.Flusher).Flush()
+ time.Sleep(20 * time.Millisecond)
+ }
+ }))
+ defer source.Close()
+
+ env := startTestEnv(t, dir, port)
+ client := newClientAt(t, base)
+ storageID := storageIDByName(t, client, "test-local")
+ var created struct {
+ TaskID string `json:"task_id"`
+ }
+ if status := client.do(http.MethodPost, "/api/v1/tasks", map[string]any{
+ "type": "directlinks",
+ "storage_id": storageID,
+ "path": "resume",
+ "params": map[string]any{"urls": []string{source.URL + "/slow.bin"}},
+ }, true, &created); status != http.StatusCreated || created.TaskID == "" {
+ t.Fatalf("create task = %d %+v", status, created)
+ }
+ waitTaskStatus(t, client, created.TaskID, "running", 15*time.Second)
+
+ env.signal(t, sig, 15*time.Second)
+
+ env = startTestEnv(t, dir, port)
+ defer env.signal(t, os.Interrupt, 10*time.Second)
+ client = newClientAt(t, base)
+ info := waitTaskStatus(t, client, created.TaskID, "completed", 30*time.Second)
+ if info.Status != "completed" {
+ t.Fatalf("task status = %q, items = %+v", info.Status, info.Items)
+ }
+ got, err := os.ReadFile(filepath.Join(dir, "downloads", "resume", "slow.bin"))
+ if err != nil {
+ t.Fatalf("read resumed file: %v", err)
+ }
+ if !bytes.Equal(got, content) {
+ t.Fatalf("resumed file size = %d, want %d", len(got), len(content))
+ }
+}
+
+type testEnvProcess struct {
+ dir string
+ port string
+ base string
+ cmd *exec.Cmd
+ stopped chan struct{}
+ log *os.File
+}
+
+func startTestEnv(t *testing.T, dir, port string) *testEnvProcess {
+ t.Helper()
+ logPath := filepath.Join(dir, "testenv.log")
+ log, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
+ if err != nil {
+ t.Fatalf("open testenv log: %v", err)
+ }
+ cmd := exec.Command(testBinary,
+ "testenv",
+ "--data-dir", dir,
+ "--keep",
+ "--listen", "127.0.0.1:"+port,
+ "--admin-password", "test-admin",
+ "--storage-name", "test-local",
+ "--user-chat-id", "10001",
+ )
+ cmd.Dir = dir
+ cmd.Stdout = log
+ cmd.Stderr = log
+ cmd.Env = append(os.Environ(), "SAVEANY_ADMIN_PASSWORD=test-admin")
+ if err := cmd.Start(); err != nil {
+ log.Close()
+ t.Fatalf("start testenv: %v", err)
+ }
+ env := &testEnvProcess{dir: dir, port: port, base: "http://127.0.0.1:" + port, cmd: cmd, stopped: make(chan struct{}), log: log}
+ go func() {
+ if err := cmd.Wait(); err != nil {
+ fmt.Fprintln(os.Stderr, "testenv wait:", err)
+ }
+ close(env.stopped)
+ }()
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ defer cancel()
+ if err := waitForHealthAt(ctx, env.base); err != nil {
+ env.signal(t, os.Interrupt, 5*time.Second)
+ t.Fatalf("testenv did not become ready: %v", err)
+ }
+ t.Cleanup(func() {
+ select {
+ case <-env.stopped:
+ default:
+ if err := env.cmd.Process.Kill(); err != nil {
+ t.Logf("kill testenv: %v", err)
+ }
+ <-env.stopped
+ }
+ if err := env.log.Close(); err != nil {
+ t.Logf("close testenv log: %v", err)
+ }
+ })
+ return env
+}
+
+func (e *testEnvProcess) signal(t *testing.T, sig os.Signal, timeout time.Duration) {
+ t.Helper()
+ if err := e.cmd.Process.Signal(sig); err != nil {
+ t.Fatalf("signal testenv: %v", err)
+ }
+ select {
+ case <-e.stopped:
+ case <-time.After(timeout):
+ if err := e.cmd.Process.Kill(); err != nil {
+ t.Logf("kill testenv: %v", err)
+ }
+ <-e.stopped
+ t.Fatalf("testenv did not stop after %s", sig)
+ }
+}
+
+func waitTaskStatus(t *testing.T, client *apiClient, id, want string, timeout time.Duration) taskInfo {
+ t.Helper()
+ deadline := time.Now().Add(timeout)
+ for {
+ var info taskInfo
+ if status := client.do(http.MethodGet, "/api/v1/tasks/"+id, nil, false, &info); status != http.StatusOK {
+ t.Fatalf("get task %s = %d", id, status)
+ }
+ if info.Status == want {
+ return info
+ }
+ if want == "running" && (info.Status == "completed" || info.Status == "failed" || info.Status == "cancelled") {
+ t.Fatalf("task %s finished before interruption: %s", id, info.Status)
+ }
+ if time.Now().After(deadline) {
+ t.Fatalf("task %s = %q, want %q", id, info.Status, want)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+}
diff --git a/e2e/task_test.go b/e2e/task_test.go
new file mode 100644
index 00000000..46c28295
--- /dev/null
+++ b/e2e/task_test.go
@@ -0,0 +1,124 @@
+//go:build e2e
+
+package e2e
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+type taskInfo struct {
+ TaskID string `json:"task_id"`
+ Status string `json:"status"`
+ Items []struct {
+ Name string `json:"name"`
+ Path string `json:"path"`
+ Status string `json:"status"`
+ } `json:"items"`
+}
+
+func TestDirectLinksTask(t *testing.T) {
+ content := []byte("SaveAny-Bot direct-links E2E\n")
+ source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/sample.txt" {
+ http.NotFound(w, r)
+ return
+ }
+ w.Header().Set("Content-Type", "text/plain")
+ if _, err := w.Write(content); err != nil {
+ t.Errorf("write source response: %v", err)
+ }
+ }))
+ defer source.Close()
+
+ client := sharedClient(t)
+ storageID := storageIDByName(t, client, "e2e-local")
+ var created struct {
+ TaskID string `json:"task_id"`
+ Status string `json:"status"`
+ }
+ status := client.do(http.MethodPost, "/api/v1/tasks", map[string]any{
+ "type": "directlinks",
+ "storage_id": storageID,
+ "path": "e2e",
+ "params": map[string]any{"urls": []string{source.URL + "/sample.txt"}},
+ }, true, &created)
+ if status != http.StatusCreated || created.TaskID == "" {
+ t.Fatalf("create task = %d %+v", status, created)
+ }
+
+ info := waitForTask(t, client, created.TaskID)
+ if info.Status != "completed" {
+ t.Fatalf("task status = %q, want completed; items = %+v", info.Status, info.Items)
+ }
+
+ path := filepath.Join(dataDir, "downloads", "e2e", "sample.txt")
+ got, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("read downloaded file: %v", err)
+ }
+ if string(got) != string(content) {
+ t.Fatalf("downloaded content = %q, want %q", got, content)
+ }
+
+ var list struct {
+ Tasks []taskInfo `json:"tasks"`
+ Total int `json:"total"`
+ }
+ if status := client.do(http.MethodGet, "/api/v1/tasks", nil, false, &list); status != http.StatusOK {
+ t.Fatalf("list tasks = %d", status)
+ }
+ if list.Total != 1 || len(list.Tasks) != 1 || list.Tasks[0].TaskID != created.TaskID {
+ t.Fatalf("task list = %+v", list)
+ }
+
+ if status := client.do(http.MethodDelete, "/api/v1/tasks/"+created.TaskID, nil, true, nil); status != http.StatusNoContent {
+ t.Fatalf("delete task = %d", status)
+ }
+}
+
+func TestTaskValidation(t *testing.T) {
+ client := sharedClient(t)
+ storageID := storageIDByName(t, client, "e2e-local")
+
+ if status := client.do(http.MethodPost, "/api/v1/tasks", map[string]any{
+ "type": "directlinks", "storage_id": 999999, "params": map[string]any{"urls": []string{"http://example.com/a"}},
+ }, true, nil); status != http.StatusBadRequest {
+ t.Fatalf("unknown storage status = %d, want 400", status)
+ }
+
+ if status := client.do(http.MethodPost, "/api/v1/tasks", map[string]any{
+ "type": "tgfiles", "storage_id": storageID, "params": map[string]any{"message_links": []string{"https://t.me/example/1"}},
+ }, true, nil); status != http.StatusServiceUnavailable {
+ t.Fatalf("tgfiles without client status = %d, want 503", status)
+ }
+
+ if status := client.do(http.MethodPost, "/api/v1/tasks", map[string]any{
+ "type": "bogus", "storage_id": storageID, "params": map[string]any{},
+ }, true, nil); status != http.StatusBadRequest {
+ t.Fatalf("unsupported type status = %d, want 400", status)
+ }
+}
+
+func waitForTask(t *testing.T, client *apiClient, id string) taskInfo {
+ t.Helper()
+ deadline := time.Now().Add(30 * time.Second)
+ for {
+ var info taskInfo
+ if status := client.do(http.MethodGet, "/api/v1/tasks/"+id, nil, false, &info); status != http.StatusOK {
+ t.Fatalf("get task %s = %d", id, status)
+ }
+ switch info.Status {
+ case "completed", "partial", "failed", "cancelled":
+ return info
+ }
+ if time.Now().After(deadline) {
+ t.Fatalf("task %s did not finish; last status %q", id, info.Status)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+}
diff --git a/e2e/telegram/dispatcher.go b/e2e/telegram/dispatcher.go
new file mode 100644
index 00000000..15cf7ec0
--- /dev/null
+++ b/e2e/telegram/dispatcher.go
@@ -0,0 +1,52 @@
+//go:build e2e
+
+package telegrame2e
+
+import (
+ "context"
+ "errors"
+
+ "github.com/gotd/td/telegram"
+ "github.com/gotd/td/tg"
+ "github.com/krau/mygotg/dispatcher"
+ "github.com/krau/mygotg/ext"
+)
+
+// Dispatcher records handlers registered by client/bot/handlers and executes
+// them in registration order, mirroring the production dispatcher groups.
+type Dispatcher struct {
+ handlers []dispatcher.Handler
+}
+
+func NewDispatcher() *Dispatcher {
+ return &Dispatcher{}
+}
+
+func (d *Dispatcher) Initialize(context.Context, context.CancelFunc, *telegram.Client, *tg.User) {}
+
+func (d *Dispatcher) Handle(context.Context, tg.UpdatesClass) error { return nil }
+
+func (d *Dispatcher) AddHandler(handler dispatcher.Handler) {
+ d.handlers = append(d.handlers, handler)
+}
+
+func (d *Dispatcher) AddHandlerToGroup(handler dispatcher.Handler, _ int) {
+ d.handlers = append(d.handlers, handler)
+}
+
+func (d *Dispatcher) Dispatch(ctx *ext.Context, update *ext.Update) error {
+ for _, handler := range d.handlers {
+ err := handler.CheckUpdate(ctx, update)
+ switch {
+ case err == nil, errors.Is(err, dispatcher.ContinueGroups):
+ continue
+ case errors.Is(err, dispatcher.EndGroups):
+ return nil
+ case errors.Is(err, dispatcher.SkipCurrentGroup):
+ return nil
+ default:
+ return err
+ }
+ }
+ return nil
+}
diff --git a/e2e/telegram/fake.go b/e2e/telegram/fake.go
new file mode 100644
index 00000000..9edbfba2
--- /dev/null
+++ b/e2e/telegram/fake.go
@@ -0,0 +1,526 @@
+//go:build e2e
+
+package telegrame2e
+
+import (
+ "context"
+ "fmt"
+ "sort"
+ "sync"
+ "time"
+
+ "github.com/gotd/td/bin"
+ "github.com/gotd/td/tg"
+)
+
+// SentMessage records a message produced by messages.sendMessage/sendMedia.
+type SentMessage struct {
+ Peer tg.PeerClass
+ Text string
+ ID int
+}
+
+// Edit records a messages.editMessage request.
+type Edit struct {
+ Peer tg.PeerClass
+ ID int
+ Text string
+}
+
+// Callback records a messages.setBotCallbackAnswer request.
+type Callback struct {
+ Text string
+ Alert bool
+}
+
+// Server is an in-memory Telegram RPC responder. It implements tg.Invoker, so
+// the real gotd client, sender and downloader code run against it unchanged.
+type Server struct {
+ mu sync.Mutex
+
+ self *tg.User
+ messages map[int]*tg.Message
+ byPeer map[int64][]*tg.Message
+ users map[int64]*tg.User
+ chats map[int64]*tg.Chat
+ channels map[int64]*tg.Channel
+ files map[int64][]byte
+ uploads map[int64]map[int][]byte
+ lastUpload int64
+
+ nextMessageID int
+ nextFileID int64
+
+ sent []SentMessage
+ edits []Edit
+ callbacks []Callback
+}
+
+func NewServer(self *tg.User) *Server {
+ return &Server{
+ self: self,
+ messages: make(map[int]*tg.Message),
+ byPeer: make(map[int64][]*tg.Message),
+ users: map[int64]*tg.User{self.ID: self},
+ chats: make(map[int64]*tg.Chat),
+ channels: make(map[int64]*tg.Channel),
+ files: make(map[int64][]byte),
+ uploads: make(map[int64]map[int][]byte),
+ }
+}
+
+func (s *Server) AddUser(user *tg.User) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.users[user.ID] = user
+}
+
+func (s *Server) AddChat(chat *tg.Chat) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.chats[chat.ID] = chat
+}
+
+func (s *Server) AddChannel(channel *tg.Channel) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.channels[channel.ID] = channel
+}
+
+func (s *Server) AddDocumentMessage(peer, from tg.PeerClass, name string, data []byte) *tg.Message {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ fileID := s.newFileLocked()
+ s.files[fileID] = append([]byte(nil), data...)
+ document := &tg.Document{
+ ID: fileID,
+ AccessHash: 1,
+ FileReference: []byte{1},
+ Date: int(time.Now().Unix()),
+ MimeType: "text/plain",
+ Size: int64(len(data)),
+ DCID: 2,
+ Attributes: []tg.DocumentAttributeClass{
+ &tg.DocumentAttributeFilename{FileName: name},
+ },
+ }
+ message := &tg.Message{
+ ID: s.newMessageIDLocked(),
+ PeerID: peer,
+ Date: int(time.Now().Unix()),
+ }
+ message.SetFromID(from)
+ message.SetMedia(&tg.MessageMediaDocument{Document: document})
+ s.storeLocked(message)
+ return message
+}
+
+func (s *Server) AddTextMessage(peer, from tg.PeerClass, text string) *tg.Message {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ message := &tg.Message{
+ ID: s.newMessageIDLocked(),
+ PeerID: peer,
+ Message: text,
+ Date: int(time.Now().Unix()),
+ }
+ message.SetFromID(from)
+ s.storeLocked(message)
+ return message
+}
+
+func (s *Server) Sent() []SentMessage {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return append([]SentMessage(nil), s.sent...)
+}
+
+func (s *Server) Edits() []Edit {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return append([]Edit(nil), s.edits...)
+}
+
+func (s *Server) Callbacks() []Callback {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return append([]Callback(nil), s.callbacks...)
+}
+
+func (s *Server) LastUpload() []byte {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.partsLocked(s.lastUpload)
+}
+
+func (s *Server) Uploaded(fileID int64) []byte {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return s.partsLocked(fileID)
+}
+
+func (s *Server) ResetRecords() {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ s.sent = nil
+ s.edits = nil
+ s.callbacks = nil
+ s.uploads = make(map[int64]map[int][]byte)
+ s.lastUpload = 0
+}
+
+func (s *Server) newMessageIDLocked() int {
+ s.nextMessageID++
+ return s.nextMessageID
+}
+
+func (s *Server) newFileLocked() int64 {
+ s.nextFileID++
+ return -1000 - s.nextFileID
+}
+
+func (s *Server) storeLocked(message *tg.Message) {
+ s.messages[message.ID] = message
+ key := peerID(message.PeerID)
+ s.byPeer[key] = append(s.byPeer[key], message)
+}
+
+func (s *Server) partsLocked(fileID int64) []byte {
+ parts := s.uploads[fileID]
+ if len(parts) == 0 {
+ return nil
+ }
+ indices := make([]int, 0, len(parts))
+ for index := range parts {
+ indices = append(indices, index)
+ }
+ sort.Ints(indices)
+ var out []byte
+ for _, index := range indices {
+ out = append(out, parts[index]...)
+ }
+ return out
+}
+
+// Invoke implements tg.Invoker.
+func (s *Server) Invoke(_ context.Context, input bin.Encoder, output bin.Decoder) error {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ switch request := input.(type) {
+ case *tg.MessagesSendMessageRequest:
+ message := s.newOutgoingLocked(request.Peer, request.Message, nil)
+ if reply, ok := request.ReplyTo.(*tg.InputReplyToMessage); ok && reply.ReplyToMsgID != 0 {
+ message.SetReplyTo(&tg.MessageReplyHeader{ReplyToMsgID: reply.ReplyToMsgID})
+ }
+ s.sent = append(s.sent, SentMessage{Peer: message.PeerID, Text: message.Message, ID: message.ID})
+ return setUpdates(output, message, false, s.chatEntitiesLocked())
+ case *tg.MessagesEditMessageRequest:
+ peer := peerFromInput(request.Peer)
+ stored := s.messages[request.ID]
+ var message tg.Message
+ if stored != nil {
+ message = *stored
+ } else {
+ message = tg.Message{ID: request.ID, PeerID: peer, Date: int(time.Now().Unix())}
+ message.SetFromID(&tg.PeerUser{UserID: s.self.ID})
+ s.storeLocked(&message)
+ }
+ message.Message = request.Message
+ if request.Entities != nil {
+ message.Entities = request.Entities
+ }
+ s.messages[request.ID] = &message
+ s.edits = append(s.edits, Edit{Peer: message.PeerID, ID: message.ID, Text: message.Message})
+ updateMessage := message
+ return setUpdates(output, &updateMessage, true, s.chatEntitiesLocked())
+ case *tg.MessagesSetBotCallbackAnswerRequest:
+ s.callbacks = append(s.callbacks, Callback{Text: request.Message, Alert: request.Alert})
+ return setBool(output, true)
+ case *tg.UploadSaveFilePartRequest:
+ if s.uploads[request.FileID] == nil {
+ s.uploads[request.FileID] = make(map[int][]byte)
+ }
+ s.uploads[request.FileID][request.FilePart] = append([]byte(nil), request.Bytes...)
+ s.lastUpload = request.FileID
+ return setBool(output, true)
+ case *tg.UploadSaveBigFilePartRequest:
+ if s.uploads[request.FileID] == nil {
+ s.uploads[request.FileID] = make(map[int][]byte)
+ }
+ s.uploads[request.FileID][request.FilePart] = append([]byte(nil), request.Bytes...)
+ s.lastUpload = request.FileID
+ return setBool(output, true)
+ case *tg.UploadGetFileRequest:
+ return s.getFileLocked(request, output)
+ case *tg.MessagesGetMessagesRequest:
+ return s.getMessagesLocked(request.ID, output)
+ case *tg.ChannelsGetMessagesRequest:
+ return s.getMessagesLocked(request.ID, output)
+ case *tg.MessagesGetHistoryRequest:
+ return s.getHistoryLocked(request, output)
+ case *tg.UsersGetUsersRequest:
+ if vector, ok := output.(*tg.UserClassVector); ok {
+ for _, id := range request.ID {
+ if user, ok := id.(*tg.InputUser); ok {
+ if found := s.users[user.UserID]; found != nil {
+ vector.Elems = append(vector.Elems, found)
+ }
+ }
+ }
+ }
+ return nil
+ case *tg.ChannelsGetChannelsRequest:
+ if box, ok := output.(*tg.MessagesChatsBox); ok {
+ chats := make([]tg.ChatClass, 0, len(request.ID))
+ for _, id := range request.ID {
+ if input, ok := id.(*tg.InputChannel); ok {
+ if channel := s.channels[input.ChannelID]; channel != nil {
+ chats = append(chats, channel)
+ }
+ }
+ }
+ box.Chats = &tg.MessagesChats{Chats: chats}
+ }
+ return nil
+ case *tg.MessagesSendMediaRequest:
+ message := s.newOutgoingLocked(request.Peer, request.Message, request.Media)
+ s.sent = append(s.sent, SentMessage{Peer: message.PeerID, Text: message.Message, ID: message.ID})
+ return setUpdates(output, message, false, s.chatEntitiesLocked())
+ case *tg.MessagesSendMultiMediaRequest:
+ messages := make([]*tg.Message, 0, len(request.MultiMedia))
+ for _, item := range request.MultiMedia {
+ message := s.newOutgoingLocked(request.Peer, item.Message, item.Media)
+ s.sent = append(s.sent, SentMessage{Peer: message.PeerID, Text: message.Message, ID: message.ID})
+ messages = append(messages, message)
+ }
+ if box, ok := output.(*tg.UpdatesBox); ok {
+ updates := make([]tg.UpdateClass, 0, len(messages))
+ for _, message := range messages {
+ updates = append(updates, &tg.UpdateNewMessage{Message: message, Pts: message.ID, PtsCount: 1})
+ }
+ box.Updates = &tg.Updates{Updates: updates, Users: []tg.UserClass{s.self}, Chats: s.chatEntitiesLocked(), Date: int(time.Now().Unix())}
+ }
+ return nil
+ case *tg.MessagesUploadMediaRequest:
+ media := s.mediaFromInputLocked(request.Media)
+ if box, ok := output.(*tg.MessageMediaBox); ok {
+ box.MessageMedia = media
+ }
+ return nil
+ }
+ return nil
+}
+
+func (s *Server) newOutgoingLocked(peerInput tg.InputPeerClass, text string, media tg.InputMediaClass) *tg.Message {
+ peer := peerFromInput(peerInput)
+ message := &tg.Message{
+ ID: s.newMessageIDLocked(),
+ PeerID: peer,
+ Message: text,
+ Date: int(time.Now().Unix()),
+ Out: true,
+ }
+ message.SetFromID(&tg.PeerUser{UserID: s.self.ID})
+ if media != nil {
+ if converted := s.mediaFromInputLocked(media); converted != nil {
+ message.SetMedia(converted)
+ }
+ }
+ s.storeLocked(message)
+ return message
+}
+
+func (s *Server) mediaFromInputLocked(input tg.InputMediaClass) tg.MessageMediaClass {
+ switch media := input.(type) {
+ case *tg.InputMediaUploadedDocument:
+ data := s.uploadedDataLocked(media.File)
+ fileID := inputFileID(media.File)
+ document := &tg.Document{
+ ID: fileID,
+ AccessHash: 1,
+ FileReference: []byte{1},
+ Date: int(time.Now().Unix()),
+ MimeType: media.MimeType,
+ Size: int64(len(data)),
+ DCID: 2,
+ Attributes: media.Attributes,
+ }
+ return &tg.MessageMediaDocument{Document: document}
+ case *tg.InputMediaDocument:
+ input, ok := media.ID.(*tg.InputDocument)
+ if !ok {
+ return nil
+ }
+ data := s.files[input.ID]
+ return &tg.MessageMediaDocument{Document: &tg.Document{
+ ID: input.ID,
+ AccessHash: input.AccessHash,
+ FileReference: append([]byte(nil), input.FileReference...),
+ Date: int(time.Now().Unix()),
+ Size: int64(len(data)),
+ DCID: 2,
+ }}
+ default:
+ return nil
+ }
+}
+
+func (s *Server) uploadedDataLocked(file tg.InputFileClass) []byte {
+ switch f := file.(type) {
+ case *tg.InputFile:
+ return s.partsLocked(f.ID)
+ case *tg.InputFileBig:
+ return s.partsLocked(f.ID)
+ default:
+ return nil
+ }
+}
+
+func (s *Server) getFileLocked(request *tg.UploadGetFileRequest, output bin.Decoder) error {
+ var data []byte
+ switch location := request.Location.(type) {
+ case *tg.InputDocumentFileLocation:
+ data = s.files[location.ID]
+ case *tg.InputPhotoFileLocation:
+ data = s.files[location.ID]
+ }
+ offset := int(request.Offset)
+ if offset > len(data) {
+ offset = len(data)
+ }
+ end := len(data)
+ if request.Limit > 0 && offset+request.Limit < end {
+ end = offset + request.Limit
+ }
+ chunk := append([]byte(nil), data[offset:end]...)
+ if box, ok := output.(*tg.UploadFileBox); ok {
+ box.File = &tg.UploadFile{Bytes: chunk}
+ }
+ return nil
+}
+
+func (s *Server) getMessagesLocked(ids []tg.InputMessageClass, output bin.Decoder) error {
+ messages := make([]tg.MessageClass, 0, len(ids))
+ for _, id := range ids {
+ input, ok := id.(*tg.InputMessageID)
+ if !ok {
+ continue
+ }
+ if message := s.messages[input.ID]; message != nil {
+ messages = append(messages, message)
+ }
+ }
+ return setMessages(output, messages, s.chatEntitiesLocked())
+}
+
+func (s *Server) getHistoryLocked(request *tg.MessagesGetHistoryRequest, output bin.Decoder) error {
+ var messages []tg.MessageClass
+ for _, message := range s.byPeer[peerID(peerFromInput(request.Peer))] {
+ messages = append(messages, message)
+ }
+ return setMessages(output, messages, s.chatEntitiesLocked())
+}
+
+func setUpdates(output bin.Decoder, message *tg.Message, edit bool, chats []tg.ChatClass) error {
+ box, ok := output.(*tg.UpdatesBox)
+ if !ok {
+ return nil
+ }
+ var update tg.UpdateClass
+ if edit {
+ if _, ok := message.PeerID.(*tg.PeerChannel); ok {
+ update = &tg.UpdateEditChannelMessage{Message: message, Pts: message.ID, PtsCount: 1}
+ } else {
+ update = &tg.UpdateEditMessage{Message: message, Pts: message.ID, PtsCount: 1}
+ }
+ } else {
+ update = &tg.UpdateNewMessage{Message: message, Pts: message.ID, PtsCount: 1}
+ }
+ box.Updates = &tg.Updates{
+ Updates: []tg.UpdateClass{update},
+ Users: []tg.UserClass{},
+ Chats: chats,
+ Date: int(time.Now().Unix()),
+ }
+ return nil
+}
+
+func setMessages(output bin.Decoder, messages []tg.MessageClass, chats []tg.ChatClass) error {
+ box, ok := output.(*tg.MessagesMessagesBox)
+ if !ok {
+ return nil
+ }
+ box.Messages = &tg.MessagesMessages{Messages: messages, Chats: chats}
+ return nil
+}
+
+func setBool(output bin.Decoder, value bool) error {
+ box, ok := output.(*tg.BoolBox)
+ if !ok {
+ return nil
+ }
+ if value {
+ box.Bool = &tg.BoolTrue{}
+ } else {
+ box.Bool = &tg.BoolFalse{}
+ }
+ return nil
+}
+
+func (s *Server) chatEntitiesLocked() []tg.ChatClass {
+ out := make([]tg.ChatClass, 0, len(s.chats)+len(s.channels))
+ for _, chat := range s.chats {
+ out = append(out, chat)
+ }
+ for _, channel := range s.channels {
+ out = append(out, channel)
+ }
+ return out
+}
+
+func peerID(peer tg.PeerClass) int64 {
+ switch value := peer.(type) {
+ case *tg.PeerUser:
+ return value.UserID
+ case *tg.PeerChat:
+ return value.ChatID
+ case *tg.PeerChannel:
+ return value.ChannelID
+ default:
+ return 0
+ }
+}
+
+func peerFromInput(peer tg.InputPeerClass) tg.PeerClass {
+ switch value := peer.(type) {
+ case *tg.InputPeerUser:
+ return &tg.PeerUser{UserID: value.UserID}
+ case *tg.InputPeerChat:
+ return &tg.PeerChat{ChatID: value.ChatID}
+ case *tg.InputPeerChannel:
+ return &tg.PeerChannel{ChannelID: value.ChannelID}
+ case *tg.InputPeerSelf:
+ return &tg.PeerUser{UserID: 0}
+ default:
+ return &tg.PeerUser{}
+ }
+}
+
+func inputFileID(file tg.InputFileClass) int64 {
+ switch value := file.(type) {
+ case *tg.InputFile:
+ return value.ID
+ case *tg.InputFileBig:
+ return value.ID
+ default:
+ return 0
+ }
+}
+
+var _ tg.Invoker = (*Server)(nil)
+
+func (s *Server) String() string {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return fmt.Sprintf("fake telegram: %d sent, %d edits", len(s.sent), len(s.edits))
+}
diff --git a/e2e/telegram/harness.go b/e2e/telegram/harness.go
new file mode 100644
index 00000000..d0421448
--- /dev/null
+++ b/e2e/telegram/harness.go
@@ -0,0 +1,280 @@
+//go:build e2e
+
+package telegrame2e
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/telegram/message"
+ "github.com/gotd/td/tg"
+ sabapp "github.com/krau/SaveAny-Bot/app"
+ "github.com/krau/SaveAny-Bot/boot"
+ "github.com/krau/SaveAny-Bot/client/bot/handlers"
+ "github.com/krau/SaveAny-Bot/config"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/settings"
+ "github.com/krau/mygotg/ext"
+ tgstorage "github.com/krau/mygotg/storage"
+ "github.com/krau/mygotg/types"
+)
+
+const (
+ selfID = int64(999)
+ localUserID = int64(10001)
+ tgUserID = int64(10002)
+ groupID = int64(20001)
+ channelID = int64(30001)
+)
+
+type Harness struct {
+ Server *Server
+ Context context.Context
+ Cancel context.CancelFunc
+ Ext *ext.Context
+ Dispatcher *Dispatcher
+ Entities *tg.Entities
+ DataDir string
+ LocalPath string
+ app *sabapp.App
+ oldWD string
+}
+
+func NewHarness() (*Harness, error) {
+ dir, err := os.MkdirTemp("", "saveany-telegram-e2e-")
+ if err != nil {
+ return nil, err
+ }
+ oldWD, err := os.Getwd()
+ if err != nil {
+ if removeErr := os.RemoveAll(dir); removeErr != nil {
+ log.Warnf("Failed to remove harness directory: %v", removeErr)
+ }
+ return nil, err
+ }
+ if err := os.Chdir(dir); err != nil {
+ if removeErr := os.RemoveAll(dir); removeErr != nil {
+ log.Warnf("Failed to remove harness directory: %v", removeErr)
+ }
+ return nil, err
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ h := &Harness{Context: ctx, Cancel: cancel, DataDir: dir, LocalPath: filepath.Join(dir, "files"), oldWD: oldWD}
+ cleanup := true
+ defer func() {
+ if cleanup {
+ cancel()
+ if err := os.Chdir(oldWD); err != nil {
+ log.Warnf("Failed to restore working directory: %v", err)
+ }
+ if err := os.RemoveAll(dir); err != nil {
+ log.Warnf("Failed to remove harness directory: %v", err)
+ }
+ }
+ }()
+
+ opts := boot.Options{DataDir: dir, DBPath: filepath.Join(dir, "saveany.db")}
+ res, err := boot.Init(ctx, opts)
+ if err != nil {
+ return nil, fmt.Errorf("boot: %w", err)
+ }
+ cfg := res.Settings.Config()
+ cfg.Temp.BasePath = filepath.Join(dir, "cache")
+ config.Set(cfg)
+
+ localRecord, err := res.Settings.CreateStorage(ctx, settings.StorageInput{
+ Name: "local", Type: "local", Enable: true,
+ Config: map[string]any{"base_path": h.LocalPath},
+ })
+ if err != nil {
+ return nil, fmt.Errorf("local storage: %w", err)
+ }
+ telegramRecord, err := res.Settings.CreateStorage(ctx, settings.StorageInput{
+ Name: "tg", Type: "telegram", Enable: true,
+ Config: map[string]any{"chat_id": tgUserID},
+ })
+ if err != nil {
+ return nil, fmt.Errorf("telegram storage: %w", err)
+ }
+ for _, user := range []struct {
+ id int64
+ storageID uint
+ }{{localUserID, localRecord.ID}, {tgUserID, telegramRecord.ID}} {
+ if _, err := database.GetUserByChatID(ctx, user.id); err != nil {
+ if !database.IsNotFound(err) {
+ return nil, fmt.Errorf("find user %d: %w", user.id, err)
+ }
+ if err := database.CreateUser(ctx, user.id); err != nil {
+ return nil, fmt.Errorf("create user %d: %w", user.id, err)
+ }
+ }
+ record, err := database.GetUserByChatID(ctx, user.id)
+ if err != nil {
+ return nil, fmt.Errorf("load user %d: %w", user.id, err)
+ }
+ record.Enabled = true
+ record.Silent = true
+ record.DefaultStorageID = user.storageID
+ if err := database.UpdateUser(ctx, record); err != nil {
+ return nil, fmt.Errorf("update user %d: %w", user.id, err)
+ }
+ }
+ if err := res.Settings.ReloadConfig(ctx); err != nil {
+ return nil, fmt.Errorf("reload users: %w", err)
+ }
+
+ self := &tg.User{ID: selfID, AccessHash: 1, Username: "testbot", FirstName: "Test"}
+ self.SetBot(true)
+ h.Server = NewServer(self)
+ users := map[int64]*tg.User{
+ selfID: self,
+ localUserID: {ID: localUserID, AccessHash: 1, FirstName: "Local"},
+ tgUserID: {ID: tgUserID, AccessHash: 1, FirstName: "Telegram"},
+ }
+ for _, user := range users {
+ h.Server.AddUser(user)
+ }
+ chat := &tg.Chat{ID: groupID, Title: "E2E Group"}
+ channel := &tg.Channel{ID: channelID, AccessHash: 1, Title: "E2E Supergroup"}
+ channel.SetMegagroup(true)
+ h.Server.AddChat(chat)
+ h.Server.AddChannel(channel)
+
+ peers := tgstorage.NewPeerStorage(nil, true)
+ peers.AddPeer(selfID, 1, tgstorage.TypeUser, "testbot")
+ for _, id := range []int64{localUserID, tgUserID} {
+ peers.AddPeer(id, 1, tgstorage.TypeUser, "")
+ }
+ peers.AddPeer(groupID, 0, tgstorage.TypeChat, "")
+ peers.AddPeer(channelID, 1, tgstorage.TypeChannel, "")
+
+ h.Entities = &tg.Entities{
+ Users: users,
+ Chats: map[int64]*tg.Chat{groupID: chat},
+ Channels: map[int64]*tg.Channel{channelID: channel},
+ }
+ raw := tg.NewClient(h.Server)
+ h.Ext = ext.NewContext(ctx, raw, peers, self, message.NewSender(raw), h.Entities, false)
+ h.Dispatcher = NewDispatcher()
+ handlers.Register(h.Dispatcher)
+
+ h.app = sabapp.New(ctx, res.Settings, sabapp.Options{
+ DataDir: dir,
+ DBPath: filepath.Join(dir, "saveany.db"),
+ NoWeb: true,
+ })
+ if err := h.app.Start(); err != nil {
+ return nil, fmt.Errorf("start app: %w", err)
+ }
+ cleanup = false
+ return h, nil
+}
+
+func (h *Harness) Close() {
+ if h == nil {
+ return
+ }
+ h.Cancel()
+ if h.app != nil {
+ h.app.Stop()
+ }
+ if h.oldWD != "" {
+ if err := os.Chdir(h.oldWD); err != nil {
+ log.Warnf("Failed to restore working directory: %v", err)
+ }
+ }
+ if h.DataDir != "" {
+ if err := os.RemoveAll(h.DataDir); err != nil {
+ log.Warnf("Failed to remove harness directory: %v", err)
+ }
+ }
+}
+
+func (h *Harness) MessageUpdate(message *tg.Message) *ext.Update {
+ return &ext.Update{
+ UpdateClass: &tg.UpdateNewMessage{Message: message, Pts: message.ID, PtsCount: 1},
+ EffectiveMessage: types.ConstructMessage(message),
+ Entities: h.Entities,
+ }
+}
+
+func (h *Harness) CommandUpdate(command *tg.Message, replyTo *tg.Message) *ext.Update {
+ update := h.MessageUpdate(command)
+ if replyTo != nil {
+ update.EffectiveMessage.ReplyToMessage = types.ConstructMessage(replyTo)
+ }
+ return update
+}
+
+func (h *Harness) Dispatch(update *ext.Update) error {
+ return h.Dispatcher.Dispatch(h.Ext, update)
+}
+
+func (h *Harness) UserPeer(id int64) *tg.PeerUser {
+ return &tg.PeerUser{UserID: id}
+}
+
+func (h *Harness) ClearLocal() error {
+ if err := os.RemoveAll(h.LocalPath); err != nil {
+ return err
+ }
+ return os.MkdirAll(h.LocalPath, 0o755)
+}
+
+func (h *Harness) WaitLocalFile(timeout time.Duration) ([]byte, error) {
+ deadline := time.Now().Add(timeout)
+ for {
+ var found []byte
+ if err := filepath.WalkDir(h.LocalPath, func(path string, entry os.DirEntry, err error) error {
+ if err != nil || entry.IsDir() || found != nil {
+ return nil
+ }
+ data, readErr := os.ReadFile(path)
+ if readErr == nil {
+ found = data
+ }
+ return nil
+ }); err != nil {
+ return nil, fmt.Errorf("walk local files: %w", err)
+ }
+ if found != nil {
+ return found, nil
+ }
+ if time.Now().After(deadline) {
+ return nil, fmt.Errorf("no file found under %s", h.LocalPath)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+}
+
+func (h *Harness) WaitFile(relative string, timeout time.Duration) ([]byte, error) {
+ path := filepath.Join(h.LocalPath, relative)
+ deadline := time.Now().Add(timeout)
+ for {
+ data, err := os.ReadFile(path)
+ if err == nil {
+ return data, nil
+ }
+ if time.Now().After(deadline) {
+ return nil, fmt.Errorf("file %s not found: %w", path, err)
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+}
+
+func (h *Harness) WaitUploaded(timeout time.Duration) ([]byte, error) {
+ deadline := time.Now().Add(timeout)
+ for {
+ if data := h.Server.LastUpload(); len(data) > 0 {
+ return data, nil
+ }
+ if time.Now().After(deadline) {
+ return nil, fmt.Errorf("no upload captured")
+ }
+ time.Sleep(100 * time.Millisecond)
+ }
+}
diff --git a/e2e/telegram/resume_test.go b/e2e/telegram/resume_test.go
new file mode 100644
index 00000000..b691b392
--- /dev/null
+++ b/e2e/telegram/resume_test.go
@@ -0,0 +1,296 @@
+//go:build e2e
+
+package telegrame2e
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "path/filepath"
+ "slices"
+ "sync"
+ "testing"
+
+ "github.com/gotd/td/telegram/downloader"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/config"
+ storcfg "github.com/krau/SaveAny-Bot/config/storage"
+ "github.com/krau/SaveAny-Bot/core/resume"
+ "github.com/krau/SaveAny-Bot/core/tasks/batchtfile"
+ "github.com/krau/SaveAny-Bot/core/tasks/tfile"
+ "github.com/krau/SaveAny-Bot/database"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+ "github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
+ "github.com/krau/SaveAny-Bot/pkg/taskspec"
+ tfilepkg "github.com/krau/SaveAny-Bot/pkg/tfile"
+ "github.com/rs/xid"
+)
+
+// failingDownloadClient fails after failAfter successful chunk requests and
+// records the offsets requested by the downloader.
+type failingDownloadClient struct {
+ downloader.Client
+ failAfter int
+ mu sync.Mutex
+ calls int
+ offsets []int64
+}
+
+func (c *failingDownloadClient) UploadGetFile(ctx context.Context, req *tg.UploadGetFileRequest) (tg.UploadFileClass, error) {
+ c.mu.Lock()
+ c.calls++
+ c.offsets = append(c.offsets, req.Offset)
+ fail := c.failAfter > 0 && c.calls > c.failAfter
+ c.mu.Unlock()
+ if fail {
+ return nil, errors.New("mock download failure")
+ }
+ return c.Client.UploadGetFile(ctx, req)
+}
+
+func (c *failingDownloadClient) requested() []int64 {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return append([]int64(nil), c.offsets...)
+}
+
+// recordingStorage captures the bytes uploaded by a task.
+type recordingStorage struct {
+ mu sync.Mutex
+ data []byte
+ files map[string][]byte
+}
+
+func (s *recordingStorage) Init(context.Context, storcfg.StorageConfig) error { return nil }
+func (s *recordingStorage) Type() storenum.StorageType { return storenum.Local }
+func (s *recordingStorage) Name() string { return "mock" }
+func (s *recordingStorage) Save(_ context.Context, r io.Reader, path string) error {
+ data, err := io.ReadAll(r)
+ if err != nil {
+ return err
+ }
+ s.mu.Lock()
+ s.data = data
+ if s.files == nil {
+ s.files = make(map[string][]byte)
+ }
+ s.files[path] = data
+ s.mu.Unlock()
+ return nil
+}
+func (s *recordingStorage) Exists(context.Context, string) (bool, error) { return false, nil }
+
+func (s *recordingStorage) saved(path string) []byte {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return append([]byte(nil), s.files[path]...)
+}
+
+func TestTelegramTaskDownloadResumesAfterMockFailure(t *testing.T) {
+ harness.Server.ResetRecords()
+ content := bytes.Repeat([]byte("resume-data-"), (12*1024*1024)/12+1)[:12*1024*1024]
+ const name = "resume.bin"
+ document := harness.Server.AddDocumentMessage(
+ harness.UserPeer(localUserID),
+ harness.UserPeer(localUserID),
+ name,
+ content,
+ )
+ media, ok := document.Media.(*tg.MessageMediaDocument)
+ if !ok {
+ t.Fatalf("message media = %T", document.Media)
+ }
+ doc, ok := media.Document.AsNotEmpty()
+ if !ok {
+ t.Fatal("document is empty")
+ }
+
+ taskID := "resume-" + xid.New().String()
+ params, err := json.Marshal(taskspec.TGFile{Items: []taskspec.TGFileItem{{
+ ID: taskID, ChatID: localUserID, MessageID: document.ID, Name: name,
+ Storage: "mock", Path: name,
+ }}})
+ if err != nil {
+ t.Fatal(err)
+ }
+ record := &database.TaskRecord{
+ TaskID: taskID, Type: tasktype.TaskTypeTgfiles.String(), Status: "running", Params: string(params),
+ }
+ if err := database.CreateTaskRecord(context.Background(), record); err != nil {
+ t.Fatal(err)
+ }
+ defer database.DeleteTaskRecord(context.Background(), taskID)
+
+ base := tg.NewClient(harness.Server)
+ location := &tg.InputDocumentFileLocation{
+ ID: doc.ID, AccessHash: doc.AccessHash, FileReference: doc.FileReference,
+ }
+ firstClient := &failingDownloadClient{Client: base, failAfter: 3}
+ file := tfilepkg.NewTGFile(location, firstClient, int64(len(content)), name)
+ storage := &recordingStorage{}
+ first, err := tfile.NewTGFileTask(taskID, context.Background(), file, storage, name, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := first.Execute(context.Background()); err == nil {
+ t.Fatal("first download unexpectedly succeeded")
+ }
+ t.Logf("first run offsets: %v", firstClient.requested())
+ if len(firstClient.requested()) < 3 {
+ t.Fatalf("first download requested %d chunks, want at least 3", len(firstClient.requested()))
+ }
+
+ cachePath, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, fmt.Sprintf("%s_%s", taskID, name)))
+ if err != nil {
+ t.Fatal(err)
+ }
+ partPath := cachePath + ".part"
+ bitmapPath := partPath + ".bitmap"
+ if _, err := os.Stat(partPath); err != nil {
+ t.Fatalf("partial cache not retained: %v", err)
+ }
+ if _, err := os.Stat(bitmapPath); err != nil {
+ t.Fatalf("resume bitmap not retained: %v", err)
+ }
+
+ secondClient := &failingDownloadClient{Client: base}
+ file = tfilepkg.NewTGFile(location, secondClient, int64(len(content)), name)
+ second, err := tfile.NewTGFileTask(taskID, context.Background(), file, storage, name, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := second.Execute(context.Background()); err != nil {
+ t.Fatalf("resumed download failed: %v", err)
+ }
+ if !bytes.Equal(storage.data, content) {
+ t.Fatalf("uploaded content size = %d, want %d", len(storage.data), len(content))
+ }
+ t.Logf("resume offsets: %v", secondClient.requested())
+ for _, offset := range secondClient.requested() {
+ if offset < 2*1024*1024 {
+ t.Fatalf("resume re-requested completed offset %d", offset)
+ }
+ }
+ if _, err := os.Stat(cachePath); !os.IsNotExist(err) {
+ t.Fatalf("cache still exists after upload: %v", err)
+ }
+ if _, err := os.Stat(partPath); !os.IsNotExist(err) {
+ t.Fatalf("part file still exists after upload: %v", err)
+ }
+ if _, err := os.Stat(bitmapPath); !os.IsNotExist(err) {
+ t.Fatalf("bitmap still exists after upload: %v", err)
+ }
+ saved, err := database.GetTaskRecord(context.Background(), taskID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var savedSpec taskspec.TGFile
+ if err := json.Unmarshal([]byte(saved.Params), &savedSpec); err != nil {
+ t.Fatalf("invalid persisted params: %v", err)
+ }
+ if !slices.Contains(savedSpec.Done, taskID) {
+ t.Fatalf("persisted done list = %v, want %s", savedSpec.Done, taskID)
+ }
+
+ // A queued record whose done list is complete must be finalized without a
+ // Telegram client or another download.
+ if err := database.UpdateTaskRecord(context.Background(), taskID, map[string]any{"status": "queued"}); err != nil {
+ t.Fatal(err)
+ }
+ resume.Run(context.Background())
+ recovered, err := database.GetTaskRecord(context.Background(), taskID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if recovered.Status != "completed" {
+ t.Fatalf("recovered status = %q, want completed", recovered.Status)
+ }
+}
+
+func TestTelegramBatchDownloadResumesAfterMockFailure(t *testing.T) {
+ harness.Server.ResetRecords()
+ type fileCase struct {
+ name string
+ content []byte
+ id string
+ doc *tg.Document
+ }
+ files := []fileCase{
+ {name: "batch-a.bin", content: bytes.Repeat([]byte("a"), 4*1024*1024), id: xid.New().String()},
+ {name: "batch-b.bin", content: bytes.Repeat([]byte("b"), 4*1024*1024), id: xid.New().String()},
+ }
+ base := tg.NewClient(harness.Server)
+ storage := &recordingStorage{}
+ taskID := "batch-resume-" + xid.New().String()
+ items := make([]taskspec.TGFileItem, 0, len(files))
+ for i := range files {
+ document := harness.Server.AddDocumentMessage(
+ harness.UserPeer(localUserID), harness.UserPeer(localUserID), files[i].name, files[i].content,
+ )
+ media := document.Media.(*tg.MessageMediaDocument)
+ doc, ok := media.Document.AsNotEmpty()
+ if !ok {
+ t.Fatal("empty document")
+ }
+ files[i].doc = doc
+ items = append(items, taskspec.TGFileItem{
+ ID: files[i].id, ChatID: localUserID, MessageID: document.ID, Name: files[i].name, Storage: "mock", Path: files[i].name,
+ })
+ }
+ params, _ := json.Marshal(taskspec.TGFile{Items: items})
+ record := &database.TaskRecord{TaskID: taskID, Type: tasktype.TaskTypeTgfiles.String(), Status: "running", Params: string(params)}
+ if err := database.CreateTaskRecord(context.Background(), record); err != nil {
+ t.Fatal(err)
+ }
+ defer database.DeleteTaskRecord(context.Background(), taskID)
+
+ makeElems := func(failAfter int) []batchtfile.TaskElement {
+ elems := make([]batchtfile.TaskElement, 0, len(files))
+ for _, tc := range files {
+ location := &tg.InputDocumentFileLocation{ID: tc.doc.ID, AccessHash: tc.doc.AccessHash, FileReference: tc.doc.FileReference}
+ client := &failingDownloadClient{Client: base, failAfter: failAfter}
+ file := tfilepkg.NewTGFile(location, client, int64(len(tc.content)), tc.name)
+ elem, err := batchtfile.NewTaskElementWithID(tc.id, storage, tc.name, file)
+ if err != nil {
+ t.Fatal(err)
+ }
+ elems = append(elems, *elem)
+ }
+ return elems
+ }
+
+ first := batchtfile.NewBatchTGFileTask(taskID, context.Background(), makeElems(1), nil, false)
+ if err := first.Execute(context.Background()); err == nil {
+ t.Fatal("first batch download unexpectedly succeeded")
+ }
+ for _, tc := range files {
+ part, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, tc.id+"_"+tc.name+".part"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(part); err != nil {
+ t.Fatalf("partial batch cache missing for %s: %v", tc.name, err)
+ }
+ }
+
+ second := batchtfile.NewBatchTGFileTask(taskID, context.Background(), makeElems(0), nil, false)
+ if err := second.Execute(context.Background()); err != nil {
+ t.Fatalf("resumed batch failed: %v", err)
+ }
+ for _, tc := range files {
+ if got := storage.saved(tc.name); !bytes.Equal(got, tc.content) {
+ t.Fatalf("uploaded %s size=%d, want %d", tc.name, len(got), len(tc.content))
+ }
+ cache, err := filepath.Abs(filepath.Join(config.C().Temp.BasePath, tc.id+"_"+tc.name))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(cache); !os.IsNotExist(err) {
+ t.Fatalf("completed batch cache still exists for %s: %v", tc.name, err)
+ }
+ }
+}
diff --git a/e2e/telegram/telegram_test.go b/e2e/telegram/telegram_test.go
new file mode 100644
index 00000000..e57304f0
--- /dev/null
+++ b/e2e/telegram/telegram_test.go
@@ -0,0 +1,141 @@
+//go:build e2e
+
+package telegrame2e
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/gotd/td/tg"
+)
+
+var harness *Harness
+
+func TestMain(m *testing.M) {
+ var err error
+ harness, err = NewHarness()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "setup telegram harness:", err)
+ os.Exit(1)
+ }
+ code := m.Run()
+ harness.Close()
+ os.Exit(code)
+}
+
+func TestBotStartCommand(t *testing.T) {
+ harness.Server.ResetRecords()
+ peer := harness.UserPeer(localUserID)
+ message := harness.Server.AddTextMessage(peer, peer, "/start")
+ if err := harness.Dispatch(harness.MessageUpdate(message)); err != nil {
+ t.Fatalf("dispatch /start: %v", err)
+ }
+ sent := harness.Server.Sent()
+ if len(sent) != 1 {
+ t.Fatalf("sent %d messages, want 1", len(sent))
+ }
+ if !strings.Contains(sent[0].Text, "Save Any Bot") {
+ t.Fatalf("help text = %q", sent[0].Text)
+ }
+}
+
+func TestGroupGate(t *testing.T) {
+ group := &tg.PeerChat{ChatID: groupID}
+ from := harness.UserPeer(localUserID)
+
+ harness.Server.ResetRecords()
+ media := harness.Server.AddDocumentMessage(group, from, "group.txt", []byte("group"))
+ if err := harness.Dispatch(harness.MessageUpdate(media)); err != nil {
+ t.Fatalf("dispatch group media: %v", err)
+ }
+ if sent := harness.Server.Sent(); len(sent) != 0 {
+ t.Fatalf("plain group media produced %d messages", len(sent))
+ }
+
+ command := harness.Server.AddTextMessage(group, from, "/start@testbot")
+ if err := harness.Dispatch(harness.MessageUpdate(command)); err != nil {
+ t.Fatalf("dispatch group command: %v", err)
+ }
+ sent := harness.Server.Sent()
+ if len(sent) != 1 {
+ t.Fatalf("group command sent %d messages, want 1", len(sent))
+ }
+ if _, ok := sent[0].Peer.(*tg.PeerChat); !ok {
+ t.Fatalf("group command peer = %T, want PeerChat", sent[0].Peer)
+ }
+
+ harness.Server.ResetRecords()
+ stranger := harness.UserPeer(10003)
+ unauthorized := harness.Server.AddTextMessage(group, stranger, "/start@testbot")
+ if err := harness.Dispatch(harness.MessageUpdate(unauthorized)); err != nil {
+ t.Fatalf("dispatch unauthorized: %v", err)
+ }
+ if sent := harness.Server.Sent(); len(sent) != 0 {
+ t.Fatalf("unauthorized group command produced %d messages", len(sent))
+ }
+
+ harness.Server.ResetRecords()
+ channel := &tg.PeerChannel{ChannelID: channelID}
+ anonymous := harness.Server.AddDocumentMessage(channel, channel, "anon.txt", []byte("anon"))
+ if err := harness.Dispatch(harness.MessageUpdate(anonymous)); err != nil {
+ t.Fatalf("dispatch anonymous admin: %v", err)
+ }
+ if sent := harness.Server.Sent(); len(sent) != 0 {
+ t.Fatalf("anonymous admin produced %d messages", len(sent))
+ }
+}
+
+func TestSaveTelegramFileToLocal(t *testing.T) {
+ harness.Server.ResetRecords()
+ if err := harness.ClearLocal(); err != nil {
+ t.Fatalf("clear local storage: %v", err)
+ }
+ peer := harness.UserPeer(localUserID)
+ content := []byte("hello from telegram\n")
+ document := harness.Server.AddDocumentMessage(peer, peer, "hello.txt", content)
+ command := harness.Server.AddTextMessage(peer, peer, "/save")
+ command.SetReplyTo(&tg.MessageReplyHeader{ReplyToMsgID: document.ID})
+
+ update := harness.CommandUpdate(command, document)
+ if err := harness.Dispatch(update); err != nil {
+ t.Fatalf("dispatch /save: %v", err)
+ }
+ got, err := harness.WaitLocalFile(20 * time.Second)
+ if err != nil {
+ t.Fatalf("wait for saved file: %v", err)
+ }
+ if !bytes.Equal(got, content) {
+ t.Fatalf("saved content = %q, want %q", got, content)
+ }
+ if edits := harness.Server.Edits(); len(edits) == 0 {
+ t.Fatal("task produced no progress edits")
+ }
+}
+
+func TestSaveTelegramFileToTelegramStorage(t *testing.T) {
+ harness.Server.ResetRecords()
+ peer := harness.UserPeer(tgUserID)
+ content := []byte("uploaded to telegram storage\n")
+ document := harness.Server.AddDocumentMessage(peer, peer, "tg-upload.txt", content)
+ command := harness.Server.AddTextMessage(peer, peer, "/save")
+ command.SetReplyTo(&tg.MessageReplyHeader{ReplyToMsgID: document.ID})
+
+ update := harness.CommandUpdate(command, document)
+ if err := harness.Dispatch(update); err != nil {
+ t.Fatalf("dispatch /save: %v", err)
+ }
+ got, err := harness.WaitUploaded(20 * time.Second)
+ if err != nil {
+ t.Fatalf("wait for telegram upload: %v", err)
+ }
+ if !bytes.Equal(got, content) {
+ t.Fatalf("uploaded content = %q, want %q", got, content)
+ }
+ if sent := harness.Server.Sent(); len(sent) == 0 {
+ t.Fatal("telegram storage did not send a media message")
+ }
+}
diff --git a/entrypoint.sh b/entrypoint.sh
old mode 100644
new mode 100755
index 4dada807..6793a947
--- a/entrypoint.sh
+++ b/entrypoint.sh
@@ -1,13 +1,3 @@
#!/bin/sh
-if [ -n "$CONFIG_URL" ]; then
- echo "[INFO] Downloading config from $CONFIG_URL"
- if curl -sSLo /app/config.toml "$CONFIG_URL"; then
- echo "[INFO] Configuration downloaded successfully"
- else
- echo "[ERROR] Failed to download config from $CONFIG_URL"
- exit 1
- fi
-fi
-
-exec /app/saveany-bot
\ No newline at end of file
+exec /app/saveany-bot
diff --git a/go.mod b/go.mod
index d42171e8..0212c2ca 100644
--- a/go.mod
+++ b/go.mod
@@ -1,34 +1,33 @@
module github.com/krau/SaveAny-Bot
-go 1.25.0
+go 1.27.0
require (
github.com/blang/semver v3.5.1+incompatible
- github.com/celestix/gotgproto v1.0.0-beta22
github.com/cenkalti/backoff/v4 v4.3.0
github.com/charmbracelet/log v1.0.0
github.com/gabriel-vasile/mimetype v1.4.15
github.com/goccy/go-yaml v1.19.2
github.com/gotd/contrib v0.21.1
- github.com/gotd/td v0.149.0
+ github.com/gotd/td v0.163.0
github.com/johannesboyne/gofakes3 v0.0.0-20250916175020-ebf3e50324d3
github.com/krau/ffmpeg-go v0.6.0
github.com/lrstanley/go-ytdlp v1.3.5
- github.com/minio/minio-go/v7 v7.2.1
github.com/playwright-community/playwright-go v0.6000.0
github.com/rs/xid v1.6.0
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0
github.com/unvgo/ghselfupdate v1.0.1
github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c
+ golang.org/x/crypto v0.54.0
golang.org/x/net v0.57.0
- golang.org/x/term v0.45.0
golang.org/x/time v0.15.0
)
require (
github.com/AnimeKaizoku/cacher v1.0.3 // indirect
github.com/ProtonMail/go-crypto v1.4.1 // indirect
+ github.com/andybalholm/brotli v1.2.2 // indirect
github.com/aws/smithy-go v1.24.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
@@ -62,34 +61,30 @@ require (
github.com/google/pprof v0.0.0-20260802141513-ef3492d7dac3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gotd/ige v0.3.0 // indirect
+ github.com/gotd/log v0.1.0 // indirect
+ github.com/gotd/log/logzap v0.1.1 // indirect
github.com/gotd/neo v0.1.5 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
- github.com/klauspost/cpuid/v2 v2.4.0 // indirect
- github.com/klauspost/crc32 v1.3.0 // indirect
github.com/lucasb-eyer/go-colorful v1.4.1 // indirect
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mattn/go-runewidth v0.0.27 // indirect
- github.com/minio/crc64nvme v1.1.1 // indirect
- github.com/minio/md5-simd v1.1.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/ncruces/go-sqlite3-wasm/v3 v3.2.35304 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/ncruces/julianday v1.0.0 // indirect
github.com/ogen-go/ogen v1.24.0 // indirect
- github.com/philhofer/fwd v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
+ github.com/refraction-networking/utls v1.8.2 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect
github.com/segmentio/asm v1.2.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
- github.com/tinylib/msgp v1.6.4 // indirect
github.com/ulikunitz/xz v0.5.16 // indirect
github.com/yuin/goldmark v1.8.5 // indirect
- github.com/zeebo/xxh3 v1.1.0 // indirect
go.mongodb.org/mongo-driver v1.17.9 // indirect
go.opentelemetry.io/otel v1.45.0 // indirect
go.opentelemetry.io/otel/metric v1.45.0 // indirect
@@ -98,10 +93,8 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/zap v1.28.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
- golang.org/x/crypto v0.54.0 // indirect
golang.org/x/mod v0.39.0 // indirect
golang.org/x/tools v0.48.0 // indirect
- gopkg.in/ini.v1 v1.67.3 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.75.3 // indirect
modernc.org/mathutil v1.7.1 // indirect
@@ -118,7 +111,7 @@ require (
github.com/glebarez/sqlite v1.11.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.19.2 // indirect
- github.com/mitchellh/mapstructure v1.5.0
+ github.com/krau/mygotg v0.5.0
github.com/ncruces/go-sqlite3 v0.35.3 // indirect
github.com/ncruces/go-sqlite3/gormlite v0.34.0
github.com/nicksnyder/go-i18n/v2 v2.6.1
diff --git a/go.sum b/go.sum
index b444cb20..467fc826 100644
--- a/go.sum
+++ b/go.sum
@@ -6,6 +6,8 @@ github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAw
github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/ProtonMail/go-crypto v1.4.1 h1:9RfcZHqEQUvP8RzecWEUafnZVtEvrBVL9BiF67IQOfM=
github.com/ProtonMail/go-crypto v1.4.1/go.mod h1:e1OaTyu5SYVrO9gKOEhTc+5UcXtTUa+P3uLudwcgPqo=
+github.com/andybalholm/brotli v1.2.2 h1:HzTuoo2ErYQqf5qvcJInB8uvqSVxRttzkFexPWtnceM=
+github.com/andybalholm/brotli v1.2.2/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM=
github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 h1:zAybnyUQXIZ5mok5Jqwlf58/TFE7uvd3IAsa1aF9cXs=
@@ -36,8 +38,6 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
-github.com/celestix/gotgproto v1.0.0-beta22 h1:Iu78cFA08nV8+flmxKs9CJ3W73+HG30fx0nLOs5A6fI=
-github.com/celestix/gotgproto v1.0.0-beta22/go.mod h1:JYC9Js/5KLUhFR5M2RslQi2DFAcF7EdrgJMXo0YrzGQ=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -140,10 +140,14 @@ github.com/gotd/contrib v0.21.1 h1:NSF+0YEnosQ34QEo2o4s6MA5YFDAor1LVvLhN1L3H1M=
github.com/gotd/contrib v0.21.1/go.mod h1:trVJBP9Q/TJbjmJbVnLc0cnX/8T4N0RpQBULVa3BNnE=
github.com/gotd/ige v0.3.0 h1:4f6LEHWsVDLBG0bT9wWG2/9TZb5aWm265G8ZlTXmRRU=
github.com/gotd/ige v0.3.0/go.mod h1:FE9bTaQtvfArizAcZuI4sS6gXaEUBmixdUufVHoCKac=
+github.com/gotd/log v0.1.0 h1:4LJUEvafD1xtBwx2QkrlzFnRgbYXTlWqJPDi8BvrLbU=
+github.com/gotd/log v0.1.0/go.mod h1:5ilhdu1Ux0QvDY/FF3Ojfw24Ws3SlCtyLwOpXy8KYXs=
+github.com/gotd/log/logzap v0.1.1 h1:O6l7d8HUbODe+UMcrM47eXYDwdJ6RNmpQejLjrlcEIQ=
+github.com/gotd/log/logzap v0.1.1/go.mod h1:5ObZkITbfhbsBOLzBkzmMk9QxXc0eNQpimau7zRL+Y8=
github.com/gotd/neo v0.1.5 h1:oj0iQfMbGClP8xI59x7fE/uHoTJD7NZH9oV1WNuPukQ=
github.com/gotd/neo v0.1.5/go.mod h1:9A2a4bn9zL6FADufBdt7tZt+WMhvZoc5gWXihOPoiBQ=
-github.com/gotd/td v0.149.0 h1:vXzNO99FFzWKKyI0vizvtgkpI7AqmbcXHLWjxkD+69o=
-github.com/gotd/td v0.149.0/go.mod h1:+s0fRWlKL+RqoKJAMnCRoj0sWtxzRlWm886elNZEpJQ=
+github.com/gotd/td v0.163.0 h1:A/QHpyMmYaQgyOY//vySoKa1wQP8NCAo3/5OcgfZVnU=
+github.com/gotd/td v0.163.0/go.mod h1:ZsGbErIos7XHF7HL0VlsjL6mmX4b1JpinHfDb7gOdvs=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
@@ -158,17 +162,14 @@ github.com/johannesboyne/gofakes3 v0.0.0-20250916175020-ebf3e50324d3 h1:2713fQZ5
github.com/johannesboyne/gofakes3 v0.0.0-20250916175020-ebf3e50324d3/go.mod h1:S4S9jGBVlLri0OeqrSSbCGG5vsI6he06UJyuz1WT1EE=
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
-github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
-github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
-github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
-github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
-github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/krau/ffmpeg-go v0.6.0 h1:F4HWvOrKXQsfLsFTOnUfP0HY6WISJqOrsAFGSIzkKto=
github.com/krau/ffmpeg-go v0.6.0/go.mod h1:sa7/bWHB6fO9j4lhmxnWQ1U07o+dE1leFjhctotxU7A=
+github.com/krau/mygotg v0.5.0 h1:UC3v+ICukxhvoxTzsRXktNa01QWJH+SkYfXjqJcakIo=
+github.com/krau/mygotg v0.5.0/go.mod h1:djnRqzJcRjS4pX8iUrhDPwGxiQI4vdArzSMCL6XlckU=
github.com/lrstanley/go-ytdlp v1.3.5 h1:eT+29mK3Lp+XPMQOH25+jVerrrjifYW1o3IkTYJ9SMs=
github.com/lrstanley/go-ytdlp v1.3.5/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
@@ -181,14 +182,6 @@ github.com/mattn/go-runewidth v0.0.27 h1:Feg/Oou5zI/wnpgDF6omIU0OokC9GxLC/WRknhV
github.com/mattn/go-runewidth v0.0.27/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
-github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
-github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
-github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
-github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
-github.com/minio/minio-go/v7 v7.2.1 h1:PfBfwvKB/MmqyN8Vb1G9voWisaM9OrLv+WwOvMwS9Dw=
-github.com/minio/minio-go/v7 v7.2.1/go.mod h1:EU9hENAStx/xXduNdrGO5e4X5vk19NtgB+RIPjZO8o0=
-github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
-github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/ncruces/go-sqlite3 v0.35.3 h1:Ei07Zv1qfV/vyXzelhFsyS5Oh9TArBZHsmFk14Xv3GY=
@@ -207,14 +200,14 @@ github.com/ogen-go/ogen v1.24.0 h1:NehBG/8s0JeM6jYHPJeFJntBG3cE/xQgZd1q8BchPNM=
github.com/ogen-go/ogen v1.24.0/go.mod h1:hcg4aTzLcu/MS1SGGahUriPBEOZB3c82RGIC6dYcdFU=
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
-github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
-github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/playwright-community/playwright-go v0.6000.0 h1:R7sENcRI6n0Zd5ZoW8EKdVF1ZVJgvTubfJeqKHNGvsw=
github.com/playwright-community/playwright-go v0.6000.0/go.mod h1:z/YpFVdU4LAi+0f9VPOCkGvmdH6dCrtza9nxnXFXgiE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo=
+github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
@@ -244,35 +237,25 @@ github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3A
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
-github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
-github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
-github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
-github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
-github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/ulikunitz/xz v0.5.16 h1:ld6NyySjx5lowVKwJvMRLnW5nxKX/xnpSiFYZ/Lxur0=
github.com/ulikunitz/xz v0.5.16/go.mod h1:H9Rt/W6/Qj27PGauhQc6nfCDy7vHpzsOThBSaYDoEhw=
github.com/unvgo/ghselfupdate v1.0.1 h1:4clbOkfPbfEmRnnYxVXDSBs0JG12DO+0FfqplJckreU=
github.com/unvgo/ghselfupdate v1.0.1/go.mod h1:3snWV5vEHGXQqqhY7FwKjPOtH6e7cFdHYN7UMAihhxs=
github.com/xo/terminfo v1.0.0 h1:2ZpYzqWzyyytjk3TP6aJVDhkMAkc99/1xKQdA3TDTBY=
github.com/xo/terminfo v1.0.0/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
+github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
+github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c h1:xA2TJS9Hu/ivzaZIrDcwvpJ3Fnpsk5fDOJ4iSnL6J0w=
github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.8.5 h1:r6N5afV5qj/5S4UTch8agZHJ8UxNCMwX7WjkkJam2NA=
github.com/yuin/goldmark v1.8.5/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
-github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
-github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
-github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
-github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.etcd.io/bbolt v1.4.3 h1:dEadXpI6G79deX5prL3QRNP6JB8UxVkqo4UPnHaNXJo=
go.etcd.io/bbolt v1.4.3/go.mod h1:tKQlpPaYCVFctUIgFKFnAlvbmB3tpy1vkTnDWohtc0E=
go.mongodb.org/mongo-driver v1.17.9 h1:IexDdCuuNJ3BHrELgBlyaH9p60JXAvdzWR128q+U5tU=
@@ -339,8 +322,6 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
-golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0=
-golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
@@ -362,8 +343,6 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
-gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
-gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/main.go b/main.go
index 8a52a00b..6b8dac36 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,7 @@ import (
"context"
"os"
"os/signal"
+ "syscall"
"github.com/krau/SaveAny-Bot/cmd"
)
@@ -11,7 +12,7 @@ import (
//go:generate go run cmd/geni18n/main.go -dir ./common/i18n/locale -out common/i18n/i18nk/keys.go -pkg i18nk
func main() {
- ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+ ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
cmd.Execute(ctx)
}
diff --git a/parsers/js/api.go b/parsers/js/api.go
index 1ae6597a..a900ad34 100644
--- a/parsers/js/api.go
+++ b/parsers/js/api.go
@@ -18,38 +18,47 @@ import (
func jsRegisterParser(vm *goja.Runtime) func(call goja.FunctionCall) goja.Value {
return func(call goja.FunctionCall) goja.Value {
+ throw := func(err error) {
+ panic(vm.NewGoError(err))
+ }
jsObj := call.Argument(0)
if jsObj == nil || goja.IsUndefined(jsObj) || goja.IsNull(jsObj) {
- return vm.NewGoError(errors.New("registerParser expects an object { canHandle, parse }"))
+ throw(errors.New("registerParser expects an object { canHandle, parse }"))
}
obj := jsObj.ToObject(vm)
if obj == nil {
- return vm.NewGoError(errors.New("registerParser expects an object { canHandle, parse }"))
+ throw(errors.New("registerParser expects an object { canHandle, parse }"))
}
metaValue := obj.Get("metadata")
if metaValue == nil || goja.IsUndefined(metaValue) {
- return vm.NewGoError(errors.New("parser must provide metadata"))
+ throw(errors.New("parser must provide metadata"))
}
var metadata PluginMeta
if exported := metaValue.Export(); exported != nil {
data, err := json.Marshal(exported)
if err != nil {
- return vm.NewGoError(fmt.Errorf("failed to marshal metadata to JSON: %w", err))
+ throw(fmt.Errorf("failed to marshal metadata to JSON: %w", err))
}
if err := json.Unmarshal(data, &metadata); err != nil {
- return vm.NewGoError(fmt.Errorf("failed to unmarshal JSON to PluginMeta: %w", err))
+ throw(fmt.Errorf("failed to unmarshal JSON to PluginMeta: %w", err))
}
} else {
- return vm.NewGoError(errors.New("metadata cannot be null or undefined"))
+ throw(errors.New("metadata cannot be null or undefined"))
}
+ if metadata.APIVersion == "" {
+ metadata.APIVersion = SupportedAPIVersion
+ }
+ if metadata.APIVersion != SupportedAPIVersion {
+ throw(fmt.Errorf("parser apiVersion %s is not supported, expected %s", metadata.APIVersion, SupportedAPIVersion))
+ }
pluginV, err := semver.Parse(metadata.Version)
if err != nil {
- return vm.NewGoError(fmt.Errorf("invalid parser version %q: %w", metadata.Version, err))
+ throw(fmt.Errorf("invalid parser version %q: %w", metadata.Version, err))
}
if pluginV.LT(MinimumParserVersion) {
- return vm.NewGoError(fmt.Errorf("parser version %s is not supported, must be at least %s", metadata.Version, MinimumParserVersion))
+ throw(fmt.Errorf("parser version %s is not supported, must be at least %s", metadata.Version, MinimumParserVersion))
}
if pluginV.Major > LatestParserVersion.Major {
log.Printf("warning: parser major version %d is newer than latest supported major version %d", pluginV.Major, LatestParserVersion.Major)
@@ -58,10 +67,10 @@ func jsRegisterParser(vm *goja.Runtime) func(call goja.FunctionCall) goja.Value
handleFn := obj.Get("canHandle")
parseFn := obj.Get("parse")
if parseFn == nil || goja.IsUndefined(parseFn) {
- return vm.NewGoError(errors.New("parser must provide a parse function"))
+ throw(errors.New("parser must provide a parse function"))
}
if handleFn == nil || goja.IsUndefined(handleFn) {
- return vm.NewGoError(errors.New("parser must provide a canHandle function"))
+ throw(errors.New("parser must provide a canHandle function"))
}
parsers.Add(newJSParser(vm, handleFn, parseFn, metadata))
return goja.Undefined()
diff --git a/parsers/js/js.go b/parsers/js/js.go
index 4ec0b806..7e1cc782 100644
--- a/parsers/js/js.go
+++ b/parsers/js/js.go
@@ -5,6 +5,7 @@ package js
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -24,6 +25,7 @@ type jsParser struct {
}
type jsParserReq struct {
+ ctx context.Context
method ParserMethod
url string
respCh chan jsParserResp
@@ -35,33 +37,39 @@ type jsParserResp struct {
err error
}
-const canHandleTimeout = 10 * time.Second
+const (
+ canHandleTimeout = 10 * time.Second
+ parseTimeout = 2 * time.Minute
+)
func (p *jsParser) CanHandle(url string) bool {
- respCh := make(chan jsParserResp, 1)
- timer := time.NewTimer(canHandleTimeout)
- defer timer.Stop()
- select {
- case p.reqCh <- jsParserReq{method: ParserMethodCanHandle, url: url, respCh: respCh}:
- case <-timer.C:
- return false
- }
- select {
- case resp := <-respCh:
- return resp.ok && resp.err == nil
- case <-timer.C:
- return false
- }
+ ctx, cancel := context.WithTimeout(context.Background(), canHandleTimeout)
+ defer cancel()
+ resp, ok := p.request(ctx, ParserMethodCanHandle, url)
+ return ok && resp.err == nil && resp.ok
}
func (p *jsParser) Parse(ctx context.Context, url string) (*parser.Item, error) {
+ resp, ok := p.request(ctx, ParserMethodParse, url)
+ if !ok {
+ return nil, ctx.Err()
+ }
+ return resp.item, resp.err
+}
+
+func (p *jsParser) request(ctx context.Context, method ParserMethod, url string) (jsParserResp, bool) {
respCh := make(chan jsParserResp, 1)
- p.reqCh <- jsParserReq{method: ParserMethodParse, url: url, respCh: respCh}
+ req := jsParserReq{ctx: ctx, method: method, url: url, respCh: respCh}
+ select {
+ case p.reqCh <- req:
+ case <-ctx.Done():
+ return jsParserResp{}, false
+ }
select {
case resp := <-respCh:
- return resp.item, resp.err
+ return resp, true
case <-ctx.Done():
- return nil, ctx.Err()
+ return jsParserResp{}, false
}
}
@@ -71,66 +79,84 @@ func newJSParser(vm *goja.Runtime, canHandleFunc, parseFunc goja.Value, metadata
reqCh: make(chan jsParserReq, 10),
meta: metadata,
}
-
go func() {
for req := range p.reqCh {
- func() {
- defer func() {
- if r := recover(); r != nil {
- log.Errorf("JS parser %q panicked while handling method %d: %v", p.meta.Name, req.method, r)
- req.respCh <- jsParserResp{err: fmt.Errorf("JS parser %q panicked: %v", p.meta.Name, r)}
- }
- }()
- switch req.method {
- case ParserMethodCanHandle:
- fn, ok := goja.AssertFunction(canHandleFunc)
- if !ok {
- req.respCh <- jsParserResp{err: fmt.Errorf("canHandle is not a function")}
- return
- }
- res, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
- if err != nil {
- req.respCh <- jsParserResp{ok: false, err: err}
- return
- }
- req.respCh <- jsParserResp{ok: res.ToBoolean()}
- case ParserMethodParse:
- fn, ok := goja.AssertFunction(parseFunc)
- if !ok {
- req.respCh <- jsParserResp{err: fmt.Errorf("parse is not a function")}
- return
- }
- result, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
- if err != nil {
- req.respCh <- jsParserResp{err: err}
- return
- }
-
- var item parser.Item
- if exported := result.Export(); exported != nil {
- data, err := json.Marshal(exported)
- if err != nil {
- req.respCh <- jsParserResp{err: fmt.Errorf("failed to marshal result to JSON: %w", err)}
- return
- }
-
- if err := json.Unmarshal(data, &item); err != nil {
- req.respCh <- jsParserResp{err: fmt.Errorf("failed to unmarshal JSON to Item: %w", err)}
- return
- }
- } else {
- req.respCh <- jsParserResp{err: fmt.Errorf("JS function returned null or undefined")}
- return
- }
- req.respCh <- jsParserResp{item: &item}
- }
- }()
+ resp := p.handle(req, canHandleFunc, parseFunc)
+ select {
+ case req.respCh <- resp:
+ case <-req.ctx.Done():
+ }
}
}()
-
return p
}
+func (p *jsParser) handle(req jsParserReq, canHandleFunc, parseFunc goja.Value) (resp jsParserResp) {
+ defer func() {
+ if recovered := recover(); recovered != nil {
+ log.Errorf("JS parser %q panicked while handling method %d: %v", p.meta.Name, req.method, recovered)
+ resp = jsParserResp{err: fmt.Errorf("JS parser %q panicked: %v", p.meta.Name, recovered)}
+ }
+ }()
+
+ execCtx, cancel := context.WithTimeout(req.ctx, parseTimeout)
+ defer cancel()
+ done := make(chan struct{})
+ defer p.vm.ClearInterrupt()
+ defer close(done)
+ go func() {
+ select {
+ case <-execCtx.Done():
+ p.vm.Interrupt(execCtx.Err())
+ case <-done:
+ }
+ }()
+
+ switch req.method {
+ case ParserMethodCanHandle:
+ fn, ok := goja.AssertFunction(canHandleFunc)
+ if !ok {
+ return jsParserResp{err: errors.New("canHandle is not a function")}
+ }
+ result, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
+ if err != nil {
+ return jsParserResp{ok: false, err: jsCallError(execCtx, p.meta.Name, err)}
+ }
+ return jsParserResp{ok: result.ToBoolean()}
+ case ParserMethodParse:
+ fn, ok := goja.AssertFunction(parseFunc)
+ if !ok {
+ return jsParserResp{err: errors.New("parse is not a function")}
+ }
+ result, err := fn(goja.Undefined(), p.vm.ToValue(req.url))
+ if err != nil {
+ return jsParserResp{err: jsCallError(execCtx, p.meta.Name, err)}
+ }
+ exported := result.Export()
+ if exported == nil {
+ return jsParserResp{err: errors.New("JS function returned null or undefined")}
+ }
+ data, err := json.Marshal(exported)
+ if err != nil {
+ return jsParserResp{err: fmt.Errorf("failed to marshal result to JSON: %w", err)}
+ }
+ var item parser.Item
+ if err := json.Unmarshal(data, &item); err != nil {
+ return jsParserResp{err: fmt.Errorf("failed to unmarshal JSON to Item: %w", err)}
+ }
+ return jsParserResp{item: &item}
+ default:
+ return jsParserResp{err: fmt.Errorf("unknown JS parser method %d", req.method)}
+ }
+}
+
+func jsCallError(ctx context.Context, name string, err error) error {
+ if ctxErr := ctx.Err(); ctxErr != nil {
+ return fmt.Errorf("JS parser %q stopped: %w", name, ctxErr)
+ }
+ return err
+}
+
func LoadPlugins(ctx context.Context, dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
diff --git a/parsers/js/plugin.go b/parsers/js/plugin.go
index 93c76370..c3d88333 100644
--- a/parsers/js/plugin.go
+++ b/parsers/js/plugin.go
@@ -7,11 +7,13 @@ import "github.com/blang/semver"
var (
LatestParserVersion = semver.MustParse("1.0.0")
MinimumParserVersion = semver.MustParse("1.0.0")
+ SupportedAPIVersion = "1"
)
type PluginMeta struct {
Name string `json:"name"`
Version string `json:"version"`
+ APIVersion string `json:"apiVersion,omitempty"`
Description string `json:"description"`
Author string `json:"author"`
}
diff --git a/parsers/native/twitter/parser.go b/parsers/native/twitter/parser.go
index 231b198a..dbc38bd0 100644
--- a/parsers/native/twitter/parser.go
+++ b/parsers/native/twitter/parser.go
@@ -9,12 +9,14 @@ import (
"path"
"regexp"
"strings"
+ "sync"
"github.com/krau/SaveAny-Bot/common/utils/netutil"
"github.com/krau/SaveAny-Bot/pkg/parser"
)
type TwitterParser struct {
+ mu sync.RWMutex
client http.Client
apiDomain string
}
@@ -42,12 +44,19 @@ func (p *TwitterParser) Parse(ctx context.Context, u string) (*parser.Item, erro
if id == "" {
return nil, errors.New("invalid Twitter URL")
}
- apiUrl := fmt.Sprintf("https://%s/_/status/%s", p.apiDomain, id)
+ p.mu.RLock()
+ client := p.client
+ apiDomain := p.apiDomain
+ p.mu.RUnlock()
+ if apiDomain == "" {
+ apiDomain = fxTwitterApi
+ }
+ apiUrl := fmt.Sprintf("https://%s/_/status/%s", apiDomain, id)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUrl, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request to Twitter API: %w", err)
}
- resp, err := p.client.Do(req)
+ resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch Twitter API: %w", err)
}
@@ -68,7 +77,7 @@ func (p *TwitterParser) Parse(ctx context.Context, u string) (*parser.Item, erro
resources := make([]parser.Resource, 0, len(fxResp.Tweet.Media.All))
for _, media := range fxResp.Tweet.Media.All {
var size int64
- resp, err := p.client.Head(media.URL)
+ resp, err := client.Head(media.URL)
if err == nil {
size = resp.ContentLength
resp.Body.Close()
@@ -101,22 +110,23 @@ func (p *TwitterParser) Name() string {
}
func (p *TwitterParser) Configure(config map[string]any) error {
- if config == nil {
- p.apiDomain = fxTwitterApi
- p.client = *netutil.DefaultParserHTTPClient()
- return nil
- }
- if domain, ok := config["api_domain"].(string); ok && domain != "" {
- p.apiDomain = domain
- } else {
- p.apiDomain = fxTwitterApi
- }
- if proxyUrl, ok := config["proxy"].(string); ok && proxyUrl != "" {
- proxyClient, err := netutil.NewProxyHTTPClient(proxyUrl)
- if err != nil {
- return fmt.Errorf("failed to create proxy client: %w", err)
+ domain := fxTwitterApi
+ client := *netutil.DefaultParserHTTPClient()
+ if config != nil {
+ if configured, ok := config["api_domain"].(string); ok && configured != "" {
+ domain = configured
+ }
+ if proxyURL, ok := config["proxy"].(string); ok && proxyURL != "" {
+ proxyClient, err := netutil.NewProxyHTTPClient(proxyURL)
+ if err != nil {
+ return fmt.Errorf("failed to create proxy client: %w", err)
+ }
+ client = *proxyClient
}
- p.client = *proxyClient
}
+ p.mu.Lock()
+ p.apiDomain = domain
+ p.client = client
+ p.mu.Unlock()
return nil
}
diff --git a/parsers/parser.go b/parsers/parser.go
index 2d448238..c62f793f 100644
--- a/parsers/parser.go
+++ b/parsers/parser.go
@@ -59,6 +59,11 @@ func CanHandle(url string) (bool, parser.Parser) {
return false, nil
}
+// Configure reapplies parser settings after a settings or plugin reload.
+func Configure() {
+ parsers.Configure()
+}
+
func LoadPlugins(ctx context.Context, dir string) error {
return js.LoadPlugins(ctx, dir)
}
diff --git a/parsers/parsers/parsers.go b/parsers/parsers/parsers.go
index b3c15a33..ab93e6ff 100644
--- a/parsers/parsers/parsers.go
+++ b/parsers/parsers/parsers.go
@@ -1,33 +1,17 @@
package parsers
import (
- "fmt"
"slices"
"sync"
+ "github.com/charmbracelet/log"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/pkg/parser"
)
var (
- parsers []parser.Parser
- mu sync.Mutex
- configOnce sync.Once
- configParsers = func() {
- mu.Lock()
- defer mu.Unlock()
- if len(parsers) == 0 {
- return
- }
- for _, pser := range parsers {
- if configurable, ok := pser.(parser.ConfigurableParser); ok {
- cfg := config.C().GetParserConfigByName(configurable.Name())
- if err := configurable.Configure(cfg); err != nil {
- fmt.Printf("Error configuring parser %s: %v\n", configurable.Name(), err)
- }
- }
- }
- }
+ parsers []parser.Parser
+ mu sync.Mutex
)
func Add(p ...parser.Parser) {
@@ -36,9 +20,28 @@ func Add(p ...parser.Parser) {
parsers = append(parsers, p...)
}
+// Get returns a snapshot of the registered parsers without reconfiguring them.
+// Call Configure after changing parser settings or loading plugins.
func Get() []parser.Parser {
- configOnce.Do(configParsers)
mu.Lock()
defer mu.Unlock()
return slices.Clone(parsers)
}
+
+// Configure applies the current parser settings to every configurable parser.
+// It is safe to call at startup and after a settings reload.
+func Configure() {
+ mu.Lock()
+ snapshot := slices.Clone(parsers)
+ mu.Unlock()
+ for _, pser := range snapshot {
+ configurable, ok := pser.(parser.ConfigurableParser)
+ if !ok {
+ continue
+ }
+ cfg := config.C().GetParserConfigByName(configurable.Name())
+ if err := configurable.Configure(cfg); err != nil {
+ log.Errorf("Failed to configure parser %s: %v", configurable.Name(), err)
+ }
+ }
+}
diff --git a/pkg/aria2/client.go b/pkg/aria2/client.go
index 43792daa..215ea33d 100644
--- a/pkg/aria2/client.go
+++ b/pkg/aria2/client.go
@@ -9,6 +9,7 @@ import (
"io"
"net/http"
"sync/atomic"
+ "time"
)
var (
@@ -125,7 +126,14 @@ func NewClient(url, secret string) (*Client, error) {
return &Client{
url: url,
secret: secret,
- client: &http.Client{},
+ client: &http.Client{
+ Transport: &http.Transport{
+ Proxy: http.ProxyFromEnvironment,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ResponseHeaderTimeout: 30 * time.Second,
+ IdleConnTimeout: 90 * time.Second,
+ },
+ },
}, nil
}
@@ -175,13 +183,13 @@ func (c *Client) call(ctx context.Context, method string, params []any, result a
resp, err := c.client.Do(httpReq)
if err != nil {
- return fmt.Errorf("%w: failed to send request: %v", ErrRPCFailed, err)
+ return fmt.Errorf("%w: failed to send request: %w", ErrRPCFailed, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
- return fmt.Errorf("%w: failed to read response: %v", ErrRPCFailed, err)
+ return fmt.Errorf("%w: failed to read response: %w", ErrRPCFailed, err)
}
if resp.StatusCode != http.StatusOK {
diff --git a/pkg/aria2/client_test.go b/pkg/aria2/client_test.go
index fab13114..e887720b 100644
--- a/pkg/aria2/client_test.go
+++ b/pkg/aria2/client_test.go
@@ -258,8 +258,7 @@ func TestClient_RPCError(t *testing.T) {
t.Error("Expected RPC error, got nil")
}
- var rpcErr *rpcError
- if !errors.As(err, &rpcErr) {
+ if _, ok := errors.AsType[*rpcError](err); !ok {
t.Errorf("Expected rpcError, got %T", err)
}
}
diff --git a/pkg/enums/fnamest/filename_srategy.go b/pkg/enums/fnamest/filename_srategy.go
index 8dfb34fd..42591fc6 100644
--- a/pkg/enums/fnamest/filename_srategy.go
+++ b/pkg/enums/fnamest/filename_srategy.go
@@ -1,5 +1,7 @@
package fnamest
+import "strings"
+
//go:generate go-enum --values --names --noprefix --flag --nocase
// FnameST
@@ -9,16 +11,21 @@ default, message, template
type FnameST string
var fnameSTDisplay = map[FnameST]map[string]string{
- Default: {"zh-CN": "默认", "en": "Default"},
- Message: {"zh-CN": "优先从消息生成", "en": "Gen From Msg First"},
- Template: {"zh-CN": "自定义模板", "en": "Template"},
+ Default: {"zh-Hans": "默认", "en": "Default"},
+ Message: {"zh-Hans": "优先从消息生成", "en": "Gen From Msg First"},
+ Template: {"zh-Hans": "自定义模板", "en": "Template"},
}
func GetDisplay(st FnameST, lang string) string {
- if display, ok := fnameSTDisplay[st]; ok {
- if str, ok := display[lang]; ok {
- return str
- }
+ display, ok := fnameSTDisplay[st]
+ if !ok {
+ return string(st)
+ }
+ if str, ok := display[lang]; ok {
+ return str
+ }
+ if strings.HasPrefix(strings.ToLower(lang), "zh") {
+ return display["zh-Hans"]
}
- return fnameSTDisplay[st]["en"]
+ return display["en"]
}
diff --git a/pkg/enums/storage/storages.go b/pkg/enums/storage/storages.go
index 8d0f6994..3896256d 100644
--- a/pkg/enums/storage/storages.go
+++ b/pkg/enums/storage/storages.go
@@ -4,6 +4,6 @@ package storage
// StorageType
/* ENUM(
-local, webdav, alist, minio, telegram, s3, rclone
+local, webdav, alist, telegram, s3, rclone
) */
type StorageType string
diff --git a/pkg/enums/storage/storages_enum.go b/pkg/enums/storage/storages_enum.go
index 4e578fc2..af81a7bb 100644
--- a/pkg/enums/storage/storages_enum.go
+++ b/pkg/enums/storage/storages_enum.go
@@ -1,8 +1,7 @@
// Code generated by go-enum DO NOT EDIT.
-// Version: 0.9.1
-// Revision: 42b1ed55945781de07471bb2db52b3f9edee19b0
-// Build Date: 2025-08-02T17:25:40Z
-// Built By: goreleaser
+// Version: v0.9.1
+
+// Built By: go install
package storage
@@ -18,8 +17,6 @@ const (
Webdav StorageType = "webdav"
// Alist is a StorageType of type alist.
Alist StorageType = "alist"
- // Minio is a StorageType of type minio.
- Minio StorageType = "minio"
// Telegram is a StorageType of type telegram.
Telegram StorageType = "telegram"
// S3 is a StorageType of type s3.
@@ -34,7 +31,6 @@ var _StorageTypeNames = []string{
string(Local),
string(Webdav),
string(Alist),
- string(Minio),
string(Telegram),
string(S3),
string(Rclone),
@@ -53,7 +49,6 @@ func StorageTypeValues() []StorageType {
Local,
Webdav,
Alist,
- Minio,
Telegram,
S3,
Rclone,
@@ -76,7 +71,6 @@ var _StorageTypeValue = map[string]StorageType{
"local": Local,
"webdav": Webdav,
"alist": Alist,
- "minio": Minio,
"telegram": Telegram,
"s3": S3,
"rclone": Rclone,
diff --git a/pkg/enums/storagefilter/storagefilter.go b/pkg/enums/storagefilter/storagefilter.go
new file mode 100644
index 00000000..524ee779
--- /dev/null
+++ b/pkg/enums/storagefilter/storagefilter.go
@@ -0,0 +1,24 @@
+package storagefilter
+
+// Mode controls how a user's storage list is interpreted.
+type Mode string
+
+const (
+ // Blacklist lets the user access every storage except the listed names.
+ Blacklist Mode = "blacklist"
+ // Whitelist lets the user access only the listed storage names.
+ Whitelist Mode = "whitelist"
+)
+
+func (m Mode) Valid() bool {
+ return m == Blacklist || m == Whitelist
+}
+
+// Normalize returns a safe mode, defaulting to Blacklist so new users can use
+// every configured storage.
+func (m Mode) Normalize() Mode {
+ if m == Whitelist {
+ return Whitelist
+ }
+ return Blacklist
+}
diff --git a/pkg/enums/taskstatus/taskstatus.go b/pkg/enums/taskstatus/taskstatus.go
new file mode 100644
index 00000000..94e2670a
--- /dev/null
+++ b/pkg/enums/taskstatus/taskstatus.go
@@ -0,0 +1,30 @@
+// Package taskstatus defines the persisted and public task status vocabulary.
+package taskstatus
+
+// Status is the lifecycle state of a task.
+type Status string
+
+const (
+ Queued Status = "queued"
+ Running Status = "running"
+ Completed Status = "completed"
+ Partial Status = "partial"
+ Failed Status = "failed"
+ Cancelled Status = "cancelled"
+)
+
+func (s Status) String() string { return string(s) }
+
+// IsTerminal reports whether the status is a final state.
+func (s Status) IsTerminal() bool {
+ switch s {
+ case Completed, Partial, Failed, Cancelled:
+ return true
+ default:
+ return false
+ }
+}
+
+func Values() []Status {
+ return []Status{Queued, Running, Completed, Partial, Failed, Cancelled}
+}
diff --git a/pkg/mediatype/mediatype.go b/pkg/mediatype/mediatype.go
new file mode 100644
index 00000000..67e9f0a1
--- /dev/null
+++ b/pkg/mediatype/mediatype.go
@@ -0,0 +1,147 @@
+// Package mediatype classifies Telegram media into the coarse types used by
+// the instance-wide media filter.
+package mediatype
+
+import (
+ "fmt"
+ "path"
+ "strings"
+
+ "github.com/gotd/td/tg"
+)
+
+// Type is a user-facing media category. It is intentionally coarser than
+// Telegram's document attributes: every Telegram document falls into one of
+// video, audio, image, or document.
+type Type string
+
+const (
+ Image Type = "image"
+ Video Type = "video"
+ Audio Type = "audio"
+ Document Type = "document"
+)
+
+var allTypes = []Type{Image, Video, Audio, Document}
+
+// Parse normalizes a configured media type. "photo" is accepted as an alias
+// for "image".
+func Parse(value string) (Type, bool) {
+ switch strings.ToLower(strings.TrimSpace(value)) {
+ case "image", "photo":
+ return Image, true
+ case "video":
+ return Video, true
+ case "audio":
+ return Audio, true
+ case "document":
+ return Document, true
+ default:
+ return "", false
+ }
+}
+
+// NormalizeAllowed validates and canonicalizes an allowlist. Empty values are
+// ignored; an empty result means that no filter is active.
+func NormalizeAllowed(values []string) ([]string, error) {
+ out := make([]string, 0, len(values))
+ seen := make(map[Type]struct{}, len(values))
+ for _, value := range values {
+ value = strings.TrimSpace(value)
+ if value == "" {
+ continue
+ }
+ typ, ok := Parse(value)
+ if !ok {
+ return nil, fmt.Errorf("invalid media type %q (allowed: image, video, audio, document)", value)
+ }
+ if _, exists := seen[typ]; exists {
+ continue
+ }
+ seen[typ] = struct{}{}
+ out = append(out, string(typ))
+ }
+ return out, nil
+}
+
+// Classify returns the category of a Telegram media object. It reports false
+// for unsupported media and empty documents.
+func Classify(media tg.MessageMediaClass) (Type, bool) {
+ switch m := media.(type) {
+ case *tg.MessageMediaPhoto:
+ return Image, true
+ case *tg.MessageMediaDocument:
+ document, ok := m.Document.AsNotEmpty()
+ if !ok {
+ return "", false
+ }
+ return classifyDocument(document), true
+ default:
+ return "", false
+ }
+}
+
+func classifyDocument(document *tg.Document) Type {
+ var hasAnimated, hasSticker, hasVideo, hasAudio, hasImage bool
+ for _, attribute := range document.Attributes {
+ switch attribute.(type) {
+ case *tg.DocumentAttributeAnimated:
+ hasAnimated = true
+ case *tg.DocumentAttributeSticker:
+ hasSticker = true
+ case *tg.DocumentAttributeVideo:
+ hasVideo = true
+ case *tg.DocumentAttributeAudio:
+ hasAudio = true
+ case *tg.DocumentAttributeImageSize:
+ hasImage = true
+ }
+ }
+
+ mimeType := strings.ToLower(document.MimeType)
+ switch {
+ case hasSticker || hasAnimated:
+ return Image
+ case hasVideo:
+ return Video
+ case hasAudio:
+ return Audio
+ case hasImage:
+ return Image
+ case strings.HasPrefix(mimeType, "image/"):
+ return Image
+ case strings.HasPrefix(mimeType, "video/"):
+ return Video
+ case strings.HasPrefix(mimeType, "audio/"):
+ return Audio
+ }
+
+ switch extension(documentFileName(document)) {
+ case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg", ".avif", ".heic":
+ return Image
+ case ".mp4", ".mkv", ".ts", ".avi", ".flv", ".mov", ".webm", ".wmv", ".rmvb", ".m2ts":
+ return Video
+ case ".mp3", ".flac", ".wav", ".aac", ".m4a", ".ogg", ".opus":
+ return Audio
+ default:
+ return Document
+ }
+}
+
+func documentFileName(document *tg.Document) string {
+ for _, attribute := range document.Attributes {
+ if name, ok := attribute.(*tg.DocumentAttributeFilename); ok {
+ return name.GetFileName()
+ }
+ }
+ return ""
+}
+
+func extension(name string) string {
+ return strings.ToLower(path.Ext(name))
+}
+
+// All returns the supported filter categories.
+func All() []Type {
+ return append([]Type(nil), allTypes...)
+}
diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go
new file mode 100644
index 00000000..a12fb6cd
--- /dev/null
+++ b/pkg/metadata/metadata.go
@@ -0,0 +1,269 @@
+package metadata
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "time"
+
+ "github.com/gotd/td/constant"
+ "github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/common/utils/strutil"
+ "github.com/krau/SaveAny-Bot/common/utils/tgutil"
+)
+
+// Version is the current normalized message metadata schema.
+const Version = 1
+
+// MetaSuffix is appended to a saved file when sidecar export is enabled.
+const MetaSuffix = ".meta.json"
+
+type ChatInfo struct {
+ ID int64 `json:"id"`
+ Title string `json:"title,omitempty"`
+ Username string `json:"username,omitempty"`
+}
+
+type SenderInfo struct {
+ ID int64 `json:"id,omitempty"`
+ Name string `json:"name,omitempty"`
+ Username string `json:"username,omitempty"`
+}
+
+type ReplyInfo struct {
+ ChatID int64 `json:"chat_id,omitempty"`
+ MessageID int `json:"message_id,omitempty"`
+ TopicID int `json:"topic_id,omitempty"`
+}
+
+type ForwardInfo struct {
+ Date string `json:"date,omitempty"`
+ ChatID int64 `json:"chat_id,omitempty"`
+ ChatTitle string `json:"chat_title,omitempty"`
+ ChatUsername string `json:"chat_username,omitempty"`
+ MessageID int `json:"message_id,omitempty"`
+ Author string `json:"author,omitempty"`
+}
+
+type MessageInfo struct {
+ ID int `json:"id"`
+ Date string `json:"date,omitempty"`
+ EditDate string `json:"edit_date,omitempty"`
+ Chat ChatInfo `json:"chat"`
+ TopicID int `json:"topic_id,omitempty"`
+ Sender *SenderInfo `json:"sender,omitempty"`
+ Text string `json:"text,omitempty"`
+ ReplyTo *ReplyInfo `json:"reply_to,omitempty"`
+ ForwardFrom *ForwardInfo `json:"forward_from,omitempty"`
+ GroupedID int64 `json:"grouped_id,omitempty"`
+ Link string `json:"link,omitempty"`
+}
+
+type FileInfo struct {
+ Name string `json:"name,omitempty"`
+ OriginalName string `json:"original_name,omitempty"`
+ MediaType string `json:"media_type,omitempty"`
+ MimeType string `json:"mime_type,omitempty"`
+ Size int64 `json:"size,omitempty"`
+ Width int `json:"width,omitempty"`
+ Height int `json:"height,omitempty"`
+ Duration float64 `json:"duration,omitempty"`
+ Title string `json:"title,omitempty"`
+ Performer string `json:"performer,omitempty"`
+ StickerEmoji string `json:"sticker_emoji,omitempty"`
+}
+
+type FileMetadata struct {
+ Version int `json:"version"`
+ Message MessageInfo `json:"message"`
+ File FileInfo `json:"file"`
+ Tags []string `json:"tags,omitempty"`
+}
+
+// BuildFromMessage normalizes the Telegram message fields stored for a saved
+// file. It never stores access hashes or raw Telegram objects.
+func BuildFromMessage(ctx context.Context, msg *tg.Message, savedName string, fileSize int64, includeText bool) FileMetadata {
+ meta := FileMetadata{
+ Version: Version,
+ File: FileInfo{Name: savedName, Size: fileSize},
+ }
+ if msg == nil {
+ return meta
+ }
+
+ meta.Message.ID = msg.GetID()
+ meta.Message.Date = unixTime(msg.GetDate())
+ if editDate, ok := msg.GetEditDate(); ok {
+ meta.Message.EditDate = unixTime(editDate)
+ }
+ if groupedID, ok := msg.GetGroupedID(); ok {
+ meta.Message.GroupedID = groupedID
+ }
+
+ peer := msg.GetPeerID()
+ chatID := tgutil.ChatIdFromPeer(peer)
+ chat := tgutil.ChatInfoFromExt(tgutil.ExtFromContext(ctx), peer)
+ meta.Message.Chat = ChatInfo{ID: chatID, Title: chat.Title, Username: chat.Username}
+ meta.Message.Link = messageLink(chatID, chat.Username, msg.GetID())
+
+ if includeText {
+ meta.Message.Text = msg.GetMessage()
+ meta.Tags = strutil.ExtractTagsFromText(meta.Message.Text)
+ }
+
+ if from, ok := msg.GetFromID(); ok {
+ info := tgutil.ChatInfoFromExt(tgutil.ExtFromContext(ctx), from)
+ name := info.Title
+ if name == "" {
+ name, _ = msg.GetPostAuthor()
+ }
+ meta.Message.Sender = &SenderInfo{
+ ID: tgutil.ChatIdFromPeer(from),
+ Name: name,
+ Username: info.Username,
+ }
+ } else if author, ok := msg.GetPostAuthor(); ok && author != "" {
+ meta.Message.Sender = &SenderInfo{Name: author}
+ }
+
+ if reply, ok := msg.GetReplyTo(); ok {
+ if header, ok := reply.(*tg.MessageReplyHeader); ok {
+ info := &ReplyInfo{MessageID: header.ReplyToMsgID}
+ if peerID, ok := header.GetReplyToPeerID(); ok {
+ info.ChatID = tgutil.ChatIdFromPeer(peerID)
+ }
+ if topicID, ok := header.GetReplyToTopID(); ok {
+ info.TopicID = topicID
+ meta.Message.TopicID = topicID
+ } else if header.ForumTopic {
+ meta.Message.TopicID = header.ReplyToMsgID
+ }
+ meta.Message.ReplyTo = info
+ }
+ }
+
+ if fwd, ok := msg.GetFwdFrom(); ok {
+ info := &ForwardInfo{
+ Date: unixTime(fwd.Date),
+ MessageID: fwd.ChannelPost,
+ Author: fwd.PostAuthor,
+ ChatID: tgutil.ChatIdFromPeer(fwd.FromID),
+ }
+ if fwd.FromID != nil {
+ chat := tgutil.ChatInfoFromExt(tgutil.ExtFromContext(ctx), fwd.FromID)
+ info.ChatTitle, info.ChatUsername = chat.Title, chat.Username
+ }
+ if info.Author == "" {
+ info.Author = fwd.FromName
+ }
+ if savedPeer, ok := fwd.GetSavedFromPeer(); ok {
+ info.ChatID = tgutil.ChatIdFromPeer(savedPeer)
+ chat := tgutil.ChatInfoFromExt(tgutil.ExtFromContext(ctx), savedPeer)
+ info.ChatTitle, info.ChatUsername = chat.Title, chat.Username
+ }
+ if fwd.SavedFromMsgID != 0 {
+ info.MessageID = fwd.SavedFromMsgID
+ }
+ meta.Message.ForwardFrom = info
+ }
+
+ meta.File.MediaType, meta.File.OriginalName = mediaInfo(msg.Media)
+ meta.File.MimeType, meta.File.Width, meta.File.Height, meta.File.Duration,
+ meta.File.Title, meta.File.Performer, meta.File.StickerEmoji = documentInfo(msg.Media)
+ return meta
+}
+
+func unixTime(seconds int) string {
+ if seconds == 0 {
+ return ""
+ }
+ return time.Unix(int64(seconds), 0).UTC().Format(time.RFC3339)
+}
+
+func messageLink(chatID int64, username string, messageID int) string {
+ if messageID == 0 {
+ return ""
+ }
+ if username != "" {
+ return fmt.Sprintf("https://t.me/%s/%d", username, messageID)
+ }
+ peerID := constant.TDLibPeerID(chatID)
+ if peerID.IsChannel() {
+ return fmt.Sprintf("https://t.me/c/%d/%d", peerID.ToPlain(), messageID)
+ }
+ return ""
+}
+
+func mediaInfo(media tg.MessageMediaClass) (mediaType, originalName string) {
+ if media == nil {
+ return "", ""
+ }
+ originalName, _ = tgutil.GetMediaFileName(media)
+ switch media.(type) {
+ case *tg.MessageMediaDocument:
+ return "document", originalName
+ case *tg.MessageMediaPhoto:
+ return "photo", originalName
+ default:
+ return "", originalName
+ }
+}
+
+func documentInfo(media tg.MessageMediaClass) (mimeType string, width, height int, duration float64, title, performer, stickerEmoji string) {
+ mediaDoc, ok := media.(*tg.MessageMediaDocument)
+ if !ok {
+ if photoMedia, ok := media.(*tg.MessageMediaPhoto); ok {
+ if photo, ok := photoMedia.Photo.AsNotEmpty(); ok {
+ for _, size := range photo.Sizes {
+ switch s := size.(type) {
+ case *tg.PhotoSize:
+ if s.W > width || (s.W == width && s.H > height) {
+ width, height = s.W, s.H
+ }
+ case *tg.PhotoSizeProgressive:
+ if s.W > width || (s.W == width && s.H > height) {
+ width, height = s.W, s.H
+ }
+ }
+ }
+ }
+ }
+ return
+ }
+ doc, ok := mediaDoc.Document.AsNotEmpty()
+ if !ok {
+ return
+ }
+ mimeType = doc.MimeType
+ for _, attr := range doc.Attributes {
+ switch a := attr.(type) {
+ case *tg.DocumentAttributeVideo:
+ duration = a.Duration
+ width, height = a.W, a.H
+ case *tg.DocumentAttributeAudio:
+ duration = float64(a.Duration)
+ title, performer = a.Title, a.Performer
+ case *tg.DocumentAttributeImageSize:
+ if width == 0 {
+ width, height = a.W, a.H
+ }
+ case *tg.DocumentAttributeSticker:
+ stickerEmoji = a.Alt
+ }
+ }
+ return
+}
+
+func (m FileMetadata) ToJSON() ([]byte, error) {
+ return json.MarshalIndent(m, "", " ")
+}
+
+// BuildJSON builds and serializes metadata for one saved file.
+func BuildJSON(ctx context.Context, msg *tg.Message, savedName string, fileSize int64, includeText bool) (string, int, error) {
+ meta := BuildFromMessage(ctx, msg, savedName, fileSize, includeText)
+ raw, err := meta.ToJSON()
+ if err != nil {
+ return "", 0, err
+ }
+ return string(raw), Version, nil
+}
diff --git a/pkg/queue/queue.go b/pkg/queue/queue.go
index 47c248d3..78505fc4 100644
--- a/pkg/queue/queue.go
+++ b/pkg/queue/queue.go
@@ -90,9 +90,46 @@ func (tq *TaskQueue[T]) Get() (*Task[T], error) {
// Done stops(cancels) and removes the task from the running tasks.
func (tq *TaskQueue[T]) Done(taskID string) {
tq.mu.Lock()
- defer tq.mu.Unlock()
+ task := tq.taskMap[taskID]
+ if task == nil {
+ task = tq.runningTaskMap[taskID]
+ }
delete(tq.taskMap, taskID)
delete(tq.runningTaskMap, taskID)
+ tq.mu.Unlock()
+ if task != nil {
+ task.Cancel()
+ }
+}
+
+// GetTask returns the queued or running task with the given ID.
+func (tq *TaskQueue[T]) GetTask(taskID string) (*Task[T], bool) {
+ tq.mu.RLock()
+ defer tq.mu.RUnlock()
+ task, ok := tq.taskMap[taskID]
+ if !ok {
+ task, ok = tq.runningTaskMap[taskID]
+ }
+ return task, ok
+}
+
+// Has reports whether the task is queued or running.
+func (tq *TaskQueue[T]) Has(taskID string) bool {
+ tq.mu.RLock()
+ defer tq.mu.RUnlock()
+ if _, ok := tq.taskMap[taskID]; ok {
+ return true
+ }
+ _, ok := tq.runningTaskMap[taskID]
+ return ok
+}
+
+// IsRunning reports whether the task is currently being executed.
+func (tq *TaskQueue[T]) IsRunning(taskID string) bool {
+ tq.mu.RLock()
+ defer tq.mu.RUnlock()
+ _, ok := tq.runningTaskMap[taskID]
+ return ok
}
func (tq *TaskQueue[T]) Length() int {
@@ -184,3 +221,11 @@ func (tq *TaskQueue[T]) Close() {
tq.closed = true
tq.cond.Broadcast()
}
+
+// ActiveCount returns the number of queued and running tasks, including
+// tasks whose context has already been cancelled.
+func (tq *TaskQueue[T]) ActiveCount() int {
+ tq.mu.RLock()
+ defer tq.mu.RUnlock()
+ return tq.tasks.Len() + len(tq.runningTaskMap)
+}
diff --git a/pkg/s3/client.go b/pkg/s3/client.go
index 594c9cf8..e92aa95d 100644
--- a/pkg/s3/client.go
+++ b/pkg/s3/client.go
@@ -1,17 +1,26 @@
package s3
import (
+ "bytes"
"context"
"crypto/hmac"
+ "crypto/md5"
"crypto/sha256"
+ "encoding/base64"
"encoding/hex"
+ "encoding/xml"
+ "errors"
"fmt"
"io"
"net/http"
"net/url"
"sort"
+ "strconv"
"strings"
"time"
+
+ "github.com/charmbracelet/log"
+ "github.com/krau/SaveAny-Bot/pkg/storagetypes"
)
type Client struct {
@@ -24,6 +33,27 @@ type Client struct {
pathStyle bool
}
+const (
+ maxRequestAttempts = 4
+ multipartThreshold = 64 << 20
+ multipartPartSize = 16 << 20
+ minMultipartPartSize = 5 << 20
+)
+
+type initiateMultipartUploadResult struct {
+ UploadID string `xml:"UploadId"`
+}
+
+type completeMultipartUpload struct {
+ XMLName xml.Name `xml:"CompleteMultipartUpload"`
+ Parts []completedPart `xml:"Part"`
+}
+
+type completedPart struct {
+ PartNumber int `xml:"PartNumber"`
+ ETag string `xml:"ETag"`
+}
+
type Config struct {
Endpoint string
Region string
@@ -36,7 +66,17 @@ type Config struct {
func (c *Config) ApplyDefaults() {
if c.HttpClient == nil {
- c.HttpClient = http.DefaultClient
+ transport, ok := http.DefaultTransport.(*http.Transport)
+ if ok {
+ cloned := transport.Clone()
+ cloned.ResponseHeaderTimeout = 30 * time.Second
+ cloned.IdleConnTimeout = 90 * time.Second
+ c.HttpClient = &http.Client{Transport: cloned}
+ } else {
+ c.HttpClient = &http.Client{
+ Transport: &http.Transport{ResponseHeaderTimeout: 30 * time.Second},
+ }
+ }
}
if c.Endpoint == "" {
switch c.Region {
@@ -66,78 +106,391 @@ func (c *Client) HeadBucket(ctx context.Context) error {
if err != nil {
return err
}
- req, err := http.NewRequestWithContext(ctx, "HEAD", url, nil)
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
+ if err != nil {
+ return nil, err
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, true)
if err != nil {
return err
}
+ defer closeBody(resp.Body)
- if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
+ if resp.StatusCode >= 300 {
+ return fmt.Errorf("head bucket failed: %s", resp.Status)
+ }
+ return nil
+}
+
+func (c *Client) Exists(ctx context.Context, key string) (bool, error) {
+ url, err := c.buildURL(key)
+ if err != nil {
+ return false, err
+ }
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
+ if err != nil {
+ return nil, err
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, true)
+ if err != nil {
+ return false, err
+ }
+ defer closeBody(resp.Body)
+
+ switch resp.StatusCode {
+ case http.StatusOK:
+ return true, nil
+ case http.StatusNotFound:
+ return false, nil
+ default:
+ return false, responseError("head object", resp)
+ }
+}
+
+func (c *Client) Put(ctx context.Context, key string, r io.Reader, size int64) error {
+ return c.put(ctx, key, r, size, false)
+}
+
+// PutIfAbsent creates an object only when the key does not exist yet.
+func (c *Client) PutIfAbsent(ctx context.Context, key string, r io.Reader, size int64) error {
+ return c.put(ctx, key, r, size, true)
+}
+
+func (c *Client) put(ctx context.Context, key string, r io.Reader, size int64, ifAbsent bool) error {
+ if size > multipartThreshold {
+ return c.putMultipart(ctx, key, r, size, ifAbsent)
+ }
+ return c.putSingle(ctx, key, r, size, ifAbsent)
+}
+
+func (c *Client) putSingle(ctx context.Context, key string, r io.Reader, size int64, ifAbsent bool) error {
+ url, err := c.buildURL(key)
+ if err != nil {
return err
}
- resp, err := c.httpClient.Do(req)
+ var start int64
+ seeker, canRewind := r.(io.Seeker)
+ if canRewind {
+ start, err = seeker.Seek(0, io.SeekCurrent)
+ if err != nil {
+ canRewind = false
+ }
+ }
+
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ if canRewind {
+ if _, err := seeker.Seek(start, io.SeekStart); err != nil {
+ return nil, fmt.Errorf("rewind S3 request body: %w", err)
+ }
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, r)
+ if err != nil {
+ return nil, err
+ }
+ if size >= 0 {
+ req.ContentLength = size
+ }
+ if ifAbsent {
+ req.Header.Set("If-None-Match", "*")
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, "UNSIGNED-PAYLOAD"); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, canRewind)
if err != nil {
return err
}
- defer resp.Body.Close()
+ defer closeBody(resp.Body)
if resp.StatusCode >= 300 {
- return fmt.Errorf("head bucket failed: %s", resp.Status)
+ if ifAbsent && (resp.StatusCode == http.StatusPreconditionFailed || resp.StatusCode == http.StatusConflict) {
+ return storagetypes.ErrAlreadyExists
+ }
+ return responseError("put object", resp)
}
return nil
}
-func (c *Client) Exists(ctx context.Context, key string) bool {
- url, err := c.buildURL(key)
+func (c *Client) putMultipart(ctx context.Context, key string, r io.Reader, size int64, ifAbsent bool) error {
+ uploadID, err := c.createMultipartUpload(ctx, key)
if err != nil {
- return false
+ return err
}
- req, err := http.NewRequestWithContext(ctx, "HEAD", url, nil)
- if err != nil {
- return false
+ complete := false
+ defer func() {
+ if complete {
+ return
+ }
+ abortCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
+ defer cancel()
+ if err := c.abortMultipartUpload(abortCtx, key, uploadID); err != nil {
+ log.FromContext(ctx).Warnf("Failed to abort S3 multipart upload: %v", err)
+ }
+ }()
+
+ partSize := int64(multipartPartSize)
+ if size <= partSize {
+ partSize = size
+ }
+ if partSize < minMultipartPartSize && size > partSize {
+ partSize = minMultipartPartSize
+ }
+ // S3 allows at most 10,000 parts; grow the part size for very large objects.
+ if (size+partSize-1)/partSize > 10000 {
+ partSize = (size + 9999) / 10000
+ partSize = ((partSize + minMultipartPartSize - 1) / minMultipartPartSize) * minMultipartPartSize
+ }
+ parts := make([]completedPart, 0, (size+partSize-1)/partSize)
+ for offset, number := int64(0), 1; offset < size; number++ {
+ length := min(partSize, size-offset)
+ buf := make([]byte, length)
+ if _, err := io.ReadFull(r, buf); err != nil {
+ return fmt.Errorf("read multipart body at part %d: %w", number, err)
+ }
+ etag, err := c.uploadPart(ctx, key, uploadID, number, buf)
+ if err != nil {
+ return err
+ }
+ parts = append(parts, completedPart{PartNumber: number, ETag: etag})
+ offset += length
}
- if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
- return false
+ if err := c.completeMultipartUpload(ctx, key, uploadID, parts, ifAbsent); err != nil {
+ return err
}
+ complete = true
+ return nil
+}
- resp, err := c.httpClient.Do(req)
+func (c *Client) createMultipartUpload(ctx context.Context, key string) (string, error) {
+ url, err := c.buildURLWithQuery(key, url.Values{"uploads": {""}})
if err != nil {
- return false
+ return "", err
+ }
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, http.NoBody)
+ if err != nil {
+ return nil, err
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, true)
+ if err != nil {
+ return "", err
}
- defer resp.Body.Close()
+ defer closeBody(resp.Body)
+ if resp.StatusCode >= 300 {
+ return "", responseError("create multipart upload", resp)
+ }
+ var result initiateMultipartUploadResult
+ if err := xml.NewDecoder(resp.Body).Decode(&result); err != nil {
+ return "", fmt.Errorf("decode multipart upload response: %w", err)
+ }
+ if result.UploadID == "" {
+ return "", errors.New("S3 returned an empty multipart upload ID")
+ }
+ return result.UploadID, nil
+}
- return resp.StatusCode == http.StatusOK
+func (c *Client) uploadPart(ctx context.Context, key, uploadID string, number int, body []byte) (string, error) {
+ url, err := c.buildURLWithQuery(key, url.Values{
+ "partNumber": {strconv.Itoa(number)},
+ "uploadId": {uploadID},
+ })
+ if err != nil {
+ return "", err
+ }
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewReader(body))
+ if err != nil {
+ return nil, err
+ }
+ req.ContentLength = int64(len(body))
+ req.Header.Set("Content-MD5", md5Base64(body))
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, "UNSIGNED-PAYLOAD"); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, true)
+ if err != nil {
+ return "", err
+ }
+ defer closeBody(resp.Body)
+ if resp.StatusCode >= 300 {
+ return "", responseError(fmt.Sprintf("upload multipart part %d", number), resp)
+ }
+ etag := strings.TrimSpace(resp.Header.Get("ETag"))
+ if etag == "" {
+ return "", fmt.Errorf("S3 did not return an ETag for part %d", number)
+ }
+ return etag, nil
}
-func (c *Client) Put(ctx context.Context, key string, r io.Reader, size int64) error {
- url, err := c.buildURL(key)
+func (c *Client) completeMultipartUpload(ctx context.Context, key, uploadID string, parts []completedPart, ifAbsent bool) error {
+ url, err := c.buildURLWithQuery(key, url.Values{"uploadId": {uploadID}})
if err != nil {
return err
}
- req, err := http.NewRequestWithContext(ctx, "PUT", url, r)
+ payload, err := xml.Marshal(completeMultipartUpload{
+ XMLName: xml.Name{Local: "CompleteMultipartUpload"},
+ Parts: parts,
+ })
+ if err != nil {
+ return fmt.Errorf("encode complete multipart upload: %w", err)
+ }
+ resp, err := c.doRetry(ctx, func() (*http.Request, error) {
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload))
+ if err != nil {
+ return nil, err
+ }
+ req.ContentLength = int64(len(payload))
+ req.Header.Set("Content-Type", "application/xml")
+ if ifAbsent {
+ req.Header.Set("If-None-Match", "*")
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(payload)); err != nil {
+ return nil, err
+ }
+ return req, nil
+ }, true)
if err != nil {
return err
}
- if size >= 0 {
- req.ContentLength = size
+ defer closeBody(resp.Body)
+ if resp.StatusCode >= 300 {
+ if ifAbsent && (resp.StatusCode == http.StatusPreconditionFailed || resp.StatusCode == http.StatusConflict) {
+ return storagetypes.ErrAlreadyExists
+ }
+ return responseError("complete multipart upload", resp)
}
+ return nil
+}
- if err := signRequest(req, c.region, c.accessKey, c.secretKey, "UNSIGNED-PAYLOAD"); err != nil {
+func (c *Client) abortMultipartUpload(ctx context.Context, key, uploadID string) error {
+ url, err := c.buildURLWithQuery(key, url.Values{"uploadId": {uploadID}})
+ if err != nil {
+ return err
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil)
+ if err != nil {
+ return err
+ }
+ if err := signRequest(req, c.region, c.accessKey, c.secretKey, hashSHA256(nil)); err != nil {
return err
}
-
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
- defer resp.Body.Close()
-
- if resp.StatusCode >= 300 {
- return responseError("put object", resp)
+ defer closeBody(resp.Body)
+ if resp.StatusCode >= 300 && resp.StatusCode != http.StatusNotFound {
+ return responseError("abort multipart upload", resp)
}
return nil
}
+func (c *Client) doRetry(ctx context.Context, build func() (*http.Request, error), canRetryBody bool) (*http.Response, error) {
+ var lastErr error
+ for attempt := 0; attempt < maxRequestAttempts; attempt++ {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ req, err := build()
+ if err != nil {
+ return nil, err
+ }
+ resp, err := c.httpClient.Do(req)
+ if err == nil && !retryableStatus(resp.StatusCode) {
+ return resp, nil
+ }
+ if err != nil {
+ if resp != nil {
+ closeBody(resp.Body)
+ }
+ lastErr = err
+ } else {
+ lastErr = responseError(req.Method+" "+req.URL.Path, resp)
+ closeBody(resp.Body)
+ }
+ if !canRetryBody {
+ return nil, lastErr
+ }
+ if attempt+1 >= maxRequestAttempts {
+ break
+ }
+ delay := retryDelay(attempt)
+ timer := time.NewTimer(delay)
+ select {
+ case <-ctx.Done():
+ timer.Stop()
+ return nil, ctx.Err()
+ case <-timer.C:
+ }
+ }
+ return nil, lastErr
+}
+
+func retryableStatus(status int) bool {
+ switch status {
+ case http.StatusRequestTimeout, http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
+ return true
+ default:
+ return false
+ }
+}
+
+func retryDelay(attempt int) time.Duration {
+ return time.Duration(200*(1< 0 {
+ continue
+ }
+ raw, err := json.Marshal(base.RawConfig)
+ if err != nil {
+ return err
+ }
+ if err := tx.Create(&database.StorageRecord{
+ Name: base.Name, Type: base.Type, Enable: base.Enable, ConfigJSON: string(raw),
+ }).Error; err != nil {
+ return err
+ }
+ }
+ storageIDs := make(map[string]uint, len(baseConfigs))
+ for _, base := range baseConfigs {
+ var record database.StorageRecord
+ if err := tx.Where("name = ?", base.Name).First(&record).Error; err != nil {
+ return err
+ }
+ storageIDs[base.Name] = record.ID
+ }
+ for _, u := range cfg.Users {
+ filterIDs := make([]uint, 0, len(u.StorageFilterIDs)+len(u.StorageFilterNames))
+ filterIDs = append(filterIDs, u.StorageFilterIDs...)
+ if len(filterIDs) == 0 {
+ for _, name := range u.StorageFilterNames {
+ if id := storageIDs[name]; id != 0 {
+ filterIDs = append(filterIDs, id)
+ }
+ }
+ }
+ raw, err := json.Marshal(filterIDs)
+ if err != nil {
+ return err
+ }
+ mode := u.FilterMode()
+ var user database.User
+ findErr := tx.Where("chat_id = ?", u.ID).First(&user).Error
+ if findErr != nil && !database.IsNotFound(findErr) {
+ return findErr
+ }
+ if database.IsNotFound(findErr) {
+ if err := tx.Create(&database.User{
+ ChatID: u.ID, Enabled: true, StorageFilterIDsJSON: string(raw), StorageFilterMode: mode,
+ }).Error; err != nil {
+ return err
+ }
+ continue
+ }
+ if err := tx.Model(&user).Updates(map[string]any{
+ "enabled": true, "storage_filter_ids": string(raw), "storage_filter_mode": string(mode),
+ }).Error; err != nil {
+ return err
+ }
+ }
+ query := tx.Model(&database.User{})
+ if len(legacyChatIDs) > 0 {
+ query = query.Where("chat_id NOT IN ?", legacyChatIDs)
+ }
+ if err := query.Update("enabled", false).Error; err != nil {
+ return err
+ }
+ return tx.Save(&database.Setting{
+ Key: legacyImportedKey, Value: time.Now().UTC().Format(time.RFC3339),
+ }).Error
+ })
+ if err != nil {
+ return err
+ }
+ log.Info("Legacy configuration imported into the database")
+ return nil
+}
+
+func cfgListenFromAPI(v *viper.Viper) string {
+ host := strings.TrimSpace(v.GetString("api.host"))
+ if host == "" {
+ host = "0.0.0.0"
+ }
+ port := v.GetInt("api.port")
+ if port <= 0 {
+ port = 8080
+ }
+ return fmt.Sprintf("%s:%d", host, port)
+}
+
+func readLegacyConfig(src string) ([]byte, error) {
+ if src == "" {
+ for _, candidate := range []string{"./config.toml", "/etc/saveany/config.toml"} {
+ if data, err := os.ReadFile(candidate); err == nil {
+ log.Warn("Importing legacy configuration; config files are deprecated", "path", candidate)
+ return data, nil
+ }
+ }
+ return nil, nil
+ }
+ if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Get(src)
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch remote config: %w", err)
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("failed to fetch remote config: status code %d", resp.StatusCode)
+ }
+ return io.ReadAll(resp.Body)
+ }
+ data, err := os.ReadFile(src)
+ if err != nil {
+ return nil, err
+ }
+ return data, nil
+}
diff --git a/settings/settings.go b/settings/settings.go
new file mode 100644
index 00000000..c50f1d56
--- /dev/null
+++ b/settings/settings.go
@@ -0,0 +1,859 @@
+package settings
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "maps"
+ "net"
+ "strings"
+ "sync"
+ "unicode"
+ "unicode/utf8"
+
+ "github.com/charmbracelet/log"
+ "github.com/gotd/td/constant"
+ "golang.org/x/crypto/bcrypt"
+
+ "github.com/krau/SaveAny-Bot/common/i18n"
+ "github.com/krau/SaveAny-Bot/config"
+ storcfg "github.com/krau/SaveAny-Bot/config/storage"
+ "github.com/krau/SaveAny-Bot/database"
+ "github.com/krau/SaveAny-Bot/parsers"
+ storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+ "github.com/krau/SaveAny-Bot/pkg/mediatype"
+ storages "github.com/krau/SaveAny-Bot/storage"
+)
+
+const (
+ GroupCore = "core"
+ GroupLog = "log"
+ GroupDB = "db"
+ GroupTelegram = "telegram"
+ GroupAria2 = "aria2"
+ GroupCache = "cache"
+ GroupTemp = "temp"
+ GroupYtdlp = "ytdlp"
+ GroupParser = "parser"
+ GroupHook = "hook"
+ GroupWeb = "web"
+ GroupMetadata = "metadata"
+ GroupMedia = "media"
+)
+
+const maskedValue = "__MASKED__"
+
+var ErrUnknownGroup = errors.New("unknown settings group")
+
+var (
+ ErrStorageInUse = errors.New("storage is in use")
+ ErrStorageExists = errors.New("storage already exists")
+)
+
+type coreGroup struct {
+ Lang string `json:"lang"`
+ Workers int `json:"workers"`
+ Retry int `json:"retry"`
+ Threads int `json:"threads"`
+ DownloadThreads int `json:"download_threads"`
+ MaxDownloadThreads int `json:"max_download_threads"`
+ TransferBudget int `json:"transfer_budget"`
+ RelayMinSizeMB int `json:"relay_min_size_mb"`
+ ChunkRetries int `json:"chunk_retries"`
+ Stream bool `json:"stream"`
+ NoCleanCache bool `json:"no_clean_cache"`
+ Proxy string `json:"proxy"`
+}
+
+type authGroup struct {
+ PasswordHash string `json:"password_hash"`
+}
+
+type mediaGroup struct {
+ AllowedMediaTypes []string `json:"allowed_media_types"`
+}
+
+type StorageInput struct {
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Enable bool `json:"enable"`
+ Config map[string]any `json:"config"`
+}
+
+func groupValue(cfg config.Config, group string) (any, error) {
+ switch group {
+ case GroupCore:
+ return coreGroup{
+ Lang: cfg.Lang,
+ Workers: cfg.Workers,
+ Retry: cfg.Retry,
+ Threads: cfg.Threads,
+ DownloadThreads: cfg.DownloadThreads,
+ MaxDownloadThreads: cfg.MaxDownloadThreads,
+ TransferBudget: cfg.TransferBudget,
+ RelayMinSizeMB: cfg.RelayMinSizeMB,
+ ChunkRetries: cfg.ChunkRetries,
+ Stream: cfg.Stream,
+ NoCleanCache: cfg.NoCleanCache,
+ Proxy: cfg.Proxy,
+ }, nil
+ case GroupLog:
+ return cfg.Log, nil
+ case GroupDB:
+ return cfg.DB, nil
+ case GroupTelegram:
+ return cfg.Telegram, nil
+ case GroupAria2:
+ return cfg.Aria2, nil
+ case GroupCache:
+ return cfg.Cache, nil
+ case GroupTemp:
+ return cfg.Temp, nil
+ case GroupYtdlp:
+ return cfg.Ytdlp, nil
+ case GroupParser:
+ return cfg.Parser, nil
+ case GroupHook:
+ return cfg.Hook, nil
+ case GroupWeb:
+ return cfg.Web, nil
+ case GroupMetadata:
+ return cfg.Metadata, nil
+ case GroupMedia:
+ return cfg.Media, nil
+ default:
+ return nil, ErrUnknownGroup
+ }
+}
+
+func applyGroup(cfg *config.Config, group string, raw []byte) error {
+ switch group {
+ case GroupCore:
+ g := coreGroup{
+ Lang: cfg.Lang,
+ Workers: cfg.Workers,
+ Retry: cfg.Retry,
+ Threads: cfg.Threads,
+ DownloadThreads: cfg.DownloadThreads,
+ MaxDownloadThreads: cfg.MaxDownloadThreads,
+ TransferBudget: cfg.TransferBudget,
+ RelayMinSizeMB: cfg.RelayMinSizeMB,
+ ChunkRetries: cfg.ChunkRetries,
+ Stream: cfg.Stream,
+ NoCleanCache: cfg.NoCleanCache,
+ Proxy: cfg.Proxy,
+ }
+ if err := json.Unmarshal(raw, &g); err != nil {
+ return err
+ }
+ cfg.Lang = g.Lang
+ cfg.Workers = g.Workers
+ cfg.Retry = g.Retry
+ cfg.Threads = g.Threads
+ cfg.DownloadThreads = g.DownloadThreads
+ cfg.MaxDownloadThreads = g.MaxDownloadThreads
+ cfg.TransferBudget = g.TransferBudget
+ cfg.RelayMinSizeMB = g.RelayMinSizeMB
+ cfg.ChunkRetries = g.ChunkRetries
+ cfg.Stream = g.Stream
+ cfg.NoCleanCache = g.NoCleanCache
+ cfg.Proxy = g.Proxy
+ return nil
+ case GroupLog:
+ return json.Unmarshal(raw, &cfg.Log)
+ case GroupDB:
+ return json.Unmarshal(raw, &cfg.DB)
+ case GroupTelegram:
+ return json.Unmarshal(raw, &cfg.Telegram)
+ case GroupAria2:
+ return json.Unmarshal(raw, &cfg.Aria2)
+ case GroupCache:
+ return json.Unmarshal(raw, &cfg.Cache)
+ case GroupTemp:
+ return json.Unmarshal(raw, &cfg.Temp)
+ case GroupYtdlp:
+ return json.Unmarshal(raw, &cfg.Ytdlp)
+ case GroupParser:
+ // The group payload is complete; reset the map so decoding cannot
+ // mutate the map still referenced by the live config snapshot.
+ cfg.Parser.ParserCfgs = nil
+ return json.Unmarshal(raw, &cfg.Parser)
+ case GroupHook:
+ return json.Unmarshal(raw, &cfg.Hook)
+ case GroupWeb:
+ return json.Unmarshal(raw, &cfg.Web)
+ case GroupMetadata:
+ return json.Unmarshal(raw, &cfg.Metadata)
+ case GroupMedia:
+ var g mediaGroup
+ if err := json.Unmarshal(raw, &g); err != nil {
+ return err
+ }
+ allowed, err := mediatype.NormalizeAllowed(g.AllowedMediaTypes)
+ if err != nil {
+ return err
+ }
+ cfg.Media.AllowedMediaTypes = allowed
+ return nil
+ default:
+ return ErrUnknownGroup
+ }
+}
+
+// Load builds a config snapshot from the database.
+func Load(ctx context.Context, dataDir string) (config.Config, error) {
+ cfg := config.Default(dataDir)
+ raw, err := database.ListSettings(ctx)
+ if err != nil {
+ return cfg, fmt.Errorf("failed to list settings: %w", err)
+ }
+ for group, value := range raw {
+ if group == "auth" || strings.HasPrefix(group, "meta.") {
+ continue
+ }
+ if err := applyGroup(&cfg, group, []byte(value)); err != nil {
+ if errors.Is(err, ErrUnknownGroup) {
+ log.Warn("Ignoring unknown settings group", "group", group)
+ continue
+ }
+ log.Error("Invalid settings group, keeping defaults", "group", group, "error", err)
+ }
+ }
+
+ users, err := database.GetAllUsers(ctx)
+ if err != nil {
+ return cfg, fmt.Errorf("failed to load users: %w", err)
+ }
+ cfg.Users = make([]config.UserConfig, 0, len(users))
+ for _, u := range users {
+ var ids []uint
+ if u.StorageFilterIDsJSON != "" {
+ if err := json.Unmarshal([]byte(u.StorageFilterIDsJSON), &ids); err != nil {
+ log.Error("Invalid storage filter for user", "chat_id", u.ChatID, "error", err)
+ }
+ }
+ cfg.Users = append(cfg.Users, config.UserConfig{
+ ID: u.ChatID,
+ Enabled: u.Enabled,
+ DefaultStorageID: u.DefaultStorageID,
+ StorageFilterIDs: ids,
+ StorageFilterMode: u.StorageFilterMode.Normalize(),
+ })
+ }
+
+ records, err := database.ListStorageRecords(ctx)
+ if err != nil {
+ return cfg, fmt.Errorf("failed to load storages: %w", err)
+ }
+ seen := make(map[string]struct{}, len(records))
+ cfg.Storages = nil
+ for _, rec := range records {
+ if _, ok := seen[rec.Name]; ok {
+ return cfg, fmt.Errorf("duplicate storage name: %s", rec.Name)
+ }
+ seen[rec.Name] = struct{}{}
+ if !rec.Enable {
+ continue
+ }
+ cfgStorage, err := storcfg.NewStorageConfigFromJSON(rec.ID, rec.Name, rec.Type, true, []byte(rec.ConfigJSON))
+ if err != nil {
+ log.Error("Invalid storage config, skipping", "storage", rec.Name, "error", err)
+ continue
+ }
+ if err := cfgStorage.Validate(); err != nil {
+ log.Error("Invalid storage config, skipping", "storage", rec.Name, "error", err)
+ continue
+ }
+ cfg.Storages = append(cfg.Storages, cfgStorage)
+ }
+
+ if cfg.Workers < 1 {
+ cfg.Workers = 1
+ }
+ if cfg.Threads < 1 {
+ cfg.Threads = 1
+ }
+ if cfg.Retry < 1 {
+ cfg.Retry = 1
+ }
+ if cfg.DownloadThreads < 0 {
+ cfg.DownloadThreads = 0
+ }
+ if cfg.MaxDownloadThreads < 1 {
+ cfg.MaxDownloadThreads = 16
+ }
+ if cfg.TransferBudget < 0 {
+ cfg.TransferBudget = 0
+ }
+ if cfg.RelayMinSizeMB < 1 {
+ cfg.RelayMinSizeMB = 8
+ }
+ if cfg.ChunkRetries < 1 {
+ cfg.ChunkRetries = 20
+ }
+ return cfg, nil
+}
+
+type Service struct {
+ ctx context.Context
+ dataDir string
+ mu sync.Mutex
+ listenOverride string
+}
+
+func NewService(ctx context.Context, dataDir string) (*Service, error) {
+ s := &Service{ctx: ctx, dataDir: dataDir}
+ if err := s.Reload(ctx); err != nil {
+ return nil, err
+ }
+ return s, nil
+}
+
+func (s *Service) Config() config.Config {
+ return config.C()
+}
+
+// SetListenOverride forces the in-process listen address (from --listen)
+// without persisting it, so settings reloads do not lose the CLI override.
+func (s *Service) SetListenOverride(listen string) {
+ s.mu.Lock()
+ s.listenOverride = listen
+ s.mu.Unlock()
+ if listen == "" {
+ return
+ }
+ cfg := config.C()
+ cfg.Web.Listen = listen
+ config.Set(cfg)
+}
+
+// ReloadConfig reloads settings and users without reinitializing storages.
+func (s *Service) ReloadConfig(ctx context.Context) error {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ cfg, err := Load(ctx, s.dataDir)
+ if err != nil {
+ return err
+ }
+ if s.listenOverride != "" {
+ cfg.Web.Listen = s.listenOverride
+ }
+ config.Set(cfg)
+ i18n.Init(cfg.Lang)
+ if level, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err == nil {
+ log.SetLevel(level)
+ }
+ parsers.Configure()
+ return nil
+}
+
+// Reload reloads settings, users, and storages.
+func (s *Service) Reload(ctx context.Context) error {
+ if err := s.ReloadConfig(ctx); err != nil {
+ return err
+ }
+ for _, err := range storages.Reload(context.WithoutCancel(ctx)) {
+ log.Errorf("Failed to initialize storage: %v", err)
+ }
+ return nil
+}
+
+func (s *Service) PublicSettings() (map[string]any, error) {
+ cfg := s.Config()
+ out := make(map[string]any)
+ for _, group := range []string{GroupCore, GroupLog, GroupDB, GroupTelegram, GroupAria2, GroupCache, GroupTemp, GroupYtdlp, GroupParser, GroupHook, GroupWeb, GroupMetadata, GroupMedia} {
+ value, err := groupValue(cfg, group)
+ if err != nil {
+ return nil, err
+ }
+ out[group] = value
+ }
+ maskSecrets(out)
+ return out, nil
+}
+
+func maskSecrets(groups map[string]any) {
+ mask := func(group string, keys ...string) {
+ m, ok := groups[group].(map[string]any)
+ if !ok {
+ raw, err := json.Marshal(groups[group])
+ if err != nil {
+ return
+ }
+ m = make(map[string]any)
+ if err := json.Unmarshal(raw, &m); err != nil {
+ return
+ }
+ groups[group] = m
+ }
+ for _, key := range keys {
+ if v, ok := m[key].(string); ok && v != "" {
+ m[key] = maskedValue
+ }
+ }
+ }
+ // Values are structs here; normalize through JSON before masking.
+ for group, value := range groups {
+ raw, err := json.Marshal(value)
+ if err != nil {
+ continue
+ }
+ var m map[string]any
+ if err := json.Unmarshal(raw, &m); err != nil {
+ continue
+ }
+ groups[group] = m
+ }
+ mask(GroupTelegram, "token", "app_hash")
+ mask(GroupAria2, "secret")
+ mask(GroupWeb, "api_token")
+}
+
+func (s *Service) UpdateGroup(ctx context.Context, group string, patch json.RawMessage) ([]string, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ oldCfg := s.Config()
+ current, err := groupValue(oldCfg, group)
+ if err != nil {
+ return nil, err
+ }
+ currentJSON, err := json.Marshal(current)
+ if err != nil {
+ return nil, err
+ }
+ var currentMap map[string]any
+ if err := json.Unmarshal(currentJSON, ¤tMap); err != nil {
+ return nil, err
+ }
+ var patchMap map[string]any
+ if err := json.Unmarshal(patch, &patchMap); err != nil {
+ return nil, fmt.Errorf("invalid patch: %w", err)
+ }
+ for key := range patchMap {
+ if _, ok := currentMap[key]; !ok {
+ return nil, fmt.Errorf("unknown field %q for group %s", key, group)
+ }
+ }
+ if group == GroupWeb && s.listenOverride != "" {
+ if _, provided := patchMap["listen"]; !provided {
+ listen := config.Default(s.dataDir).Web.Listen
+ if raw, ok, err := database.GetSetting(ctx, GroupWeb); err == nil && ok {
+ var stored map[string]any
+ if json.Unmarshal([]byte(raw), &stored) == nil {
+ if v, ok := stored["listen"].(string); ok {
+ listen = v
+ }
+ }
+ }
+ currentMap["listen"] = listen
+ }
+ }
+ merged := mergeJSON(currentMap, patchMap)
+ mergedJSON, err := json.Marshal(merged)
+ if err != nil {
+ return nil, err
+ }
+
+ newCfg := oldCfg
+ if err := applyGroup(&newCfg, group, mergedJSON); err != nil {
+ return nil, err
+ }
+ if err := validateGroup(&newCfg, group); err != nil {
+ return nil, err
+ }
+ if err := database.SetSetting(ctx, group, string(mergedJSON)); err != nil {
+ return nil, err
+ }
+ if err := s.reloadConfigLocked(ctx); err != nil {
+ return nil, err
+ }
+ return restartRequired(oldCfg, s.Config()), nil
+}
+
+func mergeJSON(current, patch map[string]any) map[string]any {
+ out := make(map[string]any, len(current))
+ maps.Copy(out, current)
+ for key, value := range patch {
+ if s, ok := value.(string); ok && s == maskedValue {
+ continue
+ }
+ if patchMap, ok := value.(map[string]any); ok {
+ if currentMap, ok := out[key].(map[string]any); ok {
+ out[key] = mergeJSON(currentMap, patchMap)
+ continue
+ }
+ }
+ out[key] = value
+ }
+ return out
+}
+
+func validateGroup(cfg *config.Config, group string) error {
+ switch group {
+ case GroupCore:
+ if cfg.Workers < 1 || cfg.Retry < 1 || cfg.Threads < 1 {
+ return errors.New("workers, retry and threads must be at least 1")
+ }
+ if cfg.DownloadThreads < 0 || cfg.MaxDownloadThreads < 0 || cfg.TransferBudget < 0 ||
+ cfg.RelayMinSizeMB < 0 || cfg.ChunkRetries < 0 {
+ return errors.New("transfer limits must be non-negative")
+ }
+ if cfg.Proxy != "" {
+ if _, err := config.ProxyTransport(cfg.Proxy); err != nil {
+ return fmt.Errorf("invalid proxy: %w", err)
+ }
+ }
+ case GroupLog:
+ if _, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err != nil {
+ return fmt.Errorf("invalid log level: %w", err)
+ }
+ case GroupWeb:
+ if _, _, err := net.SplitHostPort(cfg.Web.Listen); err != nil {
+ return fmt.Errorf("invalid listen address: %w", err)
+ }
+ case GroupTelegram:
+ if cfg.Telegram.AppID < 0 {
+ return errors.New("app_id must be non-negative")
+ }
+ if id := cfg.Telegram.RelayChatID; id != 0 && !constant.TDLibPeerID(id).IsChannel() {
+ return errors.New("relay_chat_id must be 0 or a Bot API channel ID such as -1001234567890")
+ }
+ }
+ return nil
+}
+
+func (s *Service) reloadConfigLocked(ctx context.Context) error {
+ cfg, err := Load(ctx, s.dataDir)
+ if err != nil {
+ return err
+ }
+ if s.listenOverride != "" {
+ cfg.Web.Listen = s.listenOverride
+ }
+ config.Set(cfg)
+ i18n.Init(cfg.Lang)
+ if level, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err == nil {
+ log.SetLevel(level)
+ }
+ parsers.Configure()
+ return nil
+}
+
+func restartRequired(oldCfg, newCfg config.Config) []string {
+ var fields []string
+ if oldCfg.Workers != newCfg.Workers {
+ fields = append(fields, "core.workers")
+ }
+ if oldCfg.Proxy != newCfg.Proxy {
+ fields = append(fields, "core.proxy")
+ }
+ if oldCfg.DB.Session != newCfg.DB.Session {
+ fields = append(fields, "db.session")
+ }
+ if oldCfg.Cache != newCfg.Cache {
+ fields = append(fields, "cache.ttl", "cache.num_counters", "cache.max_cost")
+ }
+ if oldCfg.Parser.PluginEnable != newCfg.Parser.PluginEnable || !equalStrings(oldCfg.Parser.PluginDirs, newCfg.Parser.PluginDirs) {
+ fields = append(fields, "parser.plugin_enable", "parser.plugin_dirs")
+ }
+ if oldCfg.Parser.Proxy != newCfg.Parser.Proxy {
+ fields = append(fields, "parser.proxy")
+ }
+ if oldCfg.Hook != newCfg.Hook {
+ fields = append(fields, "hook.exec")
+ }
+ if oldCfg.Web.Enable != newCfg.Web.Enable {
+ fields = append(fields, "web.enable")
+ }
+ if oldCfg.Web.Listen != newCfg.Web.Listen {
+ fields = append(fields, "web.listen")
+ }
+ return fields
+}
+
+func equalStrings(a, b []string) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i := range a {
+ if a[i] != b[i] {
+ return false
+ }
+ }
+ return true
+}
+
+func (s *Service) EnsureAdmin(initialPassword string) error {
+ if _, ok, err := database.GetSetting(s.ctx, "auth"); err != nil {
+ return err
+ } else if ok {
+ return nil
+ }
+ password := initialPassword
+ generated := false
+ if password == "" {
+ password = config.GeneratePassword()
+ generated = true
+ }
+ hash, err := bcrypt.GenerateFromPassword([]byte(password), 12)
+ if err != nil {
+ return err
+ }
+ data, err := json.Marshal(authGroup{PasswordHash: string(hash)})
+ if err != nil {
+ return err
+ }
+ if err := database.SetSetting(s.ctx, "auth", string(data)); err != nil {
+ return err
+ }
+ if generated {
+ log.Warn("Generated web admin password; change it after login", "password", password)
+ }
+ return nil
+}
+
+func (s *Service) VerifyAdminPassword(password string) bool {
+ value, ok, err := database.GetSetting(s.ctx, "auth")
+ if err != nil || !ok {
+ return false
+ }
+ var auth authGroup
+ if err := json.Unmarshal([]byte(value), &auth); err != nil {
+ return false
+ }
+ return bcrypt.CompareHashAndPassword([]byte(auth.PasswordHash), []byte(password)) == nil
+}
+
+func (s *Service) ChangeAdminPassword(current, next string) error {
+ if !s.VerifyAdminPassword(current) {
+ return errors.New("current password is incorrect")
+ }
+ if len(next) < 8 {
+ return errors.New("new password must be at least 8 characters")
+ }
+ hash, err := bcrypt.GenerateFromPassword([]byte(next), 12)
+ if err != nil {
+ return err
+ }
+ data, err := json.Marshal(authGroup{PasswordHash: string(hash)})
+ if err != nil {
+ return err
+ }
+ return database.SetSetting(s.ctx, "auth", string(data))
+}
+
+func normalizeStorageName(name string) (string, error) {
+ name = strings.TrimSpace(name)
+ if name == "" {
+ return "", errors.New("storage name is required")
+ }
+ if utf8.RuneCountInString(name) > 64 {
+ return "", errors.New("storage name must be at most 64 characters")
+ }
+ for _, r := range name {
+ if unicode.IsControl(r) {
+ return "", errors.New("storage name must not contain control characters")
+ }
+ }
+ return name, nil
+}
+
+func (s *Service) CreateStorage(ctx context.Context, in StorageInput) (*database.StorageRecord, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ name, err := normalizeStorageName(in.Name)
+ if err != nil {
+ return nil, err
+ }
+ storageType, err := storenum.ParseStorageType(in.Type)
+ if err != nil {
+ return nil, err
+ }
+ typeName := storageType.String()
+ records, err := database.ListStorageRecords(ctx)
+ if err != nil {
+ return nil, err
+ }
+ for _, rec := range records {
+ if rec.Name == name {
+ return nil, fmt.Errorf("%w: %s", ErrStorageExists, name)
+ }
+ }
+ raw, err := json.Marshal(in.Config)
+ if err != nil {
+ return nil, err
+ }
+ if err := storcfg.ValidateJSON(typeName, raw); err != nil {
+ return nil, err
+ }
+ if err := validateStorage(ctx, in.Name, typeName, in.Enable, raw); err != nil {
+ return nil, err
+ }
+ record := &database.StorageRecord{Name: name, Type: typeName, Enable: in.Enable, ConfigJSON: string(raw), ConfigVersion: 1}
+ if err := database.CreateStorageRecord(ctx, record); err != nil {
+ return nil, err
+ }
+ if err := s.reloadAllLocked(ctx); err != nil {
+ return nil, err
+ }
+ return record, nil
+}
+
+func (s *Service) UpdateStorage(ctx context.Context, id uint, in StorageInput) (*database.StorageRecord, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ record, err := database.GetStorageRecord(ctx, id)
+ if err != nil {
+ return nil, err
+ }
+ if strings.TrimSpace(in.Type) != "" {
+ storageType, err := storenum.ParseStorageType(in.Type)
+ if err != nil {
+ return nil, err
+ }
+ if storageType.String() != record.Type {
+ return nil, fmt.Errorf("storage type cannot be changed from %s to %s", record.Type, storageType)
+ }
+ }
+ oldName := record.Name
+ name := oldName
+ if strings.TrimSpace(in.Name) != "" {
+ name, err = normalizeStorageName(in.Name)
+ if err != nil {
+ return nil, err
+ }
+ }
+ if name != oldName {
+ records, err := database.ListStorageRecords(ctx)
+ if err != nil {
+ return nil, err
+ }
+ for _, existing := range records {
+ if existing.ID != record.ID && existing.Name == name {
+ return nil, fmt.Errorf("%w: %s", ErrStorageExists, name)
+ }
+ }
+ }
+ var current map[string]any
+ if record.ConfigJSON != "" {
+ if err := json.Unmarshal([]byte(record.ConfigJSON), ¤t); err != nil {
+ current = make(map[string]any)
+ }
+ }
+ if current == nil {
+ current = make(map[string]any)
+ }
+ for key, value := range in.Config {
+ if s, ok := value.(string); ok && s == maskedValue {
+ continue
+ }
+ current[key] = value
+ }
+ raw, err := json.Marshal(current)
+ if err != nil {
+ return nil, err
+ }
+ if err := storcfg.ValidateJSON(record.Type, raw); err != nil {
+ return nil, err
+ }
+ if err := validateStorage(ctx, name, record.Type, in.Enable, raw); err != nil {
+ return nil, err
+ }
+ record.Name = name
+ record.ConfigJSON = string(raw)
+ record.ConfigVersion = 1
+ record.Enable = in.Enable
+ if err := database.UpdateStorageRecord(ctx, record); err != nil {
+ return nil, err
+ }
+ if err := s.reloadAllLocked(ctx); err != nil {
+ return nil, err
+ }
+ return record, nil
+}
+
+func (s *Service) DeleteStorage(ctx context.Context, id uint) error {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ record, err := database.GetStorageRecord(ctx, id)
+ if err != nil {
+ return err
+ }
+ users, dirs, rules, err := database.StorageReferences(ctx, record.ID)
+ if err != nil {
+ return err
+ }
+ if users+dirs+rules > 0 {
+ return fmt.Errorf("%w: %d user(s), %d dir(s), %d rule(s) reference %s", ErrStorageInUse, users, dirs, rules, record.Name)
+ }
+ if err := database.DeleteStorageRecord(ctx, id); err != nil {
+ return err
+ }
+ return s.reloadAllLocked(ctx)
+}
+
+func (s *Service) TestStorage(ctx context.Context, id uint) error {
+ record, err := database.GetStorageRecord(ctx, id)
+ if err != nil {
+ return err
+ }
+ cfg, err := storcfg.NewStorageConfigFromJSON(record.ID, record.Name, record.Type, true, []byte(record.ConfigJSON))
+ if err != nil {
+ return err
+ }
+ if err := cfg.Validate(); err != nil {
+ return err
+ }
+ st, err := storages.NewStorage(ctx, cfg)
+ if err != nil {
+ return err
+ }
+ if closer, ok := st.(storages.StorageCloser); ok {
+ defer closer.Close()
+ }
+ if tester, ok := st.(storages.StorageTester); ok {
+ return tester.Test(ctx)
+ }
+ if listable, ok := st.(storages.StorageListable); ok {
+ _, err = listable.ListFiles(ctx, "")
+ return err
+ }
+ exists, err := st.Exists(ctx, "")
+ if err != nil {
+ return err
+ }
+ if !exists {
+ return errors.New("storage did not accept the test request")
+ }
+ return nil
+}
+
+func validateStorage(ctx context.Context, name, typ string, enable bool, raw []byte) error {
+ cfg, err := storcfg.NewStorageConfigFromJSON(0, name, typ, enable, raw)
+ if err != nil {
+ return err
+ }
+ if err := cfg.Validate(); err != nil {
+ return err
+ }
+ st, err := storages.NewStorage(ctx, cfg)
+ if err != nil {
+ return err
+ }
+ if closer, ok := st.(storages.StorageCloser); ok {
+ closer.Close()
+ }
+ return nil
+}
+
+func (s *Service) reloadAllLocked(ctx context.Context) error {
+ if err := s.reloadConfigLocked(ctx); err != nil {
+ return err
+ }
+ for _, err := range storages.Reload(context.WithoutCancel(ctx)) {
+ log.Errorf("Failed to initialize storage: %v", err)
+ }
+ return nil
+}
diff --git a/storage/alist/alist.go b/storage/alist/alist.go
index 1e7137d1..c61abeec 100644
--- a/storage/alist/alist.go
+++ b/storage/alist/alist.go
@@ -32,6 +32,7 @@ type Alist struct {
loginInfo *loginRequest
config config.AlistStorageConfig
logger *log.Logger
+ cancel context.CancelFunc
}
// authHeader returns the current token for use in API requests.
@@ -102,10 +103,18 @@ func (a *Alist) Init(ctx context.Context, cfg config.StorageConfig) error {
a.tokenMu.Unlock()
a.logger.Debug("Logged in to Alist")
- go a.refreshToken(ctx, *alistConfig)
+ runCtx, cancel := context.WithCancel(context.WithoutCancel(ctx))
+ a.cancel = cancel
+ go a.refreshToken(runCtx, *alistConfig)
return nil
}
+func (a *Alist) Close() {
+ if a.cancel != nil {
+ a.cancel()
+ }
+}
+
func (a *Alist) Type() storenum.StorageType {
return storenum.Alist
}
@@ -116,11 +125,17 @@ func (a *Alist) Name() string {
func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string) error {
a.logger.Infof("Saving file to %s", storagePath)
- candidate := a.JoinStoragePath(storagePath)
+ candidate, err := a.JoinStoragePath(storagePath)
+ if err != nil {
+ return err
+ }
if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath(a.config.BasePath, storagePath, func(c string) bool {
- return a.existsPath(ctx, c)
+ candidate, err = fsutil.UniquePath(a.config.BasePath, storagePath, func(c string) (bool, error) {
+ return a.existsPathE(ctx, c)
}, 1000)
+ if err != nil {
+ return err
+ }
}
if err := a.mkdirAll(ctx, path.Dir(candidate)); err != nil {
@@ -200,8 +215,15 @@ func (a *Alist) putFile(ctx context.Context, reader io.Reader, storagePath strin
return resp, nil
}
-func (a *Alist) JoinStoragePath(p string) string {
- return path.Join(a.config.BasePath, p)
+func (a *Alist) JoinStoragePath(p string) (string, error) {
+ fullPath, err := storagetypes.SecureJoin(a.config.BasePath, p)
+ if err != nil {
+ return "", err
+ }
+ if !strings.HasPrefix(fullPath, "/") {
+ fullPath = "/" + fullPath
+ }
+ return fullPath, nil
}
// mkdirAll creates the directory and any missing parents. Alist's upload API
@@ -215,7 +237,11 @@ func (a *Alist) mkdirAll(ctx context.Context, dirPath string) error {
tokenRefreshed := false
for i := range segments {
current := "/" + strings.Join(segments[:i+1], "/")
- if a.existsPath(ctx, current) {
+ exists, err := a.existsPathE(ctx, current)
+ if err != nil {
+ return err
+ }
+ if exists {
continue
}
status, code, message, err := a.mkdirRequest(ctx, current)
@@ -233,7 +259,11 @@ func (a *Alist) mkdirAll(ctx context.Context, dirPath string) error {
return fmt.Errorf("failed to refresh alist token: %w", err)
}
tokenRefreshed = true
- if a.existsPath(ctx, current) {
+ exists, err := a.existsPathE(ctx, current)
+ if err != nil {
+ return err
+ }
+ if exists {
continue
}
status, code, message, err = a.mkdirRequest(ctx, current)
@@ -247,7 +277,11 @@ func (a *Alist) mkdirAll(ctx context.Context, dirPath string) error {
if code != http.StatusOK {
// The directory may have been created by a concurrent upload
// between the probe and the request.
- if a.existsPath(ctx, current) {
+ exists, err := a.existsPathE(ctx, current)
+ if err != nil {
+ return err
+ }
+ if exists {
continue
}
return fmt.Errorf("failed to create directory %s: %d, %s", current, code, message)
@@ -288,11 +322,15 @@ func (a *Alist) mkdirRequest(ctx context.Context, p string) (int, int, string, e
return resp.StatusCode, mkResp.Code, mkResp.Message, nil
}
-func (a *Alist) Exists(ctx context.Context, storagePath string) bool {
- return a.existsPath(ctx, a.JoinStoragePath(storagePath))
+func (a *Alist) Exists(ctx context.Context, storagePath string) (bool, error) {
+ fullPath, err := a.JoinStoragePath(storagePath)
+ if err != nil {
+ return false, err
+ }
+ return a.existsPathE(ctx, fullPath)
}
-func (a *Alist) existsPath(ctx context.Context, storagePath string) bool {
+func (a *Alist) existsPathE(ctx context.Context, storagePath string) (bool, error) {
// POST /api/fs/get
/*
body:
@@ -310,41 +348,40 @@ func (a *Alist) existsPath(ctx context.Context, storagePath string) bool {
}
bodyBytes, err := json.Marshal(body)
if err != nil {
- a.logger.Errorf("Failed to marshal request body: %v", err)
- return false
+ return false, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseURL+"/api/fs/get", bytes.NewBuffer(bodyBytes))
if err != nil {
- a.logger.Errorf("Failed to create request: %v", err)
- return false
+ return false, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", a.authHeader())
req.Header.Set("Content-Type", "application/json")
resp, err := a.client.Do(req)
if err != nil {
- a.logger.Errorf("Failed to send request: %v", err)
- return false
+ return false, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
+ if resp.StatusCode == http.StatusNotFound {
+ return false, nil
+ }
if resp.StatusCode != http.StatusOK {
- return false
+ return false, fmt.Errorf("failed to get file info: %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
- a.logger.Errorf("Failed to read response body: %v", err)
- return false
+ return false, fmt.Errorf("failed to read response body: %w", err)
}
var fsGetResp fsGetResponse
if err := json.Unmarshal(data, &fsGetResp); err != nil {
- a.logger.Errorf("Failed to unmarshal fs get response: %v", err)
- return false
+ return false, fmt.Errorf("failed to unmarshal fs get response: %w", err)
+ }
+ if fsGetResp.Code == http.StatusNotFound {
+ return false, nil
}
if fsGetResp.Code != http.StatusOK {
- a.logger.Errorf("Failed to get file info from Alist: %d, %s", fsGetResp.Code, fsGetResp.Message)
- return false
+ return false, fmt.Errorf("failed to get file info from Alist: %d, %s", fsGetResp.Code, fsGetResp.Message)
}
- return true
-
+ return true, nil
}
// Impl StorageCannotStream interface
@@ -356,8 +393,12 @@ func (a *Alist) CannotStream() string {
func (a *Alist) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) {
a.logger.Debugf("Listing files in directory: %s", dirPath)
+ fullDirPath, err := a.JoinStoragePath(dirPath)
+ if err != nil {
+ return nil, err
+ }
reqBody := fsListRequest{
- Path: dirPath,
+ Path: fullDirPath,
Password: "",
Page: 1,
PerPage: 0, // 0 means all files
@@ -433,8 +474,12 @@ func (a *Alist) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.F
func (a *Alist) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) {
a.logger.Debugf("Opening file: %s", filePath)
+ fullFilePath, err := a.JoinStoragePath(filePath)
+ if err != nil {
+ return nil, 0, err
+ }
reqBody := map[string]any{
- "path": filePath,
+ "path": fullFilePath,
"password": "",
}
diff --git a/storage/alist/utils.go b/storage/alist/utils.go
index 3e329d54..e6a4d426 100644
--- a/storage/alist/utils.go
+++ b/storage/alist/utils.go
@@ -7,9 +7,12 @@ import (
func getHttpClient() *http.Client {
return &http.Client{
- Timeout: 12 * time.Hour,
Transport: &http.Transport{
- TLSHandshakeTimeout: 10 * time.Second,
+ Proxy: http.ProxyFromEnvironment,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ResponseHeaderTimeout: 30 * time.Second,
+ IdleConnTimeout: 90 * time.Second,
+ MaxIdleConns: 100,
},
}
}
diff --git a/storage/load.go b/storage/load.go
index 02964155..482241a5 100644
--- a/storage/load.go
+++ b/storage/load.go
@@ -17,9 +17,11 @@ var (
// Storages maps storage names to initialized storage instances.
Storages = make(map[string]Storage)
- userStoragesMu sync.RWMutex
- // UserStorages maps user IDs to their available storage instances.
- UserStorages = make(map[int64][]Storage)
+ errorsMu sync.RWMutex
+ lastErrors = make(map[string]string)
+
+ retiredMu sync.Mutex
+ retired []Storage
initFlight singleflight.Group
)
@@ -42,8 +44,28 @@ func AllStorages() map[string]Storage {
return out
}
-// GetStorageByName returns storage by name from cache or creates new one
-// It should NOT be used to get storage for user, use GetStorageByUserIDAndName instead
+// LastErrors returns the initialization errors from the last Reload.
+func LastErrors() map[string]string {
+ errorsMu.RLock()
+ defer errorsMu.RUnlock()
+ out := make(map[string]string, len(lastErrors))
+ maps.Copy(out, lastErrors)
+ return out
+}
+
+// ID returns the stable database ID for an initialized storage, or zero when
+// the storage cannot be resolved from the current config.
+func ID(st Storage) uint {
+ if st == nil {
+ return 0
+ }
+ if cfg := config.C().GetStorageByName(st.Name()); cfg != nil {
+ return cfg.GetID()
+ }
+ return 0
+}
+
+// GetStorageByName returns storage by name from cache or creates a new one.
func GetStorageByName(ctx context.Context, name string) (Storage, error) {
if name == "" {
return nil, ErrStorageNameEmpty
@@ -60,7 +82,6 @@ func GetStorageByName(ctx context.Context, name string) (Storage, error) {
return nil, fmt.Errorf("storage %s not found", name)
}
- // Merge concurrent first-time initializations.
v, err, _ := initFlight.Do("storage:"+name, func() (any, error) {
storageMu.RLock()
if existing, ok := Storages[name]; ok {
@@ -86,15 +107,31 @@ func GetStorageByName(ctx context.Context, name string) (Storage, error) {
return v.(Storage), nil
}
+func GetStorageByID(ctx context.Context, id uint) (Storage, error) {
+ cfg := config.C().GetStorageByID(id)
+ if cfg == nil {
+ return nil, fmt.Errorf("storage ID %d not found", id)
+ }
+ return GetStorageByName(ctx, cfg.GetName())
+}
+
+func GetStorageByUserIDAndID(ctx context.Context, chatID int64, id uint) (Storage, error) {
+ if id == 0 {
+ return nil, ErrStorageNameEmpty
+ }
+ if !config.C().HasStorageID(chatID, id) {
+ return nil, fmt.Errorf("no storage ID %d for user %d", id, chatID)
+ }
+ return GetStorageByID(ctx, id)
+}
+
func GetStorageByUserIDAndName(ctx context.Context, chatID int64, name string) (Storage, error) {
if name == "" {
return nil, ErrStorageNameEmpty
}
-
if !config.C().HasStorage(chatID, name) {
return nil, fmt.Errorf("no storage %s for user %d", name, chatID)
}
-
return GetStorageByName(ctx, name)
}
@@ -102,15 +139,9 @@ func GetUserStorages(ctx context.Context, chatID int64) []Storage {
if chatID <= 0 {
return nil
}
- userStoragesMu.RLock()
- cached, ok := UserStorages[chatID]
- userStoragesMu.RUnlock()
- if ok {
- return cached
- }
var storages []Storage
- for _, name := range config.C().GetStorageNamesByUserID(chatID) {
- storage, err := GetStorageByName(ctx, name)
+ for _, id := range config.C().GetStorageIDsByUserID(chatID) {
+ storage, err := GetStorageByID(ctx, id)
if err != nil {
continue
}
@@ -119,29 +150,77 @@ func GetUserStorages(ctx context.Context, chatID int64) []Storage {
return storages
}
-func LoadStorages(ctx context.Context) {
+// Reload rebuilds the storage registry from the current config snapshot.
+func Reload(ctx context.Context) []error {
logger := log.FromContext(ctx)
- logger.Debug("loading storages...")
- for _, storage := range config.C().Storages {
- _, err := GetStorageByName(ctx, storage.GetName())
+ logger.Debug("reloading storages...")
+
+ initCtx := context.WithoutCancel(ctx)
+ newStorages := make(map[string]Storage)
+ errs := make(map[string]string)
+ var out []error
+ for _, storageCfg := range config.C().Storages {
+ name := storageCfg.GetName()
+ st, err := NewStorage(initCtx, storageCfg)
if err != nil {
- logger.Errorf("failed to load storage %s: %v", storage.GetName(), err)
+ errs[name] = err.Error()
+ out = append(out, fmt.Errorf("storage %s: %w", name, err))
+ logger.Errorf("failed to load storage %s: %v", name, err)
+ continue
}
+ newStorages[name] = st
}
- storageMu.RLock()
- loaded := len(Storages)
- storageMu.RUnlock()
- logger.Infof("successfully loaded %d storages", loaded)
- for user := range config.C().GetUsersID() {
- uid := int64(user)
- storages := GetUserStorages(ctx, uid)
- userStoragesMu.Lock()
- UserStorages[uid] = storages
- userStoragesMu.Unlock()
+
+ if err := ctx.Err(); err != nil {
+ for _, st := range newStorages {
+ if closer, ok := st.(StorageCloser); ok {
+ closer.Close()
+ }
+ }
+ return append(out, err)
+ }
+
+ storageMu.Lock()
+ old := Storages
+ Storages = newStorages
+ storageMu.Unlock()
+
+ // Keep retired instances alive: in-flight tasks may still hold them.
+ retiredMu.Lock()
+ for _, st := range old {
+ retired = append(retired, st)
+ }
+ retiredMu.Unlock()
+
+ errorsMu.Lock()
+ lastErrors = errs
+ errorsMu.Unlock()
+
+ logger.Infof("successfully loaded %d storages", len(newStorages))
+ return out
+}
+
+// CloseAll closes every initialized storage.
+func CloseAll() {
+ storageMu.Lock()
+ all := make([]Storage, 0, len(Storages))
+ for _, st := range Storages {
+ all = append(all, st)
+ }
+ Storages = make(map[string]Storage)
+ storageMu.Unlock()
+ retiredMu.Lock()
+ all = append(all, retired...)
+ retired = nil
+ retiredMu.Unlock()
+ for _, st := range all {
+ if closer, ok := st.(StorageCloser); ok {
+ closer.Close()
+ }
}
}
-// GetTelegramStorageByUserID returns the first enabled Telegram storage for the user
+// GetTelegramStorageByUserID returns the first enabled Telegram storage for the user.
func GetTelegramStorageByUserID(ctx context.Context, chatID int64) (Storage, error) {
storages := GetUserStorages(ctx, chatID)
for _, stor := range storages {
diff --git a/storage/local/local.go b/storage/local/local.go
index 1c7b4b64..c8487a4f 100644
--- a/storage/local/local.go
+++ b/storage/local/local.go
@@ -2,6 +2,7 @@ package local
import (
"context"
+ "errors"
"fmt"
"io"
"os"
@@ -51,56 +52,146 @@ func (l *Local) JoinStoragePath(path string) string {
return filepath.Join(l.config.BasePath, path)
}
-func (l *Local) Save(ctx context.Context, r io.Reader, storagePath string) error {
- l.logger.Infof("Saving file to %s", storagePath)
- storagePath = filepath.Clean(storagePath)
- if filepath.IsAbs(storagePath) {
- return fmt.Errorf("local: storage path must be relative: %s", storagePath)
+// resolve maps a storage path to an absolute path inside the storage root. It
+// rejects absolute paths and traversal, including existing symlinks.
+func (l *Local) resolve(storagePath string) (string, error) {
+ clean := filepath.Clean(storagePath)
+ if clean == "." {
+ clean = ""
}
- if storagePath == ".." || strings.HasPrefix(storagePath, ".."+string(filepath.Separator)) {
- return fmt.Errorf("local: storage path escapes base directory: %s", storagePath)
+ if clean != "" && !filepath.IsLocal(clean) {
+ return "", fmt.Errorf("local: storage path escapes base directory: %s", storagePath)
}
-
- candidate := l.JoinStoragePath(storagePath)
- if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath(l.config.BasePath, storagePath, l.existsPath, 1000)
+ absPath := filepath.Join(l.config.BasePath, clean)
+ realBase, err := filepath.EvalSymlinks(l.config.BasePath)
+ if err != nil {
+ return absPath, nil
+ }
+ realPath, err := filepath.EvalSymlinks(absPath)
+ if err != nil {
+ return absPath, nil
+ }
+ rel, err := filepath.Rel(realBase, realPath)
+ if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
+ return "", fmt.Errorf("local: storage path escapes base directory: %s", storagePath)
}
+ return absPath, nil
+}
+
+func (l *Local) Save(ctx context.Context, r io.Reader, storagePath string) error {
+ _, err := l.SaveWithPath(ctx, r, storagePath)
+ return err
+}
- absPath, err := filepath.Abs(candidate)
+// SaveWithPath saves a file and returns its actual storage-relative path after
+// conflict resolution.
+func (l *Local) SaveWithPath(ctx context.Context, r io.Reader, storagePath string) (string, error) {
+ l.logger.Infof("Saving file to %s", storagePath)
+ clean := filepath.Clean(storagePath)
+ if clean == "." {
+ clean = ""
+ }
+ absPath, err := l.resolve(clean)
if err != nil {
- return err
+ return "", err
+ }
+ overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool)
+ if !overwrite {
+ absPath, err = fsutil.UniquePath(l.config.BasePath, clean, l.existsPathE, 1000)
+ if err != nil {
+ return "", err
+ }
}
if err := fileutil.CreateDir(filepath.Dir(absPath)); err != nil {
- return fmt.Errorf("failed to create directory: %w", err)
+ return "", fmt.Errorf("failed to create directory: %w", err)
}
- file, err := os.Create(absPath)
+ // Write beside the target and rename, so a failed copy never leaves a
+ // truncated file under the final name.
+ tempFile, err := os.CreateTemp(filepath.Dir(absPath), "."+filepath.Base(absPath)+".part-*")
if err != nil {
- return fmt.Errorf("failed to create file: %w", err)
+ return "", fmt.Errorf("failed to create file: %w", err)
}
+ tempPath := tempFile.Name()
+ removeTemp := true
defer func() {
- if err := file.Close(); err != nil {
- l.logger.Errorf("Failed to close file %s: %v", absPath, err)
+ if removeTemp {
+ if err := os.Remove(tempPath); err != nil && !errors.Is(err, os.ErrNotExist) {
+ l.logger.Warnf("Failed to remove temporary file %s: %v", tempPath, err)
+ }
}
}()
- _, err = io.Copy(file, r)
- return err
+ if _, err := io.Copy(tempFile, r); err != nil {
+ return "", errors.Join(err, tempFile.Close())
+ }
+ if err := tempFile.Chmod(0o644); err != nil {
+ return "", errors.Join(err, tempFile.Close())
+ }
+ if err := tempFile.Close(); err != nil {
+ return "", err
+ }
+ if !overwrite {
+ // Link refuses to replace an existing file, closing the check-then-write
+ // race in UniquePath.
+ if err := os.Link(tempPath, absPath); err != nil {
+ if errors.Is(err, os.ErrExist) {
+ return "", storagetypes.ErrAlreadyExists
+ }
+ return "", err
+ }
+ if err := os.Remove(tempPath); err != nil && !errors.Is(err, os.ErrNotExist) {
+ return "", err
+ }
+ removeTemp = false
+ return l.relativePath(absPath)
+ }
+ if err := os.Rename(tempPath, absPath); err != nil {
+ return "", err
+ }
+ removeTemp = false
+ return l.relativePath(absPath)
+}
+
+func (l *Local) relativePath(absPath string) (string, error) {
+ base, err := filepath.Abs(l.config.BasePath)
+ if err != nil {
+ return "", err
+ }
+ rel, err := filepath.Rel(base, absPath)
+ if err != nil {
+ return "", err
+ }
+ return filepath.Clean(rel), nil
}
-func (l *Local) Exists(ctx context.Context, storagePath string) bool {
- return l.existsPath(l.JoinStoragePath(storagePath))
+func (l *Local) Exists(ctx context.Context, storagePath string) (bool, error) {
+ absPath, err := l.resolve(storagePath)
+ if err != nil {
+ return false, err
+ }
+ return l.existsPathE(absPath)
}
-func (l *Local) existsPath(storagePath string) bool {
+func (l *Local) existsPathE(storagePath string) (bool, error) {
absPath, err := filepath.Abs(storagePath)
if err != nil {
- return false
+ return false, err
+ }
+ _, err = os.Stat(absPath)
+ if err == nil {
+ return true, nil
}
- return fileutil.IsExist(absPath)
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil
+ }
+ return false, err
}
// ListFiles implements StorageListable interface
func (l *Local) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) {
- absPath := l.JoinStoragePath(dirPath)
+ absPath, err := l.resolve(dirPath)
+ if err != nil {
+ return nil, err
+ }
entries, err := os.ReadDir(absPath)
if err != nil {
@@ -130,7 +221,10 @@ func (l *Local) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.F
// OpenFile implements StorageReadable interface
func (l *Local) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) {
- absPath := l.JoinStoragePath(filePath)
+ absPath, err := l.resolve(filePath)
+ if err != nil {
+ return nil, 0, err
+ }
file, err := os.Open(absPath)
if err != nil {
diff --git a/storage/local/local_test.go b/storage/local/local_test.go
new file mode 100644
index 00000000..9a55c8b0
--- /dev/null
+++ b/storage/local/local_test.go
@@ -0,0 +1,55 @@
+package local
+
+import (
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/krau/SaveAny-Bot/config/storage"
+)
+
+func newTestLocal(t *testing.T) *Local {
+ t.Helper()
+ base := t.TempDir()
+ l := &Local{}
+ if err := l.Init(context.Background(), &storage.LocalStorageConfig{
+ BaseConfig: storage.BaseConfig{Name: "test", Type: "local", Enable: true},
+ BasePath: base,
+ }); err != nil {
+ t.Fatalf("Init: %v", err)
+ }
+ return l
+}
+
+func TestLocalSaveDoesNotOverwrite(t *testing.T) {
+ l := newTestLocal(t)
+ ctx := context.Background()
+ if err := l.Save(ctx, strings.NewReader("first"), "dir/file.txt"); err != nil {
+ t.Fatalf("first Save: %v", err)
+ }
+ if err := l.Save(ctx, strings.NewReader("second"), "dir/file.txt"); err != nil {
+ t.Fatalf("second Save: %v", err)
+ }
+
+ got, err := os.ReadFile(filepath.Join(l.config.BasePath, "dir", "file_1.txt"))
+ if err != nil {
+ t.Fatalf("read renamed file: %v", err)
+ }
+ if string(got) != "second" {
+ t.Fatalf("renamed file = %q, want second", got)
+ }
+}
+
+func TestLocalRejectsTraversal(t *testing.T) {
+ l := newTestLocal(t)
+ err := l.Save(context.Background(), strings.NewReader("x"), "../escape.txt")
+ if err == nil {
+ t.Fatal("Save traversal returned no error")
+ }
+ if _, statErr := os.Stat(filepath.Join(filepath.Dir(l.config.BasePath), "escape.txt")); !errors.Is(statErr, os.ErrNotExist) {
+ t.Fatalf("traversal file was created or stat failed: %v", statErr)
+ }
+}
diff --git a/storage/minio/client.go b/storage/minio/client.go
deleted file mode 100644
index a8078689..00000000
--- a/storage/minio/client.go
+++ /dev/null
@@ -1,109 +0,0 @@
-//go:build !no_minio
-
-package minio
-
-import (
- "context"
- "fmt"
- "io"
- "path"
- "strings"
- "sync"
-
- "github.com/charmbracelet/log"
- "github.com/krau/SaveAny-Bot/common/utils/fsutil"
- config "github.com/krau/SaveAny-Bot/config/storage"
- "github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
- storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
-)
-
-var (
- deprecatedOnce sync.Once
-)
-
-type Minio struct {
- config config.MinioStorageConfig
- client *minio.Client
- logger *log.Logger
-}
-
-func (m *Minio) Init(ctx context.Context, cfg config.StorageConfig) error {
- deprecatedOnce.Do(func() {
- log.FromContext(ctx).Warn("Minio storage is deprecated, please use S3 storage type instead.")
- })
- minioConfig, ok := cfg.(*config.MinioStorageConfig)
- if !ok {
- return fmt.Errorf("failed to cast minio config")
- }
- if err := minioConfig.Validate(); err != nil {
- return err
- }
- m.config = *minioConfig
- m.logger = log.FromContext(ctx).WithPrefix(fmt.Sprintf("minio[%s]", m.config.Name))
-
- client, err := minio.New(m.config.Endpoint, &minio.Options{
- Creds: credentials.NewStaticV4(m.config.AccessKeyID, m.config.SecretAccessKey, ""),
- Secure: m.config.UseSSL,
- })
- if err != nil {
- return fmt.Errorf("failed to create minio client: %w", err)
- }
-
- exists, err := client.BucketExists(ctx, m.config.BucketName)
- if err != nil {
- return fmt.Errorf("failed to check bucket existence: %w", err)
- }
- if !exists {
- return fmt.Errorf("bucket %s does not exist", m.config.BucketName)
- }
-
- m.client = client
- return nil
-}
-
-func (m *Minio) Type() storenum.StorageType {
- return storenum.Minio
-}
-
-func (m *Minio) Name() string {
- return m.config.Name
-}
-
-func (m *Minio) JoinStoragePath(p string) string {
- return strings.TrimPrefix(path.Join(m.config.BasePath, p), "/")
-}
-
-func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error {
- m.logger.Infof("Saving file from reader to %s", storagePath)
- candidate := m.JoinStoragePath(storagePath)
- if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath(strings.TrimPrefix(m.config.BasePath, "/"), storagePath, func(c string) bool {
- return m.existsObject(ctx, c)
- }, 10)
- }
- size := int64(-1)
- if length := ctx.Value(ctxkey.ContentLength); length != nil {
- length, ok := length.(int64)
- if ok && length > 0 {
- size = length
- }
- }
- _, err := m.client.PutObject(ctx, m.config.BucketName, candidate, r, size, minio.PutObjectOptions{})
- if err != nil {
- return fmt.Errorf("failed to upload file to minio: %w", err)
- }
-
- return nil
-}
-
-func (m *Minio) Exists(ctx context.Context, storagePath string) bool {
- m.logger.Debugf("Checking if file exists at %s", storagePath)
- return m.existsObject(ctx, m.JoinStoragePath(storagePath))
-}
-
-func (m *Minio) existsObject(ctx context.Context, storagePath string) bool {
- _, err := m.client.StatObject(ctx, m.config.BucketName, storagePath, minio.StatObjectOptions{})
- return err == nil
-}
diff --git a/storage/minio/client_stub.go b/storage/minio/client_stub.go
deleted file mode 100644
index 0c6601b7..00000000
--- a/storage/minio/client_stub.go
+++ /dev/null
@@ -1,41 +0,0 @@
-//go:build no_minio
-
-package minio
-
-import (
- "context"
- "fmt"
- "io"
- "path"
- "strings"
-
- config "github.com/krau/SaveAny-Bot/config/storage"
- storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
-)
-
-type Minio struct {
-}
-
-func (m *Minio) Init(_ context.Context, _ config.StorageConfig) error {
- return fmt.Errorf("minio storage is not supported in this build")
-}
-
-func (m *Minio) Type() storenum.StorageType {
- return storenum.Minio
-}
-
-func (m *Minio) Name() string {
- return ""
-}
-
-func (m *Minio) JoinStoragePath(p string) string {
- return strings.TrimPrefix(path.Join("", p), "/")
-}
-
-func (m *Minio) Save(_ context.Context, _ io.Reader, _ string) error {
- return fmt.Errorf("minio storage is not supported in this build")
-}
-
-func (m *Minio) Exists(_ context.Context, _ string) bool {
- return false
-}
diff --git a/storage/rclone/rclone.go b/storage/rclone/rclone.go
index 9b4c661f..ceb4dab2 100644
--- a/storage/rclone/rclone.go
+++ b/storage/rclone/rclone.go
@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"os/exec"
@@ -88,27 +89,37 @@ func (r *Rclone) buildBaseArgs() []string {
return args
}
-func (r *Rclone) getRemotePath(storagePath string) string {
+func (r *Rclone) getRemotePath(storagePath string) (string, error) {
remote := r.config.Remote
if !strings.HasSuffix(remote, ":") {
remote += ":"
}
- basePath := strings.TrimPrefix(r.config.BasePath, "/")
- fullPath := path.Join(basePath, storagePath)
- return remote + fullPath
+ fullPath, err := storagetypes.SecureJoin(r.config.BasePath, storagePath)
+ if err != nil {
+ return "", err
+ }
+ return remote + fullPath, nil
}
func (r *Rclone) Save(ctx context.Context, reader io.Reader, storagePath string) error {
r.logger.Infof("Saving file to %s", storagePath)
candidate := storagePath
- if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath("", storagePath, func(c string) bool {
+ overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool)
+ if !overwrite {
+ var err error
+ candidate, err = fsutil.UniquePath("", storagePath, func(c string) (bool, error) {
return r.Exists(ctx, c)
}, 100)
+ if err != nil {
+ return err
+ }
}
- remotePath := r.getRemotePath(candidate)
+ remotePath, err := r.getRemotePath(candidate)
+ if err != nil {
+ return err
+ }
r.logger.Debugf("Remote path: %s", remotePath)
// Use rclone rcat to read from stdin and upload
@@ -130,15 +141,28 @@ func (r *Rclone) Save(ctx context.Context, reader io.Reader, storagePath string)
return nil
}
-func (r *Rclone) Exists(ctx context.Context, storagePath string) bool {
- remotePath := r.getRemotePath(storagePath)
+func (r *Rclone) Exists(ctx context.Context, storagePath string) (bool, error) {
+ remotePath, err := r.getRemotePath(storagePath)
+ if err != nil {
+ return false, err
+ }
args := r.buildBaseArgs()
args = append(args, "lsf", remotePath)
cmd := exec.CommandContext(ctx, "rclone", args...)
- err := cmd.Run()
- return err == nil
+ err = cmd.Run()
+ if err == nil {
+ return true, nil
+ }
+ var exitErr *exec.ExitError
+ if errors.As(err, &exitErr) {
+ // rclone uses 3/4 for missing directories/files.
+ if exitErr.ExitCode() == 3 || exitErr.ExitCode() == 4 {
+ return false, nil
+ }
+ }
+ return false, fmt.Errorf("failed to check rclone path %s: %w", storagePath, err)
}
// lsjsonItem represents a single entry in the output of `rclone lsjson`
@@ -155,7 +179,10 @@ type lsjsonItem struct {
func (r *Rclone) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) {
r.logger.Infof("Listing files in %s", dirPath)
- remotePath := r.getRemotePath(dirPath)
+ remotePath, err := r.getRemotePath(dirPath)
+ if err != nil {
+ return nil, err
+ }
args := r.buildBaseArgs()
args = append(args, "lsjson", remotePath)
@@ -205,7 +232,10 @@ func (r *Rclone) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
func (r *Rclone) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) {
r.logger.Infof("Opening file %s", filePath)
- remotePath := r.getRemotePath(filePath)
+ remotePath, err := r.getRemotePath(filePath)
+ if err != nil {
+ return nil, 0, err
+ }
size, err := r.getFileSize(ctx, remotePath)
if err != nil {
@@ -271,9 +301,7 @@ func (r *rcloneCatReader) Read(p []byte) (n int, err error) {
}
func (r *rcloneCatReader) Close() error {
- if err := r.reader.Close(); err != nil {
- r.logger.Warnf("Failed to close reader: %v", err)
- }
+ closeErr := r.reader.Close()
// Kill the cat process so Wait cannot block on a hung pipe.
if r.cmd.Process != nil {
if err := r.cmd.Process.Kill(); err != nil {
@@ -283,5 +311,8 @@ func (r *rcloneCatReader) Close() error {
if err := r.cmd.Wait(); err != nil {
r.logger.Warnf("rclone cat process exited with error: %v", err)
}
+ if closeErr != nil {
+ return fmt.Errorf("close rclone cat pipe: %w", closeErr)
+ }
return nil
}
diff --git a/storage/s3/s3.go b/storage/s3/s3.go
index 68121dc2..e8457f3f 100644
--- a/storage/s3/s3.go
+++ b/storage/s3/s3.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
- "path"
"strings"
"github.com/charmbracelet/log"
@@ -13,6 +12,7 @@ import (
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
"github.com/krau/SaveAny-Bot/pkg/s3"
+ "github.com/krau/SaveAny-Bot/pkg/storagetypes"
)
type S3 struct {
@@ -58,18 +58,25 @@ func (m *S3) Name() string {
return m.config.Name
}
-func (m *S3) JoinStoragePath(p string) string {
- return strings.TrimPrefix(path.Join(m.config.BasePath, p), "/")
+func (m *S3) JoinStoragePath(p string) (string, error) {
+ return storagetypes.SecureJoin(m.config.BasePath, p)
}
func (m *S3) Save(ctx context.Context, r io.Reader, storagePath string) error {
m.logger.Infof("Saving file from reader to %s", storagePath)
- candidate := m.JoinStoragePath(storagePath)
+ candidate, err := m.JoinStoragePath(storagePath)
+ if err != nil {
+ return err
+ }
- if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath(strings.TrimPrefix(m.config.BasePath, "/"), storagePath, func(c string) bool {
+ overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool)
+ if !overwrite {
+ candidate, err = fsutil.UniquePath(strings.TrimPrefix(m.config.BasePath, "/"), storagePath, func(c string) (bool, error) {
return m.existsKey(ctx, c)
}, 10)
+ if err != nil {
+ return err
+ }
}
size := int64(-1)
@@ -79,7 +86,11 @@ func (m *S3) Save(ctx context.Context, r io.Reader, storagePath string) error {
}
}
- err := m.client.Put(ctx, candidate, r, size)
+ if overwrite {
+ err = m.client.Put(ctx, candidate, r, size)
+ } else {
+ err = m.client.PutIfAbsent(ctx, candidate, r, size)
+ }
if err != nil {
return fmt.Errorf("failed to upload file to S3: %w", err)
}
@@ -87,12 +98,16 @@ func (m *S3) Save(ctx context.Context, r io.Reader, storagePath string) error {
return nil
}
-func (m *S3) Exists(ctx context.Context, storagePath string) bool {
+func (m *S3) Exists(ctx context.Context, storagePath string) (bool, error) {
m.logger.Debugf("Checking if file exists at %s", storagePath)
- return m.existsKey(ctx, m.JoinStoragePath(storagePath))
+ key, err := m.JoinStoragePath(storagePath)
+ if err != nil {
+ return false, err
+ }
+ return m.existsKey(ctx, key)
}
-func (m *S3) existsKey(ctx context.Context, key string) bool {
+func (m *S3) existsKey(ctx context.Context, key string) (bool, error) {
return m.client.Exists(ctx, key)
}
diff --git a/storage/s3/s3_test.go b/storage/s3/s3_test.go
index 77749c8e..5c16e0b1 100644
--- a/storage/s3/s3_test.go
+++ b/storage/s3/s3_test.go
@@ -68,20 +68,20 @@ func TestS3(t *testing.T) {
t.Fatalf("Save failed: %v", err)
}
- if !s.Exists(ctx, key) {
- t.Fatalf("Exists should return true for saved key")
+ if exists, err := s.Exists(ctx, key); err != nil || !exists {
+ t.Fatalf("Exists(key) = %v, %v, want true, nil", exists, err)
}
- if s.Exists(ctx, "nonexistent.txt") {
- t.Fatalf("Exists should return false for nonexistent key")
+ if exists, err := s.Exists(ctx, "nonexistent.txt"); err != nil || exists {
+ t.Fatalf("Exists(nonexistent) = %v, %v, want false, nil", exists, err)
}
if err := s.Save(ctx, bytes.NewReader(content), key); err != nil {
t.Fatalf("Save with existing key failed: %v", err)
}
- if !s.Exists(ctx, "foo/bar_1.txt") {
- t.Fatalf("Exists should return true for unique renamed key")
+ if exists, err := s.Exists(ctx, "foo/bar_1.txt"); err != nil || !exists {
+ t.Fatalf("Exists(renamed) = %v, %v, want true, nil", exists, err)
}
var length int64 = int64(len(content))
@@ -90,7 +90,23 @@ func TestS3(t *testing.T) {
t.Fatalf("Save with content length failed: %v", err)
}
- if !s.Exists(ctx, "size_test.txt") {
- t.Fatalf("Exists should return true for size_test.txt")
+ if exists, err := s.Exists(ctx, "size_test.txt"); err != nil || !exists {
+ t.Fatalf("Exists(size_test) = %v, %v, want true, nil", exists, err)
+ }
+}
+
+func TestS3MultipartSave(t *testing.T) {
+ s, _ := newFakeS3(t)
+ ctx := t.Context()
+ content := make([]byte, 65<<20)
+ for i := range content {
+ content[i] = byte(i)
+ }
+ if err := s.Save(ctx, bytes.NewReader(content), "multipart.bin"); err != nil {
+ t.Fatalf("multipart Save failed: %v", err)
+ }
+ exists, err := s.Exists(ctx, "multipart.bin")
+ if err != nil || !exists {
+ t.Fatalf("multipart object exists = %v, %v, want true, nil", exists, err)
}
}
diff --git a/storage/storage.go b/storage/storage.go
index 44265b94..3eef7bbf 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -1,16 +1,17 @@
package storage
import (
+ "bytes"
"context"
"fmt"
"io"
storcfg "github.com/krau/SaveAny-Bot/config/storage"
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
+ "github.com/krau/SaveAny-Bot/pkg/metadata"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
"github.com/krau/SaveAny-Bot/storage/alist"
"github.com/krau/SaveAny-Bot/storage/local"
- "github.com/krau/SaveAny-Bot/storage/minio"
"github.com/krau/SaveAny-Bot/storage/rclone"
"github.com/krau/SaveAny-Bot/storage/s3"
"github.com/krau/SaveAny-Bot/storage/telegram"
@@ -23,7 +24,9 @@ type Storage interface {
Type() storenum.StorageType
Name() string
Save(ctx context.Context, reader io.Reader, storagePath string) error
- Exists(ctx context.Context, storagePath string) bool
+ // Exists reports whether storagePath exists. Backend/query failures must be
+ // returned as an error rather than treated as "not found".
+ Exists(ctx context.Context, storagePath string) (bool, error)
}
type StorageCannotStream interface {
@@ -68,6 +71,47 @@ type StorageListable interface {
ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error)
}
+// StorageTester verifies that the backend can reach its configured target.
+// It is used by storage connection tests before falling back to ListFiles or
+// Exists.
+type StorageTester interface {
+ Storage
+ Test(ctx context.Context) error
+}
+
+// StorageSidecarWriter lets a backend define how metadata sidecars are stored.
+// Telegram storage implements it as a no-op because metadata is consumed during
+// the upload instead of being written as a separate chat message.
+type StorageSidecarWriter interface {
+ Storage
+ WriteMetadataSidecar(ctx context.Context, actualPath string, data []byte) error
+}
+
+// WriteMetadataSidecar writes a normalized metadata sidecar using the backend
+// policy, defaulting to a sidecar file next to the actual saved path.
+func WriteMetadataSidecar(ctx context.Context, st Storage, actualPath string, data []byte) error {
+ if writer, ok := st.(StorageSidecarWriter); ok {
+ return writer.WriteMetadataSidecar(ctx, actualPath, data)
+ }
+ return st.Save(WithOverwrite(ctx), bytes.NewReader(data), actualPath+metadata.MetaSuffix)
+}
+
+// StoragePathSaver returns the actual path assigned by a backend after any
+// conflict resolution. Backends without this interface use the requested path.
+type StoragePathSaver interface {
+ Storage
+ SaveWithPath(ctx context.Context, reader io.Reader, storagePath string) (string, error)
+}
+
+// SaveWithPath saves a reader and returns the actual storage path when the
+// backend can report it.
+func SaveWithPath(ctx context.Context, st Storage, reader io.Reader, storagePath string) (string, error) {
+ if saver, ok := st.(StoragePathSaver); ok {
+ return saver.SaveWithPath(ctx, reader, storagePath)
+ }
+ return storagePath, st.Save(ctx, reader, storagePath)
+}
+
type StorageReadable interface {
Storage
OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error)
@@ -75,6 +119,9 @@ type StorageReadable interface {
var _ StorageProgressSaver = (*telegram.Telegram)(nil)
var _ StorageBatchProgressSaver = (*telegram.Telegram)(nil)
+var _ StorageTester = (*telegram.Telegram)(nil)
+var _ StoragePathSaver = (*local.Local)(nil)
+var _ StorageSidecarWriter = (*telegram.Telegram)(nil)
var _ StorageListable = (*alist.Alist)(nil)
var _ StorageReadable = (*alist.Alist)(nil)
@@ -85,13 +132,17 @@ var _ StorageReadable = (*rclone.Rclone)(nil)
var _ StorageListable = (*webdav.Webdav)(nil)
var _ StorageReadable = (*webdav.Webdav)(nil)
+type StorageCloser interface {
+ Storage
+ Close()
+}
+
type StorageConstructor func() Storage
var storageConstructors = map[storenum.StorageType]StorageConstructor{
storenum.Alist: func() Storage { return new(alist.Alist) },
storenum.Local: func() Storage { return new(local.Local) },
storenum.Webdav: func() Storage { return new(webdav.Webdav) },
- storenum.Minio: func() Storage { return new(minio.Minio) },
storenum.S3: func() Storage { return new(s3.S3) },
storenum.Telegram: func() Storage { return new(telegram.Telegram) },
storenum.Rclone: func() Storage { return new(rclone.Rclone) },
diff --git a/storage/telegram/split.go b/storage/telegram/split.go
index 6a18dbf6..37e4c358 100644
--- a/storage/telegram/split.go
+++ b/storage/telegram/split.go
@@ -10,6 +10,19 @@ import (
"time"
)
+// ctxReader aborts a copy when the task context is cancelled.
+type ctxReader struct {
+ ctx context.Context
+ reader io.Reader
+}
+
+func (r ctxReader) Read(p []byte) (int, error) {
+ if err := r.ctx.Err(); err != nil {
+ return 0, err
+ }
+ return r.reader.Read(p)
+}
+
type splitWriter struct {
baseName string
partSize int64
@@ -120,7 +133,7 @@ func CreateSplitZip(ctx context.Context, reader io.Reader, size int64, fileName,
return fmt.Errorf("failed to create zip header: %w", err)
}
- copied, err := io.Copy(writer, reader)
+ copied, err := io.Copy(writer, ctxReader{ctx: ctx, reader: reader})
if err != nil {
return fmt.Errorf("failed to write data: %w", err)
}
diff --git a/storage/telegram/split_test.go b/storage/telegram/split_test.go
index a90301d5..1ac05e68 100644
--- a/storage/telegram/split_test.go
+++ b/storage/telegram/split_test.go
@@ -1,55 +1,45 @@
package telegram
import (
+ "bytes"
"os"
"path/filepath"
"testing"
)
func TestCreateSplitZip(t *testing.T) {
- input := "tests/testfile.dat"
- file, err := os.Open(input)
- if err != nil {
- t.Fatalf("failed to open test file: %v", err)
- }
- defer file.Close()
- fileName := filepath.Base(input)
- fileInfo, err := file.Stat()
- if err != nil {
- t.Fatalf("failed to stat test file: %v", err)
- }
- fileSize := fileInfo.Size()
-
+ content := bytes.Repeat([]byte("SaveAny-Bot split test\n"), 2*1024*1024/22+1)
+ content = content[:2*1024*1024+123]
tests := []struct {
+ name string
partSize int64
- output string
}{
- {partSize: int64(1024 * 1024 * 500), output: "tests/split_test_output_500MB"},
- {partSize: int64(1024 * 1024 * 100), output: "tests/split_test_output_100MB"},
+ {name: "1MiB", partSize: 1024 * 1024},
+ {name: "512KiB", partSize: 512 * 1024},
}
for _, tt := range tests {
- err = CreateSplitZip(t.Context(), file, fileSize, fileName, tt.output, tt.partSize)
- if err != nil {
- t.Fatalf("CreateSplitZip failed: %v", err)
- }
- matched, err := filepath.Glob(tt.output + ".z*")
- if err != nil {
- t.Fatalf("failed to glob split files: %v", err)
- }
- if len(matched) == 0 {
- t.Fatalf("no split files found")
- }
- t.Logf("Created %d split files", len(matched))
- for _, f := range matched {
- info, err := os.Stat(f)
+ t.Run(tt.name, func(t *testing.T) {
+ output := filepath.Join(t.TempDir(), "split")
+ if err := CreateSplitZip(t.Context(), bytes.NewReader(content), int64(len(content)), "testfile.dat", output, tt.partSize); err != nil {
+ t.Fatalf("CreateSplitZip failed: %v", err)
+ }
+ matched, err := filepath.Glob(output + ".z*")
if err != nil {
- t.Fatalf("failed to stat file %s: %v", f, err)
+ t.Fatalf("failed to glob split files: %v", err)
+ }
+ if len(matched) == 0 {
+ t.Fatal("no split files found")
}
- if info.Size() > tt.partSize {
- t.Errorf("file %s exceeds part size: %d > %d", f, info.Size(), tt.partSize)
+ for _, name := range matched {
+ info, err := os.Stat(name)
+ if err != nil {
+ t.Fatalf("failed to stat %s: %v", name, err)
+ }
+ if info.Size() > tt.partSize {
+ t.Errorf("file %s exceeds part size: %d > %d", name, info.Size(), tt.partSize)
+ }
}
- t.Logf(" - %s (%d bytes)", f, info.Size())
- }
+ })
}
}
diff --git a/storage/telegram/telegram.go b/storage/telegram/telegram.go
index f9cb254a..b289e328 100644
--- a/storage/telegram/telegram.go
+++ b/storage/telegram/telegram.go
@@ -2,25 +2,29 @@ package telegram
import (
"context"
+ "encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
+ "strconv"
"strings"
"time"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/duke-git/lancet/v2/retry"
"github.com/duke-git/lancet/v2/slice"
- "github.com/duke-git/lancet/v2/validator"
"github.com/gabriel-vasile/mimetype"
+ "github.com/gotd/td/constant"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/telegram/uploader"
"github.com/gotd/td/tg"
+ "github.com/krau/SaveAny-Bot/client/worker"
"github.com/krau/SaveAny-Bot/common/utils/dlutil"
+ "github.com/krau/SaveAny-Bot/common/utils/fsutil"
+ "github.com/krau/SaveAny-Bot/common/utils/rpclimit"
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config"
storconfig "github.com/krau/SaveAny-Bot/config/storage"
@@ -28,6 +32,7 @@ import (
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
"golang.org/x/time/rate"
)
@@ -58,6 +63,8 @@ type batchMediaItem struct {
useSingleSave bool
}
+type primaryUploadContextKey struct{}
+
func (t *Telegram) Init(ctx context.Context, cfg storconfig.StorageConfig) error {
telegramConfig, ok := cfg.(*storconfig.TelegramStorageConfig)
if !ok {
@@ -86,8 +93,47 @@ func (t *Telegram) Name() string {
// Exists always reports false: Telegram offers no reliable way to query
// whether a file already exists in a chat, so conflict policies do not apply
// to this backend.
-func (t *Telegram) Exists(ctx context.Context, storagePath string) bool {
- return false
+func (t *Telegram) Exists(ctx context.Context, storagePath string) (bool, error) {
+ return false, nil
+}
+
+// Test verifies that a Telegram client is available and can access the
+// configured chat.
+func (t *Telegram) Test(ctx context.Context) error {
+ tctx := tgutil.ExtFromContext(ctx)
+ if tctx == nil {
+ return fmt.Errorf("telegram client is not available")
+ }
+ chatID := t.config.ChatID
+ if chatID == 0 {
+ return fmt.Errorf("telegram chat_id is required")
+ }
+ peer := ensureInputPeer(tctx, chatID)
+ if peer == nil || peer.Zero() {
+ return fmt.Errorf("telegram chat %d is not accessible", chatID)
+ }
+ plainID := constant.TDLibPeerID(chatID).ToPlain()
+ switch peer.(type) {
+ case *tg.InputPeerChannel:
+ if _, err := tctx.GetChat(chatID); err != nil {
+ return fmt.Errorf("telegram chat %d is not accessible: %w", chatID, err)
+ }
+ case *tg.InputPeerChat:
+ if _, err := tctx.GetChat(plainID); err != nil {
+ return fmt.Errorf("telegram chat %d is not accessible: %w", chatID, err)
+ }
+ case *tg.InputPeerUser:
+ if _, err := tctx.GetUser(plainID); err != nil {
+ return fmt.Errorf("telegram user %d is not accessible: %w", chatID, err)
+ }
+ }
+ return nil
+}
+
+// WriteMetadataSidecar intentionally does nothing: Telegram storage consumes
+// metadata during upload instead of sending a separate sidecar message.
+func (t *Telegram) WriteMetadataSidecar(context.Context, string, []byte) error {
+ return nil
}
func (t *Telegram) Save(ctx context.Context, r io.Reader, storagePath string) error {
@@ -107,12 +153,40 @@ func (t *Telegram) SaveWithProgress(
}
func (t *Telegram) save(ctx context.Context, r io.Reader, storagePath string, progress *uploadProgress) error {
- storagePath = path.Clean(storagePath)
- captionOverride := sourceCaptionOverride(ctx)
- tctx := tgutil.ExtFromContext(ctx)
- if tctx == nil {
+ primary := tgutil.ExtFromContext(ctx)
+ if primary == nil {
return fmt.Errorf("failed to get telegram context")
}
+ fallback := primary
+ if original, ok := ctx.Value(primaryUploadContextKey{}).(*ext.Context); ok && original != nil {
+ fallback = original
+ }
+ tctx := t.uploadContext(primary, storagePath)
+ uploadCtx := ctx
+ if tctx != primary {
+ uploadCtx = tgutil.ExtWithContext(ctx, tctx)
+ }
+ err := t.saveWithContext(uploadCtx, tctx, r, storagePath, progress)
+ if err == nil || tctx == fallback || ctx.Err() != nil {
+ return err
+ }
+ rs, ok := r.(io.ReadSeeker)
+ if !ok {
+ return err
+ }
+ if _, seekErr := rs.Seek(0, io.SeekStart); seekErr != nil {
+ return err
+ }
+ if progress != nil {
+ progress.reset(contentLength(ctx))
+ }
+ log.FromContext(ctx).Warnf("Helper upload failed, retrying with primary bot: %v", err)
+ return t.saveWithContext(tgutil.ExtWithContext(ctx, fallback), fallback, r, storagePath, progress)
+}
+
+func (t *Telegram) saveWithContext(ctx context.Context, tctx *ext.Context, r io.Reader, storagePath string, progress *uploadProgress) error {
+ storagePath = path.Clean(storagePath)
+ captionOverride := t.captionOverride(ctx)
size := contentLength(ctx)
maxUploadSize := maxUploadFileSize(tctx)
if t.config.SkipLarge && size > maxUploadSize {
@@ -135,7 +209,7 @@ func (t *Telegram) save(ctx context.Context, r io.Reader, storagePath string, pr
}
}
upler := t.newUploader(tctx, size, progress)
- peer := tryGetInputPeer(tctx, chatID)
+ peer := ensureInputPeer(tctx, chatID)
if peer == nil || peer.Zero() {
return fmt.Errorf("failed to get input peer for chat ID %d", chatID)
}
@@ -178,13 +252,13 @@ func (t *Telegram) save(ctx context.Context, r io.Reader, storagePath string, pr
}
}
}
- return t.splitUpload(tctx, r, filename, upler, peer, size, splitSize, progress)
+ return t.splitUpload(ctx, tctx, r, filename, upler, peer, size, splitSize, progress)
}
if err := t.limiter.Wait(ctx); err != nil {
return fmt.Errorf("rate limit failed: %w", err)
}
- prepared, err := t.prepareMedia(ctx, tctx, r, storagePath, size, nil, progress)
+ prepared, err := t.prepareMedia(ctx, tctx, r, storagePath, size, captionOverride, progress)
if err != nil {
return err
}
@@ -195,6 +269,46 @@ func (t *Telegram) save(ctx context.Context, r io.Reader, storagePath string, pr
return err
}
+// uploadContext picks a helper that can resolve the target path with the same
+// destination as the primary client. Helpers that cannot reach the chat are
+// skipped, so an unusable helper never replaces the primary upload path.
+func (t *Telegram) uploadContext(primary *ext.Context, storagePaths ...string) *ext.Context {
+ if primary == nil {
+ return nil
+ }
+ for _, helper := range worker.UploadExts() {
+ if t.canUploadWith(primary, helper, storagePaths) {
+ return helper
+ }
+ }
+ return primary
+}
+
+func (t *Telegram) canUploadWith(primary, helper *ext.Context, storagePaths []string) bool {
+ if helper == nil {
+ return false
+ }
+ if len(storagePaths) == 0 {
+ return true
+ }
+ for _, storagePath := range storagePaths {
+ primaryName, primaryChatID := t.target(primary, path.Clean(storagePath))
+ helperName, helperChatID := t.target(helper, path.Clean(storagePath))
+ if primaryName != helperName || primaryChatID != helperChatID {
+ return false
+ }
+ peer := ensureInputPeer(helper, helperChatID)
+ if peer == nil || peer.Zero() {
+ return false
+ }
+ // A helper bot cannot initiate a DM with a user who has not started it.
+ if _, isUser := peer.(*tg.InputPeerUser); isUser {
+ return false
+ }
+ }
+ return true
+}
+
func contentLength(ctx context.Context) int64 {
if length := ctx.Value(ctxkey.ContentLength); length != nil {
if size, ok := length.(int64); ok {
@@ -204,12 +318,45 @@ func contentLength(ctx context.Context) int64 {
return -1
}
-func sourceCaptionOverride(ctx context.Context) *string {
- caption, ok := storagetypes.SourceCaptionFromContext(ctx)
- if !ok {
- return nil
+func (t *Telegram) captionOverride(ctx context.Context) *string {
+ if t.config.UseMetadataCaption {
+ if caption, ok := metadataCaption(storagetypes.MessageMetadataFromContext(ctx)); ok {
+ return &caption
+ }
+ }
+ if caption, ok := storagetypes.SourceCaptionFromContext(ctx); ok {
+ caption = truncateCaption(caption)
+ return &caption
+ }
+ return nil
+}
+
+func (t *Telegram) albumCaption(item storagetypes.BatchItem) *string {
+ if t.config.UseMetadataCaption {
+ if text, ok := metadataCaption(item.MetadataJSON); ok {
+ return &text
+ }
+ }
+ if item.PreserveCaption {
+ caption := truncateCaption(item.Caption)
+ return &caption
+ }
+ return nil
+}
+
+func metadataCaption(raw string) (string, bool) {
+ if raw == "" {
+ return "", false
}
- return &caption
+ var meta struct {
+ Message struct {
+ Text string `json:"text"`
+ } `json:"message"`
+ }
+ if err := json.Unmarshal([]byte(raw), &meta); err != nil || meta.Message.Text == "" {
+ return "", false
+ }
+ return truncateCaption(meta.Message.Text), true
}
func maxUploadFileSize(tctx *ext.Context) int64 {
@@ -238,7 +385,7 @@ func (t *Telegram) target(tctx *ext.Context, storagePath string) (string, int64)
if len(parts) >= 1 {
filename = parts[len(parts)-1]
}
- if len(parts) >= 2 && validator.IsAlphaNumeric(parts[0]) {
+ if len(parts) >= 2 && isChatRef(parts[0]) {
cid, err := tgutil.ParseChatID(tctx, parts[0])
if err != nil {
log.FromContext(tctx).Warnf("Failed to parse chat ID from path, using configured chat_id: %s", err)
@@ -249,8 +396,26 @@ func (t *Telegram) target(tctx *ext.Context, storagePath string) (string, int64)
return filename, chatID
}
+func isChatRef(value string) bool {
+ if strings.HasPrefix(value, "@") {
+ return len(value) > 1
+ }
+ if value == "" {
+ return false
+ }
+ if _, err := strconv.ParseInt(value, 10, 64); err == nil {
+ return true
+ }
+ for _, r := range value {
+ if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') && r != '_' {
+ return false
+ }
+ }
+ return true
+}
+
func (t *Telegram) newUploader(tctx *ext.Context, size int64, progress *uploadProgress) *uploader.Uploader {
- upler := uploader.NewUploader(tctx.Raw).
+ upler := uploader.NewUploader(rpclimit.WrapUploader(tctx.Raw)).
WithPartSize(tglimit.MaxUploadPartSize).
WithThreads(dlutil.BestThreads(size, config.C().Threads))
if progress != nil {
@@ -263,10 +428,19 @@ func mediaCaption(filename string, override *string) []message.StyledTextOption
if override == nil {
return []message.StyledTextOption{styling.Plain(filename)}
}
- if *override == "" {
+ caption := truncateCaption(*override)
+ if caption == "" {
return nil
}
- return []message.StyledTextOption{styling.Plain(*override)}
+ return []message.StyledTextOption{styling.Plain(caption)}
+}
+
+func truncateCaption(value string) string {
+ runes := []rune(value)
+ if len(runes) > 1024 {
+ runes = runes[:1024]
+ }
+ return string(runes)
}
func (t *Telegram) prepareMedia(
@@ -281,7 +455,7 @@ func (t *Telegram) prepareMedia(
storagePath = path.Clean(storagePath)
filename, chatID := t.target(tctx, storagePath)
upler := t.newUploader(tctx, size, progress)
- peer := tryGetInputPeer(tctx, chatID)
+ peer := ensureInputPeer(tctx, chatID)
if peer == nil || peer.Zero() {
return nil, fmt.Errorf("failed to get input peer for chat ID %d", chatID)
}
@@ -392,10 +566,19 @@ func (t *Telegram) saveBatch(
items []storagetypes.BatchItem,
onProgress func(index int, uploaded, total int64),
) error {
- tctx := tgutil.ExtFromContext(ctx)
- if tctx == nil {
+ primary := tgutil.ExtFromContext(ctx)
+ if primary == nil {
return fmt.Errorf("failed to get telegram context")
}
+ ctx = context.WithValue(ctx, primaryUploadContextKey{}, primary)
+ paths := make([]string, 0, len(items))
+ for _, item := range items {
+ paths = append(paths, item.StoragePath)
+ }
+ tctx := t.uploadContext(primary, paths...)
+ if tctx != primary {
+ ctx = tgutil.ExtWithContext(ctx, tctx)
+ }
inspected := make([]batchMediaItem, 0, len(items))
for index, item := range items {
@@ -406,9 +589,22 @@ func (t *Telegram) saveBatch(
mediaItem.index = index
inspected = append(inspected, mediaItem)
}
- for _, group := range planMediaGroups(inspected) {
+ for _, group := range t.planMediaGroups(inspected) {
if err := t.saveMediaGroup(ctx, tctx, group, onProgress); err != nil {
- return err
+ if tctx == primary || ctx.Err() != nil {
+ return err
+ }
+ for _, mediaItem := range group {
+ if _, seekErr := mediaItem.item.Reader.Seek(0, io.SeekStart); seekErr != nil {
+ return err
+ }
+ }
+ log.FromContext(ctx).Warnf("Helper batch upload failed, retrying group with primary bot: %v", err)
+ ctx = tgutil.ExtWithContext(ctx, primary)
+ tctx = primary
+ if err := t.saveMediaGroup(ctx, tctx, group, onProgress); err != nil {
+ return err
+ }
}
}
return nil
@@ -440,11 +636,26 @@ func (t *Telegram) inspectBatchItem(tctx *ext.Context, item storagetypes.BatchIt
return result, nil
}
+func batchGroupKey(item storagetypes.BatchItem, useMetadata bool) string {
+ if useMetadata && item.MetadataGroupKey != "" {
+ return item.MetadataGroupKey
+ }
+ return item.SourceGroupKey
+}
+
func planMediaGroups(items []batchMediaItem) [][]batchMediaItem {
+ return planMediaGroupsWithMetadata(items, false)
+}
+
+func (t *Telegram) planMediaGroups(items []batchMediaItem) [][]batchMediaItem {
+ return planMediaGroupsWithMetadata(items, t.config.UseMetadataAlbum)
+}
+
+func planMediaGroupsWithMetadata(items []batchMediaItem, useMetadata bool) [][]batchMediaItem {
groups := make([][]batchMediaItem, 0, len(items))
for i := 0; i < len(items); {
item := items[i]
- if item.useSingleSave || !item.albumEligible || item.item.SourceGroupKey == "" {
+ if item.useSingleSave || !item.albumEligible || batchGroupKey(item.item, useMetadata) == "" {
groups = append(groups, items[i:i+1])
i++
continue
@@ -452,7 +663,7 @@ func planMediaGroups(items []batchMediaItem) [][]batchMediaItem {
end := i + 1
for end < len(items) && end-i < tglimit.MaxAlbumItems {
next := items[end]
- if next.useSingleSave || !next.albumEligible || next.chatID != item.chatID || next.item.SourceGroupKey != item.item.SourceGroupKey {
+ if next.useSingleSave || !next.albumEligible || next.chatID != item.chatID || batchGroupKey(next.item, useMetadata) != batchGroupKey(item.item, useMetadata) {
break
}
end++
@@ -489,6 +700,7 @@ func (t *Telegram) saveMediaGroup(
return fmt.Errorf("failed to seek batch item: %w", err)
}
itemCtx := context.WithValue(ctx, ctxkey.ContentLength, item.Size)
+ itemCtx = storagetypes.WithMessageMetadata(itemCtx, item.MetadataJSON)
if item.PreserveCaption {
itemCtx = storagetypes.WithSourceCaption(itemCtx, item.Caption)
}
@@ -504,14 +716,15 @@ func (t *Telegram) saveMediaGroup(
}
prepared := make([]preparedMedia, 0, len(group))
- for _, mediaItem := range group {
+ emptyCaption := ""
+ for index, mediaItem := range group {
item := mediaItem.item
if _, err := item.Reader.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("failed to seek batch item: %w", err)
}
- var captionOverride *string
- if item.PreserveCaption {
- captionOverride = &item.Caption
+ captionOverride := &emptyCaption
+ if index == 0 {
+ captionOverride = t.albumCaption(item)
}
progress := batchItemUploadProgress(mediaItem, onProgress)
media, err := t.prepareMedia(ctx, tctx, item.Reader, item.StoragePath, item.Size, captionOverride, progress)
@@ -542,7 +755,8 @@ func (t *Telegram) CannotStream() string {
}
func (t *Telegram) splitUpload(
- ctx *ext.Context,
+ ctx context.Context,
+ tctx *ext.Context,
r io.Reader,
filename string,
upler *uploader.Uploader,
@@ -551,19 +765,30 @@ func (t *Telegram) splitUpload(
progress *uploadProgress,
) error {
tempId := xid.New().String()
- outputBase := filepath.Join(config.C().Temp.BasePath, tempId, strings.Split(filename, ".")[0])
+ // Keep a readable part name, but strip glob metacharacters: the base is
+ // reused as a filepath.Glob pattern below.
+ stem, _, _ := strings.Cut(filename, ".")
+ stem = fsutil.SafeBaseName(stem)
+ stem = strings.NewReplacer("*", "_", "?", "_", "[", "_", "]", "_", "\\", "_").Replace(stem)
+ if stem == "" || stem == "." || stem == ".." {
+ stem = tempId
+ }
+ outputBase := filepath.Join(config.C().Temp.BasePath, tempId, stem)
defer func() {
if err := os.RemoveAll(filepath.Join(config.C().Temp.BasePath, tempId)); err != nil {
log.FromContext(ctx).Warnf("Failed to cleanup temp split files: %s", err)
}
}()
- if err := CreateSplitZip(ctx, r, fileSize, filename, outputBase, splitSize); err != nil {
+ if err := CreateSplitZip(ctx, r, fileSize, fsutil.SafeBaseName(filename), outputBase, splitSize); err != nil {
return fmt.Errorf("failed to create split zip: %w", err)
}
matched, err := filepath.Glob(outputBase + ".z*")
if err != nil {
return fmt.Errorf("failed to glob split files: %w", err)
}
+ if len(matched) == 0 {
+ return fmt.Errorf("split upload produced no parts")
+ }
inputFiles := make([]tg.InputFileClass, 0, len(matched))
if progress != nil {
var uploadSize int64
@@ -590,6 +815,9 @@ func (t *Telegram) splitUpload(
}
partFileSize := partInfo.Size()
partName := filepath.Base(partPath)
+ if err := ctx.Err(); err != nil {
+ return err
+ }
partInputFile, err := upler.Upload(ctx, uploader.NewUpload(partName, partFile, partFileSize))
if err != nil {
return fmt.Errorf("failed to upload split part %s: %w", partPath, err)
@@ -607,13 +835,17 @@ func (t *Telegram) splitUpload(
Filename(filepath.Base(matched[0])).
ForceFile(true).
MIME("application/zip")
- _, err = ctx.Sender.
+ _, err = tctx.Sender.
WithUploader(upler).
To(peer).
- Media(ctx, doc)
+ Media(tctx, doc)
return err
}
+ if len(inputFiles) == 0 {
+ return fmt.Errorf("split upload produced no parts")
+ }
+
multiMedia := make([]message.MultiMediaOption, 0, len(inputFiles))
for i, inputFile := range inputFiles {
doc := message.UploadedDocument(inputFile).
@@ -622,12 +854,12 @@ func (t *Telegram) splitUpload(
multiMedia = append(multiMedia, doc)
}
- sender := ctx.Sender
+ sender := tctx.Sender
if len(multiMedia) <= tglimit.MaxAlbumItems {
_, err = sender.WithUploader(upler).
To(peer).
- Album(ctx, multiMedia[0], multiMedia[1:]...)
+ Album(tctx, multiMedia[0], multiMedia[1:]...)
return err
}
@@ -636,7 +868,7 @@ func (t *Telegram) splitUpload(
batch := multiMedia[i:end]
_, err = sender.WithUploader(upler).
To(peer).
- Album(ctx, batch[0], batch[1:]...)
+ Album(tctx, batch[0], batch[1:]...)
if err != nil {
return fmt.Errorf("failed to send album batch: %w", err)
}
diff --git a/storage/telegram/upload_limit_test.go b/storage/telegram/upload_limit_test.go
index 2f6c4afa..16abc191 100644
--- a/storage/telegram/upload_limit_test.go
+++ b/storage/telegram/upload_limit_test.go
@@ -4,9 +4,9 @@ import (
"bytes"
"testing"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
+ "github.com/krau/mygotg/ext"
)
func TestMaxUploadFileSize(t *testing.T) {
diff --git a/storage/telegram/util.go b/storage/telegram/util.go
index db5a5592..53004354 100644
--- a/storage/telegram/util.go
+++ b/storage/telegram/util.go
@@ -8,10 +8,10 @@ import (
"os"
"time"
- "github.com/celestix/gotgproto/ext"
"github.com/gotd/td/constant"
"github.com/gotd/td/tg"
"github.com/krau/ffmpeg-go"
+ "github.com/krau/mygotg/ext"
"github.com/yapingcat/gomedia/go-mp4"
)
@@ -167,27 +167,53 @@ func extractFrameAt(rs io.ReadSeeker, timestamp float64) ([]byte, error) {
return out.Bytes(), nil
}
-func tryGetInputPeer(ctx *ext.Context, chatID int64) tg.InputPeerClass {
- peer := ctx.PeerStorage.GetInputPeerById(chatID)
- if peer != nil && !peer.Zero() {
+func normalizeInputPeer(peer tg.InputPeerClass) tg.InputPeerClass {
+ switch p := peer.(type) {
+ case *tg.InputPeerChannel:
+ if p.ChannelID < 0 {
+ return &tg.InputPeerChannel{
+ ChannelID: constant.TDLibPeerID(p.ChannelID).ToPlain(),
+ AccessHash: p.AccessHash,
+ }
+ }
+ case *tg.InputPeerChat:
+ if p.ChatID < 0 {
+ return &tg.InputPeerChat{ChatID: constant.TDLibPeerID(p.ChatID).ToPlain()}
+ }
+ }
+ return peer
+}
+
+func ensureInputPeer(ctx *ext.Context, chatID int64) tg.InputPeerClass {
+ if ctx == nil || chatID == 0 {
+ return nil
+ }
+ if peer := tryGetInputPeer(ctx, chatID); peer != nil && !peer.Zero() {
return peer
}
- id := constant.TDLibPeerID(chatID)
- plain := id.ToPlain()
+ peer, err := ctx.ResolveInputPeerById(chatID)
+ if err != nil || peer == nil || peer.Zero() {
+ return nil
+ }
+ return peer
+}
+
+func tryGetInputPeer(ctx *ext.Context, chatID int64) tg.InputPeerClass {
+ plain := constant.TDLibPeerID(chatID).ToPlain()
var channel constant.TDLibPeerID
channel.Channel(plain)
- peer = ctx.PeerStorage.GetInputPeerById(int64(channel))
- if peer != nil && !peer.Zero() {
- return peer
- }
var chat constant.TDLibPeerID
chat.Chat(plain)
- peer = ctx.PeerStorage.GetInputPeerById(int64(chat))
- if peer != nil && !peer.Zero() {
- return peer
- }
var user constant.TDLibPeerID
user.User(plain)
- peer = ctx.PeerStorage.GetInputPeerById(int64(user))
- return peer
+ for _, id := range []int64{chatID, plain, int64(channel), int64(chat), int64(user)} {
+ if id == 0 {
+ continue
+ }
+ peer := ctx.PeerStorage.GetInputPeerById(id)
+ if peer != nil && !peer.Zero() {
+ return normalizeInputPeer(peer)
+ }
+ }
+ return nil
}
diff --git a/storage/telegram/util_test.go b/storage/telegram/util_test.go
index d89a26fa..0c2ca701 100644
--- a/storage/telegram/util_test.go
+++ b/storage/telegram/util_test.go
@@ -2,28 +2,52 @@ package telegram
import (
"os"
+ "os/exec"
+ "path/filepath"
"testing"
)
+func makeTestVideo(t *testing.T) string {
+ t.Helper()
+ if _, err := exec.LookPath("ffmpeg"); err != nil {
+ t.Skip("ffmpeg is not installed")
+ }
+ path := filepath.Join(t.TempDir(), "testvideo.mp4")
+ cmd := exec.CommandContext(t.Context(), "ffmpeg",
+ "-loglevel", "error",
+ "-f", "lavfi",
+ "-i", "color=c=blue:s=64x64:d=1",
+ "-c:v", "mpeg4",
+ "-pix_fmt", "yuv420p",
+ "-y", path,
+ )
+ if output, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("create test video: %v: %s", err, output)
+ }
+ return path
+}
+
func TestExtractThumbFrame(t *testing.T) {
- file, err := os.Open("tests/testvideo")
+ file, err := os.Open(makeTestVideo(t))
if err != nil {
t.Fatalf("failed to open test video: %v", err)
}
- defer file.Close()
+ defer closeTestFile(t, file)
thumb, err := extractThumbFrame(file)
if err != nil {
t.Fatalf("failed to extract thumb frame: %v", err)
}
- os.WriteFile("tests/testthumb.jpg", thumb, 0644)
+ if len(thumb) == 0 {
+ t.Fatal("extracted thumbnail is empty")
+ }
}
func TestGetVideoMetadata(t *testing.T) {
- file, err := os.Open("tests/testvideo")
+ file, err := os.Open(makeTestVideo(t))
if err != nil {
t.Fatalf("failed to open test video: %v", err)
}
- defer file.Close()
+ defer closeTestFile(t, file)
meta, err := getVideoMetadata(file)
if err != nil {
t.Fatalf("failed to get video metadata: %v", err)
@@ -32,3 +56,10 @@ func TestGetVideoMetadata(t *testing.T) {
t.Fatalf("invalid video metadata: %+v", meta)
}
}
+
+func closeTestFile(t *testing.T, file *os.File) {
+ t.Helper()
+ if err := file.Close(); err != nil {
+ t.Errorf("close test file: %v", err)
+ }
+}
diff --git a/storage/telegram/video_split.go b/storage/telegram/video_split.go
index afb5c551..0feb7fcf 100644
--- a/storage/telegram/video_split.go
+++ b/storage/telegram/video_split.go
@@ -13,9 +13,9 @@ import (
"strconv"
"strings"
- "github.com/celestix/gotgproto/ext"
"github.com/charmbracelet/log"
"github.com/gotd/td/telegram/message"
+ "github.com/krau/mygotg/ext"
"github.com/rs/xid"
"github.com/krau/SaveAny-Bot/config"
diff --git a/storage/webdav/client.go b/storage/webdav/client.go
index cd66898d..b6155106 100644
--- a/storage/webdav/client.go
+++ b/storage/webdav/client.go
@@ -11,6 +11,7 @@ import (
"strings"
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
+ "github.com/krau/SaveAny-Bot/pkg/storagetypes"
)
type Client struct {
@@ -148,13 +149,37 @@ func (c *Client) MkDir(ctx context.Context, dirPath string) error {
}
func (c *Client) WriteFile(ctx context.Context, remotePath string, content io.Reader) error {
+ return c.writeFile(ctx, remotePath, content, false)
+}
+
+// WriteFileIfAbsent creates a file without replacing an existing one.
+func (c *Client) WriteFileIfAbsent(ctx context.Context, remotePath string, content io.Reader) error {
+ return c.writeFile(ctx, remotePath, content, true)
+}
+
+func (c *Client) writeFile(ctx context.Context, remotePath string, content io.Reader, ifAbsent bool) error {
u, err := url.Parse(c.BaseURL)
if err != nil {
return err
}
parts := strings.Split(strings.Trim(remotePath, "/"), "/")
u.Path = path.Join(u.Path, strings.Join(parts, "/"))
- resp, err := c.doRequest(ctx, WebdavMethodPut, u.String(), content)
+ req, err := http.NewRequestWithContext(ctx, string(WebdavMethodPut), u.String(), content)
+ if err != nil {
+ return err
+ }
+ if c.Username != "" && c.Password != "" {
+ req.SetBasicAuth(c.Username, c.Password)
+ }
+ if length := ctx.Value(ctxkey.ContentLength); length != nil {
+ if l, ok := length.(int64); ok {
+ req.ContentLength = l
+ }
+ }
+ if ifAbsent {
+ req.Header.Set("If-None-Match", "*")
+ }
+ resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
@@ -163,6 +188,9 @@ func (c *Client) WriteFile(ctx context.Context, remotePath string, content io.Re
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
+ if ifAbsent && (resp.StatusCode == http.StatusPreconditionFailed || resp.StatusCode == http.StatusConflict) {
+ return storagetypes.ErrAlreadyExists
+ }
return fmt.Errorf("PUT: %s", resp.Status)
}
diff --git a/storage/webdav/webdav.go b/storage/webdav/webdav.go
index 27baba99..8f97a95f 100644
--- a/storage/webdav/webdav.go
+++ b/storage/webdav/webdav.go
@@ -35,7 +35,13 @@ func (w *Webdav) Init(ctx context.Context, cfg config.StorageConfig) error {
w.config = *webdavConfig
w.logger = log.FromContext(ctx).WithPrefix(fmt.Sprintf("webdav[%s]", w.config.Name))
w.client = NewClient(w.config.URL, w.config.Username, w.config.Password, &http.Client{
- Timeout: time.Hour * 12,
+ Transport: &http.Transport{
+ Proxy: http.ProxyFromEnvironment,
+ TLSHandshakeTimeout: 10 * time.Second,
+ ResponseHeaderTimeout: 30 * time.Second,
+ IdleConnTimeout: 90 * time.Second,
+ MaxIdleConns: 100,
+ },
})
return nil
}
@@ -48,47 +54,61 @@ func (w *Webdav) Name() string {
return w.config.Name
}
-func (w *Webdav) JoinStoragePath(p string) string {
- return path.Join(w.config.BasePath, p)
+func (w *Webdav) JoinStoragePath(p string) (string, error) {
+ return storagetypes.SecureJoin(w.config.BasePath, p)
}
func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) error {
w.logger.Infof("Saving file to %s", storagePath)
- candidate := w.JoinStoragePath(storagePath)
- if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
- candidate = fsutil.UniquePath(w.config.BasePath, storagePath, func(c string) bool {
+ candidate, err := w.JoinStoragePath(storagePath)
+ if err != nil {
+ return err
+ }
+ overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool)
+ if !overwrite {
+ candidate, err = fsutil.UniquePath(w.config.BasePath, storagePath, func(c string) (bool, error) {
return w.existsPath(ctx, c)
}, 1000)
+ if err != nil {
+ return err
+ }
}
if err := w.client.MkDir(ctx, path.Dir(candidate)); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
- if err := w.client.WriteFile(ctx, candidate, r); err != nil {
+ if overwrite {
+ err = w.client.WriteFile(ctx, candidate, r)
+ } else {
+ err = w.client.WriteFileIfAbsent(ctx, candidate, r)
+ }
+ if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
return nil
}
-func (w *Webdav) Exists(ctx context.Context, storagePath string) bool {
+func (w *Webdav) Exists(ctx context.Context, storagePath string) (bool, error) {
w.logger.Debugf("Checking if file exists at %s", storagePath)
- return w.existsPath(ctx, w.JoinStoragePath(storagePath))
-}
-
-func (w *Webdav) existsPath(ctx context.Context, storagePath string) bool {
- exists, err := w.client.Exists(ctx, storagePath)
+ fullPath, err := w.JoinStoragePath(storagePath)
if err != nil {
- w.logger.Errorf("Failed to check if file exists at %s: %v", storagePath, err)
- return false
+ return false, err
}
- return exists
+ return w.existsPath(ctx, fullPath)
+}
+
+func (w *Webdav) existsPath(ctx context.Context, storagePath string) (bool, error) {
+ return w.client.Exists(ctx, storagePath)
}
// ListFiles implements storage.StorageListable
func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error) {
w.logger.Infof("Listing files in %s", dirPath)
- fullPath := path.Join(w.config.BasePath, dirPath)
+ fullPath, err := w.JoinStoragePath(dirPath)
+ if err != nil {
+ return nil, err
+ }
responses, err := w.client.ListDir(ctx, fullPath)
if err != nil {
@@ -141,7 +161,10 @@ func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
func (w *Webdav) OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error) {
w.logger.Infof("Opening file %s", filePath)
- fullPath := path.Join(w.config.BasePath, filePath)
+ fullPath, err := w.JoinStoragePath(filePath)
+ if err != nil {
+ return nil, 0, err
+ }
reader, size, err := w.client.ReadFile(ctx, fullPath)
if err != nil {
diff --git a/web/dist/.gitkeep b/web/dist/.gitkeep
new file mode 100644
index 00000000..e69de29b
diff --git a/web/embed.go b/web/embed.go
new file mode 100644
index 00000000..9baa84e1
--- /dev/null
+++ b/web/embed.go
@@ -0,0 +1,6 @@
+package web
+
+import "embed"
+
+//go:embed all:dist
+var distFS embed.FS
diff --git a/web/server.go b/web/server.go
new file mode 100644
index 00000000..75e7f485
--- /dev/null
+++ b/web/server.go
@@ -0,0 +1,145 @@
+package web
+
+import (
+ "context"
+ "fmt"
+ "io/fs"
+ "net"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+
+ "github.com/charmbracelet/log"
+)
+
+type Server struct {
+ listen string
+ authToken string
+ apiHandler http.Handler
+ httpServer *http.Server
+}
+
+func New(listen, authToken string, apiHandler http.Handler) *Server {
+ s := &Server{listen: listen, authToken: authToken, apiHandler: apiHandler}
+ root := http.NewServeMux()
+ root.Handle("/api/", apiHandler)
+ root.Handle("/health", apiHandler)
+ root.HandleFunc("/", s.serveUI)
+ s.httpServer = &http.Server{
+ Handler: securityHeaders(root),
+ ReadHeaderTimeout: 10 * time.Second,
+ ReadTimeout: 30 * time.Second,
+ WriteTimeout: 30 * time.Second,
+ IdleTimeout: 120 * time.Second,
+ }
+ return s
+}
+
+func securityHeaders(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("X-Content-Type-Options", "nosniff")
+ w.Header().Set("X-Frame-Options", "DENY")
+ w.Header().Set("Referrer-Policy", "no-referrer")
+ next.ServeHTTP(w, r)
+ })
+}
+
+func (s *Server) Start(ctx context.Context) error {
+ ln, err := net.Listen("tcp", s.listen)
+ if err != nil {
+ return fmt.Errorf("failed to listen on %s: %w", s.listen, err)
+ }
+ log.FromContext(ctx).Infof("Starting web server on %s", ln.Addr())
+ if uiURL := webURL(ln.Addr(), s.authToken); uiURL != "" {
+ log.FromContext(ctx).Infof("Web UI available at %s", uiURL)
+ }
+ go func() {
+ if err := s.httpServer.Serve(ln); err != nil && err != http.ErrServerClosed {
+ log.FromContext(ctx).Errorf("Web server error: %v", err)
+ }
+ }()
+ go func() {
+ <-ctx.Done()
+ stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ if err := s.httpServer.Shutdown(stopCtx); err != nil {
+ log.FromContext(ctx).Errorf("Web server shutdown error: %v", err)
+ }
+ }()
+ return nil
+}
+
+func (s *Server) Stop(ctx context.Context) error {
+ return s.httpServer.Shutdown(ctx)
+}
+
+func webURL(addr net.Addr, token string) string {
+ if addr == nil {
+ return ""
+ }
+ host, port, err := net.SplitHostPort(addr.String())
+ if err != nil {
+ return ""
+ }
+ if ip := net.ParseIP(host); ip != nil && ip.IsUnspecified() {
+ if ip.To4() != nil {
+ host = "127.0.0.1"
+ } else {
+ host = "::1"
+ }
+ }
+ u := url.URL{Scheme: "http", Host: net.JoinHostPort(host, port), Path: "/"}
+ if token != "" {
+ query := u.Query()
+ query.Set("token", token)
+ u.RawQuery = query.Encode()
+ }
+ return u.String()
+}
+
+func (s *Server) serveUI(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodGet && r.Method != http.MethodHead {
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+ if r.Method == http.MethodGet {
+ if token := strings.TrimSpace(r.URL.Query().Get("token")); token != "" {
+ query := url.Values{"token": {token}}
+ w.Header().Set("Cache-Control", "no-store")
+ http.Redirect(w, r, "/api/v1/auth/token?"+query.Encode(), http.StatusSeeOther)
+ return
+ }
+ }
+ sub, err := fs.Sub(distFS, "dist")
+ if err != nil {
+ http.Error(w, "Web UI not built: run make web", http.StatusServiceUnavailable)
+ return
+ }
+ index, err := fs.ReadFile(sub, "index.html")
+ if err != nil {
+ http.Error(w, "Web UI not built: run make web", http.StatusServiceUnavailable)
+ return
+ }
+ path := strings.TrimPrefix(r.URL.Path, "/")
+ if path != "" {
+ if file, err := sub.Open(path); err == nil {
+ info, statErr := file.Stat()
+ if closeErr := file.Close(); closeErr != nil {
+ log.Warnf("Failed to close static asset %s: %v", path, closeErr)
+ }
+ if statErr == nil && !info.IsDir() {
+ if strings.HasPrefix(path, "assets/") {
+ w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
+ }
+ http.FileServer(http.FS(sub)).ServeHTTP(w, r)
+ return
+ }
+ }
+ }
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ w.Header().Set("Cache-Control", "no-cache")
+ if _, err := w.Write(index); err != nil {
+ log.Warnf("Failed to write web index: %v", err)
+ }
+}
diff --git a/web/ui/.oxfmtrc.json b/web/ui/.oxfmtrc.json
new file mode 100644
index 00000000..9fa8fe95
--- /dev/null
+++ b/web/ui/.oxfmtrc.json
@@ -0,0 +1,7 @@
+{
+ "semi": false,
+ "singleQuote": true,
+ "printWidth": 100,
+ "trailingComma": "all",
+ "vueIndentScriptAndStyle": false
+}
diff --git a/web/ui/AGENTS.md b/web/ui/AGENTS.md
new file mode 100644
index 00000000..e49f5118
--- /dev/null
+++ b/web/ui/AGENTS.md
@@ -0,0 +1,13 @@
+# Web UI conventions
+
+- Transient action results and validation errors MUST use `useToastStore` / `useFormFeedback`. Do not insert error or success banners into the document flow; they shift the interface.
+- Field validation MUST call `fail(message, elementId)` and can use the shared `.field.is-invalid` state. Clear the state on input.
+- Persistent domain state such as a task's stored error or a connection failure may remain inline.
+- Async buttons MUST use `LoadingButton`; keep the normal label and let the component overlay a spinner. Never replace labels with longer "saving..." text or animate button width.
+- Destructive or irreversible actions MUST use `useConfirmStore`; never call `window.confirm`.
+- Motion MUST use the tokens in `src/assets/main.css`, animate only transform/opacity/filter where possible, and keep the global `prefers-reduced-motion` fallback intact.
+- Theme-aware colors MUST use the CSS variables in `src/assets/main.css`; never hardcode a light-only surface or text color. Theme preference is managed by `useThemeStore`.
+- Modals whose form content changes must tween between content heights instead of using a tall fixed placeholder; keep the actions pinned and scroll only when the viewport constrains the form.
+- UI copy should state the current action or state, not explain the obvious. Avoid page subtitles and explanatory prose in empty states.
+- UI copy MUST use `vue-i18n` keys and provide both `zhHans` and `en` messages; never hardcode user-facing Chinese outside locale resources.
+- Use `pnpm` for every install and script. Do not add npm lockfiles.
diff --git a/web/ui/e2e/auth.setup.ts b/web/ui/e2e/auth.setup.ts
new file mode 100644
index 00000000..cd17ad9c
--- /dev/null
+++ b/web/ui/e2e/auth.setup.ts
@@ -0,0 +1,14 @@
+import fs from 'node:fs'
+import path from 'node:path'
+import { test } from '@playwright/test'
+import { login } from './helpers'
+
+test('authenticate', async ({ page }) => {
+ await login(page)
+ const statePath = process.env.SAVEANY_E2E_AUTH_STATE
+ if (!statePath) {
+ throw new Error('SAVEANY_E2E_AUTH_STATE is not set')
+ }
+ fs.mkdirSync(path.dirname(statePath), { recursive: true })
+ await page.context().storageState({ path: statePath })
+})
diff --git a/web/ui/e2e/auth.spec.ts b/web/ui/e2e/auth.spec.ts
new file mode 100644
index 00000000..a5c00ebc
--- /dev/null
+++ b/web/ui/e2e/auth.spec.ts
@@ -0,0 +1,22 @@
+import { expect, test } from '@playwright/test'
+import { login } from './helpers'
+
+test.describe('unauthenticated', () => {
+ test.use({ storageState: { cookies: [], origins: [] } })
+
+ test('rejects a wrong password', async ({ page }) => {
+ await page.goto('/login')
+ await page.locator('#login-password').fill('wrong')
+ await page.locator('form').getByRole('button').click()
+ await expect(page).toHaveURL(/\/login$/)
+ await expect(page.locator('#login-password')).toHaveAttribute('aria-invalid', 'true')
+ })
+
+ test('logs in and loads the overview', async ({ page }) => {
+ await login(page)
+ await expect(page.getByRole('heading', { name: 'Overview' })).toBeVisible()
+ await page.getByRole('link', { name: 'Storages' }).click()
+ await expect(page).toHaveURL(/\/storages$/)
+ await expect(page.locator('body')).toContainText('test-local')
+ })
+})
diff --git a/web/ui/e2e/helpers.ts b/web/ui/e2e/helpers.ts
new file mode 100644
index 00000000..2cb9d1ba
--- /dev/null
+++ b/web/ui/e2e/helpers.ts
@@ -0,0 +1,8 @@
+import { expect, type Page } from '@playwright/test'
+
+export async function login(page: Page, password = 'test-admin'): Promise {
+ await page.goto('/login')
+ await page.locator('#login-password').fill(password)
+ await page.locator('form').getByRole('button').click()
+ await expect(page).toHaveURL(/\/$/)
+}
diff --git a/web/ui/e2e/storages.spec.ts b/web/ui/e2e/storages.spec.ts
new file mode 100644
index 00000000..12bbf47f
--- /dev/null
+++ b/web/ui/e2e/storages.spec.ts
@@ -0,0 +1,27 @@
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { expect, test } from '@playwright/test'
+
+test('tests the seeded storage', async ({ page }) => {
+ await page.goto('/storages')
+ const card = page.locator('.storage-card', { hasText: 'test-local' })
+ await expect(card).toBeVisible()
+ await card.getByRole('button', { name: 'Test' }).click()
+ await expect(page.locator('body')).toContainText('test-local test passed')
+})
+
+test('creates a local storage', async ({ page }) => {
+ await page.goto('/storages')
+ const name = `ui-${Date.now()}`
+ const basePath = fs.mkdtempSync(path.join(os.tmpdir(), 'saveany-ui-storage-'))
+
+ await page.getByRole('button', { name: 'Create storage' }).first().click()
+ await page.locator('#storage-name').fill(name)
+ await page.locator('#storage-type').click()
+ await page.getByRole('option', { name: 'Local disk' }).click()
+ await page.locator('#storage-field-base_path').fill(basePath)
+ await page.getByRole('dialog').getByRole('button', { name: 'Create storage' }).click()
+
+ await expect(page.locator('.storage-card', { hasText: name })).toBeVisible()
+})
diff --git a/web/ui/e2e/tasks.spec.ts b/web/ui/e2e/tasks.spec.ts
new file mode 100644
index 00000000..a64c3122
--- /dev/null
+++ b/web/ui/e2e/tasks.spec.ts
@@ -0,0 +1,42 @@
+import http, { type Server } from 'node:http'
+import type { AddressInfo } from 'node:net'
+import { expect, test } from '@playwright/test'
+
+let server: Server | undefined
+let fileURL = ''
+
+test.beforeAll(async () => {
+ server = http.createServer((_request, response) => {
+ response.writeHead(200, { 'Content-Type': 'text/plain' })
+ response.end('SaveAny-Bot UI E2E\n')
+ })
+ const listening = Promise.withResolvers()
+ server.listen(0, '127.0.0.1', listening.resolve)
+ await listening.promise
+ const address = server.address() as AddressInfo
+ fileURL = `http://127.0.0.1:${address.port}/ui-e2e.txt`
+})
+
+test.afterAll(async () => {
+ if (server) {
+ const closed = Promise.withResolvers()
+ server.close(() => closed.resolve())
+ await closed.promise
+ }
+})
+
+test('creates a direct-links task from the UI', async ({ page }) => {
+ await page.goto('/tasks')
+ const target = `ui-${Date.now()}`
+
+ await page.getByRole('button', { name: 'Create task' }).first().click()
+ await page.locator('#task-type').click()
+ await page.getByRole('option', { name: 'Direct link' }).click()
+ await page.locator('#task-field-urls').fill(fileURL)
+ await page.locator('#task-path').fill(target)
+ await page.getByRole('dialog').getByRole('button', { name: 'Create task' }).click()
+
+ const row = page.locator('.task-table tbody tr', { hasText: target })
+ await expect(row).toBeVisible()
+ await expect(row).toContainText('Completed')
+})
diff --git a/web/ui/e2e/views.spec.ts b/web/ui/e2e/views.spec.ts
new file mode 100644
index 00000000..9ac8e6b6
--- /dev/null
+++ b/web/ui/e2e/views.spec.ts
@@ -0,0 +1,28 @@
+import { expect, test } from '@playwright/test'
+
+const views: Array<[string, string]> = [
+ ['/', 'Overview'],
+ ['/guide', 'Guide'],
+ ['/tasks', 'Tasks'],
+ ['/storages', 'Storages'],
+ ['/users', 'Users'],
+ ['/telegram', 'Telegram'],
+ ['/settings', 'Settings'],
+ ['/system', 'System'],
+]
+
+test('loads every console view without errors', async ({ page }) => {
+ const failures: string[] = []
+ page.on('pageerror', (error) => failures.push(`pageerror: ${error.message}`))
+ page.on('response', (response) => {
+ if (response.status() >= 500) {
+ failures.push(`${response.status()} ${response.request().method()} ${response.url()}`)
+ }
+ })
+
+ for (const [route, heading] of views) {
+ await page.goto(route)
+ await expect(page.getByRole('heading', { name: heading }).first()).toBeVisible()
+ }
+ expect(failures).toEqual([])
+})
diff --git a/web/ui/index.html b/web/ui/index.html
new file mode 100644
index 00000000..d012a725
--- /dev/null
+++ b/web/ui/index.html
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+
+
+ SaveAny-Bot
+
+
+
+
+
+
diff --git a/web/ui/package.json b/web/ui/package.json
new file mode 100644
index 00000000..2ecc42c5
--- /dev/null
+++ b/web/ui/package.json
@@ -0,0 +1,29 @@
+{
+ "name": "saveany-bot-web",
+ "version": "1.0.0",
+ "private": true,
+ "type": "module",
+ "scripts": {
+ "dev": "vite",
+ "build": "vite build",
+ "typecheck": "vue-tsc --noEmit",
+ "test:e2e": "playwright test"
+ },
+ "dependencies": {
+ "pinia": "^4.1.0",
+ "vue": "^3.5.44",
+ "vue-i18n": "^11.4.16",
+ "vue-router": "^5.4.0"
+ },
+ "devDependencies": {
+ "@playwright/test": "^1.64.1",
+ "@vitejs/plugin-vue": "^6.0.10",
+ "typescript": "^6.0.3",
+ "vite": "^8.3.5",
+ "vue-tsc": "^3.3.14"
+ },
+ "engines": {
+ "node": ">=26"
+ },
+ "packageManager": "pnpm@12.14.0"
+}
diff --git a/web/ui/playwright.config.ts b/web/ui/playwright.config.ts
new file mode 100644
index 00000000..3455bfb9
--- /dev/null
+++ b/web/ui/playwright.config.ts
@@ -0,0 +1,47 @@
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { defineConfig } from '@playwright/test'
+
+const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
+const port = process.env.E2E_PORT || '28081'
+const baseURL = process.env.E2E_BASE_URL || `http://127.0.0.1:${port}`
+const bin = process.env.SAVEANY_E2E_BIN || path.join(repoRoot, 'tmp', 'saveany-bot-e2e')
+const outputDir = path.join(repoRoot, 'tmp', 'e2e-web-results')
+const authState = path.join(outputDir, 'auth.json')
+process.env.SAVEANY_E2E_AUTH_STATE = authState
+
+export default defineConfig({
+ testDir: './e2e',
+ timeout: 60_000,
+ expect: { timeout: 15_000 },
+ fullyParallel: false,
+ workers: 1,
+ reporter: [['list']],
+ outputDir,
+ projects: [
+ { name: 'setup', testMatch: /auth\.setup\.ts/ },
+ {
+ name: 'chromium',
+ testIgnore: /auth\.setup\.ts/,
+ dependencies: ['setup'],
+ use: { storageState: authState },
+ },
+ ],
+ use: {
+ baseURL,
+ locale: 'en-US',
+ viewport: { width: 1280, height: 900 },
+ trace: 'retain-on-failure',
+ screenshot: 'only-on-failure',
+ },
+ webServer: process.env.E2E_BASE_URL
+ ? undefined
+ : {
+ command: `"${bin}" testenv --listen 127.0.0.1:${port} --admin-password test-admin --storage-name test-local --user-chat-id 10001`,
+ url: `${baseURL}/health`,
+ cwd: repoRoot,
+ timeout: 120_000,
+ reuseExistingServer: false,
+ gracefulShutdown: { signal: 'SIGINT', timeout: 10_000 },
+ },
+})
diff --git a/web/ui/pnpm-lock.yaml b/web/ui/pnpm-lock.yaml
new file mode 100644
index 00000000..75ab478b
--- /dev/null
+++ b/web/ui/pnpm-lock.yaml
@@ -0,0 +1,1394 @@
+---
+lockfileVersion: '9.0'
+
+importers:
+
+ .:
+ configDependencies: {}
+ packageManagerDependencies:
+ pnpm:
+ specifier: 12.14.0
+ version: 12.14.0
+
+packages:
+
+ '@pnpm/exe.android-arm64@12.14.0':
+ resolution: {integrity: sha512-+iSh7x21C3A2hqc+Z0ZmbKTcn6YNlzrXM142PsnQH/dLj0DYxFzoBvc6XAnL/yfQ5nnH2ebBiRj/zVxr61fZbQ==}
+ cpu: [arm64]
+ os: [android]
+
+ '@pnpm/exe.android-x64@12.14.0':
+ resolution: {integrity: sha512-qT+0LqEr2wBVaxcG2T8cX/R8SSRz2Rvpp8nRR4fh8tnGUZRch87gFmsHUAoSQh7w6j1h0qTjrA384j4xi8frvw==}
+ cpu: [x64]
+ os: [android]
+
+ '@pnpm/exe.darwin-arm64@12.14.0':
+ resolution: {integrity: sha512-tnrdUB8ecJoehuKKqi1YHCBVl61xtJ9XVGcx5EviC6fWO4DeGI8GtXrjvJxDBM3nuVjJeWistgsUp/N142x4sw==}
+ cpu: [arm64]
+ os: [darwin]
+
+ '@pnpm/exe.darwin-x64@12.14.0':
+ resolution: {integrity: sha512-vlBfGkruqQRsEHTW43cPwpYd7QlbIJ//cvpAdb3bVXkI8SgTPoXhCnc05m+ZNiPrrEkBsX/t7KHw9aXpkzPm3Q==}
+ cpu: [x64]
+ os: [darwin]
+
+ '@pnpm/exe.freebsd-x64@12.14.0':
+ resolution: {integrity: sha512-Qg5rxBrGYl6Tfso+EyygPf84fY1Fzy55E/0or+iuYKtnf0vc0MlHJfVIlT7pBukMRntQHRik7hUi0Z4s7AlJsw==}
+ cpu: [x64]
+ os: [freebsd]
+
+ '@pnpm/exe.linux-arm64-musl@12.14.0':
+ resolution: {integrity: sha512-DRIe4yQkXBGA/PhVJdzlbnllxBdAtnMMvTVLLcGpsK5FvJz9kxD2Hyxtn68ovmNkVX06f6p45cQwfm3wx2jn1A==}
+ cpu: [arm64]
+ os: [linux]
+ libc: [musl]
+
+ '@pnpm/exe.linux-arm64@12.14.0':
+ resolution: {integrity: sha512-7joGShZ44Nk3WpNU+60JoOPjCXlgxhJAq11S2UiVtwCUDgTer1yTPPfxGW8pxaD6046actYLIYDbZNlubHWs0A==}
+ cpu: [arm64]
+ os: [linux]
+ libc: [glibc]
+
+ '@pnpm/exe.linux-ppc64@12.14.0':
+ resolution: {integrity: sha512-gSoryn7ySdXd//gjgf3MvvA8ZerJayaSbuMZMtAWq4vb2/wlVEgpl0Kvqt54y58TRj533Qjq2txKgnxd4JyoAQ==}
+ cpu: [ppc64]
+ os: [linux]
+ libc: [glibc]
+
+ '@pnpm/exe.linux-riscv64@12.14.0':
+ resolution: {integrity: sha512-EMjew1H9WqQ7pL+tYqCPwiuB+0PeCiJRV7fyWeuFw0wwYUajFBjcaPO3IGAVu9sdmJDU+e+HYv/Zey6aKae0Vw==}
+ cpu: [riscv64]
+ os: [linux]
+ libc: [glibc]
+
+ '@pnpm/exe.linux-s390x@12.14.0':
+ resolution: {integrity: sha512-shYOj/SFoXD35fHe0pWr2JofwSXulC0BnEIXLj2QokDi0EF//AfKsui35yKIkTEl+s2VJybc3PO4iJNhKIj5Qw==}
+ cpu: [s390x]
+ os: [linux]
+ libc: [glibc]
+
+ '@pnpm/exe.linux-x64-musl@12.14.0':
+ resolution: {integrity: sha512-btZiZ2Ot0llKK1ndSfA5i3IRX7/S0VrmzseNH/BJ4c+Cady+S81H6vvU1zByggHMyqQhC8OgxO5PiIY7D9bH0Q==}
+ cpu: [x64]
+ os: [linux]
+ libc: [musl]
+
+ '@pnpm/exe.linux-x64@12.14.0':
+ resolution: {integrity: sha512-F6K8e/jWxiuEYJaQtRRUG9ynBSTEHa+t7Lf86MKuOO5UA7WVVMbi+oB8nY6iKW++ncWS2yG6T9faXZGxqNsfoQ==}
+ cpu: [x64]
+ os: [linux]
+ libc: [glibc]
+
+ '@pnpm/exe.win32-arm64@12.14.0':
+ resolution: {integrity: sha512-DOjlkVB6TtTKeAAe0hNZXn5S7YqESmo1Z6SGbR4BZNQ4isRg31XVHjX2RFTbY8gnZq68IwzyLmPqlEjMmYxofg==}
+ cpu: [arm64]
+ os: [win32]
+
+ '@pnpm/exe.win32-x64@12.14.0':
+ resolution: {integrity: sha512-/LSX6mew/sLf08XqcjemuklNKf1K/Ftpgj4//hK/HKzI2dKbmxDB27a/2OaQ5a0O3/3OFzlssXDziZsi+yVBwQ==}
+ cpu: [x64]
+ os: [win32]
+
+ pnpm@12.14.0:
+ resolution: {integrity: sha512-wYum2HmT09Fky16JSPMrLAbuhUSH4GzY2GeBNC+z5btGNDZwNHEehoArqpw3vqkGzhkprQjXCITJfYAd8TOJpg==}
+ engines: {node: '>=18.*'}
+ hasBin: true
+
+snapshots:
+
+ '@pnpm/exe.android-arm64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.android-x64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.darwin-arm64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.darwin-x64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.freebsd-x64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-arm64-musl@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-arm64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-ppc64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-riscv64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-s390x@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-x64-musl@12.14.0':
+ optional: true
+
+ '@pnpm/exe.linux-x64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.win32-arm64@12.14.0':
+ optional: true
+
+ '@pnpm/exe.win32-x64@12.14.0':
+ optional: true
+
+ pnpm@12.14.0:
+ optionalDependencies:
+ '@pnpm/exe.android-arm64': 12.14.0
+ '@pnpm/exe.android-x64': 12.14.0
+ '@pnpm/exe.darwin-arm64': 12.14.0
+ '@pnpm/exe.darwin-x64': 12.14.0
+ '@pnpm/exe.freebsd-x64': 12.14.0
+ '@pnpm/exe.linux-arm64': 12.14.0
+ '@pnpm/exe.linux-arm64-musl': 12.14.0
+ '@pnpm/exe.linux-ppc64': 12.14.0
+ '@pnpm/exe.linux-riscv64': 12.14.0
+ '@pnpm/exe.linux-s390x': 12.14.0
+ '@pnpm/exe.linux-x64': 12.14.0
+ '@pnpm/exe.linux-x64-musl': 12.14.0
+ '@pnpm/exe.win32-arm64': 12.14.0
+ '@pnpm/exe.win32-x64': 12.14.0
+
+---
+lockfileVersion: '9.0'
+
+settings:
+ autoInstallPeers: true
+ excludeLinksFromLockfile: false
+
+importers:
+
+ .:
+ dependencies:
+ pinia:
+ specifier: ^4.1.0
+ version: 4.1.0(@vue/devtools-api@8.2.1)(typescript@6.0.3)(vue@3.5.44(typescript@6.0.3))
+ vue:
+ specifier: ^3.5.44
+ version: 3.5.44(typescript@6.0.3)
+ vue-i18n:
+ specifier: ^11.4.16
+ version: 11.4.16(vue@3.5.44(typescript@6.0.3))
+ vue-router:
+ specifier: ^5.4.0
+ version: 5.4.0(@vue/compiler-sfc@3.5.44)(pinia@4.1.0(@vue/devtools-api@8.2.1)(typescript@6.0.3)(vue@3.5.44(typescript@6.0.3)))(rolldown@1.2.16)(vite@8.3.5)(vue@3.5.44(typescript@6.0.3))
+ devDependencies:
+ '@playwright/test':
+ specifier: ^1.64.1
+ version: 1.64.1
+ '@vitejs/plugin-vue':
+ specifier: ^6.0.10
+ version: 6.0.10(vite@8.3.5)(vue@3.5.44(typescript@6.0.3))
+ typescript:
+ specifier: ^6.0.3
+ version: 6.0.3
+ vite:
+ specifier: ^8.3.5
+ version: 8.3.5
+ vue-tsc:
+ specifier: ^3.3.14
+ version: 3.3.14(typescript@6.0.3)
+
+packages:
+
+ '@babel/helper-string-parser@7.29.11':
+ resolution: {integrity: sha512-snZbO7MEDOof+hvNa97EjEY7qD7h2kwvQr5AJC3ZhQudKW5dnej6k3kP9NQV88hJwaS8vDqRLFo9rivLEhle2g==}
+ engines: {node: '>=6.9.0'}
+
+ '@babel/helper-string-parser@8.0.8':
+ resolution: {integrity: sha512-fVM+gSuKbdv9cpmyRNh5EvsDHUZCdxQH3FdnFKSkNYHpbsn95bbrxm+ES3MVeQjcqwi2O5ur/DjLfmTQaO60ng==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+
+ '@babel/helper-validator-identifier@7.29.7':
+ resolution: {integrity: sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==}
+ engines: {node: '>=6.9.0'}
+
+ '@babel/helper-validator-identifier@8.0.6':
+ resolution: {integrity: sha512-HYx0YmuchRxRSpnjvcNffnPS8DCauKNmnumIuHG3jnQAVewRA7kfJGwF13ayEpSZs7s4R/oBH4eppuXcAQd5JQ==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+
+ '@babel/parser@7.29.12':
+ resolution: {integrity: sha512-WJ7JBp/Q4+z/ImydtI7jS6NVco5++632ExBemSX+Ysb+G1lCwQ5BVo3SY+jGGOcZXI4ZccbZSUSJLNBW6eEpJg==}
+ engines: {node: '>=6.0.0'}
+ hasBin: true
+
+ '@babel/parser@8.0.10':
+ resolution: {integrity: sha512-h+ah2Kg0ne1Vii892dShj9gky9I5jPzkd8x4s/3xgN/P2bMWmHS3qq59tum3mCVjCBXI8uqfYyYtrfp1Mz6+1w==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+ hasBin: true
+
+ '@babel/types@7.29.11':
+ resolution: {integrity: sha512-WCsemW40Cpc6gouC1SrY6PBaZ+oLRlEVY9XBvZBI7il/HyUrb2nJ505wE1MHEFyDZNnrWDEuy4jL50WVHfTTSQ==}
+ engines: {node: '>=6.9.0'}
+
+ '@babel/types@8.0.10':
+ resolution: {integrity: sha512-KV8Y+teoRsmXeEPS4sCJY1kwV+hcPzHP1qQqgCxrjKOehLdBaxpsFBfoJ8MXWFV80y6Jw56+TM95wR0Per8dYw==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+
+ '@intlify/core-base@11.4.16':
+ resolution: {integrity: sha512-xB8nMdMRLNx7b0zPvrHy0+N9uq9Vfi1aUxHqWgn2e2/TbjCKy8HfoFpnUXXxVP8I0p1ccd6LFSC3XsFjZ3vweA==}
+ engines: {node: '>= 22'}
+
+ '@intlify/devtools-types@11.4.16':
+ resolution: {integrity: sha512-Bl7arICsKgwcSEQuZJun3swenN4jwSpg35lCpijFznYbW9GvkfH4wLxcI5+gz7d/EHqu66UT9CZDIdnQdgMAGg==}
+ engines: {node: '>= 22'}
+
+ '@intlify/message-compiler@11.4.16':
+ resolution: {integrity: sha512-p4lDWmBAsi3ja+PuEfPRavYyvqkYn/Upxz7UpnyI9LgIIy8XawcGIDzN+k99Z+99etO3HmDXGeTxnJIJGgSZ+g==}
+ engines: {node: '>= 22'}
+
+ '@intlify/shared@11.4.16':
+ resolution: {integrity: sha512-xehmkbEgbbNh/V+d8r3hFup0ViaN29YTJSAfRr28C54rxjyZEO0RNQKqW7VVajj16gLdglotNjiq+sIVNtHGzw==}
+ engines: {node: '>= 22'}
+
+ '@jridgewell/gen-mapping@0.3.13':
+ resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
+
+ '@jridgewell/remapping@2.3.5':
+ resolution: {integrity: sha512-LI9u/+laYG4Ds1TDKSJW2YPrIlcVYOwi2fUC6xB43lueCjgxV4lffOCZCtYFiH6TNOX+tQKXx97T4IKHbhyHEQ==}
+
+ '@jridgewell/resolve-uri@3.1.2':
+ resolution: {integrity: sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==}
+ engines: {node: '>=6.0.0'}
+
+ '@jridgewell/sourcemap-codec@1.6.0':
+ resolution: {integrity: sha512-T7jf+5zgsZHwNJ4lvQ7/aezbyk0nNX+zJVWpmHA7VYsEx7a7qr5Rg5IbtJFqkgze5Y2sruq1RUY8Q837Od7iFw==}
+
+ '@jridgewell/trace-mapping@0.3.31':
+ resolution: {integrity: sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==}
+
+ '@oxc-project/types@0.156.0':
+ resolution: {integrity: sha512-1Duyg48TdJNdyhEB8Xn1L36bT9Pv6GUtlKjHxOys8xkaF/rQq3XJE+bAurSK/AgIz5mGFsb77cn9Pb20+WCScw==}
+
+ '@playwright/test@1.64.1':
+ resolution: {integrity: sha512-IaYGml/nSOiZWROfPtuIL7neuu9qDOZ3TNvNljUIrhDMxiGTgPjzj/g9p9RxpS2tSepPDRY+UwAaEeiKtUvIAg==}
+ engines: {node: '>=20'}
+ hasBin: true
+
+ '@rolldown/binding-android-arm-eabi@1.2.16':
+ resolution: {integrity: sha512-ia3DwAs0OsMGS7+IConptSiyk9pEXkOx/AbJ2n/lAGxH3QkWQus1t8d7gnAT38FCU2sQu8lUbeR6AiZFv4JxaA==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm]
+ os: [android]
+
+ '@rolldown/binding-android-arm64@1.2.16':
+ resolution: {integrity: sha512-kx5wBiMB6E7TEvpl2B9gjLU004BkvEm2Dr941BFjx/SontPwSzNuQUIoVKxHHoSjpFIfHMmcguyLOtyn0hObRg==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [android]
+
+ '@rolldown/binding-darwin-arm64@1.2.16':
+ resolution: {integrity: sha512-qslZrXnKJU4cUpjoAmtAo/KQM9e2IRMlmpig6Zpwj+8KWxW8v+nkAf9216Bsek9lOC1jvg2yCjygCFo2f83msQ==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [darwin]
+
+ '@rolldown/binding-darwin-x64@1.2.16':
+ resolution: {integrity: sha512-FdF+y48ND13eAyM2mQn+85kvAqNlvRWROy2rhgA463X/0Gv4vGtazZtfaF1mRBLscYvtOlh4QnLjOWiFzX69OQ==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [x64]
+ os: [darwin]
+
+ '@rolldown/binding-freebsd-x64@1.2.16':
+ resolution: {integrity: sha512-ddjqFM5e95AgLbUPtKIS7V1KTVlA0RUgCVr5mn+/IEhsdy3pr+Vuj/HZ7nlMN2FyxIwUktOH1LEPIQXO1v9y1Q==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [x64]
+ os: [freebsd]
+
+ '@rolldown/binding-linux-arm-gnueabihf@1.2.16':
+ resolution: {integrity: sha512-tHdKWbFkFBI687iim3LIyIvyL7j66VvQYkDPss5Stn/qyE2tWkR1YD0dXmXPFKKqT/o5ZEJVsaRsWS2meQwGOA==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm]
+ os: [linux]
+
+ '@rolldown/binding-linux-arm64-gnu@1.2.16':
+ resolution: {integrity: sha512-FJiLGAblPBMlBEdbOqw6kW6QcXDWAf/oJu7gYs9ZJZwmg+K+/zjFLxPTIY9izJUzxzc24FHvmseyA0IvJSl17w==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [linux]
+ libc: [glibc]
+
+ '@rolldown/binding-linux-arm64-musl@1.2.16':
+ resolution: {integrity: sha512-hrJpblMsrQ58U9veWpbXzkhV5zStunds6HWLprqq1qauvgBr1E6P+24kLj6lSLI9vjRn/RYpjprhkHQPKGWpNg==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [linux]
+ libc: [musl]
+
+ '@rolldown/binding-linux-ppc64-gnu@1.2.16':
+ resolution: {integrity: sha512-qJd/dC6j6CwLG9/s/RP2nb1RZ0GIgj7Ioj7x/SYa3RzjVrlzrmH0fhIOpfvqQDeIsdP0loi+kHaMhJzY1PBIaA==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [ppc64]
+ os: [linux]
+ libc: [glibc]
+
+ '@rolldown/binding-linux-s390x-gnu@1.2.16':
+ resolution: {integrity: sha512-Vx7Xg5uUJuNcujJjI70OICaH0EYj80BtOLZTSN7uOYg8QOLlyKnaG0IW7Fn/J0IpJsaKzmDKMhOQv1JBM9m9vg==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [s390x]
+ os: [linux]
+ libc: [glibc]
+
+ '@rolldown/binding-linux-x64-gnu@1.2.16':
+ resolution: {integrity: sha512-TNKhgE3uJTsTzatUSSYt0Kgo+Oa2uSlSjz4r1aEIea7HX6ULBym4fAlYUhG1i6pjNf2njfCUAfKwr6C3E9Xg1w==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [x64]
+ os: [linux]
+ libc: [glibc]
+
+ '@rolldown/binding-linux-x64-musl@1.2.16':
+ resolution: {integrity: sha512-7chXdpjXZ8qG0+mIxECN3cT9kvhZ3/oZhMX6Pp876P7CDrIy8nJswva6Mq357JR7eYXfuskwZxMeV1SzoZdHrA==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [x64]
+ os: [linux]
+ libc: [musl]
+
+ '@rolldown/binding-openharmony-arm64@1.2.16':
+ resolution: {integrity: sha512-Kwjt+7uNTJK5/MDkGWGVO2KQPnj8yAV75dMpfwyHZrJSXWHrniOB0aUEWlVYf3H1hOqFLIStLdM5QXq6bnKmOQ==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [openharmony]
+
+ '@rolldown/binding-win32-arm64-msvc@1.2.16':
+ resolution: {integrity: sha512-/LH6WfjDRXXJz/p5xl4TmU+jpcnwcxbKEQBW/GZE0JHfN0lGIyTtsUEGaQ/q/GikkGJ2yLKe6pwm7AnEWO8pdw==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [arm64]
+ os: [win32]
+
+ '@rolldown/binding-win32-x64-msvc@1.2.16':
+ resolution: {integrity: sha512-1lv1KvX0Z871zfk7vw+dnXMMnHNyDwMzWG8Kv3HxDXMOhzCX4SHc7HK319fInP0i+0p9D8brkfzL7pJOaGl+tw==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ cpu: [x64]
+ os: [win32]
+
+ '@rolldown/pluginutils@1.0.1':
+ resolution: {integrity: sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==}
+
+ '@types/estree@1.0.10':
+ resolution: {integrity: sha512-lTyOMnOcmRBzdCv6/4zUZCrEhV0xrqQiUZBZtVcbmLHBXKLnN8q89v/wKRF/Q0HbwVsqbIG3o5HDoFl9sduM+g==}
+
+ '@vitejs/plugin-vue@6.0.10':
+ resolution: {integrity: sha512-b80aUQh3n+qEzzZm5oQp3rT2QImeCx58K6eFl/RkJ3d65KW0kTuv5lNsLRVAjbW9YRUpo/wzmvzMgV34bTAItA==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ peerDependencies:
+ vite: ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0
+ vue: ^3.2.25 || ^3.6.0-rc.10
+
+ '@volar/language-core@2.4.30':
+ resolution: {integrity: sha512-73fjwEV8wat6aOZ6U/y/hqtE30ocrIWECg67b0txGczOMNw+MffLmvJ5fnFfN9drVUEYZzLdaNo6VAy6G6avMw==}
+
+ '@volar/language-service@2.4.30':
+ resolution: {integrity: sha512-w/xDsASiqC71ggCOm7sTDXDNk/l72XEptodeRC68tgZ0e5QM4hM3zAqBGC7mBctvhrpYwKfMG+t9xcUo2hEFtA==}
+ peerDependencies:
+ typescript: <7
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
+ '@volar/source-map@2.4.30':
+ resolution: {integrity: sha512-zym1ob/KwDDyN+0jGVtxwbxJSRRnUvv5HspCtz3yEfVVdCG4/dzxJktMGPAgemqRuVs81O0o/y0q3qOUErR8kg==}
+
+ '@volar/typescript@2.4.30':
+ resolution: {integrity: sha512-/DgA5tgYFPfrLFuSjIMXMGHk1LQv2+P8eny6+BhGNzFJELWIW7UZlc56ZLIRFhLsPpMfIFxM8/HvOPBggrjTZg==}
+ peerDependencies:
+ typescript: <7
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
+ '@vue-macros/common@3.1.4':
+ resolution: {integrity: sha512-/5Fv+6DgIcM9ajY05ZmKBv+LMX1M9A0X+IUwDRVdt67ciw8OV9bvG2r34p3RiEadlsQybjhKPRKNXDC8Bp23cw==}
+ engines: {node: '>=20.19.0'}
+ peerDependencies:
+ vue: ^2.7.0 || ^3.2.25
+ peerDependenciesMeta:
+ vue:
+ optional: true
+
+ '@vue/compiler-core@3.5.44':
+ resolution: {integrity: sha512-k34nCytbK8PQ1gR2mGROYOw6Pnv/mumqIdrZ3x2tbyXZDWm7ViKGLTfdB+DRrWqEQsLgGJvn0WN9jBif5zCTLQ==}
+
+ '@vue/compiler-dom@3.5.44':
+ resolution: {integrity: sha512-W5AoBFWdw1lQAKiHowkfgFKFab82oSRT6sGsPfw/q1GRjsy/ZJFLHGhJyBRIFN9APLTUduDH9U2JAiPOIyKuYg==}
+
+ '@vue/compiler-sfc@3.5.44':
+ resolution: {integrity: sha512-dzHmQ4jXTpdr5FjIQUeykVakN/uzqFbUo9/C5ikBFg3a3PeM6uRxeczx1ZbXBML0/zJstEh3ygYipzR5Z7b2Pw==}
+
+ '@vue/compiler-ssr@3.5.44':
+ resolution: {integrity: sha512-xr5gb1JVHlfXJwSlRz1FeP6uxoazdzHuMTVLE7up+4Vrqxk219CvxLPxD9NXBt47yvZW7+p4DXfyp4O1fkByIA==}
+
+ '@vue/devtools-api@6.6.4':
+ resolution: {integrity: sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g==}
+
+ '@vue/devtools-api@8.2.1':
+ resolution: {integrity: sha512-6u4vXBlIBAC1wMplIZgpyPn7uh/s4Bf6F5bMzvLv+EdJ0aHs/+4B7Ygv864EStQSjRbsRzTko/kUG1A1IejQ3A==}
+
+ '@vue/devtools-kit@8.2.1':
+ resolution: {integrity: sha512-FIGIuq3AWReEpbAHY/cRGeHDfI0qOb8OCQ3YjbEAX04uaxIDbGc9rhkbVcG7rnfHPXE3RsU5KrWOu9V/okd8AQ==}
+
+ '@vue/devtools-shared@8.2.1':
+ resolution: {integrity: sha512-Fkac7lUdGReh6pVOi3AYPRGe82LQqRmAfThW7RRligOAP0ZA/Z1z9XLHDM9dv34pV2HRc79DK8uKPeG2fLnA/g==}
+
+ '@vue/language-core@3.3.14':
+ resolution: {integrity: sha512-kRB+3hHg0c+BdD1XLAvwjVMQd5pC05zijSlPDRI3ERvC/s04JlRs5upy2KjeMg/9m9oD5zuMVh9yVvkKfZqoJg==}
+
+ '@vue/reactivity@3.5.44':
+ resolution: {integrity: sha512-u+QJdKeBBEADkPdevsL+6E12QlYVfBD8Zcw5qjDQKtamI6l14KyqazJMobhCKelhWN5zpyKUiEDoTyHE93cqWw==}
+
+ '@vue/runtime-core@3.5.44':
+ resolution: {integrity: sha512-uEa/ndwjhrrhnOfb2p+sZy6zeoamJOISqaLSJGk/8R6nABT1ZldpzcJoPzKj+zHVomVBPcmQYyC2r4LtvDV6CQ==}
+
+ '@vue/runtime-dom@3.5.44':
+ resolution: {integrity: sha512-3fc4ARcXVhVBaJ3ujWiNZJARD6pkyRcwG+ZYhMzjKtKUp7MAY0HLwr7r3Smep3Li6ZaWT6ewdmx8XqdaD5oNZg==}
+
+ '@vue/server-renderer@3.5.44':
+ resolution: {integrity: sha512-R4S1UoH4h5Frrauv8Z5XE5TxV9oBiUSgWIC4TyVK1FK+RocRnOLwFA5Xcmo2011RlgJp+AbZCnw6afrhlgRuUQ==}
+
+ '@vue/shared@3.5.44':
+ resolution: {integrity: sha512-H82XLH0DjmSOdbEwO15x3QTo1f5PWEtv237eLpYKc0mqLu+B8hnq16lFWmuBmNurkeiG3wr+oM1FhOll/YVFRg==}
+
+ acorn@8.19.0:
+ resolution: {integrity: sha512-oJlA3XiRm7Cyk6qFD2Jn8ak9B7jSy0qy00ADO3+8dpT0LSjFihQYv4C02LFCSYJV3Q37xYwwRy5m+IpIiUzqWw==}
+ engines: {node: '>=0.4.0'}
+ hasBin: true
+
+ alien-signals@3.2.1:
+ resolution: {integrity: sha512-I8FjmltrfnDFoZedi5CG8DghVYNhzb/Ijluz7tCSJH0xpd0484Kowhbb1XDYOxfJpU1p5wnM2X54dA+IfGyD1g==}
+
+ ast-kit@2.2.0:
+ resolution: {integrity: sha512-m1Q/RaVOnTp9JxPX+F+Zn7IcLYMzM8kZofDImfsKZd8MbR+ikdOzTeztStWqfrqIxZnYWryyI9ePm3NGjnZgGw==}
+ engines: {node: '>=20.19.0'}
+
+ ast-kit@3.0.0:
+ resolution: {integrity: sha512-8OG92q3R35qjC/4i6BLBMg8IB+fClWu/1PEwg2Z9Rn+BuNaiEgJzpzn+pxWOdHJWDCAwu2JP0wCDTozAM4QirQ==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+
+ ast-walker-scope@0.10.0:
+ resolution: {integrity: sha512-KzvoxDfXNz2Gm8KYLh47K3qcY3Zza3WKOuh2xoqa7LBu6MDKWAZLovJtU9DuAd/Er+rhQHCRn619lHS5U+qmhw==}
+ engines: {node: ^22.18.0 || >=24.11.0}
+
+ birpc@2.9.0:
+ resolution: {integrity: sha512-KrayHS5pBi69Xi9JmvoqrIgYGDkD6mcSe/i6YKi3w5kekCLzrX4+nawcXqrj2tIp50Kw/mT/s3p+GVK0A0sKxw==}
+
+ chokidar@5.0.0:
+ resolution: {integrity: sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw==}
+ engines: {node: '>= 20.19.0'}
+
+ confbox@0.1.8:
+ resolution: {integrity: sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w==}
+
+ confbox@0.3.2:
+ resolution: {integrity: sha512-ZUAKhKlkCOrIGKc8cj7zek9s9z1mveHif/25nUshI2BzCRzF7KH7KHa+Lnh2ubq+e1tIlaFuP763mzW4qVoehg==}
+
+ csstype@3.2.3:
+ resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==}
+
+ detect-libc@2.1.2:
+ resolution: {integrity: sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==}
+ engines: {node: '>=8'}
+
+ entities@7.0.1:
+ resolution: {integrity: sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==}
+ engines: {node: '>=0.12'}
+
+ estree-walker@2.0.2:
+ resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==}
+
+ estree-walker@3.0.3:
+ resolution: {integrity: sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==}
+
+ exsolve@1.2.0:
+ resolution: {integrity: sha512-h1pQPR9XoeSMdntRNCjyTznaume4UpfXrULBnlkZ2CUM/G5eSa+pBByGldO5uR8dxbPC+OuJXsPPNf+JZ81ADg==}
+
+ fdir@6.5.0:
+ resolution: {integrity: sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==}
+ engines: {node: '>=12.0.0'}
+ peerDependencies:
+ picomatch: ^3 || ^4
+ peerDependenciesMeta:
+ picomatch:
+ optional: true
+
+ fsevents@2.3.3:
+ resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
+ engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
+ os: [darwin]
+
+ hookable@5.5.3:
+ resolution: {integrity: sha512-Yc+BQe8SvoXH1643Qez1zqLRmbA5rCL+sSmk6TVos0LWVfNIB7PGncdlId77WzLGSIB5KaWgTaNTs2lNVEI6VQ==}
+
+ lightningcss-android-arm64@1.33.0:
+ resolution: {integrity: sha512-gEpRTalKdosp4Bb8qWtc2iOgE5SeIHlpS1up9bFq2wAyYhl1UdTObYiHe98zEM9SQvSoqQZ1IQD0JNpg3Ml5pg==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm64]
+ os: [android]
+
+ lightningcss-darwin-arm64@1.33.0:
+ resolution: {integrity: sha512-Sciaz8eenNTKn9b3t7+xr0ipTp9YxKQY4npwQ3mrRuL0BAVHBLyZxofhaKBAVtzmtRZ/zTyo0/to4B1uWG/Djg==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm64]
+ os: [darwin]
+
+ lightningcss-darwin-x64@1.33.0:
+ resolution: {integrity: sha512-Z5UPAxzrjlWNNyGy6i65cJzzvgJ5D3T6wMvs+gWpY9d7qRhANrxqAp6LhxIgZhWEw18RfJTGcRxjuLIBr+m8XQ==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [x64]
+ os: [darwin]
+
+ lightningcss-freebsd-x64@1.33.0:
+ resolution: {integrity: sha512-QQM/Ti/hQajJwCY+RiWuCZ9sdtI/XQk7nDK5vC8kkdwixezOlDgvDx7+RT+QjK6FcFT4MpsuoBnHIo/O3StRRg==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [x64]
+ os: [freebsd]
+
+ lightningcss-linux-arm-gnueabihf@1.33.0:
+ resolution: {integrity: sha512-N7FVBe6iS24MlM6R/4RBTxGhQheZGs7tiQ9U32UtF75NzP5Q7xWPRqLBCKxlRQRk3rY1jCIPLzx7WzOhuUIRLQ==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm]
+ os: [linux]
+
+ lightningcss-linux-arm64-gnu@1.33.0:
+ resolution: {integrity: sha512-j2v/itmy4HlNxlc6voKXYgBqNi0Ng2LShg4z7GufpEgs05P+2suBVyi9I6YHq5uoVFx9ETin3eCEhLVyXGQnKg==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm64]
+ os: [linux]
+ libc: [glibc]
+
+ lightningcss-linux-arm64-musl@1.33.0:
+ resolution: {integrity: sha512-yiO5ROMuYQgXbC60yjZU5CYSFZGKXL0HFATXt9mHJn1+zW55oCtMI9NfcVhYLMFDL7gV7oBPon/EmMMGg2OvtQ==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm64]
+ os: [linux]
+ libc: [musl]
+
+ lightningcss-linux-x64-gnu@1.33.0:
+ resolution: {integrity: sha512-ar+Ju7LmcN0Jo4FpL4hpFybwNG9/3A/Br5KW2n2jyODg3MEZXaDYADdemoNS+BDNfMgKvylJLj4S5tyRActuAg==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [x64]
+ os: [linux]
+ libc: [glibc]
+
+ lightningcss-linux-x64-musl@1.33.0:
+ resolution: {integrity: sha512-RYiYbkokw0trfKqqzfF55lginwEPrD3OJDfTuJzFs1MK6iFnDenaz1fqLLtX4ITG3OktJQXOeTaw1awrBAlZPw==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [x64]
+ os: [linux]
+ libc: [musl]
+
+ lightningcss-win32-arm64-msvc@1.33.0:
+ resolution: {integrity: sha512-1K+MPfLSFVpphzpdbfkhlWk6wBrTObBzS2T6db10PNOZgR9GoVsAWzwNyuhUYYbTp23j+4RrncfujZ4uAzXvwA==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [arm64]
+ os: [win32]
+
+ lightningcss-win32-x64-msvc@1.33.0:
+ resolution: {integrity: sha512-OlEICDx/Xl0FqSp4bry8zFnCvGpig3Gl4gCquvYwHuqJKEC1+n9NgDniFvqHGmMv1ZkqDJrDqKKSykTDX+ehuA==}
+ engines: {node: '>= 12.0.0'}
+ cpu: [x64]
+ os: [win32]
+
+ lightningcss@1.33.0:
+ resolution: {integrity: sha512-WkUDrojuJs0xkgGf2udWxa3yGBRxPtxUkB79i6aCZLRgc7PM8fZe9TosfPDcvEpQZbuFASnHYmRLBLUbmLOIIA==}
+ engines: {node: '>= 12.0.0'}
+
+ local-pkg@1.2.1:
+ resolution: {integrity: sha512-++gUqRDEvcnN6Zhqrr+y/CkVEHhlrR96vZn3nZZPYzMcBUyBtTKzB9NadClFIsIVSsu+3i9tfk/erqy9kAmt7Q==}
+ engines: {node: '>=14'}
+
+ magic-string-ast@1.0.3:
+ resolution: {integrity: sha512-CvkkH1i81zl7mmb94DsRiFeG9V2fR2JeuK8yDgS8oiZSFa++wWLEgZ5ufEOyLHbvSbD1gTRKv9NdX69Rnvr9JA==}
+ engines: {node: '>=20.19.0'}
+
+ magic-string@0.30.21:
+ resolution: {integrity: sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==}
+
+ magic-string@1.4.5:
+ resolution: {integrity: sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==}
+
+ mlly@1.8.2:
+ resolution: {integrity: sha512-d+ObxMQFmbt10sretNDytwt85VrbkhhUA/JBGm1MPaWJ65Cl4wOgLaB1NYvJSZ0Ef03MMEU/0xpPMXUIQ29UfA==}
+
+ muggle-string@0.4.1:
+ resolution: {integrity: sha512-VNTrAak/KhO2i8dqqnqnAHOa3cYBwXEZe9h+D5h/1ZqFSTEFHdM65lR7RoIqq3tBBYavsOXV84NoHXZ0AkPyqQ==}
+
+ nanoid@3.3.20:
+ resolution: {integrity: sha512-uKdg2G3GNCKQn9byYOpxbGqrT2fGO5KRt5J/8b3pok8rT6qxGWF6hxMyJiEYtAf+FVyYuD9hRaDqX5uPFYJ4ZQ==}
+ engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
+ hasBin: true
+
+ nostics@1.3.0:
+ resolution: {integrity: sha512-wzRsrJwNlFbqIRQt8aCeL2WE8USip/uuvGSOZV1D02CxX8ABTMbw8IywKjH7yWHETMXbWJI4OBpihg9rmz2Vyg==}
+
+ path-browserify@1.0.1:
+ resolution: {integrity: sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==}
+
+ pathe@2.0.3:
+ resolution: {integrity: sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==}
+
+ perfect-debounce@2.1.0:
+ resolution: {integrity: sha512-LjgdTytVFXeUgtHZr9WYViYSM/g8MkcTPYDlPa3cDqMirHjKiSZPYd6DoL7pK8AJQr+uWkQvCjHNdiMqsrJs+g==}
+
+ picocolors@1.1.1:
+ resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==}
+
+ picomatch@4.0.7:
+ resolution: {integrity: sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==}
+ engines: {node: '>=12'}
+
+ pinia@4.1.0:
+ resolution: {integrity: sha512-+5/HMuVjYQfKlCD5xQ7zfXn1xUzYFEa7pd0DQK5SaMfZCaFRTHlaH/c02N1FoGOdlzSiwf5EOPgJx//1QDw6gw==}
+ peerDependencies:
+ '@vue/devtools-api': ^8.2.1
+ typescript: '>=5.6.0'
+ vue: ^3.5.11
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
+ pkg-types@1.3.1:
+ resolution: {integrity: sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ==}
+
+ pkg-types@2.4.0:
+ resolution: {integrity: sha512-07Fys7iVx0X6TcviMOgogM1Lk33ZyqOezMnU79S+kffWqwolG/5W9V8EgQSeYCIfFTYVgssRbnH630Oa2+soBA==}
+
+ playwright-core@1.64.1:
+ resolution: {integrity: sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ==}
+ engines: {node: '>=20'}
+ hasBin: true
+
+ playwright@1.64.1:
+ resolution: {integrity: sha512-HCrVhHr5QQuwYoswEUGTb7gGp522yflbv/ST8nF4sFWGuLjQCt17Q/EubpGGSNKYn1tsTCPZxqHhHCfVURu7UA==}
+ engines: {node: '>=20'}
+ hasBin: true
+
+ postcss@8.5.32:
+ resolution: {integrity: sha512-LJlKCCAUFKOuZxipUdYw1NzIITEsPcgyOG9LkvRJvuKj/bRzbuRY0ARXvL0pWGpsZvn8jku+LMep2eU0seHp8g==}
+ engines: {node: ^10 || ^12 || >=14}
+
+ quansync@0.2.11:
+ resolution: {integrity: sha512-AifT7QEbW9Nri4tAwR5M/uzpBuqfZf+zwaEM/QkzEjj7NBuFD2rBuy0K3dE+8wltbezDV7JMA0WfnCPYRSYbXA==}
+
+ readdirp@5.1.1:
+ resolution: {integrity: sha512-Kko+Y5XQ6fM+Ce3dq3m9YGxnacYZYl9cA1wZjaF3Vbry2L3i1qVg8+CAgNPsXRArPMUMCaOR7oa9Nqntc43JKA==}
+ engines: {node: '>= 20.19.0'}
+
+ rolldown@1.2.16:
+ resolution: {integrity: sha512-bWvlMpFKiTfDKfnK7FC2MPgPeWCf/Kpwsq49ApdW+FQaBZb6O6dZ4MAPbPUjp7hYQrw/RobqCzn64BRrQecGHw==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ hasBin: true
+
+ scule@1.3.0:
+ resolution: {integrity: sha512-6FtHJEvt+pVMIB9IBY+IcCJ6Z5f1iQnytgyfKMhDKgmzYG+TeH/wx1y3l27rshSbLiSanrR9ffZDrEsmjlQF2g==}
+
+ source-map-js@1.2.2:
+ resolution: {integrity: sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw==}
+ engines: {node: '>=0.10.0'}
+
+ tinyglobby@0.2.17:
+ resolution: {integrity: sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==}
+ engines: {node: '>=12.0.0'}
+
+ typescript@6.0.3:
+ resolution: {integrity: sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==}
+ engines: {node: '>=14.17'}
+ hasBin: true
+
+ ufo@1.6.4:
+ resolution: {integrity: sha512-JFNbkD1Svwe0KvGi8GOeLcP4kAWQ609twvCdcHxq1oSL8svv39ZuSvajcD8B+5D0eL4+s1Is2D/O6KN3qcTeRA==}
+
+ unplugin-utils@0.3.2:
+ resolution: {integrity: sha512-xVToRh2CTmLk2HnEG7ac4rl1MJTT3RFkpS8B++/SnB0kXvuaavD+n3m/vrzyWQOdJNSZQACnbz01pnppbwV5BA==}
+ engines: {node: '>=20.19.0'}
+
+ unplugin@3.7.0:
+ resolution: {integrity: sha512-GrM2uq3DQo3hvLU2FjeYhooJ8BY9N6j1Fbzgx7Pta3VVJevsD3VP4dPjZkZOS0mLvN8w9zzr9OlP+31bSMyuLw==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ peerDependencies:
+ '@farmfe/core': '*'
+ '@rsbuild/core': '*'
+ '@rspack/core': '*'
+ bun-types-no-globals: '*'
+ esbuild: '*'
+ rolldown: '*'
+ rollup: '*'
+ unloader: '*'
+ vite: '*'
+ webpack: '*'
+ peerDependenciesMeta:
+ '@farmfe/core':
+ optional: true
+ '@rsbuild/core':
+ optional: true
+ '@rspack/core':
+ optional: true
+ bun-types-no-globals:
+ optional: true
+ esbuild:
+ optional: true
+ rolldown:
+ optional: true
+ rollup:
+ optional: true
+ unloader:
+ optional: true
+ vite:
+ optional: true
+ webpack:
+ optional: true
+
+ vite@8.3.5:
+ resolution: {integrity: sha512-z1YpjieJ9g0ebTmQpLBv/oq9E3F9EIlOe+7LahW8SrE3LTiG3udBRUXQOlYXP9b+IC2q2phHeVxj9xGlw34OEQ==}
+ engines: {node: ^20.19.0 || >=22.12.0}
+ hasBin: true
+ peerDependencies:
+ '@types/node': ^20.19.0 || >=22.12.0
+ '@vitejs/devtools': ^0.7.1 || ^0.8.0
+ esbuild: ^0.27.0 || ^0.28.0
+ jiti: '>=1.21.0'
+ less: ^4.0.0
+ sass: ^1.70.0
+ sass-embedded: ^1.70.0
+ stylus: '>=0.54.8'
+ sugarss: ^5.0.0
+ terser: ^5.16.0
+ tsx: ^4.8.1
+ yaml: ^2.4.2
+ peerDependenciesMeta:
+ '@types/node':
+ optional: true
+ '@vitejs/devtools':
+ optional: true
+ esbuild:
+ optional: true
+ jiti:
+ optional: true
+ less:
+ optional: true
+ sass:
+ optional: true
+ sass-embedded:
+ optional: true
+ stylus:
+ optional: true
+ sugarss:
+ optional: true
+ terser:
+ optional: true
+ tsx:
+ optional: true
+ yaml:
+ optional: true
+
+ vscode-jsonrpc@10.0.0:
+ resolution: {integrity: sha512-USSfxg0vDIpTQqlQBGxF3mBARyoS8cU7LHLpbYNh6dOJt32dlskxPWB0Irrh5TllTmyf7oNLAvqDqzklbwwnpA==}
+ engines: {node: '>=14.17.0'}
+
+ vscode-languageserver-protocol@3.18.5:
+ resolution: {integrity: sha512-FW4fBMzZ99N8xeob4py0l050wgCoRgTpEq5Xcsb9lM1bc29UKGR/wSV+4knaqFxssFHajy7OzNiKP/m3U1ZEuA==}
+
+ vscode-languageserver-textdocument@1.0.16:
+ resolution: {integrity: sha512-qeNEuOi9a3qkqRf7Gfez2WL2VwlBTLCNekHBGO9kxPVUp9XuY15yfTJ9jwvOwKI0I2MhUUfZq95NSp+LK1CLQg==}
+
+ vscode-languageserver-types@3.18.5:
+ resolution: {integrity: sha512-kPd6+JQNz3HMJvjKlqUxERSlZHAntl/aGpsk7ImwIh0ewDKDcjgAEyVSYciPiOJD3ftbK9IrfiqUeTZLSbCizw==}
+
+ vscode-uri@3.2.0:
+ resolution: {integrity: sha512-m2gXo3bn0G1kT9InzMf07fTbqMbGtyckj3bH5ktLO+1Ssv+yiATZ4dhwaQv9UZWxJh6E9IFGnQyjgWVDWVBDrg==}
+
+ vue-i18n@11.4.16:
+ resolution: {integrity: sha512-Kv9tp6iTmaAeOwop7hkAo28N1IBsbSVpnxyE26b6gqc4Q2E9Ke3ZHHA3oqqwS6/cYLT1henZU4etTaLQxrK98Q==}
+ engines: {node: '>= 22'}
+ peerDependencies:
+ vue: ^3.0.0
+
+ vue-router@5.4.0:
+ resolution: {integrity: sha512-pFvcYcxinDHjZUlf7AzFBrT8r8Xqvv4uMHoJnjc+EqPawXQvHTkF8S4tfSmQLpriVHUGfkaKlI7Et0KRjTMTdw==}
+ peerDependencies:
+ '@pinia/colada': '>=0.21.2'
+ '@vue/compiler-sfc': ^3.5.34 || ^4.0.0
+ pinia: ^3.0.4 || ^4.0.2
+ vite: ^7.3.0 || ^8.0.0
+ vue: ^3.5.34 || ^4.0.0
+ peerDependenciesMeta:
+ '@pinia/colada':
+ optional: true
+ '@vue/compiler-sfc':
+ optional: true
+ pinia:
+ optional: true
+ vite:
+ optional: true
+
+ vue-tsc@3.3.14:
+ resolution: {integrity: sha512-URAu2H9AuMZMg9GCgnqbaHHHd8pBZRAwNV2e2yYS8VDsoTVXHiOSo663OS6DkEvauQ2yixn5TPFJ9WV9QIspkA==}
+ hasBin: true
+ peerDependencies:
+ typescript: '>=5.0.0'
+
+ vue@3.5.44:
+ resolution: {integrity: sha512-dvvmaOCG7lAMkJTPqPwWeGMsPS6DIsFsfZDUgBp9V6mHf9b3+/wyVwYR4Es+WV6uJC0FKRxp81Obcq0ZwUcGFQ==}
+ peerDependencies:
+ typescript: '*'
+ peerDependenciesMeta:
+ typescript:
+ optional: true
+
+ webpack-virtual-modules@0.6.2:
+ resolution: {integrity: sha512-66/V2i5hQanC51vBQKPH4aI8NMAcBW59FVBs+rC7eGHupMyfn34q7rZIE+ETlJ+XTevqfUhVVBgSUNSW2flEUQ==}
+
+snapshots:
+
+ '@babel/helper-string-parser@7.29.11': {}
+
+ '@babel/helper-string-parser@8.0.8': {}
+
+ '@babel/helper-validator-identifier@7.29.7': {}
+
+ '@babel/helper-validator-identifier@8.0.6': {}
+
+ '@babel/parser@7.29.12':
+ dependencies:
+ '@babel/types': 7.29.11
+
+ '@babel/parser@8.0.10':
+ dependencies:
+ '@babel/types': 8.0.10
+
+ '@babel/types@7.29.11':
+ dependencies:
+ '@babel/helper-string-parser': 7.29.11
+ '@babel/helper-validator-identifier': 7.29.7
+
+ '@babel/types@8.0.10':
+ dependencies:
+ '@babel/helper-string-parser': 8.0.8
+ '@babel/helper-validator-identifier': 8.0.6
+
+ '@intlify/core-base@11.4.16':
+ dependencies:
+ '@intlify/devtools-types': 11.4.16
+ '@intlify/message-compiler': 11.4.16
+ '@intlify/shared': 11.4.16
+
+ '@intlify/devtools-types@11.4.16':
+ dependencies:
+ '@intlify/core-base': 11.4.16
+ '@intlify/shared': 11.4.16
+
+ '@intlify/message-compiler@11.4.16':
+ dependencies:
+ '@intlify/shared': 11.4.16
+ source-map-js: 1.2.2
+
+ '@intlify/shared@11.4.16': {}
+
+ '@jridgewell/gen-mapping@0.3.13':
+ dependencies:
+ '@jridgewell/sourcemap-codec': 1.6.0
+ '@jridgewell/trace-mapping': 0.3.31
+
+ '@jridgewell/remapping@2.3.5':
+ dependencies:
+ '@jridgewell/gen-mapping': 0.3.13
+ '@jridgewell/trace-mapping': 0.3.31
+
+ '@jridgewell/resolve-uri@3.1.2': {}
+
+ '@jridgewell/sourcemap-codec@1.6.0': {}
+
+ '@jridgewell/trace-mapping@0.3.31':
+ dependencies:
+ '@jridgewell/resolve-uri': 3.1.2
+ '@jridgewell/sourcemap-codec': 1.6.0
+
+ '@oxc-project/types@0.156.0': {}
+
+ '@playwright/test@1.64.1':
+ dependencies:
+ playwright: 1.64.1
+
+ '@rolldown/binding-android-arm-eabi@1.2.16':
+ optional: true
+
+ '@rolldown/binding-android-arm64@1.2.16':
+ optional: true
+
+ '@rolldown/binding-darwin-arm64@1.2.16':
+ optional: true
+
+ '@rolldown/binding-darwin-x64@1.2.16':
+ optional: true
+
+ '@rolldown/binding-freebsd-x64@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-arm-gnueabihf@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-arm64-gnu@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-arm64-musl@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-ppc64-gnu@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-s390x-gnu@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-x64-gnu@1.2.16':
+ optional: true
+
+ '@rolldown/binding-linux-x64-musl@1.2.16':
+ optional: true
+
+ '@rolldown/binding-openharmony-arm64@1.2.16':
+ optional: true
+
+ '@rolldown/binding-win32-arm64-msvc@1.2.16':
+ optional: true
+
+ '@rolldown/binding-win32-x64-msvc@1.2.16':
+ optional: true
+
+ '@rolldown/pluginutils@1.0.1': {}
+
+ '@types/estree@1.0.10': {}
+
+ '@vitejs/plugin-vue@6.0.10(vite@8.3.5)(vue@3.5.44(typescript@6.0.3))':
+ dependencies:
+ '@rolldown/pluginutils': 1.0.1
+ vite: 8.3.5
+ vue: 3.5.44(typescript@6.0.3)
+
+ '@volar/language-core@2.4.30':
+ dependencies:
+ '@volar/source-map': 2.4.30
+
+ '@volar/language-service@2.4.30(typescript@6.0.3)':
+ dependencies:
+ '@volar/language-core': 2.4.30
+ vscode-languageserver-protocol: 3.18.5
+ vscode-languageserver-textdocument: 1.0.16
+ vscode-uri: 3.2.0
+ optionalDependencies:
+ typescript: 6.0.3
+
+ '@volar/source-map@2.4.30': {}
+
+ '@volar/typescript@2.4.30(typescript@6.0.3)':
+ dependencies:
+ '@volar/language-core': 2.4.30
+ '@volar/language-service': 2.4.30(typescript@6.0.3)
+ path-browserify: 1.0.1
+ vscode-uri: 3.2.0
+ optionalDependencies:
+ typescript: 6.0.3
+
+ '@vue-macros/common@3.1.4(vue@3.5.44(typescript@6.0.3))':
+ dependencies:
+ '@vue/compiler-sfc': 3.5.44
+ ast-kit: 2.2.0
+ local-pkg: 1.2.1
+ magic-string-ast: 1.0.3
+ unplugin-utils: 0.3.2
+ optionalDependencies:
+ vue: 3.5.44(typescript@6.0.3)
+
+ '@vue/compiler-core@3.5.44':
+ dependencies:
+ '@babel/parser': 7.29.12
+ '@vue/shared': 3.5.44
+ entities: 7.0.1
+ estree-walker: 2.0.2
+ source-map-js: 1.2.2
+
+ '@vue/compiler-dom@3.5.44':
+ dependencies:
+ '@vue/compiler-core': 3.5.44
+ '@vue/shared': 3.5.44
+
+ '@vue/compiler-sfc@3.5.44':
+ dependencies:
+ '@babel/parser': 7.29.12
+ '@vue/compiler-core': 3.5.44
+ '@vue/compiler-dom': 3.5.44
+ '@vue/compiler-ssr': 3.5.44
+ '@vue/shared': 3.5.44
+ estree-walker: 2.0.2
+ magic-string: 0.30.21
+ postcss: 8.5.32
+ source-map-js: 1.2.2
+
+ '@vue/compiler-ssr@3.5.44':
+ dependencies:
+ '@vue/compiler-dom': 3.5.44
+ '@vue/shared': 3.5.44
+
+ '@vue/devtools-api@6.6.4': {}
+
+ '@vue/devtools-api@8.2.1':
+ dependencies:
+ '@vue/devtools-kit': 8.2.1
+
+ '@vue/devtools-kit@8.2.1':
+ dependencies:
+ '@vue/devtools-shared': 8.2.1
+ birpc: 2.9.0
+ hookable: 5.5.3
+ perfect-debounce: 2.1.0
+
+ '@vue/devtools-shared@8.2.1': {}
+
+ '@vue/language-core@3.3.14':
+ dependencies:
+ '@volar/language-core': 2.4.30
+ '@vue/compiler-dom': 3.5.44
+ '@vue/shared': 3.5.44
+ alien-signals: 3.2.1
+ muggle-string: 0.4.1
+ path-browserify: 1.0.1
+ picomatch: 4.0.7
+
+ '@vue/reactivity@3.5.44':
+ dependencies:
+ '@vue/shared': 3.5.44
+
+ '@vue/runtime-core@3.5.44':
+ dependencies:
+ '@vue/reactivity': 3.5.44
+ '@vue/shared': 3.5.44
+
+ '@vue/runtime-dom@3.5.44':
+ dependencies:
+ '@vue/reactivity': 3.5.44
+ '@vue/runtime-core': 3.5.44
+ '@vue/shared': 3.5.44
+ csstype: 3.2.3
+
+ '@vue/server-renderer@3.5.44':
+ dependencies:
+ '@vue/compiler-ssr': 3.5.44
+ '@vue/runtime-dom': 3.5.44
+ '@vue/shared': 3.5.44
+
+ '@vue/shared@3.5.44': {}
+
+ acorn@8.19.0: {}
+
+ alien-signals@3.2.1: {}
+
+ ast-kit@2.2.0:
+ dependencies:
+ '@babel/parser': 7.29.12
+ pathe: 2.0.3
+
+ ast-kit@3.0.0:
+ dependencies:
+ '@babel/parser': 8.0.10
+ estree-walker: 3.0.3
+ pathe: 2.0.3
+
+ ast-walker-scope@0.10.0:
+ dependencies:
+ '@babel/parser': 8.0.10
+ '@babel/types': 8.0.10
+ ast-kit: 3.0.0
+
+ birpc@2.9.0: {}
+
+ chokidar@5.0.0:
+ dependencies:
+ readdirp: 5.1.1
+
+ confbox@0.1.8: {}
+
+ confbox@0.3.2: {}
+
+ csstype@3.2.3: {}
+
+ detect-libc@2.1.2: {}
+
+ entities@7.0.1: {}
+
+ estree-walker@2.0.2: {}
+
+ estree-walker@3.0.3:
+ dependencies:
+ '@types/estree': 1.0.10
+
+ exsolve@1.2.0: {}
+
+ fdir@6.5.0(picomatch@4.0.7):
+ optionalDependencies:
+ picomatch: 4.0.7
+
+ fsevents@2.3.3:
+ optional: true
+
+ hookable@5.5.3: {}
+
+ lightningcss-android-arm64@1.33.0:
+ optional: true
+
+ lightningcss-darwin-arm64@1.33.0:
+ optional: true
+
+ lightningcss-darwin-x64@1.33.0:
+ optional: true
+
+ lightningcss-freebsd-x64@1.33.0:
+ optional: true
+
+ lightningcss-linux-arm-gnueabihf@1.33.0:
+ optional: true
+
+ lightningcss-linux-arm64-gnu@1.33.0:
+ optional: true
+
+ lightningcss-linux-arm64-musl@1.33.0:
+ optional: true
+
+ lightningcss-linux-x64-gnu@1.33.0:
+ optional: true
+
+ lightningcss-linux-x64-musl@1.33.0:
+ optional: true
+
+ lightningcss-win32-arm64-msvc@1.33.0:
+ optional: true
+
+ lightningcss-win32-x64-msvc@1.33.0:
+ optional: true
+
+ lightningcss@1.33.0:
+ dependencies:
+ detect-libc: 2.1.2
+ optionalDependencies:
+ lightningcss-android-arm64: 1.33.0
+ lightningcss-darwin-arm64: 1.33.0
+ lightningcss-darwin-x64: 1.33.0
+ lightningcss-freebsd-x64: 1.33.0
+ lightningcss-linux-arm-gnueabihf: 1.33.0
+ lightningcss-linux-arm64-gnu: 1.33.0
+ lightningcss-linux-arm64-musl: 1.33.0
+ lightningcss-linux-x64-gnu: 1.33.0
+ lightningcss-linux-x64-musl: 1.33.0
+ lightningcss-win32-arm64-msvc: 1.33.0
+ lightningcss-win32-x64-msvc: 1.33.0
+
+ local-pkg@1.2.1:
+ dependencies:
+ mlly: 1.8.2
+ pkg-types: 2.4.0
+ quansync: 0.2.11
+
+ magic-string-ast@1.0.3:
+ dependencies:
+ magic-string: 0.30.21
+
+ magic-string@0.30.21:
+ dependencies:
+ '@jridgewell/sourcemap-codec': 1.6.0
+
+ magic-string@1.4.5:
+ dependencies:
+ '@jridgewell/sourcemap-codec': 1.6.0
+
+ mlly@1.8.2:
+ dependencies:
+ acorn: 8.19.0
+ pathe: 2.0.3
+ pkg-types: 1.3.1
+ ufo: 1.6.4
+
+ muggle-string@0.4.1: {}
+
+ nanoid@3.3.20: {}
+
+ nostics@1.3.0: {}
+
+ path-browserify@1.0.1: {}
+
+ pathe@2.0.3: {}
+
+ perfect-debounce@2.1.0: {}
+
+ picocolors@1.1.1: {}
+
+ picomatch@4.0.7: {}
+
+ pinia@4.1.0(@vue/devtools-api@8.2.1)(typescript@6.0.3)(vue@3.5.44(typescript@6.0.3)):
+ dependencies:
+ '@vue/devtools-api': 8.2.1
+ nostics: 1.3.0
+ vue: 3.5.44(typescript@6.0.3)
+ optionalDependencies:
+ typescript: 6.0.3
+
+ pkg-types@1.3.1:
+ dependencies:
+ confbox: 0.1.8
+ mlly: 1.8.2
+ pathe: 2.0.3
+
+ pkg-types@2.4.0:
+ dependencies:
+ confbox: 0.3.2
+ exsolve: 1.2.0
+ pathe: 2.0.3
+
+ playwright-core@1.64.1: {}
+
+ playwright@1.64.1:
+ dependencies:
+ playwright-core: 1.64.1
+
+ postcss@8.5.32:
+ dependencies:
+ nanoid: 3.3.20
+ picocolors: 1.1.1
+ source-map-js: 1.2.2
+
+ quansync@0.2.11: {}
+
+ readdirp@5.1.1: {}
+
+ rolldown@1.2.16:
+ dependencies:
+ '@oxc-project/types': 0.156.0
+ '@rolldown/pluginutils': 1.0.1
+ optionalDependencies:
+ '@rolldown/binding-android-arm-eabi': 1.2.16
+ '@rolldown/binding-android-arm64': 1.2.16
+ '@rolldown/binding-darwin-arm64': 1.2.16
+ '@rolldown/binding-darwin-x64': 1.2.16
+ '@rolldown/binding-freebsd-x64': 1.2.16
+ '@rolldown/binding-linux-arm-gnueabihf': 1.2.16
+ '@rolldown/binding-linux-arm64-gnu': 1.2.16
+ '@rolldown/binding-linux-arm64-musl': 1.2.16
+ '@rolldown/binding-linux-ppc64-gnu': 1.2.16
+ '@rolldown/binding-linux-s390x-gnu': 1.2.16
+ '@rolldown/binding-linux-x64-gnu': 1.2.16
+ '@rolldown/binding-linux-x64-musl': 1.2.16
+ '@rolldown/binding-openharmony-arm64': 1.2.16
+ '@rolldown/binding-win32-arm64-msvc': 1.2.16
+ '@rolldown/binding-win32-x64-msvc': 1.2.16
+
+ scule@1.3.0: {}
+
+ source-map-js@1.2.2: {}
+
+ tinyglobby@0.2.17:
+ dependencies:
+ fdir: 6.5.0(picomatch@4.0.7)
+ picomatch: 4.0.7
+
+ typescript@6.0.3: {}
+
+ ufo@1.6.4: {}
+
+ unplugin-utils@0.3.2:
+ dependencies:
+ pathe: 2.0.3
+ picomatch: 4.0.7
+
+ unplugin@3.7.0(rolldown@1.2.16)(vite@8.3.5):
+ dependencies:
+ '@jridgewell/remapping': 2.3.5
+ '@rolldown/pluginutils': 1.0.1
+ picomatch: 4.0.7
+ webpack-virtual-modules: 0.6.2
+ optionalDependencies:
+ rolldown: 1.2.16
+ vite: 8.3.5
+
+ vite@8.3.5:
+ dependencies:
+ lightningcss: 1.33.0
+ picomatch: 4.0.7
+ postcss: 8.5.32
+ rolldown: 1.2.16
+ tinyglobby: 0.2.17
+ optionalDependencies:
+ fsevents: 2.3.3
+
+ vscode-jsonrpc@10.0.0: {}
+
+ vscode-languageserver-protocol@3.18.5:
+ dependencies:
+ vscode-jsonrpc: 10.0.0
+ vscode-languageserver-types: 3.18.5
+
+ vscode-languageserver-textdocument@1.0.16: {}
+
+ vscode-languageserver-types@3.18.5: {}
+
+ vscode-uri@3.2.0: {}
+
+ vue-i18n@11.4.16(vue@3.5.44(typescript@6.0.3)):
+ dependencies:
+ '@intlify/core-base': 11.4.16
+ '@intlify/devtools-types': 11.4.16
+ '@intlify/shared': 11.4.16
+ '@vue/devtools-api': 6.6.4
+ vue: 3.5.44(typescript@6.0.3)
+
+ vue-router@5.4.0(@vue/compiler-sfc@3.5.44)(pinia@4.1.0(@vue/devtools-api@8.2.1)(typescript@6.0.3)(vue@3.5.44(typescript@6.0.3)))(rolldown@1.2.16)(vite@8.3.5)(vue@3.5.44(typescript@6.0.3)):
+ dependencies:
+ '@vue-macros/common': 3.1.4(vue@3.5.44(typescript@6.0.3))
+ '@vue/devtools-api': 8.2.1
+ ast-walker-scope: 0.10.0
+ chokidar: 5.0.0
+ confbox: 0.3.2
+ local-pkg: 1.2.1
+ magic-string: 1.4.5
+ mlly: 1.8.2
+ muggle-string: 0.4.1
+ nostics: 1.3.0
+ pathe: 2.0.3
+ picomatch: 4.0.7
+ scule: 1.3.0
+ tinyglobby: 0.2.17
+ unplugin: 3.7.0(rolldown@1.2.16)(vite@8.3.5)
+ unplugin-utils: 0.3.2
+ vue: 3.5.44(typescript@6.0.3)
+ optionalDependencies:
+ '@vue/compiler-sfc': 3.5.44
+ pinia: 4.1.0(@vue/devtools-api@8.2.1)(typescript@6.0.3)(vue@3.5.44(typescript@6.0.3))
+ vite: 8.3.5
+ transitivePeerDependencies:
+ - '@farmfe/core'
+ - '@rsbuild/core'
+ - '@rspack/core'
+ - bun-types-no-globals
+ - esbuild
+ - rolldown
+ - rollup
+ - unloader
+ - webpack
+
+ vue-tsc@3.3.14(typescript@6.0.3):
+ dependencies:
+ '@volar/typescript': 2.4.30(typescript@6.0.3)
+ '@vue/language-core': 3.3.14
+ typescript: 6.0.3
+
+ vue@3.5.44(typescript@6.0.3):
+ dependencies:
+ '@vue/compiler-dom': 3.5.44
+ '@vue/compiler-sfc': 3.5.44
+ '@vue/runtime-dom': 3.5.44
+ '@vue/server-renderer': 3.5.44
+ '@vue/shared': 3.5.44
+ optionalDependencies:
+ typescript: 6.0.3
+
+ webpack-virtual-modules@0.6.2: {}
diff --git a/web/ui/pnpm-workspace.yaml b/web/ui/pnpm-workspace.yaml
new file mode 100644
index 00000000..5ed0b5af
--- /dev/null
+++ b/web/ui/pnpm-workspace.yaml
@@ -0,0 +1,2 @@
+allowBuilds:
+ esbuild: true
diff --git a/web/ui/public/favicon.svg b/web/ui/public/favicon.svg
new file mode 100644
index 00000000..c8de904b
--- /dev/null
+++ b/web/ui/public/favicon.svg
@@ -0,0 +1,24 @@
+
+ Save Any Bot
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/App.vue b/web/ui/src/App.vue
new file mode 100644
index 00000000..0643c2f6
--- /dev/null
+++ b/web/ui/src/App.vue
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/api.ts b/web/ui/src/api.ts
new file mode 100644
index 00000000..d41b53c3
--- /dev/null
+++ b/web/ui/src/api.ts
@@ -0,0 +1,409 @@
+import { translate } from './i18n'
+import type {
+ BotWorker,
+ BotWorkerInput,
+ BotWorkerTestResult,
+ CreateTaskRequest,
+ JsonObject,
+ CreateUserInput,
+ FilesystemListing,
+ MeResponse,
+ OnboardingStatus,
+ SettingsMap,
+ SettingsUpdateResult,
+ StorageInput,
+ StorageRecord,
+ StorageTestResult,
+ SystemInfo,
+ TaskCreated,
+ TaskItem,
+ TaskListResponse,
+ TaskMetadataResponse,
+ TaskTypeInfo,
+ TelegramStatus,
+ UpdateInfo,
+ UserDir,
+ UserItem,
+ UserPatch,
+ ParserList,
+ UserRule,
+ UserWatch,
+} from './types'
+
+export interface ApiErrorBody {
+ error?: string
+ message?: string
+ fields?: Record
+}
+
+/** Sentinel returned by the API for a secret that is already stored. */
+export const MASKED_SECRET = '__MASKED__'
+
+export class ApiError extends Error {
+ readonly status: number
+ readonly code: string
+ readonly fields: Record
+
+ constructor(status: number, code: string, message: string, fields: Record = {}) {
+ super(message || code)
+ this.name = 'ApiError'
+ this.status = status
+ this.code = code
+ this.fields = fields
+ }
+}
+
+type Method = 'GET' | 'POST' | 'PUT' | 'DELETE'
+
+interface RequestOptions {
+ method?: Method
+ body?: unknown
+ skipAuthRedirect?: boolean
+}
+
+let csrfProvider: () => string = () => ''
+let unauthorizedHandler: () => void = () => {}
+
+export function configureApi(config: {
+ getCsrf?: () => string
+ onUnauthorized?: () => void
+}): void {
+ if (config.getCsrf) {
+ csrfProvider = config.getCsrf
+ }
+ if (config.onUnauthorized) {
+ unauthorizedHandler = config.onUnauthorized
+ }
+}
+
+async function request(path: string, options: RequestOptions = {}): Promise {
+ const method = options.method ?? 'GET'
+ const headers: Record = { Accept: 'application/json' }
+ let body: string | undefined
+ if (options.body !== undefined) {
+ headers['Content-Type'] = 'application/json'
+ body = JSON.stringify(options.body)
+ }
+ if (method !== 'GET') {
+ const token = csrfProvider()
+ if (token) {
+ headers['X-CSRF-Token'] = token
+ }
+ }
+
+ let response: Response
+ try {
+ response = await fetch(`/api/v1${path}`, {
+ method,
+ headers,
+ body,
+ credentials: 'same-origin',
+ })
+ } catch (err) {
+ throw new ApiError(0, 'network_error', err instanceof Error ? err.message : translate('errors.network'))
+ }
+
+ if (response.status === 401 && !options.skipAuthRedirect) {
+ unauthorizedHandler()
+ }
+
+ if (!response.ok) {
+ let payload: ApiErrorBody = {}
+ try {
+ payload = (await response.json()) as ApiErrorBody
+ } catch {
+ payload = {}
+ }
+ throw new ApiError(
+ response.status,
+ payload.error ?? 'http_error',
+ payload.message ?? translate('errors.requestFailed', { status: response.status }),
+ payload.fields ?? {},
+ )
+ }
+
+ if (response.status === 204) {
+ return undefined as T
+ }
+ const text = await response.text()
+ if (!text) {
+ return undefined as T
+ }
+ return JSON.parse(text) as T
+}
+
+function query(params: Record): string {
+ const search = new URLSearchParams()
+ for (const [key, value] of Object.entries(params)) {
+ if (value !== undefined && value !== null && value !== '') {
+ search.set(key, String(value))
+ }
+ }
+ const encoded = search.toString()
+ return encoded ? `?${encoded}` : ''
+}
+
+export function errorMessage(err: unknown): string {
+ if (err instanceof ApiError) {
+ const fieldMessages = Object.entries(err.fields).map(([key, value]) => `${key}: ${value}`)
+ if (fieldMessages.length > 0) {
+ return `${err.message}(${fieldMessages.join(';')})`
+ }
+ return err.message
+ }
+ if (err instanceof Error) {
+ return err.message
+ }
+ return String(err)
+}
+
+export const authApi = {
+ login(password: string) {
+ return request<{ csrf_token: string }>('/auth/login', {
+ method: 'POST',
+ body: { password },
+ skipAuthRedirect: true,
+ })
+ },
+ logout() {
+ return request('/auth/logout', { method: 'POST', skipAuthRedirect: true })
+ },
+ me() {
+ return request('/auth/me', { skipAuthRedirect: true })
+ },
+ changePassword(currentPassword: string, newPassword: string) {
+ return request<{ ok: boolean }>('/auth/password', {
+ method: 'POST',
+ body: { current_password: currentPassword, new_password: newPassword },
+ })
+ },
+}
+
+export const filesystemApi = {
+ list(path?: string) {
+ return request(`/filesystem${query({ path })}`)
+ },
+}
+
+export const onboardingApi = {
+ status() {
+ return request('/onboarding')
+ },
+}
+
+export const settingsApi = {
+ getAll() {
+ return request('/settings')
+ },
+ updateGroup(group: string, patch: JsonObject) {
+ return request(`/settings/${group}`, { method: 'PUT', body: patch })
+ },
+}
+
+export const storagesApi = {
+ async list(): Promise {
+ const data = await request<{ storages: StorageRecord[] }>('/storages')
+ return data.storages ?? []
+ },
+ create(input: StorageInput) {
+ return request('/storages', { method: 'POST', body: input })
+ },
+ update(id: number, input: StorageInput) {
+ return request(`/storages/${id}`, { method: 'PUT', body: input })
+ },
+ remove(id: number) {
+ return request(`/storages/${id}`, { method: 'DELETE' })
+ },
+ test(id: number) {
+ return request(`/storages/${id}/test`, { method: 'POST', body: {} })
+ },
+}
+
+export const usersApi = {
+ async list(): Promise {
+ const data = await request<{ users: UserItem[] }>('/users')
+ return data.users ?? []
+ },
+ create(input: CreateUserInput) {
+ return request('/users', { method: 'POST', body: input })
+ },
+ update(id: number, patch: UserPatch) {
+ return request(`/users/${id}`, { method: 'PUT', body: patch })
+ },
+ remove(id: number) {
+ return request(`/users/${id}`, { method: 'DELETE' })
+ },
+}
+
+export const dirsApi = {
+ async list(userId: number): Promise {
+ const data = await request<{ dirs: UserDir[] }>(`/users/${userId}/dirs`)
+ return data.dirs ?? []
+ },
+ create(userId: number, input: { storage_id: number; path: string }) {
+ return request(`/users/${userId}/dirs`, { method: 'POST', body: input })
+ },
+ remove(userId: number, dirId: number) {
+ return request(`/users/${userId}/dirs/${dirId}`, { method: 'DELETE' })
+ },
+}
+
+export const rulesApi = {
+ async list(userId: number): Promise {
+ const data = await request<{ rules: UserRule[] }>(`/users/${userId}/rules`)
+ return data.rules ?? []
+ },
+ create(userId: number, input: { type: string; content: string; storage_id: number; dir_path: string }) {
+ return request(`/users/${userId}/rules`, { method: 'POST', body: input })
+ },
+ remove(userId: number, ruleId: number) {
+ return request(`/users/${userId}/rules/${ruleId}`, { method: 'DELETE' })
+ },
+ importPresets(userId: number, input: { storage_id: number; base_path: string }) {
+ return request<{ created: number }>(`/users/${userId}/rules/presets`, { method: 'POST', body: input })
+ },
+}
+
+export const watchesApi = {
+ async list(userId: number): Promise {
+ const data = await request<{ watches: UserWatch[] }>(`/users/${userId}/watches`)
+ return data.watches ?? []
+ },
+ create(userId: number, input: { chat?: string; chat_id?: number; filter: string }) {
+ return request(`/users/${userId}/watches`, { method: 'POST', body: input })
+ },
+ remove(userId: number, chatId: number) {
+ return request(`/users/${userId}/watches/${chatId}`, { method: 'DELETE' })
+ },
+}
+
+export const parsersApi = {
+ list() {
+ return request('/parsers')
+ },
+ create(input: { filename: string; content: string }) {
+ return request<{ name: string }>('/parsers', { method: 'POST', body: input })
+ },
+}
+
+export const telegramApi = {
+ status() {
+ return request('/telegram/status')
+ },
+ startBot() {
+ return request<{ state: string }>('/telegram/bot/start', { method: 'POST', body: {} })
+ },
+ stopBot(force = false) {
+ return request('/telegram/bot/stop', { method: 'POST', body: { force } })
+ },
+ restartBot(force = false) {
+ return request<{ state: string }>('/telegram/bot/restart', { method: 'POST', body: { force } })
+ },
+ login(phone: string) {
+ return request<{ flow_id: string }>('/telegram/userbot/login', {
+ method: 'POST',
+ body: { phone },
+ })
+ },
+ submitCode(flowId: string, code: string) {
+ return request<{ ok: boolean }>(`/telegram/userbot/login/${flowId}/code`, {
+ method: 'POST',
+ body: { code },
+ })
+ },
+ submitPassword(flowId: string, password: string) {
+ return request<{ ok: boolean }>(`/telegram/userbot/login/${flowId}/password`, {
+ method: 'POST',
+ body: { password },
+ })
+ },
+ cancelLogin(flowId: string) {
+ return request(`/telegram/userbot/login/${flowId}`, { method: 'DELETE' })
+ },
+ logout() {
+ return request('/telegram/userbot/logout', { method: 'POST', body: {} })
+ },
+ syncPeers() {
+ return request<{ synced: number }>('/telegram/userbot/sync-peers', { method: 'POST', body: {} })
+ },
+}
+
+export const workersApi = {
+ async list(): Promise {
+ const data = await request<{ workers: BotWorker[] }>('/telegram/workers')
+ return data.workers ?? []
+ },
+ create(input: BotWorkerInput) {
+ return request('/telegram/workers', { method: 'POST', body: input })
+ },
+ update(id: number, input: BotWorkerInput) {
+ return request(`/telegram/workers/${id}`, { method: 'PUT', body: input })
+ },
+ remove(id: number) {
+ return request(`/telegram/workers/${id}`, { method: 'DELETE' })
+ },
+ start(id: number) {
+ return request<{ state: string }>(`/telegram/workers/${id}/start`, { method: 'POST', body: {} })
+ },
+ stop(id: number) {
+ return request<{ state: string }>(`/telegram/workers/${id}/stop`, { method: 'POST', body: {} })
+ },
+ test(id: number) {
+ return request(`/telegram/workers/${id}/test`, { method: 'POST', body: {} })
+ },
+}
+
+export const tasksApi = {
+ list(params: { status?: string; type?: string; limit?: number; offset?: number }) {
+ return request(`/tasks${query(params)}`)
+ },
+ get(taskId: string) {
+ return request(`/tasks/${encodeURIComponent(taskId)}`)
+ },
+ metadata(taskId: string) {
+ return request(`/tasks/${encodeURIComponent(taskId)}/metadata`)
+ },
+ create(input: CreateTaskRequest) {
+ return request('/tasks', { method: 'POST', body: input })
+ },
+ retry(taskId: string) {
+ return request<{ task_id: string; status: string; file_count: number }>(
+ `/tasks/${encodeURIComponent(taskId)}/retry`,
+ { method: 'POST', body: {} },
+ )
+ },
+ cancel(taskId: string) {
+ return request<{ task_id: string; status: string }>(
+ `/tasks/${encodeURIComponent(taskId)}/cancel`,
+ { method: 'POST', body: {} },
+ )
+ },
+ remove(taskId: string) {
+ return request(`/tasks/${encodeURIComponent(taskId)}`, { method: 'DELETE' })
+ },
+ async types(): Promise {
+ const data = await request<{ types: TaskTypeInfo[] }>('/task-types')
+ return data.types ?? []
+ },
+}
+
+export const systemApi = {
+ info() {
+ return request('/system/info')
+ },
+ restart(force = false) {
+ return request<{ restarting: boolean }>('/system/restart', {
+ method: 'POST',
+ body: { force },
+ })
+ },
+ checkUpdate() {
+ return request('/system/update')
+ },
+ applyUpdate() {
+ return request<{ version: string; restarting: boolean }>('/system/update', {
+ method: 'POST',
+ body: {},
+ })
+ },
+}
diff --git a/web/ui/src/assets/main.css b/web/ui/src/assets/main.css
new file mode 100644
index 00000000..9db0576c
--- /dev/null
+++ b/web/ui/src/assets/main.css
@@ -0,0 +1,19 @@
+@import '../styles/tokens.css';
+@import '../styles/shell.css';
+@import '../styles/controls.css';
+@import '../styles/tables.css';
+@import '../styles/feedback.css';
+@import '../styles/dialogs.css';
+@import '../styles/views/dashboard.css';
+@import '../styles/views/storages.css';
+@import '../styles/views/path-picker.css';
+@import '../styles/views/settings.css';
+@import '../styles/views/telegram.css';
+@import '../styles/views/system.css';
+@import '../styles/views/login.css';
+@import '../styles/motion.css';
+@import '../styles/utilities.css';
+@import '../styles/responsive.css';
+@import '../styles/views/guide.css';
+@import '../styles/reduced-motion.css';
+@import '../styles/views/metadata.css';
diff --git a/web/ui/src/components/AppIcon.vue b/web/ui/src/components/AppIcon.vue
new file mode 100644
index 00000000..aba4d36b
--- /dev/null
+++ b/web/ui/src/components/AppIcon.vue
@@ -0,0 +1,113 @@
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/AppLayout.vue b/web/ui/src/components/AppLayout.vue
new file mode 100644
index 00000000..63694318
--- /dev/null
+++ b/web/ui/src/components/AppLayout.vue
@@ -0,0 +1,275 @@
+
+
+
+
+
diff --git a/web/ui/src/components/AppSelect.vue b/web/ui/src/components/AppSelect.vue
new file mode 100644
index 00000000..88c832b2
--- /dev/null
+++ b/web/ui/src/components/AppSelect.vue
@@ -0,0 +1,259 @@
+
+
+
+
+
+ {{ display }}
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/AppToaster.vue b/web/ui/src/components/AppToaster.vue
new file mode 100644
index 00000000..18c1ea97
--- /dev/null
+++ b/web/ui/src/components/AppToaster.vue
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
{{ item.message }}
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/BrandMark.vue b/web/ui/src/components/BrandMark.vue
new file mode 100644
index 00000000..49222fc9
--- /dev/null
+++ b/web/ui/src/components/BrandMark.vue
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/ConfirmDialog.vue b/web/ui/src/components/ConfirmDialog.vue
new file mode 100644
index 00000000..daf2eae3
--- /dev/null
+++ b/web/ui/src/components/ConfirmDialog.vue
@@ -0,0 +1,85 @@
+
+
+
+
+
+
+
+
+
+
+
{{ title }}
+
{{ message }}
+
+
+
+
+ {{ cancelText }}
+
+
+ {{ confirmText }}
+
+
+
+
+
diff --git a/web/ui/src/components/ConfirmHost.vue b/web/ui/src/components/ConfirmHost.vue
new file mode 100644
index 00000000..964734e5
--- /dev/null
+++ b/web/ui/src/components/ConfirmHost.vue
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/HelperBotsSection.vue b/web/ui/src/components/HelperBotsSection.vue
new file mode 100644
index 00000000..7dd88e25
--- /dev/null
+++ b/web/ui/src/components/HelperBotsSection.vue
@@ -0,0 +1,359 @@
+
+
+
+
+
+
{{ t('workers.title') }}
+
+
{{ t('workers.count', { count: workers.length }) }}
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('workers.create') }}
+
+
+
+ {{ t('workers.note') }}
+
+
+
+
+
+
+
+
+ {{ t('workers.name') }}
+ {{ t('workers.state') }}
+ {{ t('workers.username') }}
+ {{ t('workers.enabled') }}
+ {{ t('workers.actions') }}
+
+
+
+
+
+ {{ worker.name }}
+ {{ worker.error }}
+
+
+ {{ stateLabel(worker.state) }}
+
+ {{ worker.username ? `@${worker.username}` : '—' }}
+
+
+ {{ worker.enabled ? t('states.enabled') : t('states.disabled') }}
+
+
+
+
+
+ {{ t('workers.stop') }}
+
+
+ {{ t('workers.start') }}
+
+
+ {{ t('common.actions.edit') }}
+
+
+ {{ t('workers.test') }}
+
+
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+ {{ t('workers.empty') }}
+
+
+
+
+
+
+
+
+
+
+ {{ worker.name }}
+ {{ worker.username ? `@${worker.username}` : '—' }}
+
+
{{ stateLabel(worker.state) }}
+
+
+
+ {{ worker.enabled ? t('states.enabled') : t('states.disabled') }}
+
+
+ {{ worker.error }}
+
+
+ {{ t('workers.stop') }}
+
+
+ {{ t('workers.start') }}
+
+
+ {{ t('common.actions.edit') }}
+
+
+ {{ t('workers.test') }}
+
+
+ {{ t('common.actions.delete') }}
+
+
+
+
+ {{ t('workers.empty') }}
+
+
+
+
+
+
+
+
+
+
+ {{ editing ? t('workers.edit') : t('workers.create') }}
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/LoadingButton.vue b/web/ui/src/components/LoadingButton.vue
new file mode 100644
index 00000000..da899a6a
--- /dev/null
+++ b/web/ui/src/components/LoadingButton.vue
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/LocaleSwitch.vue b/web/ui/src/components/LocaleSwitch.vue
new file mode 100644
index 00000000..a408db70
--- /dev/null
+++ b/web/ui/src/components/LocaleSwitch.vue
@@ -0,0 +1,30 @@
+
+
+
+
+
+ {{ option.short }}
+
+
+
diff --git a/web/ui/src/components/PageHeader.vue b/web/ui/src/components/PageHeader.vue
new file mode 100644
index 00000000..28119cfb
--- /dev/null
+++ b/web/ui/src/components/PageHeader.vue
@@ -0,0 +1,16 @@
+
+
+
+
+
+
{{ title }}
+
+
+
+
+
+
diff --git a/web/ui/src/components/PageLoading.vue b/web/ui/src/components/PageLoading.vue
new file mode 100644
index 00000000..34bd4eee
--- /dev/null
+++ b/web/ui/src/components/PageLoading.vue
@@ -0,0 +1,45 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/ServerPathPicker.vue b/web/ui/src/components/ServerPathPicker.vue
new file mode 100644
index 00000000..3fd70b3d
--- /dev/null
+++ b/web/ui/src/components/ServerPathPicker.vue
@@ -0,0 +1,251 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ t('common.pathPicker.root') }}
+
+
+
+ {{ t('common.pathPicker.cwd') }}
+
+
+
+
+ {{ t('common.pathPicker.showHidden') }}
+
+
+
+
+
{{ t('common.pathPicker.loading') }}
+
+
{{ error }}
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+
+
+ {{ entry.name }}
+ {{ formatBytes(entry.size) }}
+
+
+
+ {{ mode === 'directory' ? t('common.pathPicker.emptyFolders') : t('common.pathPicker.empty') }}
+
+
+
+
+
+ {{ t('common.pathPicker.truncated') }}
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/StorageForm.vue b/web/ui/src/components/StorageForm.vue
new file mode 100644
index 00000000..4f9584cc
--- /dev/null
+++ b/web/ui/src/components/StorageForm.vue
@@ -0,0 +1,300 @@
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/TaskForm.vue b/web/ui/src/components/TaskForm.vue
new file mode 100644
index 00000000..cec2f479
--- /dev/null
+++ b/web/ui/src/components/TaskForm.vue
@@ -0,0 +1,356 @@
+
+
+
+
+
+
+
+
+
+
+ {{ t(field.label) }}
+
+
+
+ {{ t(field.label) }} *
+
+
+
+
+
{{ t(field.hint) }}
+
+
+
+
+
+ {{ t('common.actions.cancel') }}
+ {{ t('common.actions.createTask') }}
+
+
+
+
+
diff --git a/web/ui/src/components/TaskMetadataModal.vue b/web/ui/src/components/TaskMetadataModal.vue
new file mode 100644
index 00000000..7ddd74de
--- /dev/null
+++ b/web/ui/src/components/TaskMetadataModal.vue
@@ -0,0 +1,340 @@
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/components/TaskTable.vue b/web/ui/src/components/TaskTable.vue
new file mode 100644
index 00000000..42d04d93
--- /dev/null
+++ b/web/ui/src/components/TaskTable.vue
@@ -0,0 +1,250 @@
+
+
+
+
+
+
+
+
+ {{ t('tasks.table.task') }}
+ {{ t('tasks.table.status') }}
+ {{ t('tasks.table.storagePath') }}
+ {{ t('tasks.table.progress') }}
+ {{ t('tasks.table.createdAt') }}
+ {{ t('tasks.table.actions') }}
+
+
+
+
+
+ {{ taskDisplayTitle(task) }}
+
+ {{ taskTypeLabel(task.type) }}
+ {{ task.task_id }}
+
+ {{ task.error }}
+
+
{{ t('tasks.failedFiles') }} ({{ task.failed_count ?? task.failed_items.length }})
+
+
+ {{ item.name }}
+ {{ item.error }}
+
+
+
+
+
+
+ {{ taskStatusLabel(task.status) }}
+
+
+
+ {{ task.storage_name || '—' }}
+ {{ task.path || t('tasks.table.rootPath') }}
+
+
+
+ {{ progressText(task) }}
+
+ {{ formatDateTime(task.created_at) }}
+
+
+
+
+ {{ t('common.actions.cancel') }}
+
+
+
+ {{ t('common.actions.retry') }}
+
+
+
+ {{ t('tasks.metadata') }}
+
+
+
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+
+ {{ emptyText || t('tasks.empty') }}
+
+
+
+
+
+
+
+
+
+
{{ taskDisplayTitle(task) }}
+
+ {{ taskStatusLabel(task.status) }}
+
+
+
+ {{ taskTypeLabel(task.type) }}
+ {{ task.task_id }}
+
+
+ {{ task.storage_name || t('tasks.table.noStorage') }}
+ {{ task.path || t('tasks.table.rootPath') }}
+
+ {{ task.error }}
+
+
{{ t('tasks.failedFiles') }} ({{ task.failed_count ?? task.failed_items.length }})
+
+
+ {{ item.name }}
+ {{ item.error }}
+
+
+
+
+
+
{{ progressText(task) }}
+
+
+
+
+
+
{{ emptyText || t('tasks.empty') }}
+
+
+
+
diff --git a/web/ui/src/components/ThemeSwitch.vue b/web/ui/src/components/ThemeSwitch.vue
new file mode 100644
index 00000000..2ab475dd
--- /dev/null
+++ b/web/ui/src/components/ThemeSwitch.vue
@@ -0,0 +1,33 @@
+
+
+
+
+
diff --git a/web/ui/src/composables/useBodyScrollLock.ts b/web/ui/src/composables/useBodyScrollLock.ts
new file mode 100644
index 00000000..0ca6b987
--- /dev/null
+++ b/web/ui/src/composables/useBodyScrollLock.ts
@@ -0,0 +1,30 @@
+let lockCount = 0
+let previousOverflow = ''
+let previousPaddingRight = ''
+
+// lockBodyScroll disables page scrolling while one or more modals are open.
+// The returned function is idempotent, so callers can safely release it from
+// either a watcher or component teardown.
+export function lockBodyScroll(): () => void {
+ const body = document.body
+ if (lockCount === 0) {
+ previousOverflow = body.style.overflow
+ previousPaddingRight = body.style.paddingRight
+ const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth
+ body.style.overflow = 'hidden'
+ if (scrollbarWidth > 0) {
+ body.style.paddingRight = `${scrollbarWidth}px`
+ }
+ }
+ lockCount += 1
+
+ let released = false
+ return () => {
+ if (released) return
+ released = true
+ lockCount = Math.max(0, lockCount - 1)
+ if (lockCount > 0) return
+ body.style.overflow = previousOverflow
+ body.style.paddingRight = previousPaddingRight
+ }
+}
diff --git a/web/ui/src/composables/useFormFeedback.ts b/web/ui/src/composables/useFormFeedback.ts
new file mode 100644
index 00000000..d896577a
--- /dev/null
+++ b/web/ui/src/composables/useFormFeedback.ts
@@ -0,0 +1,30 @@
+import { ref } from 'vue'
+import { useToastStore } from '../stores/toasts'
+
+export function useFormFeedback() {
+ const toast = useToastStore()
+ const invalidField = ref('')
+
+ function fail(message: string, fieldId = '') {
+ invalidField.value = fieldId
+ toast.push(message, 'error', 5000)
+ if (!fieldId) return
+ window.requestAnimationFrame(() => {
+ const element = document.getElementById(fieldId)
+ element?.scrollIntoView({ block: 'nearest' })
+ element?.focus()
+ })
+ }
+
+ function clear(fieldId = '') {
+ if (!fieldId || invalidField.value === fieldId) {
+ invalidField.value = ''
+ }
+ }
+
+ function success(message: string) {
+ toast.push(message, 'success')
+ }
+
+ return { invalidField, fail, clear, success }
+}
diff --git a/web/ui/src/composables/useModalHeightTransition.ts b/web/ui/src/composables/useModalHeightTransition.ts
new file mode 100644
index 00000000..f057addd
--- /dev/null
+++ b/web/ui/src/composables/useModalHeightTransition.ts
@@ -0,0 +1,71 @@
+import { onUnmounted } from 'vue'
+import type { Ref } from 'vue'
+
+const RESIZE_DURATION = 280
+
+export function useModalHeightTransition(modal: Ref) {
+ let timer: number | undefined
+ let from = 0
+ let version = 0
+
+ function reset() {
+ from = 0
+ timer = undefined
+ }
+
+ function startResize() {
+ const el = modal.value
+ if (!el) return
+ if (timer) {
+ window.clearTimeout(timer)
+ timer = undefined
+ }
+ // Freeze the current height before Vue swaps the field set.
+ version += 1
+ from = el.getBoundingClientRect().height
+ el.style.transition = 'none'
+ el.style.height = `${from}px`
+ }
+
+ function endResize() {
+ const el = modal.value
+ if (!el || !from) return
+
+ const current = from
+ const currentVersion = version
+ el.style.height = 'auto'
+ const to = el.getBoundingClientRect().height
+ el.style.height = `${current}px`
+ void el.offsetHeight
+
+ const finish = () => {
+ reset()
+ if (modal.value === el) {
+ el.style.transition = ''
+ el.style.height = ''
+ }
+ }
+
+ if (
+ Math.abs(to - current) < 1 ||
+ window.matchMedia('(prefers-reduced-motion: reduce)').matches
+ ) {
+ finish()
+ return
+ }
+
+ el.style.transition = 'height var(--duration-fast) var(--ease-smooth-out)'
+ requestAnimationFrame(() => {
+ if (currentVersion === version && modal.value === el) {
+ el.style.height = `${to}px`
+ }
+ })
+ timer = window.setTimeout(finish, RESIZE_DURATION)
+ }
+
+ onUnmounted(() => {
+ if (timer) window.clearTimeout(timer)
+ })
+
+ return { startResize, endResize }
+}
diff --git a/web/ui/src/env.d.ts b/web/ui/src/env.d.ts
new file mode 100644
index 00000000..11f02fe2
--- /dev/null
+++ b/web/ui/src/env.d.ts
@@ -0,0 +1 @@
+///
diff --git a/web/ui/src/i18n/index.ts b/web/ui/src/i18n/index.ts
new file mode 100644
index 00000000..2b73fa61
--- /dev/null
+++ b/web/ui/src/i18n/index.ts
@@ -0,0 +1,60 @@
+import { createI18n } from 'vue-i18n'
+import type { MessageModule } from './messages/types'
+
+export type Locale = 'zh-Hans' | 'en'
+
+export const LOCALE_STORAGE_KEY = 'saveany-locale'
+
+const modules = import.meta.glob('./messages/*.ts', { eager: true })
+
+const messages: Record> = {
+ en: {},
+ 'zh-Hans': {},
+}
+
+for (const module of Object.values(modules)) {
+ if (!module.en || !module.zhHans) continue
+ Object.assign(messages.en, module.en)
+ Object.assign(messages['zh-Hans'], module.zhHans)
+}
+
+function detectLocale(): Locale {
+ try {
+ const stored = localStorage.getItem(LOCALE_STORAGE_KEY)
+ if (stored === 'zh-Hans' || stored === 'en') return stored
+ } catch {
+ // Fall back to the browser language when storage is unavailable.
+ }
+ return navigator.languages.some((language) => language.toLowerCase().startsWith('zh'))
+ ? 'zh-Hans'
+ : 'en'
+}
+
+export const i18n = createI18n({
+ legacy: false,
+ locale: detectLocale(),
+ fallbackLocale: 'zh-Hans',
+ messages: messages as never,
+ missingWarn: false,
+ fallbackWarn: false,
+})
+
+export function setLocale(next: Locale): void {
+ i18n.global.locale.value = next
+ document.documentElement.lang = next
+ try {
+ localStorage.setItem(LOCALE_STORAGE_KEY, next)
+ } catch {
+ // The in-memory locale still applies for this session.
+ }
+}
+
+export function initI18n(): void {
+ document.documentElement.lang = i18n.global.locale.value
+}
+
+export function translate(key: string, params?: Record): string {
+ return i18n.global.t(key, params ?? {}) as string
+}
+
+export { useI18n } from 'vue-i18n'
diff --git a/web/ui/src/i18n/messages/automation.ts b/web/ui/src/i18n/messages/automation.ts
new file mode 100644
index 00000000..df6af39c
--- /dev/null
+++ b/web/ui/src/i18n/messages/automation.ts
@@ -0,0 +1,153 @@
+export const zhHans = {
+ automation: {
+ noUsers: '请先在用户页面添加用户',
+ userTitle: '管理用户',
+ selectUser: '选择用户',
+ selectStorage: '选择存储',
+ actions: '操作',
+ directories: {
+ title: '目录',
+ count: '{count} 个目录',
+ storage: '存储',
+ path: '目录路径',
+ pathPlaceholder: '例如 downloads/anime',
+ add: '添加目录',
+ empty: '暂无目录',
+ created: '目录已添加',
+ required: '请选择存储并填写目录路径',
+ makeDefault: '设为默认目录',
+ defaultSet: '默认目录已更新',
+ deleteTitle: '删除目录',
+ deleteMessage: '删除目录 {path}?',
+ },
+ rules: {
+ title: '规则',
+ enabled: '规则已启用',
+ disabled: '规则未启用',
+ enable: '启用规则',
+ disable: '停用规则',
+ type: '规则类型',
+ content: '匹配内容',
+ contentPlaceholder: '正则表达式或 true/false',
+ contentRequired: '请填写匹配内容',
+ storage: '目标存储',
+ chosenStorage: '保存时选择的存储',
+ dirPath: '目标目录',
+ dirPathPlaceholder: '例如 videos',
+ add: '添加规则',
+ created: '规则已添加',
+ empty: '暂无规则',
+ deleteTitle: '删除规则',
+ deleteMessage: '删除 {type} 规则?',
+ presetsTitle: '预设规则',
+ presetStorage: '预设规则存储',
+ presetBasePath: '基础路径',
+ presetBasePathPlaceholder: '例如 downloads',
+ importPresets: '导入预设规则',
+ presetsImported: '已导入 {count} 条预设规则',
+ types: {
+ filename: '文件名正则',
+ message: '消息正则',
+ album: '相册',
+ },
+ },
+ watches: {
+ title: '监听',
+ notify: '发送监听通知',
+ syncPeers: '同步对话',
+ syncing: '同步中…',
+ syncConfirmTitle: '同步对话',
+ syncConfirmMessage: '将遍历账号中的全部对话并写入本地会话缓存。耗时较长,请保持页面开启。',
+ syncConfirmText: '开始同步',
+ syncStarted: '正在同步对话,请稍候…',
+ synced: '已同步 {count} 个对话',
+ chatId: '聊天 ID / 用户名',
+ chatPlaceholder: '-1001234567890 或 {\'@\'}channel',
+ filter: '过滤器',
+ filterPlaceholder: 'msgre:.*',
+ add: '添加监听',
+ created: '监听已添加',
+ empty: '暂无监听',
+ chatRequired: '请填写聊天 ID 或用户名',
+ deleteTitle: '取消监听',
+ deleteMessage: '取消监听聊天 {chat}?',
+ },
+ },
+}
+
+export const en = {
+ automation: {
+ noUsers: 'Add a user first on the Users page',
+ userTitle: 'Managed user',
+ selectUser: 'Select user',
+ selectStorage: 'Select storage',
+ actions: 'Actions',
+ directories: {
+ title: 'Directories',
+ count: '{count} directories',
+ storage: 'Storage',
+ path: 'Directory path',
+ pathPlaceholder: 'e.g. downloads/anime',
+ add: 'Add directory',
+ empty: 'No directories',
+ created: 'Directory added',
+ required: 'Select a storage and enter a directory path',
+ makeDefault: 'Set as default',
+ defaultSet: 'Default directory updated',
+ deleteTitle: 'Delete directory',
+ deleteMessage: 'Delete directory {path}?',
+ },
+ rules: {
+ title: 'Rules',
+ enabled: 'Rules enabled',
+ disabled: 'Rules disabled',
+ enable: 'Enable rules',
+ disable: 'Disable rules',
+ type: 'Rule type',
+ content: 'Match content',
+ contentPlaceholder: 'Regular expression or true/false',
+ contentRequired: 'Enter the match content',
+ storage: 'Target storage',
+ chosenStorage: 'Storage selected when saving',
+ dirPath: 'Target directory',
+ dirPathPlaceholder: 'e.g. videos',
+ add: 'Add rule',
+ created: 'Rule added',
+ empty: 'No rules',
+ deleteTitle: 'Delete rule',
+ deleteMessage: 'Delete the {type} rule?',
+ presetsTitle: 'Preset rules',
+ presetStorage: 'Preset target storage',
+ presetBasePath: 'Base path',
+ presetBasePathPlaceholder: 'e.g. downloads',
+ importPresets: 'Import presets',
+ presetsImported: 'Imported {count} preset rules',
+ types: {
+ filename: 'Filename regex',
+ message: 'Message regex',
+ album: 'Album',
+ },
+ },
+ watches: {
+ title: 'Watch chats',
+ notify: 'Send watch notifications',
+ syncPeers: 'Sync peers',
+ syncing: 'Syncing…',
+ syncConfirmTitle: 'Sync peers',
+ syncConfirmMessage: 'This walks through all dialogs on the account and writes them to the local peer cache. It can take a while, so keep this page open.',
+ syncConfirmText: 'Start syncing',
+ syncStarted: 'Syncing dialogs. This may take a while…',
+ synced: 'Synced {count} chats',
+ chatId: 'Chat ID / username',
+ chatPlaceholder: '-1001234567890 or {\'@\'}channel',
+ filter: 'Filter',
+ filterPlaceholder: 'msgre:.*',
+ add: 'Add watch',
+ created: 'Watch added',
+ empty: 'No watched chats',
+ chatRequired: 'Enter a chat ID or username',
+ deleteTitle: 'Stop watching',
+ deleteMessage: 'Stop watching chat {chat}?',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/common.ts b/web/ui/src/i18n/messages/common.ts
new file mode 100644
index 00000000..d91ab87a
--- /dev/null
+++ b/web/ui/src/i18n/messages/common.ts
@@ -0,0 +1,297 @@
+export const zhHans = {
+ nav: {
+ workspace: '工作台',
+ overview: '运行概览',
+ guide: '指南',
+ login: '登录',
+ tasks: '任务',
+ resources: '资源',
+ storages: '存储',
+ users: '用户',
+ automation: '自动化',
+ parsers: '解析器',
+ connections: '连接',
+ telegram: 'Telegram',
+ settings: '设置',
+ system: '系统',
+ administration: '管理',
+ },
+ states: {
+ enabled: '已启用',
+ disabled: '已禁用',
+ queued: '排队中',
+ running: '进行中',
+ completed: '已完成',
+ partial: '部分完成',
+ failed: '失败',
+ cancelled: '已取消',
+ stopped: '已停止',
+ starting: '启动中',
+ error: '错误',
+ loginRequired: '需要登录',
+ unknown: '未知',
+ },
+ taskTypes: {
+ directlinks: '直链下载',
+ ytdlp: 'yt-dlp 视频',
+ aria2: 'Aria2 下载',
+ parseditem: '解析链接',
+ tgfiles: 'Telegram 文件',
+ tphpics: 'Telegraph 图集',
+ transfer: '存储迁移',
+ },
+ common: {
+ adminConnected: '管理界面已连接',
+ skipToContent: '跳到主内容',
+ closeNavigation: '关闭导航',
+ openNavigation: '打开导航',
+ mainNavigation: '主导航',
+ language: '语言',
+ appearance: '外观',
+ theme: {
+ label: '主题',
+ system: '跟随系统',
+ light: '浅色',
+ dark: '深色',
+ },
+ select: '请选择',
+ loading: '加载中…',
+ dismiss: '关闭通知',
+ confirmTitle: '确认操作',
+ confirm: '确认',
+ cancel: '取消',
+ close: '关闭',
+ pathPicker: {
+ titleDirectory: '选择服务器文件夹',
+ titleFile: '选择服务器文件',
+ browse: '浏览',
+ parent: '上级',
+ open: '打开',
+ root: '根目录',
+ cwd: '工作目录',
+ loading: '正在读取目录…',
+ empty: '此目录为空',
+ emptyFolders: '没有子文件夹',
+ outsideRoot: '该路径不在存储目录内',
+ showHidden: '显示隐藏文件',
+ truncated: '仅显示前 500 项',
+ noSelection: '未选择文件',
+ selectDirectory: '选择此文件夹',
+ selectFile: '选择文件',
+ cancel: '取消',
+ },
+ actions: {
+ refresh: '刷新',
+ reload: '重新加载',
+ save: '保存',
+ changePassword: '修改密码',
+ saveApply: '保存并应用',
+ saveSettings: '保存设置',
+ saveChanges: '保存修改',
+ createTask: '创建任务',
+ createStorage: '创建存储',
+ cancel: '取消',
+ retry: '重试',
+ delete: '删除',
+ edit: '编辑',
+ test: '测试',
+ signIn: '登录',
+ sendCode: '发送验证码',
+ submit: '提交',
+ cancelLogin: '取消登录',
+ forceStop: '强制停止',
+ forceRestart: '强制重启',
+ logout: '退出登录',
+ },
+ },
+ login: {
+ title: '管理员登录',
+ password: '管理员密码',
+ submit: '登录控制台',
+ helpBefore: '密码见启动日志,或通过 ',
+ helpAfter: ' 设置。',
+ invalidPassword: '密码错误',
+ tooManyAttempts: '尝试次数过多,请稍后再试',
+ changePassword: '修改管理员密码',
+ currentPassword: '当前密码',
+ newPassword: '新密码',
+ confirmPassword: '确认新密码',
+ passwordHint: '至少 8 个字符',
+ currentPasswordRequired: '请输入当前密码',
+ passwordTooShort: '新密码至少需要 8 个字符',
+ passwordMismatch: '两次输入的新密码不一致',
+ passwordUpdated: '密码已更新',
+ saveNewPassword: '保存新密码',
+ },
+ time: {
+ justNow: '刚刚',
+ minutesAgo: '{n} 分钟前',
+ hoursAgo: '{n} 小时前',
+ yesterday: '昨天',
+ daysAgo: '{n} 天前',
+ days: '{n} 天',
+ hours: '{n} 小时',
+ minutes: '{n} 分',
+ seconds: '{n} 秒',
+ unnamedTask: '未命名任务',
+ files: '{n} 个文件,{size} MB',
+ locationUnset: '未填写位置',
+ },
+ validation: {
+ required: '请填写{field}',
+ invalid: '请填写有效的{field}',
+ },
+ errors: {
+ network: '网络请求失败',
+ requestFailed: '请求失败 (HTTP {status})',
+ },
+}
+
+export const en = {
+ nav: {
+ workspace: 'Workspace',
+ overview: 'Overview',
+ guide: 'Guide',
+ login: 'Sign in',
+ tasks: 'Tasks',
+ resources: 'Resources',
+ storages: 'Storages',
+ users: 'Users',
+ automation: 'Automation',
+ parsers: 'Parsers',
+ connections: 'Connections',
+ telegram: 'Telegram',
+ settings: 'Settings',
+ system: 'System',
+ administration: 'Administration',
+ },
+ states: {
+ enabled: 'Enabled',
+ disabled: 'Disabled',
+ queued: 'Queued',
+ running: 'Running',
+ completed: 'Completed',
+ partial: 'Partially completed',
+ failed: 'Failed',
+ cancelled: 'Cancelled',
+ stopped: 'Stopped',
+ starting: 'Starting',
+ error: 'Error',
+ loginRequired: 'Login required',
+ unknown: 'Unknown',
+ },
+ taskTypes: {
+ directlinks: 'Direct link',
+ ytdlp: 'yt-dlp video',
+ aria2: 'Aria2 download',
+ parseditem: 'Parsed link',
+ tgfiles: 'Telegram files',
+ tphpics: 'Telegraph gallery',
+ transfer: 'Storage transfer',
+ },
+ common: {
+ adminConnected: 'Admin connected',
+ skipToContent: 'Skip to content',
+ closeNavigation: 'Close navigation',
+ openNavigation: 'Open navigation',
+ mainNavigation: 'Main navigation',
+ language: 'Language',
+ appearance: 'Appearance',
+ theme: {
+ label: 'Theme',
+ system: 'System',
+ light: 'Light',
+ dark: 'Dark',
+ },
+ select: 'Select',
+ loading: 'Loading…',
+ dismiss: 'Dismiss notification',
+ confirmTitle: 'Confirm action',
+ confirm: 'Confirm',
+ cancel: 'Cancel',
+ close: 'Close',
+ pathPicker: {
+ titleDirectory: 'Select a server folder',
+ titleFile: 'Select a server file',
+ browse: 'Browse',
+ parent: 'Up',
+ open: 'Open',
+ root: 'Root',
+ cwd: 'Working directory',
+ loading: 'Reading directory…',
+ empty: 'This folder is empty',
+ emptyFolders: 'No subfolders',
+ outsideRoot: 'This path is outside the storage folder.',
+ showHidden: 'Show hidden files',
+ truncated: 'Showing the first 500 entries',
+ noSelection: 'No file selected',
+ selectDirectory: 'Select this folder',
+ selectFile: 'Select file',
+ cancel: 'Cancel',
+ },
+ actions: {
+ refresh: 'Refresh',
+ reload: 'Reload',
+ save: 'Save',
+ changePassword: 'Password',
+ saveApply: 'Save & apply',
+ saveSettings: 'Save settings',
+ saveChanges: 'Save changes',
+ createTask: 'Create task',
+ createStorage: 'Create storage',
+ cancel: 'Cancel',
+ retry: 'Retry',
+ delete: 'Delete',
+ edit: 'Edit',
+ test: 'Test',
+ signIn: 'Sign in',
+ sendCode: 'Send code',
+ submit: 'Submit',
+ cancelLogin: 'Cancel login',
+ forceStop: 'Force stop',
+ forceRestart: 'Force restart',
+ logout: 'Sign out',
+ },
+ },
+ login: {
+ title: 'Admin sign in',
+ password: 'Admin password',
+ submit: 'Sign in',
+ helpBefore: 'Find the password in the startup log, or set ',
+ helpAfter: '.',
+ invalidPassword: 'Incorrect password',
+ tooManyAttempts: 'Too many attempts. Try again later.',
+ changePassword: 'Change admin password',
+ currentPassword: 'Current password',
+ newPassword: 'New password',
+ confirmPassword: 'Confirm new password',
+ passwordHint: 'At least 8 characters',
+ currentPasswordRequired: 'Enter your current password',
+ passwordTooShort: 'The new password must be at least 8 characters',
+ passwordMismatch: 'The new passwords do not match',
+ passwordUpdated: 'Password updated',
+ saveNewPassword: 'Save new password',
+ },
+ time: {
+ justNow: 'just now',
+ minutesAgo: '{n} min ago',
+ hoursAgo: '{n} hr ago',
+ yesterday: 'yesterday',
+ daysAgo: '{n}d ago',
+ days: '{n}d',
+ hours: '{n}h',
+ minutes: '{n}m',
+ seconds: '{n}s',
+ unnamedTask: 'Untitled task',
+ files: '{n} files, {size} MB',
+ locationUnset: 'Location not set',
+ },
+ validation: {
+ required: 'Enter {field}',
+ invalid: 'Enter a valid {field}',
+ },
+ errors: {
+ network: 'Network request failed',
+ requestFailed: 'Request failed (HTTP {status})',
+ },
+}
diff --git a/web/ui/src/i18n/messages/dashboard.ts b/web/ui/src/i18n/messages/dashboard.ts
new file mode 100644
index 00000000..32fb2838
--- /dev/null
+++ b/web/ui/src/i18n/messages/dashboard.ts
@@ -0,0 +1,117 @@
+export const zhHans = {
+ dashboard: {
+ setup: {
+ eyebrow: '首次启动',
+ legacyEyebrow: '配置已导入',
+ title: '完成基础配置',
+ body: '连接 Bot、添加存储并授权账号后,即可发送文件或链接开始转存。',
+ requiredSteps: '必需步骤',
+ guide: '打开指南',
+ continue: '继续配置',
+ steps: {
+ bot: 'Bot',
+ storage: '存储',
+ user: '账号',
+ },
+ },
+ status: {
+ reading: '正在读取连接状态',
+ line: 'Bot {bot},Userbot {userbot}',
+ botDisconnected: 'Bot 未连接',
+ },
+ relay: {
+ ariaLabel: '当前中继链路',
+ entry: '入口',
+ queue: '调度队列',
+ queueCount: '{n} 个任务',
+ queueBusy: '处理中或等待 Worker',
+ queueIdle: '队列空闲',
+ storage: '归档存储',
+ storageAvailable: '{n} 个可用',
+ storageTotal: '共 {n} 个已配置存储',
+ },
+ metrics: {
+ activeTasks: '活动任务',
+ weekTasks: '近 7 天任务',
+ completedTasks: '已完成任务',
+ },
+ activity: {
+ viewAll: '查看全部',
+ ariaLabel: '最近 7 天共创建 {n} 个任务',
+ barTitle: '{label} 创建 {n} 个任务',
+ empty: '暂无任务记录',
+ },
+ storage: {
+ title: '存储',
+ manage: '管理',
+ meta: '{type},{location}',
+ empty: '暂无存储',
+ },
+ tasks: {
+ recentTitle: '最近任务',
+ all: '全部任务',
+ separator: ',',
+ noStorage: '未指定存储',
+ empty: '暂无任务',
+ },
+ },
+}
+
+export const en = {
+ dashboard: {
+ setup: {
+ eyebrow: 'First run',
+ legacyEyebrow: 'Settings imported',
+ title: 'Finish setup to start using SaveAny-Bot',
+ body: 'Connect a Telegram bot, add storage, and allow your account. Then send files or links to the bot.',
+ requiredSteps: 'Required steps',
+ guide: 'Open guide',
+ continue: 'Continue setup',
+ steps: {
+ bot: 'Bot',
+ storage: 'Storage',
+ user: 'Account',
+ },
+ },
+ status: {
+ reading: 'Reading connection status',
+ line: 'Bot {bot}, Userbot {userbot}',
+ botDisconnected: 'Bot not connected',
+ },
+ relay: {
+ ariaLabel: 'Current relay chain',
+ entry: 'Ingress',
+ queue: 'Dispatch queue',
+ queueCount: '{n} in queue',
+ queueBusy: 'Processing or waiting for a Worker',
+ queueIdle: 'Queue idle',
+ storage: 'Archive storage',
+ storageAvailable: 'Available: {n}',
+ storageTotal: 'Configured: {n}',
+ },
+ metrics: {
+ activeTasks: 'Active tasks',
+ weekTasks: 'Last 7 days',
+ completedTasks: 'Completed tasks',
+ },
+ activity: {
+ viewAll: 'View all',
+ ariaLabel: 'Tasks created in the last 7 days: {n}',
+ barTitle: '{label}: {n} created',
+ empty: 'No task history yet',
+ },
+ storage: {
+ title: 'Storages',
+ manage: 'Manage',
+ meta: '{type}, {location}',
+ empty: 'No storages yet',
+ },
+ tasks: {
+ recentTitle: 'Recent tasks',
+ all: 'All tasks',
+ separator: ', ',
+ noStorage: 'No storage specified',
+ empty: 'No tasks yet',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/guide.ts b/web/ui/src/i18n/messages/guide.ts
new file mode 100644
index 00000000..032e3d25
--- /dev/null
+++ b/web/ui/src/i18n/messages/guide.ts
@@ -0,0 +1,179 @@
+export const zhHans = {
+ guide: {
+ title: '使用指南',
+ hero: {
+ setupBadge: '首次配置',
+ readyBadge: '已就绪',
+ configuredBadge: '配置完成',
+ setupTitle: '完成 3 项配置,开始转存',
+ setupBody: '连接 Telegram Bot、添加存储并授权账号。',
+ configuredTitle: '配置已完成',
+ configuredBody: 'Bot 当前为 {state}。可前往 Telegram 页面检查连接或重启。',
+ readyTitle: 'Bot 已准备好接收任务',
+ readyBody: '向 Bot 发送文件、媒体或消息链接,任务将出现在任务页。',
+ progress: '{done}/{total} 项必需步骤',
+ requiredSteps: '必需步骤',
+ continueSetup: '继续配置',
+ openTasks: '查看任务',
+ review: '查看配置',
+ openTelegram: '打开 Telegram',
+ passwordNote: '管理员密码在首次启动日志中,或通过 SAVEANY_ADMIN_PASSWORD 指定;登录后可在侧栏修改。',
+ },
+ setup: {
+ title: '首次配置',
+ required: '必需',
+ optional: '可选',
+ done: '已完成',
+ pending: '待完成',
+ open: '去配置',
+ review: '查看',
+ steps: {
+ bot: {
+ title: '连接 Telegram Bot',
+ body: "在 {'@'}BotFather 创建 Bot,把 Token 填入 Telegram 页面。API ID 和 Hash 已提供默认值。",
+ hint: '保存后 Bot 会自动启动,状态显示在页面顶部。',
+ },
+ storage: {
+ title: '添加存储位置',
+ body: '至少启用一个存储。本地磁盘使用本地路径;Docker 部署使用 /app/downloads。',
+ hint: '二进制部署可使用 ./downloads 等相对路径。',
+ },
+ user: {
+ title: '授权 Telegram 账号',
+ body: '只有用户列表中的账号能使用 Bot。添加你的数字 Telegram User ID,并保持启用。',
+ hint: "可向 {'@'}userinfobot 发送任意消息获取自己的 ID。",
+ },
+ running: {
+ title: '启动 Bot 并发送 /start',
+ body: 'Bot 配置完成后会自动启动。在 Telegram 中向它发送 /start,确认能收到回复。',
+ hint: '启动失败时,Telegram 页面会显示具体错误。',
+ },
+ task: {
+ title: '完成第一个任务',
+ body: '发送一个文件或 Telegram 消息链接,任务会出现在任务页,并显示进度与保存位置。',
+ },
+ },
+ },
+ migration: {
+ title: 'v0 配置导入',
+ importedTitle: 'v0 配置已导入',
+ importedAt: '导入时间:{time}',
+ importedBody: '设置、存储与用户数据已导入本地数据库并由 Web 控制台管理。修改 config.toml 不再生效。',
+ intro: 'v1 首次启动时会读取旧版 config.toml,导入设置、存储、用户和 Telegram 凭据。',
+ note: 'Telegram 会话、任务记录、目录、规则和监听不会被导入,需重新配置。',
+ docs: '阅读完整迁移说明',
+ },
+ usage: {
+ title: '日常使用',
+ sendTitle: '发送文件',
+ sendBody: '向 Bot 发送文件、媒体、Telegram 消息链接或 Telegraph 链接,Bot 会下载并转存。',
+ saveTitle: '回复保存',
+ saveBody: '回复任意文件发送 /save。默认目标可在 /config 中设置。',
+ commandsTitle: '常用命令',
+ commandsIntro: '在 Telegram 中发送:',
+ commands: {
+ start: '/start',
+ task: '/tasks',
+ config: '/config',
+ },
+ consoleTitle: '控制台',
+ consoleBody: '任务页查看进度与失败原因,用户页控制账号权限,设置页调整并发、代理及各项功能。',
+ userbotTitle: '受限内容与监听',
+ userbotBody: '在 Telegram 页面登录 Userbot 后可保存受限媒体或监听聊天。',
+ },
+ data: {
+ title: '数据与更新',
+ body: '配置、任务记录和会话均保存在 data/ 目录。升级前请备份该目录;Docker 更新时切勿删除数据卷。',
+ },
+ },
+}
+
+export const en = {
+ guide: {
+ title: 'Guide',
+ hero: {
+ setupBadge: 'First run',
+ readyBadge: 'Ready',
+ configuredBadge: 'Configured',
+ setupTitle: 'Finish 3 steps to start saving',
+ setupBody: 'Connect a Telegram bot, add storage, and allow your account.',
+ configuredTitle: 'Setup complete',
+ configuredBody: 'The bot is currently {state}. Open the Telegram page to check the connection or restart it.',
+ readyTitle: 'Your bot is ready for tasks',
+ readyBody: 'Send a file, media item, or Telegram message link to the bot. Tasks will appear in the Tasks page.',
+ progress: '{done}/{total} required steps',
+ requiredSteps: 'Required steps',
+ continueSetup: 'Continue setup',
+ openTasks: 'Open tasks',
+ review: 'Review setup',
+ openTelegram: 'Open Telegram',
+ passwordNote: 'The admin password is printed in the first startup log or set with SAVEANY_ADMIN_PASSWORD. You can change it from the sidebar after signing in.',
+ },
+ setup: {
+ title: 'First-time setup',
+ required: 'Required',
+ optional: 'Optional',
+ done: 'Done',
+ pending: 'Pending',
+ open: 'Set up',
+ review: 'Review',
+ steps: {
+ bot: {
+ title: 'Connect your Telegram bot',
+ body: "Create a bot with {'@'}BotFather and enter its token on the Telegram page. API ID and hash already have defaults.",
+ hint: 'The bot starts automatically after saving; its state appears at the top of the page.',
+ },
+ storage: {
+ title: 'Add a storage destination',
+ body: 'Enable at least one storage. Local disk uses a local path; in Docker, use /app/downloads.',
+ hint: 'For a binary deployment, a relative path such as ./downloads works.',
+ },
+ user: {
+ title: 'Allow your Telegram account',
+ body: 'Only users in the Users list can use the bot. Add your numeric Telegram user ID and keep it enabled.',
+ hint: "Message {'@'}userinfobot to find your numeric ID.",
+ },
+ running: {
+ title: 'Start the bot and send /start',
+ body: 'The bot starts automatically once configured. Send /start in Telegram and confirm that it replies.',
+ hint: 'If startup fails, the Telegram page shows the error.',
+ },
+ task: {
+ title: 'Finish your first task',
+ body: 'Send a file or Telegram message link. The task will appear in the Tasks page with progress and destination.',
+ },
+ },
+ },
+ migration: {
+ title: 'v0 config import',
+ importedTitle: 'v0 configuration imported',
+ importedAt: 'Imported at {time}',
+ importedBody: 'Settings, storages, and users are stored in the local database and managed from the web console. Later config.toml changes are ignored.',
+ intro: 'v1 reads the legacy config.toml once on first startup and imports settings, storages, users, and Telegram credentials.',
+ note: 'v1 does not import Telegram sessions, task history, directories, rules, or watches; these must be recreated.',
+ docs: 'Read the full migration guide',
+ },
+ usage: {
+ title: 'Everyday use',
+ sendTitle: 'Send files',
+ sendBody: 'Send files, media, Telegram message links, or Telegraph links to the bot. It downloads and saves them.',
+ saveTitle: 'Reply to save',
+ saveBody: 'Reply to any file with /save. Set the default destination in /config.',
+ commandsTitle: 'Common commands',
+ commandsIntro: 'Send these in Telegram:',
+ commands: {
+ start: '/start',
+ task: '/tasks',
+ config: '/config',
+ },
+ consoleTitle: 'Web console',
+ consoleBody: 'Use Tasks for progress and errors, Users for access, and Settings for concurrency, proxies, and optional tools.',
+ userbotTitle: 'Restricted content and watching',
+ userbotBody: 'Sign in to the Userbot on the Telegram page to save restricted media or watch chats.',
+ },
+ data: {
+ title: 'Data and updates',
+ body: 'Settings, task history, and sessions live in data/. Back it up before upgrading. Keep the Docker volume when updating.',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/parsers.ts b/web/ui/src/i18n/messages/parsers.ts
new file mode 100644
index 00000000..7c562f60
--- /dev/null
+++ b/web/ui/src/i18n/messages/parsers.ts
@@ -0,0 +1,41 @@
+export const zhHans = {
+ parsers: {
+ title: '解析器插件',
+ enabled: '已启用',
+ disabled: '已禁用',
+ chooseFile: '选择文件',
+ noFile: '未选择文件',
+ filename: '文件名',
+ content: '插件代码',
+ contentPlaceholder: '// 粘贴解析器 JavaScript 代码',
+ install: '安装插件',
+ installed: '插件已安装',
+ required: '请选择插件文件或填写文件名和代码',
+ installedTitle: '已安装插件',
+ count: '{count} 个文件',
+ directory: '目录',
+ size: '大小',
+ empty: '暂无插件文件',
+ },
+}
+
+export const en = {
+ parsers: {
+ title: 'Parser plugins',
+ enabled: 'Enabled',
+ disabled: 'Disabled',
+ chooseFile: 'Choose file',
+ noFile: 'No file selected',
+ filename: 'Filename',
+ content: 'Plugin code',
+ contentPlaceholder: '// Paste the parser JavaScript here',
+ install: 'Install plugin',
+ installed: 'Plugin installed',
+ required: 'Choose a plugin file or enter a filename and code',
+ installedTitle: 'Installed plugins',
+ count: '{count} files',
+ directory: 'Directory',
+ size: 'Size',
+ empty: 'No plugin files',
+ },
+}
diff --git a/web/ui/src/i18n/messages/settings.ts b/web/ui/src/i18n/messages/settings.ts
new file mode 100644
index 00000000..0ccd95e4
--- /dev/null
+++ b/web/ui/src/i18n/messages/settings.ts
@@ -0,0 +1,283 @@
+export const zhHans = {
+ settings: {
+ loading: '正在加载设置…',
+ groupAria: '设置分组',
+ listSeparator: '、',
+ warningSeparator: ';',
+ groups: {
+ core: '核心',
+ metadata: '消息元数据',
+ media: '媒体过滤',
+ log: '日志',
+ aria2: 'Aria2',
+ ytdlp: 'yt-dlp',
+ parser: '解析器',
+ cache: '缓存',
+ db: '数据库',
+ temp: '临时目录',
+ hook: '钩子',
+ web: 'Web 服务',
+ telegram: 'Telegram 凭据',
+ },
+ core: {
+ botLanguage: 'Bot 语言',
+ langOptions: {
+ zhHans: '简体中文',
+ en: 'English',
+ },
+ workers: '任务并发数',
+ workersHint: '同时处理的任务数;重启后生效',
+ retry: '重试次数',
+ threads: '单文件下载线程数',
+ threadsHint: '自动计算时的单文件基础线程数;下载与上传共享全局并发额度,修改后对后续传输生效',
+ downloadThreads: '下载线程覆盖',
+ downloadThreadsHint: '0 表示自动;大于 0 时直接使用该总线程数,不再按文件大小和辅助 Bot 数计算',
+ maxDownloadThreads: '自动线程上限',
+ maxDownloadThreadsHint: '自动计算时的单文件总线程上限,默认 16',
+ transferBudget: '全局传输并发上限',
+ transferBudgetHint: '0 表示 workers × threads;下载与上传共享,调大可提高并发但可能触发 Telegram 限流',
+ relayMinSizeMb: 'Relay 最小文件大小 (MB)',
+ relayMinSizeMbHint: '小于该大小的文件不使用辅助 Bot 分块,默认 8',
+ chunkRetries: '分块重试次数',
+ chunkRetriesHint: '单个文件分块遇到超时或连接中断时的最大重试次数,默认 20',
+ stream: '流式下载',
+ noCleanCache: '保留缓存',
+ proxy: '全局代理',
+ proxyHint: '重启后生效',
+ },
+ media: {
+ allowedMediaTypes: '允许的媒体类型',
+ allowedMediaTypesHint: '每行一个:image、video、audio、document;留空表示全部允许。过滤仅作用于 Telegram 媒体入口',
+ },
+ metadata: {
+ enabled: '保存消息元数据',
+ enabledHint: '保存消息来源、发送者、文本和媒体属性;仅影响新任务',
+ includeText: '保存消息文本',
+ includeTextHint: '关闭后不保存消息正文和标签',
+ sidecar: '写入旁车 JSON',
+ sidecarHint: '在文件旁写入 .meta.json;Telegram 存储不写入旁车',
+ },
+ log: {
+ level: '日志级别',
+ levels: {
+ debug: 'debug',
+ info: 'info',
+ warn: 'warn',
+ error: 'error',
+ },
+ },
+ aria2: {
+ enable: '启用 Aria2',
+ url: 'RPC 地址',
+ secret: 'RPC 密钥',
+ keepFile: '保留临时文件',
+ },
+ ytdlp: {
+ maxHeight: '最大高度',
+ maxHeightHint: '0 表示不限制',
+ format: '格式选择',
+ recode: '转码容器',
+ filenameTemplate: '文件名模板',
+ restrictFilenames: '限制文件名(仅 ASCII)',
+ },
+ parser: {
+ pluginEnable: '启用插件',
+ restartHint: '重启后生效',
+ pluginDirs: '插件目录',
+ pluginDirsHint: '每行一个目录',
+ proxy: '解析代理',
+ configs: '插件配置 (JSON)',
+ configsHint: '按插件名组织的 JSON 对象',
+ },
+ cache: {
+ ttl: 'TTL(秒)',
+ numCounters: '计数器数量',
+ maxCost: '最大开销',
+ },
+ db: {
+ session: '会话数据库',
+ },
+ temp: {
+ basePath: '基础路径',
+ },
+ hook: {
+ taskBeforeStart: '任务开始前',
+ taskSuccess: '任务成功',
+ taskFail: '任务失败',
+ taskCancel: '任务取消',
+ },
+ web: {
+ enable: '启用 Web 服务',
+ restartHint: '重启后生效',
+ listen: '监听地址',
+ listenHint: '默认仅本机可访问;重启后生效',
+ apiToken: '访问 Token',
+ apiTokenHint: '用于 Web UI 直接访问和 Bearer 认证;留空则每次启动自动生成',
+ secureCookie: 'Secure Cookie',
+ secureCookieHint: '仅在 HTTPS 下启用',
+ },
+ telegram: {
+ token: 'Bot Token',
+ appId: 'API ID',
+ appHash: 'API Hash',
+ rpcRetry: 'RPC 重试次数',
+ mediaGroupTimeout: '媒体组超时(秒)',
+ proxyEnable: '启用代理',
+ proxyUrl: '代理地址',
+ userbotEnable: '启用 Userbot',
+ userbotSession: 'Userbot 会话文件',
+ userbotPhone: 'Userbot 手机号',
+ },
+ errors: {
+ invalidJson: '{field} 不是合法的 JSON',
+ numberRequired: '{field} 必须为数字',
+ minValue: '{field} 不能小于 {min}',
+ },
+ toast: {
+ saved: '{group}设置已保存',
+ restartRequired: '重启后生效:{items}',
+ },
+ },
+}
+
+export const en = {
+ settings: {
+ loading: 'Loading settings…',
+ groupAria: 'Settings groups',
+ listSeparator: ', ',
+ warningSeparator: '; ',
+ groups: {
+ core: 'Core',
+ metadata: 'Message metadata',
+ media: 'Media filter',
+ log: 'Logs',
+ aria2: 'Aria2',
+ ytdlp: 'yt-dlp',
+ parser: 'Parser',
+ cache: 'Cache',
+ db: 'Database',
+ temp: 'Temp directory',
+ hook: 'Hooks',
+ web: 'Web service',
+ telegram: 'Telegram credentials',
+ },
+ core: {
+ botLanguage: 'Bot language',
+ langOptions: {
+ zhHans: 'Simplified Chinese',
+ en: 'English',
+ },
+ workers: 'Task concurrency',
+ workersHint: 'Concurrent tasks; takes effect after restart',
+ retry: 'Retry attempts',
+ threads: 'Download threads per file',
+ threadsHint: 'Base per-file threads for automatic scheduling; downloads and uploads share the global transfer budget. Applies to new transfers immediately',
+ downloadThreads: 'Download thread override',
+ downloadThreadsHint: '0 uses automatic sizing; a positive value sets the total thread count directly, without file-size or helper scaling',
+ maxDownloadThreads: 'Automatic thread cap',
+ maxDownloadThreadsHint: 'Maximum total threads per file when automatic sizing is used. Default: 16',
+ transferBudget: 'Global transfer budget',
+ transferBudgetHint: '0 uses workers × threads; downloads and uploads share this cap. Raising it may trigger Telegram rate limits',
+ relayMinSizeMb: 'Relay minimum file size (MB)',
+ relayMinSizeMbHint: 'Files smaller than this use the primary client instead of helper chunk downloads. Default: 8',
+ chunkRetries: 'Chunk retry count',
+ chunkRetriesHint: 'Maximum retries for timeouts or connection failures on one file chunk. Default: 20',
+ stream: 'Streaming downloads',
+ noCleanCache: 'Keep cache',
+ proxy: 'Global proxy',
+ proxyHint: 'Takes effect after restart',
+ },
+ media: {
+ allowedMediaTypes: 'Allowed media types',
+ allowedMediaTypesHint: 'One per line: image, video, audio, document. Leave empty to allow everything. Applies to Telegram media entry points only',
+ },
+ metadata: {
+ enabled: 'Save message metadata',
+ enabledHint: 'Store source, sender, text, and media attributes; applies to new tasks only',
+ includeText: 'Store message text',
+ includeTextHint: 'Disables message text and tags when off',
+ sidecar: 'Write sidecar JSON',
+ sidecarHint: 'Writes .meta.json beside each file; Telegram storage is excluded',
+ },
+ log: {
+ level: 'Log level',
+ levels: {
+ debug: 'debug',
+ info: 'info',
+ warn: 'warn',
+ error: 'error',
+ },
+ },
+ aria2: {
+ enable: 'Enable Aria2',
+ url: 'RPC endpoint',
+ secret: 'RPC secret',
+ keepFile: 'Keep temporary files',
+ },
+ ytdlp: {
+ maxHeight: 'Max height',
+ maxHeightHint: '0 means unlimited',
+ format: 'Format selection',
+ recode: 'Transcode container',
+ filenameTemplate: 'Filename template',
+ restrictFilenames: 'Restrict filenames (ASCII only)',
+ },
+ parser: {
+ pluginEnable: 'Enable plugins',
+ restartHint: 'Takes effect after restart',
+ pluginDirs: 'Plugin directories',
+ pluginDirsHint: 'One directory per line',
+ proxy: 'Parser proxy',
+ configs: 'Plugin config (JSON)',
+ configsHint: 'JSON object keyed by plugin name',
+ },
+ cache: {
+ ttl: 'TTL (seconds)',
+ numCounters: 'Number of counters',
+ maxCost: 'Max cost',
+ },
+ db: {
+ session: 'Session database',
+ },
+ temp: {
+ basePath: 'Base path',
+ },
+ hook: {
+ taskBeforeStart: 'Before task starts',
+ taskSuccess: 'Task succeeded',
+ taskFail: 'Task failed',
+ taskCancel: 'Task cancelled',
+ },
+ web: {
+ enable: 'Enable web service',
+ restartHint: 'Takes effect after restart',
+ listen: 'Listen address',
+ listenHint: 'Local access only by default; takes effect after restart',
+ apiToken: 'Access token',
+ apiTokenHint: 'Used for direct Web UI access and Bearer authentication; generated each start when empty',
+ secureCookie: 'Secure Cookie',
+ secureCookieHint: 'Only enabled over HTTPS',
+ },
+ telegram: {
+ token: 'Bot Token',
+ appId: 'API ID',
+ appHash: 'API Hash',
+ rpcRetry: 'RPC retry count',
+ mediaGroupTimeout: 'Media group timeout (seconds)',
+ proxyEnable: 'Enable proxy',
+ proxyUrl: 'Proxy URL',
+ userbotEnable: 'Enable Userbot',
+ userbotSession: 'Userbot session file',
+ userbotPhone: 'Userbot phone number',
+ },
+ errors: {
+ invalidJson: '{field} is not valid JSON',
+ numberRequired: '{field} must be a number',
+ minValue: '{field} must be at least {min}',
+ },
+ toast: {
+ saved: '{group} settings saved',
+ restartRequired: 'Takes effect after restart: {items}',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/storages.ts b/web/ui/src/i18n/messages/storages.ts
new file mode 100644
index 00000000..1eb61c68
--- /dev/null
+++ b/web/ui/src/i18n/messages/storages.ts
@@ -0,0 +1,155 @@
+export const zhHans = {
+ storages: {
+ empty: '暂无存储',
+ created: '存储已创建',
+ updated: '存储已更新',
+ deleted: '{name} 已删除',
+ deleteTitle: '删除存储',
+ deleteMessage: '确定删除存储 {name}?已有文件不会被删除。',
+ testOk: '{name} 测试通过',
+ testFailed: '{name} 测试失败:{error}',
+ unknownError: '未知错误',
+ dialog: {
+ create: '新建存储',
+ edit: '编辑存储',
+ },
+ form: {
+ name: '名称',
+ namePlaceholder: '例如 my-webdav',
+ nameRequired: '请填写存储名称',
+ type: '类型',
+ enable: '启用该存储',
+ maskedHint: '已保存的凭据不会再次显示。留空保留原值,输入新值覆盖。',
+ },
+ types: {
+ local: '本地磁盘',
+ webdav: 'WebDAV',
+ alist: 'AList',
+ s3: 'S3 兼容存储',
+ telegram: 'Telegram',
+ rclone: 'rclone',
+ },
+ fields: {
+ basePath: '基础路径',
+ serverUrl: '服务器地址',
+ username: '用户名',
+ password: '密码',
+ token: 'Token',
+ tokenHint: '登录后获取',
+ tokenExp: 'Token 过期时间',
+ tokenExpHint: 'Unix 时间戳,可留空由服务端刷新',
+ endpoint: 'Endpoint',
+ accessKeyId: 'Access Key ID',
+ secretAccessKey: 'Secret Access Key',
+ bucket: 'Bucket',
+ region: 'Region',
+ virtualHost: 'Virtual Host 模式',
+ chatId: 'Chat ID',
+ chatIdHint: '目标频道或群组的数字 ID',
+ forceFile: '强制以文件发送',
+ rateLimit: '速率限制',
+ rateLimitHint: '每秒请求数,0 表示默认',
+ rateBurst: '突发数量',
+ skipLarge: '跳过超限文件',
+ skipLargeHint: '跳过超过 2GB 的文件',
+ splitVideo: '分割大视频',
+ splitSize: '分割大小 (MB)',
+ useMetadataCaption: '使用元数据 caption',
+ useMetadataCaptionHint: '无原始说明文字时,使用元数据中的消息文本作为发送说明',
+ useMetadataAlbum: '使用元数据媒体组',
+ useMetadataAlbumHint: '无实时分组信息时,使用元数据中的 grouped_id 作为媒体组发送',
+ remote: 'Remote 名称',
+ configPath: '配置文件路径',
+ configPathHint: '留空使用默认 rclone 配置',
+ flags: '额外参数',
+ flagsHint: '每行一个参数,例如 --transfers 4',
+ placeholders: {
+ localBasePath: '/data/downloads',
+ basePath: '/saveany',
+ webdavUrl: 'https://dav.example.com',
+ alistUrl: 'https://alist.example.com',
+ s3Endpoint: 'https://s3.example.com',
+ region: 'us-east-1',
+ remote: 'myremote',
+ },
+ },
+ },
+}
+
+export const en = {
+ storages: {
+ empty: 'No storages yet',
+ created: 'Storage created',
+ updated: 'Storage updated',
+ deleted: '{name} deleted',
+ deleteTitle: 'Delete storage',
+ deleteMessage: 'Delete storage {name}? Existing files will not be deleted.',
+ testOk: '{name} test passed',
+ testFailed: '{name} test failed: {error}',
+ unknownError: 'Unknown error',
+ dialog: {
+ create: 'New storage',
+ edit: 'Edit storage',
+ },
+ form: {
+ name: 'Name',
+ namePlaceholder: 'e.g. my-webdav',
+ nameRequired: 'Enter a storage name',
+ type: 'Type',
+ enable: 'Enable this storage',
+ maskedHint: 'Saved secrets are hidden. Leave a field unchanged to keep its value; enter a new value to replace it.',
+ },
+ types: {
+ local: 'Local disk',
+ webdav: 'WebDAV',
+ alist: 'AList',
+ s3: 'S3-compatible',
+ telegram: 'Telegram',
+ rclone: 'rclone',
+ },
+ fields: {
+ basePath: 'Base path',
+ serverUrl: 'Server URL',
+ username: 'Username',
+ password: 'Password',
+ token: 'Token',
+ tokenHint: 'Fetched after sign-in',
+ tokenExp: 'Token expiry',
+ tokenExpHint: 'Unix timestamp, leave empty to let the server refresh it',
+ endpoint: 'Endpoint',
+ accessKeyId: 'Access Key ID',
+ secretAccessKey: 'Secret Access Key',
+ bucket: 'Bucket',
+ region: 'Region',
+ virtualHost: 'Virtual Host mode',
+ chatId: 'Chat ID',
+ chatIdHint: 'Numeric ID of the target channel or group',
+ forceFile: 'Always send as file',
+ rateLimit: 'Rate limit',
+ rateLimitHint: 'Requests per second, 0 for the default',
+ rateBurst: 'Burst size',
+ skipLarge: 'Skip oversized files',
+ skipLargeHint: 'Skip files larger than 2 GB',
+ splitVideo: 'Split large videos',
+ splitSize: 'Split size (MB)',
+ useMetadataCaption: 'Use metadata caption',
+ useMetadataCaptionHint: 'Use message text from metadata as the caption when the live source caption is absent',
+ useMetadataAlbum: 'Use metadata media group',
+ useMetadataAlbumHint: 'Use metadata grouped_id to send files as an album when live grouping is absent',
+ remote: 'Remote name',
+ configPath: 'Config file path',
+ configPathHint: 'Leave empty to use the default rclone config',
+ flags: 'Extra flags',
+ flagsHint: 'One flag per line, e.g. --transfers 4',
+ placeholders: {
+ localBasePath: '/data/downloads',
+ basePath: '/saveany',
+ webdavUrl: 'https://dav.example.com',
+ alistUrl: 'https://alist.example.com',
+ s3Endpoint: 'https://s3.example.com',
+ region: 'us-east-1',
+ remote: 'myremote',
+ },
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/system.ts b/web/ui/src/i18n/messages/system.ts
new file mode 100644
index 00000000..f0197065
--- /dev/null
+++ b/web/ui/src/i18n/messages/system.ts
@@ -0,0 +1,115 @@
+export const zhHans = {
+ system: {
+ restart: '重启进程',
+ update: {
+ title: '版本更新',
+ check: '检查更新',
+ checking: '正在检查…',
+ current: '当前版本',
+ latest: '最新版本',
+ available: '有可用更新',
+ upToDate: '已是最新版本',
+ notUpdatable: '当前运行方式不支持自动更新',
+ major: '需要手动升级大版本',
+ install: '更新并重启',
+ installTitle: '安装更新',
+ installMessage: '下载并安装 {version},完成后进程将自动重启。是否继续?',
+ updating: '正在更新…',
+ updated: '更新完成,进程即将重启',
+ failed: '更新失败',
+ releaseNotes: '更新说明',
+ published: '发布时间',
+ size: '文件大小',
+ notChecked: '尚未检查',
+ noRelease: '未找到发布版本',
+ },
+ process: {
+ running: '进程运行中',
+ uptime: '运行时长',
+ tasks: '任务',
+ queued: '{n} 个排队',
+ concurrency: '任务并发数',
+ loading: '正在读取运行信息…',
+ },
+ info: {
+ title: '运行信息',
+ docker: 'Docker',
+ local: '本地进程',
+ version: '版本',
+ commit: 'Commit',
+ commitUnknown: '未知提交',
+ buildTime: '构建时间',
+ dataDir: '数据目录',
+ database: '数据库',
+ listenAddress: 'Web 地址',
+ },
+ confirm: {
+ restartTitle: '重启进程',
+ restartMessage: '确定重启进程?运行中的任务将被中断。',
+ forceTitle: '强制重启',
+ forceMessage: '仍有任务在运行,强制重启将中断这些任务。是否继续?',
+ },
+ toast: {
+ restarting: '正在重启…',
+ restarted: '重启完成',
+ },
+ },
+}
+
+export const en = {
+ system: {
+ restart: 'Restart process',
+ update: {
+ title: 'Version update',
+ check: 'Check for updates',
+ checking: 'Checking…',
+ current: 'Current version',
+ latest: 'Latest version',
+ available: 'Update available',
+ upToDate: 'Already up to date',
+ notUpdatable: 'Automatic updates are not available for this deployment',
+ major: 'A major upgrade requires a manual migration',
+ install: 'Update and restart',
+ installTitle: 'Install update',
+ installMessage: 'Download and install {version}? The process will restart when it finishes.',
+ updating: 'Updating…',
+ updated: 'Update installed; restarting',
+ failed: 'Update failed',
+ releaseNotes: 'Release notes',
+ published: 'Published',
+ size: 'Size',
+ notChecked: 'Not checked',
+ noRelease: 'No release found',
+ },
+ process: {
+ running: 'Process running',
+ uptime: 'Uptime',
+ tasks: 'Tasks',
+ queued: '{n} queued',
+ concurrency: 'Task concurrency',
+ loading: 'Reading runtime information…',
+ },
+ info: {
+ title: 'Runtime information',
+ docker: 'Docker',
+ local: 'Local process',
+ version: 'Version',
+ commit: 'Commit',
+ commitUnknown: 'Unknown commit',
+ buildTime: 'Build time',
+ dataDir: 'Data directory',
+ database: 'Database',
+ listenAddress: 'Web address',
+ },
+ confirm: {
+ restartTitle: 'Restart process',
+ restartMessage: 'Restart the process? Running tasks will be interrupted.',
+ forceTitle: 'Force restart',
+ forceMessage: 'Tasks are still running. Force restarting will interrupt them. Continue?',
+ },
+ toast: {
+ restarting: 'Restarting…',
+ restarted: 'Restart complete',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/tasks.ts b/web/ui/src/i18n/messages/tasks.ts
new file mode 100644
index 00000000..dc4c161e
--- /dev/null
+++ b/web/ui/src/i18n/messages/tasks.ts
@@ -0,0 +1,361 @@
+export const zhHans = {
+ tasks: {
+ count: '共 {total} 条记录',
+ empty: '暂无任务',
+ createTitle: '新建任务',
+ filters: {
+ status: '状态',
+ type: '类型',
+ allStatuses: '全部状态',
+ allTypes: '全部类型',
+ },
+ status: {
+ queued: '排队中',
+ running: '进行中',
+ completed: '已完成',
+ partial: '部分完成',
+ failed: '失败',
+ cancelled: '已取消',
+ },
+ table: {
+ task: '任务',
+ status: '状态',
+ storagePath: '存储 / 路径',
+ progress: '进度',
+ createdAt: '创建时间',
+ actions: '操作',
+ noStorage: '未指定存储',
+ rootPath: '根目录',
+ },
+ progress: {
+ files: '{done} / {total} 个文件',
+ separator: ',',
+ },
+ toast: {
+ created: '任务已创建',
+ retried: '已创建重试任务,仅包含失败的文件',
+ metadataDownloaded: '元数据已下载',
+ },
+ failedFiles: '失败文件',
+ metadata: '元数据',
+ metadataDialog: {
+ title: '消息元数据',
+ fileCount: '文件:{count}',
+ item: '文件 {index}',
+ version: 'v{version}',
+ loading: '正在加载元数据…',
+ empty: '没有可展示的元数据',
+ unnamed: '未命名文件',
+ download: '下载 JSON',
+ sections: {
+ message: '消息',
+ file: '文件',
+ forward: '转发来源',
+ reply: '回复',
+ },
+ fields: {
+ chat: '聊天',
+ messageId: '消息 ID',
+ date: '时间',
+ edited: '编辑时间',
+ sender: '发送者',
+ topic: '话题 ID',
+ groupedId: '相册分组 ID',
+ link: '链接',
+ text: '文本 / Caption',
+ tags: '标签',
+ name: '文件名',
+ originalName: '原始文件名',
+ mediaType: '媒体类型',
+ mimeType: 'MIME 类型',
+ size: '大小',
+ dimensions: '尺寸',
+ duration: '时长',
+ title: '标题',
+ performer: '作者 / 表演者',
+ sticker: '贴纸 Emoji',
+ author: '原作者',
+ },
+ },
+ confirm: {
+ cancelTitle: '取消任务',
+ cancelMessage: '确定取消该任务?下载将被中断。',
+ cancelConfirm: '取消任务',
+ retryTitle: '重试失败的文件',
+ retryMessage: '将为失败的文件创建新的下载任务。',
+ retryConfirm: '重试',
+ removeTitle: '删除任务记录',
+ removeMessage: '确定删除该任务记录?此操作不可撤销。',
+ removeConfirm: '删除记录',
+ },
+ form: {
+ taskType: '任务类型',
+ storage: '存储',
+ storagePlaceholder: '请选择存储',
+ targetPath: '目标路径',
+ targetPathPlaceholder: '例如 downloads/anime',
+ webhook: 'Webhook',
+ webhookPlaceholder: 'https://example.com/hook',
+ required: '请填写 {field}',
+ invalidJson: '{field} 不是合法的 JSON',
+ jsonObject: '{field} 必须是 JSON 对象',
+ notNumber: '{field} 必须为数字',
+ storageRequired: '请选择存储',
+ pathOutsideStorage: '所选目录不在该存储中',
+ tgfilesSourceRequired: '请填写消息链接,或填写聊天 ID 和消息范围',
+ },
+ types: {
+ directlinks: '直链下载',
+ ytdlp: 'yt-dlp 视频',
+ aria2: 'Aria2 下载',
+ parseditem: '解析链接',
+ tgfiles: 'Telegram 文件',
+ tphpics: 'Telegraph 图集',
+ transfer: '存储迁移',
+ },
+ fields: {
+ directlinks: {
+ urls: {
+ label: '下载链接',
+ },
+ },
+ ytdlp: {
+ urls: {
+ label: '视频链接',
+ },
+ flags: {
+ label: '额外参数',
+ hint: '每行一个 yt-dlp 参数',
+ },
+ },
+ aria2: {
+ urls: {
+ label: '下载链接',
+ },
+ options: {
+ label: 'Aria2 选项 (JSON)',
+ hint: 'JSON 对象,键值均为字符串',
+ },
+ },
+ parseditem: {
+ url: {
+ label: '页面链接',
+ },
+ },
+ tgfiles: {
+ message_links: {
+ label: '消息链接',
+ },
+ chat_id: {
+ label: '聊天 ID / 用户名',
+ },
+ message_range: {
+ label: '消息范围',
+ },
+ filter: {
+ label: '过滤器',
+ },
+ },
+ tphpics: {
+ telegraph_url: {
+ label: 'Telegraph 链接',
+ },
+ },
+ transfer: {
+ source_storage: {
+ label: '源存储',
+ },
+ source_path: {
+ label: '源路径',
+ },
+ target_storage: {
+ label: '目标存储',
+ },
+ target_path: {
+ label: '目标路径',
+ },
+ },
+ },
+ },
+}
+
+export const en = {
+ tasks: {
+ count: 'Total: {total}',
+ empty: 'No tasks',
+ createTitle: 'Create task',
+ filters: {
+ status: 'Status',
+ type: 'Type',
+ allStatuses: 'All statuses',
+ allTypes: 'All types',
+ },
+ status: {
+ queued: 'Queued',
+ running: 'Running',
+ completed: 'Completed',
+ partial: 'Partial',
+ failed: 'Failed',
+ cancelled: 'Cancelled',
+ },
+ table: {
+ task: 'Task',
+ status: 'Status',
+ storagePath: 'Storage / path',
+ progress: 'Progress',
+ createdAt: 'Created',
+ actions: 'Actions',
+ noStorage: 'No storage',
+ rootPath: 'Root',
+ },
+ progress: {
+ files: '{done} / {total} files',
+ separator: ', ',
+ },
+ toast: {
+ created: 'Task created',
+ retried: 'Retry task created for the failed files',
+ metadataDownloaded: 'Metadata downloaded',
+ },
+ failedFiles: 'Failed files',
+ metadata: 'Metadata',
+ metadataDialog: {
+ title: 'Message metadata',
+ fileCount: 'Files: {count}',
+ item: 'File {index}',
+ version: 'v{version}',
+ loading: 'Loading metadata…',
+ empty: 'No metadata to display',
+ unnamed: 'Unnamed file',
+ download: 'Download JSON',
+ sections: {
+ message: 'Message',
+ file: 'File',
+ forward: 'Forwarded from',
+ reply: 'Reply',
+ },
+ fields: {
+ chat: 'Chat',
+ messageId: 'Message ID',
+ date: 'Time',
+ edited: 'Edited',
+ sender: 'Sender',
+ topic: 'Topic ID',
+ groupedId: 'Album group ID',
+ link: 'Link',
+ text: 'Text / Caption',
+ tags: 'Tags',
+ name: 'File name',
+ originalName: 'Original name',
+ mediaType: 'Media type',
+ mimeType: 'MIME type',
+ size: 'Size',
+ dimensions: 'Dimensions',
+ duration: 'Duration',
+ title: 'Title',
+ performer: 'Artist / Performer',
+ sticker: 'Sticker emoji',
+ author: 'Original author',
+ },
+ },
+ confirm: {
+ cancelTitle: 'Cancel task',
+ cancelMessage: 'Cancel this task? The current download will be interrupted.',
+ cancelConfirm: 'Cancel task',
+ retryTitle: 'Retry failed files',
+ retryMessage: 'A new download task will be created for the failed files of this task.',
+ retryConfirm: 'Retry',
+ removeTitle: 'Delete task record',
+ removeMessage: 'Delete this task record? This cannot be undone.',
+ removeConfirm: 'Delete record',
+ },
+ form: {
+ taskType: 'Task type',
+ storage: 'Storage',
+ storagePlaceholder: 'Select storage',
+ targetPath: 'Target path',
+ targetPathPlaceholder: 'e.g. downloads/anime',
+ webhook: 'Webhook',
+ webhookPlaceholder: 'https://example.com/hook',
+ required: 'Enter {field}',
+ invalidJson: '{field} is not valid JSON',
+ jsonObject: '{field} must be a JSON object',
+ notNumber: '{field} must be a number',
+ storageRequired: 'Select storage',
+ pathOutsideStorage: 'The selected folder is outside this storage.',
+ tgfilesSourceRequired: 'Enter message links, or a chat ID and message range',
+ },
+ types: {
+ directlinks: 'Direct link',
+ ytdlp: 'yt-dlp video',
+ aria2: 'Aria2 download',
+ parseditem: 'Parsed link',
+ tgfiles: 'Telegram files',
+ tphpics: 'Telegraph gallery',
+ transfer: 'Storage transfer',
+ },
+ fields: {
+ directlinks: {
+ urls: {
+ label: 'Download links',
+ },
+ },
+ ytdlp: {
+ urls: {
+ label: 'Video links',
+ },
+ flags: {
+ label: 'Extra flags',
+ hint: 'One yt-dlp flag per line',
+ },
+ },
+ aria2: {
+ urls: {
+ label: 'Download links',
+ },
+ options: {
+ label: 'Aria2 options (JSON)',
+ hint: 'JSON object with string keys and values',
+ },
+ },
+ parseditem: {
+ url: {
+ label: 'Page link',
+ },
+ },
+ tgfiles: {
+ message_links: {
+ label: 'Message links',
+ },
+ chat_id: {
+ label: 'Chat ID / username',
+ },
+ message_range: {
+ label: 'Message range',
+ },
+ filter: {
+ label: 'Filter',
+ },
+ },
+ tphpics: {
+ telegraph_url: {
+ label: 'Telegraph link',
+ },
+ },
+ transfer: {
+ source_storage: {
+ label: 'Source storage',
+ },
+ source_path: {
+ label: 'Source path',
+ },
+ target_storage: {
+ label: 'Target storage',
+ },
+ target_path: {
+ label: 'Target path',
+ },
+ },
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/telegram.ts b/web/ui/src/i18n/messages/telegram.ts
new file mode 100644
index 00000000..7a30ad98
--- /dev/null
+++ b/web/ui/src/i18n/messages/telegram.ts
@@ -0,0 +1,199 @@
+export const zhHans = {
+ telegram: {
+ actions: {
+ refreshStatus: '刷新状态',
+ start: '启动',
+ stop: '停止',
+ restart: '重启',
+ },
+ bot: {
+ notConnected: '未连接',
+ },
+ botPanel: {
+ title: 'Bot 与 API',
+ rpcRetry: 'RPC 重试',
+ mediaTimeout: '媒体组超时(秒)',
+ relayChatId: '辅助下载频道 ID',
+ relayHint: '填写 -100 开头的频道 ID;主 Bot 需有发帖权限,且频道中至少包含两个辅助 Bot;0 表示关闭',
+ relayKeepMessages: '保留频道转发消息',
+ relayKeepHint: '开启时保留转发到辅助频道的中间消息;关闭后在下载完成后自动清理',
+ proxyURL: '代理地址',
+ proxyEnable: '使用代理',
+ maskedHint: '已保存的凭据不会再次显示。留空保留原值,输入新值覆盖。',
+ loading: '正在加载凭据…',
+ },
+ userbotPanel: {
+ title: 'Userbot 登录与会话',
+ },
+ userbot: {
+ notLoggedIn: '未登录',
+ sessionConfigured: '会话已配置',
+ loggedIn: 'Userbot 已登录',
+ },
+ flow: {
+ notStarted: '未开始',
+ connecting: '正在连接 Telegram…',
+ verifying: '正在验证,请稍候…',
+ states: {
+ starting: '正在启动',
+ codeRequired: '等待验证码',
+ passwordRequired: '等待两步验证密码',
+ verifying: '正在验证',
+ failed: '登录失败',
+ done: '登录完成',
+ cancelled: '已取消',
+ },
+ },
+ steps: {
+ phone: '手机号',
+ code: '验证码',
+ password: '两步验证',
+ },
+ login: {
+ phoneLabel: '手机号(含国家码)*',
+ codeHint: '验证码会发送到该 Telegram 账号。',
+ codeLabel: 'Telegram 验证码',
+ codePlaceholder: '输入收到的验证码',
+ passwordLabel: '两步验证密码',
+ passwordPlaceholder: '输入两步验证密码',
+ },
+ session: {
+ title: '会话设置',
+ file: '会话文件',
+ enable: '启用 Userbot',
+ loading: '正在加载会话设置…',
+ },
+ validation: {
+ relayChatId: '辅助下载频道 ID 必须为 0 或以 -100 开头的频道 ID',
+ botToken: '请填写 Bot Token',
+ apiId: '请填写有效的 API ID',
+ apiHash: '请填写 API Hash',
+ rpcRetry: '请填写有效的 RPC 重试次数',
+ mediaTimeout: '请填写有效的媒体组超时时间',
+ proxy: '请填写代理地址',
+ session: '请填写会话文件路径',
+ phone: '请填写有效的手机号(含国家码)',
+ code: '请填写验证码',
+ password: '请填写两步验证密码',
+ },
+ toasts: {
+ loginComplete: '登录完成,会话已保存',
+ botSaved: 'Bot 与 API 配置已保存',
+ userbotSaved: 'Userbot 会话设置已保存',
+ botStarting: 'Bot 正在启动',
+ botStopped: 'Bot 已停止',
+ botRestarting: 'Bot 正在重启',
+ loginStarted: '登录流程已启动',
+ loginCancelled: '登录已取消',
+ logoutDone: 'Userbot 已退出登录',
+ },
+ confirm: {
+ forceStopTitle: '强制停止 Bot',
+ forceStopMessage: '{error}\n是否仍强制停止?',
+ forceRestartTitle: '强制重启 Bot',
+ forceRestartMessage: '{error}\n是否仍强制重启?',
+ logoutTitle: '退出 Userbot',
+ logoutMessage: '确定退出 Userbot 并删除本地会话?',
+ },
+ },
+}
+
+export const en = {
+ telegram: {
+ actions: {
+ refreshStatus: 'Refresh status',
+ start: 'Start',
+ stop: 'Stop',
+ restart: 'Restart',
+ },
+ bot: {
+ notConnected: 'Not connected',
+ },
+ botPanel: {
+ title: 'Bot and API',
+ rpcRetry: 'RPC retries',
+ mediaTimeout: 'Media group timeout (seconds)',
+ relayChatId: 'Relay channel ID',
+ relayHint: 'Use the Bot API channel ID (for example -100...); the primary bot must be able to post there and at least two helper bots must be in the channel; 0 disables it',
+ relayKeepMessages: 'Keep forwarded channel messages',
+ relayKeepHint: 'On by default: keep forwarded messages in the relay channel; disable to delete them after download.',
+ proxyURL: 'Proxy URL',
+ proxyEnable: 'Use proxy',
+ maskedHint: 'Saved secrets are hidden. Leave a field unchanged to keep its value; enter a new value to replace it.',
+ loading: 'Loading credentials…',
+ },
+ userbotPanel: {
+ title: 'Userbot login and session',
+ },
+ userbot: {
+ notLoggedIn: 'Not signed in',
+ sessionConfigured: 'Session configured',
+ loggedIn: 'Userbot signed in',
+ },
+ flow: {
+ notStarted: 'Not started',
+ connecting: 'Connecting to Telegram…',
+ verifying: 'Verifying, please wait…',
+ states: {
+ starting: 'Starting',
+ codeRequired: 'Waiting for code',
+ passwordRequired: 'Waiting for 2FA password',
+ verifying: 'Verifying',
+ failed: 'Login failed',
+ done: 'Login complete',
+ cancelled: 'Cancelled',
+ },
+ },
+ steps: {
+ phone: 'Phone',
+ code: 'Code',
+ password: '2FA',
+ },
+ login: {
+ phoneLabel: 'Phone number (with country code) *',
+ codeHint: 'The code is sent to this Telegram account.',
+ codeLabel: 'Telegram code',
+ codePlaceholder: 'Enter the code you received',
+ passwordLabel: 'Two-step verification password',
+ passwordPlaceholder: 'Enter the two-step verification password',
+ },
+ session: {
+ title: 'Session settings',
+ file: 'Session file',
+ enable: 'Enable Userbot',
+ loading: 'Loading session settings…',
+ },
+ validation: {
+ relayChatId: 'Relay channel ID must be 0 or a channel ID starting with -100',
+ botToken: 'Enter the Bot Token',
+ apiId: 'Enter a valid API ID',
+ apiHash: 'Enter the API Hash',
+ rpcRetry: 'Enter a valid RPC retry count',
+ mediaTimeout: 'Enter a valid media group timeout',
+ proxy: 'Enter a proxy URL',
+ session: 'Enter the session file path',
+ phone: 'Enter a valid phone number with country code',
+ code: 'Enter the verification code',
+ password: 'Enter the two-step verification password',
+ },
+ toasts: {
+ loginComplete: 'Login complete. Session saved.',
+ botSaved: 'Bot and API settings saved',
+ userbotSaved: 'Userbot session settings saved',
+ botStarting: 'Bot is starting',
+ botStopped: 'Bot stopped',
+ botRestarting: 'Bot is restarting',
+ loginStarted: 'Login flow started',
+ loginCancelled: 'Login cancelled',
+ logoutDone: 'Userbot signed out',
+ },
+ confirm: {
+ forceStopTitle: 'Force stop bot',
+ forceStopMessage: '{error}\nForce stop anyway?',
+ forceRestartTitle: 'Force restart bot',
+ forceRestartMessage: '{error}\nForce restart anyway?',
+ logoutTitle: 'Sign out Userbot',
+ logoutMessage: 'Sign out the Userbot and delete the local session?',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/types.ts b/web/ui/src/i18n/messages/types.ts
new file mode 100644
index 00000000..6ad79b06
--- /dev/null
+++ b/web/ui/src/i18n/messages/types.ts
@@ -0,0 +1,4 @@
+export interface MessageModule {
+ en: Record
+ zhHans: Record
+}
diff --git a/web/ui/src/i18n/messages/users.ts b/web/ui/src/i18n/messages/users.ts
new file mode 100644
index 00000000..32cde520
--- /dev/null
+++ b/web/ui/src/i18n/messages/users.ts
@@ -0,0 +1,137 @@
+export const zhHans = {
+ users: {
+ add: '添加用户',
+ edit: '编辑用户',
+ count: '{count} 个用户,{enabled} 个启用',
+ empty: '暂无用户',
+ unset: '未设置',
+ storageSeparator: '、',
+ columns: {
+ chatId: 'Chat ID',
+ status: '状态',
+ defaultStorage: '默认存储',
+ storageFilter: '存储过滤',
+ actions: '操作',
+ },
+ status: {
+ enabled: '启用',
+ disabled: '禁用',
+ silent: '静默',
+ },
+ filter: {
+ blacklist: '黑名单',
+ whitelist: '白名单',
+ blacklistOption: '黑名单(排除选中的存储)',
+ whitelistOption: '白名单(仅允许选中的存储)',
+ separator: ':',
+ all: '全部',
+ none: '无',
+ },
+ form: {
+ chatId: 'Telegram Chat ID',
+ defaultStorage: '默认存储',
+ defaultDir: '默认目录',
+ enabled: '启用用户',
+ silent: '静默模式',
+ applyRule: '应用规则',
+ watchNotify: '监听通知',
+ storageFilter: '存储过滤({mode})',
+ storageFilterMode: '过滤模式',
+ filenameStrategy: '文件名策略',
+ conflictStrategy: '重名策略',
+ filenameTemplate: '文件名模板',
+ },
+ filenameStrategy: {
+ default: '默认',
+ message: '从消息生成',
+ template: '自定义模板',
+ },
+ conflictStrategy: {
+ default: '默认',
+ rename: '始终重命名',
+ ask: '每次询问',
+ overwrite: '始终覆盖',
+ skip: '始终跳过',
+ },
+ toast: {
+ created: '用户已创建',
+ updated: '用户已更新',
+ deleted: '{chatId} 已删除',
+ templateRequired: '请填写文件名模板',
+ chatIdInvalid: '请填写有效的 Telegram Chat ID',
+ },
+ confirm: {
+ deleteTitle: '删除用户',
+ deleteMessage: '确定删除用户 {chatId} 及其关联目录与规则?此操作不可撤销。',
+ },
+ },
+}
+
+export const en = {
+ users: {
+ add: 'Add user',
+ edit: 'Edit user',
+ count: 'Total: {count} · Enabled: {enabled}',
+ empty: 'No users yet',
+ unset: 'Not set',
+ storageSeparator: ', ',
+ columns: {
+ chatId: 'Chat ID',
+ status: 'Status',
+ defaultStorage: 'Default storage',
+ storageFilter: 'Storage filter',
+ actions: 'Actions',
+ },
+ status: {
+ enabled: 'Enabled',
+ disabled: 'Disabled',
+ silent: 'Silent',
+ },
+ filter: {
+ blacklist: 'Blacklist',
+ whitelist: 'Whitelist',
+ blacklistOption: 'Blacklist (exclude selected storages)',
+ whitelistOption: 'Whitelist (allow only selected storages)',
+ separator: ': ',
+ all: 'All',
+ none: 'None',
+ },
+ form: {
+ chatId: 'Telegram Chat ID',
+ defaultStorage: 'Default storage',
+ defaultDir: 'Default directory',
+ enabled: 'Enable user',
+ silent: 'Silent mode',
+ applyRule: 'Apply rules',
+ watchNotify: 'Watch notifications',
+ storageFilter: 'Storage filter ({mode})',
+ storageFilterMode: 'Filter mode',
+ filenameStrategy: 'Filename strategy',
+ conflictStrategy: 'Conflict strategy',
+ filenameTemplate: 'Filename template',
+ },
+ filenameStrategy: {
+ default: 'Default',
+ message: 'From message',
+ template: 'Custom template',
+ },
+ conflictStrategy: {
+ default: 'Default',
+ rename: 'Always rename',
+ ask: 'Ask each time',
+ overwrite: 'Always overwrite',
+ skip: 'Always skip',
+ },
+ toast: {
+ created: 'User created',
+ updated: 'User updated',
+ deleted: '{chatId} deleted',
+ templateRequired: 'Enter a filename template',
+ chatIdInvalid: 'Enter a valid Telegram Chat ID',
+ },
+ confirm: {
+ deleteTitle: 'Delete user',
+ deleteMessage: 'Delete user {chatId} along with its directories and rules? This cannot be undone.',
+ },
+ },
+}
diff --git a/web/ui/src/i18n/messages/workers.ts b/web/ui/src/i18n/messages/workers.ts
new file mode 100644
index 00000000..ea654962
--- /dev/null
+++ b/web/ui/src/i18n/messages/workers.ts
@@ -0,0 +1,61 @@
+export const zhHans = {
+ workers: {
+ title: '辅助 Bot',
+ note: '辅助 Bot 需加入目标聊天并具有发送权限。多 Bot 下载需要辅助频道和至少两个辅助 Bot。',
+ count: '辅助 Bot:{count}',
+ empty: '暂无辅助 Bot',
+ create: '添加辅助 Bot',
+ edit: '编辑辅助 Bot',
+ name: '名称',
+ token: 'Bot Token',
+ tokenHint: '编辑时留空表示保留现有 Token',
+ tokenPlaceholder: '123456:ABC-DEF...',
+ enabled: '启用',
+ state: '状态',
+ username: 'Bot 用户名',
+ actions: '操作',
+ start: '启动',
+ stop: '停止',
+ test: '测试',
+ testOk: '{name} 运行正常',
+ testFailed: '{name} 不可用:{error}',
+ created: '辅助 Bot 已添加',
+ updated: '辅助 Bot 已更新',
+ deleted: '辅助 Bot 已删除',
+ deleteTitle: '删除辅助 Bot',
+ deleteMessage: '确定删除 {name}?其本地会话文件也将一并删除。',
+ session: '会话文件',
+ noToken: 'Token 已保存',
+ },
+}
+
+export const en = {
+ workers: {
+ title: 'Helper bots',
+ note: 'Helper bots must be members of the target chat with send permission. Multi-bot downloads require a relay channel and at least two helpers.',
+ count: 'Helper bots: {count}',
+ empty: 'No helper bots yet',
+ create: 'Add helper bot',
+ edit: 'Edit helper bot',
+ name: 'Name',
+ token: 'Bot token',
+ tokenHint: 'Leave empty while editing to keep the saved token',
+ tokenPlaceholder: '123456:ABC-DEF...',
+ enabled: 'Enabled',
+ state: 'Status',
+ username: 'Bot username',
+ actions: 'Actions',
+ start: 'Start',
+ stop: 'Stop',
+ test: 'Test',
+ testOk: '{name} is running',
+ testFailed: '{name} is unavailable: {error}',
+ created: 'Helper bot added',
+ updated: 'Helper bot updated',
+ deleted: 'Helper bot deleted',
+ deleteTitle: 'Delete helper bot',
+ deleteMessage: 'Delete {name}? Its local session file will also be removed.',
+ session: 'Session file',
+ noToken: 'Token saved',
+ },
+}
diff --git a/web/ui/src/main.ts b/web/ui/src/main.ts
new file mode 100644
index 00000000..b0f8ef73
--- /dev/null
+++ b/web/ui/src/main.ts
@@ -0,0 +1,34 @@
+import { createApp } from 'vue'
+import { createPinia } from 'pinia'
+import App from './App.vue'
+import router from './router'
+import { configureApi } from './api'
+import { useAuthStore } from './stores/auth'
+import { useThemeStore } from './stores/theme'
+import { i18n, initI18n } from './i18n'
+import './assets/main.css'
+
+const app = createApp(App)
+const pinia = createPinia()
+app.use(pinia)
+app.use(i18n)
+initI18n()
+
+const theme = useThemeStore()
+theme.init()
+
+const auth = useAuthStore()
+
+configureApi({
+ getCsrf: () => auth.csrf,
+ onUnauthorized: () => {
+ auth.clear()
+ const current = router.currentRoute.value
+ if (current.name !== 'login') {
+ void router.replace({ path: '/login', query: { redirect: current.fullPath } })
+ }
+ },
+})
+
+app.use(router)
+app.mount('#app')
diff --git a/web/ui/src/router.ts b/web/ui/src/router.ts
new file mode 100644
index 00000000..8156613c
--- /dev/null
+++ b/web/ui/src/router.ts
@@ -0,0 +1,126 @@
+import { watch } from 'vue'
+import { createRouter, createWebHistory } from 'vue-router'
+import { i18n, translate } from './i18n'
+import { useAuthStore } from './stores/auth'
+import AppLayout from './components/AppLayout.vue'
+import LoginView from './views/LoginView.vue'
+import DashboardView from './views/DashboardView.vue'
+import GuideView from './views/GuideView.vue'
+import TasksView from './views/TasksView.vue'
+import StoragesView from './views/StoragesView.vue'
+import UsersView from './views/UsersView.vue'
+import AutomationView from './views/AutomationView.vue'
+import ParsersView from './views/ParsersView.vue'
+import TelegramView from './views/TelegramView.vue'
+import SettingsView from './views/SettingsView.vue'
+import SystemView from './views/SystemView.vue'
+
+const router = createRouter({
+ history: createWebHistory(),
+ routes: [
+ {
+ path: '/login',
+ name: 'login',
+ component: LoginView,
+ meta: { title: 'nav.login', public: true },
+ },
+ {
+ path: '/',
+ component: AppLayout,
+ children: [
+ {
+ path: '',
+ name: 'dashboard',
+ component: DashboardView,
+ meta: { title: 'nav.overview' },
+ },
+ {
+ path: 'guide',
+ name: 'guide',
+ component: GuideView,
+ meta: { title: 'nav.guide' },
+ },
+ {
+ path: 'tasks',
+ name: 'tasks',
+ component: TasksView,
+ meta: { title: 'nav.tasks' },
+ },
+ {
+ path: 'storages',
+ name: 'storages',
+ component: StoragesView,
+ meta: { title: 'nav.storages' },
+ },
+ {
+ path: 'users',
+ name: 'users',
+ component: UsersView,
+ meta: { title: 'nav.users' },
+ },
+ {
+ path: 'automation',
+ name: 'automation',
+ component: AutomationView,
+ meta: { title: 'nav.automation' },
+ },
+ {
+ path: 'telegram',
+ name: 'telegram',
+ component: TelegramView,
+ meta: { title: 'nav.telegram' },
+ },
+ {
+ path: 'settings',
+ name: 'settings',
+ component: SettingsView,
+ meta: { title: 'nav.settings' },
+ },
+ {
+ path: 'parsers',
+ name: 'parsers',
+ component: ParsersView,
+ meta: { title: 'nav.parsers' },
+ },
+ {
+ path: 'system',
+ name: 'system',
+ component: SystemView,
+ meta: { title: 'nav.system' },
+ },
+ ],
+ },
+ {
+ path: '/:pathMatch(.*)*',
+ redirect: '/',
+ },
+ ],
+})
+
+router.beforeEach(async (to) => {
+ const auth = useAuthStore()
+ const isPublic = to.meta.public === true
+ if (!auth.checked) {
+ await auth.ensure()
+ }
+ if (isPublic) {
+ if (auth.authenticated && to.name === 'login') {
+ return { path: '/' }
+ }
+ return true
+ }
+ if (!auth.authenticated) {
+ return { path: '/login', query: { redirect: to.fullPath } }
+ }
+ return true
+})
+
+function applyTitle(to = router.currentRoute.value) {
+ const key = typeof to.meta.title === 'string' ? to.meta.title : ''
+ document.title = key ? `${translate(key)} - SaveAny-Bot` : 'SaveAny-Bot'
+}
+
+router.afterEach((to) => applyTitle(to))
+watch(i18n.global.locale, () => applyTitle())
+
+export default router
diff --git a/web/ui/src/settingsFields.ts b/web/ui/src/settingsFields.ts
new file mode 100644
index 00000000..ed648369
--- /dev/null
+++ b/web/ui/src/settingsFields.ts
@@ -0,0 +1,389 @@
+export type SettingsFieldKind = 'text' | 'number' | 'bool' | 'password' | 'select' | 'list' | 'json'
+
+export interface SettingsField {
+ path: string
+ /** Translation key under `settings.*`. */
+ label: string
+ kind: SettingsFieldKind
+ placeholder?: string
+ /** Translation key under `settings.*`. */
+ hint?: string
+ min?: number
+ /** Option labels are translation keys under `settings.*`. */
+ options?: { value: string; label: string }[]
+ /** Span both columns in the settings grid. */
+ wide?: boolean
+ /** Local path fields can use the server-side picker. */
+ pathKind?: 'file' | 'directory'
+}
+
+export interface SettingsGroupDef {
+ key: string
+ /** Translation key under `settings.groups.*`. */
+ title: string
+ fields: SettingsField[]
+}
+
+export const settingsGroups: SettingsGroupDef[] = [
+ {
+ key: 'core',
+ title: 'settings.groups.core',
+ fields: [
+ {
+ path: 'lang',
+ label: 'settings.core.botLanguage',
+ kind: 'select',
+ options: [
+ { value: 'zh-Hans', label: 'settings.core.langOptions.zhHans' },
+ { value: 'en', label: 'settings.core.langOptions.en' },
+ ],
+ },
+ {
+ path: 'workers',
+ label: 'settings.core.workers',
+ kind: 'number',
+ hint: 'settings.core.workersHint',
+ min: 1,
+ },
+ { path: 'retry', label: 'settings.core.retry', kind: 'number', min: 0 },
+ {
+ path: 'threads',
+ label: 'settings.core.threads',
+ kind: 'number',
+ hint: 'settings.core.threadsHint',
+ min: 1,
+ },
+ {
+ path: 'download_threads',
+ label: 'settings.core.downloadThreads',
+ kind: 'number',
+ hint: 'settings.core.downloadThreadsHint',
+ min: 0,
+ },
+ {
+ path: 'max_download_threads',
+ label: 'settings.core.maxDownloadThreads',
+ kind: 'number',
+ hint: 'settings.core.maxDownloadThreadsHint',
+ min: 1,
+ },
+ {
+ path: 'transfer_budget',
+ label: 'settings.core.transferBudget',
+ kind: 'number',
+ hint: 'settings.core.transferBudgetHint',
+ min: 0,
+ },
+ {
+ path: 'relay_min_size_mb',
+ label: 'settings.core.relayMinSizeMb',
+ kind: 'number',
+ hint: 'settings.core.relayMinSizeMbHint',
+ min: 1,
+ },
+ {
+ path: 'chunk_retries',
+ label: 'settings.core.chunkRetries',
+ kind: 'number',
+ hint: 'settings.core.chunkRetriesHint',
+ min: 1,
+ },
+ { path: 'stream', label: 'settings.core.stream', kind: 'bool' },
+ { path: 'no_clean_cache', label: 'settings.core.noCleanCache', kind: 'bool' },
+ {
+ path: 'proxy',
+ label: 'settings.core.proxy',
+ kind: 'text',
+ placeholder: 'socks5://127.0.0.1:1080',
+ hint: 'settings.core.proxyHint',
+ wide: true,
+ },
+ ],
+ },
+ {
+ key: 'metadata',
+ title: 'settings.groups.metadata',
+ fields: [
+ {
+ path: 'enabled',
+ label: 'settings.metadata.enabled',
+ kind: 'bool',
+ hint: 'settings.metadata.enabledHint',
+ },
+ {
+ path: 'include_text',
+ label: 'settings.metadata.includeText',
+ kind: 'bool',
+ hint: 'settings.metadata.includeTextHint',
+ },
+ {
+ path: 'sidecar',
+ label: 'settings.metadata.sidecar',
+ kind: 'bool',
+ hint: 'settings.metadata.sidecarHint',
+ },
+ ],
+ },
+ {
+ key: 'media',
+ title: 'settings.groups.media',
+ fields: [
+ {
+ path: 'allowed_media_types',
+ label: 'settings.media.allowedMediaTypes',
+ kind: 'list',
+ placeholder: 'image\nvideo\naudio\ndocument',
+ hint: 'settings.media.allowedMediaTypesHint',
+ wide: true,
+ },
+ ],
+ },
+ {
+ key: 'log',
+ title: 'settings.groups.log',
+ fields: [
+ {
+ path: 'level',
+ label: 'settings.log.level',
+ kind: 'select',
+ options: [
+ { value: 'debug', label: 'settings.log.levels.debug' },
+ { value: 'info', label: 'settings.log.levels.info' },
+ { value: 'warn', label: 'settings.log.levels.warn' },
+ { value: 'error', label: 'settings.log.levels.error' },
+ ],
+ },
+ ],
+ },
+ {
+ key: 'aria2',
+ title: 'settings.groups.aria2',
+ fields: [
+ { path: 'enable', label: 'settings.aria2.enable', kind: 'bool' },
+ {
+ path: 'url',
+ label: 'settings.aria2.url',
+ kind: 'text',
+ placeholder: 'http://127.0.0.1:6800/jsonrpc',
+ },
+ { path: 'secret', label: 'settings.aria2.secret', kind: 'password' },
+ { path: 'keep_file', label: 'settings.aria2.keepFile', kind: 'bool' },
+ ],
+ },
+ {
+ key: 'ytdlp',
+ title: 'settings.groups.ytdlp',
+ fields: [
+ {
+ path: 'max_height',
+ label: 'settings.ytdlp.maxHeight',
+ kind: 'number',
+ hint: 'settings.ytdlp.maxHeightHint',
+ min: 0,
+ },
+ {
+ path: 'format',
+ label: 'settings.ytdlp.format',
+ kind: 'text',
+ placeholder: 'bestvideo+bestaudio/best',
+ },
+ { path: 'recode', label: 'settings.ytdlp.recode', kind: 'text', placeholder: 'mp4' },
+ {
+ path: 'filename_template',
+ label: 'settings.ytdlp.filenameTemplate',
+ kind: 'text',
+ placeholder: '%(title)s.%(ext)s',
+ },
+ {
+ path: 'restrict_filenames',
+ label: 'settings.ytdlp.restrictFilenames',
+ kind: 'bool',
+ },
+ ],
+ },
+ {
+ key: 'parser',
+ title: 'settings.groups.parser',
+ fields: [
+ {
+ path: 'plugin_enable',
+ label: 'settings.parser.pluginEnable',
+ kind: 'bool',
+ hint: 'settings.parser.restartHint',
+ },
+ {
+ path: 'plugin_dirs',
+ label: 'settings.parser.pluginDirs',
+ kind: 'list',
+ hint: 'settings.parser.pluginDirsHint',
+ placeholder: 'plugins',
+ wide: true,
+ pathKind: 'directory',
+ },
+ {
+ path: 'proxy',
+ label: 'settings.parser.proxy',
+ kind: 'text',
+ placeholder: 'http://127.0.0.1:7890',
+ wide: true,
+ },
+ {
+ path: 'configs',
+ label: 'settings.parser.configs',
+ kind: 'json',
+ placeholder: '{"example": {"key": "value"}}',
+ hint: 'settings.parser.configsHint',
+ wide: true,
+ },
+ ],
+ },
+ {
+ key: 'cache',
+ title: 'settings.groups.cache',
+ fields: [
+ { path: 'ttl', label: 'settings.cache.ttl', kind: 'number', min: 0 },
+ { path: 'num_counters', label: 'settings.cache.numCounters', kind: 'number', min: 1 },
+ { path: 'max_cost', label: 'settings.cache.maxCost', kind: 'number', min: 0, wide: true },
+ ],
+ },
+ {
+ key: 'db',
+ title: 'settings.groups.db',
+ fields: [
+ {
+ path: 'session',
+ label: 'settings.db.session',
+ kind: 'text',
+ placeholder: 'data/bot_session.db',
+ pathKind: 'file',
+ },
+ ],
+ },
+ {
+ key: 'temp',
+ title: 'settings.groups.temp',
+ fields: [
+ {
+ path: 'base_path',
+ label: 'settings.temp.basePath',
+ kind: 'text',
+ placeholder: 'cache/',
+ pathKind: 'directory',
+ },
+ ],
+ },
+ {
+ key: 'hook',
+ title: 'settings.groups.hook',
+ fields: [
+ {
+ path: 'exec.task_before_start',
+ label: 'settings.hook.taskBeforeStart',
+ kind: 'text',
+ placeholder: '/path/to/hook.sh start',
+ },
+ { path: 'exec.task_success', label: 'settings.hook.taskSuccess', kind: 'text' },
+ { path: 'exec.task_fail', label: 'settings.hook.taskFail', kind: 'text' },
+ { path: 'exec.task_cancel', label: 'settings.hook.taskCancel', kind: 'text' },
+ ],
+ },
+ {
+ key: 'web',
+ title: 'settings.groups.web',
+ fields: [
+ {
+ path: 'enable',
+ label: 'settings.web.enable',
+ kind: 'bool',
+ hint: 'settings.web.restartHint',
+ },
+ {
+ path: 'listen',
+ label: 'settings.web.listen',
+ kind: 'text',
+ placeholder: '127.0.0.1:28080',
+ hint: 'settings.web.listenHint',
+ },
+ {
+ path: 'api_token',
+ label: 'settings.web.apiToken',
+ kind: 'password',
+ hint: 'settings.web.apiTokenHint',
+ },
+ {
+ path: 'secure_cookie',
+ label: 'settings.web.secureCookie',
+ kind: 'bool',
+ hint: 'settings.web.secureCookieHint',
+ },
+ ],
+ },
+]
+
+export const telegramGroup: SettingsGroupDef = {
+ key: 'telegram',
+ title: 'settings.groups.telegram',
+ fields: [
+ {
+ path: 'token',
+ label: 'settings.telegram.token',
+ kind: 'password',
+ placeholder: '123456:ABC-DEF...',
+ },
+ { path: 'app_id', label: 'settings.telegram.appId', kind: 'number' },
+ { path: 'app_hash', label: 'settings.telegram.appHash', kind: 'password' },
+ { path: 'rpc_retry', label: 'settings.telegram.rpcRetry', kind: 'number' },
+ { path: 'media_group_timeout', label: 'settings.telegram.mediaGroupTimeout', kind: 'number' },
+ { path: 'proxy.enable', label: 'settings.telegram.proxyEnable', kind: 'bool' },
+ {
+ path: 'proxy.url',
+ label: 'settings.telegram.proxyUrl',
+ kind: 'text',
+ placeholder: 'socks5://127.0.0.1:1080',
+ },
+ { path: 'userbot.enable', label: 'settings.telegram.userbotEnable', kind: 'bool' },
+ { path: 'userbot.session', label: 'settings.telegram.userbotSession', kind: 'text' },
+ {
+ path: 'userbot.phone',
+ label: 'settings.telegram.userbotPhone',
+ kind: 'text',
+ placeholder: '+8613800000000',
+ },
+ ],
+}
+
+export function getByPath(source: Record, path: string): unknown {
+ let current: unknown = source
+ for (const key of path.split('.')) {
+ if (current === null || typeof current !== 'object') {
+ return undefined
+ }
+ current = (current as Record)[key]
+ }
+ return current
+}
+
+export function setByPath(target: Record, path: string, value: unknown): void {
+ const keys = path.split('.')
+ let current: Record = target
+ for (let i = 0; i < keys.length - 1; i += 1) {
+ const key = keys[i]
+ const next = current[key]
+ if (next === null || typeof next !== 'object' || Array.isArray(next)) {
+ const created: Record = {}
+ current[key] = created
+ current = created
+ } else {
+ current = next as Record
+ }
+ }
+ current[keys[keys.length - 1]] = value
+}
+
+export function cloneGroup(source: Record | undefined): Record {
+ if (!source) {
+ return {}
+ }
+ // Values originate from the server's JSON document, so the round-trip shape is preserved.
+ return JSON.parse(JSON.stringify(source)) as Record
+}
diff --git a/web/ui/src/storageFields.ts b/web/ui/src/storageFields.ts
new file mode 100644
index 00000000..e5c37784
--- /dev/null
+++ b/web/ui/src/storageFields.ts
@@ -0,0 +1,220 @@
+import { i18n } from './i18n'
+
+export type StorageFieldKind = 'text' | 'number' | 'bool' | 'secret' | 'list'
+
+export interface StorageField {
+ name: string
+ label: string
+ kind: StorageFieldKind
+ placeholder?: string
+ hint?: string
+ required?: boolean
+ /** Path fields can use the server-side file picker. */
+ pathKind?: 'file' | 'directory'
+}
+
+export interface StorageTypeDef {
+ type: string
+ label: string
+ fields: StorageField[]
+}
+
+export const storageTypeDefs: StorageTypeDef[] = [
+ {
+ type: 'local',
+ label: 'storages.types.local',
+ fields: [
+ {
+ name: 'base_path',
+ label: 'storages.fields.basePath',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.localBasePath',
+ required: true,
+ pathKind: 'directory',
+ },
+ ],
+ },
+ {
+ type: 'webdav',
+ label: 'storages.types.webdav',
+ fields: [
+ {
+ name: 'url',
+ label: 'storages.fields.serverUrl',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.webdavUrl',
+ required: true,
+ },
+ { name: 'username', label: 'storages.fields.username', kind: 'text' },
+ { name: 'password', label: 'storages.fields.password', kind: 'secret' },
+ {
+ name: 'base_path',
+ label: 'storages.fields.basePath',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.basePath',
+ required: true,
+ },
+ ],
+ },
+ {
+ type: 'alist',
+ label: 'storages.types.alist',
+ fields: [
+ {
+ name: 'url',
+ label: 'storages.fields.serverUrl',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.alistUrl',
+ required: true,
+ },
+ { name: 'username', label: 'storages.fields.username', kind: 'text' },
+ { name: 'password', label: 'storages.fields.password', kind: 'secret' },
+ {
+ name: 'token',
+ label: 'storages.fields.token',
+ kind: 'secret',
+ hint: 'storages.fields.tokenHint',
+ },
+ {
+ name: 'base_path',
+ label: 'storages.fields.basePath',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.basePath',
+ required: true,
+ },
+ {
+ name: 'token_exp',
+ label: 'storages.fields.tokenExp',
+ kind: 'number',
+ hint: 'storages.fields.tokenExpHint',
+ },
+ ],
+ },
+ {
+ type: 's3',
+ label: 'storages.types.s3',
+ fields: [
+ {
+ name: 'endpoint',
+ label: 'storages.fields.endpoint',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.s3Endpoint',
+ required: true,
+ },
+ { name: 'access_key_id', label: 'storages.fields.accessKeyId', kind: 'secret', required: true },
+ { name: 'secret_access_key', label: 'storages.fields.secretAccessKey', kind: 'secret', required: true },
+ { name: 'bucket_name', label: 'storages.fields.bucket', kind: 'text', required: true },
+ { name: 'base_path', label: 'storages.fields.basePath', kind: 'text', required: true },
+ { name: 'region', label: 'storages.fields.region', kind: 'text', placeholder: 'storages.fields.placeholders.region' },
+ { name: 'virtual_host', label: 'storages.fields.virtualHost', kind: 'bool' },
+ ],
+ },
+ {
+ type: 'telegram',
+ label: 'storages.types.telegram',
+ fields: [
+ {
+ name: 'chat_id',
+ label: 'storages.fields.chatId',
+ kind: 'number',
+ hint: 'storages.fields.chatIdHint',
+ required: true,
+ },
+ { name: 'force_file', label: 'storages.fields.forceFile', kind: 'bool' },
+ {
+ name: 'rate_limit',
+ label: 'storages.fields.rateLimit',
+ kind: 'number',
+ hint: 'storages.fields.rateLimitHint',
+ },
+ { name: 'rate_burst', label: 'storages.fields.rateBurst', kind: 'number' },
+ { name: 'skip_large', label: 'storages.fields.skipLarge', kind: 'bool', hint: 'storages.fields.skipLargeHint' },
+ { name: 'split_large_video', label: 'storages.fields.splitVideo', kind: 'bool' },
+ { name: 'split_size_mb', label: 'storages.fields.splitSize', kind: 'number' },
+ {
+ name: 'use_metadata_caption',
+ label: 'storages.fields.useMetadataCaption',
+ kind: 'bool',
+ hint: 'storages.fields.useMetadataCaptionHint',
+ },
+ {
+ name: 'use_metadata_album',
+ label: 'storages.fields.useMetadataAlbum',
+ kind: 'bool',
+ hint: 'storages.fields.useMetadataAlbumHint',
+ },
+ ],
+ },
+ {
+ type: 'rclone',
+ label: 'storages.types.rclone',
+ fields: [
+ {
+ name: 'remote',
+ label: 'storages.fields.remote',
+ kind: 'text',
+ placeholder: 'storages.fields.placeholders.remote',
+ required: true,
+ },
+ { name: 'base_path', label: 'storages.fields.basePath', kind: 'text' },
+ {
+ name: 'config_path',
+ label: 'storages.fields.configPath',
+ kind: 'text',
+ hint: 'storages.fields.configPathHint',
+ pathKind: 'file',
+ },
+ { name: 'flags', label: 'storages.fields.flags', kind: 'list', hint: 'storages.fields.flagsHint' },
+ ],
+ },
+]
+
+export function storageTypeDef(type: string): StorageTypeDef | undefined {
+ return storageTypeDefs.find((item) => item.type === type)
+}
+
+export function storageTypeLabel(type: string): string {
+ const def = storageTypeDef(type)
+ return def ? (i18n.global.t(def.label) as string) : type
+}
+
+export function storageTypeIcon(type: string): string {
+ switch (type) {
+ case 'local':
+ return 'folder'
+ case 'webdav':
+ case 'alist':
+ case 's3':
+ return 'cloud'
+ case 'telegram':
+ return 'send'
+ case 'rclone':
+ return 'hardDrive'
+ default:
+ return 'database'
+ }
+}
+
+export function storageDefaults(type: string): Record {
+ const def = storageTypeDef(type)
+ const config: Record = {}
+ if (!def) {
+ return config
+ }
+ for (const field of def.fields) {
+ switch (field.kind) {
+ case 'bool':
+ config[field.name] = false
+ break
+ case 'number':
+ config[field.name] = 0
+ break
+ case 'list':
+ config[field.name] = []
+ break
+ default:
+ config[field.name] = ''
+ }
+ }
+ return config
+}
diff --git a/web/ui/src/stores/auth.ts b/web/ui/src/stores/auth.ts
new file mode 100644
index 00000000..c9cbbfc1
--- /dev/null
+++ b/web/ui/src/stores/auth.ts
@@ -0,0 +1,65 @@
+import { defineStore } from 'pinia'
+import { ApiError, authApi } from '../api'
+
+interface AuthState {
+ csrf: string
+ authenticated: boolean
+ checked: boolean
+}
+
+export const useAuthStore = defineStore('auth', {
+ state: (): AuthState => ({
+ csrf: '',
+ authenticated: false,
+ checked: false,
+ }),
+ actions: {
+ clear() {
+ this.csrf = ''
+ this.authenticated = false
+ this.checked = true
+ },
+ async login(password: string) {
+ try {
+ const result = await authApi.login(password)
+ this.csrf = result.csrf_token
+ this.authenticated = true
+ this.checked = true
+ } catch (err) {
+ this.clear()
+ throw err
+ }
+ },
+ async logout() {
+ try {
+ await authApi.logout()
+ } finally {
+ this.clear()
+ }
+ },
+ async changePassword(currentPassword: string, newPassword: string) {
+ await authApi.changePassword(currentPassword, newPassword)
+ },
+ async ensure(): Promise {
+ if (this.checked) {
+ return this.authenticated
+ }
+ try {
+ const me = await authApi.me()
+ this.authenticated = me.authenticated === true
+ this.csrf = this.authenticated ? (me.csrf_token ?? '') : ''
+ } catch (err) {
+ if (err instanceof ApiError && err.status === 401) {
+ this.authenticated = false
+ this.csrf = ''
+ } else {
+ this.authenticated = false
+ this.csrf = ''
+ }
+ } finally {
+ this.checked = true
+ }
+ return this.authenticated
+ },
+ },
+})
diff --git a/web/ui/src/stores/confirm.ts b/web/ui/src/stores/confirm.ts
new file mode 100644
index 00000000..aabcb198
--- /dev/null
+++ b/web/ui/src/stores/confirm.ts
@@ -0,0 +1,51 @@
+import { defineStore } from 'pinia'
+import { translate } from '../i18n'
+
+export interface ConfirmOptions {
+ title: string
+ message: string
+ confirmText?: string
+ cancelText?: string
+ danger?: boolean
+}
+
+interface ConfirmState {
+ open: boolean
+ options: Required
+}
+
+let resolveCurrent: ((confirmed: boolean) => void) | null = null
+
+export const useConfirmStore = defineStore('confirm', {
+ state: (): ConfirmState => ({
+ open: false,
+ options: {
+ title: translate('common.confirmTitle'),
+ message: '',
+ confirmText: translate('common.confirm'),
+ cancelText: translate('common.cancel'),
+ danger: false,
+ },
+ }),
+ actions: {
+ ask(options: ConfirmOptions): Promise {
+ resolveCurrent?.(false)
+ this.options = {
+ title: options.title,
+ message: options.message,
+ confirmText: options.confirmText ?? translate('common.confirm'),
+ cancelText: options.cancelText ?? translate('common.cancel'),
+ danger: options.danger ?? false,
+ }
+ this.open = true
+ const { promise, resolve } = Promise.withResolvers()
+ resolveCurrent = resolve
+ return promise
+ },
+ respond(confirmed: boolean) {
+ this.open = false
+ resolveCurrent?.(confirmed)
+ resolveCurrent = null
+ },
+ },
+})
diff --git a/web/ui/src/stores/theme.ts b/web/ui/src/stores/theme.ts
new file mode 100644
index 00000000..b0cbec1b
--- /dev/null
+++ b/web/ui/src/stores/theme.ts
@@ -0,0 +1,70 @@
+import { defineStore } from 'pinia'
+
+export type ThemePreference = 'system' | 'light' | 'dark'
+export type ResolvedTheme = 'light' | 'dark'
+
+export const THEME_STORAGE_KEY = 'saveany-theme'
+
+const systemMedia = window.matchMedia('(prefers-color-scheme: dark)')
+const themeColors: Record = {
+ light: '#f5f6f8',
+ dark: '#0e1014',
+}
+
+let systemListenerAttached = false
+
+function readPreference(): ThemePreference {
+ try {
+ const value = localStorage.getItem(THEME_STORAGE_KEY)
+ if (value === 'light' || value === 'dark' || value === 'system') return value
+ } catch {
+ // Storage may be unavailable in privacy-restricted contexts.
+ }
+ return 'system'
+}
+
+export const useThemeStore = defineStore('theme', {
+ state: () => ({
+ preference: 'system' as ThemePreference,
+ systemDark: systemMedia.matches,
+ }),
+ getters: {
+ resolved(state): ResolvedTheme {
+ if (state.preference === 'system') return state.systemDark ? 'dark' : 'light'
+ return state.preference
+ },
+ },
+ actions: {
+ init() {
+ this.preference = readPreference()
+ this.systemDark = systemMedia.matches
+ this.apply()
+
+ if (!systemListenerAttached) {
+ systemMedia.addEventListener('change', (event) => {
+ this.systemDark = event.matches
+ if (this.preference === 'system') this.apply()
+ })
+ systemListenerAttached = true
+ }
+ },
+ setPreference(preference: ThemePreference) {
+ this.preference = preference
+ try {
+ localStorage.setItem(THEME_STORAGE_KEY, preference)
+ } catch {
+ // The in-memory preference still applies for this session.
+ }
+ this.apply()
+ },
+ apply() {
+ const theme = this.resolved
+ const root = document.documentElement
+ root.dataset.theme = theme
+ root.style.colorScheme = theme
+ document
+ .querySelector('meta[name="theme-color"]')
+ ?.setAttribute('content', themeColors[theme])
+ },
+ },
+})
diff --git a/web/ui/src/stores/toasts.ts b/web/ui/src/stores/toasts.ts
new file mode 100644
index 00000000..e0d25ba0
--- /dev/null
+++ b/web/ui/src/stores/toasts.ts
@@ -0,0 +1,51 @@
+import { defineStore } from 'pinia'
+
+export type ToastTone = 'success' | 'error' | 'info'
+
+export interface ToastItem {
+ id: number
+ tone: ToastTone
+ message: string
+}
+
+let nextId = 1
+const timers = new Map()
+
+export const useToastStore = defineStore('toasts', {
+ state: () => ({
+ items: [] as ToastItem[],
+ }),
+ actions: {
+ push(message: string, tone: ToastTone = 'info', duration = 4200): number {
+ const existing = this.items.find((item) => item.message === message && item.tone === tone)
+ if (existing) return existing.id
+ const id = nextId++
+ this.items.push({ id, tone, message })
+ if (this.items.length > 4) {
+ this.dismiss(this.items[0].id)
+ }
+ if (duration > 0) {
+ timers.set(
+ id,
+ window.setTimeout(() => this.dismiss(id), duration),
+ )
+ }
+ return id
+ },
+ dismiss(id: number) {
+ const timer = timers.get(id)
+ if (timer !== undefined) {
+ window.clearTimeout(timer)
+ timers.delete(id)
+ }
+ this.items = this.items.filter((item) => item.id !== id)
+ },
+ clear() {
+ for (const timer of timers.values()) {
+ window.clearTimeout(timer)
+ }
+ timers.clear()
+ this.items = []
+ },
+ },
+})
diff --git a/web/ui/src/styles/controls.css b/web/ui/src/styles/controls.css
new file mode 100644
index 00000000..e45eef62
--- /dev/null
+++ b/web/ui/src/styles/controls.css
@@ -0,0 +1,551 @@
+/* Buttons */
+
+.btn {
+ position: relative;
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ gap: 8px;
+ height: 38px;
+ padding: 0 14px;
+ color: var(--text);
+ background: var(--surface-strong);
+ border: 1px solid var(--line-strong);
+ border-radius: 8px;
+ font-size: 14px;
+ font-weight: 500;
+ line-height: 1;
+ white-space: nowrap;
+ transition:
+ background-color 160ms var(--ease),
+ border-color 160ms var(--ease),
+ color 160ms var(--ease),
+ transform 120ms var(--ease);
+}
+
+.btn:hover:not(:disabled) {
+ background: var(--surface-muted);
+ border-color: var(--text-3);
+}
+
+.btn:active:not(:disabled) {
+ transform: scale(0.98);
+}
+
+.btn svg {
+ display: block;
+ flex: none;
+}
+
+.btn-main {
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ gap: 8px;
+}
+
+.btn-spinner {
+ position: absolute;
+ top: 50%;
+ left: 50%;
+ width: 15px;
+ height: 15px;
+ border: 1.5px solid currentColor;
+ border-right-color: transparent;
+ border-radius: 50%;
+ animation: spin 700ms linear infinite;
+ transform: translate(-50%, -50%);
+}
+
+.btn.is-loading .btn-main {
+ opacity: 0;
+}
+
+.btn.is-loading:disabled {
+ opacity: 1;
+ cursor: wait;
+}
+
+.btn.primary.is-loading:disabled {
+ color: #f4f6f9;
+ background: var(--ink);
+ border-color: var(--ink);
+}
+
+.btn.danger.is-loading:disabled {
+ color: var(--danger);
+ background: transparent;
+ border-color: var(--line);
+}
+
+@keyframes spin {
+ to {
+ transform: translate(-50%, -50%) rotate(360deg);
+ }
+}
+
+.btn.primary {
+ color: #f4f6f9;
+ background: var(--ink);
+ border-color: var(--ink);
+}
+
+.btn.primary:hover:not(:disabled) {
+ background: var(--ink-raised);
+ border-color: var(--ink-raised);
+}
+
+.btn.ghost {
+ color: var(--text-2);
+ background: transparent;
+ border-color: transparent;
+}
+
+.btn.ghost:hover:not(:disabled) {
+ color: var(--text);
+ background: rgba(17, 19, 23, 0.05);
+ border-color: transparent;
+}
+
+.btn.danger {
+ color: var(--danger);
+ background: transparent;
+ border-color: var(--line);
+}
+
+.btn.danger:hover:not(:disabled) {
+ background: var(--danger-soft);
+ border-color: rgba(158, 63, 75, 0.35);
+}
+
+.btn.danger-solid {
+ color: #fff;
+ background: var(--danger-solid);
+ border-color: var(--danger-solid);
+}
+
+.btn.danger-solid:hover:not(:disabled) {
+ background: var(--danger-solid-hover);
+ border-color: var(--danger-solid-hover);
+}
+
+.btn.small {
+ height: 32px;
+ padding: 0 10px;
+ font-size: 14px;
+}
+
+.btn.icon {
+ width: 36px;
+ padding: 0;
+}
+
+.btn:disabled {
+ opacity: 0.48;
+ cursor: not-allowed;
+}
+
+.btn.primary:disabled {
+ color: var(--text-2);
+ background: var(--line);
+ border-color: var(--line);
+ opacity: 1;
+}
+
+.actions {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 8px;
+}
+
+.task-table .actions .btn {
+ min-width: 112px;
+}
+
+/* Status and labels */
+
+.badge {
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ height: 22px;
+ padding: 0 8px;
+ color: var(--text-2);
+ background: var(--surface-muted);
+ border: 1px solid transparent;
+ border-radius: var(--radius-sm);
+ font-size: 12px;
+ font-weight: 500;
+ line-height: 1;
+ white-space: nowrap;
+}
+
+.badge.status::before {
+ content: '';
+ width: 5px;
+ height: 5px;
+ flex: none;
+ background: currentColor;
+ border-radius: 50%;
+}
+
+.badge.success {
+ color: var(--success);
+ background: var(--success-soft);
+}
+
+.badge.warning {
+ color: var(--warning);
+ background: var(--warning-soft);
+}
+
+.badge.danger {
+ color: var(--danger);
+ background: var(--danger-soft);
+}
+
+.badge.info {
+ color: var(--info);
+ background: var(--info-soft);
+}
+
+.badge.muted {
+ color: var(--text-3);
+ background: var(--surface-muted);
+}
+
+.count {
+ color: var(--text-3);
+ font-size: 12px;
+ font-variant-numeric: tabular-nums;
+}
+
+/* Forms */
+
+.form-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
+ gap: 16px 18px;
+}
+
+.form-grid .span-2,
+.form-grid .span-full {
+ grid-column: 1 / -1;
+}
+
+.field {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 7px;
+}
+
+.field > label {
+ color: var(--text-2);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.field input,
+.field textarea {
+ width: 100%;
+ color: var(--text);
+ background: var(--surface-strong);
+ border: 1px solid var(--line-strong);
+ border-radius: 8px;
+ outline: none;
+ transition:
+ border-color 160ms var(--ease),
+ box-shadow 160ms var(--ease),
+ background-color 160ms var(--ease);
+}
+
+.field input {
+ height: 40px;
+ padding: 0 12px;
+}
+
+.field textarea {
+ min-height: 108px;
+ padding: 10px 12px;
+ resize: vertical;
+ line-height: 1.55;
+}
+
+.field input::placeholder,
+.field textarea::placeholder {
+ color: #96a2b2;
+}
+
+.field input:hover,
+.field textarea:hover {
+ border-color: #a9b4c3;
+}
+
+.field input:focus,
+.field textarea:focus {
+ border-color: var(--accent);
+ box-shadow: 0 0 0 3px var(--accent-soft);
+}
+
+.field.is-invalid input,
+.field.is-invalid textarea,
+.field.is-invalid .select-trigger {
+ border-color: var(--danger);
+ animation: field-shake 280ms linear;
+}
+
+@keyframes field-shake {
+ 0% {
+ transform: translateX(0);
+ animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
+ }
+ 28% {
+ transform: translateX(5px);
+ animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
+ }
+ 57% {
+ transform: translateX(-5px);
+ animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
+ }
+ 78% {
+ transform: translateX(3px);
+ animation-timing-function: cubic-bezier(0.22, 1, 0.36, 1);
+ }
+ 100% {
+ transform: translateX(0);
+ }
+}
+
+.field.is-invalid input:focus,
+.field.is-invalid textarea:focus,
+.field.is-invalid .select-trigger[aria-expanded='true'] {
+ border-color: var(--danger);
+ box-shadow: 0 0 0 3px var(--danger-soft);
+}
+
+.field input:disabled,
+.field textarea:disabled {
+ color: var(--text-3);
+ background: var(--surface-muted);
+ opacity: 1;
+}
+
+.field .hint,
+.hint {
+ color: var(--text-3);
+ font-size: 12px;
+ line-height: 1.55;
+}
+
+.checkbox {
+ display: flex;
+ min-height: 40px;
+ flex-direction: row;
+ align-items: center;
+ gap: 9px;
+ padding-top: 18px;
+ cursor: pointer;
+}
+
+.checkbox input {
+ width: 16px;
+ height: 16px;
+ flex: none;
+ accent-color: var(--accent);
+}
+
+.checkbox span {
+ color: var(--text-2);
+ font-size: 14px;
+}
+
+.checkbox-list {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 10px 18px;
+ padding: 8px 0 2px;
+}
+
+.checkbox-list label {
+ display: inline-flex;
+ align-items: center;
+ gap: 7px;
+ color: var(--text-2);
+ font-size: 14px;
+ cursor: pointer;
+}
+
+.checkbox-list input {
+ width: 15px;
+ height: 15px;
+ accent-color: var(--accent);
+}
+
+.field-error {
+ color: var(--danger);
+ font-size: 12px;
+ line-height: 1.55;
+}
+
+/* Custom select */
+
+.select {
+ position: relative;
+ width: 100%;
+ min-width: 0;
+}
+
+.select-trigger {
+ display: flex;
+ width: 100%;
+ height: 40px;
+ align-items: center;
+ justify-content: space-between;
+ gap: 8px;
+ padding: 0 11px;
+ color: var(--text);
+ background: var(--surface-strong);
+ border: 1px solid var(--line-strong);
+ border-radius: 8px;
+ text-align: left;
+ transition:
+ border-color 160ms var(--ease),
+ box-shadow 160ms var(--ease);
+}
+
+.select-trigger:hover:not(:disabled) {
+ border-color: #a9b4c3;
+}
+
+.select-trigger[aria-expanded='true'] {
+ border-color: var(--accent);
+ box-shadow: 0 0 0 3px var(--accent-soft);
+}
+
+.select-trigger:disabled {
+ color: var(--text-3);
+ background: var(--surface-muted);
+ cursor: not-allowed;
+}
+
+.select-value {
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ font-size: 14px;
+}
+
+.select-value.placeholder {
+ color: #96a2b2;
+}
+
+.select-chevron {
+ flex: none;
+ color: var(--text-3);
+ transition: transform 160ms var(--ease);
+}
+
+.select-trigger[aria-expanded='true'] .select-chevron {
+ transform: rotate(180deg);
+}
+
+.select-menu {
+ position: fixed;
+ z-index: 90;
+ overflow-y: auto;
+ padding: 4px;
+ margin: 0;
+ list-style: none;
+ background: var(--surface-strong);
+ border: 1px solid var(--line);
+ border-radius: 9px;
+ box-shadow: 0 16px 40px rgba(17, 19, 23, 0.14);
+}
+
+.select-option {
+ display: flex;
+ min-height: 34px;
+ align-items: center;
+ justify-content: space-between;
+ gap: 10px;
+ padding: 7px 9px;
+ color: var(--text-2);
+ border-radius: 6px;
+ font-size: 14px;
+ cursor: pointer;
+}
+
+.select-option.active {
+ color: var(--text);
+ background: var(--surface-muted);
+}
+
+.select-option.selected {
+ color: var(--text);
+}
+
+.select-option > span {
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.select-option.selected svg {
+ flex: none;
+ color: var(--accent-strong);
+}
+
+.select-option.disabled {
+ color: var(--text-3);
+ cursor: not-allowed;
+ opacity: 0.6;
+}
+
+.select-menu {
+ transform-origin: top center;
+}
+
+.select-menu[data-placement='top'] {
+ transform-origin: bottom center;
+}
+
+.select-menu-enter-active {
+ transition:
+ opacity var(--duration-fast) var(--ease-smooth-out),
+ transform var(--duration-fast) var(--ease-smooth-out),
+ filter var(--duration-fast) var(--ease-smooth-out);
+}
+
+.select-menu-leave-active {
+ transition:
+ opacity var(--duration-quick) var(--ease-smooth-out),
+ transform var(--duration-quick) var(--ease-smooth-out),
+ filter var(--duration-quick) var(--ease-smooth-out);
+}
+
+.select-menu-enter-from,
+.select-menu-leave-to {
+ opacity: 0;
+ transform: translateY(-4px) scale(0.97);
+ filter: blur(var(--blur-small));
+}
+
+.select-menu[data-placement='top'].select-menu-enter-from,
+.select-menu[data-placement='top'].select-menu-leave-to {
+ transform: translateY(4px) scale(0.97);
+}
+
+.filter-field .select-trigger {
+ height: 34px;
+ padding: 0 9px;
+ border-radius: 7px;
+}
+
+.filter-field .select-value {
+ font-size: 12px;
+}
+
diff --git a/web/ui/src/styles/dialogs.css b/web/ui/src/styles/dialogs.css
new file mode 100644
index 00000000..235aadcf
--- /dev/null
+++ b/web/ui/src/styles/dialogs.css
@@ -0,0 +1,189 @@
+/* Dialogs */
+
+.modal-overlay.confirm-overlay {
+ z-index: 80;
+}
+
+.confirm-modal {
+ max-width: 420px;
+}
+
+.confirm-heading {
+ display: flex;
+ align-items: flex-start;
+ gap: 14px;
+}
+
+.confirm-icon {
+ display: grid;
+ width: 36px;
+ height: 36px;
+ flex: none;
+ place-items: center;
+ color: var(--success);
+ background: var(--success-soft);
+ border-radius: 9px;
+}
+
+.confirm-icon.danger {
+ color: var(--danger);
+ background: var(--danger-soft);
+}
+
+.confirm-message {
+ margin: 8px 0 0;
+ color: var(--text-2);
+ font-size: 14px;
+ line-height: 1.65;
+ white-space: pre-line;
+}
+
+.modal-overlay {
+ position: fixed;
+ inset: 0;
+ z-index: 60;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ padding: 24px;
+ background: rgba(9, 11, 14, 0.58);
+ backdrop-filter: blur(3px);
+}
+
+.modal {
+ display: flex;
+ width: 100%;
+ max-width: 620px;
+ max-height: 90vh;
+ flex-direction: column;
+ gap: 18px;
+ overflow-y: auto;
+ padding: 26px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 16px;
+ box-shadow: var(--shadow);
+}
+
+.modal.wide {
+ max-width: 860px;
+}
+
+.modal-enter-active,
+.modal-leave-active {
+ transition: opacity var(--duration-fast) var(--ease-smooth-out);
+}
+
+.modal-enter-from,
+.modal-leave-to {
+ opacity: 0;
+}
+
+.modal-enter-active .modal {
+ animation: modal-in var(--duration-fast) var(--ease-smooth-out) backwards;
+}
+
+.modal-leave-active {
+ transition-duration: var(--duration-quick);
+}
+
+.modal-leave-active .modal {
+ animation: modal-out var(--duration-quick) var(--ease-smooth-out) forwards;
+}
+
+@keyframes modal-in {
+ from {
+ opacity: 0;
+ transform: translateY(var(--distance-base)) scale(var(--scale-large));
+ filter: blur(var(--blur-small));
+ }
+ to {
+ opacity: 1;
+ transform: none;
+ filter: blur(0);
+ }
+}
+
+@keyframes modal-out {
+ from {
+ opacity: 1;
+ transform: none;
+ filter: blur(0);
+ }
+ to {
+ opacity: 0;
+ transform: translateY(4px) scale(0.98);
+ filter: blur(1px);
+ }
+}
+
+.modal-head {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+}
+
+.modal-title {
+ color: var(--text);
+ font-size: 20px;
+ font-weight: 500;
+ letter-spacing: -0.015em;
+}
+
+.modal .stack {
+ gap: 16px;
+}
+
+.auto-height-modal {
+ max-height: calc(100vh - 48px);
+ max-height: calc(100dvh - 48px);
+ overflow: hidden;
+}
+
+.auto-height-modal .modal-head {
+ flex: none;
+}
+
+.auto-height-modal .modal-form {
+ min-height: 0;
+ flex: 1 1 auto;
+}
+
+.modal-form-body {
+ display: flex;
+ min-height: 0;
+ flex: 1 1 auto;
+ flex-direction: column;
+ gap: 16px;
+ overflow-y: auto;
+ padding-right: 6px;
+ margin-right: -6px;
+ scrollbar-gutter: stable;
+ scrollbar-width: thin;
+ scrollbar-color: var(--line-strong) transparent;
+}
+
+.modal-form-body::-webkit-scrollbar {
+ width: 7px;
+}
+
+.modal-form-body::-webkit-scrollbar-thumb {
+ background: var(--line-strong);
+ border-radius: 999px;
+}
+
+.auto-height-modal .modal-actions {
+ flex: none;
+ padding-top: 14px;
+ margin-top: 0;
+ border-top: 1px solid var(--line);
+}
+
+.modal-actions {
+ display: flex;
+ justify-content: flex-end;
+ gap: 8px;
+ margin-top: 4px;
+}
+
diff --git a/web/ui/src/styles/feedback.css b/web/ui/src/styles/feedback.css
new file mode 100644
index 00000000..1a5ad405
--- /dev/null
+++ b/web/ui/src/styles/feedback.css
@@ -0,0 +1,310 @@
+/* Feedback */
+
+.empty {
+ padding: 36px 20px;
+ color: var(--text-3);
+ font-size: 14px;
+ text-align: center;
+}
+
+.empty strong {
+ display: block;
+ margin-bottom: 4px;
+ color: var(--text-2);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.empty-icon {
+ display: grid;
+ place-items: center;
+ width: 38px;
+ height: 38px;
+ margin: 0 auto 12px;
+ color: var(--text-3);
+ background: var(--surface-muted);
+ border-radius: 50%;
+}
+
+.loading {
+ padding: 24px;
+ color: transparent;
+ background-image: linear-gradient(90deg, var(--text-3) 20%, var(--text-2) 50%, var(--text-3) 80%);
+ background-size: 250% 100%;
+ -webkit-background-clip: text;
+ background-clip: text;
+ -webkit-text-fill-color: transparent;
+ font-size: 14px;
+ animation: loading-shimmer 1.8s var(--ease-linear) infinite;
+}
+
+.page-loading {
+ display: grid;
+ gap: 0;
+ overflow: hidden;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.page-loading--rows {
+ padding: 0 18px;
+}
+
+.page-loading-row {
+ display: flex;
+ align-items: center;
+ gap: 14px;
+ min-height: 70px;
+ padding: 14px 0;
+ border-bottom: 1px solid var(--line);
+}
+
+.page-loading-row:last-child {
+ border-bottom: 0;
+}
+
+.page-loading-icon {
+ flex: none;
+ width: 36px;
+ height: 36px;
+ border-radius: 10px;
+}
+
+.page-loading-copy {
+ display: grid;
+ flex: 1;
+ gap: 8px;
+ min-width: 0;
+}
+
+.page-loading-title {
+ width: min(280px, 40%);
+ height: 11px;
+}
+
+.page-loading-text {
+ width: min(420px, 65%);
+ height: 9px;
+}
+
+.page-loading-text.short {
+ width: 45%;
+}
+
+.page-loading-action {
+ flex: none;
+ width: 70px;
+ height: 28px;
+ border-radius: var(--radius-sm);
+}
+
+.page-loading--cards {
+ grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
+ gap: 12px;
+ padding: 0;
+ background: transparent;
+ border: 0;
+}
+
+.page-loading-card {
+ display: flex;
+ flex-direction: column;
+ gap: 10px;
+ min-height: 190px;
+ padding: 18px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.page-loading-card .page-loading-icon {
+ width: 40px;
+ height: 40px;
+}
+
+.page-loading-footer {
+ width: 90px;
+ height: 28px;
+ margin-top: auto;
+ border-radius: var(--radius-sm);
+}
+
+.skeleton-block {
+ display: block;
+ background: var(--surface-muted);
+ border-radius: 999px;
+ animation: skeleton-pulse 1.6s ease-in-out infinite;
+}
+
+@keyframes skeleton-pulse {
+ 0%,
+ 100% {
+ opacity: 0.5;
+ }
+ 50% {
+ opacity: 0.9;
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .skeleton-block {
+ animation: none;
+ }
+}
+
+@keyframes loading-shimmer {
+ from {
+ background-position: 100% 0;
+ }
+ to {
+ background-position: 0 0;
+ }
+}
+
+@supports not (background-clip: text) {
+ .loading {
+ color: var(--text-3);
+ -webkit-text-fill-color: currentColor;
+ background: none;
+ animation: none;
+ }
+}
+
+.progress {
+ height: 5px;
+ overflow: hidden;
+ background: var(--surface-muted);
+ border-radius: 999px;
+}
+
+.progress-bar {
+ height: 100%;
+ background: var(--accent);
+ border-radius: inherit;
+ transition: width 360ms var(--ease);
+}
+
+.kv {
+ display: grid;
+ grid-template-columns: 150px minmax(0, 1fr);
+ gap: 13px 20px;
+ margin: 0;
+}
+
+.kv dt {
+ color: var(--text-3);
+ font-size: 14px;
+}
+
+.kv dd {
+ margin: 0;
+ color: var(--text);
+ font-size: 14px;
+ overflow-wrap: anywhere;
+}
+
+/* Transient notifications */
+
+.toast-stack {
+ position: fixed;
+ right: 24px;
+ bottom: 24px;
+ z-index: 70;
+ display: flex;
+ width: min(380px, calc(100vw - 32px));
+ flex-direction: column;
+ gap: 10px;
+ pointer-events: none;
+}
+
+.toast {
+ display: flex;
+ align-items: flex-start;
+ gap: 10px;
+ padding: 13px 14px;
+ background: var(--surface-strong);
+ border: 1px solid var(--line);
+ border-radius: 10px;
+ box-shadow: 0 12px 32px rgba(17, 19, 23, 0.12);
+ transform-origin: bottom right;
+ pointer-events: auto;
+}
+
+.toast-icon {
+ display: grid;
+ flex: none;
+ place-items: center;
+ width: 24px;
+ height: 24px;
+ border-radius: 6px;
+}
+
+.toast.success .toast-icon {
+ color: var(--success);
+ background: var(--success-soft);
+}
+
+.toast.error .toast-icon {
+ color: var(--danger);
+ background: var(--danger-soft);
+}
+
+.toast.info .toast-icon {
+ color: var(--info);
+ background: var(--info-soft);
+}
+
+.toast p {
+ flex: 1;
+ margin: 1px 0 0;
+ color: var(--text);
+ font-size: 14px;
+ line-height: 1.55;
+ overflow-wrap: anywhere;
+}
+
+.toast-close {
+ display: grid;
+ flex: none;
+ place-items: center;
+ width: 24px;
+ height: 24px;
+ color: var(--text-3);
+ background: transparent;
+ border: 0;
+ border-radius: 5px;
+}
+
+.toast-close:hover {
+ color: var(--text);
+ background: var(--surface-muted);
+}
+
+.toast-enter-active {
+ transition:
+ opacity var(--duration-medium) var(--ease-smooth-out),
+ transform var(--duration-medium) var(--ease-smooth-out),
+ filter var(--duration-medium) var(--ease-smooth-out);
+}
+
+.toast-leave-active {
+ position: absolute;
+ width: 100%;
+ transition:
+ opacity var(--duration-fast) var(--ease-smooth-out),
+ transform var(--duration-fast) var(--ease-smooth-out),
+ filter var(--duration-fast) var(--ease-smooth-out);
+}
+
+.toast-enter-from,
+.toast-leave-to {
+ opacity: 0;
+ transform: translateY(16px) scale(0.97);
+ filter: blur(var(--blur-small));
+}
+
+.toast-move {
+ transition: transform var(--duration-fast) var(--ease-smooth-out);
+}
+
diff --git a/web/ui/src/styles/motion.css b/web/ui/src/styles/motion.css
new file mode 100644
index 00000000..aed47c9b
--- /dev/null
+++ b/web/ui/src/styles/motion.css
@@ -0,0 +1,47 @@
+/* Route transitions */
+
+.route-enter-active {
+ transition:
+ opacity var(--duration-fast) var(--ease-smooth-out),
+ transform var(--duration-fast) var(--ease-smooth-out),
+ filter var(--duration-fast) var(--ease-smooth-out);
+ will-change: opacity, transform, filter;
+}
+
+.route-leave-active {
+ transition:
+ opacity 120ms var(--ease-smooth-out),
+ transform 120ms var(--ease-smooth-out),
+ filter 120ms var(--ease-smooth-out);
+ will-change: opacity, transform, filter;
+}
+
+.route-enter-from {
+ opacity: 0;
+ transform: translateY(var(--distance-base));
+ filter: blur(var(--blur-small));
+}
+
+.route-leave-to {
+ opacity: 0;
+ transform: translateY(-3px);
+ filter: blur(1px);
+}
+
+.shell-enter-active {
+ transition: opacity var(--duration-fast) var(--ease-smooth-out);
+}
+
+.shell-leave-active {
+ position: fixed;
+ inset: 0;
+ z-index: 5;
+ pointer-events: none;
+ transition: opacity 120ms var(--ease-smooth-out);
+}
+
+.shell-enter-from,
+.shell-leave-to {
+ opacity: 0;
+}
+
diff --git a/web/ui/src/styles/reduced-motion.css b/web/ui/src/styles/reduced-motion.css
new file mode 100644
index 00000000..af93781c
--- /dev/null
+++ b/web/ui/src/styles/reduced-motion.css
@@ -0,0 +1,12 @@
+@media (prefers-reduced-motion: reduce) {
+ *,
+ *::before,
+ *::after {
+ scroll-behavior: auto !important;
+ animation-delay: 0ms !important;
+ animation-duration: 0.01ms !important;
+ animation-iteration-count: 1 !important;
+ transition-duration: 0.01ms !important;
+ }
+}
+
diff --git a/web/ui/src/styles/responsive.css b/web/ui/src/styles/responsive.css
new file mode 100644
index 00000000..53dde40a
--- /dev/null
+++ b/web/ui/src/styles/responsive.css
@@ -0,0 +1,433 @@
+@media (max-width: 1180px) {
+ .dashboard-split {
+ grid-template-columns: minmax(0, 1.3fr) minmax(260px, 0.7fr);
+ }
+
+ .telegram-form-grid {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ }
+}
+
+@media (max-width: 1080px) {
+ .task-table-wrap,
+ .users-table-wrap,
+ .helper-bots-table-wrap {
+ display: none;
+ }
+
+ .task-mobile-list,
+ .user-mobile-list,
+ .helper-bots-mobile {
+ display: flex;
+ }
+}
+
+@media (max-width: 960px) {
+ .sidebar {
+ transform: translateX(-100%);
+ transition: transform 260ms var(--ease);
+ }
+
+ .sidebar.open {
+ transform: translateX(0);
+ }
+
+ .sidebar-close {
+ display: inline-flex;
+ }
+
+ .main {
+ margin-left: 0;
+ }
+
+ .topbar {
+ display: flex;
+ justify-content: flex-start;
+ gap: 12px;
+ padding: 0 20px;
+ }
+
+ .btn.menu-button {
+ display: inline-flex;
+ }
+
+ .topbar-mobile-title {
+ display: block;
+ }
+
+ .content {
+ padding: 24px 20px 56px;
+ }
+
+ .sidebar-backdrop {
+ position: fixed;
+ inset: 0;
+ z-index: 35;
+ background: rgba(9, 11, 14, 0.45);
+ }
+
+ .backdrop-enter-active,
+ .backdrop-leave-active {
+ transition: opacity var(--duration-fast) var(--ease-smooth-out);
+ }
+
+ .backdrop-enter-from,
+ .backdrop-leave-to {
+ opacity: 0;
+ }
+
+ .settings-layout {
+ grid-template-columns: 1fr;
+ gap: 18px;
+ }
+
+ .userbot-layout {
+ grid-template-columns: 1fr;
+ }
+
+ .settings-mobile-nav {
+ position: sticky;
+ top: 64px;
+ z-index: 12;
+ display: block;
+ padding: 8px 0 10px;
+ background: var(--paper);
+ }
+
+ .settings-nav {
+ display: none;
+ }
+
+ .settings-group {
+ scroll-margin-top: 132px;
+ }
+
+ .page-head {
+ align-items: flex-start;
+ flex-direction: column;
+ gap: 16px;
+ }
+
+ .page-actions {
+ display: flex;
+ width: 100%;
+ flex-wrap: wrap;
+ }
+
+ .page-actions .btn {
+ width: auto;
+ flex: 0 0 160px;
+ }
+
+ .page-actions .btn:not(:only-child) {
+ flex: 1 1 0;
+ max-width: 190px;
+ }
+}
+
+@media (max-width: 760px) {
+ .content {
+ gap: 16px;
+ }
+
+ .page-head h1 {
+ font-size: 24px;
+ }
+
+ .helper-bots-panel .panel-head {
+ align-items: center;
+ flex-wrap: wrap;
+ gap: 12px;
+ }
+
+ .helper-bots-panel .panel-title {
+ white-space: nowrap;
+ }
+
+ .helper-bots-panel .panel-head .actions {
+ flex: 1 1 100%;
+ flex-wrap: wrap;
+ }
+
+ .helper-bots-panel .panel-head .actions .btn {
+ flex: 1 1 130px;
+ justify-content: center;
+ }
+
+ .relay-board {
+ padding: 22px;
+ gap: 20px;
+ }
+
+ .relay-flow {
+ flex-direction: column;
+ gap: 0;
+ }
+
+ .relay-link {
+ width: auto;
+ height: 28px;
+ transform: rotate(90deg);
+ }
+
+ .relay-metrics {
+ grid-template-columns: 1fr;
+ gap: 16px;
+ }
+
+ .dashboard-split,
+ .connection-grid,
+ .process-panel {
+ grid-template-columns: 1fr;
+ }
+
+ .process-metrics {
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+ }
+
+ .storage-grid {
+ grid-template-columns: 1fr;
+ }
+
+ .form-grid {
+ grid-template-columns: 1fr;
+ }
+
+ .relay-settings {
+ grid-template-columns: 1fr;
+ gap: 12px;
+ }
+
+ .relay-toggle {
+ padding-top: 0;
+ }
+
+ .modal {
+ padding: 22px 18px;
+ }
+
+ .table-wrap {
+ border-radius: 12px;
+ }
+
+ th,
+ td {
+ padding: 12px 14px;
+ }
+
+ .kv {
+ grid-template-columns: 1fr;
+ gap: 2px;
+ }
+
+ .kv dd {
+ margin-bottom: 10px;
+ }
+}
+
+@media (max-width: 520px) {
+ .helper-bots-panel .panel-head {
+ align-items: stretch;
+ flex-direction: column;
+ gap: 12px;
+ }
+
+ .helper-bots-panel .panel-head .actions {
+ width: 100%;
+ }
+
+ .helper-bots-panel .panel-head .actions .badge {
+ align-self: center;
+ }
+
+ .toast-stack {
+ right: 16px;
+ bottom: 16px;
+ left: 16px;
+ width: auto;
+ }
+
+ .topbar {
+ padding: 0 16px;
+ }
+
+ .content {
+ padding: 20px 16px 48px;
+ }
+
+ .card,
+ .panel {
+ padding: 20px 18px;
+ }
+
+ .page-head h1 {
+ font-size: 24px;
+ }
+
+ .process-metrics {
+ grid-template-columns: 1fr;
+ gap: 14px;
+ }
+
+ .recent-task {
+ align-items: flex-start;
+ flex-direction: column;
+ gap: 8px;
+ }
+
+ .recent-task-side {
+ width: 100%;
+ justify-content: space-between;
+ }
+
+ .recent-task-progress {
+ width: 120px;
+ }
+
+ .chart-label {
+ font-size: 12px;
+ }
+}
+
+@media (max-width: 520px) {
+ .login-page {
+ gap: 16px;
+ padding: 24px 16px;
+ overflow: auto;
+ }
+
+ .login-trace {
+ display: none;
+ }
+
+ .login-preferences {
+ top: 16px;
+ right: 16px;
+ }
+
+ .login-card {
+ padding: 24px;
+ border-radius: 16px;
+ }
+
+ .login-heading {
+ margin-top: 28px;
+ }
+
+ .login-heading h1 {
+ font-size: 24px;
+ }
+}
+
+.setup-callout {
+ display: grid;
+ grid-template-columns: minmax(0, 1fr) auto;
+ gap: 24px;
+ align-items: center;
+ padding: 22px 24px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-left: 3px solid var(--accent);
+ border-radius: var(--radius-lg);
+}
+
+.setup-callout-main {
+ min-width: 0;
+}
+
+.setup-eyebrow {
+ color: var(--accent-strong);
+ font-size: 11px;
+ font-weight: 600;
+ letter-spacing: 0.06em;
+ text-transform: uppercase;
+}
+
+.setup-callout h2 {
+ margin-top: 5px;
+ color: var(--text);
+ font-size: 18px;
+ font-weight: 500;
+ letter-spacing: -0.02em;
+}
+
+.setup-callout p {
+ max-width: 72ch;
+ margin-top: 5px;
+ color: var(--text-2);
+ font-size: 13px;
+ line-height: 1.6;
+}
+
+.setup-checks {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 8px;
+ margin-top: 14px;
+}
+
+.setup-check {
+ display: inline-flex;
+ align-items: center;
+ gap: 6px;
+ height: 26px;
+ padding: 0 9px;
+ color: var(--text-3);
+ background: var(--surface-muted);
+ border: 1px solid transparent;
+ border-radius: 999px;
+ font-size: 12px;
+}
+
+.setup-check.done {
+ color: var(--success);
+ background: var(--success-soft);
+}
+
+.setup-check-icon {
+ display: grid;
+ width: 14px;
+ height: 14px;
+ place-items: center;
+ color: currentColor;
+ border: 1px solid currentColor;
+ border-radius: 50%;
+}
+
+.setup-check.done .setup-check-icon {
+ border-color: transparent;
+}
+
+.setup-callout-side {
+ display: flex;
+ flex-direction: column;
+ align-items: flex-end;
+ gap: 3px;
+}
+
+.setup-callout-side strong {
+ color: var(--text);
+ font-size: 28px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.05em;
+ line-height: 1;
+}
+
+.setup-callout-side strong span {
+ color: var(--text-3);
+ font-size: 15px;
+ letter-spacing: -0.02em;
+}
+
+.setup-callout-side small {
+ color: var(--text-3);
+ font-size: 11px;
+ text-align: right;
+}
+
+.setup-callout-side .actions {
+ justify-content: flex-end;
+ margin-top: 8px;
+}
+
diff --git a/web/ui/src/styles/shell.css b/web/ui/src/styles/shell.css
new file mode 100644
index 00000000..b1fff96a
--- /dev/null
+++ b/web/ui/src/styles/shell.css
@@ -0,0 +1,448 @@
+/* Application shell */
+
+.app-shell {
+ min-height: 100vh;
+ background: var(--paper);
+}
+
+.layout {
+ display: flex;
+ min-height: 100vh;
+}
+
+.sidebar {
+ position: fixed;
+ inset: 0 auto 0 0;
+ z-index: 40;
+ display: flex;
+ width: 256px;
+ flex-direction: column;
+ background: var(--ink);
+ color: var(--ink-text);
+ border-right: 1px solid rgba(17, 19, 23, 0.35);
+}
+
+.brand {
+ display: flex;
+ align-items: center;
+ gap: 12px;
+ min-height: 72px;
+ padding: 18px 20px;
+ border-bottom: 1px solid var(--ink-line);
+}
+
+.brand-mark {
+ display: grid;
+ place-items: center;
+ width: 36px;
+ height: 36px;
+ flex: none;
+ color: var(--ink-text);
+ background: var(--ink-raised);
+ border: 1px solid rgba(237, 240, 244, 0.12);
+ border-radius: 9px;
+}
+
+.brand-copy {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ line-height: 1.25;
+}
+
+.brand-copy strong {
+ color: #f2f3f5;
+ font-size: 16px;
+ font-weight: 600;
+}
+
+.brand-copy span {
+ margin-top: 2px;
+ color: var(--ink-muted);
+ font-size: 12px;
+}
+
+.sidebar-close {
+ display: none;
+ margin-left: auto;
+ padding: 4px;
+ color: var(--ink-muted);
+ background: transparent;
+ border: 0;
+ border-radius: var(--radius-sm);
+}
+
+.sidebar-close:hover {
+ color: var(--ink-text);
+ background: rgba(255, 255, 255, 0.06);
+}
+
+.nav {
+ display: flex;
+ flex: 1;
+ flex-direction: column;
+ gap: 22px;
+ overflow-y: auto;
+ padding: 20px 12px 24px;
+}
+
+.nav-group {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+}
+
+.nav-label {
+ padding: 0 12px;
+ margin-bottom: 7px;
+ color: var(--ink-muted);
+ font-size: 12px;
+}
+
+.nav a {
+ position: relative;
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ height: 38px;
+ padding: 0 12px;
+ color: #c2cad4;
+ border-radius: 8px;
+ font-size: 14px;
+ transition:
+ color 160ms var(--ease),
+ background-color 160ms var(--ease);
+}
+
+.nav a svg {
+ flex: none;
+ opacity: 0.85;
+}
+
+.nav a:hover {
+ color: #f1f2f4;
+ background: rgba(255, 255, 255, 0.055);
+}
+
+.nav a.is-active {
+ color: var(--ink);
+ background: #e9ebef;
+ font-weight: 500;
+}
+
+.nav a::before {
+ content: '';
+ position: absolute;
+ left: -12px;
+ top: 10px;
+ width: 3px;
+ height: 18px;
+ background: var(--accent);
+ border-radius: 0 3px 3px 0;
+ opacity: 0;
+ transform: scaleY(0.35);
+ transition:
+ opacity var(--duration-quick) var(--ease-smooth-out),
+ transform var(--duration-fast) var(--ease-smooth-out);
+}
+
+.nav a.is-active::before {
+ opacity: 1;
+ transform: scaleY(1);
+}
+
+.nav a.is-active svg {
+ color: var(--accent-strong);
+ opacity: 1;
+}
+
+.sidebar-foot {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ padding: 18px 20px 20px;
+ border-top: 1px solid var(--ink-line);
+}
+
+.sidebar-status {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ color: #c7cbd2;
+ font-size: 12px;
+}
+
+.sidebar-account {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 16px;
+ margin: 2px 0;
+}
+
+.sidebar-action {
+ display: inline-flex;
+ min-width: 0;
+ flex: none;
+ align-items: center;
+ gap: 6px;
+ height: 24px;
+ padding: 0;
+ color: #aab0b9;
+ background: transparent;
+ border: 0;
+ border-radius: 0;
+ font-size: 12px;
+ text-align: left;
+ white-space: nowrap;
+ transition: color 140ms var(--ease);
+}
+
+.sidebar-action:hover {
+ color: #f2f3f5;
+ background: transparent;
+ text-decoration: underline;
+ text-underline-offset: 3px;
+}
+
+.sidebar-account .sidebar-action:last-child {
+ justify-content: flex-end;
+ text-align: right;
+}
+
+.theme-control {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 8px;
+ margin: 2px 0;
+}
+
+.theme-control-label {
+ color: var(--ink-muted);
+ font-size: 12px;
+}
+
+.theme-switch,
+.locale-switch {
+ display: inline-flex;
+ gap: 2px;
+ padding: 2px;
+ background: rgba(255, 255, 255, 0.05);
+ border: 1px solid var(--ink-line);
+ border-radius: 8px;
+}
+
+.theme-switch button,
+.locale-switch button {
+ display: grid;
+ min-width: 26px;
+ height: 26px;
+ place-items: center;
+ padding: 0 5px;
+ color: #9aa3ae;
+ background: transparent;
+ border: 0;
+ border-radius: 5px;
+ font-size: 11px;
+ transition:
+ color 160ms var(--ease),
+ background-color 160ms var(--ease);
+}
+
+.theme-switch button:hover,
+.locale-switch button:hover {
+ color: #f2f3f5;
+ background: rgba(255, 255, 255, 0.06);
+}
+
+.theme-switch button.active,
+.locale-switch button.active {
+ color: var(--ink);
+ background: #e9ebef;
+}
+
+.live-dot {
+ width: 6px;
+ height: 6px;
+ flex: none;
+ background: #83b79b;
+ border-radius: 50%;
+ box-shadow: 0 0 0 3px rgba(131, 183, 155, 0.08);
+}
+
+.live-dot.accent {
+ background: var(--accent);
+ box-shadow: 0 0 0 3px rgba(63, 95, 158, 0.16);
+}
+
+.live-dot.running {
+ animation: breathe 2.4s ease-in-out infinite;
+}
+
+.live-dot.idle {
+ background: #7b8490;
+ box-shadow: 0 0 0 3px rgba(123, 132, 144, 0.12);
+}
+
+.live-dot.error {
+ background: #d79aa2;
+ box-shadow: 0 0 0 3px rgba(215, 154, 162, 0.12);
+}
+
+.live-dot.warning {
+ background: #cbb27f;
+ box-shadow: 0 0 0 3px rgba(203, 178, 127, 0.12);
+}
+
+@keyframes breathe {
+ 0%,
+ 100% {
+ opacity: 1;
+ }
+ 50% {
+ opacity: 0.4;
+ }
+}
+
+.main {
+ display: flex;
+ min-width: 0;
+ min-height: 100vh;
+ flex: 1;
+ flex-direction: column;
+ margin-left: 256px;
+}
+
+.topbar {
+ position: sticky;
+ top: 0;
+ z-index: 20;
+ display: none;
+ align-items: center;
+ justify-content: space-between;
+ gap: 16px;
+ min-height: 64px;
+ padding: 0 32px;
+ background: rgba(245, 246, 248, 0.94);
+ border-bottom: 1px solid var(--line);
+ backdrop-filter: blur(10px);
+}
+
+.btn.menu-button {
+ display: none;
+}
+
+.topbar-mobile-title {
+ display: none;
+ color: var(--text-2);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.content {
+ display: flex;
+ width: 100%;
+ max-width: 1440px;
+ flex-direction: column;
+ gap: 20px;
+ margin: 0 auto;
+ padding: 30px 32px 64px;
+}
+
+/* Page structure */
+
+.page-head {
+ display: flex;
+ align-items: flex-end;
+ justify-content: space-between;
+ gap: 24px;
+ padding-bottom: 6px;
+}
+
+.page-head h1 {
+ color: var(--text);
+ font-size: 28px;
+ font-weight: 500;
+ letter-spacing: -0.025em;
+ line-height: 1.15;
+}
+
+.page-actions {
+ display: flex;
+ flex: none;
+ align-items: center;
+ gap: 8px;
+ width: max-content;
+}
+
+.page-actions .btn {
+ width: 160px;
+}
+
+.stack {
+ display: flex;
+ flex-direction: column;
+ gap: 20px;
+}
+
+.row {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 10px;
+}
+
+.grow {
+ min-width: 0;
+ flex: 1;
+}
+
+.muted {
+ color: var(--text-3);
+}
+
+/* Surfaces */
+
+.panel {
+ padding: 24px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.panel-head {
+ display: flex;
+ align-items: flex-start;
+ justify-content: space-between;
+ gap: 16px;
+ margin-bottom: 18px;
+}
+
+.panel-title {
+ color: var(--text);
+ font-size: 16px;
+ font-weight: 500;
+}
+
+.stat-label {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.stat-value {
+ margin-top: 6px;
+ color: var(--text);
+ font-size: 28px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.025em;
+ line-height: 1.15;
+}
+
+.stat-sub {
+ margin-top: 6px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
diff --git a/web/ui/src/styles/tables.css b/web/ui/src/styles/tables.css
new file mode 100644
index 00000000..c7b0914d
--- /dev/null
+++ b/web/ui/src/styles/tables.css
@@ -0,0 +1,347 @@
+/* Tables */
+
+.table-wrap {
+ overflow-x: auto;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+table {
+ width: 100%;
+ min-width: 760px;
+ border-collapse: collapse;
+ font-size: 14px;
+}
+
+th {
+ padding: 11px 18px;
+ color: var(--text-3);
+ background: var(--surface-muted);
+ border-bottom: 1px solid var(--line);
+ font-size: 12px;
+ font-weight: 500;
+ text-align: left;
+ white-space: nowrap;
+}
+
+td {
+ padding: 14px 18px;
+ vertical-align: middle;
+ border-bottom: 1px solid var(--line);
+}
+
+tbody tr:last-child td {
+ border-bottom: none;
+}
+
+tbody tr {
+ transition: background-color 140ms var(--ease);
+}
+
+tbody tr:hover {
+ background: rgba(17, 19, 23, 0.025);
+}
+
+.filter-bar {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: flex-end;
+ gap: 12px;
+ padding: 14px 16px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 12px;
+}
+
+.filter-field {
+ display: flex;
+ min-width: 150px;
+ flex-direction: column;
+ gap: 5px;
+}
+
+.filter-field label {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.filter-spacer {
+ flex: 1;
+}
+
+.task-cell-title {
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+ overflow-wrap: anywhere;
+}
+
+.task-cell-meta {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 3px 7px;
+ margin-top: 3px;
+ color: var(--text-3);
+ font-size: 12px;
+ overflow-wrap: anywhere;
+}
+
+.task-path {
+ color: var(--text-3);
+ font-family: var(--font-mono);
+ font-size: 12px;
+ overflow-wrap: anywhere;
+}
+
+.task-failed {
+ margin-top: 7px;
+}
+
+.task-failed-title {
+ color: var(--danger);
+ font-size: 12px;
+ font-weight: 500;
+}
+
+.task-failed-items {
+ margin: 3px 0 0;
+ padding: 0;
+ list-style: none;
+ display: grid;
+ gap: 2px;
+}
+
+.task-failed-items li {
+ font-size: 12px;
+ color: var(--text-2);
+ overflow-wrap: anywhere;
+}
+
+.task-failed-name {
+ font-family: var(--font-mono);
+}
+
+.task-failed-reason {
+ color: var(--danger);
+ margin-left: 6px;
+}
+
+/* Compact mobile lists */
+
+.task-table,
+.users-table {
+ min-width: 0;
+}
+
+.task-mobile-list,
+.user-mobile-list {
+ display: none;
+ flex-direction: column;
+ gap: 10px;
+}
+
+.task-mobile-card,
+.user-mobile-card {
+ min-width: 0;
+ padding: 16px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 14px;
+}
+
+.task-mobile-head,
+.user-mobile-head {
+ display: flex;
+ min-width: 0;
+ align-items: flex-start;
+ justify-content: space-between;
+ gap: 12px;
+}
+
+.task-mobile-title {
+ min-width: 0;
+ flex: 1;
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+ line-height: 1.45;
+ overflow-wrap: anywhere;
+}
+
+.task-mobile-meta {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 4px 8px;
+ margin-top: 4px;
+ color: var(--text-3);
+ font-size: 12px;
+ overflow-wrap: anywhere;
+}
+
+.task-mobile-target {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: baseline;
+ gap: 4px 8px;
+ margin-top: 12px;
+ color: var(--text-2);
+ font-size: 14px;
+ overflow-wrap: anywhere;
+}
+
+.task-mobile-target .task-path {
+ font-size: 12px;
+}
+
+.task-mobile-progress {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ margin-top: 12px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.task-mobile-progress .progress {
+ min-width: 0;
+ flex: 1;
+}
+
+.task-mobile-foot {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+ padding-top: 12px;
+ margin-top: 12px;
+ border-top: 1px solid var(--line);
+}
+
+.task-mobile-time {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.user-chat {
+ min-width: 0;
+ color: var(--text);
+ font-size: 14px;
+ overflow-wrap: anywhere;
+}
+
+.user-mobile-head .actions {
+ flex: none;
+ flex-wrap: nowrap;
+}
+
+.user-mobile-details {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 10px 16px;
+ margin: 14px 0 0;
+}
+
+.user-mobile-details div {
+ min-width: 0;
+}
+
+.user-mobile-details dt {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.user-mobile-details dd {
+ margin: 2px 0 0;
+ color: var(--text-2);
+ font-size: 14px;
+ overflow-wrap: anywhere;
+}
+
+.user-mobile-actions {
+ display: flex;
+ gap: 8px;
+ padding-top: 12px;
+ margin-top: 14px;
+ border-top: 1px solid var(--line);
+}
+
+.helper-bots-mobile {
+ display: none;
+ flex-direction: column;
+ gap: 10px;
+}
+
+.helper-bot-card {
+ min-width: 0;
+ padding: 16px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 14px;
+}
+
+.helper-bot-head {
+ display: flex;
+ align-items: flex-start;
+ justify-content: space-between;
+ gap: 10px;
+}
+
+.helper-bot-identity {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 3px;
+}
+
+.helper-bot-identity strong {
+ color: var(--text);
+ font-size: 14px;
+ overflow-wrap: anywhere;
+}
+
+.helper-bot-identity .mono {
+ color: var(--text-3);
+ font-size: 12px;
+ overflow-wrap: anywhere;
+}
+
+.helper-bot-meta {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 8px;
+ margin-top: 10px;
+}
+
+.helper-bot-card .field-error {
+ margin-top: 8px;
+ overflow-wrap: anywhere;
+}
+
+.helper-bot-actions {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 8px;
+ padding-top: 12px;
+ margin-top: 12px;
+ border-top: 1px solid var(--line);
+}
+
+.helper-bot-actions .btn {
+ width: 100%;
+ min-width: 0;
+ justify-content: center;
+}
+
+.helper-bots-empty {
+ padding: 22px 16px;
+ text-align: center;
+}
+
+.task-mobile-list > .empty,
+.user-mobile-list > .empty {
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 14px;
+}
+
diff --git a/web/ui/src/styles/tokens.css b/web/ui/src/styles/tokens.css
new file mode 100644
index 00000000..8ffc79fa
--- /dev/null
+++ b/web/ui/src/styles/tokens.css
@@ -0,0 +1,260 @@
+:root {
+ color-scheme: light;
+ --font-sans:
+ -apple-system, BlinkMacSystemFont, 'Segoe UI Variable Text', 'Segoe UI', 'Noto Sans SC',
+ 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
+ --font-mono: 'SFMono-Regular', 'Cascadia Mono', 'Roboto Mono', Menlo, Consolas, monospace;
+
+ --paper: #f5f6f8;
+ --surface: #ffffff;
+ --surface-strong: #ffffff;
+ --surface-muted: #f1f2f5;
+ --ink: #121418;
+ --ink-raised: #1c1f24;
+ --ink-line: rgba(237, 240, 244, 0.1);
+ --ink-text: #edf0f4;
+ --ink-muted: #98a2ae;
+
+ --text: #17191f;
+ --text-2: #4f5560;
+ --text-3: #686f7a;
+ --line: #e4e7ec;
+ --line-strong: #d2d7df;
+
+ --accent: #3f5f9e;
+ --accent-strong: #2f4a7f;
+ --accent-soft: #eef1f7;
+ --success: #2c6a4f;
+ --success-soft: #e5f0ea;
+ --warning: #7d5c22;
+ --warning-soft: #f4eddf;
+ --danger: #9e3f4b;
+ --danger-soft: #f5e7e9;
+ --danger-solid: #9e3f4b;
+ --danger-solid-hover: #883540;
+ --info: #355f86;
+ --info-soft: #e8eff5;
+
+ --radius-sm: 6px;
+ --radius: 10px;
+ --radius-lg: 16px;
+ --shadow: 0 18px 48px rgba(17, 19, 23, 0.14);
+
+ /* Motion scale */
+ --duration-stagger: 40ms;
+ --duration-micro: 80ms;
+ --duration-quick: 150ms;
+ --duration-fast: 250ms;
+ --duration-medium: 350ms;
+ --duration-slow: 400ms;
+ --duration-very-slow: 500ms;
+ --ease: cubic-bezier(0.22, 1, 0.36, 1);
+ --ease-smooth-out: cubic-bezier(0.22, 1, 0.36, 1);
+ --ease-in-out: ease-in-out;
+ --ease-out: ease-out;
+ --ease-linear: linear;
+ --distance-base: 8px;
+ --distance-medium: 12px;
+ --scale-large: 0.96;
+ --scale-medium: 0.97;
+ --blur-small: 2px;
+ --blur-medium: 3px;
+}
+
+html[data-theme='dark'] {
+ color-scheme: dark;
+ --paper: #0e1014;
+ --surface: #15181e;
+ --surface-strong: #191d24;
+ --surface-muted: #20252d;
+ --ink: #0a0c0f;
+ --ink-raised: #14171b;
+ --ink-line: rgba(235, 240, 247, 0.1);
+ --ink-text: #edf0f4;
+ --ink-muted: #8c96a3;
+ --text: #e7ebf1;
+ --text-2: #b4bcc8;
+ --text-3: #858e9b;
+ --line: #2a2f38;
+ --line-strong: #3b424d;
+ --accent: #88a7e0;
+ --accent-strong: #b3c8ef;
+ --accent-soft: #202a3c;
+ --success: #7fc3a3;
+ --success-soft: #1b2d26;
+ --warning: #d8bd80;
+ --warning-soft: #332b1c;
+ --danger: #e09aa4;
+ --danger-soft: #352126;
+ --danger-solid: #a84f5c;
+ --danger-solid-hover: #bc6370;
+ --info: #8db7d8;
+ --info-soft: #1e2b35;
+ --shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
+}
+
+html[data-theme='dark'] .topbar {
+ background: rgba(14, 16, 20, 0.92);
+}
+
+html[data-theme='dark'] .nav a.is-active {
+ color: var(--text);
+ background: rgba(255, 255, 255, 0.1);
+}
+
+html[data-theme='dark'] .nav a.is-active svg {
+ color: var(--accent);
+}
+
+html[data-theme='dark'] .btn.primary {
+ color: #101318;
+ background: #e7ebf1;
+ border-color: #e7ebf1;
+}
+
+html[data-theme='dark'] .btn.primary:hover:not(:disabled) {
+ background: #ffffff;
+ border-color: #ffffff;
+}
+
+html[data-theme='dark'] .btn.primary.is-loading:disabled {
+ color: #101318;
+ background: #e7ebf1;
+ border-color: #e7ebf1;
+}
+
+html[data-theme='dark'] .btn.ghost:hover:not(:disabled) {
+ background: rgba(255, 255, 255, 0.06);
+}
+
+html[data-theme='dark'] .btn.danger:hover:not(:disabled) {
+ border-color: rgba(224, 154, 164, 0.4);
+}
+
+html[data-theme='dark'] .field input:hover,
+html[data-theme='dark'] .field textarea:hover,
+html[data-theme='dark'] .select-trigger:hover:not(:disabled) {
+ border-color: #4a5360;
+}
+
+html[data-theme='dark'] .field input::placeholder,
+html[data-theme='dark'] .field textarea::placeholder,
+html[data-theme='dark'] .select-value.placeholder {
+ color: #7f8997;
+}
+
+html[data-theme='dark'] tbody tr:hover {
+ background: rgba(255, 255, 255, 0.035);
+}
+
+html[data-theme='dark'] .settings-nav button:hover {
+ background: rgba(255, 255, 255, 0.05);
+}
+
+html[data-theme='dark'] .select-menu,
+html[data-theme='dark'] .toast {
+ box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
+}
+
+html[data-theme='dark'] .modal-overlay {
+ background: rgba(0, 0, 0, 0.68);
+}
+
+html[data-theme='dark'] .relay-board {
+ border: 1px solid var(--line);
+}
+
+html[data-theme='dark'] .login-card {
+ border-color: var(--line);
+}
+
+* {
+ box-sizing: border-box;
+}
+
+html,
+body,
+#app {
+ min-height: 100%;
+}
+
+html {
+ scroll-behavior: smooth;
+}
+
+body {
+ margin: 0;
+ background: var(--paper);
+ color: var(--text);
+ font-family: var(--font-sans);
+ font-size: 14px;
+ line-height: 1.6;
+ -webkit-font-smoothing: antialiased;
+ text-rendering: optimizeLegibility;
+}
+
+::selection {
+ background: var(--accent-soft);
+ color: var(--accent-strong);
+}
+
+a {
+ color: inherit;
+ text-decoration: none;
+}
+
+button,
+input,
+select,
+textarea {
+ font: inherit;
+ color: inherit;
+}
+
+button {
+ cursor: pointer;
+}
+
+h1,
+h2,
+h3,
+h4 {
+ margin: 0;
+ font-weight: 500;
+}
+
+code {
+ font-family: var(--font-mono);
+ font-size: 0.92em;
+ font-variant-numeric: tabular-nums;
+}
+
+.mono {
+ font-family: var(--font-mono);
+ font-size: inherit;
+ font-variant-numeric: tabular-nums;
+}
+
+:focus-visible {
+ outline: 2px solid var(--accent);
+ outline-offset: 2px;
+}
+
+.skip-link {
+ position: fixed;
+ top: 8px;
+ left: 8px;
+ z-index: 100;
+ padding: 10px 14px;
+ color: var(--ink-text);
+ background: var(--ink);
+ border-radius: 8px;
+ font-size: 14px;
+ transform: translateY(-160%);
+ transition: transform 160ms var(--ease);
+}
+
+.skip-link:focus {
+ transform: translateY(0);
+}
+
diff --git a/web/ui/src/styles/utilities.css b/web/ui/src/styles/utilities.css
new file mode 100644
index 00000000..2cc1507d
--- /dev/null
+++ b/web/ui/src/styles/utilities.css
@@ -0,0 +1,18 @@
+/* Utilities */
+
+@supports (corner-shape: squircle) {
+ .panel,
+ .storage-card,
+ .connection-card,
+ .process-panel,
+ .modal,
+ .toast,
+ .select-menu,
+ .select-trigger,
+ .btn,
+ .field input,
+ .field textarea {
+ corner-shape: squircle;
+ }
+}
+
diff --git a/web/ui/src/styles/views/dashboard.css b/web/ui/src/styles/views/dashboard.css
new file mode 100644
index 00000000..048c838d
--- /dev/null
+++ b/web/ui/src/styles/views/dashboard.css
@@ -0,0 +1,329 @@
+/* Dashboard */
+
+.relay-board {
+ display: flex;
+ flex-direction: column;
+ gap: 20px;
+ padding: 24px;
+ color: var(--ink-text);
+ background: var(--ink);
+ border-radius: 16px;
+}
+
+.relay-status {
+ display: inline-flex;
+ align-items: center;
+ gap: 8px;
+ color: #c9ccd2;
+ font-size: 14px;
+}
+
+.relay-flow {
+ display: flex;
+ min-width: 0;
+ align-items: stretch;
+}
+
+.relay-node {
+ display: flex;
+ min-width: 0;
+ flex: 1;
+ flex-direction: column;
+ padding: 16px 18px;
+ background: rgba(255, 255, 255, 0.04);
+ border: 1px solid rgba(234, 238, 243, 0.14);
+ border-radius: 10px;
+}
+
+.relay-node-label {
+ display: flex;
+ align-items: center;
+ gap: 7px;
+ color: #b8bdc5;
+ font-size: 14px;
+}
+
+.relay-node strong {
+ margin-top: 8px;
+ color: #f2f3f5;
+ font-size: 24px;
+ font-weight: 500;
+ letter-spacing: -0.02em;
+ line-height: 1.2;
+}
+
+.relay-node small {
+ margin-top: 5px;
+ overflow: hidden;
+ color: #b6bbc3;
+ font-size: 14px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.relay-link {
+ display: grid;
+ width: 42px;
+ flex: none;
+ place-items: center;
+ color: #878d97;
+}
+
+.relay-link.active {
+ color: var(--accent);
+}
+
+.relay-metrics {
+ display: grid;
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+ gap: 24px;
+ padding-top: 20px;
+ border-top: 1px solid rgba(234, 238, 243, 0.13);
+}
+
+.relay-metric {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 4px;
+}
+
+.relay-metric span {
+ color: #b8bdc5;
+ font-size: 14px;
+}
+
+.relay-metric strong {
+ color: #eef0f4;
+ font-size: 24px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.04em;
+ line-height: 1.2;
+}
+
+.dashboard-split {
+ display: grid;
+ grid-template-columns: minmax(0, 1.55fr) minmax(280px, 0.85fr);
+ gap: 20px;
+ align-items: stretch;
+}
+
+.dashboard-split > * {
+ min-width: 0;
+}
+
+.activity-figure {
+ display: flex;
+ align-items: baseline;
+ gap: 8px;
+ margin-top: 12px;
+}
+
+.activity-figure strong {
+ color: var(--text);
+ font-size: 28px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.05em;
+ line-height: 1;
+}
+
+.activity-chart {
+ position: relative;
+ display: flex;
+ height: 184px;
+ align-items: flex-end;
+ gap: 8px;
+ padding: 12px 4px 26px;
+ margin-top: 14px;
+ background-image: linear-gradient(to top, var(--line) 1px, transparent 1px);
+ background-size: 100% 25%;
+}
+
+.chart-col {
+ position: relative;
+ display: flex;
+ min-width: 0;
+ flex: 1;
+ flex-direction: column;
+ align-items: center;
+ justify-content: flex-end;
+ height: 100%;
+}
+
+.chart-bar {
+ width: min(34px, 68%);
+ min-height: 2px;
+ background: var(--line-strong);
+ border-radius: 4px 4px 0 0;
+ transform-origin: bottom;
+ animation: chart-rise var(--duration-very-slow) var(--ease-smooth-out) backwards;
+ animation-delay: calc(var(--bar-index, 0) * var(--duration-stagger));
+ transition: background-color 160ms var(--ease);
+}
+
+@keyframes chart-rise {
+ from {
+ transform: scaleY(0);
+ }
+ to {
+ transform: scaleY(1);
+ }
+}
+
+.chart-col.current .chart-bar {
+ background: var(--accent);
+}
+
+.chart-col:hover .chart-bar {
+ background: var(--text-3);
+}
+
+.chart-col.current:hover .chart-bar {
+ background: var(--accent-strong);
+}
+
+.chart-count {
+ margin-bottom: 6px;
+ color: var(--text-3);
+ font-size: 12px;
+ font-variant-numeric: tabular-nums;
+}
+
+.chart-label {
+ position: absolute;
+ bottom: -24px;
+ color: var(--text-3);
+ font-size: 12px;
+ white-space: nowrap;
+}
+
+.chart-col.current .chart-label {
+ color: var(--accent-strong);
+}
+
+.chart-empty {
+ display: grid;
+ height: 184px;
+ place-items: center;
+ margin-top: 14px;
+ color: var(--text-3);
+ font-size: 14px;
+ text-align: center;
+}
+
+.storage-mini-list {
+ display: flex;
+ flex-direction: column;
+}
+
+.storage-mini {
+ display: flex;
+ align-items: center;
+ gap: 11px;
+ padding: 12px 0;
+ border-bottom: 1px solid var(--line);
+}
+
+.storage-mini:last-child {
+ border-bottom: none;
+}
+
+.storage-mini-icon {
+ display: grid;
+ place-items: center;
+ width: 32px;
+ height: 32px;
+ flex: none;
+ color: var(--text-2);
+ background: var(--surface-muted);
+ border-radius: 8px;
+}
+
+.storage-mini-main {
+ min-width: 0;
+ flex: 1;
+}
+
+.storage-mini-main strong {
+ display: block;
+ overflow: hidden;
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.storage-mini-main span {
+ display: block;
+ overflow: hidden;
+ margin-top: 1px;
+ color: var(--text-3);
+ font-size: 12px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.recent-list {
+ display: flex;
+ flex-direction: column;
+}
+
+.recent-task {
+ display: flex;
+ align-items: center;
+ gap: 16px;
+ padding: 13px 0;
+ border-bottom: 1px solid var(--line);
+}
+
+.recent-task:last-child {
+ border-bottom: none;
+}
+
+.recent-task-main {
+ min-width: 0;
+ flex: 1;
+}
+
+.recent-task-title {
+ overflow: hidden;
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.recent-task-meta {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px;
+ margin-top: 3px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.recent-task-side {
+ display: flex;
+ flex: none;
+ align-items: center;
+ gap: 12px;
+}
+
+.recent-task-progress {
+ width: 90px;
+}
+
+.link-quiet {
+ color: var(--accent-strong);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.link-quiet:hover {
+ color: var(--accent);
+}
+
diff --git a/web/ui/src/styles/views/guide.css b/web/ui/src/styles/views/guide.css
new file mode 100644
index 00000000..c2b6354a
--- /dev/null
+++ b/web/ui/src/styles/views/guide.css
@@ -0,0 +1,426 @@
+/* Guide */
+
+.guide-hero {
+ display: grid;
+ grid-template-columns: minmax(0, 1fr) 210px;
+ gap: 28px;
+ align-items: center;
+ padding: 28px;
+ background: linear-gradient(135deg, var(--accent-soft) 0%, var(--surface) 62%);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.guide-hero-copy {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ align-items: flex-start;
+ gap: 12px;
+}
+
+.guide-hero-copy .badge {
+ margin-bottom: 2px;
+}
+
+.guide-hero h2 {
+ color: var(--text);
+ font-size: 26px;
+ font-weight: 500;
+ letter-spacing: -0.03em;
+ line-height: 1.2;
+}
+
+.guide-hero-copy > p:not(.guide-password-note) {
+ max-width: 64ch;
+ color: var(--text-2);
+ font-size: 14px;
+ line-height: 1.65;
+}
+
+.guide-hero-actions {
+ margin-top: 2px;
+}
+
+.guide-password-note {
+ display: flex;
+ align-items: flex-start;
+ gap: 7px;
+ max-width: 70ch;
+ margin-top: 4px;
+ color: var(--text-3);
+ font-size: 12px;
+ line-height: 1.55;
+}
+
+.guide-password-note svg {
+ margin-top: 1px;
+ flex: none;
+}
+
+.guide-progress {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 8px;
+ padding-left: 24px;
+ border-left: 1px solid var(--line);
+}
+
+.guide-progress strong {
+ color: var(--text);
+ font-size: 42px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.06em;
+ line-height: 1;
+}
+
+.guide-progress strong span {
+ color: var(--text-3);
+ font-size: 20px;
+ letter-spacing: -0.03em;
+}
+
+.guide-progress small {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.guide-meter {
+ width: 100%;
+ height: 6px;
+ overflow: hidden;
+ background: var(--line);
+ border-radius: 999px;
+}
+
+.guide-meter span {
+ display: block;
+ height: 100%;
+ background: var(--accent);
+ border-radius: inherit;
+ transition: width var(--duration-fast) var(--ease-smooth-out);
+}
+
+.guide-section .panel-head {
+ align-items: center;
+}
+
+.guide-count {
+ color: var(--text-3);
+ font-size: 12px;
+ font-variant-numeric: tabular-nums;
+}
+
+.setup-steps {
+ padding: 0;
+ margin: 0;
+ list-style: none;
+}
+
+.setup-step {
+ display: grid;
+ grid-template-columns: 30px minmax(0, 1fr) auto;
+ gap: 14px;
+ align-items: start;
+ padding: 18px 0;
+ border-top: 1px solid var(--line);
+}
+
+.setup-step:first-child {
+ padding-top: 2px;
+ border-top: 0;
+}
+
+.setup-step:last-child {
+ padding-bottom: 0;
+}
+
+.setup-step-marker {
+ display: grid;
+ width: 28px;
+ height: 28px;
+ place-items: center;
+ color: var(--text-2);
+ background: var(--surface-muted);
+ border: 1px solid var(--line);
+ border-radius: 50%;
+ font-size: 12px;
+ font-variant-numeric: tabular-nums;
+}
+
+.setup-step.done .setup-step-marker {
+ color: var(--success);
+ background: var(--success-soft);
+ border-color: transparent;
+}
+
+.setup-step-copy {
+ min-width: 0;
+}
+
+.setup-step-heading {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 8px;
+ margin-bottom: 5px;
+}
+
+.setup-step-heading h3 {
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.setup-step-copy p {
+ color: var(--text-2);
+ font-size: 13px;
+ line-height: 1.65;
+}
+
+.setup-step-copy small {
+ display: block;
+ margin-top: 4px;
+ color: var(--text-3);
+ font-size: 12px;
+ line-height: 1.55;
+}
+
+.guide-tag,
+.guide-state {
+ display: inline-flex;
+ align-items: center;
+ height: 20px;
+ padding: 0 6px;
+ border-radius: 4px;
+ font-size: 11px;
+ font-weight: 500;
+ line-height: 1;
+}
+
+.guide-tag {
+ color: var(--text-3);
+ background: var(--surface-muted);
+}
+
+.guide-state {
+ color: var(--warning);
+ background: var(--warning-soft);
+}
+
+.setup-step.done .guide-state {
+ color: var(--success);
+ background: var(--success-soft);
+}
+
+.guide-copy {
+ color: var(--text-2);
+ font-size: 14px;
+ line-height: 1.65;
+}
+
+.guide-numbered {
+ padding-left: 20px;
+ margin-top: 14px;
+ color: var(--text-2);
+ font-size: 13px;
+ line-height: 1.7;
+}
+
+.guide-numbered li + li {
+ margin-top: 8px;
+}
+
+.guide-note {
+ padding: 10px 12px;
+ margin-top: 18px;
+ color: var(--text-3);
+ background: var(--surface-muted);
+ border-radius: 8px;
+ font-size: 12px;
+ line-height: 1.6;
+}
+
+.guide-imported {
+ display: flex;
+ gap: 14px;
+ align-items: flex-start;
+ padding: 18px;
+ background: var(--success-soft);
+ border-radius: 12px;
+}
+
+.guide-imported-icon {
+ display: grid;
+ width: 30px;
+ height: 30px;
+ flex: none;
+ place-items: center;
+ color: var(--success);
+ background: var(--surface);
+ border-radius: 50%;
+}
+
+.guide-imported strong {
+ display: block;
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.guide-imported p {
+ margin-top: 4px;
+ color: var(--text-2);
+ font-size: 13px;
+ line-height: 1.65;
+}
+
+.guide-imported small {
+ display: block;
+ margin-top: 8px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.guide-usage-grid {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 14px;
+}
+
+.guide-usage-item {
+ display: flex;
+ gap: 12px;
+ align-items: flex-start;
+ padding: 16px;
+ background: var(--surface-muted);
+ border: 1px solid var(--line);
+ border-radius: 12px;
+}
+
+.guide-usage-icon {
+ display: grid;
+ width: 32px;
+ height: 32px;
+ flex: none;
+ place-items: center;
+ color: var(--text-2);
+ background: var(--surface);
+ border-radius: 8px;
+}
+
+.guide-usage-item h3 {
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.guide-usage-item p {
+ margin-top: 4px;
+ color: var(--text-2);
+ font-size: 13px;
+ line-height: 1.65;
+}
+
+
+.guide-commands {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px;
+ margin-top: 8px;
+}
+
+.guide-commands code {
+ padding: 3px 6px;
+ color: var(--text);
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: 5px;
+ font-size: 12px;
+}
+
+@media (max-width: 760px) {
+ .path-picker-overlay {
+ padding: 12px;
+ }
+
+ .path-picker-modal {
+ height: calc(100vh - 24px);
+ max-height: calc(100vh - 24px);
+ padding: 18px;
+ }
+
+ .path-picker-toolbar {
+ align-items: stretch;
+ flex-direction: column;
+ }
+
+ .path-picker-footer {
+ align-items: stretch;
+ flex-direction: column;
+ }
+
+ .path-picker-footer .actions {
+ justify-content: flex-end;
+ }
+
+ .setup-callout {
+ grid-template-columns: 1fr;
+ gap: 18px;
+ }
+
+ .setup-callout-side {
+ align-items: flex-start;
+ }
+
+ .setup-callout-side small {
+ text-align: left;
+ }
+
+ .setup-callout-side .actions {
+ justify-content: flex-start;
+ }
+
+ .guide-hero {
+ grid-template-columns: 1fr;
+ gap: 22px;
+ padding: 22px;
+ }
+
+ .guide-progress {
+ padding: 18px 0 0;
+ border-top: 1px solid var(--line);
+ border-left: 0;
+ }
+
+ .guide-usage-grid {
+ grid-template-columns: 1fr;
+ }
+
+
+ .setup-step {
+ grid-template-columns: 28px minmax(0, 1fr);
+ }
+
+ .setup-step > .btn {
+ grid-column: 2;
+ justify-self: start;
+ }
+}
+
+@media (max-width: 520px) {
+ .guide-hero {
+ padding: 20px 18px;
+ }
+
+ .guide-hero h2 {
+ font-size: 24px;
+ }
+
+ .guide-imported {
+ padding: 14px;
+ }
+}
+
diff --git a/web/ui/src/styles/views/login.css b/web/ui/src/styles/views/login.css
new file mode 100644
index 00000000..6ad30794
--- /dev/null
+++ b/web/ui/src/styles/views/login.css
@@ -0,0 +1,189 @@
+/* Login */
+
+.login-page {
+ position: relative;
+ display: flex;
+ min-height: 100dvh;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ gap: 18px;
+ overflow: hidden;
+ padding: 32px;
+ color: var(--ink-text);
+ background: var(--ink);
+}
+
+.login-preferences {
+ position: absolute;
+ top: 24px;
+ right: 24px;
+ z-index: 2;
+ display: flex;
+ gap: 8px;
+}
+
+.login-trace {
+ position: absolute;
+ inset: 0;
+ width: 100%;
+ height: 100%;
+ color: rgba(237, 240, 244, 0.09);
+ pointer-events: none;
+}
+
+.login-trace path {
+ stroke-dasharray: 1;
+ stroke-dashoffset: 1;
+ animation: login-trace-draw 900ms var(--ease-smooth-out) 80ms forwards;
+}
+
+.login-trace circle {
+ opacity: 0;
+ animation: login-dot-in 300ms var(--ease-out) 620ms forwards;
+}
+
+.login-trace circle:nth-of-type(2) {
+ animation-delay: 760ms;
+}
+
+.login-trace circle:nth-of-type(3) {
+ animation-delay: 900ms;
+}
+
+.login-card {
+ position: relative;
+ z-index: 1;
+ width: min(408px, 100%);
+ padding: 32px;
+ color: var(--text);
+ background: var(--surface);
+ border: 1px solid rgba(255, 255, 255, 0.08);
+ border-radius: 18px;
+ box-shadow: 0 30px 80px rgba(0, 0, 0, 0.34);
+ animation: login-card-in var(--duration-slow) var(--ease-smooth-out) backwards;
+}
+
+.login-brand,
+.login-heading,
+.login-form,
+.login-help {
+ animation: login-item-in 360ms var(--ease-smooth-out) backwards;
+}
+
+.login-brand {
+ animation-delay: 60ms;
+}
+
+.login-heading {
+ animation-delay: 100ms;
+}
+
+.login-form {
+ animation-delay: 150ms;
+}
+
+.login-help {
+ animation-delay: 220ms;
+}
+
+@keyframes login-card-in {
+ from {
+ opacity: 0;
+ transform: translateY(var(--distance-medium)) scale(0.99);
+ }
+ to {
+ opacity: 1;
+ transform: none;
+ }
+}
+
+@keyframes login-item-in {
+ from {
+ opacity: 0;
+ transform: translateY(var(--distance-base));
+ filter: blur(var(--blur-small));
+ }
+ to {
+ opacity: 1;
+ transform: none;
+ filter: blur(0);
+ }
+}
+
+@keyframes login-trace-draw {
+ to {
+ stroke-dashoffset: 0;
+ }
+}
+
+@keyframes login-dot-in {
+ to {
+ opacity: 1;
+ }
+}
+
+.login-brand {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+ color: var(--text);
+ font-size: 16px;
+ font-weight: 600;
+}
+
+.login-brand-mark {
+ color: var(--ink-text);
+ background: var(--ink);
+ border-color: var(--ink);
+}
+
+.login-heading {
+ margin-top: 36px;
+}
+
+.login-heading h1 {
+ color: var(--text);
+ font-size: 28px;
+ font-weight: 500;
+ letter-spacing: -0.025em;
+ line-height: 1.15;
+}
+
+.login-form {
+ display: flex;
+ flex-direction: column;
+ gap: 18px;
+ margin-top: 28px;
+}
+
+.login-form .btn {
+ width: 100%;
+ height: 42px;
+}
+
+.login-form .field input[aria-invalid='true'] {
+ border-color: var(--danger);
+ animation: field-shake 280ms linear;
+}
+
+.login-form .field input[aria-invalid='true']:focus {
+ box-shadow: 0 0 0 3px var(--danger-soft);
+}
+
+.login-help {
+ padding-top: 18px;
+ margin: 24px 0 0;
+ color: var(--text-3);
+ border-top: 1px solid var(--line);
+ font-size: 12px;
+ line-height: 1.65;
+}
+
+.login-help code {
+ padding: 1px 4px;
+ color: var(--text-2);
+ background: var(--surface-muted);
+ border-radius: 4px;
+}
+
diff --git a/web/ui/src/styles/views/metadata.css b/web/ui/src/styles/views/metadata.css
new file mode 100644
index 00000000..f8d0042b
--- /dev/null
+++ b/web/ui/src/styles/views/metadata.css
@@ -0,0 +1,224 @@
+/* Task metadata dialog */
+
+.metadata-modal {
+ max-width: 900px;
+}
+
+.metadata-heading {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 4px;
+}
+
+.metadata-subtitle {
+ overflow-wrap: anywhere;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.metadata-body {
+ display: flex;
+ min-height: 0;
+ flex: 1 1 auto;
+ flex-direction: column;
+ gap: 14px;
+ overflow-y: auto;
+ overscroll-behavior: contain;
+ padding-right: 6px;
+ margin-right: -6px;
+ scrollbar-gutter: stable;
+ scrollbar-width: thin;
+ scrollbar-color: var(--line-strong) transparent;
+}
+
+.metadata-body::-webkit-scrollbar {
+ width: 7px;
+}
+
+.metadata-body::-webkit-scrollbar-thumb {
+ background: var(--line-strong);
+ border-radius: 999px;
+}
+
+.metadata-state {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ gap: 10px;
+ padding: 36px 16px;
+ color: var(--text-3);
+ font-size: 13px;
+}
+
+.metadata-spinner {
+ width: 15px;
+ height: 15px;
+ flex: none;
+ border: 1.5px solid var(--line-strong);
+ border-top-color: var(--accent);
+ border-radius: 50%;
+ animation: metadata-spin 700ms linear infinite;
+}
+
+@keyframes metadata-spin {
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+.metadata-card {
+ display: flex;
+ flex-direction: column;
+ gap: 14px;
+ padding: 16px;
+ background: var(--surface-muted);
+ border: 1px solid var(--line);
+ border-radius: var(--radius);
+}
+
+.metadata-card-head {
+ display: flex;
+ align-items: flex-start;
+ justify-content: space-between;
+ gap: 12px;
+}
+
+.metadata-file-title {
+ overflow-wrap: anywhere;
+ color: var(--text);
+ font-size: 15px;
+ font-weight: 500;
+ line-height: 1.4;
+}
+
+.metadata-file-path {
+ margin-top: 4px;
+ overflow-wrap: anywhere;
+ color: var(--text-3);
+ font-size: 12px;
+ line-height: 1.45;
+}
+
+.metadata-item-label {
+ margin-top: 4px;
+ color: var(--text-3);
+ font-size: 11px;
+}
+
+.metadata-section {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.metadata-section-title {
+ color: var(--text-2);
+ font-size: 12px;
+ font-weight: 600;
+ letter-spacing: 0.04em;
+ text-transform: uppercase;
+}
+
+.metadata-grid {
+ display: grid;
+ grid-template-columns: minmax(100px, 0.25fr) minmax(0, 1fr);
+ gap: 7px 14px;
+ margin: 0;
+}
+
+.metadata-grid dt {
+ color: var(--text-3);
+ font-size: 12px;
+ line-height: 1.5;
+}
+
+.metadata-grid dd {
+ min-width: 0;
+ margin: 0;
+ overflow-wrap: anywhere;
+ color: var(--text);
+ font-size: 13px;
+ line-height: 1.5;
+}
+
+.metadata-grid dd a {
+ color: var(--accent-strong);
+ text-decoration: underline;
+ text-underline-offset: 2px;
+}
+
+.metadata-field-label {
+ display: block;
+ margin-bottom: 5px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.metadata-text-block,
+.metadata-tags-block {
+ display: flex;
+ flex-direction: column;
+}
+
+.metadata-text {
+ max-height: 220px;
+ overflow: auto;
+ padding: 10px 12px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-sm);
+ color: var(--text);
+ font-size: 12.5px;
+ line-height: 1.6;
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+}
+
+.metadata-tags {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 6px;
+}
+
+.metadata-tag {
+ padding: 3px 7px;
+ color: var(--accent-strong);
+ background: var(--accent-soft);
+ border-radius: var(--radius-sm);
+ font-size: 11px;
+ line-height: 1.4;
+}
+
+.metadata-raw {
+ max-height: 260px;
+ overflow: auto;
+ margin: 0;
+ padding: 10px 12px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-sm);
+ font-size: 12px;
+ line-height: 1.5;
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+}
+
+@media (max-width: 760px) {
+ .metadata-modal {
+ padding: 18px;
+ }
+
+ .metadata-grid {
+ grid-template-columns: 1fr;
+ gap: 2px;
+ }
+
+ .metadata-grid dt:not(:first-child) {
+ margin-top: 7px;
+ }
+
+ .metadata-card {
+ padding: 13px;
+ }
+}
diff --git a/web/ui/src/styles/views/path-picker.css b/web/ui/src/styles/views/path-picker.css
new file mode 100644
index 00000000..fcd68407
--- /dev/null
+++ b/web/ui/src/styles/views/path-picker.css
@@ -0,0 +1,230 @@
+/* Server path picker */
+
+.path-picker-overlay {
+ z-index: 85;
+ overscroll-behavior: contain;
+}
+
+.path-picker-modal {
+ max-width: 720px;
+ height: min(640px, 86vh);
+ gap: 14px;
+ overflow: hidden;
+}
+
+.path-picker-toolbar {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+}
+
+.path-picker-location {
+ display: flex;
+ min-width: 0;
+ flex: 1;
+ align-items: center;
+ gap: 8px;
+}
+
+.path-picker-location input {
+ width: 100%;
+ min-width: 0;
+ height: 32px;
+ padding: 0 10px;
+ color: var(--text);
+ background: var(--surface-strong);
+ border: 1px solid var(--line-strong);
+ border-radius: 7px;
+ font-family: var(--font-mono);
+ font-size: 12px;
+ outline: none;
+}
+
+.path-picker-location input:focus {
+ border-color: var(--line-strong);
+ box-shadow: 0 0 0 2px var(--line);
+}
+
+.path-picker-options {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+ flex-wrap: wrap;
+}
+
+.path-picker-shortcuts {
+ display: flex;
+ gap: 8px;
+}
+
+.path-picker-hidden-toggle {
+ min-height: 0;
+ padding-top: 0;
+ margin-left: auto;
+ gap: 6px;
+}
+
+.path-picker-hidden-toggle span {
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.path-picker-list {
+ display: flex;
+ min-height: 0;
+ flex: 1;
+ flex-direction: column;
+ gap: 2px;
+ overflow-y: auto;
+ padding: 4px;
+ background: var(--surface-muted);
+ border: 1px solid var(--line);
+ border-radius: 10px;
+ scrollbar-color: var(--line-strong) transparent;
+ scrollbar-width: thin;
+}
+
+.path-picker-list::-webkit-scrollbar {
+ width: 8px;
+ height: 8px;
+}
+
+.path-picker-list::-webkit-scrollbar-track,
+.path-picker-list::-webkit-scrollbar-corner {
+ background: transparent;
+}
+
+.path-picker-list::-webkit-scrollbar-button {
+ display: none;
+}
+
+.path-picker-list::-webkit-scrollbar-thumb {
+ background: transparent;
+ border: 2px solid transparent;
+ border-radius: 999px;
+ background-clip: padding-box;
+}
+
+.path-picker-list:hover::-webkit-scrollbar-thumb {
+ background: color-mix(in srgb, var(--text-3) 38%, transparent);
+}
+
+.path-picker-list::-webkit-scrollbar-thumb:hover {
+ background: color-mix(in srgb, var(--text-3) 58%, transparent);
+}
+
+.path-picker-entry {
+ display: grid;
+ grid-template-columns: 18px minmax(0, 1fr) auto;
+ align-items: center;
+ gap: 10px;
+ width: 100%;
+ min-height: 36px;
+ padding: 7px 10px;
+ color: var(--text);
+ background: transparent;
+ border: 0;
+ border-radius: 6px;
+ font-size: 13px;
+ text-align: left;
+}
+
+.path-picker-entry:hover {
+ background: var(--surface);
+}
+
+.path-picker-entry.selected {
+ color: var(--accent-strong);
+ background: var(--accent-soft);
+}
+
+.path-picker-entry svg {
+ color: var(--text-3);
+}
+
+.path-picker-entry.selected svg {
+ color: var(--accent);
+}
+
+.path-picker-entry-name {
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.path-picker-entry-size {
+ color: var(--text-3);
+ font-size: 11px;
+}
+
+.path-picker-entry-arrow {
+ color: var(--text-3);
+}
+
+.path-picker-state {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ gap: 10px;
+ padding: 32px 16px;
+ color: var(--text-3);
+ font-size: 13px;
+ text-align: center;
+}
+
+.path-picker-error {
+ color: var(--danger);
+}
+
+.path-picker-truncated {
+ color: var(--text-3);
+ font-size: 11px;
+}
+
+.path-picker-footer {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 16px;
+ padding-top: 14px;
+ border-top: 1px solid var(--line);
+}
+
+.path-picker-selected {
+ min-width: 0;
+ overflow: hidden;
+ color: var(--text-3);
+ font-family: var(--font-mono);
+ font-size: 11px;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.path-input-row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+}
+
+.path-input-row > input {
+ width: auto;
+ min-width: 0;
+ flex: 1;
+}
+
+.path-input-row > .btn {
+ flex: none;
+ height: 40px;
+}
+
+.path-input-column {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.path-input-column > .btn {
+ align-self: flex-start;
+}
+
diff --git a/web/ui/src/styles/views/settings.css b/web/ui/src/styles/views/settings.css
new file mode 100644
index 00000000..c06c2c6a
--- /dev/null
+++ b/web/ui/src/styles/views/settings.css
@@ -0,0 +1,124 @@
+/* Settings */
+
+.settings-layout {
+ display: grid;
+ grid-template-columns: 196px minmax(0, 1fr);
+ gap: 28px;
+ align-items: start;
+}
+
+.settings-mobile-nav {
+ display: none;
+}
+
+.settings-nav {
+ position: sticky;
+ top: 24px;
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+ padding-top: 2px;
+}
+
+.settings-nav button {
+ display: flex;
+ align-items: center;
+ height: 34px;
+ padding: 0 10px;
+ color: var(--text-2);
+ background: transparent;
+ border: 1px solid transparent;
+ border-radius: 7px;
+ font-size: 14px;
+ text-align: left;
+ transition:
+ color 140ms var(--ease),
+ background-color 140ms var(--ease),
+ border-color 140ms var(--ease);
+}
+
+.settings-nav button:hover {
+ color: var(--text);
+ background: rgba(17, 19, 23, 0.045);
+}
+
+.settings-nav button.active {
+ color: var(--text);
+ background: var(--surface);
+ border-color: var(--line);
+ font-weight: 500;
+}
+
+.settings-sections {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 18px;
+}
+
+.settings-group {
+ scroll-margin-top: 24px;
+}
+
+.settings-group-head {
+ padding-bottom: 16px;
+ margin-bottom: 20px;
+ border-bottom: 1px solid var(--line);
+}
+
+.settings-group-head h2 {
+ color: var(--text);
+ font-size: 16px;
+ font-weight: 500;
+}
+
+.settings-fields {
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+}
+
+.settings-fields > .field:only-child {
+ grid-column: 1 / -1;
+}
+
+.settings-toggles {
+ display: flex;
+ flex-wrap: wrap;
+ align-items: center;
+ gap: 10px 24px;
+ padding-top: 18px;
+}
+
+.settings-toggle {
+ min-height: 24px;
+ padding-top: 0;
+ gap: 8px;
+}
+
+.settings-toggle-copy {
+ display: flex;
+ flex-direction: column;
+ gap: 2px;
+}
+
+.settings-toggle-copy small {
+ color: var(--text-3);
+ font-size: 12px;
+ font-weight: 400;
+ line-height: 1.4;
+}
+
+html[data-theme='dark'] .btn.primary:disabled:not(.is-loading) {
+ color: var(--text-3);
+ background: var(--surface-muted);
+ border-color: var(--line);
+}
+
+.settings-actions {
+ display: flex;
+ justify-content: flex-end;
+ gap: 8px;
+ padding-top: 18px;
+ margin-top: 28px;
+ border-top: 1px solid var(--line);
+}
+
diff --git a/web/ui/src/styles/views/storages.css b/web/ui/src/styles/views/storages.css
new file mode 100644
index 00000000..62b748ff
--- /dev/null
+++ b/web/ui/src/styles/views/storages.css
@@ -0,0 +1,101 @@
+/* Storages */
+
+.storage-grid {
+ display: grid;
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
+ gap: 16px;
+}
+
+.storage-card {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 18px;
+ padding: 22px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.storage-card-head {
+ display: flex;
+ align-items: flex-start;
+ justify-content: space-between;
+ gap: 12px;
+}
+
+.storage-identity {
+ display: flex;
+ min-width: 0;
+ align-items: center;
+ gap: 11px;
+}
+
+.storage-icon {
+ display: grid;
+ place-items: center;
+ width: 38px;
+ height: 38px;
+ flex: none;
+ color: var(--text-2);
+ background: var(--surface-muted);
+ border-radius: 9px;
+}
+
+.storage-card h2 {
+ overflow: hidden;
+ color: var(--text);
+ font-size: 16px;
+ font-weight: 500;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.storage-kind {
+ margin-top: 2px;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.storage-detail {
+ min-height: 38px;
+ color: var(--text-2);
+ font-family: var(--font-mono);
+ font-size: 12px;
+ line-height: 1.6;
+ overflow-wrap: anywhere;
+}
+
+.storage-card-error {
+ margin: -6px 0 0;
+ color: var(--danger);
+ font-size: 12px;
+}
+
+.storage-card-foot {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 10px;
+ padding-top: 16px;
+ margin-top: auto;
+ border-top: 1px solid var(--line);
+}
+
+.storage-empty {
+ display: flex;
+ grid-column: 1 / -1;
+ min-height: 220px;
+ align-items: center;
+ justify-content: center;
+ flex-direction: column;
+ gap: 10px;
+ padding: 32px;
+ color: var(--text-3);
+ background: var(--surface);
+ border: 1px dashed var(--line-strong);
+ border-radius: var(--radius-lg);
+ font-size: 14px;
+ text-align: center;
+}
+
diff --git a/web/ui/src/styles/views/system.css b/web/ui/src/styles/views/system.css
new file mode 100644
index 00000000..4d13d7e2
--- /dev/null
+++ b/web/ui/src/styles/views/system.css
@@ -0,0 +1,40 @@
+/* System */
+
+.process-panel {
+ display: grid;
+ grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr);
+ gap: 28px;
+ padding: 26px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.process-version {
+ margin-top: 12px;
+ color: var(--text);
+ font-size: 28px;
+ font-variant-numeric: tabular-nums;
+ font-weight: 500;
+ letter-spacing: -0.05em;
+ line-height: 1.1;
+}
+
+.process-meta {
+ margin-top: 6px;
+ color: var(--text-3);
+ font-family: var(--font-mono);
+ font-size: 12px;
+ overflow-wrap: anywhere;
+}
+
+.process-metrics {
+ display: grid;
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+ gap: 16px;
+}
+
+.process-metrics .stat-value {
+ font-size: 24px;
+}
+
diff --git a/web/ui/src/styles/views/telegram.css b/web/ui/src/styles/views/telegram.css
new file mode 100644
index 00000000..cba278d5
--- /dev/null
+++ b/web/ui/src/styles/views/telegram.css
@@ -0,0 +1,200 @@
+/* Telegram */
+
+.connection-grid {
+ display: grid;
+ grid-template-columns: repeat(2, minmax(0, 1fr));
+ gap: 16px;
+}
+
+.connection-card {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 16px;
+ padding: 22px;
+ background: var(--surface);
+ border: 1px solid var(--line);
+ border-radius: var(--radius-lg);
+}
+
+.connection-head {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 12px;
+}
+
+.connection-title {
+ display: flex;
+ align-items: center;
+ gap: 9px;
+ color: var(--text);
+ font-size: 16px;
+ font-weight: 500;
+}
+
+.connection-title svg {
+ color: var(--text-3);
+}
+
+.connection-identity {
+ min-width: 0;
+ color: var(--text);
+ font-family: var(--font-mono);
+ font-size: 16px;
+ line-height: 1.35;
+ overflow-wrap: anywhere;
+}
+
+.connection-actions {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 8px;
+ padding-top: 2px;
+ margin-top: auto;
+}
+
+.telegram-panel {
+ scroll-margin-top: 80px;
+}
+
+.telegram-form-grid {
+ grid-template-columns: repeat(3, minmax(0, 1fr));
+}
+
+.relay-settings {
+ display: grid;
+ grid-template-columns: minmax(0, 1.2fr) minmax(240px, 0.8fr);
+ gap: 16px 24px;
+ align-items: start;
+}
+
+.relay-toggle {
+ padding-top: 26px;
+}
+
+.proxy-row {
+ display: flex;
+ align-items: center;
+ gap: 10px;
+}
+
+.proxy-row input {
+ flex: 1;
+ min-width: 0;
+}
+
+.proxy-row .checkbox {
+ min-height: auto;
+ flex: none;
+ padding-top: 0;
+}
+
+.userbot-layout {
+ display: grid;
+ grid-template-columns: minmax(0, 1.3fr) minmax(260px, 0.7fr);
+ gap: 24px;
+ align-items: start;
+}
+
+.userbot-auth {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 16px;
+}
+
+.userbot-settings {
+ display: flex;
+ min-width: 0;
+ flex-direction: column;
+ gap: 14px;
+ padding: 18px;
+ background: var(--surface-muted);
+ border: 1px solid var(--line);
+ border-radius: 12px;
+}
+
+.userbot-settings-head h3 {
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.userbot-settings .checkbox {
+ min-height: 32px;
+ padding-top: 0;
+}
+
+.userbot-settings-actions {
+ display: flex;
+ justify-content: flex-end;
+ padding-top: 2px;
+}
+
+.userbot-session-state {
+ display: flex;
+ align-items: flex-start;
+ gap: 12px;
+ padding: 16px;
+ background: var(--success-soft);
+ border: 1px solid transparent;
+ border-radius: 12px;
+}
+
+.userbot-session-icon {
+ display: grid;
+ width: 30px;
+ height: 30px;
+ flex: none;
+ place-items: center;
+ color: var(--success);
+ background: var(--surface);
+ border-radius: 50%;
+}
+
+.userbot-session-state strong {
+ display: block;
+ color: var(--text);
+ font-size: 14px;
+ font-weight: 500;
+}
+
+.userbot-cancel {
+ display: flex;
+ justify-content: flex-start;
+}
+
+.flow-status {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 12px 0;
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.flow-steps {
+ display: grid;
+ grid-template-columns: repeat(3, 1fr);
+ gap: 8px;
+ padding: 4px 0 2px;
+}
+
+.flow-step {
+ padding-top: 9px;
+ border-top: 2px solid var(--line);
+ color: var(--text-3);
+ font-size: 12px;
+}
+
+.flow-step.active {
+ border-color: var(--accent);
+ color: var(--accent-strong);
+}
+
+.flow-step.done {
+ border-color: var(--success);
+ color: var(--success);
+}
+
diff --git a/web/ui/src/taskFields.ts b/web/ui/src/taskFields.ts
new file mode 100644
index 00000000..85b4d0e7
--- /dev/null
+++ b/web/ui/src/taskFields.ts
@@ -0,0 +1,167 @@
+export type TaskFieldKind = 'text' | 'number' | 'bool' | 'list' | 'json'
+
+export interface TaskField {
+ name: string
+ label: string
+ kind: TaskFieldKind
+ placeholder?: string
+ required?: boolean
+ hint?: string
+ storageSelect?: boolean
+}
+
+export interface TaskTypeDef {
+ type: string
+ label: string
+ fields: TaskField[]
+}
+
+export const taskTypeDefs: TaskTypeDef[] = [
+ {
+ type: 'directlinks',
+ label: 'tasks.types.directlinks',
+ fields: [
+ {
+ name: 'urls',
+ label: 'tasks.fields.directlinks.urls.label',
+ kind: 'list',
+ required: true,
+ placeholder: 'https://example.com/file.zip\nhttps://example.com/another.zip',
+ },
+ ],
+ },
+ {
+ type: 'ytdlp',
+ label: 'tasks.types.ytdlp',
+ fields: [
+ {
+ name: 'urls',
+ label: 'tasks.fields.ytdlp.urls.label',
+ kind: 'list',
+ required: true,
+ placeholder: 'https://www.youtube.com/watch?v=...',
+ },
+ {
+ name: 'flags',
+ label: 'tasks.fields.ytdlp.flags.label',
+ kind: 'list',
+ placeholder: '--cookies-from-browser chrome',
+ hint: 'tasks.fields.ytdlp.flags.hint',
+ },
+ ],
+ },
+ {
+ type: 'aria2',
+ label: 'tasks.types.aria2',
+ fields: [
+ {
+ name: 'urls',
+ label: 'tasks.fields.aria2.urls.label',
+ kind: 'list',
+ required: true,
+ placeholder: 'https://example.com/file.iso',
+ },
+ {
+ name: 'options',
+ label: 'tasks.fields.aria2.options.label',
+ kind: 'json',
+ placeholder: '{"split": "8", "max-connection-per-server": "8"}',
+ hint: 'tasks.fields.aria2.options.hint',
+ },
+ ],
+ },
+ {
+ type: 'parseditem',
+ label: 'tasks.types.parseditem',
+ fields: [
+ {
+ name: 'url',
+ label: 'tasks.fields.parseditem.url.label',
+ kind: 'text',
+ required: true,
+ placeholder: 'https://example.com/post/123',
+ },
+ ],
+ },
+ {
+ type: 'tgfiles',
+ label: 'tasks.types.tgfiles',
+ fields: [
+ {
+ name: 'message_links',
+ label: 'tasks.fields.tgfiles.message_links.label',
+ kind: 'list',
+ placeholder: 'https://t.me/c/123456789/42',
+ },
+ {
+ name: 'chat_id',
+ label: 'tasks.fields.tgfiles.chat_id.label',
+ kind: 'text',
+ placeholder: '@channel or -1001234567890',
+ },
+ {
+ name: 'message_range',
+ label: 'tasks.fields.tgfiles.message_range.label',
+ kind: 'text',
+ placeholder: '100-120',
+ },
+ {
+ name: 'filter',
+ label: 'tasks.fields.tgfiles.filter.label',
+ kind: 'text',
+ placeholder: '.*\.mp4$',
+ },
+ ],
+ },
+ {
+ type: 'tphpics',
+ label: 'tasks.types.tphpics',
+ fields: [
+ {
+ name: 'telegraph_url',
+ label: 'tasks.fields.tphpics.telegraph_url.label',
+ kind: 'text',
+ required: true,
+ placeholder: 'https://telegra.ph/...',
+ },
+ ],
+ },
+ {
+ type: 'transfer',
+ label: 'tasks.types.transfer',
+ fields: [
+ {
+ name: 'source_storage_id',
+ label: 'tasks.fields.transfer.source_storage.label',
+ kind: 'text',
+ required: true,
+ storageSelect: true,
+ },
+ {
+ name: 'source_path',
+ label: 'tasks.fields.transfer.source_path.label',
+ kind: 'text',
+ required: true,
+ placeholder: 'folder/subfolder',
+ },
+ {
+ name: 'target_storage_id',
+ label: 'tasks.fields.transfer.target_storage.label',
+ kind: 'text',
+ required: true,
+ storageSelect: true,
+ },
+ {
+ name: 'target_path',
+ label: 'tasks.fields.transfer.target_path.label',
+ kind: 'text',
+ required: true,
+ placeholder: 'backup',
+ },
+ ],
+ },
+]
+
+export function taskTypeDef(type: string): TaskTypeDef | undefined {
+ return taskTypeDefs.find((item) => item.type === type)
+}
diff --git a/web/ui/src/types.ts b/web/ui/src/types.ts
new file mode 100644
index 00000000..2f4705df
--- /dev/null
+++ b/web/ui/src/types.ts
@@ -0,0 +1,330 @@
+export type JsonValue =
+ | string
+ | number
+ | boolean
+ | null
+ | JsonValue[]
+ | { [key: string]: JsonValue }
+
+export type JsonObject = { [key: string]: JsonValue }
+
+export type TaskStatus = 'queued' | 'running' | 'completed' | 'partial' | 'failed' | 'cancelled'
+
+export interface TaskProgress {
+ total_bytes?: number
+ downloaded_bytes?: number
+ total_files?: number
+ downloaded_files?: number
+ percent?: number
+ speed_mbps?: number
+}
+
+export interface TaskFileItem {
+ index: number
+ name: string
+ path?: string
+ storage_id?: number
+ storage_name?: string
+ status: string
+ error?: string
+ size?: number
+ source_chat_id?: number
+ source_message_id?: number
+ metadata_available?: boolean
+}
+
+export interface TaskItem {
+ task_id: string
+ type: string
+ title: string
+ status: TaskStatus
+ storage_id: number
+ storage_name: string
+ path: string
+ source?: string
+ error?: string
+ created_at: string
+ started_at?: string
+ finished_at?: string
+ updated_at?: string
+ progress?: TaskProgress
+ items?: TaskFileItem[]
+ failed_items?: TaskFileItem[]
+ failed_count?: number
+ metadata_available?: boolean
+}
+
+export interface TaskListResponse {
+ tasks: TaskItem[]
+ total: number
+}
+
+export interface CreateTaskRequest {
+ type: string
+ storage_id: number
+ path: string
+ webhook?: string
+ params: JsonObject
+}
+
+export interface TaskCreated {
+ task_id: string
+ type: string
+ status: TaskStatus
+ storage_id: number
+ storage_name: string
+ created_at: string
+}
+
+export interface TaskTypeInfo {
+ type: string
+ label: string
+}
+
+export interface StorageRecord {
+ storage_id: number
+ name: string
+ type: string
+ enable: boolean
+ config: JsonObject
+ config_version?: number
+ error?: string
+}
+
+export interface StorageInput {
+ name: string
+ type: string
+ enable: boolean
+ config: JsonObject
+}
+
+export interface StorageTestResult {
+ ok: boolean
+ error?: string
+}
+
+export interface UserStorageFilter {
+ storage_ids: number[]
+ storage_names?: string[]
+ mode: string
+}
+
+export interface UserItem {
+ id: number
+ chat_id: number
+ enabled: boolean
+ silent: boolean
+ default_storage_id: number
+ default_storage_name?: string
+ default_dir_id: number
+ apply_rule: boolean
+ filename_strategy: string
+ filename_template: string
+ conflict_strategy: string
+ watch_notify: boolean
+ storage_filter: UserStorageFilter
+}
+
+export interface UserPatch {
+ enabled?: boolean
+ silent?: boolean
+ default_storage_id?: number
+ default_dir_id?: number
+ apply_rule?: boolean
+ filename_strategy?: string
+ filename_template?: string
+ conflict_strategy?: string
+ watch_notify?: boolean
+ storage_filter?: UserStorageFilter
+}
+
+export interface CreateUserInput extends UserPatch {
+ chat_id: number
+}
+
+export interface UserDir {
+ id: number
+ storage_id: number
+ storage_name?: string
+ path: string
+}
+
+export interface UserRule {
+ id: number
+ type: string
+ content: string
+ storage_id: number
+ storage_name?: string
+ dir_path: string
+}
+
+export interface UserWatch {
+ id: number
+ chat_id: number
+ message_filter?: string
+}
+
+export interface ParserFile {
+ name: string
+ directory: string
+ size: number
+ modified: string
+}
+
+export interface ParserList {
+ enabled: boolean
+ dirs: string[]
+ files: ParserFile[]
+}
+
+export interface UpdateInfo {
+ current: string
+ latest?: string
+ available: boolean
+ updatable: boolean
+ docker: boolean
+ release_notes?: string
+ url?: string
+ published_at?: string
+ size?: number
+ reason?: string
+ reason_code?: string
+}
+
+export interface BotStatus {
+ state: string
+ id?: number
+ username?: string
+ error?: string
+ started_at?: string
+}
+
+export type LoginFlowState =
+ | 'starting'
+ | 'code_required'
+ | 'password_required'
+ | 'verifying'
+ | 'failed'
+ | 'done'
+ | 'cancelled'
+
+export interface LoginFlow {
+ flow_id: string
+ state: LoginFlowState
+ error?: string
+}
+
+export interface UserbotStatus {
+ enabled: boolean
+ state: string
+ id?: number
+ username?: string
+ phone?: string
+ error?: string
+ login?: LoginFlow
+}
+
+export interface TelegramStatus {
+ bot: BotStatus
+ userbot: UserbotStatus
+}
+
+export interface BotWorker {
+ id: number
+ name: string
+ enabled: boolean
+ session_path?: string
+ token_set: boolean
+ state: string
+ username?: string
+ error?: string
+}
+
+export interface BotWorkerInput {
+ name: string
+ token: string
+ enabled: boolean
+}
+
+export interface BotWorkerTestResult {
+ ok: boolean
+ state?: string
+ username?: string
+ error?: string
+}
+
+export interface SettingsMap {
+ [group: string]: JsonObject
+}
+
+export interface SettingsUpdateResult {
+ restart_required: string[]
+ warnings?: string[]
+}
+
+export interface SystemInfo {
+ version: string
+ commit: string
+ build_time: string
+ docker: boolean
+ uptime_seconds: number
+ data_dir: string
+ db_path: string
+ workers: number
+ queued_tasks: number
+ running_tasks: number
+}
+
+export interface MeResponse {
+ authenticated: boolean
+ csrf_token?: string
+}
+
+export interface FilesystemEntry {
+ name: string
+ path: string
+ is_dir: boolean
+ size?: number
+}
+
+export interface FilesystemListing {
+ path: string
+ parent?: string
+ root: string
+ cwd: string
+ selected?: string
+ entries: FilesystemEntry[]
+ truncated?: boolean
+}
+
+export interface OnboardingSteps {
+ bot_configured: boolean
+ storage_ready: boolean
+ user_ready: boolean
+ bot_running: boolean
+ first_task: boolean
+}
+
+export interface OnboardingLegacy {
+ imported: boolean
+ imported_at?: string
+}
+
+export interface OnboardingStatus {
+ complete: boolean
+ steps: OnboardingSteps
+ bot_state: string
+ legacy: OnboardingLegacy
+}
+
+export interface TaskMetadataItem {
+ index: number
+ name?: string
+ path?: string
+ metadata: JsonObject
+}
+
+export interface TaskMetadataResponse {
+ task_id: string
+ items: TaskMetadataItem[]
+}
diff --git a/web/ui/src/utils.ts b/web/ui/src/utils.ts
new file mode 100644
index 00000000..73af3ae0
--- /dev/null
+++ b/web/ui/src/utils.ts
@@ -0,0 +1,169 @@
+import { translate as t } from './i18n'
+
+export function formatDateTime(value?: string | null): string {
+ if (!value) {
+ return '—'
+ }
+ const date = new Date(value)
+ if (Number.isNaN(date.getTime())) {
+ return value
+ }
+ const pad = (n: number) => String(n).padStart(2, '0')
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
+}
+
+export function taskDisplayTitle(task: { title?: string; type?: string }): string {
+ const raw = (task.title ?? '').trim()
+ const markdown = raw.match(/^\[[^\]]+\]\((.*)\)$/)
+ const inner = (markdown?.[1] ?? raw).trim()
+ if (!inner) {
+ return t('time.unnamedTask')
+ }
+ const transfer = inner.match(/^(\d+)\s+files\/([\d.]+)MB$/)
+ if (transfer) {
+ return t('time.files', { n: transfer[1], size: transfer[2] })
+ }
+ const arrow = inner.indexOf('->')
+ const display = (arrow >= 0 ? inner.slice(0, arrow) : inner).replace(/\.\.\.$/, '').trim()
+ if (display) {
+ return display
+ }
+ return task.type ? taskTypeLabel(task.type) : t('time.unnamedTask')
+}
+
+export function storageLocation(config: Record): string {
+ const keys = ['url', 'endpoint', 'remote', 'chat_id', 'base_path', 'path', 'bucket_name']
+ for (const key of keys) {
+ const value = config[key]
+ if (value !== undefined && value !== null && String(value).trim() !== '') {
+ return `${key} ${String(value)}`
+ }
+ }
+ return t('time.locationUnset')
+}
+
+export function formatRelativeTime(value?: string | null): string {
+ if (!value) {
+ return '—'
+ }
+ const date = new Date(value)
+ if (Number.isNaN(date.getTime())) {
+ return value
+ }
+ const seconds = Math.max(0, Math.floor((Date.now() - date.getTime()) / 1000))
+ if (seconds < 60) return t('time.justNow')
+ if (seconds < 3600) return t('time.minutesAgo', { n: Math.floor(seconds / 60) })
+ if (seconds < 86400) return t('time.hoursAgo', { n: Math.floor(seconds / 3600) })
+ if (seconds < 172800) return t('time.yesterday')
+ if (seconds < 604800) return t('time.daysAgo', { n: Math.floor(seconds / 86400) })
+ const pad = (n: number) => String(n).padStart(2, '0')
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
+}
+
+export function formatUptime(seconds?: number): string {
+ if (!seconds || seconds < 0) {
+ return '—'
+ }
+ const total = Math.floor(seconds)
+ const days = Math.floor(total / 86400)
+ const hours = Math.floor((total % 86400) / 3600)
+ const minutes = Math.floor((total % 3600) / 60)
+ const secs = total % 60
+ const parts: string[] = []
+ if (days > 0) parts.push(t('time.days', { n: days }))
+ if (hours > 0) parts.push(t('time.hours', { n: hours }))
+ if (minutes > 0) parts.push(t('time.minutes', { n: minutes }))
+ if (parts.length === 0 || secs > 0) parts.push(t('time.seconds', { n: secs }))
+ return parts.join(' ')
+}
+
+export function formatBytes(bytes?: number): string {
+ if (!bytes || bytes <= 0) {
+ return '0 B'
+ }
+ const units = ['B', 'KB', 'MB', 'GB', 'TB']
+ let value = bytes
+ let unit = 0
+ while (value >= 1024 && unit < units.length - 1) {
+ value /= 1024
+ unit += 1
+ }
+ return `${value.toFixed(unit === 0 ? 0 : 2)} ${units[unit]}`
+}
+
+const taskStatusLabels: Record = {
+ queued: 'states.queued',
+ running: 'states.running',
+ completed: 'states.completed',
+ partial: 'states.partial',
+ failed: 'states.failed',
+ cancelled: 'states.cancelled',
+}
+
+export function taskStatusLabel(status: string): string {
+ const key = taskStatusLabels[status]
+ return key ? t(key) : status
+}
+
+export function taskStatusTone(status: string): string {
+ switch (status) {
+ case 'running':
+ return 'info'
+ case 'completed':
+ return 'success'
+ case 'partial':
+ return 'warning'
+ case 'failed':
+ return 'danger'
+ case 'cancelled':
+ return 'muted'
+ default:
+ return 'warning'
+ }
+}
+
+const telegramStateLabels: Record = {
+ stopped: 'states.stopped',
+ starting: 'states.starting',
+ running: 'states.running',
+ error: 'states.error',
+ login_required: 'states.loginRequired',
+}
+
+export function telegramStateLabel(state?: string): string {
+ if (!state) {
+ return t('states.unknown')
+ }
+ const key = telegramStateLabels[state]
+ return key ? t(key) : state
+}
+
+export function telegramStateTone(state?: string): string {
+ switch (state) {
+ case 'running':
+ return 'success'
+ case 'starting':
+ return 'warning'
+ case 'error':
+ return 'danger'
+ case 'login_required':
+ return 'warning'
+ default:
+ return 'muted'
+ }
+}
+
+const taskTypeKeys: Record = {
+ directlinks: 'taskTypes.directlinks',
+ ytdlp: 'taskTypes.ytdlp',
+ aria2: 'taskTypes.aria2',
+ parseditem: 'taskTypes.parseditem',
+ tgfiles: 'taskTypes.tgfiles',
+ tphpics: 'taskTypes.tphpics',
+ transfer: 'taskTypes.transfer',
+}
+
+export function taskTypeLabel(type: string): string {
+ const key = taskTypeKeys[type]
+ return key ? t(key) : type
+}
diff --git a/web/ui/src/views/AutomationView.vue b/web/ui/src/views/AutomationView.vue
new file mode 100644
index 00000000..635e1910
--- /dev/null
+++ b/web/ui/src/views/AutomationView.vue
@@ -0,0 +1,581 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+
+
+
+
+
+
+
{{ t('automation.noUsers') }}
+
+
+
+
+
+
{{ t('automation.userTitle') }}
+
+
+
{{ t('automation.selectUser') }}
+
+
+
+
+
+
+
{{ t('automation.directories.title') }}
+ {{ t('automation.directories.count', { count: dirs.length }) }}
+
+
+
+
+
+
+
{{ t('automation.directories.empty') }}
+
+
+ {{ t('automation.directories.storage') }} {{ t('automation.directories.path') }} {{ t('automation.actions') }}
+
+
+ {{ storageName(dir.storage_id) }}
+ {{ dir.path }}
+
+
+ {{ t('automation.directories.makeDefault') }}
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+
+
+
+
+
{{ t('automation.rules.title') }}
+
+
+ {{ selectedUser?.apply_rule ? t('automation.rules.enabled') : t('automation.rules.disabled') }}
+
+
+ {{ selectedUser?.apply_rule ? t('automation.rules.disable') : t('automation.rules.enable') }}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
{{ t('automation.rules.empty') }}
+
+
+ {{ t('automation.rules.type') }} {{ t('automation.rules.content') }} {{ t('automation.rules.storage') }} {{ t('automation.rules.dirPath') }} {{ t('automation.actions') }}
+
+
+ {{ ruleTypeLabel(rule.type) }}
+ {{ rule.content }}
+ {{ rule.storage_id === 0 ? t('automation.rules.chosenStorage') : storageName(rule.storage_id) }}
+ {{ rule.dir_path || '—' }}
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+
+
+
{{ t('automation.watches.title') }}
+
+
+
+
+
+
+
+
+
{{ t('automation.watches.empty') }}
+
+
+ {{ t('automation.watches.chatId') }} {{ t('automation.watches.filter') }} {{ t('automation.actions') }}
+
+
+ {{ watch.chat_id }}
+ {{ watch.message_filter || '—' }}
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/DashboardView.vue b/web/ui/src/views/DashboardView.vue
new file mode 100644
index 00000000..198c9d55
--- /dev/null
+++ b/web/ui/src/views/DashboardView.vue
@@ -0,0 +1,347 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('common.actions.createTask') }}
+
+
+
+
+
+
+
+
+
+
+ {{ t(onboarding?.legacy.imported ? 'dashboard.setup.legacyEyebrow' : 'dashboard.setup.eyebrow') }}
+
+
{{ t('dashboard.setup.title') }}
+
{{ t('dashboard.setup.body') }}
+
+
+
+
+
+ {{ step.label }}
+
+
+
+
+
{{ setupDone }}/{{ setupTotal }}
+
{{ t('dashboard.setup.requiredSteps') }}
+
+ {{ t('dashboard.setup.continue') }}
+ {{ t('dashboard.setup.guide') }}
+
+
+
+
+
+
+
+ {{ statusLine }}
+
+
+
+
+
+
+ {{ t('dashboard.relay.entry') }}
+
+
{{ telegramStateLabel(status?.bot.state) }}
+
{{ status?.bot.username ? `@${status.bot.username}` : t('dashboard.status.botDisconnected') }}
+
+
+
+
+
+ {{ t('dashboard.relay.queue') }}
+
+
{{ t('dashboard.relay.queueCount', { n: activeTasks }) }}
+
{{ activeTasks > 0 ? t('dashboard.relay.queueBusy') : t('dashboard.relay.queueIdle') }}
+
+
+
+
+
+ {{ t('dashboard.relay.storage') }}
+
+
{{ t('dashboard.relay.storageAvailable', { n: enabledStorages }) }}
+
{{ t('dashboard.relay.storageTotal', { n: storages.length }) }}
+
+
+
+
+
+ {{ t('dashboard.metrics.activeTasks') }}
+ {{ activeTasks }}
+
+
+ {{ t('dashboard.metrics.weekTasks') }}
+ {{ weekTotal }}
+
+
+ {{ t('dashboard.metrics.completedTasks') }}
+ {{ completedTasks }}
+
+
+
+
+
+
+
+
{{ t('dashboard.metrics.weekTasks') }}
+ {{ t('dashboard.activity.viewAll') }}
+
+
+ {{ weekTotal }}
+
+
+
+
{{ day.count }}
+
+
{{ day.label }}
+
+
+ {{ t('dashboard.activity.empty') }}
+
+
+
+
+
{{ t('dashboard.storage.title') }}
+ {{ t('dashboard.storage.manage') }}
+
+
+
+
+
+ {{ storage.name }}
+ {{ t('dashboard.storage.meta', { type: storageTypeLabel(storage.type), location: storageLocation(storage.config) }) }}
+
+
+ {{ storage.enable ? t('states.enabled') : t('states.disabled') }}
+
+
+
+
+
+
{{ t('dashboard.storage.empty') }}
+
+
+
+ {{ t('common.actions.createStorage') }}
+
+
+
+
+
+
+
+
+
{{ t('dashboard.tasks.recentTitle') }}
+ {{ t('dashboard.tasks.all') }}
+
+
+
+
+
{{ taskDisplayTitle(task) }}
+
+ {{ taskTypeLabel(task.type) }}{{ t('dashboard.tasks.separator') }}
+ {{ task.storage_name || t('dashboard.tasks.noStorage') }}{{ t('dashboard.tasks.separator') }}
+ {{ formatRelativeTime(task.created_at) }}
+
+
+
+
+
+ {{ taskStatusLabel(task.status) }}
+
+
+
+
+
+
+
{{ t('dashboard.tasks.empty') }}
+
+
+
+
+
diff --git a/web/ui/src/views/GuideView.vue b/web/ui/src/views/GuideView.vue
new file mode 100644
index 00000000..d7340728
--- /dev/null
+++ b/web/ui/src/views/GuideView.vue
@@ -0,0 +1,290 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+
+
+
+
+
{{ heroBadge }}
+
{{ heroTitle }}
+
{{ heroBody }}
+
+
+
+ {{ t('guide.hero.continueSetup') }}
+
+
+
+ {{ t('guide.hero.openTasks') }}
+
+
+
+ {{ t('guide.hero.openTelegram') }}
+
+
+ {{ t('guide.hero.openTasks') }}
+
+
+ {{ botReady ? t('guide.hero.review') : t('guide.hero.openTasks') }}
+
+
+
+
+ {{ t('guide.hero.passwordNote') }}
+
+
+
+
+
{{ requiredDone }}/{{ requiredTotal }}
+
+
{{ botReady ? t('guide.hero.readyBadge') : ready ? t('guide.hero.configuredBadge') : t('guide.hero.requiredSteps') }}
+
+
+
+
+
+
{{ t('guide.setup.title') }}
+ {{ requiredDone }}/{{ requiredTotal }}
+
+
+
+
+
+ {{ index + 1 }}
+
+
+
+
{{ step.title }}
+ {{ step.required ? t('guide.setup.required') : t('guide.setup.optional') }}
+ {{ step.done ? t('guide.setup.done') : t('guide.setup.pending') }}
+
+
{{ step.body }}
+
{{ step.hint }}
+
+ {{ step.action }}
+
+
+
+
+
+
+
+
+
+
{{ t('guide.migration.importedTitle') }}
+
{{ t('guide.migration.importedBody') }}
+
{{ t('guide.migration.importedAt', { time: legacyImportedAt }) }}
+
+
+
+ {{ t('guide.migration.intro') }}
+ {{ t('guide.migration.note') }}
+
+
+
+
+
+
{{ t('guide.usage.title') }}
+
+
+
+
+
+
{{ t('guide.usage.sendTitle') }}
+
{{ t('guide.usage.sendBody') }}
+
+
+
+
+
+
{{ t('guide.usage.saveTitle') }}
+
{{ t('guide.usage.saveBody') }}
+
+
+
+
+
+
{{ t('guide.usage.commandsTitle') }}
+
{{ t('guide.usage.commandsIntro') }}
+
+ {{ t('guide.usage.commands.start') }}
+ {{ t('guide.usage.commands.task') }}
+ {{ t('guide.usage.commands.config') }}
+
+
+
+
+
+
+
{{ t('guide.usage.consoleTitle') }}
+
{{ t('guide.usage.consoleBody') }}
+
+
+
+
+
+
{{ t('guide.usage.userbotTitle') }}
+
{{ t('guide.usage.userbotBody') }}
+
+
+
+
+
+
{{ t('guide.data.title') }}
+
{{ t('guide.data.body') }}
+
+
+
+
+
+
+
+
+
{{ loadError }}
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+
{{ t('guide.title') }}
+
+
diff --git a/web/ui/src/views/LoginView.vue b/web/ui/src/views/LoginView.vue
new file mode 100644
index 00000000..99b3f32d
--- /dev/null
+++ b/web/ui/src/views/LoginView.vue
@@ -0,0 +1,101 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ SaveAny-Bot
+
+
+
+
{{ t('login.title') }}
+
+
+
+
+ {{ t('login.password') }}
+
+
+
+
+ {{ t('login.submit') }}
+
+
+
+
+ {{ t('login.helpBefore') }}SAVEANY_ADMIN_PASSWORD{{ t('login.helpAfter') }}
+
+
+
+
+
diff --git a/web/ui/src/views/ParsersView.vue b/web/ui/src/views/ParsersView.vue
new file mode 100644
index 00000000..197f01b1
--- /dev/null
+++ b/web/ui/src/views/ParsersView.vue
@@ -0,0 +1,165 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+
+
+
+
+
+
+
{{ t('parsers.title') }}
+
+ {{ data.enabled ? t('parsers.enabled') : t('parsers.disabled') }}
+
+
+
+
+
+
+
+ {{ t('parsers.chooseFile') }}
+
+
+
{{ form.filename || t('parsers.noFile') }}
+
+
+
+ {{ t('parsers.filename') }}
+
+
+
+ {{ t('parsers.content') }}
+
+
+
+
+
+ {{ t('parsers.install') }}
+
+
+
+
+
+
+
+
{{ t('parsers.installedTitle') }}
+ {{ t('parsers.count', { count: data.files.length }) }}
+
+
{{ t('parsers.empty') }}
+
+
+ {{ t('parsers.filename') }} {{ t('parsers.directory') }} {{ t('parsers.size') }}
+
+
+ {{ file.name }}
+ {{ file.directory }}
+ {{ file.size }} B
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/SettingsView.vue b/web/ui/src/views/SettingsView.vue
new file mode 100644
index 00000000..b0ae234b
--- /dev/null
+++ b/web/ui/src/views/SettingsView.vue
@@ -0,0 +1,411 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.reload') }}
+
+
+
+
+
{{ t('settings.loading') }}
+
+
+
+
+
+
+ {{ group.title }}
+
+
+
+
+
+
+
{{ group.title }}
+
+
+
+
+
+
+ {{ field.label }}
+ {{ field.hint }}
+
+
+
+
+
+ {{ t('common.actions.save') }}
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/StoragesView.vue b/web/ui/src/views/StoragesView.vue
new file mode 100644
index 00000000..0db559cf
--- /dev/null
+++ b/web/ui/src/views/StoragesView.vue
@@ -0,0 +1,221 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('common.actions.createStorage') }}
+
+
+
+
+
+
+
+
+
+
+
+
+
{{ storage.name }}
+
{{ storageTypeLabel(storage.type) }}
+
+
+
+ {{ storage.enable ? t('states.enabled') : t('states.disabled') }}
+
+
+
+ {{ storageLocation(storage.config) }}
+ {{ storage.error }}
+
+
+
+
+
+
+
{{ t('storages.empty') }}
+
+
+ {{ t('common.actions.createStorage') }}
+
+
+
+
+
+
+
+
+
+
+ {{ editing ? t('storages.dialog.edit') : t('storages.dialog.create') }}
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/SystemView.vue b/web/ui/src/views/SystemView.vue
new file mode 100644
index 00000000..a87f508e
--- /dev/null
+++ b/web/ui/src/views/SystemView.vue
@@ -0,0 +1,203 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('system.restart') }}
+
+
+
+
+
+
+
{{ t('system.process.running') }}
+
{{ info.version }}
+
{{ info.commit || t('system.info.commitUnknown') }}
+
{{ info.build_time }}
+
+
+
+
{{ t('system.process.uptime') }}
+
{{ formatUptime(info.uptime_seconds) }}
+
+
+
{{ t('system.process.tasks') }}
+
{{ info.running_tasks }}
+
{{ t('system.process.queued', { n: info.queued_tasks }) }}
+
+
+
{{ t('system.process.concurrency') }}
+
{{ info.workers }}
+
+
+
+
+
{{ t('system.process.loading') }}
+
+
+
+
{{ t('system.info.title') }}
+
+ {{ info.docker ? t('system.info.docker') : t('system.info.local') }}
+
+
+
+ {{ t('system.info.version') }} {{ info.version }}
+ {{ t('system.info.commit') }} {{ info.commit || '—' }}
+ {{ t('system.info.buildTime') }} {{ info.build_time }}
+ {{ t('system.info.dataDir') }} {{ info.data_dir }}
+ {{ t('system.info.database') }} {{ info.db_path }}
+ {{ t('system.info.listenAddress') }} {{ host }}
+
+
+
+
+
{{ t('system.update.title') }}
+
+
+ {{ checking ? t('system.update.checking') : t('system.update.check') }}
+
+
+
{{ t('system.update.notChecked') }}
+
+
+ {{ t('system.update.current') }} {{ updateInfo.current }}
+ {{ t('system.update.latest') }} {{ updateInfo.latest }}
+ {{ t('system.update.published') }} {{ new Date(updateInfo.published_at).toLocaleString() }}
+ {{ t('system.update.size') }} {{ updateInfo.size }} B
+
+ {{ t('system.update.notUpdatable') }}: {{ updateInfo.reason }}
+ {{ t('system.update.major') }}
+ {{ t('system.update.noRelease') }}
+ {{ t('system.update.available') }}
+ {{ t('system.update.upToDate') }}
+
+ {{ updating ? t('system.update.updating') : t('system.update.install') }}
+
+
+ {{ t('system.update.releaseNotes') }}
+ {{ updateInfo.release_notes }}
+
+
+
+
+
diff --git a/web/ui/src/views/TasksView.vue b/web/ui/src/views/TasksView.vue
new file mode 100644
index 00000000..3296da58
--- /dev/null
+++ b/web/ui/src/views/TasksView.vue
@@ -0,0 +1,304 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('common.actions.createTask') }}
+
+
+
+
+
+
+
{{ t('tasks.filters.status') }}
+
+
+
+
{{ t('tasks.filters.type') }}
+
+
+
+
{{ t('tasks.count', { total }) }}
+
+
+
+
+
+
+
+
+
+
+
{{ t('tasks.createTitle') }}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/TelegramView.vue b/web/ui/src/views/TelegramView.vue
new file mode 100644
index 00000000..723f9f4a
--- /dev/null
+++ b/web/ui/src/views/TelegramView.vue
@@ -0,0 +1,752 @@
+
+
+
+
+
+
+
+
+ {{ t('telegram.actions.refreshStatus') }}
+
+
+
+
+
+
+
+
+
+
+
+
+ {{ telegramStateLabel(status?.bot.state) }}
+
+
+
+
+ {{ status?.bot.username ? `@${status.bot.username}` : t('telegram.bot.notConnected') }}
+
+
+ {{ status.bot.error }}
+
+
+
+ {{ t('telegram.actions.start') }}
+
+
+
+ {{ t('telegram.actions.stop') }}
+
+
+
+ {{ t('telegram.actions.restart') }}
+
+
+
+
+
+
+
+
+ {{ telegramStateLabel(status?.userbot.state) }}
+
+
+
+
{{ userbotIdentity }}
+
+ {{ status.userbot.error }}
+
+
+
+ {{ t('common.actions.signIn') }}
+
+
+
+ {{ t('common.actions.logout') }}
+
+
+
+
+
+
+
+
{{ t('telegram.botPanel.title') }}
+
+
+
+ {{ t('telegram.botPanel.maskedHint') }}
+
+ {{ t('common.actions.saveApply') }}
+
+
+ {{ t('telegram.botPanel.loading') }}
+
+
+
+
+
{{ t('telegram.userbotPanel.title') }}
+ {{ flowStateLabel }}
+
+
+
+
+
+
+
{{ isUserbotLoggedIn ? t('telegram.userbot.loggedIn') : t('telegram.flow.states.done') }}
+
+
+
+
+ {{ t('telegram.steps.phone') }}
+ {{ t('telegram.steps.code') }}
+ {{ t('telegram.steps.password') }}
+
+
+
+
+ {{ flowState === 'starting' ? t('telegram.flow.connecting') : t('telegram.flow.verifying') }}
+
+
+ {{ loginFlow.error }}
+
+
+
+
+ {{ t('telegram.login.phoneLabel') }}
+
+
+
+
+
+ {{ t('common.actions.sendCode') }}
+
+
+ {{ t('telegram.login.codeHint') }}
+
+
+
+
{{ t('telegram.login.codeLabel') }}
+
+
+ {{ t('common.actions.submit') }}
+
+
+
+
+
{{ t('telegram.login.passwordLabel') }}
+
+
+ {{ t('common.actions.submit') }}
+
+
+
+
+ {{ t('common.actions.cancelLogin') }}
+
+
+
+
+
+
+
{{ t('telegram.session.title') }}
+
+
+
{{ t('telegram.session.file') }} *
+
+
+
+
+ {{ t('telegram.session.enable') }}
+
+
+ {{ t('common.actions.saveSettings') }}
+
+
+
{{ t('telegram.session.loading') }}
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/src/views/UsersView.vue b/web/ui/src/views/UsersView.vue
new file mode 100644
index 00000000..97c534bb
--- /dev/null
+++ b/web/ui/src/views/UsersView.vue
@@ -0,0 +1,413 @@
+
+
+
+
+
+
+
+
+ {{ t('common.actions.refresh') }}
+
+
+
+ {{ t('users.add') }}
+
+
+
+
+
+
+
+
{{ t('users.count', { count: users.length, enabled: enabledUsers }) }}
+
+
+
+
+
+
+ {{ t('users.columns.chatId') }} {{ t('users.columns.status') }} {{ t('users.columns.defaultStorage') }} {{ t('users.columns.storageFilter') }} {{ t('users.columns.actions') }}
+
+
+ {{ user.chat_id }}
+
+
+
+ {{ user.enabled ? t('users.status.enabled') : t('users.status.disabled') }}
+
+ {{ t('users.status.silent') }}
+
+
+ {{ storageName(user.default_storage_id) || '—' }}
+
+ {{ (user.storage_filter.mode || 'blacklist') === 'blacklist' ? t('users.filter.blacklist') : t('users.filter.whitelist') }}{{ t('users.filter.separator') }}
+ {{ (user.storage_filter.storage_ids ?? []).length ? storageNames(user.storage_filter.storage_ids).join(t('users.storageSeparator')) : ((user.storage_filter.mode || 'blacklist') === 'blacklist' ? t('users.filter.all') : t('users.filter.none')) }}
+
+
+
+ {{ t('common.actions.edit') }}
+ {{ t('common.actions.delete') }}
+
+
+
+
+
+
+ {{ t('users.empty') }}
+
+
+
+
+
+
+
+
+
+
{{ user.chat_id }}
+
+
+ {{ user.enabled ? t('users.status.enabled') : t('users.status.disabled') }}
+
+ {{ t('users.status.silent') }}
+
+
+
+
+
{{ t('users.columns.defaultStorage') }}
+ {{ storageName(user.default_storage_id) || t('users.unset') }}
+
+
+
{{ t('users.columns.storageFilter') }}
+
+ {{ (user.storage_filter.mode || 'blacklist') === 'blacklist' ? t('users.filter.blacklist') : t('users.filter.whitelist') }}{{ t('users.filter.separator') }}{{
+ (user.storage_filter.storage_ids ?? []).length
+ ? storageNames(user.storage_filter.storage_ids).join(t('users.storageSeparator'))
+ : (user.storage_filter.mode || 'blacklist') === 'blacklist'
+ ? t('users.filter.all')
+ : t('users.filter.none')
+ }}
+
+
+
+
+ {{ t('common.actions.edit') }}
+ {{ t('common.actions.delete') }}
+
+
+
+
+
{{ t('users.empty') }}
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/web/ui/tsconfig.json b/web/ui/tsconfig.json
new file mode 100644
index 00000000..13613c04
--- /dev/null
+++ b/web/ui/tsconfig.json
@@ -0,0 +1,20 @@
+{
+ "compilerOptions": {
+ "target": "ES2024",
+ "useDefineForClassFields": true,
+ "module": "ESNext",
+ "lib": ["ES2024", "DOM", "DOM.Iterable"],
+ "skipLibCheck": true,
+ "moduleResolution": "bundler",
+ "allowImportingTsExtensions": true,
+ "resolveJsonModule": true,
+ "isolatedModules": true,
+ "noEmit": true,
+ "strict": true,
+ "noUnusedLocals": true,
+ "noUnusedParameters": true,
+ "noFallthroughCasesInSwitch": true,
+ "jsx": "preserve"
+ },
+ "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.vue"]
+}
diff --git a/web/ui/vite.config.ts b/web/ui/vite.config.ts
new file mode 100644
index 00000000..eee0b1c0
--- /dev/null
+++ b/web/ui/vite.config.ts
@@ -0,0 +1,24 @@
+import { fileURLToPath, URL } from 'node:url'
+import { defineConfig } from 'vite'
+import vue from '@vitejs/plugin-vue'
+
+export default defineConfig({
+ plugins: [vue()],
+ resolve: {
+ alias: {
+ '@': fileURLToPath(new URL('./src', import.meta.url)),
+ },
+ },
+ build: {
+ outDir: '../dist',
+ emptyOutDir: true,
+ },
+ server: {
+ proxy: {
+ '/api': {
+ target: 'http://127.0.0.1:28080',
+ changeOrigin: true,
+ },
+ },
+ },
+})