Skip to content

Commit

Permalink
add config param receive.exit_after_file_count
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Oct 28, 2024
1 parent 53bbdd7 commit a7d6503
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ type Config struct {
NameOfDevice string `toml:"name"`
NameLanguage string `toml:"name_language"`
Receive struct {
Directory string `toml:"directory"`
SaveUserID int `toml:"save_user_id"`
SaveGroupID int `toml:"save_group_id"`
Clipboard bool `toml:"clipboard"`
Directory string `toml:"directory"`
SaveUserID int `toml:"save_user_id"`
SaveGroupID int `toml:"save_group_id"`
Clipboard bool `toml:"clipboard"`
ExitAfterFileCount int `toml:"exit_after_file_count"`
} `toml:"receive"`
Functions struct {
HttpFileServer bool `toml:"http_file_server"`
Expand Down
13 changes: 13 additions & 0 deletions pkg/handlers/upload_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ilius/localsend-go/pkg/config"
"github.com/ilius/localsend-go/pkg/go-clipboard"
Expand All @@ -21,6 +22,7 @@ var (
sessionIDCounter = &atomic.Int64{}
fileNames = make(map[string]string) // To save the file name
fileNamesRWMutex sync.RWMutex
uploadCount = &atomic.Int64{}
)

func PrepareUploadAPIHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -126,4 +128,15 @@ func UploadAPIHandler(w http.ResponseWriter, r *http.Request) {

slog.Info("Saved file", "filePath", filePath)
w.WriteHeader(http.StatusOK)

if config.ConfigData.Receive.ExitAfterFileCount > 0 {
count := int(uploadCount.Add(1))
if count >= config.ConfigData.Receive.ExitAfterFileCount {
slog.Info("Exiting due to max recieved file count reached")
go func() {
time.Sleep(100 * time.Millisecond)
os.Exit(0)
}()
}
}
}

0 comments on commit a7d6503

Please sign in to comment.