mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-12 01:49:43 +08:00
- Added a new command handler for /watch that allows users to listen to messages from a specified chat and save them according to storage rules. - Introduced filtering options for messages using regular expressions. - Implemented functionality to start and stop watching chats, including error handling for invalid inputs and user settings. - Created a new utility package for message element handling related to the watch feature. - Updated the user model to manage watched chats, including methods to add, remove, and check if a chat is being watched.
80 lines
1.5 KiB
Go
80 lines
1.5 KiB
Go
package tftask
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type ProgressWriterAt struct {
|
|
ctx context.Context
|
|
wrAt io.WriterAt
|
|
progress ProgressTracker
|
|
downloaded *atomic.Int64
|
|
total int64
|
|
info TaskInfo
|
|
}
|
|
|
|
func (w *ProgressWriterAt) WriteAt(p []byte, off int64) (int, error) {
|
|
at, err := w.wrAt.WriteAt(p, off)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if w.progress != nil {
|
|
w.progress.OnProgress(w.ctx, w.info, w.downloaded.Add(int64(at)), w.total)
|
|
}
|
|
return at, nil
|
|
}
|
|
|
|
func newWriterAt(
|
|
ctx context.Context,
|
|
wrAt io.WriterAt,
|
|
progress ProgressTracker,
|
|
taskInfo TaskInfo,
|
|
) *ProgressWriterAt {
|
|
return &ProgressWriterAt{
|
|
ctx: ctx,
|
|
progress: progress,
|
|
downloaded: &atomic.Int64{},
|
|
total: taskInfo.FileSize(),
|
|
wrAt: wrAt,
|
|
info: taskInfo,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
if w.progress != nil {
|
|
w.progress.OnProgress(w.ctx, w.info, w.downloaded.Add(int64(at)), w.total)
|
|
}
|
|
return at, nil
|
|
}
|
|
|
|
func newWriter(
|
|
ctx context.Context,
|
|
wr io.Writer,
|
|
progress ProgressTracker,
|
|
taskInfo TaskInfo,
|
|
) *ProgressWriter {
|
|
return &ProgressWriter{
|
|
ctx: ctx,
|
|
progress: progress,
|
|
downloaded: &atomic.Int64{},
|
|
total: taskInfo.FileSize(),
|
|
wrAt: wr,
|
|
info: taskInfo,
|
|
}
|
|
}
|