Upload.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = config('app.host') . '/uploads/' . $fileInfo['file_path'];
  73. Log::error($filePath);
  74. $storageObj = $bucket->upload(
  75. fopen($filePath, 'r'),
  76. [
  77. 'name' => $fileInfo['file_path']
  78. ]
  79. );
  80. $name = $storageObj->name();
  81. if (empty($name)){
  82. Log::error('上传文件到google云失败');
  83. }
  84. // 图片上传成功
  85. return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
  86. }
  87. /**
  88. * 视频上传接口
  89. * @param int $groupId 分组ID
  90. * @return Json
  91. * @throws \think\Exception
  92. */
  93. public function video(int $groupId = 0): Json
  94. {
  95. // 实例化存储驱动
  96. $storage = new StorageDriver($this->config);
  97. // 设置上传文件的信息
  98. $storage->setUploadFile('iFile')
  99. ->setRootDirName((string)$this->storeId)
  100. ->setValidationScene('video');
  101. // 执行文件上传
  102. if (!$storage->upload()) {
  103. return $this->renderError('视频上传失败:' . $storage->getError());
  104. }
  105. // 文件信息
  106. $fileInfo = $storage->getSaveFileInfo();
  107. // 添加文件库记录
  108. $model = new UploadFileModel;
  109. $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
  110. // 图片上传成功
  111. return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
  112. }
  113. }