Files
marka/server/internal/handlers/post.go
2025-09-24 18:18:57 +02:00

48 lines
1007 B
Go

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)
}