github.com/Files-com/files-sdk-go/v2@v2.1.2/file/download.go (about)

     1  package file
     2  
     3  import (
     4  	"io"
     5  	"io/fs"
     6  	"os"
     7  
     8  	"github.com/Files-com/files-sdk-go/v2/file/manager"
     9  	"github.com/Files-com/files-sdk-go/v2/file/status"
    10  
    11  	files_sdk "github.com/Files-com/files-sdk-go/v2"
    12  )
    13  
    14  func (c *Client) DownloadRetry(job status.Job, opts ...files_sdk.RequestResponseOption) *status.Job {
    15  	newJob := job.ClearStatuses()
    16  	return c.Downloader(
    17  		DownloaderParams{
    18  			RemotePath:     newJob.RemotePath,
    19  			Sync:           newJob.Sync,
    20  			Manager:        newJob.Manager,
    21  			LocalPath:      newJob.LocalPath,
    22  			RetryPolicy:    newJob.RetryPolicy.(RetryPolicy),
    23  			EventsReporter: newJob.EventsReporter,
    24  		},
    25  		opts...)
    26  }
    27  
    28  func (c *Client) DownloadToFile(params files_sdk.FileDownloadParams, filePath string, opts ...files_sdk.RequestResponseOption) (files_sdk.File, error) {
    29  	out, err := os.Create(filePath)
    30  	if err != nil {
    31  		return files_sdk.File{}, err
    32  	}
    33  	return c.Download(params, append(opts, files_sdk.ResponseBodyOption(func(closer io.ReadCloser) error {
    34  		_, err := io.Copy(out, closer)
    35  		return err
    36  	}))...)
    37  }
    38  
    39  type DownloaderParams struct {
    40  	RemotePath    string
    41  	RemoteFile    files_sdk.File
    42  	LocalPath     string
    43  	Sync          bool
    44  	PreserveTimes bool
    45  	RetryPolicy
    46  	*manager.Manager
    47  	status.EventsReporter
    48  	files_sdk.Config
    49  	DryRun bool
    50  }
    51  
    52  func (c *Client) Downloader(params DownloaderParams, opts ...files_sdk.RequestResponseOption) *status.Job {
    53  	params.Config = c.Config
    54  	return downloader(files_sdk.ContextOption(opts), (&FS{}).Init(c.Config, true), params)
    55  }
    56  
    57  type Entity struct {
    58  	fs.File
    59  	fs.FS
    60  	error
    61  }