Local.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  31. * 生成上传文件目录
  32. * @param $path
  33. * @param null $root
  34. * @return string
  35. */
  36. protected function uploadDir($path, $root = null)
  37. {
  38. if ($root === null) $root = app()->getRootPath() . 'public' . DS;
  39. return str_replace('\\', '/', $root . 'uploads' . DS . $path);
  40. }
  41. /**
  42. * 检查上传目录不存在则生成
  43. * @param $dir
  44. * @return bool
  45. */
  46. protected function validDir($dir)
  47. {
  48. return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
  49. }
  50. /**
  51. * 文件上传
  52. * @param string $file
  53. * @return array|bool|mixed|\StdClass
  54. */
  55. public function move(string $file = 'file')
  56. {
  57. $fileHandle = app()->request->file($file);
  58. if (!$fileHandle) {
  59. return $this->setError('Upload file does not exist');
  60. }
  61. if ($this->validate) {
  62. try {
  63. validate([$file => $this->validate])->check([$file => $fileHandle]);
  64. } catch (ValidateException $e) {
  65. return $this->setError($e->getMessage());
  66. }
  67. }
  68. $fileName = Filesystem::putFile($this->path, $fileHandle);
  69. if (!$fileName)
  70. return $this->setError('Upload failure');
  71. $filePath = Filesystem::path($fileName);
  72. $this->fileInfo->uploadInfo = new File($filePath);
  73. $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
  74. $this->fileInfo->filePath = $this->defaultPath . '/' . str_replace('\\', '/', $fileName);
  75. return $this->fileInfo;
  76. }
  77. /**
  78. * 文件流上传
  79. * @param string $fileContent
  80. * @param string|null $key
  81. * @return array|bool|mixed|\StdClass
  82. */
  83. public function stream(string $fileContent, string $key = null)
  84. {
  85. if (!$key) {
  86. $key = $this->saveFileName();
  87. }
  88. $dir = $this->uploadDir($this->path);
  89. if (!$this->validDir($dir)) {
  90. return $this->setError('Failed to generate upload directory, please check the permission!');
  91. }
  92. $fileName = $dir . '/' . $key;
  93. file_put_contents($fileName, $fileContent);
  94. $this->fileInfo->uploadInfo = new File($fileName);
  95. $this->fileInfo->fileName = $key;
  96. $this->fileInfo->filePath = '/uploads/' . $this->path . '/' . $key;
  97. return $this->fileInfo;
  98. }
  99. /**
  100. * 删除文件
  101. * @param string $filePath
  102. * @return bool|mixed
  103. */
  104. public function delete(string $filePath)
  105. {
  106. if (file_exists($filePath)) {
  107. try {
  108. unlink($filePath);
  109. return true;
  110. } catch (UploadException $e) {
  111. return $this->setError($e->getMessage());
  112. }
  113. }
  114. return false;
  115. }
  116. }