Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable GIF images (#1) #56

Merged
merged 1 commit into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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