Browse Source

增强url base64解码失败时的提示信息

url
kl 4 years ago committed by kl
parent
commit
ef46e2c51e
  1. 24
      server/src/main/java/cn/keking/service/impl/OtherFilePreviewImpl.java
  2. 30
      server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java

24
server/src/main/java/cn/keking/service/impl/OtherFilePreviewImpl.java

@ -15,7 +15,7 @@ public class OtherFilePreviewImpl implements FilePreview {
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
return this.notSupportedFile(model,fileAttribute,"系统还不支持该格式文件的在线预览");
return this.notSupportedFile(model, fileAttribute, "系统还不支持该格式文件的在线预览");
}
/**
@ -24,8 +24,28 @@ public class OtherFilePreviewImpl implements FilePreview {
* @return 页面
*/
public String notSupportedFile(Model model, FileAttribute fileAttribute, String errMsg) {
model.addAttribute("fileType", fileAttribute.getSuffix());
return this.notSupportedFile(model, fileAttribute.getSuffix(), errMsg);
}
/**
* 通用的预览失败导向到不支持的文件响应页面
*
* @return 页面
*/
public String notSupportedFile(Model model, String errMsg) {
return this.notSupportedFile(model, "未知", errMsg);
}
/**
* 通用的预览失败导向到不支持的文件响应页面
*
* @return 页面
*/
public String notSupportedFile(Model model, String fileType, String errMsg) {
model.addAttribute("fileType", fileType);
model.addAttribute("msg", errMsg);
return NOT_SUPPORTED_FILE_PAGE;
}
}

30
server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java

@ -5,6 +5,7 @@ import cn.keking.service.FilePreview;
import cn.keking.service.FilePreviewFactory;
import cn.keking.service.cache.CacheService;
import cn.keking.service.impl.OtherFilePreviewImpl;
import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService;
import org.slf4j.Logger;
@ -31,21 +32,30 @@ import static cn.keking.service.FilePreview.PICTURE_FILE_PREVIEW_PAGE;
@Controller
public class OnlinePreviewController {
public static final String BASE64_DECODE_ERROR_MSG = "Base64解码失败,请检查你的 %s 是否采用 Base64 + urlEncode 双重编码了!";
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
private final FilePreviewFactory previewFactory;
private final CacheService cacheService;
private final FileHandlerService fileHandlerService;
private final OtherFilePreviewImpl otherFilePreview;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService) {
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FileHandlerService fileHandlerService, CacheService cacheService, OtherFilePreviewImpl otherFilePreview) {
this.previewFactory = filePreviewFactory;
this.fileHandlerService = fileHandlerService;
this.cacheService = cacheService;
this.otherFilePreview = otherFilePreview;
}
@RequestMapping(value = "/onlinePreview")
public String onlinePreview(String url, Model model, HttpServletRequest req) {
String fileUrl = new String(Base64Utils.decodeFromString(url));
String fileUrl;
try {
fileUrl = new String(Base64Utils.decodeFromString(url));
} catch (Exception ex) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
return otherFilePreview.notSupportedFile(model, errorMsg);
}
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
FilePreview filePreview = previewFactory.get(fileAttribute);
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
@ -54,18 +64,24 @@ public class OnlinePreviewController {
@RequestMapping(value = "/picturesPreview")
public String picturesPreview(String urls, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
String fileUrls = new String(Base64Utils.decodeFromString(urls));
String fileUrls;
try {
fileUrls = new String(Base64Utils.decodeFromString(urls));
} catch (Exception ex) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "urls");
return otherFilePreview.notSupportedFile(model, errorMsg);
}
logger.info("预览文件url:{},urls:{}", fileUrls, urls);
// 抽取文件并返回文件列表
String[] imgs = fileUrls.split("\\|");
List<String> imgUrls = Arrays.asList(imgs);
String[] images = fileUrls.split("\\|");
List<String> imgUrls = Arrays.asList(images);
model.addAttribute("imgUrls", imgUrls);
String currentUrl = req.getParameter("currentUrl");
if(StringUtils.hasText(currentUrl)){
if (StringUtils.hasText(currentUrl)) {
String decodedCurrentUrl = new String(Base64Utils.decodeFromString(currentUrl));
model.addAttribute("currentUrl", decodedCurrentUrl);
}else {
} else {
model.addAttribute("currentUrl", imgUrls.get(0));
}
return PICTURE_FILE_PREVIEW_PAGE;

Loading…
Cancel
Save