| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.officeimporter.internal.filter; |
| 21 |
|
|
| 22 |
|
import java.net.URLDecoder; |
| 23 |
|
import java.nio.charset.Charset; |
| 24 |
|
import java.util.HashMap; |
| 25 |
|
import java.util.List; |
| 26 |
|
import java.util.Map; |
| 27 |
|
|
| 28 |
|
import javax.inject.Inject; |
| 29 |
|
import javax.inject.Named; |
| 30 |
|
import javax.inject.Singleton; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.lang3.StringUtils; |
| 33 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
| 34 |
|
import org.apache.tika.mime.MimeTypeException; |
| 35 |
|
import org.apache.tika.mime.MimeTypes; |
| 36 |
|
import org.slf4j.Logger; |
| 37 |
|
import org.w3c.dom.Attr; |
| 38 |
|
import org.w3c.dom.Comment; |
| 39 |
|
import org.w3c.dom.Document; |
| 40 |
|
import org.w3c.dom.Element; |
| 41 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
| 42 |
|
import org.xwiki.component.annotation.Component; |
| 43 |
|
import org.xwiki.model.reference.AttachmentReference; |
| 44 |
|
import org.xwiki.model.reference.DocumentReference; |
| 45 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 46 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
| 47 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
| 48 |
|
import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer; |
| 49 |
|
import org.xwiki.xml.XMLUtils; |
| 50 |
|
import org.xwiki.xml.html.filter.AbstractHTMLFilter; |
| 51 |
|
|
| 52 |
|
import com.github.ooxi.jdatauri.DataUri; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@version |
| 69 |
|
@since |
| 70 |
|
|
| 71 |
|
@Component |
| 72 |
|
@Named("officeimporter/image") |
| 73 |
|
@Singleton |
| |
|
| 93.9% |
Uncovered Elements: 4 (66) |
Complexity: 16 |
Complexity Density: 0.36 |
|
| 74 |
|
public class ImageFilter extends AbstractHTMLFilter |
| 75 |
|
{ |
| 76 |
|
private static final String UTF_8 = "UTF-8"; |
| 77 |
|
|
| 78 |
|
private static final String EMBEDDED_IMAGES = "embeddedImages"; |
| 79 |
|
|
| 80 |
|
@Inject |
| 81 |
|
private Logger logger; |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
@link |
| 85 |
|
|
| 86 |
|
@Inject |
| 87 |
|
private DocumentAccessBridge documentAccessBridge; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@Inject |
| 93 |
|
@Named("xhtmlmarker") |
| 94 |
|
private ResourceReferenceSerializer xhtmlMarkerSerializer; |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
@Inject |
| 100 |
|
@Named("currentmixed") |
| 101 |
|
private DocumentReferenceResolver<String> stringDocumentReferenceResolver; |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 103 |
71 |
@Override... |
| 104 |
|
public void filter(Document htmlDocument, Map<String, String> cleaningParams) |
| 105 |
|
{ |
| 106 |
71 |
String targetDocumentName = cleaningParams.get("targetDocument"); |
| 107 |
71 |
DocumentReference targetDocumentReference = |
| 108 |
71 |
targetDocumentName == null ? null : this.stringDocumentReferenceResolver.resolve(targetDocumentName); |
| 109 |
|
|
| 110 |
71 |
boolean attachEmbeddedImages = Boolean.valueOf(cleaningParams.get("attachEmbeddedImages")); |
| 111 |
71 |
if (attachEmbeddedImages) { |
| 112 |
2 |
htmlDocument.setUserData(EMBEDDED_IMAGES, new HashMap<String, byte[]>(), null); |
| 113 |
|
} |
| 114 |
|
|
| 115 |
71 |
List<Element> images = filterDescendants(htmlDocument.getDocumentElement(), new String[] {TAG_IMG}); |
| 116 |
71 |
for (Element image : images) { |
| 117 |
6 |
Attr source = image.getAttributeNode(ATTRIBUTE_SRC); |
| 118 |
6 |
if (source != null && targetDocumentReference != null) { |
| 119 |
5 |
filterImageSource(source, targetDocumentReference); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
6 |
image.removeAttribute(ATTRIBUTE_ALIGN); |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 3 |
Complexity Density: 0.19 |
|
| 130 |
5 |
private void filterImageSource(Attr source, DocumentReference targetDocumentReference)... |
| 131 |
|
{ |
| 132 |
5 |
String fileName = null; |
| 133 |
5 |
try { |
| 134 |
5 |
fileName = getFileName(source); |
| 135 |
|
} catch (Exception e) { |
| 136 |
0 |
this.logger.warn("Failed to extract the image file name. Root cause is [{}]", |
| 137 |
|
ExceptionUtils.getRootCauseMessage(e)); |
| 138 |
0 |
this.logger.debug("Full stacktrace is: ", e); |
| 139 |
|
} |
| 140 |
5 |
if (StringUtils.isEmpty(fileName)) { |
| 141 |
2 |
return; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
3 |
AttachmentReference attachmentReference = new AttachmentReference(fileName, targetDocumentReference); |
| 146 |
3 |
source.setValue(this.documentAccessBridge.getAttachmentURL(attachmentReference, false)); |
| 147 |
|
|
| 148 |
3 |
ResourceReference imageReference = new ResourceReference(fileName, ResourceType.ATTACHMENT); |
| 149 |
3 |
imageReference.setTyped(false); |
| 150 |
3 |
Comment beforeComment = source.getOwnerDocument().createComment( |
| 151 |
|
XMLUtils.escapeXMLComment("startimage:" + this.xhtmlMarkerSerializer.serialize(imageReference))); |
| 152 |
3 |
Comment afterComment = source.getOwnerDocument().createComment("stopimage"); |
| 153 |
3 |
Element image = source.getOwnerElement(); |
| 154 |
3 |
image.getParentNode().insertBefore(beforeComment, image); |
| 155 |
3 |
image.getParentNode().insertBefore(afterComment, image.getNextSibling()); |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 92.9% |
Uncovered Elements: 2 (28) |
Complexity: 8 |
Complexity Density: 0.44 |
|
| 158 |
5 |
private String getFileName(Attr source) throws MimeTypeException... |
| 159 |
|
{ |
| 160 |
5 |
String value = source.getValue(); |
| 161 |
5 |
String fileName = null; |
| 162 |
5 |
@SuppressWarnings("unchecked") |
| 163 |
|
Map<String, byte[]> embeddedImages = |
| 164 |
|
(Map<String, byte[]>) source.getOwnerDocument().getUserData(EMBEDDED_IMAGES); |
| 165 |
5 |
if (embeddedImages != null && value.startsWith("data:")) { |
| 166 |
|
|
| 167 |
2 |
DataUri dataURI = DataUri.parse(value, Charset.forName(UTF_8)); |
| 168 |
2 |
fileName = dataURI.getFilename(); |
| 169 |
2 |
if (StringUtils.isEmpty(fileName)) { |
| 170 |
1 |
fileName = String.valueOf(Math.abs(dataURI.hashCode())); |
| 171 |
1 |
if (!StringUtils.isEmpty(dataURI.getMime())) { |
| 172 |
1 |
String extension = MimeTypes.getDefaultMimeTypes().forName(dataURI.getMime()).getExtension(); |
| 173 |
1 |
fileName += extension; |
| 174 |
|
} |
| 175 |
|
} |
| 176 |
2 |
embeddedImages.put(fileName, dataURI.getData()); |
| 177 |
3 |
} else if (!value.contains("://")) { |
| 178 |
|
|
| 179 |
1 |
int separator = value.lastIndexOf('/'); |
| 180 |
1 |
fileName = separator < 0 ? value : value.substring(separator + 1); |
| 181 |
1 |
try { |
| 182 |
|
|
| 183 |
1 |
fileName = URLDecoder.decode(fileName, UTF_8); |
| 184 |
|
} catch (Exception e) { |
| 185 |
|
|
| 186 |
|
} |
| 187 |
|
} |
| 188 |
5 |
return fileName; |
| 189 |
|
} |
| 190 |
|
} |