Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\S3;
3
 
4
use Aws\CommandInterface;
5
use Aws\Multipart\UploadState;
6
use Aws\ResultInterface;
7
 
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(
22
        S3ClientInterface $client,
23
        $bucket,
24
        $key,
25
        $uploadId
26
    ) {
27
        $state = new UploadState([
28
            'Bucket'   => $bucket,
29
            'Key'      => $key,
30
            'UploadId' => $uploadId,
31
        ]);
32
 
33
        foreach ($client->getPaginator('ListParts', $state->getId()) as $result) {
34
            // Get the part size from the first part in the first result.
35
            if (!$state->getPartSize()) {
36
                $state->setPartSize($result->search('Parts[0].Size'));
37
            }
38
            // Mark all the parts returned by ListParts as uploaded.
39
            foreach ($result['Parts'] as $part) {
40
                $state->markPartAsUploaded($part['PartNumber'], [
41
                    'PartNumber' => $part['PartNumber'],
42
                    'ETag'       => $part['ETag']
43
                ]);
44
            }
45
        }
46
 
47
        $state->setStatus(UploadState::INITIATED);
48
 
49
        return $state;
50
    }
51
 
52
    protected function handleResult(CommandInterface $command, ResultInterface $result)
53
    {
54
        $this->getState()->markPartAsUploaded($command['PartNumber'], [
55
            'PartNumber' => $command['PartNumber'],
56
            'ETag'       => $this->extractETag($result),
57
        ]);
58
    }
59
 
60
    abstract protected function extractETag(ResultInterface $result);
61
 
62
    protected function getCompleteParams()
63
    {
64
        $config = $this->getConfig();
65
        $params = isset($config['params']) ? $config['params'] : [];
66
 
67
        $params['MultipartUpload'] = [
68
            'Parts' => $this->getState()->getUploadedParts()
69
        ];
70
 
71
        return $params;
72
    }
73
 
74
    protected function determinePartSize()
75
    {
76
        // Make sure the part size is set.
77
        $partSize = $this->getConfig()['part_size'] ?: MultipartUploader::PART_MIN_SIZE;
78
 
79
        // Adjust the part size to be larger for known, x-large uploads.
80
        if ($sourceSize = $this->getSourceSize()) {
81
            $partSize = (int) max(
82
                $partSize,
83
                ceil($sourceSize / MultipartUploader::PART_MAX_NUM)
84
            );
85
        }
86
 
87
        // Ensure that the part size follows the rules: 5 MB <= size <= 5 GB.
88
        if ($partSize < MultipartUploader::PART_MIN_SIZE || $partSize > MultipartUploader::PART_MAX_SIZE) {
89
            throw new \InvalidArgumentException('The part size must be no less '
90
                . 'than 5 MB and no greater than 5 GB.');
91
        }
92
 
93
        return $partSize;
94
    }
95
 
96
    protected function getInitiateParams()
97
    {
98
        $config = $this->getConfig();
99
        $params = isset($config['params']) ? $config['params'] : [];
100
 
101
        if (isset($config['acl'])) {
102
            $params['ACL'] = $config['acl'];
103
        }
104
 
105
        // Set the ContentType if not already present
106
        if (empty($params['ContentType']) && $type = $this->getSourceMimeType()) {
107
            $params['ContentType'] = $type;
108
        }
109
 
110
        return $params;
111
    }
112
 
113
    /**
114
     * @return UploadState
115
     */
116
    abstract protected function getState();
117
 
118
    /**
119
     * @return array
120
     */
121
    abstract protected function getConfig();
122
 
123
    /**
124
     * @return int
125
     */
126
    abstract protected function getSourceSize();
127
 
128
    /**
129
     * @return string|null
130
     */
131
    abstract protected function getSourceMimeType();
132
}