Local.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace crmeb\services\upload\storage;
  3. use crmeb\basic\BaseUpload;
  4. use crmeb\exceptions\UploadException;
  5. use think\exception\ValidateException;
  6. use think\facade\Config;
  7. use think\facade\Filesystem;
  8. use think\File;
  9. /**
  10. * 本地上传
  11. * Class Local
  12. * @package crmeb\services\upload\storage
  13. */
  14. class Local extends BaseUpload
  15. {
  16. /**
  17. * 默认存放路径
  18. * @var string
  19. */
  20. protected $defaultPath;
  21. public function initialize(array $config)
  22. {
  23. parent::initialize($config);
  24. $this->defaultPath = Config::get('filesystem.disks.' . Config::get('filesystem.default') . '.url');
  25. }
  26. protected function app()
  27. {
  28. // TODO: Implement app() method.
  29. }
  30. public function getTempKeys()
  31. {
  32. // TODO: Implement getTempKeys() method.
  33. }
  34. /**
  35. * 生成上传文件目录
  36. * @param $path
  37. * @param null $root
  38. * @return string
  39. */
  40. protected function uploadDir($path, $root = null)
  41. {
  42. if ($root === null) $root = app()->getRootPath() . 'public' . DS;
  43. return str_replace('\\', '/', $root . 'uploads' . DS . $path);
  44. }
  45. /**
  46. * 检查上传目录不存在则生成
  47. * @param $dir
  48. * @return bool
  49. */
  50. protected function validDir($dir)
  51. {
  52. return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
  53. }
  54. /**
  55. * 文件上传
  56. * @param string $file
  57. * @return array|bool|mixed|\StdClass
  58. */
  59. public function move(string $file = 'file')
  60. {
  61. $fileHandle = app()->request->file($file);
  62. if (!$fileHandle) {
  63. return $this->setError('Upload file does not exist');
  64. }
  65. if ($this->validate) {
  66. try {
  67. validate([$file => $this->validate])->check([$file => $fileHandle]);
  68. } catch (ValidateException $e) {
  69. return $this->setError($e->getMessage());
  70. }
  71. }
  72. $fileName = Filesystem::putFile($this->path, $fileHandle);
  73. if (!$fileName)
  74. return $this->setError('Upload failure');
  75. $filePath = Filesystem::path($fileName);
  76. $this->fileInfo->uploadInfo = new File($filePath);
  77. $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
  78. $this->fileInfo->filePath = $this->defaultPath . '/' . str_replace('\\', '/', $fileName);
  79. return $this->fileInfo;
  80. }
  81. /**
  82. * 文件流上传
  83. * @param string $fileContent
  84. * @param string|null $key
  85. * @return array|bool|mixed|\StdClass
  86. */
  87. public function stream(string $fileContent, string $key = null)
  88. {
  89. if (!$key) {
  90. $key = $this->saveFileName();
  91. }
  92. $dir = $this->uploadDir($this->path);
  93. if (!$this->validDir($dir)) {
  94. return $this->setError('Failed to generate upload directory, please check the permission!');
  95. }
  96. $fileName = $dir . '/' . $key;
  97. file_put_contents($fileName, $fileContent);
  98. $this->fileInfo->uploadInfo = new File($fileName);
  99. $this->fileInfo->fileName = $key;
  100. $this->fileInfo->filePath = $this->defaultPath . '/' . $this->path . '/' . $key;
  101. return $this->fileInfo;
  102. }
  103. /**
  104. * 删除文件
  105. * @param string $filePath
  106. * @return bool|mixed
  107. */
  108. public function delete(string $filePath)
  109. {
  110. if (file_exists($filePath)) {
  111. try {
  112. unlink($filePath);
  113. return true;
  114. } catch (UploadException $e) {
  115. return $this->setError($e->getMessage());
  116. }
  117. }
  118. return false;
  119. }
  120. }