Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Multipart;
3
 
4
/**
5
 * Representation of the multipart upload.
6
 *
7
 * This object keeps track of the state of the upload, including the status and
8
 * which parts have been uploaded.
9
 */
10
class UploadState
11
{
12
    const CREATED = 0;
13
    const INITIATED = 1;
14
    const COMPLETED = 2;
1441 ariadna 15
    const PROGRESS_THRESHOLD_SIZE = 8;
1 efrain 16
 
1441 ariadna 17
    private $progressBar = [
18
        "Transfer initiated...\n|                    | 0.0%\n",
19
        "|==                  | 12.5%\n",
20
        "|=====               | 25.0%\n",
21
        "|=======             | 37.5%\n",
22
        "|==========          | 50.0%\n",
23
        "|============        | 62.5%\n",
24
        "|===============     | 75.0%\n",
25
        "|=================   | 87.5%\n",
26
        "|====================| 100.0%\nTransfer complete!\n"
27
    ];
28
 
1 efrain 29
    /** @var array Params used to identity the upload. */
30
    private $id;
31
 
32
    /** @var int Part size being used by the upload. */
33
    private $partSize;
34
 
35
    /** @var array Parts that have been uploaded. */
36
    private $uploadedParts = [];
37
 
38
    /** @var int Identifies the status the upload. */
39
    private $status = self::CREATED;
40
 
1441 ariadna 41
    /** @var array Thresholds for progress of the upload. */
42
    private $progressThresholds = [];
43
 
44
    /** @var boolean Determines status for tracking the upload */
45
    private $displayProgress = false;
46
 
1 efrain 47
    /**
48
     * @param array $id Params used to identity the upload.
49
     */
1441 ariadna 50
    public function __construct(array $id, array $config = [])
1 efrain 51
    {
52
        $this->id = $id;
1441 ariadna 53
 
54
        if (isset($config['display_progress'])
55
            && is_bool($config['display_progress'])
56
        ) {
57
            $this->displayProgress = $config['display_progress'];
58
        }
1 efrain 59
    }
60
 
61
    /**
62
     * Get the upload's ID, which is a tuple of parameters that can uniquely
63
     * identify the upload.
64
     *
65
     * @return array
66
     */
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
 
72
    /**
1441 ariadna 73
     * Sets the "upload_id", or 3rd part of the upload's ID. This typically
1 efrain 74
     * only needs to be done after initiating an upload.
75
     *
76
     * @param string $key   The param key of the upload_id.
77
     * @param string $value The param value of the upload_id.
78
     */
79
    public function setUploadId($key, $value)
80
    {
81
        $this->id[$key] = $value;
82
    }
83
 
84
    /**
85
     * Get the part size.
86
     *
87
     * @return int
88
     */
89
    public function getPartSize()
90
    {
91
        return $this->partSize;
92
    }
93
 
94
    /**
95
     * Set the part size.
96
     *
97
     * @param $partSize int Size of upload parts.
98
     */
99
    public function setPartSize($partSize)
100
    {
101
        $this->partSize = $partSize;
102
    }
103
 
104
    /**
1441 ariadna 105
     * Sets the 1/8th thresholds array. $totalSize is only sent if
106
     * 'track_upload' is true.
107
     *
108
     * @param $totalSize numeric Size of object to upload.
109
     *
110
     * @return array
111
     */
112
    public function setProgressThresholds($totalSize): array
113
    {
114
        if(!is_numeric($totalSize)) {
115
            throw new \InvalidArgumentException(
116
                'The total size of the upload must be a number.'
117
            );
118
        }
119
 
120
        $this->progressThresholds[0] = 0;
121
        for ($i = 1; $i <= self::PROGRESS_THRESHOLD_SIZE; $i++) {
122
            $this->progressThresholds[] = round(
123
                $totalSize * ($i / self::PROGRESS_THRESHOLD_SIZE)
124
            );
125
        }
126
 
127
        return $this->progressThresholds;
128
    }
129
 
130
    /**
131
     * Prints progress of upload.
132
     *
133
     * @param $totalUploaded numeric Size of upload so far.
134
     */
135
    public function getDisplayProgress($totalUploaded): void
136
    {
137
        if (!is_numeric($totalUploaded)) {
138
            throw new \InvalidArgumentException(
139
                'The size of the bytes being uploaded must be a number.'
140
            );
141
        }
142
 
143
        if ($this->displayProgress) {
144
            while (!empty($this->progressBar)
145
                && $totalUploaded >= $this->progressThresholds[0]
146
            ) {
147
                echo array_shift($this->progressBar);
148
                array_shift($this->progressThresholds);
149
            }
150
        }
151
    }
152
 
153
    /**
1 efrain 154
     * Marks a part as being uploaded.
155
     *
156
     * @param int   $partNumber The part number.
157
     * @param array $partData   Data from the upload operation that needs to be
158
     *                          recalled during the complete operation.
159
     */
160
    public function markPartAsUploaded($partNumber, array $partData = [])
161
    {
162
        $this->uploadedParts[$partNumber] = $partData;
163
    }
164
 
165
    /**
166
     * Returns whether a part has been uploaded.
167
     *
168
     * @param int $partNumber The part number.
169
     *
170
     * @return bool
171
     */
172
    public function hasPartBeenUploaded($partNumber)
173
    {
174
        return isset($this->uploadedParts[$partNumber]);
175
    }
176
 
177
    /**
178
     * Returns a sorted list of all the uploaded parts.
179
     *
180
     * @return array
181
     */
182
    public function getUploadedParts()
183
    {
184
        ksort($this->uploadedParts);
185
        return $this->uploadedParts;
186
    }
187
 
188
    /**
189
     * Set the status of the upload.
190
     *
191
     * @param int $status Status is an integer code defined by the constants
192
     *                    CREATED, INITIATED, and COMPLETED on this class.
193
     */
194
    public function setStatus($status)
195
    {
196
        $this->status = $status;
197
    }
198
 
199
    /**
200
     * Determines whether the upload state is in the INITIATED status.
201
     *
202
     * @return bool
203
     */
204
    public function isInitiated()
205
    {
206
        return $this->status === self::INITIATED;
207
    }
208
 
209
    /**
210
     * Determines whether the upload state is in the COMPLETED status.
211
     *
212
     * @return bool
213
     */
214
    public function isCompleted()
215
    {
216
        return $this->status === self::COMPLETED;
217
    }
218
}