改进储存驱动

This commit is contained in:
WispX
2018-10-25 21:36:21 +08:00
parent 440a88104e
commit bcb4a9a3ca
6 changed files with 33 additions and 20 deletions
+11 -3
View File
@@ -25,7 +25,7 @@ class User extends Base
$model = $model->where('pathname', 'like', "%{$keyword}%");
}
$images = $model->paginate($limit)->each(function ($item) {
$item->url = $item->url;
$item->url = $item->url;
// TODO 生成缩略图
return $item;
});
@@ -66,11 +66,19 @@ class User extends Base
$strategyAll = array_keys(Config::pull('strategy'));
foreach ($strategyAll as $value) {
// 获取储存策略驱动
$strategy[$value] = $this->getStrategyInstance($value);
$strategy[$value] = $this->getStrategyInstance($value);
}
foreach ($deletes as $key => $val) {
$strategy[$key]->deletes($val);
if (1 === count($val)) {
if (!$strategy[$key]->delete(isset($val[0]) ? $val[0] : null)) {
throw new Exception('删除失败');
}
} else {
if (!$strategy[$key]->deletes($val)) {
throw new Exception('批量删除失败');
}
}
}
}
Db::commit();
@@ -91,7 +91,15 @@ class Images extends Base
}
foreach ($deletes as $key => $val) {
$strategy[$key]->deletes($val);
if (1 === count($val)) {
if (!$strategy[$key]->delete(isset($val[0]) ? $val[0] : null)) {
throw new Exception('删除失败');
}
} else {
if (!$strategy[$key]->deletes($val)) {
throw new Exception('批量删除失败');
}
}
}
}
Db::commit();
@@ -37,7 +37,7 @@ class Strategy extends Base
try {
$data = $this->request->post();
foreach ($data as $key => $value) {
Config::where('name', $key)->setField('value', $value);
Config::where('name', $key)->setField('value', trim($value));
}
Db::commit();
} catch (Exception $e) {
+6 -6
View File
@@ -116,14 +116,14 @@ class Cos implements Driver
public function deletes(array $list)
{
try {
$objects = [];
foreach ($list as $value) {
if (is_string($value)) {
$this->cos->deleteObject([
'Bucket' => $this->options['cos_bucket'],
'Key' => $value,
]);
}
$objects[] = ['Key' => $value ];
}
$this->cos->deleteObjects([
'Bucket' => $this->options['cos_bucket'],
'Objects' => $objects,
]);
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
+1 -1
View File
@@ -77,7 +77,7 @@ class Local implements Driver
*/
public function delete($pathname)
{
$delete = unlink(self::ROOT_PATH . ltrim($pathname, DIRECTORY_SEPARATOR));
$delete = @unlink(self::ROOT_PATH . ltrim($pathname, DIRECTORY_SEPARATOR));
if (!$delete) {
$this->error = '文件删除失败';
}
+5 -8
View File
@@ -122,15 +122,12 @@ class Qiniu implements Driver
*/
public function deletes(array $list)
{
foreach ($list as $value) {
if (is_string($value)) {
$err = $this->bucketMgr->delete($this->options['qiniu_bucket'], $value);
if ($err) {
$this->error = $err;
}
}
$ops = $this->bucketMgr->buildBatchDelete($this->options['qiniu_bucket'], $list);
list($ret, $err) = $this->bucketMgr->batch($ops);
if ($err) {
$this->error = $err;
return false;
}
return true;
}