feat: prevent invalidating upload during transfer

pull/5270/head
Ramires Viana 2025-07-10 16:36:59 -03:00
parent 99edda74cf
commit e5d946caa2
1 changed files with 26 additions and 0 deletions

View File

@ -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())