提交 e168ff43 编写于 作者: O o2sword 提交者: o2null

优化附件上传下载,临时附件存储从缓存改到数据库及附件中,处理集群环境下缓存无法同步问题


(cherry picked from commit c29df193edb5ad55655f1e150b168be48512aa71)
上级 d680daab
......@@ -6,7 +6,7 @@ import com.x.base.core.project.annotation.ModuleCategory;
import com.x.base.core.project.annotation.ModuleType;
@Module(type = ModuleType.ASSEMBLE, category = ModuleCategory.OFFICIAL, name = "公共模块", packageName = "com.x.general.assemble.control", containerEntities = {
"com.x.general.core.entity.area.District" }, storageTypes = {
StorageType.file }, storeJars = { "x_general_core_entity" })
"com.x.general.core.entity.area.District", "com.x.general.core.entity.file.GeneralFile" }, storageTypes = {
StorageType.file, StorageType.general }, storeJars = { "x_general_core_entity" })
public class x_general_assemble_control extends Deployable {
}
......@@ -30,7 +30,8 @@ import com.x.base.core.project.annotation.ModuleType;
"com.x.processplatform.core.entity.element.Split", "com.x.processplatform.core.entity.element.QueryView",
"com.x.processplatform.core.entity.element.QueryStat", "com.x.processplatform.core.entity.element.Mapping",
"com.x.query.core.entity.Item", "com.x.cms.core.entity.element.Script",
"com.x.portal.core.entity.Script" }, storageTypes = { StorageType.processPlatform, StorageType.general}, storeJars = {
"com.x.portal.core.entity.Script", "com.x.general.core.entity.file.GeneralFile" },
storageTypes = { StorageType.processPlatform, StorageType.general}, storeJars = {
"x_organization_core_entity", "x_organization_core_express", "x_processplatform_core_entity",
"x_processplatform_core_express", "x_query_core_entity", "x_cms_core_entity", "x_portal_core_entity",
"x_general_core_entity" })
......
......@@ -4,6 +4,7 @@ import com.x.base.core.project.Context;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.general.assemble.control.schedule.Clean;
public class ThisApplication {
......@@ -21,6 +22,8 @@ public class ThisApplication {
try {
CacheManager.init(context.clazz().getSimpleName());
LoggerFactory.setLevel(Config.logLevel().x_general_assemble_control());
context().schedule(Clean.class, "0 0 2 * * ?");
} catch (Exception e) {
e.printStackTrace();
}
......
package com.x.general.assemble.control.schedule;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.schedule.AbstractJob;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.ListTools;
import com.x.base.core.project.utils.time.TimeStamp;
import com.x.general.assemble.control.ThisApplication;
import com.x.general.core.entity.file.GeneralFile;
import com.x.general.core.entity.file.GeneralFile_;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;
public class Clean extends AbstractJob {
private static Logger logger = LoggerFactory.getLogger(Clean.class);
@Override
public void schedule(JobExecutionContext jobExecutionContext) throws Exception {
try{
logger.print("开始定时清理过期的缓存附件!");
TimeStamp stamp = new TimeStamp();
Long instantCount = this.clearGeneralFile();
logger.print("清理过期的缓存附件:{} 条, 耗时: {}.", instantCount,
stamp.consumingMilliseconds());
} catch (Exception e) {
logger.error(e);
throw new JobExecutionException(e);
}
}
private Long clearGeneralFile() throws Exception {
List<GeneralFile> os = null;
Long count = 0L;
do {
os = this.listInstant();
if (!os.isEmpty()) {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
emc.beginTransaction(GeneralFile.class);
for (GeneralFile o : os) {
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
o.getStorage());
o.deleteContent(gfMapping);
emc.remove(o);
}
emc.commit();
count += os.size();
}
}
} while (ListTools.isNotEmpty(os));
return count;
}
private List<GeneralFile> listInstant() throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
EntityManager em = emc.get(GeneralFile.class);
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<GeneralFile> cq = cb.createQuery(GeneralFile.class);
Root<GeneralFile> root = cq.from(GeneralFile.class);
Date limit = DateTools.floorDate(new Date(), 0);
Predicate p = cb.lessThan(root.get(GeneralFile_.createTime), limit);
return em.createQuery(cq.select(root).where(p)).setMaxResults(100).getResultList();
}
}
}
\ No newline at end of file
......@@ -7,16 +7,15 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
......@@ -89,12 +88,16 @@ class ActionBatchDownloadWithWorkOrWorkCompleted extends BaseAction {
Map<String, byte[]> map = new HashMap<>();
if (StringUtils.isNotEmpty(flag)) {
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
CacheResultObject ro = (CacheResultObject) optional.get();
map.put(ro.getName(), ro.getBytes());
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
map.put(generalFile.getName(), generalFile.readContent(gfMapping));
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
}
}
......
......@@ -7,16 +7,15 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
......@@ -89,12 +88,16 @@ class ActionBatchDownloadWithWorkOrWorkCompletedStream extends BaseAction {
Map<String, byte[]> map = new HashMap<>();
if (StringUtils.isNotEmpty(flag)) {
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
CacheResultObject ro = (CacheResultObject) optional.get();
map.put(ro.getName(), ro.getBytes());
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
map.put(generalFile.getName(), generalFile.readContent(gfMapping));
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
}
}
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import java.util.Optional;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoFile;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
class ActionDownloadTransfer extends BaseAction {
......@@ -21,13 +19,18 @@ class ActionDownloadTransfer extends BaseAction {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<Wo> result = new ActionResult<>();
Wo wo = null;
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
CacheResultObject ro = (CacheResultObject) optional.get();
wo = new Wo(ro.getBytes(), this.contentType(false, ro.getName()),
this.contentDisposition(false, ro.getName()));
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
wo = new Wo(generalFile.readContent(gfMapping), this.contentType(false, generalFile.getName()),
this.contentDisposition(false, generalFile.getName()));
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
}
result.setData(wo);
return result;
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import java.util.Optional;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.config.StorageMapping;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoFile;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.assemble.surface.ThisApplication;
import com.x.processplatform.core.entity.content.Work;
import com.x.processplatform.core.entity.content.WorkCompleted;
......@@ -41,13 +39,18 @@ class ActionDownloadWorkInfo extends BaseAction {
}
}
Wo wo = null;
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
CacheResultObject ro = (CacheResultObject) optional.get();
wo = new Wo(ro.getBytes(), this.contentType(false, ro.getName()),
this.contentDisposition(false, ro.getName()));
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
wo = new Wo(generalFile.readContent(gfMapping), this.contentType(false, generalFile.getName()),
this.contentDisposition(false, generalFile.getName()));
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
}
result.setData(wo);
return result;
......
......@@ -2,6 +2,11 @@ package com.x.processplatform.assemble.surface.jaxrs.attachment;
import java.io.ByteArrayOutputStream;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.lang3.StringUtils;
import com.google.gson.JsonElement;
......@@ -14,9 +19,6 @@ import com.itextpdf.kernel.pdf.PdfWriter;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.annotation.FieldDescribe;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.gson.GsonPropertyObject;
import com.x.base.core.project.http.ActionResult;
......@@ -25,7 +27,6 @@ import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.StringTools;
class ActionHtmlToPdf extends BaseAction {
......@@ -34,9 +35,10 @@ class ActionHtmlToPdf extends BaseAction {
ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
ActionResult<Wo> result = new ActionResult<>();
Business business = new Business(emc);
Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
Wo wo = new Wo();
String id = savePdf(wi, effectivePerson.getDistinguishedName());
String id = savePdf(wi, effectivePerson.getDistinguishedName(), business);
wo.setId(id);
result.setData(wo);
return result;
......@@ -47,11 +49,8 @@ class ActionHtmlToPdf extends BaseAction {
}
private String savePdf(Wi wi, String person) {
private String savePdf(Wi wi, String person, Business business) {
try {
CacheResultObject ro = new CacheResultObject();
ro.setPerson(person);
String workHtml = wi.getWorkHtml();
if (StringUtils.isEmpty(workHtml)) {
workHtml = "无内容";
......@@ -59,10 +58,11 @@ class ActionHtmlToPdf extends BaseAction {
if (workHtml.toLowerCase().indexOf("<html") == -1) {
workHtml = "<html><head></head><body>" + workHtml + "</body></html>";
}
String title = person + DateTools.now() + ".pdf";
String name = person + DateTools.now() + ".pdf";
if (StringUtils.isNotEmpty(wi.getTitle())) {
title = wi.getTitle() + ".pdf";
name = wi.getTitle() + ".pdf";
}
byte[] bytes;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
ConverterProperties props = new ConverterProperties();
DefaultFontProvider dfp = new DefaultFontProvider(false, false, false);
......@@ -77,13 +77,17 @@ class ActionHtmlToPdf extends BaseAction {
}
pdf.setDefaultPageSize(new PageSize(width, PageSize.A4.getHeight()));
HtmlConverter.convertToPdf(workHtml, pdf, props);
ro.setBytes(out.toByteArray());
ro.setName(title + ".pdf");
bytes = out.toByteArray();
}
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
String key = StringTools.uniqueToken();
CacheKey cacheKey = new CacheKey(key);
CacheManager.put(cacheCategory, cacheKey, ro);
StorageMapping gfMapping = ThisApplication.context().storageMappings().random(GeneralFile.class);
GeneralFile generalFile = new GeneralFile(gfMapping.getName(), name, person);
generalFile.saveContent(gfMapping, bytes, name);
EntityManagerContainer emc = business.entityManagerContainer();
emc.beginTransaction(GeneralFile.class);
emc.persist(generalFile, CheckPersistType.all);
emc.commit();
String key = generalFile.getId();
return key;
} catch (Exception e) {
logger.warn("写work信息异常" + e.getMessage());
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.io.FilenameUtils;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.annotation.ActionLogger;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.exception.ExceptionEntityNotExist;
import com.x.base.core.project.http.ActionResult;
......@@ -16,7 +17,6 @@ import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.tools.DocumentTools;
import com.x.base.core.project.tools.StringTools;
import com.x.processplatform.assemble.surface.Business;
import com.x.processplatform.core.entity.content.Attachment;
......@@ -39,17 +39,20 @@ class ActionPreviewImage extends BaseAction {
throw new ExceptionAccessDenied(effectivePerson);
}
}
String key = "";
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
byte[] bytes = DocumentTools.toImage(attachment.getName(), attachment.getBytes(), "", page);
String name = FilenameUtils.getBaseName(attachment.getName()) + ".png";
StorageMapping gfMapping = ThisApplication.context().storageMappings().random(GeneralFile.class);
GeneralFile generalFile = new GeneralFile(gfMapping.getName(), name, effectivePerson.getDistinguishedName());
generalFile.saveContent(gfMapping, bytes, name);
emc.beginTransaction(GeneralFile.class);
emc.persist(generalFile, CheckPersistType.all);
emc.commit();
byte[] bytes = DocumentTools.toImage(attachment.getName(), attachment.getBytes(), "", page);
PreviewImageResultObject obj = new PreviewImageResultObject();
obj.setPerson(effectivePerson.getDistinguishedName());
obj.setBytes(bytes);
obj.setName(FilenameUtils.getBaseName(attachment.getName()) + ".png");
key = generalFile.getId();
}
String key = StringTools.uniqueToken();
CacheCategory cacheCategory = new CacheCategory(PreviewImageResultObject.class);
CacheKey cacheKey = new CacheKey(key);
CacheManager.put(cacheCategory, cacheKey, obj);
Wo wo = new Wo();
wo.setId(key);
result.setData(wo);
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import java.util.Optional;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
......@@ -20,20 +20,27 @@ class ActionPreviewImageResult extends BaseAction {
ActionResult<Wo> execute(EffectivePerson effectivePerson, String flag) throws Exception {
ActionResult<Wo> result = new ActionResult<>();
CacheCategory cacheCategory = new CacheCategory(PreviewImageResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
Wo wo = null;
if (optional.isPresent()) {
PreviewImageResultObject obj = (PreviewImageResultObject) optional.get();
if (!StringUtils.equals(effectivePerson.getDistinguishedName(), obj.getPerson())) {
throw new ExceptionAccessDenied(effectivePerson);
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
if (!StringUtils.equals(effectivePerson.getDistinguishedName(), generalFile.getPerson())) {
throw new ExceptionAccessDenied(effectivePerson);
}
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
wo = new Wo(generalFile.readContent(gfMapping), this.contentType(true, generalFile.getName()),
this.contentDisposition(true, generalFile.getName()));
result.setData(wo);
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
} else {
throw new ExceptionPreviewImageResultObject(flag);
}
wo = new Wo(obj.getBytes(), this.contentType(true, obj.getName()),
this.contentDisposition(true, obj.getName()));
result.setData(wo);
} else {
throw new ExceptionPreviewImageResultObject(flag);
}
result.setData(wo);
return result;
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.io.FilenameUtils;
import com.x.base.core.container.EntityManagerContainer;
......@@ -38,18 +42,19 @@ class ActionPreviewPdf extends BaseAction {
throw new ExceptionAccessDenied(effectivePerson);
}
}
String key = "";
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
byte[] bytes = DocumentTools.toPdf(attachment.getName(), attachment.getBytes(), "");
String name = FilenameUtils.getBaseName(attachment.getName()) + ".pdf";
StorageMapping gfMapping = ThisApplication.context().storageMappings().random(GeneralFile.class);
GeneralFile generalFile = new GeneralFile(gfMapping.getName(), name, effectivePerson.getDistinguishedName());
generalFile.saveContent(gfMapping, bytes, name);
emc.beginTransaction(GeneralFile.class);
emc.persist(generalFile, CheckPersistType.all);
emc.commit();
byte[] bytes = DocumentTools.toPdf(attachment.getName(), attachment.getBytes(), "");
PreviewPdfResultObject obj = new PreviewPdfResultObject();
obj.setPerson(effectivePerson.getDistinguishedName());
obj.setBytes(bytes);
obj.setName(FilenameUtils.getBaseName(attachment.getName()) + ".pdf");
String key = StringTools.uniqueToken();
CacheCategory cacheCategory = new CacheCategory(PreviewPdfResultObject.class);
CacheKey cacheKey = new CacheKey(key);
CacheManager.put(cacheCategory, cacheKey, obj);
key = generalFile.getId();
}
Wo wo = new Wo();
wo.setId(key);
result.setData(wo);
......
package com.x.processplatform.assemble.surface.jaxrs.attachment;
import java.util.Optional;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.commons.lang3.StringUtils;
import com.x.base.core.project.annotation.ActionLogger;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.exception.ExceptionAccessDenied;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
......@@ -23,19 +23,26 @@ class ActionPreviewPdfResult extends BaseAction {
ActionResult<Wo> execute(EffectivePerson effectivePerson, String flag) throws Exception {
ActionResult<Wo> result = new ActionResult<>();
Wo wo = null;
CacheCategory cacheCategory = new CacheCategory(PreviewPdfResultObject.class);
CacheKey cacheKey = new CacheKey(flag);
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
PreviewPdfResultObject obj = (PreviewPdfResultObject) optional.get();
if (!StringUtils.equals(effectivePerson.getDistinguishedName(), obj.getPerson())) {
throw new ExceptionAccessDenied(effectivePerson);
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
GeneralFile generalFile = emc.find(flag, GeneralFile.class);
if(generalFile!=null){
if (!StringUtils.equals(effectivePerson.getDistinguishedName(), generalFile.getPerson())) {
throw new ExceptionAccessDenied(effectivePerson);
}
StorageMapping gfMapping = ThisApplication.context().storageMappings().get(GeneralFile.class,
generalFile.getStorage());
wo = new Wo(generalFile.readContent(gfMapping), this.contentType(true, generalFile.getName()),
this.contentDisposition(true, generalFile.getName()));
result.setData(wo);
generalFile.deleteContent(gfMapping);
emc.beginTransaction(GeneralFile.class);
emc.delete(GeneralFile.class, generalFile.getId());
emc.commit();
} else {
throw new ExceptionPreviewImageResultObject(flag);
}
wo = new Wo(obj.getBytes(), this.contentType(true, obj.getName()),
this.contentDisposition(true, obj.getName()));
result.setData(wo);
} else {
throw new ExceptionPreviewPdfResultObject(flag);
}
result.setData(wo);
return result;
......
......@@ -7,6 +7,10 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.x.base.core.entity.annotation.CheckPersistType;
import com.x.base.core.project.config.StorageMapping;
import com.x.general.core.entity.file.GeneralFile;
import com.x.processplatform.assemble.surface.ThisApplication;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.google.gson.JsonElement;
......@@ -69,7 +73,7 @@ class ActionUploadWorkInfo extends BaseAction {
workHtml = "<html><head></head><body>" + workHtml + "</body></html>";
}
String id = saveHtml(workId, flag, workHtml, effectivePerson.getDistinguishedName(), title,
wi.getPageWidth());
wi.getPageWidth(), business);
Wo wo = new Wo();
wo.setId(id);
result.setData(wo);
......@@ -80,18 +84,19 @@ class ActionUploadWorkInfo extends BaseAction {
public static class Wo extends WoId {
}
private String saveHtml(String workId, String flag, String workHtml, String person, String title, Float pageWidth) {
private String saveHtml(String workId, String flag, String workHtml, String person, String title,
Float pageWidth, Business business) {
try {
CacheResultObject ro = new CacheResultObject();
ro.setPerson(person);
String name = "";
byte[] bytes;
if ("word".equals(flag)) {
try (POIFSFileSystem fs = new POIFSFileSystem();
InputStream is = new ByteArrayInputStream(workHtml.getBytes("UTF-8"));
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
fs.createDocument(is, "WordDocument");
fs.writeFilesystem(out);
ro.setBytes(out.toByteArray());
ro.setName(title + "-处理单.doc");
bytes = out.toByteArray();
name = title + "-处理单.doc";
}
} else if ("pdf".equals(flag)) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
......@@ -108,19 +113,22 @@ class ActionUploadWorkInfo extends BaseAction {
}
pdf.setDefaultPageSize(new PageSize(width, PageSize.A4.getHeight()));
HtmlConverter.convertToPdf(workHtml, pdf, props);
ro.setBytes(out.toByteArray());
ro.setName(title + "-处理单.pdf");
bytes = out.toByteArray();
name = title + "-处理单.pdf";
}
} else {
ro.setBytes(workHtml.getBytes(DefaultCharset.charset));
ro.setName(title + "-处理单.html");
bytes = workHtml.getBytes(DefaultCharset.charset);
name = title + "-处理单.html";
}
CacheCategory cacheCategory = new CacheCategory(CacheResultObject.class);
String key = StringTools.uniqueToken();
CacheKey cacheKey = new CacheKey(key);
// String cacheKey = ApplicationCache.concreteCacheKey(workId,
// UUID.randomUUID().toString());
CacheManager.put(cacheCategory, cacheKey, ro);
StorageMapping gfMapping = ThisApplication.context().storageMappings().random(GeneralFile.class);
GeneralFile generalFile = new GeneralFile(gfMapping.getName(), name, person);
generalFile.saveContent(gfMapping, bytes, name);
EntityManagerContainer emc = business.entityManagerContainer();
emc.beginTransaction(GeneralFile.class);
emc.persist(generalFile, CheckPersistType.all);
emc.commit();
String key = generalFile.getId();
return key;
} catch (Exception e) {
logger.warn("写work信息异常" + e.getMessage());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册