FileService.php
3.93 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
<?php
namespace Resource;
use Yii;
use yii\base\Exception;
use yii\log\Logger;
/**
* Class FileService 和ImageServer 是一样的,只是命名不一样
* @package resource
*/
class FileService
{
/** @var **/
protected $serviceProvider;
/** @var string **/
public $name = 'OSS';
/**
* @param \yii\web\Application $app
*/
public function __construct()
{
if('OSS' == $this->name){
$this->options = \yii::$app->params['ossOptions'];
$serviceProvider = new ServiceProviderOss($this->options);
$this->setServiceProvider($serviceProvider);
}
}
/**
* @param $path
* @param $destPath
* @return mixed
*/
public function setServiceProvider(ServiceProviderAbstract $serviceProvider)
{
$this->serviceProvider = $serviceProvider;
}
/**
* @param $path
* @param $destPath
* @return mixed
*/
public function add($path, $destPath)
{
return $this->serviceProvider->add($path, $destPath);
}
/**
* @param $path
* @param null $style
* @return mixed
*/
public function getUrl($path, $style = null)
{
return $this->serviceProvider->getUrl($path, $style);
}
/**
* @param $path
* @return mixed
*/
public function delete($path)
{
return $this->serviceProvider->delete($path);
}
/**
* @param $oldPath
* @param $newPath
* @return mixed
*/
public function replace($oldPath, $newPath, $tmpPath)
{
return $this->serviceProvider->replace($oldPath, $newPath, $tmpPath);
}
/**
* @param $path
* @return mixed
*/
public function exists($path)
{
return $this->serviceProvider->exists($path);
}
/**
* @param $oldPath
* @param $newPath
* @return mixed
*/
public function move($oldPath, $newPath)
{
return $this->serviceProvider->move($oldPath, $newPath);
}
/**
* @param $paths
* @return mixed
*/
public function batchDelete($paths)
{
return $this->serviceProvider->batchDelete($paths);
}
/** 测试用 */
public function getProvider()
{
return $this->serviceProvider;
}
/** 获取指定目录下面的所有文件和文件夹
* @param string $dir
* @param int $maxKeys
* @return array|bool|null
*/
public function getDirFiles($dir = '', $maxKeys = 20)
{
$serviceProvider = $this->serviceProvider;
if (!$serviceProvider) {
return null;
}
if (empty($dir)) {
return false;
}
$returnList = ['files' => [], 'directories' => []];
$ossClient = $serviceProvider->getClient();
$delimiter = '/';
$nextMarker = '';
$options = array(
'delimiter' => $delimiter,
'prefix' => $dir,
'max-keys' => $maxKeys,
'marker' => $nextMarker,
);
$ossConfig = $this->options;
$bucket = $ossConfig['bucket'];
try {
$listObjectInfo = $ossClient->listObjects($bucket, $options);
$objectList = $listObjectInfo->getObjectList(); // object list
$prefixList = $listObjectInfo->getPrefixList(); // directory list
if (!empty($objectList)) {
foreach ($objectList as $objectInfo) {
$returnList['files'][] = [$objectInfo->getKey(), $objectInfo->getLastModified(), 'type' => $objectInfo->getType()];
}
}
if (!empty($prefixList)) {
foreach ($prefixList as $prefixInfo) {
$returnList['directories'][] = $prefixInfo->getPrefix();
}
}
return $returnList;
} catch (Exception $e) {
Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR);
return false;
}
}
}