From e5d946caa23d13315a2f41db4cb2cec532e750f1 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Thu, 10 Jul 2025 16:36:59 -0300 Subject: [PATCH] feat: prevent invalidating upload during transfer --- http/tus_handlers.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/http/tus_handlers.go b/http/tus_handlers.go index 3bb99f1e..5e4df034 100644 --- a/http/tus_handlers.go +++ b/http/tus_handlers.go @@ -52,6 +52,28 @@ func getActiveUploadLength(filePath string) (int64, error) { return item.Value(), nil } +func keepUploadActive(filePath string) func() { + stop := make(chan bool) + + go func() { + ticker := time.NewTicker(2 * time.Second) + defer ticker.Stop() + + for { + select { + case <-stop: + return + case <-ticker.C: + activeUploads.Touch(filePath) + } + } + }() + + return func() { + close(stop) + } +} + func tusPostHandler() handleFunc { return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { if !d.user.Perm.Create { @@ -196,6 +218,10 @@ func tusPatchHandler() handleFunc { return http.StatusForbidden, err } + // Prevent the upload from being evicted during the transfer + stop := keepUploadActive(file.RealPath()) + defer stop() + switch { case file.IsDir: return http.StatusBadRequest, fmt.Errorf("cannot upload to a directory %s", file.RealPath())