fix (cloudflare): cloudflare chunked issue

This commit is contained in:
MickaelK 2024-12-03 23:14:13 +11:00
parent f2e006b545
commit 1b47b613b6
2 changed files with 14 additions and 4 deletions

View file

@ -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;

View file

@ -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