Upload.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\facade\Log;
  19. use think\response\Json;
  20. use Google\Cloud\Storage\StorageClient;
  21. use Google\Auth\Credentials\ServiceAccountCredentials;
  22. /**
  23. * 文件库管理
  24. * Class Upload
  25. * @package app\store\controller
  26. */
  27. class Upload extends Controller
  28. {
  29. // 当前商城的上传设置
  30. private $config;
  31. /**
  32. * 构造方法
  33. * @throws \app\common\exception\BaseException
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function initialize()
  39. {
  40. parent::initialize();
  41. // 存储配置信息
  42. $this->config = SettingModel::getItem(SettingEnum::STORAGE);
  43. }
  44. /**
  45. * 图片上传接口
  46. * @param int $groupId 分组ID
  47. * @return Json
  48. * @throws \think\Exception
  49. */
  50. public function image(int $groupId = 0): Json
  51. {
  52. // 实例化存储驱动
  53. $storage = new StorageDriver($this->config);
  54. // 设置上传文件的信息
  55. $storage->setUploadFile('iFile')
  56. ->setRootDirName((string)$this->storeId)
  57. ->setValidationScene('image');
  58. // 执行文件上传
  59. if (!$storage->upload()) {
  60. return $this->renderError('图片上传失败:' . $storage->getError());
  61. }
  62. // 文件信息
  63. $fileInfo = $storage->getSaveFileInfo();
  64. // 添加文件库记录
  65. $model = new UploadFileModel;
  66. $model->add($fileInfo, FileTypeEnum::IMAGE, $groupId);
  67. //$filePath = $fileInfo['file_path'];
  68. //todo 上传到谷歌GCS
  69. $keyFilePath = '/etc/nginx/nginx/html/dihua-01-5690813badec.json';
  70. $storage = new StorageClient(['projectId' => 'dihua-01', 'keyFilePath' => $keyFilePath]);
  71. $bucket = $storage->bucket('freeshippingvps');
  72. $filePath = public_path($fileInfo['file_path']);
  73. $storageObj = $bucket->upload(
  74. fopen($filePath, 'r'),
  75. [
  76. 'name' => $fileInfo['file_path']
  77. ]
  78. );
  79. $name = $storageObj->name();
  80. if (empty($name)){
  81. Log::error('上传文件到google云失败');
  82. }
  83. // 图片上传成功
  84. return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
  85. }
  86. /**
  87. * 视频上传接口
  88. * @param int $groupId 分组ID
  89. * @return Json
  90. * @throws \think\Exception
  91. */
  92. public function video(int $groupId = 0): Json
  93. {
  94. // 实例化存储驱动
  95. $storage = new StorageDriver($this->config);
  96. // 设置上传文件的信息
  97. $storage->setUploadFile('iFile')
  98. ->setRootDirName((string)$this->storeId)
  99. ->setValidationScene('video');
  100. // 执行文件上传
  101. if (!$storage->upload()) {
  102. return $this->renderError('视频上传失败:' . $storage->getError());
  103. }
  104. // 文件信息
  105. $fileInfo = $storage->getSaveFileInfo();
  106. // 添加文件库记录
  107. $model = new UploadFileModel;
  108. $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
  109. // 图片上传成功
  110. return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
  111. }
  112. }