PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.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.2.11, at includes/sdk/s3/Aws/S3/MultipartUploadingTrait.php

93 lines 3.6 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 /**
11 * Creates an UploadState object for a multipart upload by querying the
12 * service for the specified upload's information.
13 *
14 * @param S3ClientInterface $client S3Client used for the upload.
15 * @param string $bucket Bucket for the multipart upload.
16 * @param string $key Object key for the multipart upload.
17 * @param string $uploadId Upload ID for the multipart upload.
18 *
19 * @return UploadState
20 */
21 public static function getStateFromService(S3ClientInterface $client, $bucket, $key, $uploadId)
22 {
23 $state = new UploadState(['Bucket' => $bucket, 'Key' => $key, 'UploadId' => $uploadId]);
24 foreach ($client->getPaginator('ListParts', $state->getId()) as $result) {
25 // Get the part size from the first part in the first result.
26 if (!$state->getPartSize()) {
27 $state->setPartSize($result->search('Parts[0].Size'));
28 }
29 // Mark all the parts returned by ListParts as uploaded.
30 foreach ($result['Parts'] as $part) {
31 $state->markPartAsUploaded($part['PartNumber'], ['PartNumber' => $part['PartNumber'], 'ETag' => $part['ETag']]);
32 }
33 }
34 $state->setStatus(UploadState::INITIATED);
35 return $state;
36 }
37 protected function handleResult(CommandInterface $command, ResultInterface $result)
38 {
39 $this->getState()->markPartAsUploaded($command['PartNumber'], ['PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result)]);
40 }
41 protected abstract function extractETag(ResultInterface $result);
42 protected function getCompleteParams()
43 {
44 $config = $this->getConfig();
45 $params = isset($config['params']) ? $config['params'] : [];
46 $params['MultipartUpload'] = ['Parts' => $this->getState()->getUploadedParts()];
47 return $params;
48 }
49 protected function determinePartSize()
50 {
51 // Make sure the part size is set.
52 $partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
53 // Adjust the part size to be larger for known, x-large uploads.
54 if ($sourceSize = $this->getSourceSize()) {
55 $partSize = (int) \max($partSize, \ceil($sourceSize / MultipartUploader::PART_MAX_NUM));
56 }
57 // Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
58 if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
59 throw new \InvalidArgumentException('The part size must be no less ' . 'than 5 MB and no greater than 5 GB.');
60 }
61 return $partSize;
62 }
63 protected function getInitiateParams()
64 {
65 $config = $this->getConfig();
66 $params = isset($config['params']) ? $config['params'] : [];
67 if (isset($config['acl'])) {
68 $params['ACL'] = $config['acl'];
69 }
70 // Set the ContentType if not already present
71 if (empty($params['ContentType']) && ($type = $this->getSourceMimeType())) {
72 $params['ContentType'] = $type;
73 }
74 return $params;
75 }
76 /**
77 * @return UploadState
78 */
79 protected abstract function getState();
80 /**
81 * @return array
82 */
83 protected abstract function getConfig();
84 /**
85 * @return int
86 */
87 protected abstract function getSourceSize();
88 /**
89 * @return string|null
90 */
91 protected abstract function getSourceMimeType();
92 }
93