Local.php 4.2 KB

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