Server.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use app\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 = app('\app\Request')->file($name);
  34. if (empty($this->file)) {
  35. throw new Exception('未找到上传文件的信息');
  36. }
  37. // 文件信息
  38. $this->fileInfo = [
  39. 'ext' => $this->file->extension(),
  40. 'size' => $this->file->getSize(),
  41. 'mime' => $this->file->getMime(),
  42. 'name' => $this->file->getOriginalName(),
  43. 'realPath' => $this->file->getRealPath(),
  44. ];
  45. // 生成保存文件名
  46. $this->fileName = $this->buildSaveName();
  47. }
  48. /**
  49. * 设置上传的文件信息
  50. * @param string $filePath
  51. */
  52. public function setUploadFileByReal($filePath)
  53. {
  54. // 设置为系统内部上传
  55. $this->isInternal = true;
  56. // 文件信息
  57. $this->fileInfo = [
  58. 'name' => basename($filePath),
  59. 'size' => filesize($filePath),
  60. 'tmp_name' => $filePath,
  61. 'error' => 0,
  62. ];
  63. // 生成保存文件名
  64. $this->fileName = $this->buildSaveName();
  65. }
  66. /**
  67. * Notes: 抓取网络资源
  68. * @param $url
  69. * @param $key
  70. * @author 张无忌(2021/3/2 14:15)
  71. * @return mixed
  72. */
  73. abstract protected function fetch($url, $key);
  74. /**
  75. * 文件上传
  76. * @param $save_dir (保存路径)
  77. * @return mixed
  78. */
  79. abstract protected function upload($save_dir);
  80. /**
  81. * 文件删除
  82. * @param $fileName
  83. * @return mixed
  84. */
  85. abstract protected function delete($fileName);
  86. /**
  87. * 返回上传后文件路径
  88. * @return mixed
  89. */
  90. abstract public function getFileName();
  91. /**
  92. * 返回文件信息
  93. * @return mixed
  94. */
  95. public function getFileInfo()
  96. {
  97. return $this->fileInfo;
  98. }
  99. protected function getRealPath()
  100. {
  101. return $this->fileInfo['realPath'];
  102. }
  103. /**
  104. * 返回错误信息
  105. * @return mixed
  106. */
  107. public function getError()
  108. {
  109. return $this->error;
  110. }
  111. /**
  112. * 生成保存文件名
  113. */
  114. private function buildSaveName()
  115. {
  116. // 要上传图片的本地路径
  117. $realPath = $this->getRealPath();
  118. // 扩展名
  119. $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
  120. // 自动生成文件名
  121. return date('YmdHis') . substr(md5($realPath), 0, 5)
  122. . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
  123. }
  124. }