2018-01-17 14:10:40 +08:00
|
|
|
|
package cn.keking.web.controller;
|
|
|
|
|
|
2020-05-14 19:28:21 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
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;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2019-06-17 14:21:16 +08:00
|
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
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.*;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author yudian-it
|
|
|
|
|
*/
|
|
|
|
|
@Controller
|
|
|
|
|
public class OnlinePreviewController {
|
|
|
|
|
|
2020-05-14 10:11:15 +08:00
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private final FilePreviewFactory previewFactory;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private final CacheService cacheService;
|
|
|
|
|
|
|
|
|
|
private final FileUtils fileUtils;
|
|
|
|
|
|
|
|
|
|
private final DownloadUtils downloadUtils;
|
|
|
|
|
|
|
|
|
|
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
|
|
|
|
|
FileUtils fileUtils,
|
|
|
|
|
CacheService cacheService,
|
|
|
|
|
DownloadUtils downloadUtils) {
|
|
|
|
|
this.previewFactory = filePreviewFactory;
|
|
|
|
|
this.fileUtils = fileUtils;
|
|
|
|
|
this.cacheService = cacheService;
|
|
|
|
|
this.downloadUtils = downloadUtils;
|
|
|
|
|
}
|
2018-01-19 14:51:18 +08:00
|
|
|
|
|
2019-06-17 14:21:16 +08:00
|
|
|
|
|
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"));
|
2020-05-14 19:28:21 +08:00
|
|
|
|
model.addAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
|
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);
|
2020-05-14 10:11:15 +08:00
|
|
|
|
logger.info("预览文件url:{},previewType:{}", url, fileAttribute.getType());
|
2019-06-17 14:21:16 +08:00
|
|
|
|
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")
|
2020-05-14 10:11:15 +08:00
|
|
|
|
public String picturesPreview(Model model, HttpServletRequest req) {
|
2018-03-08 14:28:44 +08:00
|
|
|
|
String urls = req.getParameter("urls");
|
|
|
|
|
String currentUrl = req.getParameter("currentUrl");
|
2020-05-14 10:11:15 +08:00
|
|
|
|
logger.info("预览文件url:{},urls:{}", currentUrl, urls);
|
|
|
|
|
String[] imgs = urls.split("\\|");
|
2020-05-15 18:09:19 +08:00
|
|
|
|
List<String> imgurls = Arrays.asList(imgs);
|
2018-03-08 14:28:44 +08:00
|
|
|
|
model.addAttribute("imgurls", imgurls);
|
2020-05-14 10:11:15 +08:00
|
|
|
|
model.addAttribute("currentUrl", currentUrl);
|
2018-03-08 14:28:44 +08:00
|
|
|
|
return "picture";
|
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据url获取文件内容
|
|
|
|
|
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
|
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param urlPath url
|
|
|
|
|
* @param response response
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
2020-05-15 18:09:19 +08:00
|
|
|
|
public void getCorsFile(String urlPath, HttpServletResponse response) {
|
2020-05-14 10:11:15 +08:00
|
|
|
|
logger.info("下载跨域pdf文件url:{}", urlPath);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
try {
|
2020-05-20 06:52:49 +08:00
|
|
|
|
downloadUtils.saveToOutputStreamFromUrl(urlPath, response.getOutputStream());
|
2020-05-14 10:11:15 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("下载跨域pdf文件异常,url:{}", urlPath, e);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 14:51:18 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通过api接口入队
|
|
|
|
|
* @param url 请编码后在入队
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/addTask")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public String addQueueTask(String url) {
|
2020-05-14 10:11:15 +08:00
|
|
|
|
logger.info("添加转码队列url:{}", 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
|
|
|
|
}
|