Server.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use think\Request;
  4. use think\Exception;
  5. /**
  6. * 存储引擎抽象类
  7. * Class server
  8. * @package app\common\library\storage\drivers
  9. */
  10. abstract class Server
  11. {
  12. protected $file;
  13. protected $error;
  14. protected $fileName;
  15. protected $fileInfo;
  16. // 是否为内部上传
  17. protected $isInternal = false;
  18. /**
  19. * 构造函数
  20. * Server constructor.
  21. */
  22. protected function __construct()
  23. {
  24. }
  25. /**
  26. * 设置上传的文件信息
  27. * @param string $name
  28. * @throws Exception
  29. */
  30. public function setUploadFile($name)
  31. {
  32. // 接收上传的文件
  33. $this->file = request()->file($name);
  34. if (empty($this->file)) {
  35. throw new Exception('未找到上传文件的信息');
  36. }
  37. // 校验上传文件后缀
  38. $limit = array_merge(config('project.file_image'), config('project.file_video'), config('project.file_file'));
  39. if (!in_array(strtolower($this->file->extension()), $limit)) {
  40. throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
  41. }
  42. // 文件信息
  43. $this->fileInfo = [
  44. 'ext' => $this->file->extension(),
  45. 'size' => $this->file->getSize(),
  46. 'mime' => $this->file->getMime(),
  47. 'name' => $this->file->getOriginalName(),
  48. 'realPath' => $this->file->getRealPath(),
  49. ];
  50. // 生成保存文件名
  51. $this->fileName = $this->buildSaveName();
  52. }
  53. /**
  54. * 设置上传的文件信息
  55. * @param string $filePath
  56. */
  57. public function setUploadFileByReal($filePath)
  58. {
  59. // 设置为系统内部上传
  60. $this->isInternal = true;
  61. // 文件信息
  62. $this->fileInfo = [
  63. 'name' => basename($filePath),
  64. 'size' => filesize($filePath),
  65. 'tmp_name' => $filePath,
  66. 'error' => 0,
  67. ];
  68. // 生成保存文件名
  69. $this->fileName = $this->buildSaveName();
  70. }
  71. /**
  72. * Notes: 抓取网络资源
  73. * @param $url
  74. * @param $key
  75. * @author 张无忌(2021/3/2 14:15)
  76. * @return mixed
  77. */
  78. abstract protected function fetch($url, $key);
  79. /**
  80. * 文件上传
  81. * @param $save_dir (保存路径)
  82. * @return mixed
  83. */
  84. abstract protected function upload($save_dir);
  85. /**
  86. * 文件删除
  87. * @param $fileName
  88. * @return mixed
  89. */
  90. abstract protected function delete($fileName);
  91. /**
  92. * 返回上传后文件路径
  93. * @return mixed
  94. */
  95. abstract public function getFileName();
  96. /**
  97. * 返回文件信息
  98. * @return mixed
  99. */
  100. public function getFileInfo()
  101. {
  102. return $this->fileInfo;
  103. }
  104. protected function getRealPath()
  105. {
  106. return $this->fileInfo['realPath'];
  107. }
  108. /**
  109. * 返回错误信息
  110. * @return mixed
  111. */
  112. public function getError()
  113. {
  114. return $this->error;
  115. }
  116. /**
  117. * 生成保存文件名
  118. */
  119. private function buildSaveName()
  120. {
  121. // 要上传图片的本地路径
  122. $realPath = $this->getRealPath();
  123. // 扩展名
  124. $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
  125. // 自动生成文件名
  126. return date('YmdHis') . substr(md5($realPath), 0, 5)
  127. . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
  128. }
  129. }