Skip to content

Commit 086d79c

Browse files
committed
feat(Uploader): Enable chunked upload for public shares if supported
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 4288b71 commit 086d79c

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

lib/uploader/uploader.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,11 @@ export class Uploader {
474474
// We can cast here as we handled system entries in the if above
475475
const file = fileHandle as File
476476

477-
// If manually disabled or if the file is too small
478-
// TODO: support chunk uploading in public pages
477+
// @ts-expect-error TS2339 Object has no defined properties
478+
const supportsPublicChunking = getCapabilities().dav?.public_shares_chunking ?? false
479479
const maxChunkSize = getMaxChunksSize('size' in file ? file.size : undefined)
480-
const disabledChunkUpload = this._isPublic
480+
// If manually disabled or if the file is too small
481+
const disabledChunkUpload = !supportsPublicChunking
481482
|| maxChunkSize === 0
482483
|| ('size' in file && file.size < maxChunkSize)
483484

@@ -492,7 +493,7 @@ export class Uploader {
492493
logger.debug('Initializing chunked upload', { file, upload })
493494

494495
// Let's initialize a chunk upload
495-
const tempUrl = await initChunkWorkspace(encodedDestinationFile, retries)
496+
const tempUrl = await initChunkWorkspace(encodedDestinationFile, retries, this._isPublic)
496497
const chunksQueue: Array<Promise<void>> = []
497498

498499
// Generate chunks array

lib/utils/upload.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55
import type { AxiosProgressEvent, AxiosResponse, AxiosError } from 'axios'
6-
import { generateRemoteUrl } from '@nextcloud/router'
6+
import { generateRemoteUrl, getBaseUrl } from '@nextcloud/router'
77
import { getCurrentUser } from '@nextcloud/auth'
88
import axios from '@nextcloud/axios'
99
import axiosRetry, { exponentialDelay, isNetworkOrIdempotentRequestError } from 'axios-retry'
10+
import { getSharingToken } from '@nextcloud/sharing/public'
1011

1112
import logger from './logger'
1213

@@ -114,9 +115,16 @@ export const getChunk = function(file: File, start: number, length: number): Pro
114115
* Create a temporary upload workspace to upload the chunks to
115116
* @param destinationFile The file name after finishing the chunked upload
116117
* @param retries number of retries
118+
* @param isPublic whether this upload is in a public share or not
117119
*/
118-
export const initChunkWorkspace = async function(destinationFile: string | undefined = undefined, retries: number = 5): Promise<string> {
119-
const chunksWorkspace = generateRemoteUrl(`dav/uploads/${getCurrentUser()?.uid}`)
120+
export const initChunkWorkspace = async function(destinationFile: string | undefined = undefined, retries: number = 5, isPublic: boolean = false): Promise<string> {
121+
let chunksWorkspace: string
122+
if (isPublic) {
123+
chunksWorkspace = `${getBaseUrl()}/public.php/dav/uploads/${getSharingToken()}`
124+
} else {
125+
chunksWorkspace = generateRemoteUrl(`dav/uploads/${getCurrentUser()?.uid}`)
126+
}
127+
120128
const hash = [...Array(16)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')
121129
const tempWorkspace = `web-file-upload-${hash}`
122130
const url = `${chunksWorkspace}/${tempWorkspace}`

0 commit comments

Comments
 (0)