Skip to content
Snippets Groups Projects
Commit 0bbe4d73 authored by Manuel Leduc's avatar Manuel Leduc
Browse files

XWIKI-20981: Cannot upload an image on Image Dialog when the Max Upload size xproperty is empty

parent 8b886f4c
No related branches found
No related tags found
No related merge requests found
Showing
with 129 additions and 3 deletions
...@@ -163,6 +163,17 @@ ...@@ -163,6 +163,17 @@
</item> </item>
</differences> </differences>
</revapi.differences> </revapi.differences>
<revapi.differences>
<differences>
<item>
<ignore>true</ignore>
<code>java.method.addedToInterface</code>
<new>method long org.xwiki.attachment.validation.AttachmentValidationConfiguration::getMaxUploadSize(org.xwiki.model.reference.EntityReference)</new>
<justification>Young API</justification>
<criticality>documented</criticality>
</item>
</differences>
</revapi.differences>
</analysisConfiguration> </analysisConfiguration>
</configuration> </configuration>
</plugin> </plugin>
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
import org.xwiki.component.annotation.Role; import org.xwiki.component.annotation.Role;
import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.stability.Unstable; import org.xwiki.stability.Unstable;
/** /**
...@@ -64,4 +65,12 @@ public interface AttachmentValidationConfiguration ...@@ -64,4 +65,12 @@ public interface AttachmentValidationConfiguration
* @since 15.0RC1 * @since 15.0RC1
*/ */
List<String> getBlockerMimetypes(DocumentReference documentReference); List<String> getBlockerMimetypes(DocumentReference documentReference);
/**
* @param entityReference the entity reference to use as the context when looking for the configuration
* @return the max allowed file size in bytes
* @since 15.5RC1
* @since 14.10.13
*/
long getMaxUploadSize(EntityReference entityReference);
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
import org.xwiki.component.manager.ComponentLookupException; import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager; import org.xwiki.component.manager.ComponentManager;
import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.script.service.ScriptService; import org.xwiki.script.service.ScriptService;
import org.xwiki.stability.Unstable; import org.xwiki.stability.Unstable;
...@@ -106,6 +107,31 @@ public List<String> getBlockerMimetypes(DocumentReference documentReference) ...@@ -106,6 +107,31 @@ public List<String> getBlockerMimetypes(DocumentReference documentReference)
.orElse(List.of()); .orElse(List.of());
} }
/**
* @param entityReference the entity reference to use as the context when resolving the configuration, or the
* current entity of {@code null}
* @return the maximum file size allowed for a given entity, in bytes
* @since 15.5RC1
* @since 14.10.13
*/
public long getUploadMaxSize(EntityReference entityReference)
{
return getAttachmentValidationConfiguration()
.map(attachmentValidationConfiguration -> attachmentValidationConfiguration
.getMaxUploadSize(entityReference))
.orElse(0L);
}
/**
* @return the maximum file size allowed for the current document, in bytes
* @since 15.5RC1
* @since 14.10.13
*/
public long getUploadMaxSize()
{
return getUploadMaxSize(null);
}
private Optional<AttachmentValidationConfiguration> getAttachmentValidationConfiguration() private Optional<AttachmentValidationConfiguration> getAttachmentValidationConfiguration()
{ {
try { try {
......
...@@ -33,11 +33,15 @@ ...@@ -33,11 +33,15 @@
import org.xwiki.component.annotation.Component; import org.xwiki.component.annotation.Component;
import org.xwiki.configuration.ConfigurationSource; import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.model.reference.DocumentReference; import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDocument; import com.xpn.xwiki.doc.XWikiDocument;
import static com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.UPLOAD_DEFAULT_MAXSIZE;
import static com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.UPLOAD_MAXSIZE_PARAMETER;
import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage; import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage;
import static org.xwiki.attachment.validation.internal.AttachmentMimetypeRestrictionClassDocumentInitializer.ALLOWED_MIMETYPES_FIELD; import static org.xwiki.attachment.validation.internal.AttachmentMimetypeRestrictionClassDocumentInitializer.ALLOWED_MIMETYPES_FIELD;
import static org.xwiki.attachment.validation.internal.AttachmentMimetypeRestrictionClassDocumentInitializer.BLOCKED_MIMETYPES_FIELD; import static org.xwiki.attachment.validation.internal.AttachmentMimetypeRestrictionClassDocumentInitializer.BLOCKED_MIMETYPES_FIELD;
...@@ -103,6 +107,26 @@ public List<String> getBlockerMimetypes(DocumentReference documentReference) ...@@ -103,6 +107,26 @@ public List<String> getBlockerMimetypes(DocumentReference documentReference)
return wrapWithScope(documentReference, this::getBlockerMimetypes); return wrapWithScope(documentReference, this::getBlockerMimetypes);
} }
@Override
public long getMaxUploadSize(EntityReference entityReference)
{
XWikiContext context = this.contextProvider.get();
XWikiDocument previous = context.getDoc();
try {
XWiki wiki = context.getWiki();
if (entityReference != null) {
context.setDoc(wiki.getDocument(entityReference, context));
}
return wiki.getSpacePreferenceAsLong(UPLOAD_MAXSIZE_PARAMETER, UPLOAD_DEFAULT_MAXSIZE, context);
} catch (XWikiException e) {
this.logger.warn("Failed to resolve the entity [{}]. Cause: [{}]", entityReference, getRootCauseMessage(e));
return UPLOAD_DEFAULT_MAXSIZE;
} finally {
context.setDoc(previous);
}
}
private List<String> getPropertyWithFallback(String attachmentXObjectProperty, String xWikiPropertiesProperty) private List<String> getPropertyWithFallback(String attachmentXObjectProperty, String xWikiPropertiesProperty)
{ {
return get(this.attachmentConfigurationSource, attachmentXObjectProperty) return get(this.attachmentConfigurationSource, attachmentXObjectProperty)
......
...@@ -22,14 +22,29 @@ ...@@ -22,14 +22,29 @@
import java.util.List; import java.util.List;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Provider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.xwiki.configuration.ConfigurationSource; import org.xwiki.configuration.ConfigurationSource;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.test.junit5.mockito.ComponentTest; import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents; import org.xwiki.test.junit5.mockito.InjectMockComponents;
import org.xwiki.test.junit5.mockito.MockComponent; import org.xwiki.test.junit5.mockito.MockComponent;
import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;
import static com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.UPLOAD_DEFAULT_MAXSIZE;
import static com.xpn.xwiki.plugin.fileupload.FileUploadPlugin.UPLOAD_MAXSIZE_PARAMETER;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoInteractions;
...@@ -61,6 +76,22 @@ class DefaultAttachmentValidationConfigurationTest ...@@ -61,6 +76,22 @@ class DefaultAttachmentValidationConfigurationTest
@Named("xwikiproperties") @Named("xwikiproperties")
private ConfigurationSource xWikiPropertiesConfigurationSource; private ConfigurationSource xWikiPropertiesConfigurationSource;
@MockComponent
private Provider<XWikiContext> contextProvider;
@Mock
private XWikiContext xwikiContext;
@Mock
private XWiki wiki;
@BeforeEach
void setUp()
{
when(this.contextProvider.get()).thenReturn(this.xwikiContext);
when(this.xwikiContext.getWiki()).thenReturn(this.wiki);
}
@Test @Test
void getAllowedMimetypesInConfigurationSource() void getAllowedMimetypesInConfigurationSource()
{ {
...@@ -157,4 +188,29 @@ void getAllowedMimeTypesEmptyLists() ...@@ -157,4 +188,29 @@ void getAllowedMimeTypesEmptyLists()
verify(this.wikiConfigurationSource).getProperty(ALLOWED_MIMETYPES_FIELD); verify(this.wikiConfigurationSource).getProperty(ALLOWED_MIMETYPES_FIELD);
verifyNoInteractions(this.xWikiPropertiesConfigurationSource); verifyNoInteractions(this.xWikiPropertiesConfigurationSource);
} }
@Test
void getMaxUploadSize()
{
XWikiDocument previous = mock(XWikiDocument.class);
when(this.xwikiContext.getDoc()).thenReturn(previous);
this.configuration.getMaxUploadSize(null);
verify(this.wiki).getSpacePreferenceAsLong(UPLOAD_MAXSIZE_PARAMETER, UPLOAD_DEFAULT_MAXSIZE, this.xwikiContext);
verify(this.xwikiContext).setDoc(previous);
}
@Test
void getMaxUploadSizeWithReference() throws Exception
{
DocumentReference entityReference = new DocumentReference("xwiki", "Space", "Page");
XWikiDocument previous = mock(XWikiDocument.class);
XWikiDocument doc = mock(XWikiDocument.class);
when(this.xwikiContext.getDoc()).thenReturn(previous);
when(this.wiki.getDocument(any(EntityReference.class), any())).thenReturn(doc);
this.configuration.getMaxUploadSize(entityReference);
verify(this.wiki).getSpacePreferenceAsLong(UPLOAD_MAXSIZE_PARAMETER, UPLOAD_DEFAULT_MAXSIZE, this.xwikiContext);
InOrder inOrder = inOrder(this.xwikiContext);
inOrder.verify(this.xwikiContext).setDoc(doc);
inOrder.verify(this.xwikiContext).setDoc(previous);
}
} }
...@@ -37,9 +37,9 @@ ...@@ -37,9 +37,9 @@
<syntaxId>xwiki/2.1</syntaxId> <syntaxId>xwiki/2.1</syntaxId>
<hidden>true</hidden> <hidden>true</hidden>
<content>{{velocity wiki='false'}} <content>{{velocity wiki='false'}}
#set ($spaceReference = $services.model.resolveDocument($!request.documentReference).extractReference('SPACE')) #set ($documentReference = $services.model.resolveDocument($!request.documentReference))
$jsontool.serialize({ $jsontool.serialize({
"maxFileSize": $xwiki.getSpacePreferenceFor('upload_maxsize', $spaceReference) "maxFileSize": $services.attachmentValidation.getUploadMaxSize($documentReference)
}) })
{{/velocity}}</content> {{/velocity}}</content>
<object> <object>
...@@ -323,7 +323,7 @@ ...@@ -323,7 +323,7 @@
<content>{{velocity}} <content>{{velocity}}
{{html clean='false'}} {{html clean='false'}}
&lt;script type='application/json' id='attachment-validation-filesize-configuration'&gt;$jsontool.serialize({ &lt;script type='application/json' id='attachment-validation-filesize-configuration'&gt;$jsontool.serialize({
"maxFileSize": $xwiki.getSpacePreference('upload_maxsize', 107374182400) "maxFileSize": $services.attachmentValidation.getUploadMaxSize()
}).replace('&lt;', '\u003C').replace('{{/', '\u007B\u007B\u002F')&lt;/script&gt; }).replace('&lt;', '\u003C').replace('{{/', '\u007B\u007B\u002F')&lt;/script&gt;
{{/html}} {{/html}}
{{/velocity}}</content> {{/velocity}}</content>
......
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