* refactor: a big refactor. wip * refactor: port handle file * refactor: place all handlers * fix: task info nil pointer * feat: enhance task progress tracking and context management * feat: cancel task * feat: stream mode * feat: silent mode * feat: dir cmd * refactor: remove unused old file * feat: rule cmd * feat: handle silent mode * feat: batch task * fix: batch task progress and temp file cleanup * refactor: update file creation and cleanup methods for better resource management * feat: add save command with silent mode handling * feat: message link * feat: update message prompts to include file count in storage selection * feat: slient save links * refactor: reduce dup code * feat: rule type * feat: chose dir * feat: refactor file handling and storage rules, improve error handling and logging * feat: rule mode * feat: telegraph pics * fix: tphpics nil pointer and inaccurate dirpath * feat: silent save telegraph * feat: add suffix to avoid file overwrite * feat: new storage telegram * chore: tidy go mod
76 lines
1.4 KiB
Go
76 lines
1.4 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
|
|
}
|
|
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
|
|
}
|
|
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,
|
|
}
|
|
}
|