|
|
@@ -1,5 +1,6 @@
|
|
|
package com.github.jfcloud.excel.editor.docdeal.controller;
|
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.io.file.FileNameUtil;
|
|
|
import cn.hutool.core.lang.Pair;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
@@ -8,6 +9,7 @@ import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.amazonaws.services.s3.model.S3ObjectInputStream;
|
|
|
import com.github.jfcloud.excel.editor.docdeal.bean.*;
|
|
|
import com.github.jfcloud.excel.editor.docdeal.constant.CommandEnum;
|
|
|
import com.github.jfcloud.excel.editor.docdeal.constant.DocumentConstants;
|
|
|
@@ -17,12 +19,16 @@ import com.github.jfcloud.excel.editor.docdeal.oss.service.OssTemplate;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.MediaTypeFactory;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.ui.Model;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -49,17 +55,34 @@ public class PreviewController {
|
|
|
vo.setEdit(false);
|
|
|
log.info("==> 查看文档 vo: {}", vo);
|
|
|
Pair<Document, DocumentEditParam> pair = docInfo(vo);
|
|
|
+ model.addAttribute("name", vo.getName());
|
|
|
|
|
|
- if (!ossTemplate.exist(pair.getKey().getStorage(), vo.getName())) {
|
|
|
- model.addAttribute("name", vo.getName());
|
|
|
+ String bucketName = pair.getKey().getStorage();
|
|
|
+ if (!ossTemplate.exist(bucketName, vo.getName())) {
|
|
|
return "/notFound";
|
|
|
}
|
|
|
|
|
|
+ Optional<MediaType> opt = MediaTypeFactory.getMediaType(vo.getName());
|
|
|
+ if (opt.isPresent()) {
|
|
|
+ MediaType mediaType = opt.get();
|
|
|
+ //图片
|
|
|
+ if (mediaType.getType().equals("image")) {
|
|
|
+ model.addAttribute("src", "/download?name=" + vo.getName() + "&bucket=" + bucketName);
|
|
|
+ return "/viewerImg";
|
|
|
+ }
|
|
|
+ //文本
|
|
|
+ if (mediaType.getType().equals("text")) {
|
|
|
+ S3ObjectInputStream inputStream = ossTemplate.getObject(bucketName, vo.getName()).getObjectContent();
|
|
|
+ model.addAttribute("content", IoUtil.read(inputStream, StandardCharsets.UTF_8));
|
|
|
+ return "/viewerTxt";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
model.addAttribute("document", pair.getKey());
|
|
|
model.addAttribute("documentEditParam", pair.getValue());
|
|
|
|
|
|
String suffix = FileNameUtil.getSuffix(vo.getName());
|
|
|
- if (suffix.equals("xls") || suffix.equals("xlsx")) {
|
|
|
+ if (suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) {
|
|
|
return "/viewerExcel";
|
|
|
}
|
|
|
return "/viewer";
|