diff options
author | ache <ache@ache.one> | 2019-02-16 21:19:43 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2019-02-16 21:19:43 +0100 |
commit | f6d498043c774b1f3b4da9705e71b6a204dd29ba (patch) | |
tree | 172b02701c2e5c0f0257401dfd6248a0e679f2ad | |
parent | Handle Move method (diff) |
Method delete
-rw-r--r-- | dir.html | 4 | ||||
-rw-r--r-- | server.go | 29 |
2 files changed, 27 insertions, 6 deletions
@@ -7,13 +7,13 @@ <h1>Index of {{ .Name }}</h1><br> <section id="main"> <pre>{{range .ListFiles}} - <a href="{{ .Name }}">{{ .Name }}</a>{{ .Size }}{{end}} + <a href="{{ .Path }}">{{ .Name }}</a>{{ .Size }}{{end}} </pre> </section> <section id="nav"> <h2>List of directories</h2> <pre>{{range .ListCols}} - <a href="{{ .Name }}">{{ .Name }}</a>{{end}} + <a href="{{ .Path }}">{{ .Name }}</a>{{end}} </pre> </section> <section id="dav"> @@ -14,12 +14,15 @@ import ( var tmpl *template.Template +func getPath( r *http.Request ) string { + if r.URL.Path == "/" { + return "." + } + return r.URL.Path[1:] +} func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { + filename := getPath(r) - filename := r.URL.Path[1:] - if filename == "" { - filename = "." - } fmt.Printf("Trying to get [%s]\n", filename) stat, err := os.Stat(filename) @@ -96,6 +99,24 @@ func handleGet( w http.ResponseWriter, r *http.Request, headOnly bool) { } } func handleDelete( w http.ResponseWriter, r *http.Request) { + filename := getPath(r) + + _, err := os.Stat(filename) + + if err != nil { + if os.IsNotExist(err) { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Bro, they is nothing here") + } else { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Shit happen") + } + } + err = os.Remove(filename) + if err != nil { + w.WriteHeader(http.StatusConflict) + fmt.Fprintf(w, "Error : %s\n", err.Error()) + } } func handlePut( w http.ResponseWriter, r *http.Request) { filename := r.URL.Path[1:] |