SystemAttachmentLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use Exception;
  9. use plugin\saiadmin\app\model\system\SystemAttachment;
  10. use plugin\saiadmin\basic\BaseLogic;
  11. use plugin\saiadmin\exception\ApiException;
  12. use plugin\saiadmin\service\storage\UploadService;
  13. use plugin\saiadmin\utils\Arr;
  14. use plugin\saiadmin\utils\Helper;
  15. use support\Request;
  16. /**
  17. * 角色逻辑层
  18. */
  19. class SystemAttachmentLogic extends BaseLogic
  20. {
  21. /**
  22. * 构造函数
  23. */
  24. public function __construct()
  25. {
  26. $this->model = new SystemAttachment();
  27. }
  28. /**
  29. * 保存网络图片
  30. * @param $url
  31. * @param $config
  32. * @return array
  33. * @throws ApiException|Exception
  34. */
  35. public function saveNetworkImage($url, $config): array
  36. {
  37. $image_data = file_get_contents($url);
  38. if ($image_data === false) {
  39. throw new ApiException('获取文件资源失败');
  40. }
  41. $image_resource = imagecreatefromstring($image_data);
  42. if (!$image_resource) {
  43. throw new ApiException('创建图片资源失败');
  44. }
  45. $filename = basename($url);
  46. $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
  47. $full_dir = runtime_path() . '/resource/';
  48. if (!is_dir($full_dir)) {
  49. mkdir($full_dir, 0777, true);
  50. }
  51. $save_path = $full_dir . $filename;
  52. $mime_type = 'image/';
  53. switch($file_extension) {
  54. case 'jpg':
  55. case 'jpeg':
  56. $mime_type = 'image/jpeg';
  57. $result = imagejpeg($image_resource, $save_path);
  58. break;
  59. case 'png':
  60. $mime_type = 'image/png';
  61. $result = imagepng($image_resource, $save_path);
  62. break;
  63. case 'gif':
  64. $mime_type = 'image/gif';
  65. $result = imagegif($image_resource, $save_path);
  66. break;
  67. default:
  68. imagedestroy($image_resource);
  69. throw new ApiException('文件格式错误');
  70. }
  71. imagedestroy($image_resource);
  72. if (!$result) {
  73. throw new ApiException('文件保存失败');
  74. }
  75. $hash = md5_file($save_path);
  76. $size = filesize($save_path);
  77. $model = $this->model->where('hash', $hash)->find();
  78. if ($model) {
  79. unlink($save_path);
  80. return $model->toArray();
  81. } else {
  82. $logic = new SystemConfigLogic();
  83. $uploadConfig = $logic->getGroup('upload_config');
  84. $root = Arr::getConfigValue($uploadConfig,'local_root');
  85. $folder = date('Ymd');
  86. $full_dir = base_path().DIRECTORY_SEPARATOR.$root.$folder.DIRECTORY_SEPARATOR;
  87. if (!is_dir($full_dir)) {
  88. mkdir($full_dir, 0777, true);
  89. }
  90. $object_name = bin2hex(pack('Nn',time(), random_int(1, 65535))) . ".$file_extension";
  91. $newPath = $full_dir . $object_name;
  92. copy($save_path, $newPath);
  93. unlink($save_path);
  94. $domain = Arr::getConfigValue($uploadConfig,'local_domain');
  95. $uri = Arr::getConfigValue($uploadConfig,'local_uri');
  96. $baseUrl = $domain.$uri.$folder.'/';
  97. $info['storage_mode'] = 1;
  98. $info['origin_name'] = $filename;
  99. $info['object_name'] = $object_name;
  100. $info['hash'] = $hash;
  101. $info['mime_type'] = $mime_type;
  102. $info['storage_path'] = $root.$folder.'/'.$object_name;
  103. $info['suffix'] = $file_extension;
  104. $info['size_byte'] = $size;
  105. $info['size_info'] = formatBytes($size);
  106. $info['url'] = $baseUrl . $object_name;
  107. $this->model->save($info);
  108. return $info;
  109. }
  110. }
  111. /**
  112. * 文件上传
  113. * @param string $upload
  114. * @param bool $local
  115. * @return array
  116. */
  117. public function uploadBase($upload = 'image', $local = false)
  118. {
  119. $logic = new SystemConfigLogic();
  120. $uploadConfig = $logic->getGroup('upload_config');
  121. $type = Arr::getConfigValue($uploadConfig, 'upload_mode');
  122. if ($local === true) {
  123. $type = 1;
  124. }
  125. $result = UploadService::disk($type, $upload)->uploadFile();
  126. $data = $result[0];
  127. $hash = $data['unique_id'];
  128. $hash_check = config('plugin.saiadmin.saithink.file_hash', false);
  129. if ($hash_check) {
  130. $model = $this->model->where('hash', $hash)->findOrEmpty();
  131. if (!$model->isEmpty()) {
  132. return $model->toArray();
  133. }
  134. }
  135. $url = str_replace('\\', '/', $data['url']);
  136. $savePath = str_replace('\\', '/', $data['save_path']);
  137. $info['storage_mode'] = $type;
  138. $info['origin_name'] = $data['origin_name'];
  139. $info['object_name'] = $data['save_name'];
  140. $info['hash'] = $data['unique_id'];
  141. $info['mime_type'] = $data['mime_type'];
  142. $info['storage_path'] = $savePath;
  143. $info['suffix'] = $data['extension'];
  144. $info['size_byte'] = $data['size'];
  145. $info['size_info'] = formatBytes($data['size']);
  146. $info['url'] = $url;
  147. $this->model->save($info);
  148. return $info;
  149. }
  150. }