Radek
Radek

Reputation: 11091

How to assemble chunks while uploading large file to Nextcloud via sabre webdav php client?

I would like to implement chunked upload for large files. I was following this documentation https://docs.nextcloud.com/server/20/developer_manual/client_apis/WebDAV/chunking.html but I struggle with the final the most important step - Assembling the chunks

The code I used moves the whole temp directory to new directory that is named as the final file.

$sourcePath = $tempFolder . '.file';
$destinationPath = $baseUri . $uploadedFolderName . $uploadedFileName;

// Send the MOVE request
$response = $client->request('MOVE', $tempFolder, null, [
    'Destination' => $destinationPath
]);

My thinking is that

  1. syntax of the assembling code is not right or
  2. there must be something set up or turned on on the server or
  3. the doc says " The API is only available for registered users of your instance. And uses the path: /remote.php/dav/uploads/." but I use files in the url. If I use uploads then I cannot see if the temp folder was created. There is no error though. It looks like the chunks were uploaded but I cannot confirm and also there is no error returned when uploading. But the MOVE finishes with an error message "The destination node is not found"

Could someone please help me to fix this?

The code for uploading chunks


// Read the file in chunks and upload
$fp = fopen($filePath, 'rb');
$chunkNumber = 0;
$startByte = 0;
while (!feof($fp)) {
    $chunk = fread($fp, $chunkSize);
    $endByte = $startByte + strlen($chunk) - 1;

    // Generate a unique filename based on start and end bytes
    $chunkFileName = sprintf('%020d-%020d', $startByte, $endByte);
    $chunkPath = $tempFolder . '/' . $chunkFileName;

    // Upload the chunk
    // $client->put($chunkPath, $chunk);

//     // Upload the chunk
    try {
        $client->request('PUT', $chunkPath, $chunk);
        echo "Uploaded chunk $chunkNumber successfully.\n";
    } catch (Exception $e) {
        echo "Error uploading chunk $chunkNumber: " . $e->getMessage() . "\n";
        break; // Handle errors appropriately
    }



    $startByte += strlen($chunk);
    $chunkNumber++;
}
fclose($fp);

Upvotes: 0

Views: 35

Answers (0)

Related Questions