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