From 98516c24827d9158e79efd4b65a481eff591107b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Thu, 14 Dec 2023 15:22:32 +0100 Subject: [PATCH] fix(uploader): encode destination MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- lib/upload.ts | 1 + lib/uploader.ts | 15 ++++++++++----- package-lock.json | 1 + package.json | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/upload.ts b/lib/upload.ts index cbfa7771..0f42c14b 100644 --- a/lib/upload.ts +++ b/lib/upload.ts @@ -1,3 +1,4 @@ +import { encodePath } from '@nextcloud/paths' import { getMaxChunksSize } from './utils/config.js' import type { AxiosResponse } from 'axios' diff --git a/lib/uploader.ts b/lib/uploader.ts index 295c7eb4..19f72fb7 100644 --- a/lib/uploader.ts +++ b/lib/uploader.ts @@ -2,6 +2,7 @@ import { CanceledError, type AxiosError, type AxiosResponse } from 'axios' import { generateRemoteUrl } from '@nextcloud/router' import { getCurrentUser } from '@nextcloud/auth' import { Folder, Permission } from '@nextcloud/files' +import { encodePath } from '@nextcloud/paths' import axios from '@nextcloud/axios' import PCancelable from 'p-cancelable' import PQueue from 'p-queue' @@ -166,7 +167,11 @@ export class Uploader { upload(destinationPath: string, file: File): PCancelable { const destinationFile = `${this.root}/${destinationPath.replace(/^\//, '')}` - logger.debug(`Uploading ${file.name} to ${destinationFile}`) + // Get the encoded source url to this object for requests purposes + const { origin } = new URL(destinationFile) + const encodedDestinationFile = origin + encodePath(destinationFile.slice(origin.length)) + + logger.debug(`Uploading ${file.name} to ${encodedDestinationFile}`) // If manually disabled or if the file is too small // TODO: support chunk uploading in public pages @@ -188,7 +193,7 @@ export class Uploader { logger.debug('Initializing chunked upload', { file, upload }) // Let's initialize a chunk upload - const tempUrl = await initChunkWorkspace(destinationFile) + const tempUrl = await initChunkWorkspace(encodedDestinationFile) const chunksQueue: Array> = [] // Generate chunks array @@ -206,7 +211,7 @@ export class Uploader { blob, upload.signal, () => this.updateStats(), - destinationFile, + encodedDestinationFile, { 'X-OC-Mtime': file.lastModified / 1000, 'OC-Total-Length': file.size, @@ -234,7 +239,7 @@ export class Uploader { method: 'MOVE', url: `${tempUrl}/.file`, headers: { - Destination: destinationFile, + Destination: encodedDestinationFile, }, }) @@ -272,7 +277,7 @@ export class Uploader { const request = async () => { try { upload.response = await uploadData( - destinationFile, + encodedDestinationFile, blob, upload.signal, () => this.updateStats(), diff --git a/package-lock.json b/package-lock.json index 8e61231f..366a5c02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "@nextcloud/files": "^3.0.0", "@nextcloud/l10n": "^2.2.0", "@nextcloud/logger": "^2.7.0", + "@nextcloud/paths": "^2.1.0", "@nextcloud/router": "^2.2.0", "buffer": "^6.0.3", "crypto-browserify": "^3.12.0", diff --git a/package.json b/package.json index 9c65c6a8..cdb55305 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "@nextcloud/files": "^3.0.0", "@nextcloud/l10n": "^2.2.0", "@nextcloud/logger": "^2.7.0", + "@nextcloud/paths": "^2.1.0", "@nextcloud/router": "^2.2.0", "buffer": "^6.0.3", "crypto-browserify": "^3.12.0",