Local.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\upload\storage;
  12. use crmeb\services\upload\BaseUpload;
  13. use crmeb\exceptions\AdminException;
  14. use crmeb\utils\DownloadImage;
  15. use think\exception\ValidateException;
  16. use think\facade\Config;
  17. use think\facade\Filesystem;
  18. use think\File;
  19. use think\Image;
  20. /**
  21. * 本地上传
  22. * Class Local
  23. * @package crmeb\services\upload\storage
  24. */
  25. class Local extends BaseUpload
  26. {
  27. /**
  28. * 默认存放路径
  29. * @var string
  30. */
  31. protected $defaultPath;
  32. /**
  33. * 缩略图、水印图存放位置
  34. * @var string
  35. */
  36. public $thumbWaterPath = 'thumb_water';
  37. public function initialize(array $config)
  38. {
  39. parent::initialize($config);
  40. $this->defaultPath = Config::get('filesystem.disks.' . Config::get('filesystem.default') . '.url');
  41. $this->waterConfig['watermark_text_font'] = app()->getRootPath() . 'public' . '/statics/font/simsunb.ttf';
  42. }
  43. protected function app()
  44. {
  45. // TODO: Implement app() method.
  46. }
  47. public function getTempKeys()
  48. {
  49. // TODO: Implement getTempKeys() method.
  50. return $this->setError('请检查您的上传配置,视频默认oss上传');
  51. }
  52. /**
  53. * 生成上传文件目录
  54. * @param $path
  55. * @param null $root
  56. * @return string
  57. */
  58. public function uploadDir($path, $root = null)
  59. {
  60. if ($root === null) $root = app()->getRootPath() . 'public/';
  61. return str_replace('\\', '/', $root . 'uploads/' . $path);
  62. }
  63. /**
  64. * 检查上传目录不存在则生成
  65. * @param $dir
  66. * @return bool
  67. */
  68. protected function validDir($dir)
  69. {
  70. return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
  71. }
  72. /**
  73. * 检测filepath是否是远程地址
  74. * @param string $filePath
  75. * @return bool
  76. */
  77. public function checkFilePathIsRemote(string $filePath)
  78. {
  79. return strpos($filePath, 'https:') !== false || strpos($filePath, 'http:') !== false || substr($filePath, 0, 2) === '//';
  80. }
  81. /**
  82. * 生成与配置相关的文件名称以及路径
  83. * @param string $filePath 原地址
  84. * @param string $toPath 保存目录
  85. * @param array $config 配置相关参数
  86. * @param string $root
  87. * @return string
  88. */
  89. public function createSaveFilePath(string $filePath, string $toPath, array $config = [], $root = '/')
  90. {
  91. [$path, $ext] = $this->getFileName($filePath);
  92. $fileName = md5(json_encode($config) . $filePath);
  93. return $this->uploadDir($toPath, $root) . '/' . $fileName . '.' . $ext;
  94. }
  95. /**
  96. * 文件上传
  97. * @param string $file
  98. * @return array|bool|mixed|\StdClass
  99. */
  100. public function move(string $file = 'file', $realName = false)
  101. {
  102. $fileHandle = app()->request->file($file);
  103. if (!$fileHandle) {
  104. return $this->setError('上传的文件不存在');
  105. }
  106. if ($this->validate) {
  107. if (!in_array(pathinfo($fileHandle->getOriginalName(), PATHINFO_EXTENSION), $this->validate['fileExt'])) {
  108. return $this->setError('不合法的文件后缀');
  109. }
  110. if (filesize($fileHandle) > $this->validate['filesize']) {
  111. return $this->setError('文件过大');
  112. }
  113. if (!in_array($fileHandle->getOriginalMime(), $this->validate['fileMime'])) {
  114. return $this->setError('不合法的文件类型');
  115. }
  116. }
  117. if ($realName) {
  118. $fileName = Filesystem::putFileAs($this->path, $fileHandle, $fileHandle->getOriginalName());
  119. } else {
  120. $fileName = Filesystem::putFile($this->path, $fileHandle);
  121. }
  122. if (!$fileName)
  123. return $this->setError('Upload failure');
  124. $filePath = Filesystem::path($fileName);
  125. $this->fileInfo->uploadInfo = new File($filePath);
  126. $this->fileInfo->realName = $fileHandle->getOriginalName();
  127. $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
  128. $this->fileInfo->filePath = $this->defaultPath . '/' . str_replace('\\', '/', $fileName);
  129. if ($this->checkImage(public_path() . $this->fileInfo->filePath) && $this->authThumb && pathinfo($fileName, PATHINFO_EXTENSION) != 'ico' && pathinfo($fileName, PATHINFO_EXTENSION) != 'gif') {
  130. try {
  131. $this->thumb($this->fileInfo->filePath, $this->fileInfo->fileName);
  132. } catch (\Throwable $e) {
  133. return $this->setError($e->getMessage());
  134. }
  135. }
  136. return $this->fileInfo;
  137. }
  138. /**
  139. * 文件流上传
  140. * @param $fileContent
  141. * @param string|null $key
  142. * @return array|bool|mixed|\StdClass
  143. */
  144. public function stream($fileContent, string $key = null)
  145. {
  146. if (!$key) {
  147. $key = $this->saveFileName();
  148. }
  149. $dir = $this->uploadDir($this->path);
  150. if (!$this->validDir($dir)) {
  151. return $this->setError('Failed to generate upload directory, please check the permission!');
  152. }
  153. $fileName = $dir . '/' . $key;
  154. file_put_contents($fileName, $fileContent);
  155. $this->fileInfo->uploadInfo = new File($fileName);
  156. $this->fileInfo->realName = $key;
  157. $this->fileInfo->fileName = $key;
  158. $this->fileInfo->filePath = $this->defaultPath . '/' . $this->path . '/' . $key;
  159. if ($this->checkImage(public_path() . $this->fileInfo->filePath) && $this->authThumb) {
  160. try {
  161. $this->thumb($this->fileInfo->filePath, $this->fileInfo->fileName);
  162. } catch (\Throwable $e) {
  163. return $this->setError($e->getMessage());
  164. }
  165. }
  166. return $this->fileInfo;
  167. }
  168. /**
  169. * 文件流下载保存图片
  170. * @param string $fileContent
  171. * @param string|null $key
  172. * @return array|bool|mixed|\StdClass
  173. */
  174. public function down(string $fileContent, string $key = null)
  175. {
  176. if (!$key) {
  177. $key = $this->saveFileName();
  178. }
  179. $dir = $this->uploadDir($this->path);
  180. if (!$this->validDir($dir)) {
  181. return $this->setError('Failed to generate upload directory, please check the permission!');
  182. }
  183. $fileName = $dir . '/' . $key;
  184. file_put_contents($fileName, $fileContent);
  185. $this->downFileInfo->downloadInfo = new File($fileName);
  186. $this->downFileInfo->downloadRealName = $key;
  187. $this->downFileInfo->downloadFileName = $key;
  188. $this->downFileInfo->downloadFilePath = $this->defaultPath . '/' . $this->path . '/' . $key;
  189. return $this->downFileInfo;
  190. }
  191. /**
  192. * 生成缩略图
  193. * @param string $filePath
  194. * @param string $fileName
  195. * @param string $type
  196. * @return array|mixed|string[]
  197. */
  198. public function thumb(string $filePath = '', string $fileName = '', string $type = 'all')
  199. {
  200. $config = $this->thumbConfig;
  201. $data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath];
  202. $this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath;
  203. //地址存在且不是远程地址
  204. $filePath = str_replace(sys_config('site_url'), '', $filePath);
  205. if ($filePath && !$this->checkFilePathIsRemote($filePath)) {
  206. $findPath = str_replace($fileName, $type . '_' . $fileName, $filePath);
  207. if (file_exists('.' . $findPath)) {
  208. return [
  209. 'big' => str_replace($fileName, 'big_' . $fileName, $filePath),
  210. 'mid' => str_replace($fileName, 'mid_' . $fileName, $filePath),
  211. 'small' => str_replace($fileName, 'small_' . $fileName, $filePath)
  212. ];
  213. }
  214. try {
  215. $this->water($filePath);
  216. foreach ($this->thumb as $v) {
  217. if ($type == 'all' || $type == $v) {
  218. $height = 'thumb_' . $v . '_height';
  219. $width = 'thumb_' . $v . '_width';
  220. $savePath = str_replace($fileName, $v . '_' . $fileName, $filePath);
  221. //防止重复生成
  222. if (!file_exists('.' . $savePath)) {
  223. $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
  224. $Image->thumb($config[$width], $config[$height])->save(root_path() . 'public' . $savePath);
  225. }
  226. $key = 'filePath' . ucfirst($v);
  227. $data[$v] = $this->fileInfo->$key = $savePath;
  228. }
  229. }
  230. } catch (\Throwable $e) {
  231. throw new AdminException($e->getMessage());
  232. }
  233. }
  234. return $data;
  235. }
  236. /**
  237. * 添加水印
  238. * @param string $filePath
  239. * @return mixed|string
  240. */
  241. public function water(string $filePath = '')
  242. {
  243. $waterConfig = $this->waterConfig;
  244. if ($waterConfig['image_watermark_status'] && $filePath) {
  245. switch ($waterConfig['watermark_type']) {
  246. case 1:
  247. if ($waterConfig['watermark_image']) $filePath = $this->image($filePath, $waterConfig);
  248. break;
  249. case 2:
  250. $filePath = $this->text($filePath, $waterConfig);
  251. break;
  252. }
  253. }
  254. return $filePath;
  255. }
  256. /**
  257. * 图片水印
  258. * @param string $filePath
  259. * @param array $waterConfig
  260. * @param string $waterPath
  261. * @return string
  262. */
  263. public function image(string $filePath, array $waterConfig = [])
  264. {
  265. if (!$waterConfig) {
  266. $waterConfig = $this->waterConfig;
  267. }
  268. $watermark_image = $waterConfig['watermark_image'];
  269. //远程图片
  270. $filePath = str_replace(sys_config('site_url'), '', $filePath);
  271. if ($watermark_image && $this->checkFilePathIsRemote($watermark_image)) {
  272. //看是否在本地
  273. $pathName = $this->getFilePath($watermark_image, true);
  274. if ($pathName == $watermark_image) {//不再本地 继续下载
  275. [$p, $e] = $this->getFileName($watermark_image);
  276. $name = 'water_image_' . md5($watermark_image) . '.' . $e;
  277. $watermark_image = '.' . $this->defaultPath . '/' . $this->thumbWaterPath . '/' . $name;
  278. if (!file_exists($watermark_image)) {
  279. try {
  280. /** @var DownloadImage $down */
  281. $down = app()->make(DownloadImage::class);
  282. $data = $down->path($this->thumbWaterPath)->downloadImage($waterConfig['watermark_image'], $name);
  283. $watermark_image = $data['path'] ?? '';
  284. } catch (\Throwable $e) {
  285. throw new AdminException(400724);
  286. }
  287. }
  288. } else {
  289. $watermark_image = '.' . $pathName;
  290. }
  291. }
  292. if (!$watermark_image) {
  293. throw new AdminException(400722);
  294. }
  295. $savePath = public_path() . $filePath;
  296. try {
  297. $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
  298. $Image->water($watermark_image, $waterConfig['watermark_position'] ?: 1, (int)$waterConfig['watermark_opacity'])->save($savePath);
  299. } catch (\Throwable $e) {
  300. throw new AdminException($e->getMessage());
  301. }
  302. return $savePath;
  303. }
  304. /**
  305. * 文字水印
  306. * @param string $filePath
  307. * @param array $waterConfig
  308. * @return string
  309. */
  310. public function text(string $filePath, array $waterConfig = [])
  311. {
  312. if (!$waterConfig) {
  313. $waterConfig = $this->waterConfig;
  314. }
  315. if (!$waterConfig['watermark_text']) {
  316. throw new AdminException(400723);
  317. }
  318. $savePath = public_path() . $filePath;
  319. try {
  320. $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
  321. if (strlen($waterConfig['watermark_text_color']) < 7) {
  322. $waterConfig['watermark_text_color'] = substr($waterConfig['watermark_text_color'], 1);
  323. $waterConfig['watermark_text_color'] = '#' . $waterConfig['watermark_text_color'] . $waterConfig['watermark_text_color'];
  324. }
  325. if (strlen($waterConfig['watermark_text_color']) > 7) {
  326. $waterConfig['watermark_text_color'] = substr($waterConfig['watermark_text_color'], 0, 7);
  327. }
  328. $Image->text($waterConfig['watermark_text'], $waterConfig['watermark_text_font'], (float)$waterConfig['watermark_text_size'], $waterConfig['watermark_text_color'], $waterConfig['watermark_position'], [$waterConfig['watermark_x'], $waterConfig['watermark_y'], $waterConfig['watermark_text_angle']])->save($savePath);
  329. } catch (\Throwable $e) {
  330. throw new AdminException($e->getMessage() . $e->getLine());
  331. }
  332. return $savePath;
  333. }
  334. /**
  335. * 删除文件
  336. * @param string $filePath
  337. * @return bool|mixed
  338. */
  339. public function delete(string $filePath)
  340. {
  341. if (file_exists($filePath)) {
  342. try {
  343. $fileArr = explode('/', $filePath);
  344. $fileName = end($fileArr);
  345. unlink($filePath);
  346. unlink(str_replace($fileName, 'big_' . $fileName, $filePath));
  347. unlink(str_replace($fileName, 'mid_' . $fileName, $filePath));
  348. unlink(str_replace($fileName, 'small_' . $fileName, $filePath));
  349. return true;
  350. } catch (\Exception $e) {
  351. return $this->setError($e->getMessage());
  352. }
  353. }
  354. return false;
  355. }
  356. public function listbuckets(string $region, bool $line = false, bool $shared = false)
  357. {
  358. return [];
  359. }
  360. public function createBucket(string $name, string $region)
  361. {
  362. return null;
  363. }
  364. public function deleteBucket(string $name)
  365. {
  366. return null;
  367. }
  368. public function setBucketCors(string $name, string $region)
  369. {
  370. return true;
  371. }
  372. public function getRegion()
  373. {
  374. return [];
  375. }
  376. public function bindDomian(string $name, string $domain, string $region = null)
  377. {
  378. return true;
  379. }
  380. public function getDomian(string $name, string $region = null)
  381. {
  382. return [];
  383. }
  384. }