Skip to content

Commit

Permalink
✨ 上传的图片已存在将会直接返回该图片地址 (#30)
Browse files Browse the repository at this point in the history
通过文件md5校验,上传的图片已存在将会直接返回该图片地址,图片不会重复上传
  • Loading branch information
0xxb committed Jan 8, 2020
1 parent 58b5956 commit 406040d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions application/index/controller/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ public function execute($user = null)
$strategy = $this->getStrategyInstance();

$pathname = strtolower($this->makePathname($image->getInfo('name')));
if (!$strategy->create($pathname, $image->getPathname())) {
if (Config::get('app.app_debug')) {
throw new Exception($strategy->getError());
}
throw new Exception('上传失败,请检查策略配置是否正确!');
}

$cdnDomain = $currentStrategy . '_cdn_domain';
$domain = $this->request->domain();
Expand All @@ -98,6 +92,19 @@ public function execute($user = null)
}
$url = make_url($domain, $pathname);

// 检测是否存在该图片,有则直接返回
if ($oldImage = Images::where('md5', $md5)->find()) {
$url = make_url($domain, $oldImage->pathname);
goto exist;
}

if (!$strategy->create($pathname, $image->getPathname())) {
if (Config::get('app.app_debug')) {
throw new Exception($strategy->getError());
}
throw new Exception('上传失败,请检查策略配置是否正确!');
}

// 图片鉴黄
$suspicious = 0;
if ($this->config['open_audit']) {
Expand Down Expand Up @@ -155,6 +162,8 @@ public function execute($user = null)
throw new Exception('图片数据保存失败');
}

exist:

$data = [
'name' => $image->getInfo('name'),
'url' => $url,
Expand Down

0 comments on commit 406040d

Please sign in to comment.