-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
51 lines (44 loc) · 1.08 KB
/
delete.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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
)
func deleteSpecifiedFileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
handleSpecifiedFileDeletion(w, r)
} else {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func handleSpecifiedFileDeletion(w http.ResponseWriter, r *http.Request) {
filename := r.FormValue("filename")
if filename == "" {
http.Error(w, "File not specified", http.StatusBadRequest)
return
}
filePath := filepath.Join("./uploads", filename)
hash, err := calculateFileHash(filePath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
lerr := os.Remove(filePath)
if lerr != nil {
http.Error(w, lerr.Error(), http.StatusInternalServerError)
return
}
hashSlice := FileHash[hash]
for i, v := range hashSlice {
if v == filename {
hashSlice = append(hashSlice[:i], hashSlice[i+1:]...)
break
}
}
FileHash[hash] = hashSlice
if len(FileHash[hash]) == 0 {
delete(FileHash, hash)
}
fmt.Fprintf(w, "File %s removed successfully!", filename)
}