@@ -4,8 +4,10 @@ import (
4
4
"bytes"
5
5
"encoding/json"
6
6
"fmt"
7
+ "mime/multipart"
7
8
"net/http"
8
9
"net/url"
10
+ "os"
9
11
"path/filepath"
10
12
"strings"
11
13
"time"
@@ -63,11 +65,19 @@ func (s *Server) handleUpload(c *fiber.Ctx) error {
63
65
if err != nil {
64
66
return fiber .ErrBadRequest
65
67
}
68
+
69
+ size , _ := multipartFileSize (fr )
70
+ if size == 0 {
71
+ // fallback
72
+ size = f .Size
73
+ }
74
+
66
75
defer fr .Close ()
67
76
blobs = append (blobs , & file {
68
77
Name : f .Filename ,
69
78
MIMEType : mediaTypeOrDefault (f .Header ),
70
79
Reader : fr ,
80
+ Size : size ,
71
81
})
72
82
}
73
83
}
@@ -96,11 +106,11 @@ func (s *Server) handlePut(c *fiber.Ctx) error {
96
106
return fiber .ErrBadRequest
97
107
}
98
108
99
- var err error
100
- fn , err = url .QueryUnescape (fn )
101
- if err != nil {
102
- return err
103
- }
109
+ var err error
110
+ fn , err = url .QueryUnescape (fn )
111
+ if err != nil {
112
+ return err
113
+ }
104
114
105
115
body := c .Body ()
106
116
if len (body ) == 0 {
@@ -153,12 +163,12 @@ func (s *Server) handleCat(c *fiber.Ctx) error {
153
163
// return fiber.ErrBadRequest
154
164
// }
155
165
156
- filename , err := url .QueryUnescape (c .Params ("file" ))
157
- if err != nil {
158
- return err
159
- }
166
+ filename , err := url .QueryUnescape (c .Params ("file" ))
167
+ if err != nil {
168
+ return err
169
+ }
160
170
161
- path := filepath .Join (cid , filename )
171
+ path := filepath .Join (cid , filename )
162
172
src , err := s .ipc .CatStream (path )
163
173
if err != nil {
164
174
zap .S ().Errorf ("ipfs cat err: %s" , err )
@@ -168,3 +178,21 @@ func (s *Server) handleCat(c *fiber.Ctx) error {
168
178
c .SendStream (src )
169
179
return nil
170
180
}
181
+
182
+ // ref: https://medium.com/dtoebe/how-to-get-a-multipart-file-size-in-golang-3ab4ab4c3e3
183
+ func multipartFileSize (fr multipart.File ) (int64 , error ) {
184
+ switch t := fr .(type ) {
185
+ case * os.File :
186
+ f , err := t .Stat ()
187
+ if err != nil {
188
+ return 0 , err
189
+ }
190
+ return f .Size (), nil
191
+ default :
192
+ sr , err := fr .Seek (0 , 0 )
193
+ if err != nil {
194
+ return 0 , err
195
+ }
196
+ return sr , nil
197
+ }
198
+ }
0 commit comments