FileHandlerService.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. package cn.keking.service;
  2. import cn.keking.config.ConfigConstants;
  3. import cn.keking.model.FileAttribute;
  4. import cn.keking.model.FileType;
  5. import cn.keking.service.cache.CacheService;
  6. import cn.keking.service.cache.NotResourceCache;
  7. import cn.keking.utils.EncodingDetects;
  8. import cn.keking.utils.KkFileUtils;
  9. import cn.keking.utils.UrlEncoderUtils;
  10. import cn.keking.utils.WebUtils;
  11. import cn.keking.web.filter.BaseUrlFilter;
  12. import com.aspose.cad.*;
  13. import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
  14. import com.aspose.cad.imageoptions.CadRasterizationOptions;
  15. import com.aspose.cad.imageoptions.PdfOptions;
  16. import com.aspose.cad.imageoptions.SvgOptions;
  17. import com.aspose.cad.imageoptions.TiffOptions;
  18. import com.aspose.cad.internal.Exceptions.TimeoutException;
  19. import com.itextpdf.text.pdf.PdfReader;
  20. import org.apache.commons.lang3.exception.ExceptionUtils;
  21. import org.apache.pdfbox.pdmodel.PDDocument;
  22. import org.apache.pdfbox.rendering.ImageType;
  23. import org.apache.pdfbox.rendering.PDFRenderer;
  24. import org.apache.pdfbox.tools.imageio.ImageIOUtil;
  25. import org.apache.poi.EncryptedDocumentException;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.InitializingBean;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import org.springframework.context.annotation.DependsOn;
  31. import org.springframework.stereotype.Component;
  32. import org.springframework.util.CollectionUtils;
  33. import org.springframework.util.ObjectUtils;
  34. import org.springframework.util.StringUtils;
  35. import javax.servlet.http.HttpServletRequest;
  36. import java.awt.image.BufferedImage;
  37. import java.io.*;
  38. import java.net.URLDecoder;
  39. import java.net.URLEncoder;
  40. import java.nio.charset.StandardCharsets;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Objects;
  45. import java.util.concurrent.*;
  46. import java.util.stream.IntStream;
  47. /**
  48. * @author yudian-it
  49. * @date 2017/11/13
  50. */
  51. @Component
  52. @DependsOn(ConfigConstants.BEAN_NAME)
  53. public class FileHandlerService implements InitializingBean {
  54. private static final String PDF2JPG_IMAGE_FORMAT = ".jpg";
  55. private static final String PDF_PASSWORD_MSG = "password";
  56. private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class);
  57. private final String fileDir = ConfigConstants.getFileDir();
  58. private final CacheService cacheService;
  59. @Value("${server.tomcat.uri-encoding:UTF-8}")
  60. private String uriEncoding;
  61. public FileHandlerService(CacheService cacheService) {
  62. this.cacheService = cacheService;
  63. }
  64. /**
  65. * @return 已转换过的文件集合(缓存)
  66. */
  67. public Map<String, String> listConvertedFiles() {
  68. return cacheService.getPDFCache();
  69. }
  70. /**
  71. * @return 已转换过的文件,根据文件名获取
  72. */
  73. public String getConvertedFile(String key) {
  74. return cacheService.getPDFCache(key);
  75. }
  76. /**
  77. * @param key pdf本地路径
  78. * @return 已将pdf转换成图片的图片本地相对路径
  79. */
  80. public Integer getPdf2jpgCache(String key) {
  81. return cacheService.getPdfImageCache(key);
  82. }
  83. /**
  84. * 从路径中获取文件负
  85. *
  86. * @param path 类似这种:C:\Users\yudian-it\Downloads
  87. * @return 文件名
  88. */
  89. public String getFileNameFromPath(String path) {
  90. return path.substring(path.lastIndexOf(File.separator) + 1);
  91. }
  92. /**
  93. * 获取相对路径
  94. *
  95. * @param absolutePath 绝对路径
  96. * @return 相对路径
  97. */
  98. public String getRelativePath(String absolutePath) {
  99. return absolutePath.substring(fileDir.length());
  100. }
  101. /**
  102. * 添加转换后PDF缓存
  103. *
  104. * @param fileName pdf文件名
  105. * @param value 缓存相对路径
  106. */
  107. public void addConvertedFile(String fileName, String value) {
  108. cacheService.putPDFCache(fileName, value);
  109. }
  110. /**
  111. * 添加转换后图片组缓存
  112. *
  113. * @param pdfFilePath pdf文件绝对路径
  114. * @param num 图片张数
  115. */
  116. public void addPdf2jpgCache(String pdfFilePath, int num) {
  117. cacheService.putPdfImageCache(pdfFilePath, num);
  118. }
  119. /**
  120. * 获取redis中压缩包内图片文件
  121. *
  122. * @param compressFileKey compressFileKey
  123. * @return 图片文件访问url列表
  124. */
  125. public List<String> getImgCache(String compressFileKey) {
  126. return cacheService.getImgCache(compressFileKey);
  127. }
  128. /**
  129. * 设置redis中压缩包内图片文件
  130. *
  131. * @param fileKey fileKey
  132. * @param imgs 图片文件访问url列表
  133. */
  134. public void putImgCache(String fileKey, List<String> imgs) {
  135. cacheService.putImgCache(fileKey, imgs);
  136. }
  137. /**
  138. * cad定义线程池
  139. */
  140. private ExecutorService pool = null;
  141. @Override
  142. public void afterPropertiesSet() throws Exception {
  143. pool = Executors.newFixedThreadPool(ConfigConstants.getCadThread());
  144. }
  145. /**
  146. * 对转换后的文件进行操作(改变编码方式)
  147. *
  148. * @param outFilePath 文件绝对路径
  149. */
  150. public void doActionConvertedFile(String outFilePath) {
  151. String charset = EncodingDetects.getJavaEncode(outFilePath);
  152. StringBuilder sb = new StringBuilder();
  153. try (InputStream inputStream = new FileInputStream(outFilePath); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
  154. String line;
  155. while (null != (line = reader.readLine())) {
  156. if (line.contains("charset=gb2312")) {
  157. line = line.replace("charset=gb2312", "charset=utf-8");
  158. }
  159. sb.append(line);
  160. }
  161. // 添加sheet控制头
  162. sb.append("<script src=\"js/jquery-3.6.1.min.js\" type=\"text/javascript\"></script>");
  163. sb.append("<script src=\"excel/excel.header.js\" type=\"text/javascript\"></script>");
  164. sb.append("<link rel=\"stylesheet\" href=\"excel/excel.css\">");
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. // 重新写入文件
  169. try (FileOutputStream fos = new FileOutputStream(outFilePath); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
  170. writer.write(sb.toString());
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. /**
  176. * 获取本地 pdf 转 image 后的 web 访问地址
  177. *
  178. * @param pdfFilePath pdf文件名
  179. * @param index 图片索引
  180. * @return 图片访问地址
  181. */
  182. private String getPdf2jpgUrl(String pdfFilePath, int index) {
  183. String baseUrl = BaseUrlFilter.getBaseUrl();
  184. pdfFilePath = pdfFilePath.replace(fileDir, "");
  185. String pdfFolder = pdfFilePath.substring(0, pdfFilePath.length() - 4);
  186. String urlPrefix;
  187. try {
  188. urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
  189. } catch (UnsupportedEncodingException e) {
  190. logger.error("UnsupportedEncodingException", e);
  191. urlPrefix = baseUrl + pdfFolder;
  192. }
  193. return urlPrefix + "/" + index + PDF2JPG_IMAGE_FORMAT;
  194. }
  195. /**
  196. * 获取缓存中的 pdf 转换成 jpg 图片集
  197. *
  198. * @param pdfFilePath pdf文件路径
  199. * @return 图片访问集合
  200. */
  201. private List<String> loadPdf2jpgCache(String pdfFilePath) {
  202. List<String> imageUrls = new ArrayList<>();
  203. Integer imageCount = this.getPdf2jpgCache(pdfFilePath);
  204. if (Objects.isNull(imageCount)) {
  205. return imageUrls;
  206. }
  207. IntStream.range(0, imageCount).forEach(i -> {
  208. String imageUrl = this.getPdf2jpgUrl(pdfFilePath, i);
  209. imageUrls.add(imageUrl);
  210. });
  211. return imageUrls;
  212. }
  213. /**
  214. * pdf文件转换成jpg图片集
  215. * fileNameFilePath pdf文件路径
  216. * pdfFilePath pdf输出文件路径
  217. * pdfName pdf文件名称
  218. * loadPdf2jpgCache 图片访问集合
  219. */
  220. public List<String> pdf2jpg(String fileNameFilePath, String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
  221. boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
  222. boolean usePasswordCache = fileAttribute.getUsePasswordCache();
  223. String filePassword = fileAttribute.getFilePassword();
  224. String pdfPassword = null;
  225. PDDocument doc = null;
  226. PdfReader pdfReader = null;
  227. if (!forceUpdatedCache) {
  228. List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath);
  229. if (!CollectionUtils.isEmpty(cacheResult)) {
  230. return cacheResult;
  231. }
  232. }
  233. List<String> imageUrls = new ArrayList<>();
  234. try {
  235. File pdfFile = new File(fileNameFilePath);
  236. if (!pdfFile.exists()) {
  237. return null;
  238. }
  239. doc = PDDocument.load(pdfFile, filePassword);
  240. doc.setResourceCache(new NotResourceCache());
  241. int pageCount = doc.getNumberOfPages();
  242. PDFRenderer pdfRenderer = new PDFRenderer(doc);
  243. int index = pdfFilePath.lastIndexOf(".");
  244. String folder = pdfFilePath.substring(0, index);
  245. File path = new File(folder);
  246. if (!path.exists() && !path.mkdirs()) {
  247. logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
  248. }
  249. String imageFilePath;
  250. for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
  251. imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
  252. BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
  253. ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
  254. String imageUrl = this.getPdf2jpgUrl(pdfFilePath, pageIndex);
  255. imageUrls.add(imageUrl);
  256. }
  257. try {
  258. if (!ObjectUtils.isEmpty(filePassword)) { //获取到密码 判断是否是加密文件
  259. pdfReader = new PdfReader(fileNameFilePath); //读取PDF文件 通过异常获取该文件是否有密码字符
  260. }
  261. } catch (Exception e) { //获取异常方法 判断是否有加密字符串
  262. Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
  263. for (Throwable throwable : throwableArray) {
  264. if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
  265. if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
  266. pdfPassword = PDF_PASSWORD_MSG; //查询到该文件是密码文件 输出带密码的值
  267. }
  268. }
  269. }
  270. if (!PDF_PASSWORD_MSG.equals(pdfPassword)) { //该文件异常 错误原因非密码原因输出错误
  271. logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
  272. }
  273. } finally {
  274. if (pdfReader != null) { //关闭
  275. pdfReader.close();
  276. }
  277. }
  278. if (usePasswordCache || !PDF_PASSWORD_MSG.equals(pdfPassword)) { //加密文件 判断是否启用缓存命令
  279. this.addPdf2jpgCache(pdfFilePath, pageCount);
  280. }
  281. } catch (IOException e) {
  282. if (!e.getMessage().contains(PDF_PASSWORD_MSG)) {
  283. logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
  284. }
  285. throw new Exception(e);
  286. } finally {
  287. if (doc != null) { //关闭
  288. doc.close();
  289. }
  290. }
  291. return imageUrls;
  292. }
  293. /**
  294. * cad文件转pdf
  295. *
  296. * @param inputFilePath cad文件路径
  297. * @param outputFilePath pdf输出文件路径
  298. * @return 转换是否成功
  299. */
  300. public String cadToPdf(String inputFilePath, String outputFilePath, String cadPreviewType, FileAttribute fileAttribute) throws Exception {
  301. final InterruptionTokenSource source = new InterruptionTokenSource();//CAD延时
  302. if (fileAttribute.isCompressFile()) { //判断 是压缩包的创建新的目录
  303. int index = outputFilePath.lastIndexOf("/"); //截取最后一个斜杠的前面的内容
  304. String folder = outputFilePath.substring(0, index);
  305. File path = new File(folder);
  306. //目录不存在 创建新的目录
  307. if (!path.exists()) {
  308. path.mkdirs();
  309. }
  310. }
  311. Callable<String> call = () -> {
  312. File outputFile = new File(outputFilePath);
  313. LoadOptions opts = new LoadOptions();
  314. opts.setSpecifiedEncoding(CodePages.SimpChinese);
  315. Image cadImage = Image.load(inputFilePath, opts);
  316. CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
  317. cadRasterizationOptions.setBackgroundColor(Color.getWhite());
  318. cadRasterizationOptions.setPageWidth(1400);
  319. cadRasterizationOptions.setPageHeight(650);
  320. cadRasterizationOptions.setAutomaticLayoutsScaling(true);
  321. cadRasterizationOptions.setNoScaling(false);
  322. cadRasterizationOptions.setDrawType(1);
  323. SvgOptions SvgOptions = null;
  324. PdfOptions pdfOptions = null;
  325. TiffOptions TiffOptions = null;
  326. switch (cadPreviewType) { //新增格式方法
  327. case "svg":
  328. SvgOptions = new SvgOptions();
  329. SvgOptions.setVectorRasterizationOptions(cadRasterizationOptions);
  330. SvgOptions.setInterruptionToken(source.getToken());
  331. break;
  332. case "pdf":
  333. pdfOptions = new PdfOptions();
  334. pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
  335. pdfOptions.setInterruptionToken(source.getToken());
  336. break;
  337. case "tif":
  338. TiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
  339. TiffOptions.setVectorRasterizationOptions(cadRasterizationOptions);
  340. TiffOptions.setInterruptionToken(source.getToken());
  341. break;
  342. }
  343. try (OutputStream stream = new FileOutputStream(outputFile)) {
  344. switch (cadPreviewType) {
  345. case "svg":
  346. cadImage.save(stream, SvgOptions);
  347. break;
  348. case "pdf":
  349. cadImage.save(stream, pdfOptions);
  350. break;
  351. case "tif":
  352. cadImage.save(stream, TiffOptions);
  353. break;
  354. }
  355. } catch (IOException e) {
  356. logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
  357. return null;
  358. } finally {
  359. //关闭
  360. if (cadImage != null) { //关闭
  361. cadImage.dispose();
  362. }
  363. source.interrupt(); //结束任务
  364. }
  365. return "true";
  366. };
  367. Future<String> result = pool.submit(call);
  368. try {
  369. // 如果在超时时间内,没有数据返回:则抛出TimeoutException异常
  370. result.get(Long.parseLong(ConfigConstants.getCadTimeout()), TimeUnit.SECONDS);
  371. } catch (InterruptedException e) {
  372. System.out.println("InterruptedException发生");
  373. return null;
  374. } catch (ExecutionException e) {
  375. System.out.println("ExecutionException发生");
  376. return null;
  377. } catch (TimeoutException e) {
  378. System.out.println("TimeoutException发生,意味着线程超时报错");
  379. return null;
  380. } finally {
  381. source.dispose();
  382. }
  383. return "true";
  384. }
  385. /**
  386. * @param str 原字符串(待截取原串)
  387. * @param posStr 指定字符串
  388. * @return 截取截取指定字符串之后的数据
  389. */
  390. public static String getSubString(String str, String posStr) {
  391. return str.substring(str.indexOf(posStr) + posStr.length());
  392. }
  393. /**
  394. * 获取文件属性
  395. *
  396. * @param url url
  397. * @return 文件属性
  398. */
  399. public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
  400. FileAttribute attribute = new FileAttribute();
  401. String suffix;
  402. FileType type;
  403. String originFileName; //原始文件名
  404. String outFilePath; //生成文件的路径
  405. String originFilePath; //原始文件路径
  406. String fullFileName = WebUtils.getUrlParameterReg(url, "fullfilename");
  407. String compressFileKey = WebUtils.getUrlParameterReg(url, "kkCompressfileKey"); //压缩包获取文件名
  408. String compressFilePath = WebUtils.getUrlParameterReg(url, "kkCompressfilepath"); //压缩包获取文件路径
  409. if (StringUtils.hasText(fullFileName)) {
  410. originFileName = fullFileName;
  411. type = FileType.typeFromFileName(fullFileName);
  412. suffix = KkFileUtils.suffixFromFileName(fullFileName);
  413. // 移除fullfilename参数
  414. if (url.indexOf("fullfilename=" + fullFileName + "&") > 0) {
  415. url = url.replace("fullfilename=" + fullFileName + "&", "");
  416. } else {
  417. url = url.replace("fullfilename=" + fullFileName, "");
  418. }
  419. } else {
  420. originFileName = WebUtils.getFileNameFromURL(url);
  421. type = FileType.typeFromUrl(url);
  422. suffix = WebUtils.suffixFromUrl(url);
  423. }
  424. boolean isCompressFile = !ObjectUtils.isEmpty(compressFileKey);
  425. if (isCompressFile) { //判断是否使用特定压缩包符号
  426. try {
  427. // http://127.0.0.1:8012/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_
  428. // http://127.0.0.1:8012/preview/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_ 获取路径就会错误 需要下面的方法
  429. String urlStrr = getSubString(compressFilePath, compressFileKey); //反代情况下添加前缀,只获取有压缩包字符的路径
  430. originFileName = compressFileKey + urlStrr.trim(); //拼接完整路径
  431. originFileName = URLDecoder.decode(originFileName, uriEncoding); //压缩包文件中文编码问题
  432. attribute.setSkipDownLoad(true);
  433. } catch (UnsupportedEncodingException e) {
  434. e.printStackTrace();
  435. }
  436. }
  437. url = WebUtils.encodeUrlFileName(url);
  438. if (UrlEncoderUtils.hasUrlEncoded(originFileName)) { //判断文件名是否转义
  439. try {
  440. originFileName = URLDecoder.decode(originFileName, uriEncoding).replaceAll("\\+", "%20");
  441. } catch (UnsupportedEncodingException e) {
  442. e.printStackTrace();
  443. }
  444. }
  445. originFileName = KkFileUtils.htmlEscape(originFileName); //文件名处理
  446. boolean isHtmlView = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
  447. String cacheFilePrefixName = originFileName.substring(0, originFileName.lastIndexOf(".")) + suffix + "."; //这里统一文件名处理 下面更具类型 各自添加后缀
  448. String cacheFileName = this.getCacheFileName(type, originFileName, cacheFilePrefixName, isHtmlView, isCompressFile);
  449. outFilePath = fileDir + cacheFileName;
  450. originFilePath = fileDir + originFileName;
  451. String cacheListName = cacheFilePrefixName + "ListName"; //文件列表缓存文件名
  452. attribute.setType(type);
  453. attribute.setName(originFileName);
  454. attribute.setCacheName(cacheFileName);
  455. attribute.setCacheListName(cacheListName);
  456. attribute.setHtmlView(isHtmlView);
  457. attribute.setOutFilePath(outFilePath);
  458. attribute.setOriginFilePath(originFilePath);
  459. attribute.setSuffix(suffix);
  460. attribute.setUrl(url);
  461. if (req != null) {
  462. String officePreviewType = req.getParameter("officePreviewType");
  463. String forceUpdatedCache = req.getParameter("forceUpdatedCache");
  464. String usePasswordCache = req.getParameter("usePasswordCache");
  465. if (StringUtils.hasText(officePreviewType)) {
  466. attribute.setOfficePreviewType(officePreviewType);
  467. }
  468. if (StringUtils.hasText(compressFileKey)) {
  469. attribute.setCompressFile(isCompressFile);
  470. attribute.setCompressFileKey(compressFileKey);
  471. }
  472. if ("true".equalsIgnoreCase(forceUpdatedCache)) {
  473. attribute.setForceUpdatedCache(true);
  474. }
  475. String tifPreviewType = req.getParameter("tifPreviewType");
  476. if (StringUtils.hasText(tifPreviewType)) {
  477. attribute.setTifPreviewType(tifPreviewType);
  478. }
  479. String filePassword = req.getParameter("filePassword");
  480. if (StringUtils.hasText(filePassword)) {
  481. attribute.setFilePassword(filePassword);
  482. }
  483. if ("true".equalsIgnoreCase(usePasswordCache)) {
  484. attribute.setUsePasswordCache(true);
  485. }
  486. String kkProxyAuthorization = req.getHeader("kk-proxy-authorization");
  487. attribute.setKkProxyAuthorization(kkProxyAuthorization);
  488. }
  489. return attribute;
  490. }
  491. /**
  492. * 获取缓存的文件名
  493. *
  494. * @return 文件名
  495. */
  496. private String getCacheFileName(FileType type, String originFileName, String cacheFilePrefixName, boolean isHtmlView, boolean isCompressFile) {
  497. String cacheFileName;
  498. if (type.equals(FileType.OFFICE)) {
  499. cacheFileName = cacheFilePrefixName + (isHtmlView ? "html" : "pdf"); //生成文件添加类型后缀 防止同名文件
  500. } else if (type.equals(FileType.PDF)) {
  501. cacheFileName = originFileName;
  502. } else if (type.equals(FileType.MEDIACONVERT)) {
  503. cacheFileName = cacheFilePrefixName + "mp4";
  504. } else if (type.equals(FileType.CAD)) {
  505. String cadPreviewType = ConfigConstants.getCadPreviewType();
  506. cacheFileName = cacheFilePrefixName + cadPreviewType; //生成文件添加类型后缀 防止同名文件
  507. } else if (type.equals(FileType.COMPRESS)) {
  508. cacheFileName = originFileName;
  509. } else if (type.equals(FileType.TIFF)) {
  510. cacheFileName = cacheFilePrefixName + ConfigConstants.getTifPreviewType();
  511. } else {
  512. cacheFileName = originFileName;
  513. }
  514. if (isCompressFile) { //判断是否使用特定压缩包符号
  515. cacheFileName = "_decompression" + cacheFileName;
  516. }
  517. return cacheFileName;
  518. }
  519. /**
  520. * @return 已转换过的视频文件集合(缓存)
  521. */
  522. public Map<String, String> listConvertedMedias() {
  523. return cacheService.getMediaConvertCache();
  524. }
  525. /**
  526. * 添加转换后的视频文件缓存
  527. *
  528. * @param fileName
  529. * @param value
  530. */
  531. public void addConvertedMedias(String fileName, String value) {
  532. cacheService.putMediaConvertCache(fileName, value);
  533. }
  534. /**
  535. * @return 已转换视频文件缓存,根据文件名获取
  536. */
  537. public String getConvertedMedias(String key) {
  538. return cacheService.getMediaConvertCache(key);
  539. }
  540. }