fafnir

package module
v0.0.0-...-39e1a26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

README

Fafnir


Fafnir is a library that acts as a download manager with advanced features for your App

Usage example

package main

import (
	"sync"

	"github.com/khatibomar/fafnir"
	"github.com/khatibomar/fafnir/repository"
)

func main() {
	errChan := make(chan error, 10)
	go func() {
		for err := range errChan {
			// handle error
		}
	}()

	repo := repository.NewInMemory()

	var wg sync.WaitGroup

	cfg := &fafnir.Config{
		ErrChan:                errChan,
		MaxConcurrentDownloads: 2,
		MaxFailError:           3,
		UpdateTimeMs:           1000,
		Repo:                   repo,
		WaitGroup:              &wg,
	}
	faf, err := fafnir.New(cfg)
	if err != nil {
		// handle error
	}

	queueName := "q1"
	downloadDir := "./downloads"
	faf.Add(
		queueName,
		"https://static.wikia.nocookie.net/maid-dragon/images/f/ff/Fafnir_profile.jpg",
		downloadDir,
		"fafnir.jpg",
		"Fafnir (ファフニール Fafunīru)",
	)

	faf.Add(
		queueName,
		"https://static.wikia.nocookie.net/maid-dragon/images/5/57/Kanna_Anime.png",
		downloadDir,
		"kanna.png",
		"Kanna Kamui (カンナカムイ) ",
	)

	if err = faf.StartQueueDownload(queueName); err != nil {
		// handle error
	}
}

Showcase

https://github.com/khatibomar/fafnir/assets/35725554/4efa7cd2-2cf8-42fb-8de7-1b2e8f2487b6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyQueue    = fmt.Errorf("Queue is empty")
	ErrQueueNotFound = fmt.Errorf("Queue doesn't exist")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// ErrCh!=nil means errors during download are sent to
	// this channel for the client to consume.
	ErrChan                chan error
	MaxConcurrentDownloads uint
	// MaxFailError is the maximum number that an entry can fail
	// after failing N times it will not make it will not be re-queued
	MaxFailError uint
	// UpdateTimeMs specify the rate of which the info about the
	// entity that is currently downloading should be updated ,
	// this should be in millisecond (ms)
	UpdateTimeMs uint
	Repo         Repository
	// WaitGroup!=nil will update the wait group as goroutines
	// are started and finished.
	WaitGroup *sync.WaitGroup
}

type Entry

type Entry struct {
	ID          int
	Filename    string
	DwnDir      string
	Url         string
	Description string
	ExtraData   EntryExtraData
}

type EntryExtraData

type EntryExtraData struct {
	FailCount       int
	Size            int64
	BytesTransfered int64
	BytesPerSecond  float64
	ETA             time.Time
	Err             error
	Duration        time.Duration
	CanResume       bool
	DidResume       bool
	Start           time.Time
	End             time.Time
	Progress        float64
}

type EntryRepository

type EntryRepository interface {
	Update(Entry) error
	Add(string, string, string, string, string) error
	Complete(Entry) error
}

type Fafnir

type Fafnir struct {
	Cfg *Config
}

func New

func New(cfg *Config) (*Fafnir, error)

func (*Fafnir) Add

func (f *Fafnir) Add(queueName, link, dwnDir, filename, description string) error

func (*Fafnir) SortByName

func (f *Fafnir) SortByName(queueName string, descending bool) error

func (*Fafnir) StartQueueDownload

func (f *Fafnir) StartQueueDownload(queueName string) error

func (*Fafnir) StartQueueDownloadWithCtx

func (f *Fafnir) StartQueueDownloadWithCtx(ctx context.Context, queueName string) error

type Queue

type Queue struct {
	Name          string
	Entries       []Entry
	FailedEntries []Entry
	// contains filtered or unexported fields
}

func NewQueue

func NewQueue(name string) *Queue

func (*Queue) DeQueue

func (q *Queue) DeQueue() (Entry, error)

func (*Queue) DeQueueFail

func (q *Queue) DeQueueFail() (Entry, error)

func (*Queue) EnQueue

func (q *Queue) EnQueue(e Entry) error

func (*Queue) EnQueueFail

func (q *Queue) EnQueueFail(e Entry) error

func (*Queue) SortByName

func (q *Queue) SortByName(descending bool)

type QueueRepository

type QueueRepository interface {
	Fetch() ([]*Queue, error)
	Create(string) error
	Delete(string) error
	Get(string, chan error) (*Queue, error)
}

type Repository

type Repository interface {
	QueueRepository
	EntryRepository
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL