diff --git a/o2server/pom.xml b/o2server/pom.xml index 7b2d13fd84507dae190d09ca2effbbde586ddba4..d482367880d2be9c803c566b05e47e1dfd26e409 100644 --- a/o2server/pom.xml +++ b/o2server/pom.xml @@ -352,6 +352,10 @@ net.lingala.zip4j zip4j + + com.github.xuwei-k + html2image + @@ -866,6 +870,11 @@ activation 1.1.1 + + com.github.xuwei-k + html2image + 0.1.0 + o2oa x_base_core_project diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java new file mode 100644 index 0000000000000000000000000000000000000000..fdc9e577990e33349a851f080d229606999b8f53 --- /dev/null +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/ActionHtmlToImage.java @@ -0,0 +1,181 @@ +package com.x.processplatform.assemble.surface.jaxrs.attachment; + +import com.google.gson.JsonElement; +import com.x.base.core.container.EntityManagerContainer; +import com.x.base.core.container.factory.EntityManagerContainerFactory; +import com.x.base.core.entity.annotation.CheckPersistType; +import com.x.base.core.project.annotation.FieldDescribe; +import com.x.base.core.project.config.Config; +import com.x.base.core.project.config.StorageMapping; +import com.x.base.core.project.exception.ExceptionAccessDenied; +import com.x.base.core.project.exception.ExceptionEntityNotExist; +import com.x.base.core.project.gson.GsonPropertyObject; +import com.x.base.core.project.http.ActionResult; +import com.x.base.core.project.http.EffectivePerson; +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.FileTools; +import com.x.general.core.entity.GeneralFile; +import com.x.processplatform.assemble.surface.Business; +import com.x.processplatform.assemble.surface.ThisApplication; +import com.x.processplatform.assemble.surface.WorkControl; +import com.x.processplatform.core.entity.content.Attachment; +import com.x.processplatform.core.entity.content.Work; +import gui.ava.html.image.generator.HtmlImageGenerator; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.tika.Tika; + +import javax.print.DocFlavor; +import java.io.File; +import java.util.Date; + +class ActionHtmlToImage extends BaseAction { + + private static Logger logger = LoggerFactory.getLogger(ActionEdit.class); + + ActionResult execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception { + try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { + ActionResult result = new ActionResult<>(); + Business business = new Business(emc); + Wi wi = this.convertToWrapIn(jsonElement, Wi.class); + Wo wo = new Wo(); + String id = saveImage(wi, effectivePerson, business); + wo.setId(id); + result.setData(wo); + return result; + } + } + + public static class Wo extends WoId { + + } + + private String saveImage(Wi wi, EffectivePerson effectivePerson, Business business) throws Exception{ + Work work = null; + EntityManagerContainer emc = business.entityManagerContainer(); + if(StringUtils.isNotBlank(wi.getWorkId())) { + /* 后面要重新保存 */ + work = emc.find(wi.getWorkId(), Work.class); + /* 判断work是否存在 */ + if (null == work) { + throw new ExceptionEntityNotExist(wi.getWorkId(), Work.class); + } + WoControl control = business.getControl(effectivePerson, work, WoControl.class); + if (BooleanUtils.isNotTrue(control.getAllowSave())) { + throw new ExceptionAccessDenied(effectivePerson, work); + } + } + + String workHtml = wi.getWorkHtml(); + if (StringUtils.isEmpty(workHtml)) { + workHtml = "无内容"; + } + if (workHtml.toLowerCase().indexOf(""; + } + String name = StringUtils.split(effectivePerson.getDistinguishedName(),"@")[0] + DateTools.compact(new Date()) + ".png"; + if (StringUtils.isNotEmpty(wi.getTitle())) { + name = wi.getTitle() + ".png"; + } + + HtmlImageGenerator imageGenerator = new HtmlImageGenerator(); + imageGenerator.loadHtml(workHtml); + File tempDir = Config.dir_local_temp(); + FileTools.forceMkdir(tempDir); + File file = new File(tempDir, name); + imageGenerator.saveAsImage(file); + byte[] bytes = FileUtils.readFileToByteArray(file); + String key = ""; + if(work!=null){ + StorageMapping mapping = ThisApplication.context().storageMappings().random(Attachment.class); + Attachment attachment = this.concreteAttachment(work, effectivePerson, wi.getSite()); + attachment.saveContent(mapping, bytes, name); + attachment.setType((new Tika()).detect(bytes, name)); + emc.beginTransaction(Attachment.class); + emc.persist(attachment, CheckPersistType.all); + emc.commit(); + + key = attachment.getId(); + }else{ + 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(); + + key = generalFile.getId(); + } + + return key; + } + + private Attachment concreteAttachment(Work work, EffectivePerson effectivePerson, String site) throws Exception { + Attachment attachment = new Attachment(); + attachment.setCompleted(false); + attachment.setPerson(effectivePerson.getDistinguishedName()); + attachment.setLastUpdatePerson(effectivePerson.getDistinguishedName()); + attachment.setSite(site); + /** 用于判断目录的值 */ + attachment.setWorkCreateTime(work.getCreateTime()); + attachment.setApplication(work.getApplication()); + attachment.setProcess(work.getProcess()); + attachment.setJob(work.getJob()); + attachment.setActivity(work.getActivity()); + attachment.setActivityName(work.getActivityName()); + attachment.setActivityToken(work.getActivityToken()); + attachment.setActivityType(work.getActivityType()); + return attachment; + } + + public static class Wi extends GsonPropertyObject { + + @FieldDescribe("*待转换html.") + private String workHtml; + @FieldDescribe("图片标题") + private String title; + @FieldDescribe("工作标识,把图片保存到工单的附件中,非必填") + private String workId; + @FieldDescribe("位置,工作标识不为空的时候必填") + private String site; + + public String getWorkHtml() { + return workHtml; + } + + public void setWorkHtml(String workHtml) { + this.workHtml = workHtml; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getWorkId() { + return workId; + } + + public void setWorkId(String workId) { + this.workId = workId; + } + + public String getSite() { + return site; + } + + public void setSite(String site) { + this.site = site; + } + } + + public static class WoControl extends WorkControl { + } +} diff --git a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java index 99f749e0732f1ff6eb71081d5fd535ae7c620c78..152681f9700e24e7f802a04d21afe02e0d5d8f61 100644 --- a/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java +++ b/o2server/x_processplatform_assemble_surface/src/main/java/com/x/processplatform/assemble/surface/jaxrs/attachment/AttachmentAction.java @@ -1215,4 +1215,22 @@ public class AttachmentAction extends StandardJaxrsAction { } asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); } + + @JaxrsMethodDescribe(value = "html转图片工具类,转换后如果工作不为空通过downloadWithWork接口下载,为空downloadTransfer接口下载.", action = ActionHtmlToImage.class) + @POST + @Path("html/to/image") + @Produces(HttpMediaType.APPLICATION_JSON_UTF_8) + @Consumes(MediaType.APPLICATION_JSON) + public void htmlToImage(@Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request, + JsonElement jsonElement) { + ActionResult result = new ActionResult<>(); + EffectivePerson effectivePerson = this.effectivePerson(request); + try { + result = new ActionHtmlToImage().execute(effectivePerson, jsonElement); + } catch (Exception e) { + logger.error(e, effectivePerson, request, null); + result.error(e); + } + asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result)); + } }