feat: some updates

This commit is contained in:
Max Richter
2025-09-24 18:18:57 +02:00
parent b3c01bb43d
commit 26c303d9cf
32 changed files with 464 additions and 263 deletions

View File

@@ -0,0 +1,47 @@
package handlers
import (
"io"
"net/http"
"os"
"path/filepath"
"git.max-richter.dev/max/marka/server/internal/httpx"
)
func (h *File) post(w http.ResponseWriter, r *http.Request, target string) {
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
httpx.WriteError(w, http.StatusInternalServerError, err)
return
}
tmpPath := target + ".tmp~"
f, err := os.Create(tmpPath)
if err != nil {
httpx.WriteError(w, http.StatusInternalServerError, err)
return
}
_, copyErr := io.Copy(f, r.Body)
closeErr := f.Close()
if copyErr != nil {
os.Remove(tmpPath)
httpx.WriteError(w, http.StatusInternalServerError, copyErr)
return
}
if closeErr != nil {
os.Remove(tmpPath)
httpx.WriteError(w, http.StatusInternalServerError, closeErr)
return
}
if err := os.Rename(tmpPath, target); err != nil {
os.Remove(tmpPath)
httpx.WriteError(w, http.StatusInternalServerError, err)
return
}
w.Header().Set("Location", r.URL.Path)
w.WriteHeader(http.StatusCreated)
}