- 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.
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package tftask
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
func executeStream(ctx context.Context, task *Task) error {
|
|
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("file[%s]", task.File.Name()))
|
|
|
|
pr, pw := io.Pipe()
|
|
defer pr.Close()
|
|
errg, uploadCtx := errgroup.WithContext(ctx)
|
|
errg.Go(func() error {
|
|
return task.Storage.Save(uploadCtx, pr, task.Path)
|
|
})
|
|
wr := newWriter(ctx, pw, task.Progress, task)
|
|
errg.Go(func() error {
|
|
defer pw.Close()
|
|
logger.Info("Starting file download in stream mode")
|
|
_, err := tfile.NewDownloader(task.File).Stream(uploadCtx, wr)
|
|
if err != nil {
|
|
logger.Errorf("Failed to download file: %v", err)
|
|
pw.CloseWithError(err)
|
|
}
|
|
return err
|
|
})
|
|
var err error
|
|
defer func() {
|
|
if task.Progress != nil {
|
|
task.Progress.OnDone(ctx, task, err)
|
|
}
|
|
}()
|
|
if err = errg.Wait(); err != nil {
|
|
return err
|
|
}
|
|
logger.Info("File downloaded successfully in stream mode")
|
|
return nil
|
|
}
|