123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\store\controller;
- use app\store\model\Setting as SettingModel;
- use app\store\model\UploadFile as UploadFileModel;
- use app\common\enum\Setting as SettingEnum;
- use app\common\enum\file\FileType as FileTypeEnum;
- use app\common\library\storage\Driver as StorageDriver;
- use Exception;
- use think\facade\Log;
- use think\response\Json;
- use Google\Cloud\Storage\StorageClient;
- use Google\Auth\Credentials\ServiceAccountCredentials;
- /**
- * 文件库管理
- * Class Upload
- * @package app\store\controller
- */
- class Upload extends Controller
- {
- // 当前商城的上传设置
- private $config;
- /**
- * 构造方法
- * @throws \app\common\exception\BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function initialize()
- {
- parent::initialize();
- // 存储配置信息
- $this->config = SettingModel::getItem(SettingEnum::STORAGE);
- }
- /**
- * 图片上传接口
- * @param int $groupId 分组ID
- * @return Json
- * @throws \think\Exception
- */
- public function image(int $groupId = 0): Json
- {
- // 实例化存储驱动
- $storage = new StorageDriver($this->config);
- // 设置上传文件的信息
- $storage->setUploadFile('iFile')
- ->setRootDirName((string)$this->storeId)
- ->setValidationScene('image');
- // 执行文件上传
- if (!$storage->upload()) {
- return $this->renderError('图片上传失败:' . $storage->getError());
- }
- // 文件信息
- $fileInfo = $storage->getSaveFileInfo();
- // 添加文件库记录
- $model = new UploadFileModel;
- $model->add($fileInfo, FileTypeEnum::IMAGE, $groupId);
- //$filePath = $fileInfo['file_path'];
- //todo 上传到谷歌GCS
- $keyFilePath = '/etc/nginx/nginx/html/dihua-01-5690813badec.json';
- $storage = new StorageClient(['projectId' => 'dihua-01', 'keyFilePath' => $keyFilePath]);
- $bucket = $storage->bucket('freeshippingvps');
- $filePathAbsolute = public_path() . 'uploads/' . $fileInfo['file_path'];
- list($storeId, $date, $fileName) = explode('/', $fileInfo['file_path']);
- $thumbnailFile = public_path() . 'uploads/' . $storeId . '/' . $date . '/TH' . $fileName;// 缩略图保存路径
- // 使用示例
- try {
- createThumbnail($filePathAbsolute, $thumbnailFile, 100, 80); // 创建缩略图
- } catch (Exception $e) {
- Log::error($e->getMessage());
- return $this->renderError($e->getMessage());
- }
- Log::error($thumbnailFile);
- //缩略图上传到谷歌
- $storageObj = $bucket->upload(
- fopen($thumbnailFile, 'r'),
- [
- 'name' => $fileInfo['file_path']
- ]
- );
- $name = $storageObj->name();
- if (empty($name)) {
- Log::error('上传文件到google云失败');
- return $this->renderError('上传文件到google云失败');
- }
- // 图片上传成功
- return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
- }
- /**
- * 视频上传接口
- * @param int $groupId 分组ID
- * @return Json
- * @throws \think\Exception
- */
- public function video(int $groupId = 0): Json
- {
- // 实例化存储驱动
- $storage = new StorageDriver($this->config);
- // 设置上传文件的信息
- $storage->setUploadFile('iFile')
- ->setRootDirName((string)$this->storeId)
- ->setValidationScene('video');
- // 执行文件上传
- if (!$storage->upload()) {
- return $this->renderError('视频上传失败:' . $storage->getError());
- }
- // 文件信息
- $fileInfo = $storage->getSaveFileInfo();
- // 添加文件库记录
- $model = new UploadFileModel;
- $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
- // 图片上传成功
- return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
- }
- }
|