-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler.go
189 lines (162 loc) · 3.85 KB
/
filehandler.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"io"
"log"
"github.com/golang/groupcache/lru"
"net/http"
"os"
"path"
"time"
)
const UID_LENGTH = 20
type FileHandler struct {
uids chan string
notFound http.Handler
reaper *Reaper
cache *lru.Cache
cacheLock chan bool
}
type NotFoundInfo struct {
Path string
}
type CacheEntry struct {
data []byte
name string
lastModified time.Time
}
func NewFileHandler(notFound http.Handler) *FileHandler {
reaper := &Reaper{make(chan Life)}
go reaper.Run()
lockChan := make(chan bool, 1)
lockChan <- true
return &FileHandler{
make(chan string), notFound, reaper, lru.New(32), lockChan}
}
func IsValidUID(uid string) bool {
return len(uid) == UID_LENGTH*2
}
func MakePathFromUID(uid string) (string, bool) {
if !IsValidUID(uid) {
return "", false
}
return path.Join("data", string(uid[0]), string(uid[1]), uid), true
}
func (f *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// File will be retrieved if possible
uid := r.URL.Path
if !IsValidUID(uid) {
if DEBUG {
log.Println(uid, " is not a valid UID")
}
f.notFound.ServeHTTP(w, r)
return
}
f.handleGet(w, r, uid)
case "POST":
// File will be uploaded
uid := <-f.uids
f.cachedPost(w, r, uid)
// Write it or something
}
}
func (f *FileHandler) GenerateUIDs() {
for {
uid := make([]byte, UID_LENGTH)
io.ReadFull(rand.Reader, uid)
f.uids <- hex.EncodeToString(uid)
}
}
func (f *FileHandler) handleGet(w http.ResponseWriter, r *http.Request, uid string) {
filePath, ok := MakePathFromUID(uid)
if !ok {
f.notFound.ServeHTTP(w, r)
return
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
f.notFound.ServeHTTP(w, r)
return
}
if f.cachedGet(w, r, uid) {
return
}
<- f.cacheLock
// Always unlock cache on return
defer func() {
f.cacheLock <- true
}()
// Check to see if during the time required to claim the cache
// the value has been cached by someone else
if f.cachedGet(w, r, uid) {
return
}
file, err := os.Open(filePath)
if err != nil {
f.notFound.ServeHTTP(w, r)
return
}
defer file.Close()
stat, err := os.Stat(filePath)
if err != nil {
f.notFound.ServeHTTP(w, r)
log.Fatal(err)
return
}
if DEBUG {
log.Println("cache load")
}
buf := bytes.NewBuffer(make([]byte, 0, stat.Size()))
io.Copy(buf, file)
f.cache.Add(uid, &CacheEntry{buf.Bytes(), uid, stat.ModTime()})
http.ServeContent(w, r, uid, stat.ModTime(), bytes.NewReader(buf.Bytes()))
}
func (f *FileHandler) cachedGet(w http.ResponseWriter, r *http.Request, uid string) bool {
cachebuf, ok := f.cache.Get(uid)
if ok {
cacheEntry, ok := cachebuf.(*CacheEntry)
if ok {
http.ServeContent(w, r, uid, cacheEntry.lastModified, bytes.NewReader(cacheEntry.data))
return true
} else if DEBUG {
log.Println("cache miss: wrong type: ", cachebuf)
}
}
return false
}
func (f *FileHandler) cachedPost(w http.ResponseWriter, r *http.Request, uid string) {
filePath, ok := MakePathFromUID(uid)
if !ok {
if DEBUG {
log.Println("invalid uid", uid);
}
http.Error(w, "unable to create file", 500)
return
}
os.MkdirAll(path.Dir(filePath), 0775)
file, err := os.Create(filePath)
if err != nil {
if DEBUG {
log.Println(err)
}
http.Error(w, "unable to create file", 500)
return
}
if r.ContentLength > 10*1024*1024 {
http.Error(w, "Sorry, files are limited to 10 MB", 500)
return
}
buf := bytes.NewBuffer(make([]byte, 0, r.ContentLength))
io.Copy(buf, r.Body)
f.cache.Add(uid, &CacheEntry{buf.Bytes(), uid, time.Now()})
io.Copy(file, buf)
file.Close()
io.WriteString(w, "/fetch/"+uid);
f.reaper.Track(file.Name(), 24*time.Hour)
if DEBUG {
log.Println("saved file ", filePath)
}
}