mirror of
https://github.com/restic/rest-server.git
synced 2026-09-25 13:31:31 -07:00
Miscellaneous handlers.go changes
This commit is contained in:
+9
-15
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/zcalusic/restic-server/fs"
|
"github.com/zcalusic/restic-server/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context contains repository meta-data.
|
// Context contains repository metadata.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
@@ -23,12 +23,7 @@ type Context struct {
|
|||||||
// stored in f and returns the http.HandlerFunc.
|
// stored in f and returns the http.HandlerFunc.
|
||||||
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
username, password, ok := r.BasicAuth()
|
if username, password, ok := r.BasicAuth(); !ok || !f.Validate(username, password) {
|
||||||
if !ok {
|
|
||||||
http.Error(w, "401 unauthorized", 401)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !f.Validate(username, password) {
|
|
||||||
http.Error(w, "401 unauthorized", 401)
|
http.Error(w, "401 unauthorized", 401)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -74,8 +69,7 @@ func SaveConfig(c *Context) http.HandlerFunc {
|
|||||||
http.Error(w, "400 bad request", 400)
|
http.Error(w, "400 bad request", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
errw := ioutil.WriteFile(config, bytes, 0600)
|
if err := ioutil.WriteFile(config, bytes, 0600); err != nil {
|
||||||
if errw != nil {
|
|
||||||
http.Error(w, "500 internal server error", 500)
|
http.Error(w, "500 internal server error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -185,20 +179,20 @@ func SaveBlob(c *Context) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if _, err := io.Copy(tf, r.Body); err != nil {
|
if _, err := io.Copy(tf, r.Body); err != nil {
|
||||||
http.Error(w, "400 bad request", 400)
|
|
||||||
tf.Close()
|
tf.Close()
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
|
http.Error(w, "400 bad request", 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := tf.Sync(); err != nil {
|
if err := tf.Sync(); err != nil {
|
||||||
http.Error(w, "500 internal server error", 500)
|
|
||||||
tf.Close()
|
tf.Close()
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
|
http.Error(w, "500 internal server error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := tf.Close(); err != nil {
|
if err := tf.Close(); err != nil {
|
||||||
http.Error(w, "500 internal server error", 500)
|
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
|
http.Error(w, "500 internal server error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,8 +202,9 @@ func SaveBlob(c *Context) http.HandlerFunc {
|
|||||||
path := filepath.Join(c.path, dir, name)
|
path := filepath.Join(c.path, dir, name)
|
||||||
|
|
||||||
if err := os.Rename(tmp, path); err != nil {
|
if err := os.Rename(tmp, path); err != nil {
|
||||||
http.Error(w, "500 internal server error", 500)
|
|
||||||
os.Remove(tmp)
|
os.Remove(tmp)
|
||||||
|
os.Remove(path)
|
||||||
|
http.Error(w, "500 internal server error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +224,7 @@ func DeleteBlob(c *Context) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
path := filepath.Join(c.path, dir, name)
|
path := filepath.Join(c.path, dir, name)
|
||||||
|
|
||||||
err := os.Remove(path)
|
if err := os.Remove(path); err != nil {
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "500 internal server error", 500)
|
http.Error(w, "500 internal server error", 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user