PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / Aws / S3 / MultipartUploadingTrait.php

MultipartUploadingTrait.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/Aws/S3/MultipartUploadingTrait.php

109 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws\S3;
4
5 use Dudlewebs\WPMCS\s3\Aws\CommandInterface;
6 use Dudlewebs\WPMCS\s3\Aws\Multipart\UploadState;
7 use Dudlewebs\WPMCS\s3\Aws\ResultInterface;
8 trait MultipartUploadingTrait
9 {
10 private $uploadedBytes = 0;
11 /**
12 * Creates an UploadState object for a multipart upload by querying the
13 * service for the specified upload's information.
14 *
15 * @param S3ClientInterface $client S3Client used for the upload.
16 * @param string $bucket Bucket for the multipart upload.
17 * @param string $key Object key for the multipart upload.
18 * @param string $uploadId Upload ID for the multipart upload.
19 *
20 * @return UploadState
21 */
22 public static function getStateFromService(S3ClientInterface $client, $bucket, $key, $uploadId)
23 {
24 $state = new UploadState(['Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId]);
25 foreach ($client->getPaginator('ListParts', $state->getId()) as $result) {
26 // Get the part size from the first part in the first result.
27 if (!$state->getPartSize()) {
28 $state->setPartSize($result->search('Parts[0].Size'));
29 }
30 // Mark all the parts returned by ListParts as uploaded.
31 foreach ($result['Parts'] as $part) {
32 $state->markPartAsUploaded($part['PartNumber'], ['PartNumber' => $part['PartNumber'], 'ETag' => $part['ETag']]);
33 }
34 }
35 $state->setStatus(UploadState::INITIATED);
36 return $state;
37 }
38 protected function handleResult(CommandInterface $command, ResultInterface $result)
39 {
40 $partData = [];
41 $partData['PartNumber'] = $command['PartNumber'];
42 $partData['ETag'] = $this->extractETag($result);
43 $commandName = $command->getName();
44 $checksumResult = $commandName === 'UploadPart' ? $result : $result[$commandName . 'Result'];
45 if (isset($command['ChecksumAlgorithm'])) {
46 $checksumMemberName = 'Checksum' . \strtoupper($command['ChecksumAlgorithm']);
47 $partData[$checksumMemberName] = $checksumResult[$checksumMemberName] ?? null;
48 }
49 $this->getState()->markPartAsUploaded($command['PartNumber'], $partData);
50 // Updates counter for uploaded bytes.
51 $this->uploadedBytes += $command["ContentLength"];
52 // Sends uploaded bytes to progress tracker if getDisplayProgress set
53 if ($this->displayProgress) {
54 $this->getState()->getDisplayProgress($this->uploadedBytes);
55 }
56 }
57 protected abstract function extractETag(ResultInterface $result);
58 protected function getCompleteParams()
59 {
60 $config = $this->getConfig();
61 $params = isset($config['params']) ? $config['params'] : [];
62 $params['MultipartUpload'] = ['Parts' => $this->getState()->getUploadedParts()];
63 return $params;
64 }
65 protected function determinePartSize()
66 {
67 // Make sure the part size is set.
68 $partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
69 // Adjust the part size to be larger for known, x-large uploads.
70 if ($sourceSize = $this->getSourceSize()) {
71 $partSize = (int) \max($partSize, \ceil($sourceSize / MultipartUploader::PART_MAX_NUM));
72 }
73 // Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
74 if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
75 throw new \InvalidArgumentException('The part size must be no less ' . 'than 5 MB and no greater than 5 GB.');
76 }
77 return $partSize;
78 }
79 protected function getInitiateParams()
80 {
81 $config = $this->getConfig();
82 $params = isset($config['params']) ? $config['params'] : [];
83 if (isset($config['acl'])) {
84 $params['ACL'] = $config['acl'];
85 }
86 // Set the ContentType if not already present
87 if (empty($params['ContentType']) && ($type = $this->getSourceMimeType())) {
88 $params['ContentType'] = $type;
89 }
90 return $params;
91 }
92 /**
93 * @return UploadState
94 */
95 protected abstract function getState();
96 /**
97 * @return array
98 */
99 protected abstract function getConfig();
100 /**
101 * @return int
102 */
103 protected abstract function getSourceSize();
104 /**
105 * @return string|null
106 */
107 protected abstract function getSourceMimeType();
108 }
109