Server.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use think\file\UploadedFile;
  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,$fileName = null)
  32. {
  33. $this->file = null;
  34. if($fileName){
  35. for($key=0;$key<count($_FILES[$name]['tmp_name']);$key++){
  36. if($_FILES[$name]['name'][$key] == $fileName){
  37. $this->file = new UploadedFile($_FILES[$name]['tmp_name'][$key], $_FILES[$name]['name'][$key], $_FILES[$name]['type'][$key], $_FILES[$name]['error'][$key]);
  38. break;
  39. }
  40. }
  41. ////tp框架这种写法有问题,需要原生数据上传处理 todo
  42. // $files = request()->file($name);
  43. // foreach($files as $file){
  44. // if($file->getOriginalName() == $fileName){
  45. // $this->file = $file;
  46. // break;
  47. // }
  48. // }
  49. } else {
  50. $this->file = request()->file($name);
  51. }
  52. // 接收上传的文件
  53. if (empty($this->file)) {
  54. throw new Exception('未找到上传文件的信息');
  55. }
  56. // 校验上传文件后缀
  57. $limit = array_merge(config('project.file_image'), config('project.file_video'), config('project.file_file'));
  58. if (!in_array(strtolower($this->file->extension()), $limit)) {
  59. throw new Exception('不允许上传' . $this->file->extension() . '后缀文件');
  60. }
  61. // 文件信息
  62. $this->fileInfo = [
  63. 'ext' => $this->file->extension(),
  64. 'size' => $this->file->getSize(),
  65. 'mime' => $this->file->getMime(),
  66. 'name' => $this->file->getOriginalName(),
  67. 'realPath' => $this->file->getRealPath(),
  68. ];
  69. // 生成保存文件名
  70. $this->fileName = $this->buildSaveName();
  71. }
  72. /**
  73. * 设置上传的文件信息
  74. * @param string $filePath
  75. */
  76. public function setUploadFileByReal($filePath)
  77. {
  78. // 设置为系统内部上传
  79. $this->isInternal = true;
  80. // 文件信息
  81. $this->fileInfo = [
  82. 'name' => basename($filePath),
  83. 'size' => filesize($filePath),
  84. 'tmp_name' => $filePath,
  85. 'error' => 0,
  86. ];
  87. // 生成保存文件名
  88. $this->fileName = $this->buildSaveName();
  89. }
  90. /**
  91. * Notes: 抓取网络资源
  92. * @param $url
  93. * @param $key
  94. * @author 张无忌(2021/3/2 14:15)
  95. * @return mixed
  96. */
  97. abstract protected function fetch($url, $key);
  98. /**
  99. * 文件上传
  100. * @param $save_dir (保存路径)
  101. * @return mixed
  102. */
  103. abstract protected function upload($save_dir);
  104. /**
  105. * 文件删除
  106. * @param $fileName
  107. * @return mixed
  108. */
  109. abstract protected function delete($fileName);
  110. /**
  111. * 返回上传后文件路径
  112. * @return mixed
  113. */
  114. abstract public function getFileName();
  115. /**
  116. * 返回文件信息
  117. * @return mixed
  118. */
  119. public function getFileInfo()
  120. {
  121. return $this->fileInfo;
  122. }
  123. protected function getRealPath()
  124. {
  125. return $this->fileInfo['realPath'];
  126. }
  127. /**
  128. * 返回错误信息
  129. * @return mixed
  130. */
  131. public function getError()
  132. {
  133. return $this->error;
  134. }
  135. /**
  136. * 生成保存文件名
  137. */
  138. private function buildSaveName()
  139. {
  140. // 要上传图片的本地路径
  141. $realPath = $this->getRealPath();
  142. // 扩展名
  143. $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);
  144. // 自动生成文件名
  145. return date('YmdHis') . substr(md5($realPath), 0, 5)
  146. . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
  147. }
  148. }