Просмотр исходного кода

流程上传附件控制附件类型和附件大小

o2sword 5 лет назад
Родитель
Сommit
97b3e6284b
10 измененных файлов с 158 добавлено и 3 удалено
  1. 10 1
      o2server/configSample/processPlatform.json
  2. 57 1
      o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/ProcessPlatform.java
  3. 2 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUpdate.java
  4. 2 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUpdateCallback.java
  5. 2 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadCallback.java
  6. 3 1
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWithWork.java
  7. 3 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWithWorkCompleted.java
  8. 45 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/BaseAction.java
  9. 17 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ExceptionAttachmentInvalid.java
  10. 17 0
      o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ExceptionAttachmentInvalidCallback.java

+ 10 - 1
o2server/configSample/processPlatform.json

@@ -59,6 +59,14 @@
     "###thresholdMinutes": "当工作滞留设定时间后,将尝试触发工作流转,可以自动处理由于人员变动的引起的工作滞留,默认24*60分钟.###"
   },
   "processingSignalPersistEnable": false,
+  "attachmentConfig": {
+    "fileSize": 0.0,
+    "fileTypeIncludes": [],
+    "fileTypeExcludes": [],
+    "###fileSize": "附件大小限制(单位M,默认不限制).###",
+    "###fileTypeIncludes": "只允许上传的文件后缀###",
+    "###fileTypeExcludes": "不允许上传的文件后缀###"
+  },
   "###maintenanceIdentity": "维护身份,当工作发生意外错误,无法找到对应的处理人情况下,先尝试将工作分配给创建身份,如果创建身份也不可获取,那么分配给指定人员,默认情况下这个值为空.###",
   "###formVersionCount": "表单历史版本保留数量,0为不保留.###",
   "###processVersionCount": "流程历史版本保留数量,0为不保留.###",
@@ -96,5 +104,6 @@
     "###count": "提醒数量限制.###"
   },
   "###extensionEvents": "事件扩充.###",
-  "###processingSignalPersistEnable": "是否保存工作处理信号内容,默认false.###"
+  "###processingSignalPersistEnable": "是否保存工作处理信号内容,默认false.###",
+  "###attachmentConfig": "流程附件上传限制大小或者类型.###"
 }

+ 57 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/ProcessPlatform.java

@@ -75,6 +75,7 @@ public class ProcessPlatform extends ConfigObject {
 		this.deleteDraft = new DeleteDraft();
 		this.passExpired = new PassExpired();
 		this.processingSignalPersistEnable = DEFAULT_PROCESSINGSIGNALPERSISTENABLE;
+		this.attachmentConfig = new AttachmentConfig();
 	}
 
 	public Integer getExecutorCount() {
@@ -170,6 +171,10 @@ public class ProcessPlatform extends ConfigObject {
 	@FieldDescribe("是否保存工作处理信号内容,默认false.")
 	private Boolean processingSignalPersistEnable;
 
+	@FieldDescribe("流程附件上传限制大小或者类型.")
+	private AttachmentConfig attachmentConfig;
+
+
 	public Boolean getProcessingSignalPersistEnable() {
 		if (processingSignalPersistEnable == null) {
 			this.processingSignalPersistEnable = DEFAULT_PROCESSINGSIGNALPERSISTENABLE;
@@ -184,6 +189,14 @@ public class ProcessPlatform extends ConfigObject {
 		return extensionEvents;
 	}
 
+	public AttachmentConfig getAttachmentConfig() {
+		return this.attachmentConfig == null ? new AttachmentConfig() : attachmentConfig;
+	}
+
+	public void setAttachmentConfig(AttachmentConfig attachmentConfig) {
+		this.attachmentConfig = attachmentConfig;
+	}
+
 	public Urge getUrge() {
 		return this.urge == null ? new Urge() : this.urge;
 	}
@@ -593,6 +606,49 @@ public class ProcessPlatform extends ConfigObject {
 
 	}
 
+	public static class AttachmentConfig extends ConfigObject {
+
+		public static AttachmentConfig defaultInstance() {
+			AttachmentConfig o = new AttachmentConfig();
+			return o;
+		}
+
+		public static final Integer DEFAULT_FILE_SIZE = 0;
+
+		@FieldDescribe("附件大小限制(单位M,默认不限制).")
+		private Integer fileSize = DEFAULT_FILE_SIZE;
+
+		@FieldDescribe("只允许上传的文件后缀")
+		private List<String> fileTypeIncludes = new ArrayList<>();
+
+		@FieldDescribe("不允许上传的文件后缀")
+		private List<String> fileTypeExcludes = new ArrayList<>();
+
+		public Integer getFileSize() {
+			return fileSize;
+		}
+
+		public void setFileSize(Integer fileSize) {
+			this.fileSize = fileSize;
+		}
+
+		public List<String> getFileTypeIncludes() {
+			return fileTypeIncludes;
+		}
+
+		public void setFileTypeIncludes(List<String> fileTypeIncludes) {
+			this.fileTypeIncludes = fileTypeIncludes;
+		}
+
+		public List<String> getFileTypeExcludes() {
+			return fileTypeExcludes;
+		}
+
+		public void setFileTypeExcludes(List<String> fileTypeExcludes) {
+			this.fileTypeExcludes = fileTypeExcludes;
+		}
+	}
+
 	public static class ExtensionEvents {
 
 		@FieldDescribe("工作附件上传.")
@@ -841,4 +897,4 @@ public class ProcessPlatform extends ConfigObject {
 
 	}
 
-}
+}

+ 2 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUpdate.java

@@ -72,6 +72,8 @@ class ActionUpdate extends BaseAction {
 				throw new ExceptionAccessDenied(effectivePerson, attachment);
 			}
 
+			this.verifyConstraint(bytes.length, fileName, null);
+
 			StorageMapping mapping = ThisApplication.context().storageMappings().get(Attachment.class,
 					attachment.getStorage());
 			emc.beginTransaction(Attachment.class);

+ 2 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUpdateCallback.java

@@ -51,6 +51,8 @@ class ActionUpdateCallback extends BaseAction {
 			// attachment.getExtension())) {
 			// throw new ExceptionExtensionNotMatch(fileName, attachment.getExtension());
 			// }
+			this.verifyConstraint(bytes.length, fileName, null);
+
 			/** 统计待办数量判断用户是否可以上传附件 */
 			WoControl control = business.getControl(effectivePerson, work, WoControl.class);
 			if (BooleanUtils.isNotTrue(control.getAllowProcessing())) {

+ 2 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadCallback.java

@@ -48,6 +48,8 @@ class ActionUploadCallback extends BaseAction {
 			// if (StringUtils.isEmpty(fileName)) {
 			// throw new ExceptionEmptyExtension(fileName);
 			// }
+			this.verifyConstraint(bytes.length, fileName, callback);
+
 			StorageMapping mapping = ThisApplication.context().storageMappings().random(Attachment.class);
 			Attachment attachment = this.concreteAttachment(work, effectivePerson, site);
 			attachment.saveContent(mapping, bytes, fileName);

+ 3 - 1
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWithWork.java

@@ -62,6 +62,8 @@ class ActionUploadWithWork extends BaseAction {
 			/* 调整可能的附件名称 */
 			fileName = this.adjustFileName(business, work.getJob(), fileName);
 
+			this.verifyConstraint(bytes.length, fileName, null);
+
 			StorageMapping mapping = ThisApplication.context().storageMappings().random(Attachment.class);
 			Attachment attachment = this.concreteAttachment(work, effectivePerson, site);
 			attachment.saveContent(mapping, bytes, fileName);
@@ -73,7 +75,7 @@ class ActionUploadWithWork extends BaseAction {
 				logger.debug("filename:{}, file type:{}, text:{}.", attachment.getName(), attachment.getType(),
 						attachment.getText());
 			}
-			
+
 			emc.beginTransaction(Attachment.class);
 			emc.persist(attachment, CheckPersistType.all);
 			emc.commit();

+ 3 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionUploadWithWorkCompleted.java

@@ -62,6 +62,9 @@ class ActionUploadWithWorkCompleted extends BaseAction {
 			}
 			/* 调整可能的附件名称 */
 			fileName = this.adjustFileName(business, workCompleted.getJob(), fileName);
+
+			this.verifyConstraint(bytes.length, fileName, null);
+
 			/* 天印扩展 */
 			if (StringUtils.isNotEmpty(extraParam)) {
 				WiExtraParam wiExtraParam = gson.fromJson(extraParam, WiExtraParam.class);

+ 45 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/BaseAction.java

@@ -2,6 +2,7 @@ package com.x.processplatform.assemble.surface.jaxrs.attachment;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 
 import javax.persistence.EntityManager;
@@ -10,6 +11,10 @@ import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 
+import com.x.base.core.project.cache.Cache;
+import com.x.base.core.project.cache.CacheManager;
+import com.x.base.core.project.config.Config;
+import com.x.base.core.project.config.ProcessPlatform;
 import org.apache.commons.collections4.ListUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -243,4 +248,44 @@ abstract class BaseAction extends StandardJaxrsAction {
 			return value;
 		});
 	}
+
+	/**
+	 * 判断附件是否符合大小、文件类型的约束
+	 * @param size
+	 * @param fileName
+	 * @param callback
+	 * @throws Exception
+	 */
+	protected void verifyConstraint(long size, String fileName, String callback) throws Exception{
+		ProcessPlatform.AttachmentConfig attConfig = Config.processPlatform().getAttachmentConfig();
+		if(attConfig.getFileSize()!=null && attConfig.getFileSize()>0) {
+			size = size / (1024 * 1024);
+			if (size > attConfig.getFileSize()) {
+				if (StringUtils.isNotEmpty(callback)){
+					throw new ExceptionAttachmentInvalidCallback(callback, fileName, attConfig.getFileSize());
+				}else{
+					throw new ExceptionAttachmentInvalid(fileName, attConfig.getFileSize());
+				}
+			}
+		}
+		String fileType = FilenameUtils.getExtension(fileName).toLowerCase();
+		if(attConfig.getFileTypeIncludes()!=null && !attConfig.getFileTypeIncludes().isEmpty()){
+			if(!ListTools.contains(attConfig.getFileTypeIncludes(), fileType)){
+				if (StringUtils.isNotEmpty(callback)){
+					throw new ExceptionAttachmentInvalidCallback(callback, fileName);
+				}else{
+					throw new ExceptionAttachmentInvalid(fileName);
+				}
+			}
+		}
+		if(attConfig.getFileTypeExcludes()!=null && !attConfig.getFileTypeExcludes().isEmpty()){
+			if(ListTools.contains(attConfig.getFileTypeExcludes(), fileType)){
+				if (StringUtils.isNotEmpty(callback)){
+					throw new ExceptionAttachmentInvalidCallback(callback, fileName);
+				}else{
+					throw new ExceptionAttachmentInvalid(fileName);
+				}
+			}
+		}
+	}
 }

+ 17 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ExceptionAttachmentInvalid.java

@@ -0,0 +1,17 @@
+package com.x.processplatform.assemble.surface.jaxrs.attachment;
+
+import com.x.base.core.project.exception.PromptException;
+
+class ExceptionAttachmentInvalid extends PromptException {
+
+	private static final long serialVersionUID = 3232548525722242208L;
+
+	ExceptionAttachmentInvalid(String fileName) {
+		super("附件:{}, 不符合上传类型.", fileName);
+	}
+
+	ExceptionAttachmentInvalid(String fileName, Integer fileSize) {
+		super("附件:{},附件大小超过限制{}M.", fileName, fileSize);
+	}
+
+}

+ 17 - 0
o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ExceptionAttachmentInvalidCallback.java

@@ -0,0 +1,17 @@
+package com.x.processplatform.assemble.surface.jaxrs.attachment;
+
+import com.x.base.core.project.exception.CallbackPromptException;
+
+class ExceptionAttachmentInvalidCallback extends CallbackPromptException {
+
+	private static final long serialVersionUID = 8275405268546054638L;
+
+	ExceptionAttachmentInvalidCallback(String callbackName, String fileName) {
+		super(callbackName, "附件:{}, 不符合上传类型.", fileName);
+	}
+
+	ExceptionAttachmentInvalidCallback(String callbackName, String fileName, Integer fileSize) {
+		super(callbackName, "附件:{},附件大小超过限制{}M.", fileName, fileSize);
+	}
+
+}