截流自动化的商城平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Server.php 3.3KB

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