diff --git a/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentRenderData.java b/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentRenderData.java index 4380f066..0e10b68a 100644 --- a/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentRenderData.java +++ b/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentRenderData.java @@ -15,48 +15,25 @@ */ package com.deepoove.poi.data; -import java.io.File; -import java.io.InputStream; - -import com.deepoove.poi.util.ByteUtils; - /** - * attachment file:docx or xlsx - * + * attachment file + * * @author Sayi */ -public class AttachmentRenderData implements RenderData { +public abstract class AttachmentRenderData implements RenderData { private static final long serialVersionUID = 1L; - private byte[] attachment; private AttachmentType fileType; private PictureRenderData icon; - AttachmentRenderData() { - } - - public AttachmentRenderData(File attachmentFile) { - this(ByteUtils.getLocalByteArray(attachmentFile)); - } - - public AttachmentRenderData(InputStream inputStream) { - this(ByteUtils.toByteArray(inputStream)); - } - - public AttachmentRenderData(byte[] input) { - this.attachment = input; - } - - public byte[] getAttachment() { - return attachment; - } - - public void setAttachment(byte[] attachment) { - this.attachment = attachment; - } + public abstract byte[] readAttachmentData(); public AttachmentType getFileType() { + if (null != fileType) { + return fileType; + } + setFileType(detectFileType()); return fileType; } @@ -72,4 +49,16 @@ public void setIcon(PictureRenderData icon) { this.icon = icon; } + protected String getFileSrc() { + return null; + } + + protected AttachmentType detectFileType() { + AttachmentType type = AttachmentType.suggestFileType(getFileSrc()); + if (null == type) { + type = AttachmentType.suggestFileType(readAttachmentData()); + } + return type; + } + } diff --git a/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentType.java b/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentType.java index 8cff7e81..b40e17dc 100644 --- a/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentType.java +++ b/poi-tl/src/main/java/com/deepoove/poi/data/AttachmentType.java @@ -15,9 +15,80 @@ */ package com.deepoove.poi.data; +import static com.deepoove.poi.util.ByteUtils.startsWith; + /** * @author Sayi */ public enum AttachmentType { - DOCX, XLSX; + DOCX( + new String[]{"504B0304", "DOCF11E0"}, + "Word.Document.12", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + new String[]{".docx", ".doc"}, + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAABv5JREFUeF7tWwtQVFUY/i7vl5KVqfnAGHWXx6KA7ydkCSoiaIsgoYapoDA2YySBguazTGc0hEghEwUfmKhJYFmIjzE1lRFIMHzjIwSZTDNl9zTnLnu5S9I+WOAC+8/s7L3nnnvO/3/nP99/XpdBM4uTNLmXHJAYARJiBAkIJAD744QQJu+3vXM9m0M1pqkqsZcm21oCEtZYI0iIwlAxgFfV1dnqABBLkyXGyhYF4wAiFwOMSJ2hDT0XLABK9wUDJwYQKYwk1FC1raoNGC0OAOu+jNyZwFjEgIgIw4ga26qCBUAkTRGZQCaSM0TclK0qWAAcA5KJNsq1kbzXAZwt3jMvgGmnANS2I/lckAAMcnwdC6RuTepss5Z/R8uvEiwA2+J9mgyAs8V3UAsADAAIkQNoFzB4QHvvAoMdu+mNA+ZL3VXKEjwH6M1yAC/qTgYAWkMU0Oc4gHoBXwTvAYYoYAiDbWgcQN3Z1sYc1pamsLKgPxOU3a5G3q839En2DZalcxSwsTSDjZUp6L+1lRkKSu+rVNK9cwesjfTESzYWrIH0F5+cj6xjpSr5/D1EWBk+hkt7UP0EgTFZuFv5l7ABKNo9l1Ow9GYV/KMyVRQe7doLSdHeKmmphwqwfscvKmmR0wYibErdzO5EwS3MW/19sxhPK9HZA76O88FgJ0X4ePqsBt6Ru1BR/YRTPHRSfyx6d4iKIfkXbiJ8bY5K2qeRb8JnZB8uLeXARWxIP/O/AAgiCoRPdUNEwEBOUTp9pPFTKavCPeDn0U/FkPKKR5iwcDdqZHIuPWOlH1z6vsbdR206iuyTZcIHwF3cFduX+3KK0v6d+dNl7n7PGn842Xf+jyEBH3+LoqsP2HQLMxOcSpkJczNj9v7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3h20HC1BOoHwQ92U+G+74EjjOEUtnj1RJW5J0DPvzSvRpj1ZlNToKaFMbfxLT0dqMBav+Iqk25ekjb7MCoA+FlRxg2Bpr7+cDBBUG9eXampYjmDVBTRUWcr5WSYL6BLRVAiCYNUF9toQ2ZQluKKyN8vrIawDAcEKkDZ0Q0aVLGLpAMx2XJ0CFIE+L6+I1urxDQBLbMQDy5XI58hgXaYqoxui5mCGMiABiwkDEEPYDR8XmXwtIi382J5me2EkmN3VmiFxECBTgMISCUnfYpwmBaXEAGrJNMiXRXmZqKgGROzGgHiMXK/6h2CnRkwgWgBfZ5yhdZiZHd4kRiIQwxIUBHOj3hwR4Q1c8WhUADRnpHLSli0xGOGBAGPrxND0tYaMOmDYBQIPdaGqyWGai+JqcIcQF7Cf0jGK3tVaaE4B/Ac9myWiNuu1sAAAAAElFTkSuQmCC"), + XLSX( + new String[]{"504B0304", "DOCF11E0"}, + "Excel.Sheet.12", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + new String[]{".xlsx", ".xls"}, + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAABXFJREFUeF7tW3tQVFUc/u7dxQU1bPJZoBLQDIWwV0EqnRo0iLKEQEbyQVpjCgnGkDkRwWypNWpOBiNPS0GzUEHy1eAjBcEnfzQp5oROWWg6IiEUk+De25xloL3cu487tevZ7v7+2/O49/d953e/3zlnz2GgcmNowx9qCJ/wvaHxvLP8umcERBoiPdvQwTFGgeMFhmNY6CGAA+DppfEYdtpwusMZJDiFgDBD2IjuuzzHsqyeFwSOIUAZTLAE0KUJ4Aycn5HX6hme5xgGnADTqPopGU2XIUCfow/mGU3viPaOqh7ASCVg5dpSR0BgXqBu8M1hHCMInMAwHCAQoGRkvf4tWOoICMqKGD7Io4eMqt4EtleYQhwBlBoN4HK4yUawWQwEDmAediZYuXf5hPrka7Wabkf5cRco/Ca++jJ5vikLEAJ4sGcc9UKlz/XV+4LV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E6eYbl5C4MUkWzIzQ5/FR4ipJ3aGmI1hescJ1CPDQeKBkYSEmjZ8ocfrN7Zk4drHWIhi5uQJpvHLPh9jVWGmxH1UiaGlLnHhvbWub1D/uHyFLHKnb+91+tPwuv0qkigCbiuWABm4CaPtnyAGDbPWRqo8A6tKgsyPATYATzgkq/mvMmVHgjgB3BDj+qKz7E1D7OcF98dWmY8L9Z4WpOi4f7LOa9dDccZTwCsbu0gOzD1zvJ8D8Raq8MGGL6cD0QN3g+1VyZcYWGeb1qrk0pYQU7h3OzzhIJdfm7CVGVRcn7SXFzxDp6d3TwTGsCq7O2ksKaaeay9NKSHFk278BpaKbbiHMt1QAAAAASUVORK5CYII="), + ; + + private final String[] fileMagics; + private final String programId; + private final String contentType; + private final String[] extensions; + private final String icon; + + AttachmentType(String[] fileMagics, String programId, String contentType, String[] extensions, String icon) { + this.fileMagics = fileMagics; + this.programId = programId; + this.contentType = contentType; + this.extensions = extensions; + this.icon = icon; + } + + public String programId() { + return programId; + } + + public String contentType() { + return contentType; + } + + public String[] extensions() { + return extensions; + } + + public String ext() { + return extensions[0]; + } + + public String icon() { + return icon; + } + + public static AttachmentType suggestFileType(byte[] bytes) { + for (AttachmentType type : values()) { + for (String magic : type.fileMagics) { + if (startsWith(bytes, magic.getBytes())) { + return type; + } + } + } + return null; + } + + public static AttachmentType suggestFileType(String fileLocation) { + if (null == fileLocation) return null; + for (AttachmentType type : values()) { + for (String extension : type.extensions) { + if (fileLocation.endsWith(extension)) { + return type; + } + } + } + return null; + } } diff --git a/poi-tl/src/main/java/com/deepoove/poi/data/Attachments.java b/poi-tl/src/main/java/com/deepoove/poi/data/Attachments.java index a8487940..17d74f3a 100644 --- a/poi-tl/src/main/java/com/deepoove/poi/data/Attachments.java +++ b/poi-tl/src/main/java/com/deepoove/poi/data/Attachments.java @@ -15,7 +15,6 @@ */ package com.deepoove.poi.data; -import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -29,7 +28,7 @@ /** * Factory method to create {@link AttachmentRenderData} - * + * * @author Sayi * */ @@ -37,8 +36,28 @@ public class Attachments { private Attachments() { } - public static AttachmentBuilder ofLocal(String src, AttachmentType fileType) { - return ofBytes(ByteUtils.getLocalByteArray(new File(src)), fileType); + public static AttachmentBuilder of(String src) { + if (src.startsWith("http")) { + return Attachments.ofUrl(src); + } else { + return Attachments.ofLocal(src); + } + } + + public static AttachmentBuilder ofLocal(String path) { + return ofLocal(path, null); + } + + public static AttachmentBuilder ofLocal(String path, AttachmentType fileType) { + return new AttachmentBuilder(new FileAttachmentRenderData(path, fileType)); + } + + public static AttachmentBuilder ofUrl(String url) { + return ofUrl(url, null); + } + + public static AttachmentBuilder ofUrl(String url, AttachmentType fileType) { + return new AttachmentBuilder(new UrlAttachmentRenderData(url, fileType)); } public static AttachmentBuilder ofWord(XWPFDocument src) { @@ -69,8 +88,16 @@ public static AttachmentBuilder ofStream(InputStream inputStream, AttachmentType return ofBytes(ByteUtils.toByteArray(inputStream), fileType); } + public static AttachmentBuilder ofStream(InputStream inputStream) { + return ofBytes(ByteUtils.toByteArray(inputStream)); + } + public static AttachmentBuilder ofBytes(byte[] bytes, AttachmentType fileType) { - return new AttachmentBuilder(bytes, fileType); + return new AttachmentBuilder(new ByteArrayAttachmentRenderData(bytes, fileType)); + } + + public static AttachmentBuilder ofBytes(byte[] bytes) { + return ofBytes(bytes, null); } /** @@ -81,9 +108,8 @@ public static class AttachmentBuilder implements RenderDataBuilder { - private static final String EXCEL_SHEET_12 = "Excel.Sheet.12"; - private static final String WORD_DOCUMENT_12 = "Word.Document.12"; private static final String SHAPE_TYPE_ID = "_x0000_t79"; private static final String SHAPE_TYPE_XML = "\n" @@ -64,18 +66,13 @@ public class AttachmentRenderPolicy extends AbstractRenderPolicy\n" + " \n" + " \n"; - - private static final String docxIcon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAABv5JREFUeF7tWwtQVFUY/i7vl5KVqfnAGH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GVRcn7SXFzxDp6d3TwTGsCq7O2ksKaaeay9NKSHFk278BpaKbbiHMt1QAAAAASUVORK5CYII="; - private static final String WORD_CONTENTTYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; - private static final String EXCEL_CONTENTTYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; private static boolean haveShapeType; @Override protected boolean validate(AttachmentRenderData data) { - return null != data && null != data.getAttachment() && null != data.getFileType(); + return null != data && null != data.readAttachmentData() && null != data.getFileType(); } @Override @@ -101,25 +98,11 @@ public void doRender(RenderContext context) throws Excepti AttachmentRenderData data = context.getData(); AttachmentType fileType = data.getFileType(); - byte[] attachment = data.getAttachment(); - - String contentType = ""; - String part = ""; - String progId = ""; - if (fileType == AttachmentType.DOCX) { - progId = WORD_DOCUMENT_12; - contentType = WORD_CONTENTTYPE; - part = "/word/embeddings/word" + uuidRandom + ".docx"; - } else { - progId = EXCEL_SHEET_12; - contentType = EXCEL_CONTENTTYPE; - part = "/word/embeddings/excel" + uuidRandom + ".xlsx"; - } + byte[] attachment = data.readAttachmentData(); + PictureRenderData icon = data.getIcon(); if (null == icon) { - icon = Pictures.ofBase64(fileType == AttachmentType.DOCX ? docxIcon : xlsxIcon, PictureType.PNG) - .size(64, 64) - .create(); + icon = Pictures.ofBase64(fileType.icon(), PictureType.PNG).size(64, 64).create(); } byte[] image = icon.readPictureData(); PictureType pictureType = icon.getPictureType(); @@ -133,23 +116,24 @@ public void doRender(RenderContext context) throws Excepti double heightPt = Units.pixelToPoints(style.getHeight()); String imageRId = doc.addPictureData(image, pictureType.type()); - //String embeddId = doc.addEmbeddData(attachment, fileType.ordinal()); - String embeddId = doc.addEmbeddData(attachment, contentType, part); + // String embeddId = doc.addEmbeddData(attachment, fileType.ordinal()); + String embeddId = doc.addEmbeddData(attachment, fileType.contentType(), + "/word/embeddings/" + uuidRandom + fileType.ext()); String wObjectXml = "\n" + shapeTypeXml - + " \n" + " \n" - + " \n" - + " \n" + + " \n" + " \\s\n" - + " " + + " " + " "; Document document = DocumentHelper.readDocument(new InputSource(new StringReader(wObjectXml)));