Local.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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' . DIRECTORY_SEPARATOR;
  43. return str_replace('\\', '/', $root . 'uploads' . DIRECTORY_SEPARATOR . $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. * @param string $path
  58. * @return array|bool|mixed|\StdClass
  59. */
  60. public function move(string $file = 'file',$path=null)
  61. {
  62. $fileHandle = app()->request->file($file);
  63. if (!$fileHandle) {
  64. return $this->setError('Upload file does not exist');
  65. }
  66. if ($this->validate) {
  67. try {
  68. validate([$file => $this->validate])->check([$file => $fileHandle]);
  69. } catch (ValidateException $e) {
  70. return $this->setError($e->getMessage());
  71. }
  72. }
  73. $fileName = Filesystem::putFile($this->path, $fileHandle);
  74. if (!$fileName)
  75. return $this->setError('Upload failure');
  76. $filePath = Filesystem::path($fileName);
  77. $this->fileInfo->uploadInfo = new File($filePath);
  78. $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
  79. $this->fileInfo->filePath = $this->defaultPath . '/' . str_replace('\\', '/', $fileName);
  80. return $this->fileInfo;
  81. }
  82. /**
  83. * 文件流上传
  84. * @param string $fileContent
  85. * @param string|null $key
  86. * @return array|bool|mixed|\StdClass
  87. */
  88. public function stream(string $fileContent, string $key = null)
  89. {
  90. if (!$key) {
  91. $key = $this->saveFileName();
  92. }
  93. $dir = $this->uploadDir($this->path);
  94. if (!$this->validDir($dir)) {
  95. return $this->setError('Failed to generate upload directory, please check the permission!');
  96. }
  97. $fileName = $dir . '/' . $key;
  98. file_put_contents($fileName, $fileContent);
  99. $this->fileInfo->uploadInfo = new File($fileName);
  100. $this->fileInfo->fileName = $key;
  101. $this->fileInfo->filePath = $this->defaultPath . '/' . $this->path . '/' . $key;
  102. return $this->fileInfo;
  103. }
  104. /**
  105. * 删除文件
  106. * @param string $filePath
  107. * @return bool|mixed
  108. */
  109. public function delete(string $filePath)
  110. {
  111. if (file_exists($filePath)) {
  112. try {
  113. unlink($filePath);
  114. return true;
  115. } catch (UploadException $e) {
  116. return $this->setError($e->getMessage());
  117. }
  118. }
  119. return false;
  120. }
  121. }