Upload.php 4.8 KB

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