tusd

package module
v0.0.0-...-d87b72e Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2015 License: MIT Imports: 11 Imported by: 0

README

tusd

Build Status Build status

tusd is the official reference implementation of the tus resumable upload protocol. The protocol specifies a flexible method to upload files to remote servers using HTTP. The special feature is the ability to pause and resume uploads at any moment allowing to continue seamlessly after e.g. network interruptions.

Protocol version: 1.0.0

Getting started

Requirements:

  • Go (1.2 or newer)

Running tusd from source:

Clone the git repository and cd into it.

git clone git@github.com:tus/tusd.git
cd tusd

Now you can run tusd:

go run cmd/tusd/main.go

Using tusd manually

Besides from running tusd using the provided binary, you can embed it into your own Golang program:

package main

import (
	"github.com/tus/tusd"
	"github.com/tus/tusd/filestore"
	"net/http"
)

func main() {
	// Create a new FileStore instance which is responsible for
	// storing the uploaded file on disk in the specified directory.
	// If you want to save them on a different medium, for example
	// a remote FTP server, you can implement your own storage backend
	// by implementing the tusd.DataStore interface.
	store := filestore.FileStore{
		Path: "./uploads",
	}

	// Create a new HTTP handler for the tusd server by providing
	// a configuration object. The DataStore property must be set
	// in order to allow the handler to function.
	handler, err := tusd.NewHandler(tusd.Config{
		BasePath:              "files/",
		DataStore:             store,
	})
	if err != nil {
		panic("Unable to create handler: %s", err)
	}

	// Right now, nothing has happened since we need to start the
	// HTTP server on our own. In the end, tusd will listen on
	// and accept request at http://localhost:8080/files
	http.Handle("files/", http.StripPrefix("files/", handler))
	err = http.ListenAndServe(":8080", nil)
	if err != nil {
		panic("Unable to listen: %s", err)
	}
}

Implementing own storages

The tusd server is built to be as flexible as possible and to allow the use of different upload storage mechanisms. By default the tusd binary includes filestore which will save every upload to a specific directory on disk.

If you have different requirements, you can build your own storage backend which will save the files to S3, a remote FTP server or similar. Doing so is as simple as implementing the tusd.DataStore interface and using the new struct in the configuration object. Please consult the documentation about detailed information about the required methods.

Running the testsuite

go test -v ./...

License

This project is licensed under the MIT license, see LICENSE.txt.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedVersion  = errors.New("unsupported version")
	ErrMaxSizeExceeded     = errors.New("maximum size exceeded")
	ErrInvalidContentType  = errors.New("missing or invalid Content-Type header")
	ErrInvalidUploadLength = errors.New("missing or invalid Upload-Length header")
	ErrInvalidOffset       = errors.New("missing or invalid Upload-Offset header")
	ErrNotFound            = errors.New("upload not found")
	ErrFileLocked          = errors.New("file currently locked")
	ErrMismatchOffset      = errors.New("mismatched offset")
	ErrSizeExceeded        = errors.New("resource's size exceeded")
	ErrNotImplemented      = errors.New("feature not implemented")
	ErrUploadNotFinished   = errors.New("one of the partial uploads is not finished")
	ErrInvalidConcat       = errors.New("invalid Upload-Concat header")
	ErrModifyFinal         = errors.New("modifying a final upload is not allowed")
)

HTTP status codes sent in the response when the specific error is returned.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DataStore implementation used to store and retrieve the single uploads.
	// Must no be nil.
	DataStore DataStore
	// MaxSize defines how many bytes may be stored in one single upload. If its
	// value is is 0 or smaller no limit will be enforced.
	MaxSize int64
	// BasePath defines the URL path used for handling uploads, e.g. "/files/".
	// If no trailing slash is presented it will be added. You may specify an
	// absolute URL containing a scheme, e.g. "http://tus.io"
	BasePath string
	// Initiate the CompleteUploads channel in the Handler struct in order to
	// be notified about complete uploads
	NotifyCompleteUploads bool
	// Logger the logger to use internally
	Logger *log.Logger
}

type DataStore

type DataStore interface {
	// Create a new upload using the size as the file's length. The method must
	// return an unique id which is used to identify the upload. If no backend
	// (e.g. Riak) specifes the id you may want to use the uid package to
	// generate one. The properties Size and MetaData will be filled.
	NewUpload(info FileInfo) (id string, err error)
	// Write the chunk read from src into the file specified by the id at the
	// given offset. The handler will take care of validating the offset and
	// limiting the size of the src to not overflow the file's size. It may
	// return an os.ErrNotExist which will be interpretet as a 404 Not Found.
	// It will also lock resources while they are written to ensure only one
	// write happens per time.
	// The function call must return the number of bytes written.
	WriteChunk(id string, offset int64, src io.Reader) (int64, error)
	// Read the fileinformation used to validate the offset and respond to HEAD
	// requests. It may return an os.ErrNotExist which will be interpretet as a
	// 404 Not Found.
	GetInfo(id string) (FileInfo, error)
	// Get an io.Reader to allow downloading the file. This feature is not
	// part of the official tus specification. If this additional function
	// should not be enabled any call to GetReader should return
	// tusd.ErrNotImplemented. The length of the resource is determined by
	// retrieving the offset using GetInfo.
	// If the returned reader also implements the io.Closer interface, the
	// Close() method will be invoked once everything has been read.
	GetReader(id string) (io.Reader, error)
	// Terminate an upload so any further requests to the resource, both reading
	// and writing, must return os.ErrNotExist or similar.
	Terminate(id string) error
}

type FileInfo

type FileInfo struct {
	ID string
	// Total file size in bytes specified in the NewUpload call
	Size int64
	// Offset in bytes (zero-based)
	Offset   int64
	MetaData MetaData
	// Indicates that this is a partial upload which will later be used to form
	// a final upload by concatenation. Partial uploads should not be processed
	// when they are finished since they are only incomplete chunks of files.
	IsPartial bool
	// Indicates that this is a final upload
	IsFinal bool
	// If the upload is a final one (see IsFinal) this will be a non-empty
	// ordered slice containing the ids of the uploads of which the final upload
	// will consist after concatenation.
	PartialUploads []string
}

type Handler

type Handler struct {

	// For each finished upload the corresponding info object will be sent using
	// this unbuffered channel. The NotifyCompleteUploads property in the Config
	// struct must be set to true in order to work.
	CompleteUploads chan FileInfo
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(config Config) (*Handler, error)

Create a new handler using the given configuration.

func (*Handler) ServeHTTP

func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Implement the http.Handler interface.

type MetaData

type MetaData map[string]string

Directories

Path Synopsis
cmd
FileStore is a storage backend used as a tusd.DataStore in tusd.NewHandler.
FileStore is a storage backend used as a tusd.DataStore in tusd.NewHandler.
Package limitedstore implements a simple wrapper around existing datastores (tusd.DataStore) while limiting the used storage size.
Package limitedstore implements a simple wrapper around existing datastores (tusd.DataStore) while limiting the used storage size.

Jump to

Keyboard shortcuts

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