From 02d038387df02b1e4dcf140c3e78c6f95719155e Mon Sep 17 00:00:00 2001
From: 许鹏程 <1821349743@qq.com>
Date: 星期二, 11 二月 2025 10:50:53 +0800
Subject: [PATCH] excel文件预览app端特殊处理

---
 src/main/java/com/product/file/service/FileManagerService.java |  119 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 97 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java
index 02613ba..e8d3efe 100644
--- a/src/main/java/com/product/file/service/FileManagerService.java
+++ b/src/main/java/com/product/file/service/FileManagerService.java
@@ -1,9 +1,11 @@
 package com.product.file.service;
 
+import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.product.common.lang.StringUtils;
+import com.product.core.config.CoreConst;
 import com.product.core.config.Global;
 import com.product.core.connection.ConnectionManager;
 import com.product.core.dao.BaseDao;
@@ -20,13 +22,11 @@
 import com.product.file.util.FileUtil;
 import com.product.file.util.FileUtils;
 import com.product.module.sys.entity.SystemUser;
+import com.product.util.BaseUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
 
 import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.net.URLEncoder;
@@ -232,6 +232,39 @@
 		return baseDao.executeUpdate(sql.toString());
 	}
 
+	/**
+	 * 鏍规嵁闄勪欢uuid鍒ゆ柇鏂囦欢鏄惁瀛樺湪
+	 *
+	 * @param uuid
+	 * @return
+	 */
+	public boolean fileExist(String uuid) {
+		if (StringUtils.isEmpty(uuid)) {
+			return false;
+		}
+		FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false);
+		if (attachmentFse == null) {
+			return false;
+		}
+
+		//鍒ゆ柇鏂囦欢瀛樻斁鍦ㄦ湇鍔″櫒杩樻槸鏈湴
+		boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN));
+		String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL);
+		String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE);
+		boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN));
+
+		if (needDownloadFromServerFlag) {
+			// 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�
+			FTPService ftpService = new FTPService();
+			return ftpService.fileIsExist(dir, fileName);
+		} else {
+			// 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢
+			String localBasePath = Global.getSystemConfig("local.dir", "");
+			String path = localBasePath + File.separator + dir + File.separator + fileName;
+			File file = new File(path);
+			return file.isFile();
+		}
+	}
 
 	/**
 	 * 涓婁紶鏂囦欢鍒版湰鍦版湇鍔″櫒
@@ -286,7 +319,7 @@
 		String uuids = "";
 		for (Map.Entry<Object, Object> entry : map.entrySet()) {
 			fieldFse = fieldMetaEntity.getFieldMeta(fieldName);
-			if (fieldFse == null || !CmnConst.ATTACHMENT_TYPE.equals(fieldFse.getString(CmnConst.FIELD_TYPE))) {
+			if (fieldFse == null || (!CmnConst.ATTACHMENT_TYPE.equals(fieldFse.getString(CmnConst.FIELD_TYPE)) && !"file-image".equals(fieldFse.getString(CmnConst.FIELD_TYPE)))) {
 				fse.setValue(entry.getKey().toString(), null);
 				continue;
 			}
@@ -348,7 +381,7 @@
 				attachmentFse.setValue(CmnConst.ENCRPT_SIGN, Global.getPropertyToBoolean("file.encrypt", "true") ? 1 : 0);
 				attachmentFse.setValue(CmnConst.ATTACHMENT_SIZE, fileLength);
 				attachmentFse.setValue(CmnConst.UPLOAD_SIGN, needUpload2FileServerFlag ? 1 : 0);
-
+				attachmentFse.setValue(CmnConst.FUNCTION_UUID, fse.getString(CmnConst.FUNCTION_UUID));
 				attachmentFse.setValue(CmnConst.ATTACHMENT_CONTAINER, fse.getString(CmnConst.ATTACHMENT_CONTAINER));
 				attachmentFse.setValue(CmnConst.ATTACHMENT_DOMAIN, fse.getString(CmnConst.ATTACHMENT_DOMAIN));
 				attachmentFse.setValue(CmnConst.MODULE_UUID, fse.getString(CmnConst.MODULE_UUID));
@@ -386,6 +419,34 @@
 	}
 
 	/**
+	 * 	鏍规嵁闄勪欢琛ㄦ暟鎹敓鎴愭枃妗g储寮�
+	 */
+	public void readAttachmentCreateIndex() {
+		// 閬嶅巻闄勪欢琛�
+		DataTableEntity dtAttachment = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS);
+		if (BaseUtil.dataTableIsEmpty(dtAttachment)) {
+			return;
+		}
+		for (int i = 0; i < dtAttachment.getRows(); i++) {
+			// 鑾峰彇闄勪欢淇℃伅锛屽苟鍒ゆ柇鏄惁鏈夎〃鍜屽瓧娈�
+			FieldSetEntity fseAttachment = dtAttachment.getFieldSetEntity(i);
+			if (BaseUtil.strIsNull(fseAttachment.getString("attachment_data_field")) || BaseUtil.strIsNull(fseAttachment.getString("attachment_data_field"))) {
+				continue;
+			}
+			// 鏌ヨ鍘熸暟鎹紝鏈夊師鏁版嵁鍐嶇敓鎴愭枃妗f绱�
+			FieldSetEntity fseRecord = baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), fseAttachment.getString("attachment_data_field") + " LIKE ?", new Object[]{"%" + fseAttachment.getUUID() + "%"}, false);
+			if (fseRecord != null) {
+				// 鐢熸垚鏂囨。妫�绱俊鎭�
+				FieldSetEntity fseIndex = new FieldSetEntity();
+				fseIndex.setTableName("fseIndex");
+				fseIndex.setValue("function_uuid", fseAttachment.getString("function_uuid"));
+				fseIndex.setValue("attachment_uuid", fseAttachment.getUUID());
+				CreateDocumentIndexThread.getInstance().appendAttaInfo(fseIndex);
+			}
+		}
+	}
+
+	/**
 	 * 鎻愬彇鏂囦欢淇℃伅-涓嬭浇鏂囦欢鎴栬�呭湪绾块瑙堟枃浠�
 	 *
 	 * @param fse
@@ -406,9 +467,17 @@
 		String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN);
 		boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN));
 		boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign);
+		String clientType = SpringMVCContextHolder.getCurrentUser().getClientType();
+
+		String contentType = "multipart/form-data";
+		boolean isExcel = realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls");
 		if (needOnlineViewFlag) {
-			// 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮
-			dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
+			//鐗规畩澶勭悊锛� 濡傛灉瀹㈡埛绔笉鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 鐩存帴杩斿洖excel婧愭枃浠� content澶存爣璇嗘槸excel cheng 2025骞�2鏈�11鏃�10:41:41
+			if (isExcel && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+				contentType = "application/vnd.ms-excel";
+			} else {
+				dir += File.separator + CmnConst.TRANSFER_DIR_NAME;
+			}
 		}
 
 		String path = dir + File.separator + fileName;
@@ -417,11 +486,8 @@
 			FTPService ftpService = new FTPService();
 			logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�...");
 			response.setHeader("Access-Control-Expose-Headers", "*");
-			if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
-				response.setContentType("application/vnd.ms-excel");
-			} else {
-				response.setContentType("multipart/form-data");
-			}
+
+			response.setContentType(contentType);
 			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
 			//璁剧疆澶翠腑鐨勬枃浠剁被鍨�
 			if (!needOnlineViewFlag && !encrptSignFlag) {
@@ -472,6 +538,17 @@
 			String localBasePath = Global.getSystemConfig("local.dir", "");
 			path = localBasePath + File.separator + path;
 			File file = new File(path);
+			if (needOnlineViewFlag && file.exists() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) {
+				//鐗规畩澶勭悊锛� 鏄疉pp 浣嗛瑙堢殑鏂囦欢鏄疎xcel 闇�瑕佸皢涔嬪墠宸茶浆鎹负鐨勬枃浠跺垹闄ゆ帀閲嶆柊杞崲锛堝洜杞崲鍚庣殑鏍煎紡涓嶆槸pdf)
+				String changeTime = "2025-02-11 23:59:59";
+				Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss");
+				long fileTime = file.lastModified();
+				//濡傛灉鏃堕棿鏄� 2025骞�2鏈�10鏃�23:59:59锛岄偅涔堝氨閲嶆柊杞崲
+				//鍒犻櫎鏂囦欢
+				if (fileTime <= changeDate.getTime()) {
+					file.delete();
+				}
+			}
 			if (needOnlineViewFlag && !file.exists()) {
 				// 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣�
 				File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag);
@@ -484,11 +561,8 @@
 			byte[] b = new byte[1024];
 			InputStream is = new FileInputStream(file);
 			response.setHeader("Access-Control-Expose-Headers", "*");
-			if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
-				response.setContentType("application/vnd.ms-excel");
-			} else {
-				response.setContentType("multipart/form-data");
-			}
+
+			response.setContentType(contentType);
 			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
 			response.setContentLengthLong(file.length());
 			try (ServletOutputStream os = response.getOutputStream()) {
@@ -504,7 +578,7 @@
 				}
 				os.flush();
 			}
-
+			is.close();
 		}
 		logger.info("鏂囦欢娴佽幏鍙栨垚鍔�");
 	}
@@ -541,7 +615,7 @@
 			try {
 				temp.createNewFile();
 			} catch (IOException e) {
-				throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : ""));
+				throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : ""));
 			}
 			try (FileOutputStream fos = new FileOutputStream(temp); BufferedOutputStream out = new BufferedOutputStream(fos)) {
 
@@ -549,10 +623,10 @@
 				return temp;
 			} catch (Exception e) {
 				e.printStackTrace();
-				throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : ""));
+				throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : ""));
 			}
 		}
-		throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText());
+		throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + ",uuid:" + attachmentFse.getUUID());
 	}
 
 	/**
@@ -560,7 +634,8 @@
 	 */
 	public byte[] getFileContent(FieldSetEntity attachmentFse) throws BaseException {
 		if (attachmentFse == null || !CmnConst.PRODUCT_SYS_ATTACHMENTS.equals(attachmentFse.getTableName())) {
-			return new byte[16];
+			//杩斿洖涓�涓┖鐨勫瓧鑺傛暟缁�
+			return new byte[0];
 		}
 		try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {
 			FTPService ftpService = new FTPService();

--
Gitblit v1.9.2