Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 10... Línea 10...
10
class UploadState
10
class UploadState
11
{
11
{
12
    const CREATED = 0;
12
    const CREATED = 0;
13
    const INITIATED = 1;
13
    const INITIATED = 1;
14
    const COMPLETED = 2;
14
    const COMPLETED = 2;
-
 
15
    const PROGRESS_THRESHOLD_SIZE = 8;
-
 
16
 
-
 
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
    ];
Línea 15... Línea 28...
15
 
28
 
16
    /** @var array Params used to identity the upload. */
29
    /** @var array Params used to identity the upload. */
Línea 17... Línea 30...
17
    private $id;
30
    private $id;
Línea 23... Línea 36...
23
    private $uploadedParts = [];
36
    private $uploadedParts = [];
Línea 24... Línea 37...
24
 
37
 
25
    /** @var int Identifies the status the upload. */
38
    /** @var int Identifies the status the upload. */
Línea -... Línea 39...
-
 
39
    private $status = self::CREATED;
-
 
40
 
-
 
41
    /** @var array Thresholds for progress of the upload. */
-
 
42
    private $progressThresholds = [];
-
 
43
 
-
 
44
    /** @var boolean Determines status for tracking the upload */
26
    private $status = self::CREATED;
45
    private $displayProgress = false;
27
 
46
 
28
    /**
47
    /**
29
     * @param array $id Params used to identity the upload.
48
     * @param array $id Params used to identity the upload.
30
     */
49
     */
31
    public function __construct(array $id)
50
    public function __construct(array $id, array $config = [])
-
 
51
    {
-
 
52
        $this->id = $id;
-
 
53
 
-
 
54
        if (isset($config['display_progress'])
-
 
55
            && is_bool($config['display_progress'])
-
 
56
        ) {
32
    {
57
            $this->displayProgress = $config['display_progress'];
Línea 33... Línea 58...
33
        $this->id = $id;
58
        }
34
    }
59
    }
35
 
60
 
Línea 43... Línea 68...
43
    {
68
    {
44
        return $this->id;
69
        return $this->id;
45
    }
70
    }
Línea 46... Línea 71...
46
 
71
 
47
    /**
72
    /**
48
     * Set's the "upload_id", or 3rd part of the upload's ID. This typically
73
     * Sets the "upload_id", or 3rd part of the upload's ID. This typically
49
     * only needs to be done after initiating an upload.
74
     * only needs to be done after initiating an upload.
50
     *
75
     *
51
     * @param string $key   The param key of the upload_id.
76
     * @param string $key   The param key of the upload_id.
52
     * @param string $value The param value of the upload_id.
77
     * @param string $value The param value of the upload_id.
Línea 75... Línea 100...
75
    {
100
    {
76
        $this->partSize = $partSize;
101
        $this->partSize = $partSize;
77
    }
102
    }
Línea 78... Línea 103...
78
 
103
 
-
 
104
    /**
-
 
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
 
79
    /**
153
    /**
80
     * Marks a part as being uploaded.
154
     * Marks a part as being uploaded.
81
     *
155
     *
82
     * @param int   $partNumber The part number.
156
     * @param int   $partNumber The part number.
83
     * @param array $partData   Data from the upload operation that needs to be
157
     * @param array $partData   Data from the upload operation that needs to be
Línea 106... Línea 180...
106
     * @return array
180
     * @return array
107
     */
181
     */
108
    public function getUploadedParts()
182
    public function getUploadedParts()
109
    {
183
    {
110
        ksort($this->uploadedParts);
184
        ksort($this->uploadedParts);
111
 
-
 
112
        return $this->uploadedParts;
185
        return $this->uploadedParts;
113
    }
186
    }
Línea 114... Línea 187...
114
 
187
 
115
    /**
188
    /**