From aa43585622acbe092d72e244069a66a9a5511fe2 Mon Sep 17 00:00:00 2001 From: WispX <1591788658@qq.com> Date: Wed, 4 Mar 2020 20:45:48 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E7=9B=B8=E5=90=8C=20?= =?UTF-8?q?md5=20=E7=9A=84=E6=96=87=E4=BB=B6=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=E8=AE=B0=E5=BD=95=E4=BC=9A=E5=B0=86=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=80=E5=B9=B6=E5=88=A0=E9=99=A4=E7=9A=84=20bug.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/index/controller/User.php | 20 +++++++++++++++++-- application/index/controller/admin/Images.php | 20 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/application/index/controller/User.php b/application/index/controller/User.php index f23e3f21..36d6aa10 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -55,7 +55,10 @@ class User extends Base if (is_array($id)) { $images = Images::all($id); foreach ($images as &$value) { - $deletes[$value->strategy][] = $value->pathname; + // 查找是否有相同 md5 的文件记录,有的话则只删除记录不删除文件 + if (!$this->exists($value)) { + $deletes[$value->strategy][] = $value->pathname; + } $value->delete(); unset($value); } @@ -64,7 +67,9 @@ class User extends Base if (!$image) { throw new Exception('没有找到该图片数据'); } - $deletes[$image->strategy][] = $image->pathname; + if (!$this->exists($image)) { + $deletes[$image->strategy][] = $image->pathname; + } $image->delete(); } // 是否开启软删除(开启了只删除记录,不删除文件) @@ -211,6 +216,17 @@ class User extends Base } } + /** + * 检测除本身图片以外的记录是否存在 + * + * @param Images $image + * @return float|string + */ + private function exists(Images $image) + { + return Images::where('id', 'neq', $image->id)->where('md5', $image->md5)->count(); + } + private function getDeleteFoldersAndImages($folderId, &$folders, &$images) { $folderList = Folders::where('parent_id', $folderId)->column('id'); diff --git a/application/index/controller/admin/Images.php b/application/index/controller/admin/Images.php index a9f7c3d8..de9ec899 100644 --- a/application/index/controller/admin/Images.php +++ b/application/index/controller/admin/Images.php @@ -80,7 +80,10 @@ class Images extends Base if (is_array($id)) { $images = ImagesModel::all($id); foreach ($images as &$value) { - $deletes[$value->strategy][] = $value->pathname; + // 查找是否有相同 md5 的文件记录,有的话则只删除记录不删除文件 + if (!$this->exists($value)) { + $deletes[$value->strategy][] = $value->pathname; + } $value->delete(); unset($value); } @@ -89,7 +92,9 @@ class Images extends Base if (!$image) { throw new Exception('没有找到该图片数据'); } - $deletes[$image->strategy][] = $image->pathname; + if (!$this->exists($image)) { + $deletes[$image->strategy][] = $image->pathname; + } $image->delete(); } // 是否开启软删除(开启了只删除记录,不删除文件) @@ -123,6 +128,17 @@ class Images extends Base } } + /** + * 检测除本身图片以外的记录是否存在 + * + * @param ImagesModel $image + * @return float|string + */ + private function exists(ImagesModel $image) + { + return ImagesModel::where('id', 'neq', $image->id)->where('md5', $image->md5)->count(); + } + public function getIpInfo() { if ($this->request->isPost()) {