b891f6cf9869ba3ec02997b3f3548ec06d7b96e8
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 /**
4 * @see https://github.com/laminas/laminas-progressbar for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-progressbar/blob/master/COPYRIGHT.md
6 * @license https://github.com/laminas/laminas-progressbar/blob/master/LICENSE.md New BSD License
7 */
8
9 namespace Laminas\ProgressBar\Upload;
10
11 use Laminas\ProgressBar\Adapter\AbstractAdapter as AbstractProgressAdapter;
12 use Laminas\ProgressBar\Exception;
13 use Laminas\ProgressBar\ProgressBar;
14 use Laminas\Stdlib\ArrayUtils;
15 use Traversable;
16
17 /**
18 * Abstract class for Upload Progress Handlers
19 */
20 abstract class AbstractUploadHandler implements UploadHandlerInterface
21 {
22 /**
23 * @var string
24 */
25 protected $sessionNamespace = 'Laminas\ProgressBar\Upload\AbstractUploadHandler';
26
27 /**
28 * @var AbstractProgressAdapter|ProgressBar
29 */
30 protected $progressAdapter;
31
32 /**
33 * @param array|Traversable $options Optional options
34 * @throws Exception\InvalidArgumentException
35 */
36 public function __construct($options = [])
37 {
38 if (! empty($options)) {
39 $this->setOptions($options);
40 }
41 }
42
43 /**
44 * Set options for a upload handler. Accepted options are:
45 * - session_namespace: session namespace for upload progress
46 * - progress_adapter: progressbar adapter to use for updating progress
47 *
48 * @param array|Traversable $options
49 * @return AbstractUploadHandler
50 * @throws Exception\InvalidArgumentException
51 */
52 public function setOptions($options)
53 {
54 if ($options instanceof Traversable) {
55 $options = ArrayUtils::iteratorToArray($options);
56 } elseif (! is_array($options)) {
57 throw new Exception\InvalidArgumentException(
58 'The options parameter must be an array or a Traversable'
59 );
60 }
61
62 if (isset($options['session_namespace'])) {
63 $this->setSessionNamespace($options['session_namespace']);
64 }
65 if (isset($options['progress_adapter'])) {
66 $this->setProgressAdapter($options['progress_adapter']);
67 }
68
69 return $this;
70 }
71
72 /**
73 * @param string $sessionNamespace
74 * @return AbstractUploadHandler|UploadHandlerInterface
75 */
76 public function setSessionNamespace($sessionNamespace)
77 {
78 $this->sessionNamespace = $sessionNamespace;
79 return $this;
80 }
81
82 /**
83 * @return string
84 */
85 public function getSessionNamespace()
86 {
87 return $this->sessionNamespace;
88 }
89
90 /**
91 * @param AbstractProgressAdapter|ProgressBar $progressAdapter
92 * @return AbstractUploadHandler|UploadHandlerInterface
93 */
94 public function setProgressAdapter($progressAdapter)
95 {
96 $this->progressAdapter = $progressAdapter;
97 return $this;
98 }
99
100 /**
101 * @return AbstractProgressAdapter|ProgressBar
102 */
103 public function getProgressAdapter()
104 {
105 return $this->progressAdapter;
106 }
107
108 /**
109 * @param string $id
110 * @return array
111 */
112 public function getProgress($id)
113 {
114 $status = [
115 'total' => 0,
116 'current' => 0,
117 'rate' => 0,
118 'message' => 'No upload in progress',
119 'done' => true
120 ];
121 if (empty($id)) {
122 return $status;
123 }
124
125 $newStatus = $this->getUploadProgress($id);
126 if (false === $newStatus) {
127 return $status;
128 }
129 $status = $newStatus;
130 if ('' === $status['message']) {
131 $status['message'] = $this->toByteString($status['current']) .
132 " - " . $this->toByteString($status['total']);
133 }
134 $status['id'] = $id;
135
136 $adapter = $this->getProgressAdapter();
137 if (isset($adapter)) {
138 if ($adapter instanceof AbstractProgressAdapter) {
139 $adapter = new ProgressBar($adapter, 0, $status['total'], $this->getSessionNamespace());
140 $this->setProgressAdapter($adapter);
141 }
142
143 if (! $adapter instanceof ProgressBar) {
144 throw new Exception\RuntimeException('Unknown Adapter type given');
145 }
146
147 if ($status['done']) {
148 $adapter->finish();
149 } else {
150 $adapter->update($status['current'], $status['message']);
151 }
152 }
153
154 return $status;
155 }
156
157 /**
158 * @param string $id
159 * @return array|bool
160 */
161 abstract protected function getUploadProgress($id);
162
163 /**
164 * Returns the formatted size
165 *
166 * @param int $size
167 * @return string
168 */
169 protected function toByteString($size)
170 {
171 $sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
172 for ($i = 0; $size >= 1024 && $i < 9; $i++) {
173 $size /= 1024;
174 }
175
176 return round($size, 2) . $sizes[$i];
177 }
178 }