From 1b47b613b6f1bb049762fbf489b4a7a9c5596fc3 Mon Sep 17 00:00:00 2001 From: MickaelK Date: Tue, 3 Dec 2024 23:14:13 +1100 Subject: [PATCH] fix (cloudflare): cloudflare chunked issue --- public/assets/pages/filespage/ctrl_upload.js | 16 +++++++++++++--- server/ctrl/files.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/public/assets/pages/filespage/ctrl_upload.js b/public/assets/pages/filespage/ctrl_upload.js index e475aa94..749b3f9c 100644 --- a/public/assets/pages/filespage/ctrl_upload.js +++ b/public/assets/pages/filespage/ctrl_upload.js @@ -335,13 +335,17 @@ function workerImplFile({ progress, speed }) { async prepareJob({ file, path, virtual }) { const chunkSize = (window.CONFIG["upload_chunk_size"] || 0) *1024*1024; const numberOfChunks = Math.ceil(file.size / chunkSize); + const headersNoCache = { + "Cache-Control": "no-store", + "Pragma": "no-cache", + }; // Case1: basic upload if (chunkSize === 0 || numberOfChunks === 0 || numberOfChunks === 1) { try { await executeHttp.call(this, toHref(`/api/files/cat?path=${encodeURIComponent(path)}`), { method: "POST", - headers: {}, + headers: { ...headersNoCache }, body: file, progress, speed, @@ -359,7 +363,10 @@ function workerImplFile({ progress, speed }) { try { let resp = await executeHttp.call(this, toHref(`/api/files/cat?path=${encodeURIComponent(path)}&proto=tus`), { method: "POST", - headers: { "Upload-Length": file.size }, + headers: { + "Upload-Length": file.size, + ...headersNoCache, + }, body: null, progress: (n) => progress(n), speed, @@ -372,7 +379,10 @@ function workerImplFile({ progress, speed }) { const offset = chunkSize * i; resp = await executeHttp.call(this, url, { method: "PATCH", - headers: { "Upload-Offset": offset }, + headers: { + "Upload-Offset": offset, + ...headersNoCache, + }, body: file.slice(offset, offset + chunkSize), progress: (p) => { const chunksAlreadyDownloaded = i * chunkSize; diff --git a/server/ctrl/files.go b/server/ctrl/files.go index 7a199557..4ac7f437 100644 --- a/server/ctrl/files.go +++ b/server/ctrl/files.go @@ -496,6 +496,7 @@ func FileSave(ctx *App, res http.ResponseWriter, req *http.Request) { return } if proto == "tus" && req.Method == http.MethodPatch { + h.Set("Connection", "Close") requestOffset, err := strconv.ParseUint(req.Header.Get("Upload-Offset"), 10, 0) if err != nil { SendErrorResult(res, ErrNotValid) @@ -530,7 +531,6 @@ func FileSave(ctx *App, res http.ResponseWriter, req *http.Request) { } chunkedUploadCache.Del(ctx.Session) } - h.Set("Connection", "Close") h.Set("Upload-Offset", fmt.Sprintf("%d", newOffset)) res.WriteHeader(http.StatusNoContent) return