Files
SaveAny-Bot/core/tasks/tfile/writer.go

96 lines
2.0 KiB
Go

package tfile
import (
"context"
"io"
"sync/atomic"
"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
}
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
}
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
}
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
}
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,
}
}