Client
1 year ago
.htaccess
1 year ago
ChunkedDownload.php
1 year ago
ChunkedUpload.php
1 year ago
DirIterator.php
1 year ago
index.html
1 year ago
pCloud.php
1 year ago
web.config
1 year ago
ChunkedUpload.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Destination\Vendors\pCloud; |
| 4 | |
| 5 | use JetBackup\Destination\Integration\DestinationChunkedUpload; |
| 6 | use JetBackup\Exception\IOException; |
| 7 | use JetBackup\Web\File\FileChunk; |
| 8 | |
| 9 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 10 | |
| 11 | class ChunkedUpload implements DestinationChunkedUpload { |
| 12 | |
| 13 | private pCloud $_client; |
| 14 | private string $_destination; |
| 15 | private object $_data; |
| 16 | |
| 17 | public function __construct(pCloud $client, $destination) { |
| 18 | $this->_client = $client; |
| 19 | $this->_destination = $destination; |
| 20 | $this->_data = new \stdClass(); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return object |
| 25 | */ |
| 26 | public function prepare():object { |
| 27 | return $this->_client->_retries(function() { |
| 28 | $response = $this->_client->getClient()->startUploadSession(); |
| 29 | |
| 30 | // Debugging: Log the full response |
| 31 | $this->_client->getlogController()->logDebug("[ChunkedUpload prepare] Response: " . json_encode($response)); |
| 32 | |
| 33 | if (!isset($response->uploadid)) { |
| 34 | throw new IOException("Upload session ID is missing from the response."); |
| 35 | } |
| 36 | |
| 37 | $output = new \stdClass(); |
| 38 | $output->id = $response->uploadid; |
| 39 | return $output; |
| 40 | }, "Failed creating upload session"); |
| 41 | } |
| 42 | |
| 43 | public function setData(object $data):void { |
| 44 | $this->_data = $data; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return int |
| 49 | */ |
| 50 | public function getOffset():int { |
| 51 | if(!isset($this->_data->id)) throw new IOException("No session id was found"); |
| 52 | |
| 53 | return $this->_client->_retries(function() { |
| 54 | $offset = $this->_client->getClient()->getUploadSession($this->_data->id)->size; |
| 55 | $this->_client->getlogController()->logDebug("[ChunkedUpload getOffset] Offset: $offset"); |
| 56 | return max( $offset, 0 ); |
| 57 | }, "Failed fetching upload offset"); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return int|null |
| 62 | */ |
| 63 | public function getChunkSize():?int { |
| 64 | if (!isset($this->_data->part_size)) { |
| 65 | $this->_client->getlogController()->logDebug("[ChunkedUpload getChunkSize] part_size is missing in the session data. Returning default chunk size."); |
| 66 | return $this->_client->getChunkSize(); |
| 67 | } |
| 68 | return $this->_data->part_size; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param FileChunk $chunk |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function upload(FileChunk $chunk): void { |
| 77 | if (!isset($this->_data->id)) { |
| 78 | throw new IOException("No session id was found"); |
| 79 | } |
| 80 | |
| 81 | $this->_client->_retries(function () use ($chunk) { |
| 82 | $this->_client->getClient()->appendUploadSession($this->_data->id, $chunk); |
| 83 | }, "Failed uploading chunk"); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * @return void |
| 89 | */ |
| 90 | public function finalize(): void { |
| 91 | if (!isset($this->_data->id)) { |
| 92 | throw new IOException("No session id was found"); |
| 93 | } |
| 94 | |
| 95 | $this->_client->_retries(function () { |
| 96 | $response = $this->_client->getClient()->commitUploadSession($this->_data->id, $this->_destination); |
| 97 | |
| 98 | if (!$response) { |
| 99 | $this->_client->getlogController()->logDebug("[ChunkedUpload finalize] Commit response is empty."); |
| 100 | } else { |
| 101 | $this->_client->getlogController()->logDebug("[ChunkedUpload finalize] Upload session committed successfully."); |
| 102 | } |
| 103 | }, "Failed committing upload session"); |
| 104 | } |
| 105 | |
| 106 | } |