Upload.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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\controller;
  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 think\response\Json;
  19. use Google\Cloud\Storage\StorageClient;
  20. use Google\Auth\Credentials\ServiceAccountCredentials;
  21. /**
  22. * 文件库管理
  23. * Class Upload
  24. * @package app\store\controller
  25. */
  26. class Upload extends Controller
  27. {
  28. // 当前商城的上传设置
  29. private $config;
  30. /**
  31. * 构造方法
  32. * @throws \app\common\exception\BaseException
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function initialize()
  38. {
  39. parent::initialize();
  40. // 存储配置信息
  41. $this->config = SettingModel::getItem(SettingEnum::STORAGE);
  42. }
  43. /**
  44. * 图片上传接口
  45. * @param int $groupId 分组ID
  46. * @return Json
  47. * @throws \think\Exception
  48. */
  49. public function image(int $groupId = 0): Json
  50. {
  51. // 实例化存储驱动
  52. $storage = new StorageDriver($this->config);
  53. // 设置上传文件的信息
  54. $storage->setUploadFile('iFile')
  55. ->setRootDirName((string)$this->storeId)
  56. ->setValidationScene('image');
  57. // 执行文件上传
  58. if (!$storage->upload()) {
  59. return $this->renderError('图片上传失败:' . $storage->getError());
  60. }
  61. // 文件信息
  62. $fileInfo = $storage->getSaveFileInfo();
  63. // 添加文件库记录
  64. $model = new UploadFileModel;
  65. $model->add($fileInfo, FileTypeEnum::IMAGE, $groupId);
  66. //$filePath = $fileInfo['file_path'];
  67. //todo 上传到谷歌GCS
  68. // 图片上传成功
  69. return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
  70. }
  71. /**
  72. * 视频上传接口
  73. * @param int $groupId 分组ID
  74. * @return Json
  75. * @throws \think\Exception
  76. */
  77. public function video(int $groupId = 0): Json
  78. {
  79. // 实例化存储驱动
  80. $storage = new StorageDriver($this->config);
  81. // 设置上传文件的信息
  82. $storage->setUploadFile('iFile')
  83. ->setRootDirName((string)$this->storeId)
  84. ->setValidationScene('video');
  85. // 执行文件上传
  86. if (!$storage->upload()) {
  87. return $this->renderError('视频上传失败:' . $storage->getError());
  88. }
  89. // 文件信息
  90. $fileInfo = $storage->getSaveFileInfo();
  91. // 添加文件库记录
  92. $model = new UploadFileModel;
  93. $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
  94. // 图片上传成功
  95. return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
  96. }
  97. }