From f92b240a86fee1b6d8f4f36515df09e221eec272 Mon Sep 17 00:00:00 2001 From: feiniks <36756310+feiniks@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:01:43 +0800 Subject: [PATCH] Parse Content-Range to get file size (#775) Co-authored-by: Heran Yang --- fileserver/fileop.go | 3 +++ server/upload-file.c | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/fileserver/fileop.go b/fileserver/fileop.go index e32e783..2aa1610 100644 --- a/fileserver/fileop.go +++ b/fileserver/fileop.go @@ -1681,6 +1681,9 @@ func parseUploadHeaders(r *http.Request) (*recvData, *appError) { if contentLen < 0 { contentLen = 0 } + if fsm.fsize > 0 { + contentLen = fsm.fsize + } } if err := checkQuotaByContentLength(r, repoID, contentLen); err != nil { return nil, err diff --git a/server/upload-file.c b/server/upload-file.c index e5f48f6..f2a67a0 100755 --- a/server/upload-file.c +++ b/server/upload-file.c @@ -2572,8 +2572,20 @@ upload_headers_cb (evhtp_request_t *req, evhtp_headers_t *hdr, void *arg) goto err; } + gint64 rstart = -1; + gint64 rend = -1; + gint64 fsize = -1; + if (!parse_range_val (hdr, &rstart, &rend, &fsize)) { + seaf_warning ("Invalid Seafile-Content-Range value.\n"); + err_msg = "Invalid Seafile-Content-Range"; + goto err; + } + if (method == htp_method_POST || method == htp_method_PUT) { gint64 content_len = get_content_length (req); + if (fsize > 0) { + content_len = fsize; + } // Check whether the file to be uploaded would exceed the quota before receiving the body, in order to avoid unnecessarily receiving the body. // After receiving the body, the quota is checked again to handle cases where the Content-Length in the request header is missing, which could make the initial quota check inaccurate. if (seaf_quota_manager_check_quota_with_delta (seaf->quota_mgr, @@ -2612,15 +2624,6 @@ upload_headers_cb (evhtp_request_t *req, evhtp_headers_t *hdr, void *arg) pthread_mutex_unlock (&pg_lock); } - gint64 rstart = -1; - gint64 rend = -1; - gint64 fsize = -1; - if (!parse_range_val (hdr, &rstart, &rend, &fsize)) { - seaf_warning ("Invalid Seafile-Content-Range value.\n"); - err_msg = "Invalid Seafile-Content-Range"; - goto err; - } - fsm = g_new0 (RecvFSM, 1); fsm->boundary = boundary; fsm->repo_id = repo_id;