2018-01-17 14:10:40 +08:00
|
|
|
package cn.keking.web.controller;
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
import cn.keking.model.FileAttribute;
|
2018-01-17 17:51:53 +08:00
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
import cn.keking.service.FilePreviewFactory;
|
|
|
|
|
2019-04-08 17:50:13 +08:00
|
|
|
import cn.keking.service.cache.CacheService;
|
2019-06-17 14:21:16 +08:00
|
|
|
import cn.keking.utils.FileUtils;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2019-06-17 14:21:16 +08:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
2018-01-19 14:51:18 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2018-01-17 14:10:40 +08:00
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
2018-01-19 14:51:18 +08:00
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2019-05-09 15:02:02 +08:00
|
|
|
import java.io.*;
|
|
|
|
import java.net.*;
|
2018-01-17 14:10:40 +08:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author yudian-it
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
public class OnlinePreviewController {
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(OnlinePreviewController.class);
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
@Autowired
|
2020-02-18 19:36:15 +08:00
|
|
|
private FilePreviewFactory previewFactory;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
@Autowired
|
2020-02-18 19:36:15 +08:00
|
|
|
private CacheService cacheService;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
@Autowired
|
|
|
|
private FileUtils fileUtils;
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
/**
|
|
|
|
* @param url
|
|
|
|
* @param model
|
|
|
|
* @return
|
|
|
|
*/
|
2019-10-31 16:52:58 +08:00
|
|
|
@RequestMapping(value = "/onlinePreview", method = RequestMethod.GET)
|
2018-01-17 17:51:53 +08:00
|
|
|
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
2019-06-17 14:21:16 +08:00
|
|
|
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
2018-01-19 14:51:18 +08:00
|
|
|
req.setAttribute("fileKey", req.getParameter("fileKey"));
|
2019-04-25 18:39:58 +08:00
|
|
|
model.addAttribute("officePreviewType", req.getParameter("officePreviewType"));
|
2019-06-17 14:21:16 +08:00
|
|
|
FilePreview filePreview = previewFactory.get(fileAttribute);
|
|
|
|
return filePreview.filePreviewHandle(url, model, fileAttribute);
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-31 16:52:58 +08:00
|
|
|
@RequestMapping(value = "/picturesPreview")
|
2018-03-08 14:28:44 +08:00
|
|
|
public String picturesPreview(Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
|
|
|
String urls = req.getParameter("urls");
|
|
|
|
String currentUrl = req.getParameter("currentUrl");
|
|
|
|
// 路径转码
|
|
|
|
String decodedUrl = URLDecoder.decode(urls, "utf-8");
|
|
|
|
String decodedCurrentUrl = URLDecoder.decode(currentUrl, "utf-8");
|
|
|
|
// 抽取文件并返回文件列表
|
|
|
|
String[] imgs = decodedUrl.split("\\|");
|
|
|
|
List imgurls = Arrays.asList(imgs);
|
|
|
|
model.addAttribute("imgurls", imgurls);
|
|
|
|
model.addAttribute("currentUrl",decodedCurrentUrl);
|
|
|
|
return "picture";
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
/**
|
|
|
|
* 根据url获取文件内容
|
|
|
|
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
|
|
|
|
*
|
|
|
|
* @param urlPath
|
|
|
|
* @param resp
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
|
|
|
public void getCorsFile(String urlPath, HttpServletResponse resp) {
|
|
|
|
InputStream inputStream = null;
|
|
|
|
try {
|
|
|
|
String strUrl = urlPath.trim();
|
2019-05-09 15:02:02 +08:00
|
|
|
URL url = new URL(new URI(strUrl).toASCIIString());
|
2018-01-17 14:10:40 +08:00
|
|
|
//打开请求连接
|
|
|
|
URLConnection connection = url.openConnection();
|
|
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
|
|
|
|
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
|
inputStream = httpURLConnection.getInputStream();
|
|
|
|
byte[] bs = new byte[1024];
|
|
|
|
int len;
|
|
|
|
while (-1 != (len = inputStream.read(bs))) {
|
|
|
|
resp.getOutputStream().write(bs, 0, len);
|
|
|
|
}
|
2019-05-09 15:02:02 +08:00
|
|
|
} catch (IOException | URISyntaxException e) {
|
2019-06-17 14:21:16 +08:00
|
|
|
LOGGER.error("下载pdf文件失败", e);
|
2018-01-17 14:10:40 +08:00
|
|
|
} finally {
|
|
|
|
if (inputStream != null) {
|
|
|
|
IOUtils.closeQuietly(inputStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
/**
|
|
|
|
* 通过api接口入队
|
|
|
|
* @param url 请编码后在入队
|
|
|
|
*/
|
|
|
|
@GetMapping("/addTask")
|
|
|
|
@ResponseBody
|
|
|
|
public String addQueueTask(String url) {
|
2019-04-08 17:50:13 +08:00
|
|
|
cacheService.addQueueTask(url);
|
2018-01-19 14:51:18 +08:00
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
}
|