-
Notifications
You must be signed in to change notification settings - Fork 8
/
removehandler.go
52 lines (44 loc) · 1.06 KB
/
removehandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
// standard
"encoding/json"
"fmt"
"net/http"
"os"
// external
"github.com/gorilla/mux"
"github.com/jheise/yaramsg"
)
func RemoveHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filename := vars["filename"]
info.Printf("Removing %s\n", filename)
// check that file exists with traversal safe function
fileexists, err := fileExists(filename)
if err != nil {
elog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if fileexists != true {
http.Error(w, fmt.Sprintf("%s does not exist", filename), http.StatusNotFound)
return
}
response := new(yaramsg.RemoveResponse)
response.Message = "Removing " + filename
err = os.Remove(fullpath(filename))
if err != nil {
elog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else {
response.Result = true
}
output, err := json.Marshal(response)
if err != nil {
elog.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(output))
}