forked from qpwoeiru96/PHP-FastDFS-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorage.php
More file actions
460 lines (334 loc) · 14.2 KB
/
Copy pathStorage.php
File metadata and controls
460 lines (334 loc) · 14.2 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* PHP-FastDFS-Client (FOR FastDFS v4.0.6)
*
* 用PHP Socket实现的FastDFS客户端
*
* @author: $Author: QPWOEIRU96
* @version: $Rev: 525 $
* @date: $Date: 2014-01-17 15:02:03 +0800 (周五, 17 一月 2014) $
*/
namespace FastDFS;
class Storage extends Base {
/**
* 上传文件
*
* @command 11
* @param string $index 索引
* @param string $filename
* @param string $ext
* @throws Exception
* @internal param string $文件扩展名
* @return array
*/
public function uploadFile($index, $filename, $ext = '') {
if(!file_exists($filename))
return FALSE;
$pathInfo = pathinfo($filename);
if(strlen($ext) > Base::FILE_EXT_NAME_MAX_LEN)
throw new Exception('file ext too long.', 0);
if($ext === '') {
$ext = $pathInfo['extension'];
}
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
$fileSize = filesize($filename);
$requestBodyLength = 1 + Base::PROTO_PKG_LEN_SIZE +
Base::FILE_EXT_NAME_MAX_LEN + $fileSize;
$requestHeader = self::buildHeader(11, $requestBodyLength);
$requestBody = pack('C', $index) . self::packU64($fileSize) . self::padding($ext, Base::FILE_EXT_NAME_MAX_LEN);
$this->send($requestHeader . $requestBody);
stream_copy_to_stream($fp, $this->_sock, $fileSize);
flock($fp, LOCK_UN);
fclose($fp);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if($responseInfo['status'] !== 0)
throw new Exception(
'something wrong with uplode file',
$responseInfo['status']);
$responseBody = $responseInfo['length']
? $this->read($responseInfo['length'])
: FALSE;
$groupName = trim(substr($responseBody, 0, Base::GROUP_NAME_MAX_LEN));
$filePath = trim(substr($responseBody, Base::GROUP_NAME_MAX_LEN));
return array(
'group' => $groupName,
'path' => $filePath
);
}
/**
* 上传Slave文件
*
* @command 21
* @param string $filename 待上传的文件名称
* @param string $masterFilePath 主文件名称
* @param string $prefix_name 后缀的前缀名
* @param string $ext 后缀名称
* @throws Exception
* @return array/boolean
*/
public function uploadSlaveFile($filename, $masterFilePath, $prefix_name, $ext = '') {
if(!file_exists($filename))
return FALSE;
$pathInfo = pathinfo($filename);
if(strlen($ext) > Base::FILE_EXT_NAME_MAX_LEN)
throw new Exception('file ext too long.', 0);
if($ext === '') {
$ext = $pathInfo['extension'];
}
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
$fileSize = filesize($filename);
$masterFilePathLen = strlen($masterFilePath);
$requestBodyLength = 16 + Base::FILE_PREFIX_MAX_LEN +
Base::FILE_EXT_NAME_MAX_LEN + $masterFilePathLen + $fileSize;
$requestHeader = self::buildHeader(21, $requestBodyLength);
$requestBody = pack('x4N', $masterFilePathLen) . self::packU64($fileSize) . self::padding($prefix_name, Base::FILE_PREFIX_MAX_LEN);
$requestBody .= self::padding($ext, Base::FILE_EXT_NAME_MAX_LEN) . $masterFilePath;
$this->send($requestHeader . $requestBody);
stream_copy_to_stream($fp, $this->_sock, $fileSize);
flock($fp, LOCK_UN);
fclose($fp);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if($responseInfo['status'] !== 0) {
if($responseInfo['status'] == 17) {
$msg = 'target slave file already existd';
} else {
$msg = 'something in upload slave file';
}
throw new Exception(
$msg,
$responseInfo['status']);
}
$responseBody = $responseInfo['length']
? $this->read($responseInfo['length'])
: FALSE;
$groupName = trim(substr($responseBody, 0, Base::GROUP_NAME_MAX_LEN));
$filePath = trim(substr($responseBody, Base::GROUP_NAME_MAX_LEN));
return array(
"group" => $groupName,
'path' => $filePath
);
}
/**
*
* @command 23
*/
public function uploadAppenderFile($index, $filename, $ext = '') {
if(!file_exists($filename))
return FALSE;
$pathInfo = pathinfo($filename);
if(strlen($ext) > Base::FILE_EXT_NAME_MAX_LEN)
throw new Exception('file ext too long.', 0);
if($ext === '') {
$ext = $pathInfo['extension'];
}
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
$fileSize = filesize($filename);
$requestBodyLength = 1 + Base::PROTO_PKG_LEN_SIZE +
Base::FILE_EXT_NAME_MAX_LEN + $fileSize;
$requestHeader = self::buildHeader(23, $requestBodyLength);
$requestBody = pack('C', $index) . self::packU64($fileSize) . self::padding($ext, Base::FILE_EXT_NAME_MAX_LEN);
$this->send($requestHeader . $requestBody);
stream_copy_to_stream($fp, $this->_sock, $fileSize);
flock($fp, LOCK_UN);
fclose($fp);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if($responseInfo['status'] !== 0) {
throw new Exception(
'something wrong with uplode file',
$responseInfo['status']);
return FALSE;
}
$responseBody = $responseInfo['length']
? $this->read($responseInfo['length'])
: FALSE;
$groupName = trim(substr($responseBody, 0, Base::GROUP_NAME_MAX_LEN));
$filePath = trim(substr($responseBody, Base::GROUP_NAME_MAX_LEN));
return array(
"group" => $groupName,
'path' => $filePath
);
}
/**
* @command 24
*/
public function appendFile($content, $appenderFilePath) {
$appenderFilePathLength = strlen($appenderFilePath);
$content_length = strlen($content);
$requestBodyLength = (2 * Base::PROTO_PKG_LEN_SIZE) + $appenderFilePathLength + $content_length;
$requestHeader = self::buildHeader(24, $requestBodyLength);
$requestBody = pack('x4N', $appenderFilePathLength) . self::packU64($content_length) . $appenderFilePath . $content;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
return !$responseInfo['status'];
}
/**
* @command 34
*/
public function modifyFile($filename, $filePath, $offset = 0) {
if(!file_exists($filename))
return FALSE;
$filePathLength = strlen($filePath);
$fileSize = filesize($filename);
$requestBodyLength = (3 * Base::PROTO_PKG_LEN_SIZE) + $filePathLength + $fileSize;
$requestHeader = self::buildHeader(34, $requestBodyLength);
$requestBody = pack('x4N', $filePathLength) . self::packU64($offset) . self::packU64($fileSize) . $filePath;
$this->send($requestHeader . $requestBody);
$fp = fopen($filename, 'rb');
flock($fp, LOCK_SH);
stream_copy_to_stream($fp, $this->_sock, $fileSize);
flock($fp, LOCK_UN);
fclose($fp);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
return !$responseInfo['status'];
}
/**
* 删除文件
*
* @command 12
* @param string $groupName 组名称
* @param string $filePath 文件路径
* @return boolean 删除成功与否
*/
public function deleteFile($groupName, $filePath) {
$requestBodyLength = strlen($filePath) + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(12, $requestBodyLength);
$requestBody = self::padding($groupName, Base::GROUP_NAME_MAX_LEN) . $filePath;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
return !$responseInfo['status'];
}
/**
* 获取文件元信息
*
* @command 15
* @param string $groupName 组名称
* @param string $filePath 文件路径
* @return array 元信息数组
*/
public function getFileMetaData($groupName, $filePath) {
$requestBodyLength = strlen($filePath) + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(15, $requestBodyLength);
$requestBody = self::padding($groupName, Base::GROUP_NAME_MAX_LEN) . $filePath;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if(!!$responseInfo['status'])
return FALSE;
$responseBody = $responseInfo['length']
? $this->read($responseInfo['length'])
: FALSE;
return self::parseMetaData($responseBody);
}
/**
* 设置文件元信息
*
* @command 13
* @param string $groupName 组名称
* @param string $filePath 文件路径
* @param array $meta_data 元信息数组
* @param $flag
* @return boolean 设置成功与否
*/
public function setFileMetaData($groupName, $filePath, array $meta_data, $flag = Base::OVERWRITE_METADATA) {
$meta_data = self::buildMetaData($meta_data);
$meta_data_length = strlen($meta_data);
$filePathLength = strlen($filePath);
$flag = $flag === Base::OVERWRITE_METADATA ? 'O' : 'M';
$requestBodyLength = (Base::PROTO_PKG_LEN_SIZE * 2) + 1 + $meta_data_length + $filePathLength + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(13, $requestBodyLength);
$requestBody = self::packU64($filePathLength) . self::packU64($meta_data_length);
$requestBody .= $flag . self::padding($groupName, Base::GROUP_NAME_MAX_LEN) . $filePath . $meta_data;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
return !$responseInfo['status'];
}
/**
* 读取文件(不建议对大文件使用)
*
* @command 14
* @param string $groupName 组名称
* @param string $filePath 文件路径
* @param int $offset 下载文件偏移量
* @param int $length 下载文件大小
* @return string 文件内容
*/
public function readFile($groupName, $filePath, $offset = 0, $length = 0) {
$filePathLength = strlen($filePath);
$requestBodyLength = (Base::PROTO_PKG_LEN_SIZE * 2) + $filePathLength + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(14, $requestBodyLength);
$requestBody = self::packU64($offset) . self::packU64($length) . self::padding($groupName, Base::GROUP_NAME_MAX_LEN);
$requestBody .= $filePath;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if(!!$responseInfo['status']) return FALSE;
return $this->read($responseInfo['length']);
}
/**
* 下载文件
*
* @param $groupName
* @param $filePath
* @param $targetPath
* @param int $offset
* @param int $length
* @return bool
*/
public function downloadFile($groupName, $filePath, $targetPath, $offset = 0, $length = 0)
{
$filePathLength = strlen($filePath);
$requestBodyLength = (Base::PROTO_PKG_LEN_SIZE * 2) + $filePathLength + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(14, $requestBodyLength);
$requestBody = self::packU64($offset) . self::packU64($length) . self::padding($groupName, Base::GROUP_NAME_MAX_LEN);
$requestBody .= $filePath;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if($responseInfo['status']) return FALSE;
$fp = fopen($targetPath, 'w+b');
stream_copy_to_stream($this->_sock, $fp, $responseInfo['length'], 0);
fclose($fp);
return true;
}
/**
* 检索文件信息
*
* @command 22
* @param string $groupName 组名称
* @param string $filePath 文件路径
* @return array
*/
public function getFileInfo($groupName, $filePath) {
$requestBodyLength = strlen($filePath) + Base::GROUP_NAME_MAX_LEN;
$requestHeader = self::buildHeader(22, $requestBodyLength);
$requestBody = self::padding($groupName, Base::GROUP_NAME_MAX_LEN) . $filePath;
$this->send($requestHeader . $requestBody);
$responseHeader = $this->read(Base::HEADER_LENGTH);
$responseInfo = self::parseHeader($responseHeader);
if(!!$responseInfo['status']) return FALSE;
$responseBody = $responseInfo['length']
? $this->read($responseInfo['length'])
: FALSE;
$fileSize = self::unpackU64(substr($responseBody, 0, Base::PROTO_PKG_LEN_SIZE));
$timestamp = self::unpackU64(substr($responseBody, Base::PROTO_PKG_LEN_SIZE, Base::PROTO_PKG_LEN_SIZE));
list(,,$crc32) = unpack('N2', substr($responseBody, 2 * Base::PROTO_PKG_LEN_SIZE, Base::PROTO_PKG_LEN_SIZE));
$crc32 = base_convert(sprintf('%u', $crc32), 10, 16);
$storageId = trim(substr($responseBody, -16));
return array(
'file_size' => $fileSize,
'timestamp' => $timestamp,
'crc32' => $crc32,
'storage_id' => $storageId
);
}
}