1 | <?php |
||||
2 | namespace RazonYang\Yii2\Uploader; |
||||
3 | |||||
4 | use creocoder\flysystem\Filesystem; |
||||
5 | use creocoder\flysystem\LocalFilesystem; |
||||
6 | use yii\base\Component; |
||||
7 | use yii\di\Instance; |
||||
8 | |||||
9 | /** |
||||
10 | * Uploader |
||||
11 | * |
||||
12 | * @property string $host |
||||
13 | */ |
||||
14 | class Uploader extends Component implements UploaderInterface |
||||
15 | { |
||||
16 | /** |
||||
17 | * @var Filesystem $filesystem |
||||
18 | */ |
||||
19 | public $filesystem = 'filesystem'; |
||||
20 | |||||
21 | /** |
||||
22 | * @var string $host hostname |
||||
23 | */ |
||||
24 | private $host = 'http://localhost/'; |
||||
25 | |||||
26 | /** |
||||
27 | * Returns hostname. |
||||
28 | * |
||||
29 | * @return string |
||||
30 | */ |
||||
31 | 2 | public function getHost(): string |
|||
32 | { |
||||
33 | 2 | return $this->host; |
|||
34 | } |
||||
35 | |||||
36 | /** |
||||
37 | * Sets hostname |
||||
38 | * |
||||
39 | * @param string $host |
||||
40 | */ |
||||
41 | 6 | public function setHost(string $host): void |
|||
42 | { |
||||
43 | 6 | $this->host = rtrim($host, '/') . '/'; |
|||
44 | 6 | } |
|||
45 | |||||
46 | 11 | public function init() |
|||
47 | { |
||||
48 | 11 | parent::init(); |
|||
49 | |||||
50 | 11 | $this->filesystem = Instance::ensure($this->filesystem, FileSystem::class); |
|||
51 | 11 | } |
|||
52 | |||||
53 | 2 | public function getUrl(string $path): string |
|||
54 | { |
||||
55 | 2 | return $this->host . ltrim($path, '/'); |
|||
56 | } |
||||
57 | |||||
58 | 2 | public function getPath(string $url): string |
|||
59 | { |
||||
60 | 2 | return str_replace($this->host, '', $url); |
|||
61 | } |
||||
62 | |||||
63 | public function save(string $path, string $content, bool $overwrite = false): string |
||||
64 | { |
||||
65 | if ($overwrite) { |
||||
66 | $saved = $this->filesystem->put($path, $content); |
||||
67 | } else { |
||||
68 | $saved = $this->filesystem->write($path, $content); |
||||
69 | } |
||||
70 | if (!$saved) { |
||||
71 | throw \RuntimeException('Unable to save stream as ' . $path); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
72 | } |
||||
73 | return $this->getUrl($path); |
||||
74 | } |
||||
75 | |||||
76 | public function saveStream(string $path, $resource, bool $overwrite = false): string |
||||
77 | { |
||||
78 | if ($overwrite) { |
||||
79 | $saved = $this->filesystem->putStream($path, $resource); |
||||
80 | } else { |
||||
81 | $saved = $this->filesystem->writeStream($path, $resource); |
||||
82 | } |
||||
83 | if (!$saved) { |
||||
84 | throw \RuntimeException('Unable to save stream as ' . $path); |
||||
0 ignored issues
–
show
The function
RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
85 | } |
||||
86 | return $this->getUrl($path); |
||||
87 | } |
||||
88 | |||||
89 | public function saveFile(string $path, string $filename, bool $overwrite = false): string |
||||
90 | { |
||||
91 | $stream = fopen($filename, 'r'); |
||||
92 | if ($stream === false) { |
||||
93 | throw new \RuntimeException('Unable to read stream of ' . $filename); |
||||
94 | } |
||||
95 | $url = $this->saveStream($path, $stream, $overwrite); |
||||
96 | fclose($stream); |
||||
97 | return $url; |
||||
98 | } |
||||
99 | } |
||||
100 |