/
file.go
128 lines (103 loc) · 2.65 KB
/
file.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
// Copyright 2012 by sdm. All rights reserved.
// license that can be found in the LICENSE file.
package wk
import (
"io"
"mime"
"net/http"
"path/filepath"
"strings"
"time"
)
// FileResult is wrap of static file
type FileResult struct {
Path string
//file http.File
}
// String
func (file *FileResult) String() string {
if file == nil {
return "<nil>"
}
return file.Path
}
// File return *FileResult
func File(path string) *FileResult {
return &FileResult{
Path: path,
}
}
// Execute replies to the request with the contents of FileResult.Path
func (file *FileResult) Execute(ctx *HttpContext) error {
if strings.HasPrefix(file.Path, "~/") {
http.ServeFile(ctx.Resonse, ctx.Request, ctx.Server.MapPath(file.Path[2:]))
return nil
}
http.ServeFile(ctx.Resonse, ctx.Request, file.Path)
return nil
}
// ContentType return mime type of file
func (file *FileResult) Type() string {
ctype := mime.TypeByExtension(filepath.Ext(file.Path))
// var buf [1024]byte
// n, _ := io.ReadFull(content, buf[:])
// b := buf[:n]
// ctype = DetectContentType(b)
// _, err := content.Seek(0, os.SEEK_SET)
return ctype
}
// FileStreamResult
type FileStreamResult struct {
// ContentType
ContentType string
DownloadName string
ModifyTime time.Time
// Data ?Data io.ReadSeeker
Data io.Reader
}
// FileStream return *FileStream
func FileStream(contentType, downloadName string, reader io.Reader, modtime time.Time) *FileStreamResult {
return &FileStreamResult{
ContentType: contentType,
DownloadName: downloadName,
Data: reader,
ModifyTime: modtime,
}
}
func (file *FileStreamResult) Execute(ctx *HttpContext) error {
//Set ContentType = "application/octet-stream";
if file.DownloadName != "" {
ctx.SetHeader("Content-Disposition", "attachment; filename=\""+file.DownloadName+"\";")
}
if ctype := ctx.Resonse.Header().Get("Content-Type"); ctype == "" {
ctype = file.Type()
if ctype != "" {
ctx.ContentType(ctype)
}
}
if rs, ok := file.Data.(io.ReadSeeker); ok {
http.ServeContent(ctx.Resonse, ctx.Request, file.DownloadName, file.ModifyTime, rs)
return nil
}
if checkLastModified(ctx.Resonse, ctx.Request, file.ModifyTime) {
return nil
}
io.Copy(ctx.Resonse, file.Data)
return nil
}
// ContentType return mime type of file
func (file *FileStreamResult) Type() string {
if file.ContentType != "" {
return file.ContentType
}
if file.DownloadName != "" {
return mime.TypeByExtension(filepath.Ext(file.DownloadName))
}
return ""
//return "application/octet-stream"
// var buf [1024]byte
// n, _ := io.ReadFull(content, buf[:])
// b := buf[:n]
// ctype = DetectContentType(b)
// _, err := content.Seek(0, os.SEEK_SET)
}