Local.php 3.6 KB

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