Skip to content

Commit 4ddb579

Browse files
committed
fix(upload): handle Content-Length and File-Size headers for better size management
1 parent 4ae1464 commit 4ddb579

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

internal/stream/stream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ func (f *FileStream) CacheFullAndWriter(up *model.UpdateProgress, writer io.Writ
138138
reader = io.TeeReader(reader, writer)
139139
}
140140

141-
if f.GetSize() == 0 {
141+
if f.GetSize() < 0 {
142142
if f.peekBuff == nil {
143143
f.peekBuff = &buffer.Reader{}
144144
}
145145
// 检查是否有数据
146-
buf := make([]byte, 64*utils.KB)
146+
buf := []byte{0}
147147
n, err := io.ReadFull(reader, buf)
148148
if n > 0 {
149149
f.peekBuff.Append(buf[:n])

server/handles/fsup.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ func FsStream(c *gin.Context) {
5656
}
5757
}
5858
dir, name := stdpath.Split(path)
59-
sizeStr := c.GetHeader("Content-Length")
60-
if sizeStr == "" {
61-
sizeStr = "0"
62-
}
63-
size, err := strconv.ParseInt(sizeStr, 10, 64)
64-
if err != nil {
65-
common.ErrorResp(c, err, 400)
66-
return
59+
size := c.Request.ContentLength
60+
if size < 0 {
61+
sizeStr := c.GetHeader("File-Size")
62+
if sizeStr != "" {
63+
size, err = strconv.ParseInt(sizeStr, 10, 64)
64+
if err != nil {
65+
common.ErrorResp(c, err, 400)
66+
return
67+
}
68+
}
6769
}
6870
h := make(map[*utils.HashType]string)
6971
if md5 := c.GetHeader("X-File-Md5"); md5 != "" {

server/webdav/webdav.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/url"
1515
"os"
1616
"path"
17+
"strconv"
1718
"strings"
1819
"time"
1920

@@ -341,9 +342,19 @@ func (h *Handler) handlePut(w http.ResponseWriter, r *http.Request) (status int,
341342
if err != nil {
342343
return http.StatusForbidden, err
343344
}
345+
size := r.ContentLength
346+
if size < 0 {
347+
sizeStr := r.Header.Get("File-Size")
348+
if sizeStr != "" {
349+
size, err = strconv.ParseInt(sizeStr, 10, 64)
350+
if err != nil {
351+
return http.StatusBadRequest, err
352+
}
353+
}
354+
}
344355
obj := model.Object{
345356
Name: path.Base(reqPath),
346-
Size: r.ContentLength,
357+
Size: size,
347358
Modified: h.getModTime(r),
348359
Ctime: h.getCreateTime(r),
349360
}

0 commit comments

Comments
 (0)