forked from CoderKungfu/php-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalFS.php
More file actions
207 lines (179 loc) · 6.39 KB
/
Copy pathLocalFS.php
File metadata and controls
207 lines (179 loc) · 6.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace PHPQueue\Backend;
use PHPQueue\Exception\BackendException;
class LocalFS extends FS
{
public $doc_root;
public function __construct($options=array())
{
parent::__construct();
if (!empty($options['doc_root'])) {
$this->doc_root = $options['doc_root'];
}
if (!empty($options['container'])) {
$this->container = $options['container'];
}
}
public function connect()
{
$this->connection = true;
}
public function clear($key = null)
{
$file_path = $this->getFullPath($key);
if (!is_file($file_path)) {
throw new BackendException('File does not exist: '.$file_path);
}
if (!is_writable($file_path)) {
throw new BackendException('File is not deletable: '.$file_path);
}
$status = unlink($file_path);
if (!$status) {
throw new BackendException('Unable to delete file: '.$file_path);
}
clearstatcache();
return $status;
}
public function createContainer($container_name)
{
$dir_path = $this->getContainerPath($container_name);
$status = mkdir($dir_path, 0777, true);
if (!$status) {
throw new BackendException('Unable to create directory: '.$dir_path);
}
clearstatcache();
return $status;
}
public function deleteContainer($container_name)
{
$dir_path = $this->getContainerPath($container_name);
$status = rmdir($dir_path);
if (!$status) {
throw new BackendException('Unable to delete directory: '.$dir_path);
}
clearstatcache();
return $status;
}
public function listContainers()
{
if (empty($this->doc_root)) {
throw new BackendException('Document root is not set.');
}
$all_containers = array();
$dir = new \DirectoryIterator($this->doc_root);
foreach ($dir as $file_info) {
if (!$file_info->isDot() && $file_info->isDir()) {
$all_containers[] = array(
'name' => $file_info->getFilename()
, 'url' => $file_info->getPathname()
, 'object' => $file_info
);
}
}
return $all_containers;
}
public function listFiles()
{
if (empty($this->doc_root) || empty($this->container)) {
throw new BackendException('Document root or Container not set.');
}
$all_files = array();
$dir = new \DirectoryIterator($this->getCurrentContainerPath());
foreach ($dir as $file_info) {
if (!$file_info->isDot() && $file_info->isFile()) {
$all_files[] = array(
'name' => $file_info->getFilename()
, 'url' => $file_info->getPathname()
, 'object' => $file_info
);
}
}
return $all_files;
}
public function copy($src_container, $src_file, $dest_container, $dest_file)
{
$src_path = $this->getContainerPath($src_container) . DIRECTORY_SEPARATOR . $src_file;
$dest_path = $this->getContainerPath($dest_container) . DIRECTORY_SEPARATOR . $dest_file;
$status = copy($src_path, $dest_path);
if (!$status) {
$msg = sprintf('Unable to copy file: %s to file: %s', $src_path, $dest_path);
throw new BackendException($msg);
}
clearstatcache();
return $status;
}
public function putFile($key, $file_path = null, $options = null)
{
$dest_path = $this->getFullPath($key);
if (!is_file($file_path)) {
throw new BackendException('Upload file does not exist: '.$file_path);
}
if (!is_writable($this->getCurrentContainerPath())) {
throw new BackendException('Destination Container is not writable: '.$this->container);
}
if (is_file($dest_path) && !is_writable($dest_path)) {
throw new BackendException('Destination file is not writable: '.$dest_path);
}
if (is_uploaded_file($file_path)) {
$status = move_uploaded_file($file_path, $dest_path);
} else {
$status = copy($file_path, $dest_path);
}
if (!$status) {
$msg = sprintf('Unable to put file: %s to file: %s', $file_path, $dest_path);
throw new BackendException($msg);
}
clearstatcache();
return $status;
}
public function fetchFile($key, $destination_path = null, $options = null)
{
$src_path = $this->getFullPath($key);
if (!is_file($src_path)) {
throw new BackendException('File does not exist: '.$src_path);
}
if (!is_writable($destination_path)) {
throw new BackendException('Destination path is not writable: '.$destination_path);
}
$destination_file_path = $destination_path . DIRECTORY_SEPARATOR . $key;
$status = copy($src_path, $destination_file_path);
if (!$status) {
$msg = sprintf('Unable to fetch file: %s to file: %s', $src_path, $destination_path);
throw new BackendException($msg);
}
clearstatcache();
return $status;
}
public function hasContainer($container)
{
$dir_path = $this->getContainerPath($container);
return is_dir($dir_path);
}
protected function getContainerPath($directory_name)
{
if (empty($this->doc_root)) {
throw new BackendException('Document root is not set.');
}
if (empty($directory_name)) {
throw new BackendException('Invalid directory name.');
}
return $this->doc_root . DIRECTORY_SEPARATOR . $directory_name;
}
protected function getFullPath($key)
{
if (empty($this->doc_root) || empty($this->container)) {
throw new BackendException('Document root or Container not set.');
}
if (empty($key)) {
throw new BackendException('Invalid file name.');
}
return $this->getCurrentContainerPath() . DIRECTORY_SEPARATOR . $key;
}
protected function getCurrentContainerPath()
{
if (empty($this->container)) {
throw new BackendException('Container not set.');
}
return $this->getContainerPath($this->container);
}
}