Skip to content

Commit f9a4071

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

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

lib/uploader/uploader.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Directory } from '../utils/fileTree.ts'
2525
import { t } from '../utils/l10n.ts'
2626
import logger from '../utils/logger.ts'
2727
import { Eta } from './eta.ts'
28+
import { generateRemoteUrl, getBaseUrl } from '@nextcloud/router'
2829

2930
export enum UploaderStatus {
3031
IDLE = 0,
@@ -477,7 +478,7 @@ export class Uploader {
477478
// If manually disabled or if the file is too small
478479
// TODO: support chunk uploading in public pages
479480
const maxChunkSize = getMaxChunksSize('size' in file ? file.size : undefined)
480-
const disabledChunkUpload = this._isPublic
481+
const disabledChunkUpload = !(getCapabilities().dav['public-shares-chunking'] ?? false)
481482
|| maxChunkSize === 0
482483
|| ('size' in file && file.size < maxChunkSize)
483484

@@ -491,8 +492,16 @@ export class Uploader {
491492
if (!disabledChunkUpload) {
492493
logger.debug('Initializing chunked upload', { file, upload })
493494

495+
let chunksWorkspace: string
496+
if (this._isPublic) {
497+
const shareToken = root!.split('/').at(-1)!
498+
chunksWorkspace = `${getBaseUrl()}/public.php/dav/uploads/${shareToken}`
499+
} else {
500+
chunksWorkspace = generateRemoteUrl(`dav/uploads/${getCurrentUser()?.uid}`)
501+
}
502+
494503
// Let's initialize a chunk upload
495-
const tempUrl = await initChunkWorkspace(encodedDestinationFile, retries)
504+
const tempUrl = await initChunkWorkspace(chunksWorkspace, encodedDestinationFile, retries)
496505
const chunksQueue: Array<Promise<void>> = []
497506

498507
// Generate chunks array

lib/utils/upload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ export const getChunk = function(file: File, start: number, length: number): Pro
112112

113113
/**
114114
* Create a temporary upload workspace to upload the chunks to
115+
* @param chunksWorkspace The workspace where the file is uploaded to
115116
* @param destinationFile The file name after finishing the chunked upload
116117
* @param retries number of retries
117118
*/
118-
export const initChunkWorkspace = async function(destinationFile: string | undefined = undefined, retries: number = 5): Promise<string> {
119-
const chunksWorkspace = generateRemoteUrl(`dav/uploads/${getCurrentUser()?.uid}`)
119+
export const initChunkWorkspace = async function(chunksWorkspace: string, destinationFile: string | undefined = undefined, retries: number = 5): Promise<string> {
120120
const hash = [...Array(16)].map(() => Math.floor(Math.random() * 16).toString(16)).join('')
121121
const tempWorkspace = `web-file-upload-${hash}`
122122
const url = `${chunksWorkspace}/${tempWorkspace}`

0 commit comments

Comments
 (0)