Server.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use app\Request;
  4. use think\Exception;
  5. use app\common\service\ConfigService;
  6. use app\common\service\ImageCompressService;
  7. /**
  8. * 存储引擎抽象类
  9. * Class server
  10. * @package app\common\library\storage\drivers
  11. */
  12. abstract class Server
  13. {
  14. protected $file;
  15. protected $error;
  16. protected $fileName;
  17. protected $fileInfo;
  18. // 是否为内部上传
  19. protected $isInternal = false;
  20. /**
  21. * 构造函数
  22. * Server constructor.
  23. */
  24. protected function __construct()
  25. {
  26. }
  27. /**
  28. * 设置上传的文件信息
  29. * @param string $name
  30. * @throws Exception
  31. */
  32. public function setUploadFile($name)
  33. {
  34. // 接收上传的文件
  35. $this->file = app('\app\Request')->file($name);
  36. if (empty($this->file)) {
  37. throw new Exception('未找到上传文件的信息');
  38. }
  39. // 文件信息
  40. $this->fileInfo = [
  41. 'ext' => $this->file->extension(),
  42. 'size' => $this->file->getSize(),
  43. 'mime' => $this->file->getMime(),
  44. 'name' => $this->file->getOriginalName(),
  45. 'realPath' => $this->file->getRealPath(),
  46. ];
  47. // 如果是图片文件,进行压缩处理
  48. if ($this->isImageFile()) {
  49. $compressedPath = $this->compressImage();
  50. if ($compressedPath) {
  51. $this->fileInfo['realPath'] = $compressedPath;
  52. $this->fileInfo['size'] = filesize($compressedPath);
  53. }
  54. }
  55. // 生成保存文件名
  56. $this->fileName = $this->buildSaveName();
  57. }
  58. /**
  59. * 压缩图片
  60. */
  61. protected function compressImage()
  62. {
  63. // 获取压缩配置(可以从配置文件读取)
  64. $quality = ConfigService::get('upload', 'image_quality', 80);
  65. $maxWidth = ConfigService::get('upload', 'max_width', 1920);
  66. $maxHeight = ConfigService::get('upload', 'max_height', 1080);
  67. return ImageCompressService::compress(
  68. $this->fileInfo['realPath'],
  69. $quality,
  70. $maxWidth,
  71. $maxHeight
  72. );
  73. }
  74. /**
  75. * 判断是否为图片文件
  76. */
  77. protected function isImageFile()
  78. {
  79. $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
  80. return in_array(strtolower($this->fileInfo['ext']), $imageExtensions);
  81. }
  82. /**
  83. * 设置上传的文件信息
  84. * @param string $filePath
  85. */
  86. public function setUploadFileByReal($filePath)
  87. {
  88. // 设置为系统内部上传
  89. $this->isInternal = true;
  90. // 文件信息
  91. $this->fileInfo = [
  92. 'name' => basename($filePath),
  93. 'size' => filesize($filePath),
  94. 'tmp_name' => $filePath,
  95. 'error' => 0,
  96. ];
  97. // 生成保存文件名
  98. $this->fileName = $this->buildSaveName();
  99. }
  100. /**
  101. * Notes: 抓取网络资源
  102. * @param $url
  103. * @param $key
  104. * @author 张无忌(2021/3/2 14:15)
  105. * @return mixed
  106. */
  107. abstract protected function fetch($url, $key);
  108. /**
  109. * 文件上传
  110. * @param $save_dir (保存路径)
  111. * @return mixed
  112. */
  113. abstract protected function upload($save_dir);
  114. /**
  115. * 文件删除
  116. * @param $fileName
  117. * @return mixed
  118. */
  119. abstract protected function delete($fileName);
  120. /**
  121. * 返回上传后文件路径
  122. * @return mixed
  123. */
  124. abstract public function getFileName();
  125. /**
  126. * 返回文件信息
  127. * @return mixed
  128. */
  129. public function getFileInfo()
  130. {
  131. return $this->fileInfo;
  132. }
  133. protected function getRealPath()
  134. {
  135. return $this->fileInfo['realPath'];
  136. }
  137. /**
  138. * 返回错误信息
  139. * @return mixed
  140. */
  141. public function getError()
  142. {
  143. return $this->error;
  144. }
  145. /**
  146. * 生成保存文件名
  147. */
  148. private function buildSaveName()
  149. {
  150. // 要上传图片的本地路径
  151. $realPath = $this->getRealPath();
  152. // 扩展名
  153. $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
  154. // 自动生成文件名
  155. return date('YmdHis') . substr(md5($realPath), 0, 5)
  156. . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
  157. }
  158. }