Skip to content
Snippets Groups Projects
Commit b52c8eaa authored by Simon Urli's avatar Simon Urli
Browse files

[Misc] Fix NPath complexity in ImagePlugin

parent 159c798a
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
<name>XWiki Platform - Image - Processing - Plugin</name> <name>XWiki Platform - Image - Processing - Plugin</name>
<description>Deprecated Image plugin</description> <description>Deprecated Image plugin</description>
<properties> <properties>
<checkstyle.suppressions.location>${basedir}/src/main/checkstyle/checkstyle-suppressions.xml</checkstyle.suppressions.location>
<xwiki.jacoco.instructionRatio>0.64</xwiki.jacoco.instructionRatio> <xwiki.jacoco.instructionRatio>0.64</xwiki.jacoco.instructionRatio>
<!-- Old names of this module used for retro compatibility when resolving dependencies of old extensions --> <!-- Old names of this module used for retro compatibility when resolving dependencies of old extensions -->
<xwiki.extension.features>org.xwiki.platform:xwiki-platform-image-plugin</xwiki.extension.features> <xwiki.extension.features>org.xwiki.platform:xwiki-platform-image-plugin</xwiki.extension.features>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.0//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">
<suppressions>
<suppress checks="NPathComplexity" files="ImagePlugin.java"/>
</suppressions>
\ No newline at end of file
...@@ -176,6 +176,24 @@ public void flushCache() ...@@ -176,6 +176,24 @@ public void flushCache()
this.imageCache = null; this.imageCache = null;
} }
private int parseIntIgnoringException(String parameter)
{
try {
return Integer.parseInt(parameter);
} catch (NumberFormatException e) {
return -1;
}
}
private float parseFloatIgnoringException(String parameter)
{
try {
return Float.parseFloat(parameter);
} catch (NumberFormatException | NullPointerException e) {
return -1;
}
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p> * <p>
...@@ -189,45 +207,26 @@ public void flushCache() ...@@ -189,45 +207,26 @@ public void flushCache()
@Override @Override
public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context) public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context)
{ {
if (attachment == null || !this.imageProcessor.isMimeTypeSupported(attachment.getMimeType(context))) { XWikiAttachment result = attachment;
return attachment; if (attachment != null && this.imageProcessor.isMimeTypeSupported(attachment.getMimeType(context))) {
} int height = parseIntIgnoringException(context.getRequest().getParameter("height"));
int width = parseIntIgnoringException(context.getRequest().getParameter("width"));
int height = -1; float quality = parseFloatIgnoringException(context.getRequest().getParameter("quality"));
try {
height = Integer.parseInt(context.getRequest().getParameter("height")); // If no scaling is needed, return the original image.
} catch (NumberFormatException e) { if (!(height <= 0 && width <= 0 && quality < 0)) {
// Ignore. try {
} // Transform the image attachment before is it downloaded.
result = downloadImage(attachment, width, height, quality, context);
int width = -1; } catch (Exception e) {
try { LOG.warn(
width = Integer.parseInt(context.getRequest().getParameter("width")); "Failed to transform image attachment {} for scaling, falling back to original attachment.",
} catch (NumberFormatException e) { attachment.getFilename());
// Ignore. LOG.debug("Full stack trace for image attachment scaling error: ", e);
} }
}
float quality = -1;
try {
quality = Float.parseFloat(context.getRequest().getParameter("quality"));
} catch (NumberFormatException | NullPointerException e) {
// Ignore.
}
// If no scaling is needed, return the original image.
if (height <= 0 && width <= 0 && quality < 0) {
return attachment;
}
try {
// Transform the image attachment before is it downloaded.
return downloadImage(attachment, width, height, quality, context);
} catch (Exception e) {
LOG.warn("Failed to transform image attachment {} for scaling, falling back to original attachment.",
attachment.getFilename());
LOG.debug("Full stack trace for image attachment scaling error: ", e);
return attachment;
} }
return result;
} }
/** /**
......
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