1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* BsbFlystem |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @see https://bushbaby.nl/ |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright (c) 2014-2021 bushbaby multimedia. (https://bushbaby.nl) |
12
|
|
|
* @author Bas Kamer <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* |
15
|
|
|
* @package bushbaby/flysystem |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace BsbFlysystem\Filter\File; |
21
|
|
|
|
22
|
6 |
|
use Laminas\Filter\Exception; |
23
|
|
|
use Laminas\Filter\File\RenameUpload as RenameUploadFilter; |
24
|
6 |
|
use League\Flysystem\FilesystemInterface; |
25
|
1 |
|
use UnexpectedValueException; |
26
|
|
|
|
27
|
|
|
class RenameUpload extends RenameUploadFilter |
28
|
5 |
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FilesystemInterface |
31
|
5 |
|
*/ |
32
|
|
|
protected $filesystem; |
33
|
5 |
|
|
34
|
5 |
|
/** |
35
|
|
|
* @throws UnexpectedValueException |
36
|
6 |
|
*/ |
37
|
|
|
public function getFilesystem(): FilesystemInterface |
38
|
6 |
|
{ |
39
|
|
|
if (! $this->filesystem) { |
40
|
|
|
throw new UnexpectedValueException('Missing required filesystem.'); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
6 |
|
return $this->filesystem; |
44
|
1 |
|
} |
45
|
1 |
|
|
46
|
|
|
public function setFilesystem(FilesystemInterface $filesystem): void |
47
|
|
|
{ |
48
|
4 |
|
$this->filesystem = $filesystem; |
49
|
|
|
} |
50
|
4 |
|
|
51
|
|
|
protected function getFinalTarget($uploadData, $clientFileName): string |
52
|
4 |
|
{ |
53
|
1 |
|
return \trim(\str_replace('\\', '/', parent::getFinalTarget($uploadData, $clientFileName)), '/'); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
protected function checkFileExists($targetFile): void |
57
|
|
|
{ |
58
|
3 |
|
if (! $this->getOverwrite() && $this->getFilesystem()->has($targetFile)) { |
59
|
3 |
|
throw new Exception\InvalidArgumentException(\sprintf("File '%s' could not be uploaded. It already exists.", $targetFile)); |
60
|
3 |
|
} |
61
|
|
|
} |
62
|
3 |
|
|
63
|
1 |
|
protected function moveUploadedFile($sourceFile, $targetFile): bool |
64
|
1 |
|
{ |
65
|
|
|
if (! is_uploaded_file($sourceFile)) { |
66
|
|
|
throw new Exception\RuntimeException(\sprintf("File '%s' could not be uploaded. Filter can move only uploaded files.", $sourceFile), 0); |
67
|
|
|
} |
68
|
|
|
$stream = \fopen($sourceFile, 'r+'); |
69
|
2 |
|
$result = $this->getFilesystem()->putStream($targetFile, $stream); |
70
|
|
|
\fclose($stream); |
71
|
|
|
|
72
|
|
|
if (! $result) { |
73
|
|
|
throw new Exception\RuntimeException(\sprintf("File '%s' could not be uploaded. An error occurred while processing the file.", $sourceFile), 0); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|