rename uploads channel and add option

This commit is contained in:
Acconut 2015-04-26 00:46:53 +02:00
parent 152a8dad72
commit 30b913d544
2 changed files with 21 additions and 15 deletions

View File

@ -61,6 +61,9 @@ type Config struct {
// If no trailing slash is presented it will be added. You may specify an // If no trailing slash is presented it will be added. You may specify an
// absolute URL containing a scheme, e.g. "http://tus.io" // absolute URL containing a scheme, e.g. "http://tus.io"
BasePath string BasePath string
// Initiate the CompleteUploads channel in the Handler struct in order to
// be notified about complete uploads
NotifyCompleteUploads bool
} }
type Handler struct { type Handler struct {
@ -71,8 +74,10 @@ type Handler struct {
routeHandler http.Handler routeHandler http.Handler
locks map[string]bool locks map[string]bool
// For each finished upload the corresponding info object will be sent // For each finished upload the corresponding info object will be sent using
Uploads chan FileInfo // this unbuffered channel. The NotifyCompleteUploads property in the Config
// struct must be set to true in order to work.
CompleteUploads chan FileInfo
} }
// Create a new handler using the given configuration. // Create a new handler using the given configuration.
@ -96,13 +101,13 @@ func NewHandler(config Config) (*Handler, error) {
mux := pat.New() mux := pat.New()
handler := &Handler{ handler := &Handler{
config: config, config: config,
dataStore: config.DataStore, dataStore: config.DataStore,
basePath: base, basePath: base,
isBasePathAbs: uri.IsAbs(), isBasePathAbs: uri.IsAbs(),
routeHandler: mux, routeHandler: mux,
locks: make(map[string]bool), locks: make(map[string]bool),
Uploads: make(chan FileInfo), CompleteUploads: make(chan FileInfo),
} }
mux.Post("", http.HandlerFunc(handler.postFile)) mux.Post("", http.HandlerFunc(handler.postFile))
@ -329,9 +334,9 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10)) w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))
// If the upload is completed, send the info out to the channel // If the upload is completed, send the info out to the channel
if newOffset == info.Size { if handler.config.NotifyCompleteUploads && newOffset == info.Size {
info.Size = newOffset info.Size = newOffset
handler.Uploads <- info handler.CompleteUploads <- info
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)

View File

@ -56,9 +56,10 @@ func main() {
stdout.Printf("Using %.2fMB as maximum size.\n", float64(maxSize)/1024/1024) stdout.Printf("Using %.2fMB as maximum size.\n", float64(maxSize)/1024/1024)
handler, err := tusd.NewHandler(tusd.Config{ handler, err := tusd.NewHandler(tusd.Config{
MaxSize: maxSize, MaxSize: maxSize,
BasePath: "files/", BasePath: "files/",
DataStore: store, DataStore: store,
NotifyCompleteUploads: true,
}) })
if err != nil { if err != nil {
stderr.Fatalf("Unable to create handler: %s", err) stderr.Fatalf("Unable to create handler: %s", err)
@ -70,7 +71,7 @@ func main() {
go func() { go func() {
for { for {
select { select {
case info := <-handler.Uploads: case info := <-handler.CompleteUploads:
stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size) stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size)
} }
} }