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