Upload.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\model\Setting as SettingModel;
  14. use app\api\model\UploadFile as UploadFileModel;
  15. use app\api\service\User as UserService;
  16. use app\common\enum\Setting as SettingEnum;
  17. use app\common\enum\file\FileType as FileTypeEnum;
  18. use app\common\library\storage\Driver as StorageDriver;
  19. use app\common\service\BaseService;
  20. use cores\exception\BaseException;
  21. /**
  22. * 文件上传管理
  23. * Class Upload
  24. * @package app\service\controller
  25. */
  26. class Upload extends BaseService
  27. {
  28. /**
  29. * 文件上传场景
  30. */
  31. const UPLOAD_SCENE_ENUM = [
  32. FileTypeEnum::IMAGE => 'image',
  33. FileTypeEnum::VIDEO => 'video',
  34. ];
  35. // 用户上传文件的file名称
  36. const FORM_NAME = 'file';
  37. // 文件信息 (上传后)
  38. private array $fileInfo;
  39. /**
  40. * 文件上传(用户提交上传)
  41. * @param int $fileType 文件类型 image和video
  42. * @param bool $checkLogin 是否验证登录
  43. * @return bool
  44. * @throws BaseException
  45. * @throws \think\Exception
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function upload(int $fileType, bool $checkLogin = true): bool
  51. {
  52. $userId = $checkLogin && UserService::isLogin(true) ? UserService::getCurrentLoginUserId() : 0;
  53. // 实例化上传驱动
  54. $storage = $this->getDriver(self::UPLOAD_SCENE_ENUM[$fileType]);
  55. // 执行文件上传
  56. if (!$storage->upload()) {
  57. $this->error = $storage->getError();
  58. return false;
  59. }
  60. // 文件信息
  61. $fileInfo = $storage->getSaveFileInfo();
  62. // 添加文件库记录
  63. return $this->record($fileInfo, $fileType, $userId);
  64. }
  65. /**
  66. * 实例化存储驱动
  67. * @param string $scene 上传场景 image和video
  68. * @return mixed
  69. * @throws \think\Exception
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. private function getDriver(string $scene)
  75. {
  76. $config = $this->getConfig();
  77. $storage = new StorageDriver($config);
  78. // 设置上传文件的信息
  79. return $storage->setUploadFile(self::FORM_NAME)
  80. ->setRootDirName((string)$this->storeId)
  81. ->setValidationScene($scene);
  82. }
  83. /**
  84. * 添加文件库记录
  85. * @param array $fileInfo 文件信息
  86. * @param int $fileType 文件类型 image和video
  87. * @param int $userId 用户ID
  88. * @return bool
  89. */
  90. private function record(array $fileInfo, int $fileType, int $userId = 0): bool
  91. {
  92. // 添加文件库记录
  93. $model = new UploadFileModel;
  94. $model->add($fileInfo, $fileType, $userId);
  95. $this->fileInfo = $model->toArray();
  96. return true;
  97. }
  98. /**
  99. * 文件信息 (上传后)
  100. * @return array
  101. */
  102. public function getFileInfo(): array
  103. {
  104. return $this->fileInfo;
  105. }
  106. /**
  107. * 获取存储配置信息
  108. * @return array|mixed
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. private function getConfig()
  114. {
  115. return SettingModel::getItem(SettingEnum::STORAGE);
  116. }
  117. }