Upload.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\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. // 用户上传文件的file名称
  34. const FORM_NAME = 'iFile';
  35. /**
  36. * 上传成功的文件信息
  37. * @var array
  38. */
  39. private array $fileInfo = [];
  40. /**
  41. * 文件上传(用户提交上传)
  42. * @param int $fileType 文件类型 image和video
  43. * @param int $groupId 分组ID
  44. * @return bool
  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, int $groupId = 0): bool
  51. {
  52. // 实例化上传驱动
  53. $storage = $this->getDriver(self::UPLOAD_SCENE_ENUM[$fileType]);
  54. // 执行文件上传
  55. if (!$storage->upload()) {
  56. $this->error = $storage->getError();
  57. return false;
  58. }
  59. // 文件信息
  60. $fileInfo = $storage->getSaveFileInfo();
  61. // 添加文件库记录
  62. return $this->record($fileInfo, $fileType, $groupId);
  63. }
  64. /**
  65. * 文件上传 (服务器本地上传)
  66. * @param int $fileType 文件类型 image和video
  67. * @param string $filePath 文件路径
  68. * @return bool
  69. * @throws \think\Exception
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function uploadByLocal(int $fileType, string $filePath): bool
  75. {
  76. // 实例化上传驱动
  77. $storage = $this->getDriver(self::UPLOAD_SCENE_ENUM[$fileType], true, $filePath);
  78. // 执行文件上传
  79. if (!$storage->upload()) {
  80. $this->error = $storage->getError();
  81. return false;
  82. }
  83. // 文件信息
  84. $fileInfo = $storage->getSaveFileInfo();
  85. // 添加文件库记录
  86. return $this->record($fileInfo, $fileType);
  87. }
  88. /**
  89. * 文件上传 (保存外链)
  90. * @param int $fileType
  91. * @param string $fileUrl
  92. * @return bool
  93. */
  94. public function uploadByExternal(int $fileType, string $fileUrl): bool
  95. {
  96. // 文件信息
  97. $parseUrl = parse_url($fileUrl);
  98. $fileInfo = [
  99. 'storage' => 'external',
  100. 'domain' => "{$parseUrl['scheme']}://{$parseUrl['host']}",
  101. 'file_name' => basename($parseUrl['path']),
  102. 'file_path' => ltrim($parseUrl['path'], '/'),
  103. 'file_size' => 0,
  104. 'file_ext' => pathinfo($fileUrl)['extension'],
  105. ];
  106. // 添加文件库记录
  107. return $this->record($fileInfo, $fileType);
  108. }
  109. /**
  110. * 添加文件库记录
  111. * @param array $fileInfo 文件信息
  112. * @param int $fileType 文件类型 image和video
  113. * @param int $groupId 分组ID
  114. * @return bool
  115. */
  116. private function record(array $fileInfo, int $fileType, int $groupId = 0): bool
  117. {
  118. // 添加文件库记录
  119. $model = new UploadFileModel;
  120. $model->add($fileInfo, $fileType, $groupId, $this->getStoreId());
  121. $this->fileInfo = $model->toArray();
  122. return true;
  123. }
  124. /**
  125. * 上传成功的文件信息
  126. * @return array
  127. */
  128. public function getFileInfo(): array
  129. {
  130. return $this->fileInfo;
  131. }
  132. /**
  133. * 实例化上传驱动
  134. * @param string $scene 上传场景 image和video
  135. * @param bool $isReal 是否为本地服务器上传(反之为用户提交上传)
  136. * @param string $filePath 文件路径
  137. * @return mixed
  138. * @throws \think\Exception
  139. * @throws \think\db\exception\DataNotFoundException
  140. * @throws \think\db\exception\DbException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. */
  143. private function getDriver(string $scene, bool $isReal = false, string $filePath = '')
  144. {
  145. // 获取文件上传设置
  146. $config = $this->getConfig();
  147. // 实例化存储驱动
  148. $storage = new StorageDriver($config);
  149. // 判断文件来源是用户上传或真实路径
  150. $storage = $isReal ? $storage->setUploadFileByReal($filePath) : $storage->setUploadFile(self::FORM_NAME);
  151. // 设置上传文件的信息
  152. return $storage->setRootDirName((string)$this->getStoreId())
  153. ->setValidationScene($scene);
  154. }
  155. /**
  156. * 获取文件上传设置
  157. * @return array|mixed
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. private function getConfig()
  163. {
  164. return SettingModel::getItem(SettingEnum::STORAGE, $this->getStoreId());
  165. }
  166. }