Skip to content

Commit

Permalink
extend tus protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
peifan-tes committed Jun 12, 2020
1 parent 4ba45d8 commit 03bbe4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cover.out
data/
node_modules/
.DS_Store
.idea
10 changes: 7 additions & 3 deletions pkg/handler/unrouted_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"math"
Expand Down Expand Up @@ -414,7 +415,7 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request)
// Directly finish the upload if the upload is empty (i.e. has a size of 0).
// This statement is in an else-if block to avoid causing duplicate calls
// to finishUploadIfComplete if an upload is empty and contains a chunk.
if err := handler.finishUploadIfComplete(ctx, upload, info, r); err != nil {
if err := handler.finishUploadIfComplete(ctx, upload, info, r, w); err != nil {
handler.sendError(w, r, err)
return
}
Expand Down Expand Up @@ -676,20 +677,23 @@ func (handler *UnroutedHandler) writeChunk(ctx context.Context, upload Upload, i
handler.Metrics.incBytesReceived(uint64(bytesWritten))
info.Offset = newOffset

return handler.finishUploadIfComplete(ctx, upload, info, r)
return handler.finishUploadIfComplete(ctx, upload, info, r, w)
}

// finishUploadIfComplete checks whether an upload is completed (i.e. upload offset
// matches upload size) and if so, it will call the data store's FinishUpload
// function and send the necessary message on the CompleteUpload channel.
func (handler *UnroutedHandler) finishUploadIfComplete(ctx context.Context, upload Upload, info FileInfo, r *http.Request) error {
func (handler *UnroutedHandler) finishUploadIfComplete(ctx context.Context, upload Upload, info FileInfo, r *http.Request, rw http.ResponseWriter) error {
// If the upload is completed, ...
if !info.SizeIsDeferred && info.Offset == info.Size {
// ... allow custom mechanism to finish and cleanup the upload
if err := upload.FinishUpload(ctx); err != nil {
return err
}

rw.Header().Set("Tus-Finished", "true")
rw.Header().Set("Tus-Filesize", fmt.Sprintf("%d", info.Size))

// ... send the info out to the channel
if handler.config.NotifyCompleteUploads {
handler.CompleteUploads <- newHookEvent(info, r)
Expand Down

0 comments on commit 03bbe4f

Please sign in to comment.