-
Notifications
You must be signed in to change notification settings - Fork 56
/
content.go
37 lines (31 loc) · 966 Bytes
/
content.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
package serverHandler
import (
"../util"
"net/http"
"strconv"
"time"
)
func (h *handler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
header := w.Header()
header.Set("X-Content-Type-Options", "nosniff")
if data.IsDownload {
header.Set("Content-Disposition", "attachment; filename*=UTF-8''"+data.ItemName)
}
item := data.Item
file := data.File
if needResponseBody(r.Method) {
http.ServeContent(w, r, item.Name(), item.ModTime(), file)
return
}
ctype, err := util.GetContentType(item.Name(), file)
if err == nil {
header.Set("Accept-Ranges", "bytes")
header.Set("Content-Type", ctype)
header.Set("Content-Length", strconv.FormatInt(item.Size(), 10))
header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
header.Set("Last-Modified", item.ModTime().UTC().Format(http.TimeFormat))
} else if data.Status == http.StatusOK {
data.Status = http.StatusInternalServerError
}
w.WriteHeader(data.Status)
}