SystemAttachmentLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\app\model\system\SystemCategory;
  11. use plugin\saiadmin\basic\think\BaseLogic;
  12. use plugin\saiadmin\exception\ApiException;
  13. use plugin\saiadmin\service\storage\ChunkUploadService;
  14. use plugin\saiadmin\service\storage\UploadService;
  15. use plugin\saiadmin\utils\Arr;
  16. use plugin\saiadmin\utils\Helper;
  17. /**
  18. * 附件逻辑层
  19. */
  20. class SystemAttachmentLogic extends BaseLogic
  21. {
  22. /**
  23. * 构造函数
  24. */
  25. public function __construct()
  26. {
  27. $this->model = new SystemAttachment();
  28. }
  29. /**
  30. * @param $category_id
  31. * @param $ids
  32. * @return mixed
  33. */
  34. public function move($category_id, $ids): mixed
  35. {
  36. $category = SystemCategory::where('id', $category_id)->findOrEmpty();
  37. if ($category->isEmpty()) {
  38. throw new ApiException('目标分类不存在');
  39. }
  40. return $this->model->whereIn('id', $ids)->update(['category_id' => $category_id]);
  41. }
  42. /**
  43. * 保存网络图片
  44. * @param $url
  45. * @param $config
  46. * @return array
  47. * @throws ApiException|Exception
  48. */
  49. public function saveNetworkImage($url, $config): array
  50. {
  51. $image_data = file_get_contents($url);
  52. if ($image_data === false) {
  53. throw new ApiException('获取文件资源失败');
  54. }
  55. $image_resource = imagecreatefromstring($image_data);
  56. if (!$image_resource) {
  57. throw new ApiException('创建图片资源失败');
  58. }
  59. $filename = basename($url);
  60. $file_extension = pathinfo($filename, PATHINFO_EXTENSION);
  61. $full_dir = runtime_path() . '/resource/';
  62. if (!is_dir($full_dir)) {
  63. mkdir($full_dir, 0777, true);
  64. }
  65. $save_path = $full_dir . $filename;
  66. $mime_type = 'image/';
  67. switch ($file_extension) {
  68. case 'jpg':
  69. case 'jpeg':
  70. $mime_type = 'image/jpeg';
  71. $result = imagejpeg($image_resource, $save_path);
  72. break;
  73. case 'png':
  74. $mime_type = 'image/png';
  75. $result = imagepng($image_resource, $save_path);
  76. break;
  77. case 'gif':
  78. $mime_type = 'image/gif';
  79. $result = imagegif($image_resource, $save_path);
  80. break;
  81. default:
  82. imagedestroy($image_resource);
  83. throw new ApiException('文件格式错误');
  84. }
  85. imagedestroy($image_resource);
  86. if (!$result) {
  87. throw new ApiException('文件保存失败');
  88. }
  89. $hash = md5_file($save_path);
  90. $size = filesize($save_path);
  91. $model = $this->model->where('hash', $hash)->find();
  92. if ($model) {
  93. unlink($save_path);
  94. return $model->toArray();
  95. } else {
  96. $logic = new SystemConfigLogic();
  97. $uploadConfig = $logic->getGroup('upload_config');
  98. $root = Arr::getConfigValue($uploadConfig, 'local_root');
  99. $folder = date('Ymd');
  100. $full_dir = base_path() . DIRECTORY_SEPARATOR . $root . $folder . DIRECTORY_SEPARATOR;
  101. if (!is_dir($full_dir)) {
  102. mkdir($full_dir, 0777, true);
  103. }
  104. $object_name = bin2hex(pack('Nn', time(), random_int(1, 65535))) . ".$file_extension";
  105. $newPath = $full_dir . $object_name;
  106. copy($save_path, $newPath);
  107. unlink($save_path);
  108. $domain = Arr::getConfigValue($uploadConfig, 'local_domain');
  109. $uri = Arr::getConfigValue($uploadConfig, 'local_uri');
  110. $baseUrl = $domain . $uri . $folder . '/';
  111. $info['storage_mode'] = 1;
  112. $info['category_id'] = request()->input('category_id', 1);
  113. $info['origin_name'] = $filename;
  114. $info['object_name'] = $object_name;
  115. $info['hash'] = $hash;
  116. $info['mime_type'] = $mime_type;
  117. $info['storage_path'] = $root . $folder . '/' . $object_name;
  118. $info['suffix'] = $file_extension;
  119. $info['size_byte'] = $size;
  120. $info['size_info'] = formatBytes($size);
  121. $info['url'] = $baseUrl . $object_name;
  122. $this->model->save($info);
  123. return $info;
  124. }
  125. }
  126. /**
  127. * 文件上传
  128. * @param string $upload
  129. * @param bool $local
  130. * @return array
  131. */
  132. public function uploadBase(string $upload = 'image', bool $local = false): array
  133. {
  134. $logic = new SystemConfigLogic();
  135. $uploadConfig = $logic->getGroup('upload_config');
  136. $type = Arr::getConfigValue($uploadConfig, 'upload_mode');
  137. if ($local === true) {
  138. $type = 1;
  139. }
  140. $result = UploadService::disk($type, $upload)->uploadFile();
  141. $data = $result[0];
  142. $hash = $data['unique_id'];
  143. $hash_check = config('plugin.saiadmin.saithink.file_hash', false);
  144. if ($hash_check) {
  145. $model = $this->model->where('hash', $hash)->findOrEmpty();
  146. if (!$model->isEmpty()) {
  147. return $model->toArray();
  148. }
  149. }
  150. $url = str_replace('\\', '/', $data['url']);
  151. $savePath = str_replace('\\', '/', $data['save_path']);
  152. $info['storage_mode'] = $type;
  153. $info['category_id'] = request()->input('category_id', 1);
  154. $info['origin_name'] = $data['origin_name'];
  155. $info['object_name'] = $data['save_name'];
  156. $info['hash'] = $data['unique_id'];
  157. $info['mime_type'] = $data['mime_type'];
  158. $info['storage_path'] = $savePath;
  159. $info['suffix'] = $data['extension'];
  160. $info['size_byte'] = $data['size'];
  161. $info['size_info'] = formatBytes($data['size']);
  162. $info['url'] = $url;
  163. $this->model->save($info);
  164. return $info;
  165. }
  166. /**
  167. * 切片上传
  168. * @param $data
  169. * @return array
  170. */
  171. public function chunkUpload($data): array
  172. {
  173. $chunkService = new ChunkUploadService();
  174. if ($data['index'] == 0) {
  175. $model = $this->model->where('hash', $data['hash'])->findOrEmpty();
  176. if (!$model->isEmpty()) {
  177. return $model->toArray();
  178. } else {
  179. return $chunkService->checkChunk($data);
  180. }
  181. } else {
  182. return $chunkService->uploadChunk($data);
  183. }
  184. }
  185. }