Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uploader): encode destination #996

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 11 additions & 6 deletions lib/uploader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CanceledError, type AxiosError, type AxiosResponse } from 'axios'
import { encodePath } from '@nextcloud/paths'
import { Folder, Permission } from '@nextcloud/files'
import { generateRemoteUrl } from '@nextcloud/router'
import { getCurrentUser } from '@nextcloud/auth'
import { Folder, Permission } from '@nextcloud/files'
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
Loading