Skip to content

Commit

Permalink
fix(uploader): encode destination
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 14, 2023
1 parent 0f3a65b commit e1e1d57
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getMaxChunksSize } from './utils/config.js'
import type { AxiosResponse } from 'axios'
import { getMaxChunksSize } from './utils/config.js'

export enum Status {
INITIALIZED = 0,
Expand Down
15 changes: 10 additions & 5 deletions lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -166,7 +167,11 @@ export class Uploader {
upload(destinationPath: string, file: File): PCancelable<Upload> {
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
Expand All @@ -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<Promise<any>> = []

// Generate chunks array
Expand All @@ -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,
Expand Down Expand Up @@ -234,7 +239,7 @@ export class Uploader {
method: 'MOVE',
url: `${tempUrl}/.file`,
headers: {
Destination: destinationFile,
Destination: encodedDestinationFile,
},
})

Expand Down Expand Up @@ -272,7 +277,7 @@ export class Uploader {
const request = async () => {
try {
upload.response = await uploadData(
destinationFile,
encodedDestinationFile,
blob,
upload.signal,
() => this.updateStats(),
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e1e1d57

Please sign in to comment.