Skip to content

Commit

Permalink
Enable GIF images (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Minardi committed Dec 14, 2018
1 parent e24f561 commit 73c1a5e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
29 changes: 29 additions & 0 deletions docet-core/src/main/java/docet/DocetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

import docet.model.DocetPackageDescriptor;
import io.netty.util.internal.PlatformDependent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
Expand Down Expand Up @@ -170,4 +173,30 @@ public static Integer[] convertHexColorToRgb(final String hex) {
Integer.valueOf( hexCode.substring( 5, 7 ), 16 )
};
}

public static boolean extensionAllowed(String extension) {
List<String> forbiddenExtensions = new ArrayList<>();

for (ForbiddenExtensions fe : ForbiddenExtensions.values()) {
forbiddenExtensions.add(fe.extension());
}

return forbiddenExtensions.contains(extension) == false;
}

private enum ForbiddenExtensions {
JPEG("jpeg"),
JPG("jpg");

private final String extension;

private ForbiddenExtensions(final String extension) {
this.extension = extension;
}

public String extension() {
return extension;
}
}

}
15 changes: 8 additions & 7 deletions docet-core/src/main/java/docet/engine/DocetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import docet.DocetLanguage;
import docet.DocetPackageLocator;
import docet.DocetUtils;
import static docet.DocetUtils.extensionAllowed;
import docet.SimpleDocetDocumentAccessor;
import docet.SimplePackageLocator;
import docet.StatsCollector;
Expand Down Expand Up @@ -1163,15 +1164,15 @@ private void servePackageListRequest(final String lang, final String[] packageId
}

private void serveImageRequest(final String packageId, final String imageName, final String lang,
final DocetExecutionContext ctx, final HttpServletResponse response)
throws DocetException {
final String imageFormat = imageName.substring(imageName.indexOf('.') + 1);
if (!"png".equals(imageFormat)) {
final DocetExecutionContext ctx, final HttpServletResponse response)
throws DocetException {
String imageExtension = imageName.substring(imageName.indexOf('.') + 1);
if (!extensionAllowed(imageExtension)) {
throw new DocetException(DocetException.CODE_RESOURCE_NOTFOUND,
"Unsupported image file format " + imageFormat);
"Unsupported image file format " + imageExtension);
}
response.setContentType("image/png");
try (OutputStream out = response.getOutputStream();) {
response.setContentType("image/" + imageExtension);
try (OutputStream out = response.getOutputStream()) {
this.getImageBylangForPackage(imageName, lang, packageId, out, ctx);
} catch (DocetException ex) {
LOGGER.log(Level.SEVERE, "Error on serving Image " + imageName + " packageid " + packageId + " lang ", ex);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ <h1>Sample Style Document</h1>
<p>A styling guide is essential in order to start writing down a guide. Whether you adopt your own style or customize the one presented here, it is fundamental to get acquainted with docet source language</p>
<p>The following is a brandnew sample image:
<img src="sampleimage.png">
<p>And a brandnew sample image gif:
<img src="sampleimage.gif">
</p>

<div class="related">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ private static boolean allowedFileExtension(final String fileExtension) {
}

private enum ForbiddenExtensions {
JPEG("jpeg"), JPG("jpg"), GIF("gif");
JPEG("jpeg"), JPG("jpg");

private String extension;

Expand Down

0 comments on commit 73c1a5e

Please sign in to comment.