1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Saito - The Threaded Web Forum |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
8
|
|
|
* @link https://github.com/Schlaefer/Saito |
9
|
|
|
* @license http://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ImageUploader\Lib; |
13
|
|
|
|
14
|
|
|
use League\Flysystem\Adapter\Local; |
15
|
|
|
use League\Flysystem\Filesystem; |
16
|
|
|
use League\Flysystem\FilesystemInterface; |
17
|
|
|
|
18
|
|
|
class File |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Path to file |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected string $path; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Filesystem |
28
|
|
|
* @var \League\Flysystem\FilesystemInterface |
29
|
|
|
*/ |
30
|
|
|
protected FilesystemInterface $fs; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* @param string $path path to file |
35
|
|
|
* @param null|\League\Flysystem\FilesystemInterface $filesystem filesystem (default: local from path) |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $path, ?FilesystemInterface $filesystem = null) |
39
|
|
|
{ |
40
|
|
|
$this->path = $path; |
41
|
|
|
$this->fs = $filesystem ?: new Filesystem(new Local($this->getDirname())); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get Filesystem |
46
|
|
|
* @return \League\Flysystem\FilesystemInterface |
47
|
|
|
*/ |
48
|
|
|
public function getFs(): FilesystemInterface |
49
|
|
|
{ |
50
|
|
|
return $this->fs; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get dirname of path |
55
|
|
|
* @return string dirname |
56
|
|
|
*/ |
57
|
|
|
public function getDirname(): string |
58
|
|
|
{ |
59
|
|
|
return dirname($this->getPath()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Path getter |
64
|
|
|
* @return string path |
65
|
|
|
*/ |
66
|
|
|
public function getPath(): string |
67
|
|
|
{ |
68
|
|
|
return $this->path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get mime type from physical file |
73
|
|
|
* @return string Mime Type |
74
|
|
|
*/ |
75
|
|
|
public function getMime(): string |
76
|
|
|
{ |
77
|
|
|
return $this->fs->getMimetype($this->getBasename()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get size from storage |
82
|
|
|
* @return int size |
83
|
|
|
*/ |
84
|
|
|
public function getSize(): int |
85
|
|
|
{ |
86
|
|
|
return $this->fs->getSize($this->getBasename()); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get basename of path |
91
|
|
|
* @return string basename |
92
|
|
|
*/ |
93
|
|
|
public function getBasename(): string |
94
|
|
|
{ |
95
|
|
|
return basename($this->getPath()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Read content from storage |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function read(): string |
103
|
|
|
{ |
104
|
|
|
return $this->fs->read($this->getBasename()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if item exists in storage |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function exists(): bool |
112
|
|
|
{ |
113
|
|
|
return $this->fs->has($this->getBasename()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Delete a file in path |
118
|
|
|
* @return bool True on success, False otherwise |
119
|
|
|
*/ |
120
|
|
|
public function delete(): bool |
121
|
|
|
{ |
122
|
|
|
return $this->fs->delete($this->getBasename()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|