feat: some updates
This commit is contained in:
47
server/internal/handlers/post.go
Normal file
47
server/internal/handlers/post.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user