Upload.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Transferito\Models\Transfer; |
| 4 | |
| 5 | use Aws\S3\S3Client; |
| 6 | use Aws\Credentials\Credentials; |
| 7 | use Transferito\Models\Core\Api as TransferitoAPI; |
| 8 | |
| 9 | if (!defined('ABSPATH')) exit; |
| 10 | |
| 11 | class Upload { |
| 12 | |
| 13 | private $s3Client; |
| 14 | |
| 15 | private $api; |
| 16 | |
| 17 | public function __construct() |
| 18 | { |
| 19 | $this->api = new TransferitoAPI(); |
| 20 | |
| 21 | /** |
| 22 | * Create instance of the S3 Client |
| 23 | */ |
| 24 | $this->s3Client = new S3Client([ |
| 25 | 'version' => 'latest', |
| 26 | 'region' => 'eu-west-2', |
| 27 | 'credentials' => new Credentials(TRANSFERITO_AWS_ACCESS, TRANSFERITO_AWS_SECRET) |
| 28 | ]); |
| 29 | } |
| 30 | |
| 31 | public function startUpload() |
| 32 | { |
| 33 | try { |
| 34 | |
| 35 | /** |
| 36 | * Get the extension |
| 37 | */ |
| 38 | $archiveExtension = get_transient('transferito_archive_extension'); |
| 39 | |
| 40 | /** |
| 41 | * Create the name of the archive name |
| 42 | */ |
| 43 | $archiveName = gmdate('gymsdi') . bin2hex(openssl_random_pseudo_bytes(64)) . '.' . $archiveExtension; |
| 44 | |
| 45 | /** |
| 46 | * Create the upload request |
| 47 | */ |
| 48 | $multipartCreated = $this->s3Client->createMultipartUpload([ |
| 49 | 'Bucket' => TRANSFERITO_AWS_BUCKET, |
| 50 | 'Key' => $archiveName, |
| 51 | 'StorageClass' => 'STANDARD', |
| 52 | 'ACL' => 'public-read' |
| 53 | ]); |
| 54 | |
| 55 | /** |
| 56 | * Assign the upload information to a variable |
| 57 | */ |
| 58 | $uploadInfo = [ |
| 59 | 'uploadId' => $multipartCreated['UploadId'], |
| 60 | 'filename' => $archiveName, |
| 61 | 'parts' => [] |
| 62 | ]; |
| 63 | |
| 64 | /** |
| 65 | * Set the initial transient with the upload information |
| 66 | */ |
| 67 | set_transient('transferito_upload_information', $uploadInfo); |
| 68 | |
| 69 | /** |
| 70 | * Hit the API |
| 71 | */ |
| 72 | $this->api->updateUploadStarted([ |
| 73 | 'token' => get_transient('transferito_migration_token'), |
| 74 | 'timestamp' => get_transient('transferito_migration_timestamp') |
| 75 | ]); |
| 76 | |
| 77 | return $uploadInfo['uploadId']; |
| 78 | } catch (\Exception $exception) { |
| 79 | throw new \Exception(esc_html($exception->getMessage())); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function uploadChunk($partNumber, $chunk) |
| 84 | { |
| 85 | /** |
| 86 | * Pull the upload info |
| 87 | */ |
| 88 | $uploadInfo = get_transient('transferito_upload_information'); |
| 89 | |
| 90 | try { |
| 91 | /** |
| 92 | * Upload the chunk |
| 93 | */ |
| 94 | $chunkUpload = $this->s3Client->uploadPart([ |
| 95 | 'Bucket' => TRANSFERITO_AWS_BUCKET, |
| 96 | 'Key' => $uploadInfo['filename'], |
| 97 | 'UploadId' => $uploadInfo['uploadId'], |
| 98 | 'PartNumber' => $partNumber, |
| 99 | 'Body' => $chunk, |
| 100 | ]); |
| 101 | |
| 102 | /** |
| 103 | * Create the parts |
| 104 | */ |
| 105 | $uploadInfo['parts'][$partNumber] = [ |
| 106 | 'PartNumber' => $partNumber, |
| 107 | 'ETag' => trim($chunkUpload['ETag'], '"') |
| 108 | ]; |
| 109 | |
| 110 | /** |
| 111 | * Update the initial transient with the upload information |
| 112 | */ |
| 113 | set_transient('transferito_upload_information', $uploadInfo); |
| 114 | |
| 115 | return true; |
| 116 | |
| 117 | } catch (\Exception $exception) { |
| 118 | /** |
| 119 | * Cancel the upload |
| 120 | */ |
| 121 | $this->s3Client->abortMultipartUpload([ |
| 122 | 'Bucket' => TRANSFERITO_AWS_BUCKET, |
| 123 | 'Key' => $uploadInfo['filename'], |
| 124 | 'UploadId' => $uploadInfo['uploadId'] |
| 125 | ]); |
| 126 | |
| 127 | throw new \Exception(esc_html($exception->getMessage())); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public function completeUpload() |
| 132 | { |
| 133 | try { |
| 134 | /** |
| 135 | * Pull the upload info |
| 136 | */ |
| 137 | $uploadInfo = get_transient('transferito_upload_information'); |
| 138 | |
| 139 | /** |
| 140 | * Call the API to complete the upload |
| 141 | */ |
| 142 | $this->s3Client->completeMultipartUpload([ |
| 143 | 'Bucket' => TRANSFERITO_AWS_BUCKET, |
| 144 | 'Key' => $uploadInfo['filename'], |
| 145 | 'UploadId' => $uploadInfo['uploadId'], |
| 146 | 'MultipartUpload' => [ |
| 147 | 'Parts' => $uploadInfo['parts'] |
| 148 | ] |
| 149 | ]); |
| 150 | |
| 151 | return [ |
| 152 | 'URL' => TRANSFERITO_AWS_BASE_URL . $uploadInfo['filename'], |
| 153 | 'path' => $uploadInfo['filename'] |
| 154 | ]; |
| 155 | } catch (\Exception $exception) { |
| 156 | throw new \Exception(esc_html($exception->getMessage())); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } |
| 161 |