This repository was archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathFileOps.php
227 lines (188 loc) · 4.95 KB
/
FileOps.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
namespace SDK;
trait FileOps
{
protected function md(string $name = "", bool $tmp = false) : string
{/*{{{*/
$ret = $name;
if (!$name) {
if ($tmp) {
$pre = Config::getTmpDir();
$ret = $pre . DIRECTORY_SEPARATOR . md5(uniqid());
} else {
throw new Exception("Dir name is empty");
}
}
if (!is_dir($ret)) {
if (!mkdir($ret, 0755, true)) {
throw new Exception("Unable to create '$ret'");
}
}
return $ret;
}/*}}}*/
/* TODO is link and more checks. */
protected function rm(string $path) : bool
{/*{{{*/
if (!file_exists($path)) {
return false;
} else if (is_file($path)) {
return unlink($path);
}
$ret = true;
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$path,
\FilesystemIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir()) {
$ret = $ret && rmdir($item->getPathname());
} else {
$ret = $ret && unlink($item->getPathname());
}
}
return $ret && rmdir($path);
}/*}}}*/
/* TODO islink and more checks */
protected function cp_or_mv(string $src, string $dst, callable $cb) : bool
{/*{{{*/
if (!file_exists($src)) {
return false;
} else if (is_file($src)) {
return call_user_func($cb, $src, $dst);
}
if (!file_exists($dst)) {
$this->md($dst);
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$src,
\FilesystemIterator::SKIP_DOTS
),
\RecursiveIteratorIterator::CHILD_FIRST
);
$cut_len = strlen($src)+1;
foreach ($iterator as $item) {
$src_path = $item->getPathname();
$sub = substr($src_path, $cut_len);
$dst_path = $dst . DIRECTORY_SEPARATOR . $sub;
$dst_parent = dirname($dst_path);
if (!is_dir($dst_parent)) {
if (!$this->md($dst_parent)) {
throw new Exception("Unable to create '$dst_parent'");
}
}
if ($item->isFile()) {
if (!call_user_func($cb, $src_path, $dst_path)) {
throw new Exception("Unable to $cb '$src_path' to '$dst_path'");
}
}
}
return true;
}/*}}}*/
protected function cp(string $src, string $dst) : bool
{/*{{{*/
return $this->cp_or_mv($src, $dst, "copy");
}/*}}}*/
protected function mv(string $src, string $dst) : bool
{/*{{{*/
$ret = $this->cp_or_mv($src, $dst, "rename");
$ret = $ret && $this->rm($src);
return $ret;
}/*}}}*/
protected function download(string $url, string $dest_fn = NULL) : ?string
{/*{{{*/
$fd = NULL;
$retry = 0;
retry:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($dest_fn) {
$fd = fopen($dest_fn, "w+");
curl_setopt($ch, CURLOPT_FILE, $fd);
} else {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, Config::getSdkUserAgentName());
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$ret = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (false === $ret || 200 !== $code) {
$err = curl_error($ch);
curl_close($ch);
if ($dest_fn) {
fclose($fd);
}
if ($retry++ < 3) {
goto retry;
}
throw new Exception($err);
}
curl_close($ch);
if ($dest_fn) {
fclose($fd);
return NULL;
}
return $ret;
}/*}}}*/
/* TODO More detailed zip errors. */
protected function unzip(string $zip_fn, string $dest_fn, string $dest_dn = NULL) : void
{/*{{{*/
$zip = new \ZipArchive;
$res = $zip->open($zip_fn);
if (true !== $res) {
throw new Exception("Failed to open '$zip_fn'.");
}
$res = $zip->extractTo($dest_fn);
if (true !== $res) {
$zip->close();
throw new Exception("Failed to unzip '$zip_fn'.");
}
/* Not robust, useful for zips containing one dir sibling only in the root. */
if ($dest_dn) {
$stat = $zip->statIndex(0);
if (false === $stat) {
$zip->close();
throw new Exception("Failed to stat first index in '$zip_fn'.");
}
$zip->close();
/* Index of zero might be not the zipped folder, unusual but true. */
/*$name = $stat["name"];
if ("/" != substr($name, -1)) {
throw new Exception("'$name' is not a directory.");
}
$name = substr($name, 0, -1);*/
$name = rtrim($stat["name"], "/");
while (strstr($name, '/') !== false) {
$name = dirname($name);
}
$old_dir = $dest_fn . DIRECTORY_SEPARATOR . $name;
$new_dir = $dest_fn . DIRECTORY_SEPARATOR . $dest_dn;
if (file_exists($new_dir)) {
if (!$this->rm($new_dir)) {
throw new Exception("Failed to remove '$new_dir'.");
}
}
/* if (!$this->mv($old_dir, $new_dir)) { */
if (!rename($old_dir, $new_dir)) {
throw new Exception("Failed to rename '$old_dir' to '$new_dir'.");
}
} else {
$zip->close();
}
}/*}}}*/
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/