-
Notifications
You must be signed in to change notification settings - Fork 3
/
raw.go
52 lines (42 loc) · 1.08 KB
/
raw.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 server
import (
"net/http"
"github.com/gorilla/mux"
"github.com/knoebber/dotfile/db"
)
// Sets the contents of file to response writer.
func handleRawFile(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
file, err := db.UncompressFile(db.Connection, vars["username"], vars["alias"])
if err != nil {
rawContentError(w, err)
return
}
_, err = w.Write(file.Content)
if err != nil {
rawContentError(w, err)
return
}
}
// Sets the contents of file at hash to response writer.
func handleRawUncompressedCommit(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
commit, err := db.UncompressCommit(db.Connection, vars["username"], vars["alias"], vars["hash"], nil)
if err != nil {
rawContentError(w, err)
return
}
_, err = w.Write(commit.Content)
if err != nil {
rawContentError(w, err)
return
}
return
}
func rawContentError(w http.ResponseWriter, err error) {
if db.NotFound(err) {
setError(w, err, "File not found", http.StatusNotFound)
} else {
setError(w, err, "Failed to retrieve raw content", http.StatusInternalServerError)
}
}