/
FileSystemResourceManager.php
executable file
·125 lines (112 loc) · 3.15 KB
/
FileSystemResourceManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* Created by PhpStorm.
* User: danielfiebig
* Date: 04/05/15
* Time: 12:06
*/
namespace bdart\resourcemanager;
use yii\helpers\ArrayHelper;
use yii\base\Component;
use Yii;
/**
* Class FileSystemResourceManager
* @package bdart\resourcemanager
*/
class FileSystemResourceManager extends Component implements ResourceManagerInterface
{
/**
* @var string
*/
public $directory = 'uploads';
/**
* @var
*/
private $_basePath;
/**
* @var
*/
private $_baseUrl;
/**
* Saves a file
* @param \yii\web\UploadedFile $file the file uploaded
* @param string $name the name of the file. If empty, it will be set to the name of the uploaded file
* @param array $options to save the file. The options can be any of the following:
* - `folder` : whether we should create a subfolder where to save the file
* @return boolean
*/
public function save($file, $name, $options = [])
{
$folder = ArrayHelper::getValue($options, 'folder');
$path = $folder
? $this->getBasePath() . DIRECTORY_SEPARATOR . $folder . ltrim($name, DIRECTORY_SEPARATOR)
: $this->getBasePath() . DIRECTORY_SEPARATOR . ltrim($name, DIRECTORY_SEPARATOR);
@mkdir(dirname($path), 0777, true);
return $file->saveAs($path);
}
/**
* Removes a file
* @param string $name the name of the file to remove
* @return boolean
*/
public function delete($name)
{
return $this->fileExists($name) ? @unlink($this->getBasePath() . DIRECTORY_SEPARATOR . $name) : false;
}
/**
* Checks whether a file exists or not
* @param string $name the name of the file
* @return boolean
*/
public function fileExists($name)
{
return file_exists($this->getBasePath() . DIRECTORY_SEPARATOR . $name);
}
/**
* Returns the url of the file or empty string if the file doesn't exist.
* @param string $name the name of the file
* @return string
*/
public function getUrl($name)
{
return $this->getBaseUrl() . '/' . $name;
}
/**
* Returns the upload directory path
* @return string
*/
public function getBasePath()
{
if ($this->_basePath === null) {
$this->_basePath = Yii::$app->getBasePath() . DIRECTORY_SEPARATOR . $this->directory;
}
return $this->_basePath;
}
/**
* Sets the upload directory path
* @param $value
*/
public function setBasePath($value)
{
$this->_basePath = rtrim($value, DIRECTORY_SEPARATOR);
}
/**
* Returns the base url
* @return string the url pointing to the directory where we saved the files
*/
public function getBaseUrl()
{
if ($this->_baseUrl === null) {
$this->_baseUrl = Yii::$app->getRequest()->getBaseUrl() . '/' . $this->directory;
}
return $this->_baseUrl;
}
/**
* Sets the base url
* @param string $value the url pointing to the directory where to get the files
*/
public function setBaseUrl($value)
{
$this->_baseUrl = rtrim($value, '/');
}
}