-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch.go
More file actions
61 lines (58 loc) · 1.25 KB
/
Copy pathpatch.go
File metadata and controls
61 lines (58 loc) · 1.25 KB
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
package temp
import (
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func patch(w http.ResponseWriter, r *http.Request) {
uuid := strings.Split(r.URL.EscapedPath(), "/")[2]
tempinfo, e := readFromFile(uuid)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusNotFound)
return
}
infoFile := os.Getenv("STORAGE_ROOT") + "/temp/" + uuid
datFile := infoFile + ".dat"
f, e := os.OpenFile(datFile, os.O_WRONLY|os.O_APPEND, 0)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
defer f.Close()
_, e = io.Copy(f, r.Body)
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
info, e := f.Stat()
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusInternalServerError)
return
}
actual := info.Size()
if actual > tempinfo.Size {
os.Remove(datFile)
os.Remove(infoFile)
log.Println("actual size", actual, "exceeds", tempinfo.Size)
w.WriteHeader(http.StatusInternalServerError)
}
}
func readFromFile(uuid string) (*tempInfo, error) {
f, e := os.Open(os.Getenv("STORAGE_ROOT") + "/temp/" + uuid)
if e != nil {
return nil, e
}
defer f.Close()
b, _ := ioutil.ReadAll(f)
var info tempInfo
json.Unmarshal(b, &info)
return &info, nil
}