|
@@ -0,0 +1,160 @@
|
|
|
+package com.persagy.adm.diagram.controller;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.persagy.adm.diagram.entity.IconEntity;
|
|
|
+import com.persagy.adm.diagram.frame.BdtpRequest;
|
|
|
+import com.persagy.adm.diagram.service.IconService;
|
|
|
+import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
+import com.persagy.dmp.common.exception.BusinessException;
|
|
|
+import com.persagy.dmp.common.model.response.CommonResult;
|
|
|
+import com.persagy.dmp.common.utils.ResultHelper;
|
|
|
+import com.persagy.dmp.file.model.FileInfo;
|
|
|
+import com.persagy.dmp.file.model.FileInfoCreator;
|
|
|
+import com.persagy.dmp.file.utils.FileStorageHelper;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.*;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 图标相关接口
|
|
|
+ *
|
|
|
+ * @author liyang
|
|
|
+ * @date 2022-01-06
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@CrossOrigin
|
|
|
+@RequestMapping("/icon")
|
|
|
+public class IconController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IconService iconService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传图标接口
|
|
|
+ *
|
|
|
+ * @param file 文件
|
|
|
+ * @param legendId 所关联的图例id
|
|
|
+ * @param skin 皮肤
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation("上传图标")
|
|
|
+ @PostMapping("/uploadIcon")
|
|
|
+ public CommonResult<Object> uploadIcon(@RequestParam("file") MultipartFile file,
|
|
|
+ @RequestParam String legendId,
|
|
|
+ @RequestParam(required = false) String skin) {
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
+ long size = file.getSize();
|
|
|
+
|
|
|
+ IconEntity entity = new IconEntity();
|
|
|
+ entity.setIconSkin(skin);
|
|
|
+ entity.setLegendId(legendId);
|
|
|
+ entity.setIconSize(String.valueOf(size));
|
|
|
+
|
|
|
+ Optional.ofNullable(fileName).ifPresent(s -> {
|
|
|
+ int index = s.lastIndexOf(".");
|
|
|
+ String name = s.substring(0, index);
|
|
|
+ String fileType = s.substring(index + 1);
|
|
|
+ entity.setIconName(name);
|
|
|
+ entity.setIconType(fileType);
|
|
|
+ });
|
|
|
+
|
|
|
+ InputStream inputStream = null;
|
|
|
+ Image img = null;
|
|
|
+ try {
|
|
|
+ inputStream = file.getInputStream();
|
|
|
+ img = ImageIO.read(inputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ boolean isImage = img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0;
|
|
|
+ if (null == inputStream || isImage) {
|
|
|
+ throw new BusinessException(ResponseCode.A0400.getCode(), "文件不能为空,并且必须是图片!");
|
|
|
+ }
|
|
|
+
|
|
|
+ FileInfo fileInfo = FileInfoCreator.of(BdtpRequest.getCurrent().getGroupCode(), null,
|
|
|
+ null, entity.getIconName());
|
|
|
+ String fileId = FileStorageHelper.uploadFile(fileInfo, inputStream);
|
|
|
+
|
|
|
+ entity.setIconFileId(fileId);
|
|
|
+
|
|
|
+ return ResultHelper.single(iconService.saveIcon(entity));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图标预览
|
|
|
+ * TODO 目前这个接口不可用 文件服务器好像有问题
|
|
|
+ * @param request 请求体
|
|
|
+ * @param response 响应体
|
|
|
+ * @param iconId 图标数据id
|
|
|
+ */
|
|
|
+ @ApiOperation("图标预览")
|
|
|
+ @GetMapping("/showImage")
|
|
|
+ public void loadFile(HttpServletRequest request, HttpServletResponse response,
|
|
|
+ @RequestParam("id") String iconId) {
|
|
|
+ IconEntity info = iconService.getIconInfo(iconId);
|
|
|
+ if (null == info) {
|
|
|
+ throw new BusinessException(ResponseCode.A0400.getCode(), "未查到此数据");
|
|
|
+ }
|
|
|
+ FileInfo fileInfo = FileStorageHelper.downloadUrl(info.getIconFileId());
|
|
|
+ System.out.println(fileInfo);
|
|
|
+ // 2.通过http请求获取文件流
|
|
|
+ byte[] bytes = HttpUtil.downloadBytes(fileInfo.getFileDownloadUrl());
|
|
|
+ ServletOutputStream out = null;
|
|
|
+ try {
|
|
|
+ response.setHeader("Content-Type", "image/" + info.getIconType());
|
|
|
+ out = response.getOutputStream();
|
|
|
+ out.write(bytes);
|
|
|
+ out.flush();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (out != null) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据图例id获取图标列表
|
|
|
+ *
|
|
|
+ * @param legendId 图例id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation("根据图例id获取图标列表")
|
|
|
+ @GetMapping("/icons/legend")
|
|
|
+ public CommonResult<List<IconEntity>> getIconsByLegendId(String legendId) {
|
|
|
+ return ResultHelper.multi(iconService.getIconsByLegendId(legendId));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取项目 集团 下的所有图标
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ApiOperation("获取所有图标")
|
|
|
+ @GetMapping("/cons")
|
|
|
+ public CommonResult<List<IconEntity>> getIcons() {
|
|
|
+ return ResultHelper.multi(iconService.getIcons());
|
|
|
+ }
|
|
|
+}
|