From 5900f7403002c4ad2448a03841261be3ec69f4a0 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 11 Oct 2023 10:14:17 +0200 Subject: [PATCH] Set proper headers in upload requests See https://docs.nextcloud.com/server/latest/developer_manual/client_apis/WebDAV/basic.html#request-headers Signed-off-by: Louis Chemineau --- lib/uploader.ts | 23 +++++++++++++++++++++-- lib/utils/upload.ts | 7 +++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/uploader.ts b/lib/uploader.ts index 9cfe662b..c9bbd7ed 100644 --- a/lib/uploader.ts +++ b/lib/uploader.ts @@ -201,7 +201,17 @@ export class Uploader { // Init request queue const request = () => { - return uploadData(`${tempUrl}/${chunk+1}`, blob, upload.signal, () => this.updateStats(), destinationFile) + return uploadData( + `${tempUrl}/${chunk+1}`, + blob, + upload.signal, + () => this.updateStats(), + destinationFile, + { + 'X-OC-Mtime': file.lastModified, + 'OC-Total-Length': file.size, + } + ) // Update upload progress on chunk completion .then(() => { upload.uploaded = upload.uploaded + maxChunkSize }) .catch((error) => { @@ -261,7 +271,16 @@ export class Uploader { const blob = await getChunk(file, 0, upload.size) const request = async () => { try { - upload.response = await uploadData(destinationFile, blob, upload.signal, () => this.updateStats()) + upload.response = await uploadData( + destinationFile, + blob, + upload.signal, + () => this.updateStats(), + undefined, + { + 'X-OC-Mtime': file.lastModified, + } + ) // Update progress upload.uploaded = upload.size diff --git a/lib/utils/upload.ts b/lib/utils/upload.ts index 5716af26..db91ad76 100644 --- a/lib/utils/upload.ts +++ b/lib/utils/upload.ts @@ -12,7 +12,7 @@ type UploadData = Blob | (() => Promise) /** * Upload some data to a given path */ -export const uploadData = async function(url: string, uploadData: UploadData, signal: AbortSignal, onUploadProgress = () => {}, destinationFile: string | undefined = undefined): Promise { +export const uploadData = async function(url: string, uploadData: UploadData, signal: AbortSignal, onUploadProgress = () => {}, destinationFile: string | undefined = undefined, headers: any = undefined): Promise { let data: Blob if (uploadData instanceof Blob) { @@ -21,7 +21,10 @@ export const uploadData = async function(url: string, uploadData: UploadData, si data = await uploadData() } - const headers = destinationFile ? { Destination: destinationFile } : undefined + if (destinationFile) { + headers ??= {} + headers.Destination = destinationFile + } return await axios.request({ method: 'PUT',