Upload.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\response\Json;
  19. /**
  20. * 文件库管理
  21. * Class Upload
  22. * @package app\store\controller
  23. */
  24. class Upload extends Controller
  25. {
  26. // 当前商城的上传设置
  27. private $config;
  28. /**
  29. * 构造方法
  30. * @throws \app\common\exception\BaseException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function initialize()
  36. {
  37. parent::initialize();
  38. // 存储配置信息
  39. $this->config = SettingModel::getItem(SettingEnum::STORAGE);
  40. }
  41. /**
  42. * 图片上传接口
  43. * @param int $groupId 分组ID
  44. * @return Json
  45. * @throws \think\Exception
  46. */
  47. public function image(int $groupId = 0): Json
  48. {
  49. // 实例化存储驱动
  50. $storage = new StorageDriver($this->config);
  51. // 设置上传文件的信息
  52. $storage->setUploadFile('iFile')
  53. ->setRootDirName((string)$this->storeId)
  54. ->setValidationScene('image');
  55. // 执行文件上传
  56. if (!$storage->upload()) {
  57. return $this->renderError('图片上传失败:' . $storage->getError());
  58. }
  59. // 文件信息
  60. $fileInfo = $storage->getSaveFileInfo();
  61. // 添加文件库记录
  62. $model = new UploadFileModel;
  63. $model->add($fileInfo, FileTypeEnum::IMAGE, $groupId);
  64. // 图片上传成功
  65. return $this->renderSuccess(['fileInfo' => $model->toArray()], '图片上传成功');
  66. }
  67. /**
  68. * 视频上传接口
  69. * @param int $groupId 分组ID
  70. * @return Json
  71. * @throws \think\Exception
  72. */
  73. public function video(int $groupId = 0): Json
  74. {
  75. // 实例化存储驱动
  76. $storage = new StorageDriver($this->config);
  77. // 设置上传文件的信息
  78. $storage->setUploadFile('iFile')
  79. ->setRootDirName((string)$this->storeId)
  80. ->setValidationScene('video');
  81. // 执行文件上传
  82. if (!$storage->upload()) {
  83. return $this->renderError('视频上传失败:' . $storage->getError());
  84. }
  85. // 文件信息
  86. $fileInfo = $storage->getSaveFileInfo();
  87. // 添加文件库记录
  88. $model = new UploadFileModel;
  89. $model->add($fileInfo, FileTypeEnum::VIDEO, $groupId);
  90. // 图片上传成功
  91. return $this->renderSuccess(['fileInfo' => $model->toArray()], '视频上传成功');
  92. }
  93. }