Skip to content

Commit

Permalink
#929 merge 1.10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayi committed Jan 31, 2023
1 parent 96a9f21 commit 8569350
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

}
73 changes: 72 additions & 1 deletion poi-tl/src/main/java/com/deepoove/poi/data/AttachmentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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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WSYL6BLRVAiCYNUF9toQ2ZQluKKyN8vrIawDAcEKkDZ0Q0aVLGLpAMx2XJ0CFIE+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+4LVsEq7KWq/76VqFgwE1RLA8GzE3llVZ90EqPUTcEeA+xNwa4BbBKnNAiey6zBEN0SU12dWx/f/nhOUhLlBL9ud99+tz8G5VvEWA9Ui6CaAtghInbZENtwECCg6WmIxFC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Pwuv0qkigCbiuWABm4CaPtnyAGDbPWRqo8A6tKgsyPATYATzgkq/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;
}
}
42 changes: 34 additions & 8 deletions poi-tl/src/main/java/com/deepoove/poi/data/Attachments.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.deepoove.poi.data;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -29,16 +28,36 @@

/**
* Factory method to create {@link AttachmentRenderData}
*
*
* @author Sayi
*
*/
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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -81,9 +108,8 @@ public static class AttachmentBuilder implements RenderDataBuilder<AttachmentRen

AttachmentRenderData data;

private AttachmentBuilder(byte[] bytes, AttachmentType fileType) {
data = new AttachmentRenderData(bytes);
data.setFileType(fileType);
private AttachmentBuilder(AttachmentRenderData data) {
this.data = data;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2014-2021 Sayi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.deepoove.poi.data;

public class ByteArrayAttachmentRenderData extends AttachmentRenderData {

private static final long serialVersionUID = 1L;

private byte[] bytes;

public ByteArrayAttachmentRenderData(byte[] bytes) {
this(bytes, null);
}

public ByteArrayAttachmentRenderData(byte[] bytes, AttachmentType fileType) {
this.bytes = bytes;
this.setFileType(fileType);
}

@Override
public byte[] readAttachmentData() {
return bytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2014-2021 Sayi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.deepoove.poi.data;

import java.io.File;

import com.deepoove.poi.util.ByteUtils;

public class FileAttachmentRenderData extends AttachmentRenderData {

private static final long serialVersionUID = 1L;

private String path;

public FileAttachmentRenderData(String path) {
this(path, null);
}

public FileAttachmentRenderData(String path, AttachmentType fileType) {
this.path = path;
this.setFileType(fileType);
}

@Override
protected String getFileSrc() {
return path;
}

@Override
public byte[] readAttachmentData() {
return ByteUtils.getLocalByteArray(new File(path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014-2021 Sayi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.deepoove.poi.data;

import com.deepoove.poi.util.ByteUtils;

public class UrlAttachmentRenderData extends AttachmentRenderData {

private static final long serialVersionUID = 1L;

private String url;

public UrlAttachmentRenderData(String url) {
this(url, null);
}

public UrlAttachmentRenderData(String url, AttachmentType fileType) {
this.url = url;
this.setFileType(fileType);
}

@Override
protected String getFileSrc() {
return url;
}

@Override
public byte[] readAttachmentData() {
return ByteUtils.getUrlByteArray(url);
}
}
Loading

0 comments on commit 8569350

Please sign in to comment.