Upload.php 4.4 KB

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