add uploads channel to handler

This commit is contained in:
Acconut 2015-04-18 20:56:24 +02:00
parent 21a6280ef8
commit 152a8dad72
2 changed files with 21 additions and 1 deletions

View File

@ -70,6 +70,9 @@ type Handler struct {
basePath string basePath string
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
Uploads chan FileInfo
} }
// Create a new handler using the given configuration. // Create a new handler using the given configuration.
@ -99,6 +102,7 @@ func NewHandler(config Config) (*Handler, error) {
isBasePathAbs: uri.IsAbs(), isBasePathAbs: uri.IsAbs(),
routeHandler: mux, routeHandler: mux,
locks: make(map[string]bool), locks: make(map[string]bool),
Uploads: make(chan FileInfo),
} }
mux.Post("", http.HandlerFunc(handler.postFile)) mux.Post("", http.HandlerFunc(handler.postFile))
@ -321,7 +325,14 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) {
} }
// Send new offset to client // Send new offset to client
w.Header().Set("Upload-Offset", strconv.FormatInt(offset+bytesWritten, 10)) newOffset := offset + bytesWritten
w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10))
// If the upload is completed, send the info out to the channel
if newOffset == info.Size {
info.Size = newOffset
handler.Uploads <- info
}
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }

View File

@ -67,6 +67,15 @@ func main() {
address := httpHost + ":" + httpPort address := httpHost + ":" + httpPort
stdout.Printf("Using %s as address to listen.\n", address) stdout.Printf("Using %s as address to listen.\n", address)
go func() {
for {
select {
case info := <-handler.Uploads:
stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size)
}
}
}()
http.Handle(basepath, http.StripPrefix(basepath, handler)) http.Handle(basepath, http.StripPrefix(basepath, handler))
err = http.ListenAndServe(address, nil) err = http.ListenAndServe(address, nil)
if err != nil { if err != nil {