diff --git a/hiver-admin/src/main/resources/application.yml b/hiver-admin/src/main/resources/application.yml index 3a2cccec..3268d6d7 100644 --- a/hiver-admin/src/main/resources/application.yml +++ b/hiver-admin/src/main/resources/application.yml @@ -285,6 +285,7 @@ ignored: - /hiver/license/verifyLicense # 临时增加 - /hiver/app/** + - /hiver/thorui/** # 限流及黑名单不拦截的路径 limitUrls: - /**/*.js diff --git a/hiver-modules/hiver-file/src/main/java/cc/hiver/file/controller/ThoruiFileUploadController.java b/hiver-modules/hiver-file/src/main/java/cc/hiver/file/controller/ThoruiFileUploadController.java new file mode 100644 index 00000000..8176758f --- /dev/null +++ b/hiver-modules/hiver-file/src/main/java/cc/hiver/file/controller/ThoruiFileUploadController.java @@ -0,0 +1,99 @@ +package cc.hiver.file.controller; + +import cc.hiver.core.common.constant.OssConstant; +import cc.hiver.core.common.constant.SettingConstant; +import cc.hiver.core.common.utils.CommonUtil; +import cc.hiver.core.common.utils.ResultUtil; +import cc.hiver.core.common.vo.Result; +import cc.hiver.core.entity.Setting; +import cc.hiver.core.service.SettingService; +import cc.hiver.core.vo.OssSetting; +import cc.hiver.file.entity.File; +import cc.hiver.file.manage.FileManageFactory; +import cc.hiver.file.service.FileService; +import cn.hutool.core.util.StrUtil; +import com.google.gson.Gson; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; + +@Slf4j +@Controller +@Api(tags = "文件上传接口") +@RequestMapping("/hiver/thorui/upload") +@Transactional +public class ThoruiFileUploadController { + @Value("${hiver.maxUploadFile}") + private Long maxUploadFile; + + @Autowired + private FileManageFactory fileManageFactory; + + @Autowired + private SettingService settingService; + + @Autowired + private FileService fileService; + + @PostMapping("/file") + @ApiOperation(value = "文件上传") + @ResponseBody + public Result upload(@RequestBody MultipartFile files) throws IOException { + if (files == null) { + return ResultUtil.error(500, "缺少文件参数"); + } + if (files != null && files.getSize() > maxUploadFile * 1024 * 1024) { + return ResultUtil.error(500, "文件大小过大,不能超过" + maxUploadFile + "MB"); + } + Setting setting = settingService.get(SettingConstant.OSS_USED); + if (setting == null || StrUtil.isBlank(setting.getValue())) { + return ResultUtil.error(500, "您还未配置OSS存储服务"); + } + String result = ""; + String fKey = CommonUtil.renamePic(files.getOriginalFilename()); + File f = new File(); + try { + InputStream inputStream = files.getInputStream(); + // 上传至第三方云服务或服务器 + result = fileManageFactory.getFileManage(null).inputStreamUpload(inputStream, fKey, files); + // 保存数据信息至数据库 + f.setLocation(getType(setting.getValue())).setTitle(files.getOriginalFilename()).setSize(files.getSize()) + .setType(files.getContentType()).setFKey(fKey).setUrl(result).setCategoryId("0"); + fileService.save(f); + } catch (Exception e) { + log.error(e.toString()); + return ResultUtil.error(500, e.toString()); + } + if (setting.getValue().equals(SettingConstant.LOCAL_OSS)) { + OssSetting os = new Gson().fromJson(settingService.get(SettingConstant.LOCAL_OSS).getValue(), OssSetting.class); + result = os.getHttp() + os.getEndpoint() + "/" + f.getId(); + } + return ResultUtil.data(result); + } + + public Integer getType(String type) { + switch (type) { + case SettingConstant.QINIU_OSS: + return OssConstant.OSS_QINIU; + case SettingConstant.ALI_OSS: + return OssConstant.OSS_ALI; + case SettingConstant.TENCENT_OSS: + return OssConstant.OSS_TENCENT; + case SettingConstant.MINIO_OSS: + return OssConstant.OSS_MINIO; + case SettingConstant.LOCAL_OSS: + return OssConstant.OSS_LOCAL; + default: + return -1; + } + } +}