UploadFile::getHashAlgorithm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class for organize uploads storage
4
 *
5
 * @file      UploadFile.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2013-07-27 22:06
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Tools;
17
18
/**
19
 * Class UploadFile
20
 *
21
 * @author  Yancharuk Alexander <alex at itvault dot info>
22
 */
23
class UploadFile extends File
24
{
25
	/** @var string Temporary file path */
26
	protected $tmp_path;
27
	/** @var string File content hash */
28
	protected $hash;
29
	/** @var string Sub dir in upload dir */
30
	protected $sub_dir;
31
	/** @var string Original file name */
32
	protected $orig_name;
33
	/** @var string Path for www usage */
34
	protected $www_path;
35
	/** @var int Upload dir access mask */
36
	protected $dir_mask = 0755;
37
	/** @var string Hash algorithm */
38
	protected $hash_algorithm = 'sha1';
39
40
	/**
41
	 * Method created for test purpose
42
	 *
43
	 * @codeCoverageIgnore
44
	 *
45
	 * @param string $filename    File name
46
	 * @param string $destination Save destination
47
	 *
48
	 * @return bool
49
	 */
50
	protected function moveUploadedFile($filename, $destination)
51
	{
52
		return move_uploaded_file($filename, $destination);
53
	}
54
55
	/**
56
	 * Set upload directory mask
57
	 *
58
	 * @param int $dir_mask Value must be octal. Examples: 0755, 02755
59
	 *
60
	 * @return UploadFile
61
	 */
62 1
	public function setDirMask($dir_mask)
63
	{
64 1
		$this->dir_mask = $dir_mask;
65
66 1
		return $this;
67
	}
68
69
	/**
70
	 * Det upload directory mask
71
	 *
72
	 * @return int
73
	 */
74 1
	public function getDirMask()
75
	{
76 1
		return $this->dir_mask;
77
	}
78
79
	/**
80
	 * Get uploaded file origin name
81
	 *
82
	 * @return string
83
	 */
84 1
	public function getOrigName()
85
	{
86 1
		return $this->orig_name;
87
	}
88
89
	/**
90
	 * Set uploaded file origin name
91
	 *
92
	 * @param string $orig_name Origin file name
93
	 *
94
	 * @return UploadFile
95
	 */
96 1
	public function setOrigName($orig_name)
97
	{
98 1
		$this->orig_name = $orig_name;
99
100 1
		return $this;
101
	}
102
103
	/**
104
	 * Generate path for uploaded file
105
	 */
106 1
	public function initStorageName()
107
	{
108
		// initialize storage name only once
109 1
		if (!empty($this->getHash())) {
110 1
			return;
111
		}
112
113 1
		$array     = explode('.', $this->getOrigName());
114 1
		$extension = strtolower(end($array));
115
116 1
		$this->setHash(hash_file($this->getHashAlgorithm(), $this->getTmpPath()))
117 1
			->setSubDir(substr($this->getHash(), 0, 2))
118 1
			->setName(substr($this->getHash(), 2) . '.' . $extension)
119 1
			->setWwwPath($this->buildWwwPath())
120 1
			->setPath($this->buildPath());
121
	}
122
123
	/**
124
	 * Build WWW-path
125
	 *
126
	 * @return string
127
	 */
128 1
	protected function buildWwwPath()
129
	{
130 1
		return str_replace($_SERVER['DOCUMENT_ROOT'], '', $this->getDir())
131 1
			. DIRECTORY_SEPARATOR . $this->getSubDir() . DIRECTORY_SEPARATOR
132 1
			. $this->getName();
133
	}
134
135
	/**
136
	 * Build absolute file path
137
	 *
138
	 * @return string
139
	 */
140 1
	protected function buildPath()
141
	{
142 1
		return $this->getDir() . DIRECTORY_SEPARATOR . $this->getSubDir()
143 1
			. DIRECTORY_SEPARATOR . $this->getName();
144
	}
145
146
	/**
147
	 * Get uploaded file name hash
148
	 *
149
	 * @return string
150
	 */
151 1
	public function getHash()
152
	{
153 1
		return $this->hash;
154
	}
155
156
	/**
157
	 * Set uploaded file name hash
158
	 *
159
	 * @param string $hash
160
	 *
161
	 * @return UploadFile
162
	 */
163 1
	public function setHash($hash)
164
	{
165 1
		$this->hash = $hash;
166
167 1
		return $this;
168
	}
169
170
	/**
171
	 * Get uploaded file sub-dir
172
	 *
173
	 * @return string
174
	 */
175 1
	public function getSubDir()
176
	{
177 1
		return $this->sub_dir;
178
	}
179
180
	/**
181
	 * Set uploaded file sub-dir
182
	 *
183
	 * @param string $sub_dir
184
	 *
185
	 * @return UploadFile
186
	 */
187 1
	public function setSubDir($sub_dir)
188
	{
189 1
		$this->sub_dir = $sub_dir;
190
191 1
		return $this;
192
	}
193
194
	/**
195
	 * Save uploaded file
196
	 *
197
	 * @return bool
198
	 */
199 4
	public function save()
200
	{
201 4
		$dir = $this->getDir();
202
203 4
		is_dir($dir) || mkdir($dir, $this->getDirMask(), true);
204 4
		is_writable($dir) || chmod($dir, $this->getDirMask());
205
206 4
		return file_exists($this->getPath())
207 1
			? true
208 4
			: $this->moveUploadedFile($this->getTmpPath(), $this->getPath());
209
	}
210
211
	/**
212
	 * Get uploaded file temporary path
213
	 *
214
	 * @return string
215
	 */
216 1
	public function getTmpPath()
217
	{
218 1
		return $this->tmp_path;
219
	}
220
221
	/**
222
	 * Set uploaded file temporary path
223
	 *
224
	 * @param string $tmp_path Uploaded file temp path
225
	 *
226
	 * @return UploadFile
227
	 */
228 1
	public function setTmpPath($tmp_path)
229
	{
230 1
		$this->tmp_path = $tmp_path;
231
232 1
		return $this;
233
	}
234
235
	/**
236
	 * Get uploaded file www-path
237
	 *
238
	 * @return string
239
	 */
240 1
	public function getWwwPath()
241
	{
242 1
		return $this->www_path;
243
	}
244
245
	/**
246
	 * Set uploaded file www-path
247
	 *
248
	 * @param string $www_path www-path to file
249
	 *
250
	 * @return UploadFile
251
	 */
252 1
	public function setWwwPath($www_path)
253
	{
254 1
		$this->www_path = $www_path;
255
256 1
		return $this;
257
	}
258
259
	/**
260
	 * Get uploaded file name hashing algorithm
261
	 *
262
	 * @see hash_algos()
263
	 * @return string
264
	 */
265 1
	public function getHashAlgorithm()
266
	{
267 1
		return $this->hash_algorithm;
268
	}
269
270
	/**
271
	 * Set uploaded file name hashing algorithm
272
	 *
273
	 * @param string $hash_algorithm Hash algorithm
274
	 *
275
	 * @see hash_algos()
276
	 * @return UploadFile
277
	 */
278 1
	public function setHashAlgorithm($hash_algorithm)
279
	{
280 1
		$this->hash_algorithm = $hash_algorithm;
281
282 1
		return $this;
283
	}
284
}
285