Upload.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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\store\service;
  13. use app\store\model\Setting as SettingModel;
  14. use app\store\model\UploadFile as UploadFileModel;
  15. use app\common\enum\Setting as SettingEnum;
  16. use app\common\enum\file\FileType as FileTypeEnum;
  17. use app\common\library\storage\Driver as StorageDriver;
  18. use app\common\service\BaseService;
  19. /**
  20. * 文件上传服务类
  21. * Class Upload
  22. * @package app\store\service
  23. */
  24. class Upload extends BaseService
  25. {
  26. /**
  27. * 文件上传场景
  28. */
  29. const UPLOAD_SCENE_ENUM = [
  30. FileTypeEnum::IMAGE => 'image',
  31. FileTypeEnum::VIDEO => 'video',
  32. ];
  33. /**
  34. * 上传成功的文件信息
  35. * @var array
  36. */
  37. private array $fileInfo = [];
  38. /**
  39. * 图片上传接口
  40. * @param int $fileType 文件类型 image和video
  41. * @param int $groupId 分组ID
  42. * @return bool
  43. * @throws \think\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function upload(int $fileType, int $groupId = 0): bool
  49. {
  50. // 实例化上传驱动
  51. $storage = $this->getStorage(self::UPLOAD_SCENE_ENUM[$fileType]);
  52. // 执行文件上传
  53. if (!$storage->upload()) {
  54. $this->error = $storage->getError();
  55. return false;
  56. }
  57. // 文件信息
  58. $fileInfo = $storage->getSaveFileInfo();
  59. // 添加文件库记录
  60. $model = new UploadFileModel;
  61. $model->add($fileInfo, $fileType, $groupId);
  62. $this->fileInfo = $model->toArray();
  63. return true;
  64. }
  65. /**
  66. * 上传成功的文件信息
  67. * @return array
  68. */
  69. public function getFileInfo(): array
  70. {
  71. return $this->fileInfo;
  72. }
  73. /**
  74. * 实例化上传驱动
  75. * @param string $scene 上传场景 image和video
  76. * @return mixed
  77. * @throws \think\Exception
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. private function getStorage(string $scene)
  83. {
  84. // 获取文件上传设置
  85. $config = $this->getConfig();
  86. // 实例化存储驱动
  87. $storage = new StorageDriver($config);
  88. // 设置上传文件的信息
  89. return $storage->setUploadFile('iFile')
  90. ->setRootDirName((string)$this->getStoreId())
  91. ->setValidationScene($scene);
  92. }
  93. /**
  94. * 获取文件上传设置
  95. * @return array|mixed
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\DbException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. */
  100. private function getConfig()
  101. {
  102. return SettingModel::getItem(SettingEnum::STORAGE);
  103. }
  104. }