Skip to content

Commit

Permalink
Merge file creation function back to main file
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLeister committed Feb 17, 2024
1 parent 8dc7bc9 commit 0d8bfef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
37 changes: 0 additions & 37 deletions filehandling.go

This file was deleted.

29 changes: 29 additions & 0 deletions prosody-filer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"encoding/hex"
"flag"
"fmt"
"io"
"mime"
"net"
"net/http"
Expand Down Expand Up @@ -205,6 +206,34 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
}
}

func createFile(absFilename string, fileStorePath string, w http.ResponseWriter, r *http.Request) error {
// Make sure the directory path exists
absDirectory := filepath.Dir(absFilename)
err := os.MkdirAll(absDirectory, os.ModePerm)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return fmt.Errorf("failed to create directory %s: %s", absDirectory, err)
}

// Make sure the target file exists (MUST NOT exist before! -> O_EXCL)
targetFile, err := os.OpenFile(absFilename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
http.Error(w, "Conflict", http.StatusConflict)
return fmt.Errorf("failed to create file %s: %s", absFilename, err)
}
defer targetFile.Close()

// Copy file contents to file
_, err = io.Copy(targetFile, r.Body)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return fmt.Errorf("failed to copy file contents to %s: %s", absFilename, err)
}

w.WriteHeader(http.StatusCreated)
return nil
}

func readConfig(configFilename string, conf *Config) error {
configData, err := os.ReadFile(configFilename)
if err != nil {
Expand Down

0 comments on commit 0d8bfef

Please sign in to comment.