2015-12-18 22:24:12 +00:00
|
|
|
// Package tusd provides ways to accept tusd calls using HTTP.
|
2015-02-01 13:57:57 +00:00
|
|
|
package tusd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/bmizerany/pat"
|
|
|
|
)
|
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
// Handler is a ready to use handler with routing (using pat)
|
2015-02-01 13:57:57 +00:00
|
|
|
type Handler struct {
|
2015-11-29 01:33:55 +00:00
|
|
|
unroutedHandler *UnroutedHandler
|
|
|
|
routeHandler http.Handler
|
2015-04-25 22:46:53 +00:00
|
|
|
CompleteUploads chan FileInfo
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
// NewHandler creates a routed tus protocol handler. This is the simplest
|
|
|
|
// way to use tusd but may not be as configurable as you require. If you are
|
2015-12-07 21:26:35 +00:00
|
|
|
// integrating this into an existing app you may like to use tusd.NewUnroutedHandler
|
2015-12-07 20:09:47 +00:00
|
|
|
// instead. Using tusd.NewUnroutedHandler allows the tus handlers to be combined into
|
2015-11-29 01:33:55 +00:00
|
|
|
// your existing router (aka mux) directly. It also allows the GET and DELETE
|
|
|
|
// endpoints to be customized. These are not part of the protocol so can be
|
|
|
|
// changed depending on your needs.
|
2015-02-01 13:57:57 +00:00
|
|
|
func NewHandler(config Config) (*Handler, error) {
|
2015-11-29 01:33:55 +00:00
|
|
|
handler, err := NewUnroutedHandler(config)
|
2015-02-01 13:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
routedHandler := &Handler{
|
|
|
|
unroutedHandler: handler,
|
|
|
|
CompleteUploads: handler.CompleteUploads,
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mux := pat.New()
|
|
|
|
|
2015-12-07 20:10:02 +00:00
|
|
|
routedHandler.routeHandler = handler.Middleware(mux)
|
2015-02-17 13:19:56 +00:00
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
mux.Post("", http.HandlerFunc(handler.PostFile))
|
|
|
|
mux.Head(":id", http.HandlerFunc(handler.HeadFile))
|
|
|
|
mux.Add("PATCH", ":id", http.HandlerFunc(handler.PatchFile))
|
2015-03-08 00:06:39 +00:00
|
|
|
|
2015-12-26 23:44:02 +00:00
|
|
|
// Only attach the DELETE handler if the Terminate() method is provided
|
|
|
|
if _, ok := config.DataStore.(TerminaterDataStore); ok {
|
|
|
|
mux.Del(":id", http.HandlerFunc(handler.DelFile))
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:32:15 +00:00
|
|
|
// GET handler requires the GetReader() method
|
|
|
|
if _, ok := config.DataStore.(GetReaderDataStore); ok {
|
|
|
|
mux.Get(":id", http.HandlerFunc(handler.GetFile))
|
|
|
|
}
|
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
return routedHandler, nil
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-29 01:33:55 +00:00
|
|
|
// ServeHTTP Implements the http.Handler interface.
|
|
|
|
func (rHandler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rHandler.routeHandler.ServeHTTP(w, r)
|
2015-02-17 13:19:56 +00:00
|
|
|
}
|