1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2014 Mautic Contributors. All rights reserved |
5
|
|
|
* @author Mautic |
6
|
|
|
* |
7
|
|
|
* @see http://mautic.org |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\CoreBundle\Helper; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\Exception\FilePathException; |
15
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
18
|
|
|
|
19
|
|
|
class FilePathResolver |
20
|
|
|
{ |
21
|
|
|
private $filesystem; |
22
|
|
|
|
23
|
|
|
private $inputHelper; |
24
|
|
|
|
25
|
|
|
public function __construct(Filesystem $filesystem, InputHelper $inputHelper) |
26
|
|
|
{ |
27
|
|
|
$this->filesystem = $filesystem; |
28
|
|
|
$this->inputHelper = $inputHelper; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $uploadDir |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* |
36
|
|
|
* @throws FilePathException |
37
|
|
|
*/ |
38
|
|
|
public function getUniqueFileName($uploadDir, UploadedFile $file) |
39
|
|
|
{ |
40
|
|
|
$inputHelper = $this->inputHelper; |
41
|
|
|
$fullName = $file->getClientOriginalName(); |
42
|
|
|
$fullNameSanitized = $inputHelper::filename($fullName); |
43
|
|
|
$ext = $this->getFileExtension($file); |
44
|
|
|
$baseFileName = pathinfo($fullNameSanitized, PATHINFO_FILENAME); |
45
|
|
|
$name = $baseFileName; |
46
|
|
|
$filePath = $this->getFilePath($uploadDir, $baseFileName, $ext); |
|
|
|
|
47
|
|
|
$i = 1; |
48
|
|
|
|
49
|
|
|
while ($this->filesystem->exists($filePath)) { |
50
|
|
|
$name = $baseFileName.'-'.$i; |
|
|
|
|
51
|
|
|
$filePath = $this->getFilePath($uploadDir, $name, $ext); |
52
|
|
|
++$i; |
53
|
|
|
|
54
|
|
|
if ($i > 1000) { |
55
|
|
|
throw new FilePathException('Could not generate path'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $name.$ext; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $directory |
64
|
|
|
* |
65
|
|
|
* @throws FilePathException |
66
|
|
|
*/ |
67
|
|
|
public function createDirectory($directory) |
68
|
|
|
{ |
69
|
|
|
if ($this->filesystem->exists($directory)) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
try { |
73
|
|
|
$this->filesystem->mkdir($directory); |
74
|
|
|
} catch (IOException $e) { |
75
|
|
|
throw new FilePathException('Could not create directory'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $path |
81
|
|
|
*/ |
82
|
|
|
public function delete($path) |
83
|
|
|
{ |
84
|
|
|
if (!$this->filesystem->exists($path)) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
try { |
88
|
|
|
$this->filesystem->remove($path); |
89
|
|
|
} catch (IOException $e) { |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function move(string $originPath, string $targetPath): void |
94
|
|
|
{ |
95
|
|
|
$this->filesystem->rename($originPath, $targetPath); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $uploadDir |
100
|
|
|
* @param string $fileName |
101
|
|
|
* @param string $ext |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
private function getFilePath($uploadDir, $fileName, $ext) |
106
|
|
|
{ |
107
|
|
|
return $uploadDir.DIRECTORY_SEPARATOR.$fileName.$ext; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
private function getFileExtension(UploadedFile $file) |
114
|
|
|
{ |
115
|
|
|
$ext = $file->getClientOriginalExtension(); |
116
|
|
|
|
117
|
|
|
return ('' === $ext ? '' : '.').$ext; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|