Upload.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\models\common;
  8. use app\librarys\storage\components\StorageComponent;
  9. use app\librarys\storage\drivers\Aliyun;
  10. use app\librarys\storage\drivers\Local;
  11. use app\librarys\storage\drivers\Qcloud;
  12. use app\librarys\storage\drivers\Qiniu;
  13. use app\librarys\storage\helpers\UrlConverter;
  14. use app\librarys\storage\UploadFile;
  15. use app\models\Option;
  16. use app\models\Storage;
  17. use app\models\UploadConfig;
  18. use yii\base\Model;
  19. use yii\helpers\Json;
  20. use app\constants\OptionSetting;
  21. /**
  22. * 上传工具类
  23. * Class Upload
  24. * @package app\models\common
  25. * @property UploadConfig $upload_config
  26. */
  27. class Upload extends Model
  28. {
  29. public $upload_config;
  30. public $store_id = 1;
  31. public $mch_id = 0;
  32. public $supplier_id = 0;
  33. public $bd_id = 0;
  34. public $parent_id = 0;
  35. // 获取图片样式
  36. public static function getImgStyle($imgUrl, $store_id = 0)
  37. {
  38. if (empty($imgUrl)) {
  39. return $imgUrl;
  40. }
  41. if (!$store_id) {
  42. $store_id = \get_store_id();
  43. }
  44. $config = null;
  45. if (\Yii::$app->isSaas()) {
  46. $is_use_platform = (bool)Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
  47. if ($is_use_platform) {
  48. $config = UploadConfig::findOne(['store_id' => 0, 'is_delete' => 0]);
  49. if (!$config) {
  50. return $imgUrl;
  51. }
  52. }
  53. }
  54. if (!$config) {
  55. $config = UploadConfig::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  56. if (!$config) {
  57. return $imgUrl;
  58. }
  59. }
  60. if ($config->storage_type == 'local') {
  61. return $imgUrl;
  62. }
  63. $params = Json::decode($config->{$config->storage_type});
  64. if ($params && isset($params['style']) && !empty($params['style'])) {
  65. $style = $params['style'];
  66. // Todo 还有很多细节需要处理
  67. if ($config->storage_type == 'aliyun') {
  68. return $imgUrl . '?x-oss-process=style/' . $style;
  69. }
  70. if ($config->storage_type == 'qcloud') {
  71. $find = $params['bucket'];
  72. if (\strpos($imgUrl, $find) !== false) {
  73. return $imgUrl . '/' . $style;
  74. }
  75. if (isset($params['domain']) && !empty($params['domain'])) {
  76. $find = $params['domain'];
  77. if (\strpos($imgUrl, $find) !== false) {
  78. return $imgUrl . '/' . $style;
  79. }
  80. }
  81. }
  82. if ($config->storage_type == 'qiniu') {
  83. if (isset($params['domain']) && !empty($params['domain'])) {
  84. $find = $params['domain'];
  85. if (\strpos($imgUrl, $find) !== false) {
  86. return $imgUrl . '-' . $style;
  87. }
  88. }
  89. }
  90. }
  91. return $imgUrl;
  92. }
  93. /**
  94. * 获取文件最大上传大小
  95. * @return mixed
  96. */
  97. public static function getMaxUploadSize($type = Storage::TYPE_IMAGE, $supplier_id = 0)
  98. {
  99. $upload_max_size = self::getMaxFileSizeByte('upload_max_filesize');
  100. $upload_max_size = intval($upload_max_size / (1024 * 1024));
  101. $post_max_size = self::getMaxFileSizeByte('post_max_size');
  102. $post_max_size = intval($post_max_size / (1024 * 1024));
  103. // 商户是否使用平台配置
  104. if (\Yii::$app->isSaas()) {
  105. $is_use_platform = (bool)Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
  106. if ($supplier_id) {
  107. $is_use_platform = 1;
  108. }
  109. $upload_limit = Option::get(OptionSetting::PLATFORM_UPLOAD_LIMIT, 0, 'saas', json_encode([
  110. 'max_picture' => 10,
  111. 'over_picture' => 0,
  112. 'max_file' => 10,
  113. 'over_file' => 0
  114. ]))['value'];
  115. if ($is_use_platform) {
  116. $upload_limit = Json::decode($upload_limit);
  117. if (!isset($upload_limit['over_file'])) {
  118. $upload_limit['over_file'] = $upload_limit['over_picture'];
  119. }
  120. if (!isset($upload_limit['max_file'])) {
  121. $upload_limit['max_file'] = $upload_limit['max_picture'];
  122. }
  123. if ($type != Storage::TYPE_IMAGE) {
  124. if ($upload_limit['over_file'] > 0) {
  125. return min($upload_max_size, $post_max_size);
  126. } else {
  127. return min($upload_max_size, $upload_limit['max_file'], $post_max_size);
  128. }
  129. } else {
  130. if ($upload_limit['over_picture'] > 0) {
  131. return min($upload_max_size, $post_max_size);
  132. } else {
  133. return min($upload_max_size, $upload_limit['max_picture'], $post_max_size);
  134. }
  135. }
  136. }
  137. }
  138. // 获取后台越限配置
  139. $options = Option::get('overrun', get_store_id(), 'store');
  140. if (!$options['value']) {
  141. return min($upload_max_size, $post_max_size);
  142. } else {
  143. $options = Json::decode($options['value']);
  144. if ($type != Storage::TYPE_IMAGE) {
  145. if ($options['over_file'] > 0) {
  146. return min($upload_max_size, $post_max_size);
  147. } else {
  148. return min($upload_max_size, $options['max_file'], $post_max_size);
  149. }
  150. } else {
  151. if ($options['over_picture'] > 0) {
  152. return min($upload_max_size, $post_max_size);
  153. } else {
  154. return min($upload_max_size, $options['max_picture'], $post_max_size);
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * @param $type
  161. * @param int $dec
  162. * @return float
  163. */
  164. private static function getMaxFileSizeByte($type, $dec = 2)
  165. {
  166. $max_size = ini_get($type);
  167. preg_match('/(^[0-9\.]+)(\w+)/', $max_size, $info);
  168. $size = $info[1];
  169. $suffix = strtoupper($info[2]);
  170. $a = array_flip(array("B", "KB", "MB", "GB", "TB", "PB"));
  171. $b = array_flip(array("B", "K", "M", "G", "T", "P"));
  172. $pos = isset($a[$suffix]) && $a[$suffix] !== 0 ? $a[$suffix] : $b[$suffix];
  173. return round($size * pow(1024, $pos), $dec);
  174. }
  175. private function getFileType($extension)
  176. {
  177. $type_list = [
  178. 'image' => ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp',],
  179. 'video' => ['mp4', 'flv', 'ogg', 'mov',],
  180. ];
  181. foreach ($type_list as $type => $exs) {
  182. if (in_array($extension, $exs)) {
  183. return $type . "/" . $extension;
  184. }
  185. }
  186. return '';
  187. }
  188. private function mkdir($dir)
  189. {
  190. if (!is_dir($dir)) {
  191. if (!$this->mkdir(dirname($dir))) {
  192. return false;
  193. }
  194. if (!mkdir($dir)) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. /**
  201. * @param null $name
  202. * @return array
  203. */
  204. public function setDriver($name = null)
  205. {
  206. /**
  207. * @var StorageComponent $storage
  208. */
  209. $storage = \Yii::$app->storage;
  210. $driver = $this->getDriverConf();
  211. $storage->setDriver($driver);
  212. return $storage->getUploadedFile($name);
  213. }
  214. public function getDriverConf() {
  215. switch ($this->upload_config->storage_type) {
  216. case Storage::PLATFORM_ALIYUN:
  217. $config = Json::decode($this->upload_config->aliyun);
  218. $driver = [
  219. 'class' => Aliyun::class,
  220. 'accessKey' => $config['access_key'],
  221. 'secretKey' => $config['secret_key'],
  222. 'bucket' => $config['bucket'],
  223. 'isCName' => $config['CNAME'],
  224. 'endPoint' => $config['domain'],
  225. ];
  226. $driver['urlCallback'] = function ($objectUrl, $saveTo) use ($config) {
  227. $url = $objectUrl;
  228. if ($config['style_api']) {
  229. $url = $url . '?' . $config['style_api'];
  230. }
  231. return $url;
  232. };
  233. break;
  234. case Storage::PLATFORM_QINIU:
  235. $config = Json::decode($this->upload_config->qiniu);
  236. if(!$config['domain']){
  237. $configq = Json::decode($this->upload_config->qcloud);
  238. $config['domain'] = $configq['region'];
  239. }
  240. $driver = [
  241. 'class' => Qiniu::class,
  242. //saas后台配置参数名是secret_ke,独立商城参数名是access_key
  243. 'accessKey' => isset($config['secret_ke']) ? $config['secret_ke'] : $config['access_key'],
  244. 'secretKey' => $config['secret_key'],
  245. 'region' => $config['domain'],
  246. 'bucket' => $config['bucket'],
  247. 'zone' => $config['zone'],
  248. ];
  249. $driver['urlCallback'] = function ($objectUrl, $saveTo) use ($config) {
  250. $url = $config['domain'] . '/' . $saveTo;
  251. if ($config['style_api']) {
  252. $url = $url . '?' . $config['style_api'];
  253. }
  254. return $url;
  255. };
  256. break;
  257. case Storage::PLATFORM_QCLOUD:
  258. $config = Json::decode($this->upload_config->qcloud);
  259. preg_match('/(.*?)-(\d+)\.cos\.?(.*?)\.myqcloud\.com/', $config['region'], $region);
  260. if (!$region || $region == 0) {
  261. return [
  262. 'code' => 1,
  263. 'msg' => '默认域名不正确'
  264. ];
  265. }
  266. $driver = [
  267. 'class' => Qcloud::class,
  268. 'accessKey' => $config['secret_id'],
  269. 'secretKey' => $config['secret_key'],
  270. 'bucket' => $config['bucket'],
  271. 'region' => $region[3],
  272. 'appId' => $region[2],
  273. 'urlCallBack' => function ($objectUrl, $saveTo) use ($config) {
  274. $url = ($config['domain'] ? $config['domain'] : $config['region']) . '/' . $saveTo;
  275. if ($config['style']) {
  276. $url = $url . '?' . $config['style'];
  277. }
  278. return $url;
  279. }
  280. ];
  281. break;
  282. default:
  283. $driver = [
  284. 'class' => Local::class
  285. ];
  286. break;
  287. }
  288. return $driver;
  289. }
  290. /**
  291. * 临时文件存储
  292. * @param integer $type
  293. * @return array
  294. */
  295. public function saveFileTemp($type = Storage::TYPE_IMAGE)
  296. {
  297. $name = $type === Storage::TYPE_IMAGE ? 'image' : 'video';
  298. /**
  299. * @var StorageComponent $storageTemp
  300. */
  301. $storageTemp = \Yii::$app->storageTemp;
  302. $storageTemp->setDriver([
  303. 'class' => Local::class
  304. ]);
  305. $file = $storageTemp->getUploadedFile($name);
  306. $checkRes = $this->checkSize($file, $type);
  307. if ($checkRes['code'] == 1) {
  308. return [
  309. 'code' => 1,
  310. 'msg' => $checkRes['msg']
  311. ];
  312. }
  313. try {
  314. $url = $file->saveAsUniqueHash();
  315. if (!$url) {
  316. return [
  317. 'code' => 1,
  318. 'msg' => '文件创建失败'
  319. ];
  320. }
  321. $res = [
  322. 'code' => 0,
  323. 'msg' => 'success',
  324. 'data' => [
  325. 'url' => $url,
  326. 'extension' => $file->extension,
  327. 'size' => $file->size,
  328. 'type' => $this->getFileType($file->extension)
  329. ]
  330. ];
  331. return $res;
  332. } catch (\Exception $e) {
  333. return [
  334. 'code' => 1,
  335. 'msg' => $e->getMessage()
  336. ];
  337. }
  338. }
  339. /**
  340. * 获取文件保存目录
  341. * @param $name
  342. * @param integer $type
  343. * @return string
  344. */
  345. public function getUploadDir($name, $type = Storage::TYPE_IMAGE)
  346. {
  347. $timeDir = date('Y-m-d');
  348. $typeDir = $type === Storage::TYPE_IMAGE ? 'images' : 'videos';
  349. $dir = sprintf('%s/store_%d/%s/%s', $typeDir, $this->store_id, $timeDir, $name);
  350. if ($this->mch_id > 0) {
  351. $dir = sprintf('%s/store_%d/mch_%d/%s/%s', $typeDir, $this->store_id, $this->mch_id, $timeDir, $name);
  352. }
  353. if ($this->supplier_id > 0) {
  354. $dir = sprintf('%s/supplier_%d/%s/%s', $typeDir, $this->supplier_id, $timeDir, $name);
  355. }
  356. if ($this->bd_id > 0) {
  357. $dir = sprintf('%s/bd_%d/%s/%s', $typeDir, $this->bd_id, $timeDir, $name);
  358. }
  359. return $dir;
  360. }
  361. /**
  362. * 检查文件mime和扩展名
  363. * @param $ext
  364. * @param $mime
  365. * @param int $type
  366. * @return bool
  367. */
  368. public function checkMime($ext, $mime, $type = Storage::TYPE_IMAGE)
  369. {
  370. $extType = Storage::IMAGE_TYPE;
  371. $mimeType = Storage::IMAGE_MIME;
  372. if ($type == Storage::TYPE_VIDEO) {
  373. $extType = Storage::VIDEO_TYPE;
  374. $mimeType = Storage::VIDEO_MIME;
  375. }
  376. if ($type == Storage::TYPE_AUDIO) {
  377. $extType = Storage::AUDIO_TYPE;
  378. $mimeType = Storage::AUDIO_MIME;
  379. }
  380. if ($type == Storage::TYPE_XlSX) {
  381. $extType = Storage::XlSX_TYPE;
  382. $mimeType = Storage::XlSX_MIME;
  383. }
  384. if ($type == Storage::TYPE_CRT || $type == Storage::TYPE_CER) {
  385. $extType = Storage::CRT_TYPE;
  386. $mimeType = Storage::CRT_MIME;
  387. }
  388. if ($type == Storage::TYPE_APK) {
  389. $extType = Storage::APK_TYPE;
  390. $mimeType = Storage::APK_MIME;
  391. }
  392. if ($type == Storage::TYPE_PFX) {
  393. $extType = Storage::PFX_TYPE;
  394. $mimeType = Storage::PFX_MIME;
  395. }
  396. if ($type == Storage::TYPE_PEM) {
  397. $extType = Storage::PEM_TYPE;
  398. $mimeType = Storage::PEM_MIME;
  399. }
  400. if (in_array($ext, $extType) && in_array($mime, $mimeType)) {
  401. return true;
  402. }
  403. return false;
  404. }
  405. //获取网络图片到临时目录
  406. public function saveTempImage($url)
  407. {
  408. if (strpos($url, 'http') === false) {
  409. $url = 'http:' . trim($url);
  410. }
  411. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  412. mkdir(\Yii::$app->runtimePath . '/image');
  413. }
  414. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  415. \app\utils\CurlHelper::download($url, $save_path);
  416. return $save_path;
  417. }
  418. //下载保存图片
  419. public function downloadImg($url = '')
  420. {
  421. $type = Storage::TYPE_IMAGE;
  422. try {
  423. $tmpfile = $this->saveTempImage($url);
  424. $uniqueName = md5($url . md5(microtime(true)));
  425. $saveDir = $this->getUploadDir($uniqueName, $type);
  426. $this->setDriver('images');
  427. $path = \Yii::$app->storage->getBasePath() . '/' . $saveDir . '.jpg';
  428. $saveFile = \Yii::$app->storage->driver->saveFile($tmpfile, $path);
  429. @unlink($tmpfile);
  430. if ($saveFile) {
  431. return [
  432. 'code' => 0,
  433. 'msg' => 'ok',
  434. 'url' => $saveFile,
  435. ];
  436. }
  437. return [
  438. 'code' => 1,
  439. 'msg' => '图片保存错误',
  440. ];
  441. } catch (\Throwable $throwable) {
  442. \Yii::error([__METHOD__, '$throwable', $throwable]);
  443. return [
  444. 'code' => 1,
  445. 'msg' => $throwable->getMessage(),
  446. ];
  447. }
  448. }
  449. /**
  450. * 上传文件
  451. * @param int $type
  452. * @return array
  453. */
  454. public function uploadFile($type = Storage::TYPE_IMAGE)
  455. {
  456. $name = 'images';
  457. if ($type === Storage::TYPE_VIDEO) {
  458. $name = 'videos';
  459. } elseif ($type === Storage::TYPE_AUDIO) {
  460. $name = 'audios';
  461. } elseif ($type === Storage::TYPE_XlSX) {
  462. $name = 'xlsx';
  463. } elseif ($type === Storage::TYPE_CRT) {
  464. $name = 'crt';
  465. } elseif ($type === Storage::TYPE_APK) {
  466. $name = 'apk';
  467. } elseif ($type === Storage::TYPE_PFX) {
  468. $name = 'pfx';
  469. } elseif ($type === Storage::TYPE_CER) {
  470. $name = 'cer';
  471. } elseif ($type === Storage::TYPE_PEM) {
  472. $name = 'pem';
  473. }
  474. // $name = $type === Storage::TYPE_IMAGE ? 'images' : 'videos';
  475. $files = $this->setDriver($name);
  476. $results = [];
  477. foreach ($files as $file) {
  478. /**
  479. * @var UploadFile $file
  480. */
  481. $result = [
  482. 'status' => true,
  483. ];
  484. try {
  485. $checkRes = $this->checkSize($file, $type);
  486. if ($checkRes['code'] == 1) {
  487. throw new \Exception($checkRes['msg']);
  488. }
  489. $uniqueName = sha1_file($file->tempName);
  490. $saveDir = $this->getUploadDir($uniqueName, $type);
  491. $url = $file->saveWithOriginalExtension($saveDir);
  492. $result['url'] = $url;
  493. if (strpos($url, 'http') === false) {
  494. $result['url'] = 'https://' . $url;
  495. }
  496. $this->saveStorage($file, $saveDir, $result['url'], $type);
  497. } catch (\Throwable $throwable) {
  498. $result = [
  499. 'status' => false,
  500. 'error' => $throwable->getMessage(),
  501. ];
  502. }
  503. $results[] = $result;
  504. }
  505. return $results;
  506. }
  507. /**
  508. * 客户端上传文件
  509. * @param int $type
  510. * @return array
  511. */
  512. public function uploadClientFile($type = Storage::TYPE_IMAGE)
  513. {
  514. $name = $type === Storage::TYPE_IMAGE ? 'image' : 'video';
  515. /**
  516. * @var UploadFile $file
  517. */
  518. $file = $this->setDriver($name);
  519. try {
  520. $result = [
  521. 'code' => 0,
  522. 'msg' => 'success',
  523. ];
  524. $checkRes = $this->checkSize($file, $type);
  525. if ($checkRes['code'] == 1) {
  526. throw new \Exception($checkRes['msg']);
  527. }
  528. $uniqueName = sha1_file($file->tempName);
  529. $saveDir = $this->getUploadDir($uniqueName, $type);
  530. $url = $file->saveWithOriginalExtension($saveDir);
  531. $result['url'] = $url;
  532. if (strpos($url, 'http') === false) {
  533. $result['url'] = 'https://' . $url;
  534. }
  535. if ($type === Storage::TYPE_VIDEO) {
  536. $result['cover_pic_url'] = '';
  537. if ($this->upload_config->storage_type == Storage::PLATFORM_QINIU) {
  538. if (!empty($result['url'])) {
  539. $result['cover_pic_url'] = $result['url'] . '?vframe/jpg/offset/1/w/200/h/200';
  540. }
  541. }
  542. }
  543. } catch (\Throwable $throwable) {
  544. \Yii::error([__METHOD__, '$throwable', $throwable]);
  545. $result = [
  546. 'code' => 1,
  547. 'msg' => $throwable->getMessage(),
  548. ];
  549. }
  550. return $result;
  551. }
  552. /**
  553. * 本地文件上传到云
  554. * @param $file
  555. * @return array
  556. */
  557. public function local2cloud($file)
  558. {
  559. $cache = cache();
  560. $k = ['local2cloud', $file];
  561. $v = $cache->get($k);
  562. if($v){
  563. return [
  564. 'cache' => true,
  565. 'status' => true,
  566. 'url' => $v,
  567. ];
  568. }
  569. $storage = \Yii::$app->storageTemp;
  570. $storage->setDriver($this->getDriverConf());
  571. $driver = $storage->getDriver();
  572. if ($driver instanceof Local){
  573. return null;
  574. }
  575. $result = [
  576. 'status' => true,
  577. 'cache' => false,
  578. ];
  579. try {
  580. $uniqueName = sha1_file($file) . '.' . strtolower(pathinfo($file, PATHINFO_EXTENSION));
  581. $saveDir = $this->getUploadDir($uniqueName, Storage::TYPE_IMAGE);
  582. $saveDir = rtrim($storage->basePath, '/') . '/' . ltrim($saveDir, '/');
  583. $url = $driver->saveFile($file, $saveDir);
  584. $result['url'] = $url;
  585. if (strpos($url, 'http') === false) {
  586. $result['url'] = 'https://' . $url;
  587. }
  588. $cache->set($k, $result['url']);
  589. } catch (\Throwable $throwable) {
  590. \Yii::error($throwable);
  591. $result = [
  592. 'status' => false,
  593. 'error' => $throwable->getMessage(),
  594. ];
  595. }
  596. return $result;
  597. }
  598. /**
  599. * 本地文件上传到云
  600. * @param $file
  601. * @return $file
  602. */
  603. public function local2cloudLink($file)
  604. {
  605. $local2cloud = $this->local2cloud($file);
  606. if($local2cloud && $local2cloud['status']){
  607. return $local2cloud['url'];
  608. }
  609. return null;
  610. }
  611. /**
  612. * @param UploadFile $file
  613. * @param $saveDir
  614. * @param $url
  615. * @param $type
  616. * @return bool
  617. */
  618. public function saveStorage(UploadFile $file, $saveDir, $url, $type)
  619. {
  620. $form = new Storage();
  621. $form->parent_id = $this->parent_id;
  622. $form->name = $file->getBaseName();
  623. $form->mime = $file->type;
  624. $form->ext = $file->getExtension();
  625. $form->size = $file->size;
  626. $path = \Yii::$app->storage->getBasePath() . '/' . $saveDir . '.' . $file->getExtension();
  627. $form->path = $path;
  628. $form->url = $url;
  629. $form->platform = isset($this->upload_config->storage_type) ? $this->upload_config->storage_type : Storage::PLATFORM_LOCAL;
  630. $form->type = $type;
  631. $form->store_id = $this->store_id;
  632. $form->mch_id = $this->mch_id;
  633. $form->supplier_id = $this->supplier_id;
  634. $form->bd_id = $this->bd_id;
  635. return $form->save();
  636. }
  637. /**
  638. * 检查文件大小
  639. * @param UploadFile $file
  640. * @param int $type
  641. * @return array
  642. */
  643. public function checkSize(UploadFile $file, $type = Storage::TYPE_IMAGE) {
  644. $maxUploadSize = self::getMaxUploadSize($type, $this->supplier_id);
  645. $uploadSize = $maxUploadSize * 1024 * 1024;
  646. // 检查mime和扩展名
  647. if (false === $this->checkMime($file->getExtension(), $file->type, $type)) {
  648. return [
  649. 'code' => 1,
  650. 'msg' => '文件格式不正确'
  651. ];
  652. }
  653. // 检查文件大小
  654. if ($file->size > $uploadSize) {
  655. return [
  656. 'code' => 1,
  657. 'msg' => '上传文件超过 ' . ($maxUploadSize ?: 0) . 'M'
  658. ];
  659. }
  660. return [
  661. 'code' => 0
  662. ];
  663. }
  664. }