1
|
|
|
<?php |
2
|
|
|
namespace Nip\Filesystem; |
3
|
|
|
|
4
|
|
|
use League\Flysystem\FilesystemInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class File |
8
|
|
|
* @package Nip\Filesystem |
9
|
|
|
* |
10
|
|
|
* @method FilesystemInterface|FileDisk getFilesystem |
11
|
|
|
*/ |
12
|
|
|
class File extends \League\Flysystem\File |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
protected $name; |
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
protected $url; |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function __construct(FilesystemInterface $filesystem = null, $path = null) |
26
|
|
|
{ |
27
|
|
|
$this->parseNameFromPath($path); |
28
|
|
|
return parent::__construct($filesystem, $path); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* @param $path |
32
|
|
|
*/ |
33
|
|
|
protected function parseNameFromPath($path) |
34
|
|
|
{ |
35
|
|
|
$name = pathinfo($path, PATHINFO_BASENAME); |
36
|
|
|
$this->setName($name); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setFileName($name) |
44
|
|
|
{ |
45
|
|
|
$path_parts = pathinfo($this->getPath()); |
46
|
|
|
$path_parts['filename'] = $name; |
47
|
|
|
$this->setPath( |
48
|
|
|
$path_parts['dirname'] |
49
|
|
|
. '/' . $path_parts['filename'] . '.' . $path_parts['extension'] |
50
|
|
|
); |
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get File path with init check |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getPath() |
60
|
|
|
{ |
61
|
|
|
if (!$this->path) { |
62
|
|
|
$this->initPath(); |
63
|
|
|
} |
64
|
|
|
return parent::getPath(); |
65
|
|
|
} |
66
|
|
|
/** |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected function initPath() |
70
|
|
|
{ |
71
|
|
|
$this->setPath($this->getPathFolder() . $this->getName()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
* @param string $path |
77
|
|
|
*/ |
78
|
|
|
public function setPath($path) |
79
|
|
|
{ |
80
|
|
|
$this->parseNameFromPath($path); |
81
|
|
|
return parent::setPath($path); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getPathFolder() |
88
|
|
|
{ |
89
|
|
|
return '/'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function getName() |
96
|
|
|
{ |
97
|
|
|
if (!$this->name) { |
98
|
|
|
$this->initName(); |
99
|
|
|
} |
100
|
|
|
return $this->name; |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* @param string $name |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setName($name) |
107
|
|
|
{ |
108
|
|
|
$this->name = $name; |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function initName() |
113
|
|
|
{ |
114
|
|
|
$this->name = $this->getDefaultName(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getDefaultName() |
121
|
|
|
{ |
122
|
|
|
return 'file'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function download(): \Symfony\Component\HttpFoundation\StreamedResponse |
126
|
|
|
{ |
127
|
|
|
return $this->getFilesystem()->download($this->getPath(), $this->getName()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function getUrl() |
134
|
|
|
{ |
135
|
|
|
if (!$this->url) { |
136
|
|
|
$this->initUrl(); |
137
|
|
|
} |
138
|
|
|
return $this->url; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function initUrl() |
142
|
|
|
{ |
143
|
|
|
$this->url = $this->getFilesystem()->getUrl($this->getPath()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|