1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\FileAdder\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Logger\Exception; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait HasFileTrait |
11
|
|
|
* @package ByTIC\MediaLibrary\FileAdder\Traits |
12
|
|
|
*/ |
13
|
|
|
trait HasFileTrait |
14
|
|
|
{ |
15
|
|
|
/** @var string|\Symfony\Component\HttpFoundation\File\File */ |
16
|
|
|
protected $file; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $fileName; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $pathToFile; |
23
|
|
|
|
24
|
7 |
|
/** |
25
|
|
|
* @return string|SymfonyFile |
26
|
7 |
|
*/ |
27
|
|
|
public function getFile() |
28
|
|
|
{ |
29
|
|
|
return $this->file; |
30
|
|
|
} |
31
|
|
|
|
32
|
3 |
|
/** |
33
|
|
|
* @return string |
34
|
3 |
|
*/ |
35
|
|
|
public function getPathToFile(): string |
36
|
|
|
{ |
37
|
|
|
return $this->pathToFile; |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
/** |
41
|
|
|
* @param string $pathToFile |
42
|
6 |
|
*/ |
43
|
6 |
|
public function setPathToFile(string $pathToFile) |
44
|
|
|
{ |
45
|
|
|
$this->pathToFile = $pathToFile; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $file |
50
|
|
|
* |
51
|
|
|
* @throws Exception |
52
|
6 |
|
* |
53
|
|
|
* @return $this |
54
|
6 |
|
*/ |
55
|
6 |
|
public function setFile($file) |
56
|
|
|
{ |
57
|
6 |
|
if (is_string($file)) { |
58
|
|
|
$file = new SymfonyFile($file, true); |
59
|
|
|
} |
60
|
6 |
|
if ($file instanceof UploadedFile) { |
61
|
6 |
|
return $this->setFileFromUploadedFile($file); |
62
|
|
|
} |
63
|
|
|
if ($file instanceof SymfonyFile) { |
64
|
|
|
return $this->setFileFromSymfonyFile($file); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
throw new Exception('Invalid File'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param SymfonyFile $file |
72
|
|
|
* |
73
|
|
|
* @throws Exception |
74
|
6 |
|
* |
75
|
|
|
* @return $this |
76
|
6 |
|
*/ |
77
|
6 |
|
public function setFileFromSymfonyFile($file) |
78
|
6 |
|
{ |
79
|
6 |
|
$this->file = $file; |
80
|
|
|
$this->setPathToFile($file->getPath() . '/' . $file->getFilename()); |
81
|
6 |
|
$this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME)); |
|
|
|
|
82
|
|
|
$this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME); |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param UploadedFile $file |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setFileFromUploadedFile($file) |
93
|
|
|
{ |
94
|
|
|
$this->file = $file; |
95
|
|
|
$this->setPathToFile($file->getPath() . '/' . $file->getFilename()); |
96
|
|
|
$this->setFileName($file->getClientOriginalName()); |
97
|
|
|
$this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); |
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Set the name of the file that is stored on disk. |
103
|
|
|
* |
104
|
|
|
* @param string $fileName |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function setFileName(string $fileName) |
109
|
|
|
{ |
110
|
|
|
$this->fileName = $this->sanitizeFileName($fileName); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $fileName |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function sanitizeFileName(string $fileName): string |
121
|
|
|
{ |
122
|
|
|
return str_replace(['#', '/', '\\'], '-', $fileName); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|