-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
38 lines (35 loc) · 987 Bytes
/
writer.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
package pick
import (
"encoding/json"
"github.com/hopeio/utils/errors/errcode"
"github.com/hopeio/utils/log"
httpi "github.com/hopeio/utils/net/http"
http_fs "github.com/hopeio/utils/net/http/fs"
"go.uber.org/zap"
"io"
"net/http"
"reflect"
)
func ResWriteReflect(w http.ResponseWriter, traceId string, result []reflect.Value) {
if !result[1].IsNil() {
err := errcode.ErrHandle(result[1].Interface())
log.Errorw(err.Error(), zap.String(log.FieldTraceId, traceId))
json.NewEncoder(w).Encode(err)
return
}
if info, ok := result[0].Interface().(*http_fs.File); ok {
header := w.Header()
header.Set(httpi.HeaderContentType, httpi.ContentBinaryHeaderValue)
header.Set(httpi.HeaderContentDisposition, "attachment;filename="+info.Name)
io.Copy(w, info.File)
if flusher, canFlush := w.(http.Flusher); canFlush {
flusher.Flush()
}
info.File.Close()
return
}
json.NewEncoder(w).Encode(httpi.ResAnyData{
Msg: "OK",
Data: result[0].Interface(),
})
}