downloader

package
v3.0.0-pre-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package downloader provides the sync and async file download functionality.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoFS = errors.New("fs adapter not initialised")
View Source
var ErrNotStarted = errors.New("downloader not started")

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the instance of the downloader.

func New

func New(sc Downloader, fs fsadapter.FS, opts ...Option) *Client

New initialises new file downloader.

Example (Advanced)
package main

import (
	"context"
	"fmt"

	"github.com/rusq/fsadapter"
	"github.com/rusq/slack"
	"github.com/rusq/slackdump/v3/downloader"
	"golang.org/x/time/rate"
)

func main() {
	client := slack.New("token")

	// initialise the filesystem (files.zip archive)
	fs, err := fsadapter.NewZipFile("files.zip")
	if err != nil {
		fmt.Println("failed to initialise the file system")
		return
	}
	defer fs.Close()

	dl := downloader.NewV1(
		client,
		fs,
		downloader.RetriesV1(100), // 100 retries when rate limited
		downloader.LimiterV1(rate.NewLimiter(20, 1)), // rate limit
		downloader.WorkersV1(8),                      // number of download workers
	)

	f := &slack.File{}

	if n, err := dl.SaveFile(context.Background(), "some_dir", f); err != nil {
		fmt.Printf("failed to save the file: %s", err)
	} else {
		fmt.Printf("downloaded: %d bytes", n)
	}
}
Output:

Example (Basic)
package main

import (
	"context"
	"fmt"

	"github.com/rusq/fsadapter"
	"github.com/rusq/slack"
	"github.com/rusq/slackdump/v3/downloader"
)

func main() {
	client := slack.New("token")
	fs := fsadapter.NewDirectory("files/")

	dl := downloader.NewV1(
		client,
		fs,
	)

	f := &slack.File{}

	if n, err := dl.SaveFile(context.Background(), "some_dir", f); err != nil {
		fmt.Printf("failed to save the file: %s", err)
	} else {
		fmt.Printf("downloaded: %d bytes", n)
	}
}
Output:

func (*Client) AsyncDownloader

func (c *Client) AsyncDownloader(ctx context.Context, queueC <-chan Request) (<-chan struct{}, error)

func (*Client) Download

func (c *Client) Download(fullpath string, url string) error

Download requires a started downloader, otherwise it will return ErrNotStarted. Will place the file to the download queue.

func (*Client) Start

func (c *Client) Start(ctx context.Context)

Start starts an async file downloader. If the downloader is already started, it does nothing.

func (*Client) Stop

func (c *Client) Stop()

Stop waits for all transfers to finish, and stops the downloader.

type ClientV1 deprecated

type ClientV1 struct {
	// contains filtered or unexported fields
}

ClientV1 is the instance of the downloader.

Deprecated: Use Client.

func NewV1 deprecated

func NewV1(client Downloader, fs fsadapter.FS, opts ...OptionV1) *ClientV1

NewV1 initialises new file downloader.

Deprecated: use New instead.

func (*ClientV1) AsyncDownloader

func (c *ClientV1) AsyncDownloader(ctx context.Context, dir string, fileDlQueue <-chan *slack.File) (<-chan struct{}, error)

AsyncDownloader starts Client.worker goroutines to download files concurrently. It will download any file that is received on fileDlQueue channel. It returns the "done" channel and an error. "done" channel will be closed once all downloads are complete.

func (*ClientV1) DownloadFile

func (c *ClientV1) DownloadFile(dir string, f slack.File) (string, error)

DownloadFile requires a started downloader, otherwise it will return ErrNotStarted. Will place the file to the download queue, and save the file to the directory that was specified when Start was called. If the file buffer is full, will block until it becomes empty. It returns the filepath within the filesystem.

func (*ClientV1) SaveFile

func (c *ClientV1) SaveFile(ctx context.Context, dir string, f *slack.File) (int64, error)

SaveFile saves a single file to the specified directory synchrounously.

func (*ClientV1) Start

func (c *ClientV1) Start(ctx context.Context)

Start starts an async file downloader. If the downloader is already started, it does nothing.

func (*ClientV1) Stop

func (c *ClientV1) Stop()

type Downloader

type Downloader interface {
	// GetFile retreives a given file from its private download URL
	GetFile(downloadURL string, writer io.Writer) error
}

Downloader is the file downloader interface. It exists primarily for mocking in tests.

type FilenameFunc

type FilenameFunc func(*slack.File) string

FilenameFunc is the file naming function that should return the output filename for slack.File.

var Filename FilenameFunc = stdFilenameFn

Filename returns name of the file generated from the slack.File.

type Option

type Option func(*Client)

Option is the function signature for the option functions.

func Limiter

func Limiter(l *rate.Limiter) Option

Limiter uses the initialised limiter instead of built in.

func Retries

func Retries(n int) Option

Retries sets the number of attempts that will be taken for the file download.

func WithLogger

func WithLogger(l *slog.Logger) Option

Logger allows to use an external log library, that satisfies the *slog.Logger.

func Workers

func Workers(n int) Option

Workers sets the number of workers for the download queue.

type OptionV1

type OptionV1 func(*ClientV1)

OptionV1 is the function signature for the option functions.

func LimiterV1

func LimiterV1(l *rate.Limiter) OptionV1

LimiterV1 uses the initialised limiter instead of built in.

func LoggerV1

func LoggerV1(l *slog.Logger) OptionV1

LoggerV1 allows to use an external log library, that satisfies the logger.Interface.

func RetriesV1

func RetriesV1(n int) OptionV1

RetriesV1 sets the number of attempts that will be taken for the file download.

func WithNameFunc

func WithNameFunc(fn FilenameFunc) OptionV1

func WorkersV1

func WorkersV1(n int) OptionV1

WorkersV1 sets the number of workers for the download queue.

type Request

type Request struct {
	Fullpath string
	URL      string
}

Jump to

Keyboard shortcuts

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