Skip to content
Snippets Groups Projects
Commit 2a78e737 authored by tmortagne's avatar tmortagne
Browse files

XWIKI-11357: It's possible to loose external images files from an office...

XWIKI-11357: It's possible to loose external images files from an office preview when several preview of the same attachment are done with different parameters
parent 1b770ff1
No related branches found
No related tags found
No related merge requests found
...@@ -157,11 +157,11 @@ public class DefaultOfficeResourceViewer implements OfficeResourceViewer, Initia ...@@ -157,11 +157,11 @@ public class DefaultOfficeResourceViewer implements OfficeResourceViewer, Initia
@Inject @Inject
private Logger logger; private Logger logger;
String getFilePath(String resourceName, String fileName) String getFilePath(String resourceName, String fileName, Map<String, ?> parameters)
{ {
try { try {
return URLEncoder.encode(getSafeFileName(resourceName), DEFAULT_ENCODING) + '/' return URLEncoder.encode(getSafeFileName(resourceName), DEFAULT_ENCODING) + '/' + parameters.hashCode()
+ URLEncoder.encode(getSafeFileName(fileName), DEFAULT_ENCODING); + '/' + URLEncoder.encode(getSafeFileName(fileName), DEFAULT_ENCODING);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
// Should never happen // Should never happen
...@@ -180,10 +180,12 @@ String getFilePath(String resourceName, String fileName) ...@@ -180,10 +180,12 @@ String getFilePath(String resourceName, String fileName)
* during the office import process should be processed * during the office import process should be processed
* @param attachmentReference a reference to the office file that is being viewed; this reference is used to compute * @param attachmentReference a reference to the office file that is being viewed; this reference is used to compute
* the path to the temporary directory holding the image artifacts * the path to the temporary directory holding the image artifacts
* @param parameters the build parameters. Note that currently only {@code filterStyles} is supported and if "true"
* it means that styles will be filtered to the maximum and the focus will be put on importing only the
* @return the set of temporary files corresponding to image artifacts * @return the set of temporary files corresponding to image artifacts
*/ */
private Set<File> processImages(XDOM xdom, Map<String, byte[]> artifacts, DocumentReference documentReference, private Set<File> processImages(XDOM xdom, Map<String, byte[]> artifacts, DocumentReference documentReference,
String resourceReference) String resourceReference, Map<String, ?> parameters)
{ {
// Process all image blocks. // Process all image blocks.
Set<File> temporaryFiles = new HashSet<File>(); Set<File> temporaryFiles = new HashSet<File>();
...@@ -194,7 +196,7 @@ private Set<File> processImages(XDOM xdom, Map<String, byte[]> artifacts, Docume ...@@ -194,7 +196,7 @@ private Set<File> processImages(XDOM xdom, Map<String, byte[]> artifacts, Docume
// Check whether there is a corresponding artifact. // Check whether there is a corresponding artifact.
if (artifacts.containsKey(imageReference)) { if (artifacts.containsKey(imageReference)) {
try { try {
String filePath = getFilePath(resourceReference, imageReference); String filePath = getFilePath(resourceReference, imageReference, parameters);
// Write the image into a temporary file. // Write the image into a temporary file.
File tempFile = getTemporaryFile(documentReference, filePath); File tempFile = getTemporaryFile(documentReference, filePath);
...@@ -339,7 +341,7 @@ private OfficeDocumentView getView(ResourceReference reference, AttachmentRefere ...@@ -339,7 +341,7 @@ private OfficeDocumentView getView(ResourceReference reference, AttachmentRefere
XDOM xdom = xdomOfficeDocument.getContentDocument(); XDOM xdom = xdomOfficeDocument.getContentDocument();
Set<File> temporaryFiles = Set<File> temporaryFiles =
processImages(xdom, xdomOfficeDocument.getArtifacts(), attachmentReference.getDocumentReference(), processImages(xdom, xdomOfficeDocument.getArtifacts(), attachmentReference.getDocumentReference(),
this.resourceReferenceSerializer.serialize(reference)); this.resourceReferenceSerializer.serialize(reference), parameters);
view = view =
new AttachmentOfficeDocumentView(reference, attachmentReference, attachmentVersion, xdom, new AttachmentOfficeDocumentView(reference, attachmentReference, attachmentVersion, xdom,
temporaryFiles); temporaryFiles);
...@@ -366,7 +368,8 @@ private OfficeDocumentView getView(ResourceReference resourceReference, Map<Stri ...@@ -366,7 +368,8 @@ private OfficeDocumentView getView(ResourceReference resourceReference, Map<Stri
XDOMOfficeDocument xdomOfficeDocument = createXDOM(ownerDocument, resourceReference, parameters); XDOMOfficeDocument xdomOfficeDocument = createXDOM(ownerDocument, resourceReference, parameters);
XDOM xdom = xdomOfficeDocument.getContentDocument(); XDOM xdom = xdomOfficeDocument.getContentDocument();
Set<File> temporaryFiles = Set<File> temporaryFiles =
processImages(xdom, xdomOfficeDocument.getArtifacts(), ownerDocument, serializedResourceReference); processImages(xdom, xdomOfficeDocument.getArtifacts(), ownerDocument, serializedResourceReference,
parameters);
view = new OfficeDocumentView(resourceReference, xdom, temporaryFiles); view = new OfficeDocumentView(resourceReference, xdom, temporaryFiles);
this.externalCache.set(cacheKey, view); this.externalCache.set(cacheKey, view);
......
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
import org.xwiki.rendering.listener.reference.AttachmentResourceReference; import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
import org.xwiki.rendering.listener.reference.ResourceReference; import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType; import org.xwiki.rendering.listener.reference.ResourceType;
import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer; import org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer;
import org.xwiki.test.mockito.MockitoComponentMockingRule; import org.xwiki.test.mockito.MockitoComponentMockingRule;
...@@ -313,10 +312,10 @@ public void testGetTemporaryFile() throws Exception ...@@ -313,10 +312,10 @@ public void testGetTemporaryFile() throws Exception
DefaultOfficeResourceViewer implementation = (DefaultOfficeResourceViewer) mocker.getComponentUnderTest(); DefaultOfficeResourceViewer implementation = (DefaultOfficeResourceViewer) mocker.getComponentUnderTest();
File tempFile = File tempFile =
implementation implementation.getTemporaryFile(ATTACHMENT_REFERENCE.getDocumentReference(),
.getTemporaryFile(ATTACHMENT_REFERENCE.getDocumentReference(), "Test+file.doc/some+image.png"); "Test+file.doc/0/some+image.png");
Assert.assertTrue(tempFile.getAbsolutePath().endsWith( Assert.assertTrue(tempFile.getAbsolutePath().endsWith(
"/temp/officeviewer/xwiki/Main/Test/Test+file.doc/some+image.png")); "/temp/officeviewer/xwiki/Main/Test/Test+file.doc/0/some+image.png"));
} }
/** /**
...@@ -331,7 +330,9 @@ public void testGetFilePath() throws Exception ...@@ -331,7 +330,9 @@ public void testGetFilePath() throws Exception
.thenReturn("/xwiki/bin/temp/Main/Test"); .thenReturn("/xwiki/bin/temp/Main/Test");
DefaultOfficeResourceViewer implementation = (DefaultOfficeResourceViewer) mocker.getComponentUnderTest(); DefaultOfficeResourceViewer implementation = (DefaultOfficeResourceViewer) mocker.getComponentUnderTest();
String filePath = implementation.getFilePath(ATTACHMENT_REFERENCE.getName(), "some temporary artifact.gif"); String filePath =
Assert.assertEquals("Test+file.doc/some+temporary+artifact.gif", filePath); implementation.getFilePath(ATTACHMENT_REFERENCE.getName(), "some temporary artifact.gif",
Collections.<String, Object>emptyMap());
Assert.assertEquals("Test+file.doc/0/some+temporary+artifact.gif", filePath);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment