Skip to content

Commit

Permalink
add config params: receive.max_file_size, send.directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Oct 29, 2024
1 parent a7d6503 commit 27a21eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ type Config struct {
NameLanguage string `toml:"name_language"`
Receive struct {
Directory string `toml:"directory"`
MaxFileSize int `toml:"max_file_size"`
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"`
Send struct {
Directory string `toml:"directory"`
} `toml:"send"`
Functions struct {
HttpFileServer bool `toml:"http_file_server"`
LocalSendServer bool `toml:"local_send_server"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ name_language = "en"

[receive]
directory = "uploads"
max_file_size = 1000000000
save_user_id = 0
save_group_id = 0
clipboard = false

[send]
directory = "uploads"

[functions]
http_file_server = true
local_send_server = true
Expand Down
6 changes: 4 additions & 2 deletions pkg/handlers/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"os"
"path/filepath"

"github.com/ilius/localsend-go/pkg/config"
)

// DownloadHandler handles file download requests
Expand All @@ -16,8 +18,8 @@ func DownloadHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Assuming the files are stored in the "uploads" directory
filePath := filepath.Join("uploads", fileName)
// Assuming the files are stored in the configured directory
filePath := filepath.Join(config.ConfigData.Send.Directory, fileName)
file, err := os.Open(filePath)
if err != nil {
http.Error(w, fmt.Sprintf("Could not open file: %v", err), http.StatusNotFound)
Expand Down
32 changes: 22 additions & 10 deletions pkg/handlers/upload_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func UploadAPIHandler(w http.ResponseWriter, r *http.Request) {
slog.Error("Error creating directory", "err", err)
return
}

if config.ConfigData.Receive.ExitAfterFileCount > 0 {
defer checkExitAfterFileCount()
}

// Create a file
file, err := os.Create(filePath)
if err != nil {
Expand All @@ -105,6 +110,8 @@ func UploadAPIHandler(w http.ResponseWriter, r *http.Request) {
defer file.Close()

buffer := make([]byte, 2*1024*1024) // 2MB buffer
size := 0
maxSize := config.ConfigData.Receive.MaxFileSize
for {
n, err := r.Body.Read(buffer)
if err != nil && err != io.EOF {
Expand All @@ -115,28 +122,33 @@ func UploadAPIHandler(w http.ResponseWriter, r *http.Request) {
if n == 0 {
break
}
size += n
if size > maxSize {
http.Error(w, "Failed to write file", http.StatusInternalServerError)
slog.Error("Max file size reached", "size", size, "maxSize", maxSize)
}

_, err = file.Write(buffer[:n])
if err != nil {
http.Error(w, "Failed to write file", http.StatusInternalServerError)
slog.Error("Error writing file", "err", err)
return

}
}
changeFileOwnerGroup(filePath)

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

0 comments on commit 27a21eb

Please sign in to comment.