resumable

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: MIT Imports: 15 Imported by: 0

README

go-resumable

It's a Go library providing multiple simultaneous and resumable uploads.

Library is designed to introduce fault-tolerance into the upload of large files throught HTTP. This is done by splitting each file into small chunks; whenever the upload of a chunk fails, uploading is retried until the procedure completes. This allows uploads to automatically resume uploading after a network connection is lost either locally or to the server. Additionally, it allows users to pause, resume and even recover uploads without losing state.

Demo

In this demo upload is paused for 2 seconds after initial second, then it uploads till 100%.

Please check here for full code sample.

Installation
$ go get -v https://github.com/bleenco/go-resumable
Usage
import (
  "net/http"

  "github.com/bleenco/go-resumable"
)

func main() {
  httpClient := &http.Client{}
  url := "http://example.com/upload"
  filePath := "/path/to/file/to/upload.zip"
  chunkSize := int(1 * (1 << 20)) // 1MB
  client := resumable.New(url, filePath, httpClient, chunkSize)

  client.Init()
  client.Start()

  resumable.WG.Wait() // this is important
}
Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWG sync.WaitGroup

WG exports wait group, so we can wait for it

View Source
var FileStorage = fileStorage{
	Path:     "./files",
	TempPath: ".tmp",
}

FileStorage settings. When finished uploading with success files are stored inside Path config. While uploading temporary files are stored inside TempPath directory.

Functions

func HTTPHandler

func HTTPHandler(w http.ResponseWriter, r *http.Request)

HTTPHandler is main request/response handler for HTTP server.

Types

type CalculateTransferredSize

type CalculateTransferredSize func(body string, partSize int, status UploadStatus) (int64, error)

type Logger

type Logger struct {
	ErrorLog *log.Logger
	InfoLog  *log.Logger
	DebugLog *log.Logger
}

type NullWriter

type NullWriter struct {
}

func NewNullWriter

func NewNullWriter() NullWriter

func (NullWriter) Write

func (NullWriter) Write(p []byte) (n int, err error)

type Resumable

type Resumable struct {
	Status UploadStatus
	// contains filtered or unexported fields
}

Resumable structure

func New

func New(url string, filePath string, client *http.Client, chunkSize int, debug bool) *Resumable

New creates new instance of resumable Client

func NewResumable

func NewResumable(method string, url string, filePath string, client *http.Client, chunkSize int,
	logger *Logger,
	wg *sync.WaitGroup) *Resumable

New creates new instance of resumable Client

func (*Resumable) Cancel

func (c *Resumable) Cancel() error

Cancel set upload state to stopped

func (*Resumable) Close

func (c *Resumable) Close()

func (*Resumable) GetWG

func (c *Resumable) GetWG() sync.WaitGroup

func (*Resumable) Init

func (c *Resumable) Init() error

Init method initializes upload

func (*Resumable) Pause

func (c *Resumable) Pause() error

Pause set upload state to paused

func (*Resumable) Start

func (c *Resumable) Start() error

Start set upload state to uploading

type UploadStatus

type UploadStatus struct {
	Size                 int64
	SizeTransferred      int64
	Parts                uint64
	PartsTransferred     uint64
	IsDone               bool
	TransferredException bool
}

UploadStatus holds the data about upload

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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