fix: 修复开源版显示图片的问题

This commit is contained in:
Captain.B 2020-12-14 15:38:38 +08:00
parent e071f33dbd
commit 723d5aa462
3 changed files with 83 additions and 1 deletions

View File

@ -5,14 +5,17 @@ import io.metersphere.commons.constants.UserSource;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.controller.request.LoginRequest;
import io.metersphere.service.BaseDisplayService;
import io.metersphere.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
@RestController
@RequestMapping
@ -22,6 +25,8 @@ public class LoginController {
private UserService userService;
@Resource
private Environment env;
@Resource
private BaseDisplayService baseDisplayService;
@GetMapping(value = "/isLogin")
public ResultHolder isLogin() {
@ -66,4 +71,9 @@ public class LoginController {
public String getDefaultLanguage() {
return userService.getDefaultLanguage();
}
@GetMapping("display/file/{imageName}")
public ResponseEntity<byte[]> image(@PathVariable("imageName") String imageName) throws IOException {
return baseDisplayService.getImage(imageName);
}
}

View File

@ -0,0 +1,72 @@
package io.metersphere.service;
import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.domain.SystemParameterExample;
import io.metersphere.base.mapper.SystemParameterMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@Service
public class BaseDisplayService {
@Resource
private SystemParameterMapper systemParameterMapper;
@Resource
private FileService fileService;
public List<SystemParameter> getParamList(String type) {
SystemParameterExample example = new SystemParameterExample();
example.createCriteria().andParamKeyLike(type + "%");
return systemParameterMapper.selectByExample(example);
}
public byte[] loadFileAsBytes(String fileId) {
return fileService.loadFileAsBytes(fileId);
}
public ResponseEntity<byte[]> getImage(String imageName) throws IOException {
byte[] bytes = null;
List<SystemParameter> paramList = getParamList("ui." + imageName);
if (!CollectionUtils.isEmpty(paramList)) {
SystemParameter sp = paramList.get(0);
String paramValue = sp.getParamValue();
if (StringUtils.isNotBlank(paramValue)) {
bytes = loadFileAsBytes(paramValue);
}
}
MediaType contentType = MediaType.parseMediaType("application/octet-stream");
if (bytes == null) {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
switch (imageName) {
case "logo":
bytes = IOUtils.toByteArray(resolver.getResources("/static/img/logo-light-MeterSphere.*.svg")[0].getInputStream());
contentType = MediaType.valueOf("image/svg+xml");
break;
case "loginImage":
bytes = IOUtils.toByteArray(resolver.getResources("/static/img/info.*.png")[0].getInputStream());
break;
case "loginLogo":
bytes = IOUtils.toByteArray(resolver.getResources("/static/img/logo-dark-MeterSphere.*.svg")[0].getInputStream());
contentType = MediaType.valueOf("image/svg+xml");
break;
default:
break;
}
}
return ResponseEntity.ok()
.contentType(contentType)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + imageName + "\"")
.body(bytes);
}
}

@ -1 +1 @@
Subproject commit 2ba1351aa0135cdf3e5de740fa2d9c185b60ce9d
Subproject commit f514243111c3acb317e80da23857143857a53566