【程序目录】优化图片下载

This commit is contained in:
evoxwht 2023-10-09 15:23:14 +08:00
parent 931bee5af4
commit bac210a9ed
4 changed files with 72 additions and 2 deletions

View File

@ -16,6 +16,7 @@ use crmeb\services\FormBuilder as Form;
use app\services\other\UploadService;
use think\facade\Log;
use think\facade\Route;
use think\facade\Config;
class DivisionAgentApplyServices extends BaseServices
{
@ -245,6 +246,18 @@ class DivisionAgentApplyServices extends BaseServices
public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
{
if (!strlen(trim($url))) return '';
if (!strlen(trim($name))) {
//TODO 获取要下载的文件名称
$downloadImageInfo = $this->getImageExtname($url);
$ext = $downloadImageInfo['ext_name'];
$name = $downloadImageInfo['file_name'];
if (!strlen(trim($name))) return '';
} else {
$ext = $this->getImageExtname($name)['ext_name'];
}
if (!in_array($ext, Config::get('upload.fileExt'))) {
throw new AdminException(400558);
}
//TODO 获取远程文件所采用的方法
if ($type) {
$ch = curl_init();
@ -283,4 +296,25 @@ class DivisionAgentApplyServices extends BaseServices
$data['is_exists'] = false;
return $data;
}
/**
* 获取即将要下载的图片扩展名
* @param string $url
* @param string $ex
* @return array|string[]
*/
public function getImageExtname($url = '', $ex = 'jpg')
{
$_empty = ['file_name' => '', 'ext_name' => $ex];
if (!$url) return $_empty;
if (strpos($url, '?')) {
$_tarr = explode('?', $url);
$url = trim($_tarr[0]);
}
$arr = explode('.', $url);
if (!is_array($arr) || count($arr) <= 1) return $_empty;
$ext_name = trim($arr[count($arr) - 1]);
$ext_name = !$ext_name ? $ex : $ext_name;
return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
}
}

View File

@ -18,6 +18,7 @@ use app\services\system\attachment\SystemAttachmentCategoryServices;
use app\services\system\attachment\SystemAttachmentServices;
use crmeb\exceptions\AdminException;
use app\services\other\UploadService;
use think\facade\Config;
/**
*
@ -288,7 +289,7 @@ class CopyTaobaoServices extends BaseServices
} else {
$ext = $this->getImageExtname($name)['ext_name'];
}
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF'])) {
if (!in_array($ext, Config::get('upload.fileExt'))) {
throw new AdminException(400558);
}
//TODO 获取远程文件所采用的方法

View File

@ -14,6 +14,7 @@ use app\services\user\UserServices;
use crmeb\exceptions\AdminException;
use app\services\other\UploadService;
use crmeb\services\app\WechatService;
use think\facade\Config;
/**
* Class WechatQrcodeServices
@ -172,6 +173,18 @@ class WechatQrcodeServices extends BaseServices
public function downloadImage($url = '', $name = '', $type = 0, $timeout = 30, $w = 0, $h = 0)
{
if (!strlen(trim($url))) return '';
if (!strlen(trim($name))) {
//TODO 获取要下载的文件名称
$downloadImageInfo = $this->getImageExtname($url);
$ext = $downloadImageInfo['ext_name'];
$name = $downloadImageInfo['file_name'];
if (!strlen(trim($name))) return '';
} else {
$ext = $this->getImageExtname($name)['ext_name'];
}
if (!in_array($ext, Config::get('upload.fileExt'))) {
throw new AdminException(400558);
}
//TODO 获取远程文件所采用的方法
if ($type) {
$ch = curl_init();
@ -211,6 +224,27 @@ class WechatQrcodeServices extends BaseServices
return $data;
}
/**
* 获取即将要下载的图片扩展名
* @param string $url
* @param string $ex
* @return array|string[]
*/
public function getImageExtname($url = '', $ex = 'jpg')
{
$_empty = ['file_name' => '', 'ext_name' => $ex];
if (!$url) return $_empty;
if (strpos($url, '?')) {
$_tarr = explode('?', $url);
$url = trim($_tarr[0]);
}
$arr = explode('.', $url);
if (!is_array($arr) || count($arr) <= 1) return $_empty;
$ext_name = trim($arr[count($arr) - 1]);
$ext_name = !$ext_name ? $ex : $ext_name;
return ['file_name' => md5($url) . '.' . $ext_name, 'ext_name' => $ext_name];
}
/**
* 扫码完成后方法
* @param $qrcodeInfo

View File

@ -15,6 +15,7 @@ namespace crmeb\utils;
use app\services\other\UploadService;
use crmeb\exceptions\AdminException;
use think\Image;
use think\facade\Config;
/**
* 下载图片到本地
@ -80,7 +81,7 @@ class DownloadImage
} else {
$ext = $this->getImageExtname($name)['ext_name'];
}
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF'])) {
if (!in_array($ext, Config::get('upload.fileExt'))) {
throw new AdminException(400558);
}
if (strstr($url, 'http://') === false && strstr($url, 'https://') === false) {