Implement resumable download queue (T-301)

This commit is contained in:
ila
2026-07-16 19:49:06 +08:00
parent 2d302b731a
commit 8dad40f934
27 changed files with 5498 additions and 18 deletions
+79
View File
@@ -0,0 +1,79 @@
package downloader
import (
"context"
"softbox.local/core/application"
)
// ApplicationPublisher is implemented by application.Runtime.
type ApplicationPublisher interface {
Publish(context.Context, application.Event) error
}
// ApplicationObserver maps downloader events to the documented application
// event envelope and concrete payloads.
type ApplicationObserver struct {
Publisher ApplicationPublisher
}
// PublishDownload implements Observer.
func (observer ApplicationObserver) PublishDownload(
ctx context.Context,
event Event,
) error {
if observer.Publisher == nil {
return nil
}
envelope := application.Event{
RequestID: event.RequestID,
AppID: event.AppID,
}
switch event.Type {
case EventStarted:
envelope.Type = application.EventDownloadStarted
envelope.Payload = application.DownloadStartedPayload{
Attempt: event.Attempt,
Done: event.Done,
TotalKnown: event.TotalKnown,
Total: event.Total,
}
case EventProgress:
envelope.Type = application.EventDownloadProgress
envelope.Payload = application.DownloadProgressPayload{
Attempt: event.Attempt,
Done: event.Done,
TotalKnown: event.TotalKnown,
Total: event.Total,
SpeedBytesSec: event.SpeedBytesSec,
}
case EventPaused:
envelope.Type = application.EventDownloadPaused
envelope.Payload = application.DownloadPausedPayload{
Attempt: event.Attempt,
Done: event.Done,
}
case EventCompleted:
envelope.Type = application.EventDownloadCompleted
envelope.Payload = application.DownloadCompletedPayload{
Attempt: event.Attempt,
Done: event.Done,
Path: event.CompletedPath,
}
case EventFailed:
envelope.Type = application.EventDownloadFailed
envelope.Payload = application.DownloadFailedPayload{
Attempt: event.Attempt,
Done: event.Done,
ErrorCode: event.ErrorCode,
}
case EventCanceled:
envelope.Type = application.EventDownloadCanceled
envelope.Payload = application.DownloadCanceledPayload{
Attempt: event.Attempt,
}
default:
return nil
}
return observer.Publisher.Publish(ctx, envelope)
}