From bcb4a9a3cae24fe7030e4e23401c05cc2f30910d Mon Sep 17 00:00:00 2001 From: WispX <1591788658@qq.com> Date: Thu, 25 Oct 2018 21:36:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=82=A8=E5=AD=98=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/index/controller/User.php | 14 +++++++++++--- application/index/controller/admin/Images.php | 10 +++++++++- application/index/controller/admin/Strategy.php | 2 +- extend/strategy/driver/Cos.php | 12 ++++++------ extend/strategy/driver/Local.php | 2 +- extend/strategy/driver/Qiniu.php | 13 +++++-------- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/application/index/controller/User.php b/application/index/controller/User.php index 9e71afbd..eb45193f 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -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(); diff --git a/application/index/controller/admin/Images.php b/application/index/controller/admin/Images.php index d33699a8..1350bbfd 100644 --- a/application/index/controller/admin/Images.php +++ b/application/index/controller/admin/Images.php @@ -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(); diff --git a/application/index/controller/admin/Strategy.php b/application/index/controller/admin/Strategy.php index 71aa2e80..02e39288 100644 --- a/application/index/controller/admin/Strategy.php +++ b/application/index/controller/admin/Strategy.php @@ -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) { diff --git a/extend/strategy/driver/Cos.php b/extend/strategy/driver/Cos.php index 1851a5ea..ec7fc6e6 100644 --- a/extend/strategy/driver/Cos.php +++ b/extend/strategy/driver/Cos.php @@ -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; diff --git a/extend/strategy/driver/Local.php b/extend/strategy/driver/Local.php index cce283b6..b7f1ad7f 100644 --- a/extend/strategy/driver/Local.php +++ b/extend/strategy/driver/Local.php @@ -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 = '文件删除失败'; } diff --git a/extend/strategy/driver/Qiniu.php b/extend/strategy/driver/Qiniu.php index ef6a27d8..e1f3e190 100644 --- a/extend/strategy/driver/Qiniu.php +++ b/extend/strategy/driver/Qiniu.php @@ -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; }