Client
3 days ago
.htaccess
1 year ago
ChunkedDownload.php
1 year ago
ChunkedUpload.php
1 year ago
DirIterator.php
1 year ago
OneDrive.php
1 year ago
index.html
1 year ago
web.config
1 year ago
ChunkedUpload.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Destination\Vendors\OneDrive; |
| 4 | |
| 5 | use JetBackup\Destination\Integration\DestinationChunkedUpload; |
| 6 | use JetBackup\Destination\Vendors\OneDrive\Client\ClientException; |
| 7 | use JetBackup\Exception\IOException; |
| 8 | use JetBackup\Web\File\FileChunk; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | class ChunkedUpload implements DestinationChunkedUpload { |
| 13 | |
| 14 | private OneDrive $_client; |
| 15 | private string $_destination; |
| 16 | private object $_data; |
| 17 | |
| 18 | public function __construct(OneDrive $client, $destination) { |
| 19 | $this->_client = $client; |
| 20 | $this->_destination = $destination; |
| 21 | $this->_data = new \stdClass(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return object |
| 26 | * @throws ClientException |
| 27 | */ |
| 28 | public function prepare():object { |
| 29 | |
| 30 | $this->_client->getLogController()->logDebug("[ChunkedUpload] [prepare]"); |
| 31 | $output = new \stdClass(); |
| 32 | $response = $this->_client->client('createUploadSession', OneDrive::DRIVE_ROOT . ':' . $this->_destination); |
| 33 | $output->upload_url = $response->Body->uploadUrl; |
| 34 | return $output; |
| 35 | } |
| 36 | |
| 37 | public function setData(object $data):void { |
| 38 | $this->_data = $data; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @return int |
| 43 | * @throws ClientException |
| 44 | * @throws IOException |
| 45 | */ |
| 46 | public function getOffset():int { |
| 47 | |
| 48 | if(!isset($this->_data->upload_url)) throw new IOException("No upload url was found"); |
| 49 | |
| 50 | $response = $this->_client->client('checkUploadSession', $this->_data->upload_url); |
| 51 | |
| 52 | $this->_client->getLogController()->logDebug("[ChunkedUpload] [getOffset]"); |
| 53 | $range = $response->Body->nextExpectedRanges[0]; |
| 54 | |
| 55 | // no range, start from the beginning |
| 56 | if (empty($range)) { |
| 57 | $this->_client->getLogController()->logDebug("[ChunkedUpload] [getOffset] No Range, Start from beginning"); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | list($offset) = explode('-', $range); |
| 62 | $this->_client->getLogController()->logDebug("[ChunkedUpload] [getOffset] Offset: $offset"); |
| 63 | |
| 64 | return (int) $offset; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return int|null |
| 70 | */ |
| 71 | public function getChunkSize():?int { return null; } |
| 72 | |
| 73 | /** |
| 74 | * @param FileChunk $chunk |
| 75 | * |
| 76 | * @return void |
| 77 | * @throws ClientException |
| 78 | * @throws IOException |
| 79 | */ |
| 80 | public function upload(FileChunk $chunk):void { |
| 81 | $this->_client->getLogController()->logDebug("..uploading chunk [" . $chunk->getSize() . " Bytes]"); |
| 82 | if(!isset($this->_data->upload_url)) throw new IOException("No upload url was found"); |
| 83 | $this->_client->client('putChunk', $chunk, $this->_data->upload_url); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return void |
| 88 | */ |
| 89 | public function finalize():void {} |
| 90 | } |