Documentation ¶
Overview ¶
Package downloader provides the sync and async file download functionality.
Index ¶
- Variables
- type Client
- type ClientV1deprecated
- func (c *ClientV1) AsyncDownloader(ctx context.Context, dir string, fileDlQueue <-chan *slack.File) (<-chan struct{}, error)
- func (c *ClientV1) DownloadFile(dir string, f slack.File) (string, error)
- func (c *ClientV1) SaveFile(ctx context.Context, dir string, f *slack.File) (int64, error)
- func (c *ClientV1) Start(ctx context.Context)
- func (c *ClientV1) Stop()
- type Downloader
- type FilenameFunc
- type Option
- type OptionV1
- type Request
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoFS = errors.New("fs adapter not initialised")
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 (*Client) Download ¶
Download requires a started downloader, otherwise it will return ErrNotStarted. Will place the file to the download queue.
type ClientV1
deprecated
type ClientV1 struct {
// contains filtered or unexported fields
}
ClientV1 is the instance of the downloader.
Deprecated: Use Client.
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 ¶
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.
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 ¶
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 WithLogger ¶
Logger allows to use an external log library, that satisfies the *slog.Logger.
type OptionV1 ¶
type OptionV1 func(*ClientV1)
OptionV1 is the function signature for the option functions.
func LoggerV1 ¶
LoggerV1 allows to use an external log library, that satisfies the logger.Interface.
func WithNameFunc ¶
func WithNameFunc(fn FilenameFunc) OptionV1