-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
115 lines (94 loc) · 2.93 KB
/
handlers.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"github.com/golang/glog"
)
// Handles and processes the home page
func home(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, template.HTML(fmt.Sprintf(`http://%s/`, r.Host)))
}
// Upload a file, save and attribute a hash
func upload(w http.ResponseWriter, r *http.Request) {
glog.Info("Upload request recieved")
var uuid string = GenerateUUID()
var filepath string = fmt.Sprintf("./storage/%s/", uuid)
// Prepare to get the file
file, header, err := r.FormFile("file")
defer func() {
file.Close()
glog.Infof(`File "%s" closed.`, header.Filename)
}()
if err != nil {
glog.Errorf("Error retrieving file.")
glog.Errorf("Error: %s", err.Error())
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Bad request. Error retrieving file.")
return
}
// Creates directory with UUID
_, err = os.Stat(filepath)
for !os.IsNotExist(err) {
uuid = GenerateUUID()
filepath := fmt.Sprintf("./storage/%s/", uuid)
_, err = os.Stat(filepath)
}
if err := os.MkdirAll(filepath, 0777); err != nil {
glog.Error("Error saving file on server...")
glog.Errorf("Error: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "No storage available.")
return
}
f, err := os.OpenFile(path.Join(filepath, header.Filename), os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
glog.Errorf("Error creating file.")
glog.Errorf("Error: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error creating file.")
return
}
defer f.Close()
if _, err := io.Copy(f, file); err != nil {
glog.Errorf("Error writing file.")
glog.Errorf("Error: %s", err.Error())
w.WriteHeader(http.StatusInsufficientStorage)
fmt.Fprintf(w, "Insufficient Storage. Error storing file.")
return
}
// All good
fmt.Fprintf(w, "OK, Successfully Uploaded\n http://%s/%s\n", r.Host, uuid)
}
// Gets the file using the provided UUID on the URL
func getFile(w http.ResponseWriter, r *http.Request) {
glog.Info("Retrieve request received")
var uuid string = strings.Replace(r.URL.Path[1:], "/", "", -1)
var path string = fmt.Sprintf("./storage/%s/", uuid)
glog.Infof(`Route "%s"`, r.URL.Path)
glog.Infof(`Retrieving UUID "%s"`, uuid)
glog.Infof(`Retrieving Path "%s"`, path)
files, err := ioutil.ReadDir(path)
if err != nil {
glog.Errorf(`Error walking filepath "%s"`, path)
glog.Errorf("Error: %s", err.Error())
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "File Not Found.")
return
}
if len(files) <= 0 {
glog.Errorf(`No files in directory "%s"`, path)
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "File Not Found.")
return
}
var filename = files[0].Name()
glog.Infof(`Retrieving Filename "%s"`, fmt.Sprintf("./%s", filename))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
http.ServeFile(w, r, fmt.Sprintf("./%s/%s", path, filename))
}