Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,23 @@ public HTMLContent fetchContent() throws IOException {
if (!(content instanceof UrlContent urlContent)) {
throw new IllegalStateException("HTMLContent is not a URL");
}
var url = urlContent.url();

try {
Optional<Library> libraryOpt = new LibraryManager().getLibrary(urlContent.url()).get();
Optional<Library> libraryOpt = new LibraryManager().getLibrary(url).get();
if (libraryOpt.isEmpty()) {
throw new IOException(
I18N.getText("msg.error.html.loadingURL", urlContent.url().toExternalForm()));
throw new IOException(I18N.getText("msg.error.html.loadingURL", url.toExternalForm()));
}

var library = libraryOpt.get();
var assetKey = library.getAssetKey(urlContent.url()).get().orElse(null);
var assetKey = library.getAssetKey(url).get().orElse(null);
// Check if the asset key is null, if so try reading the resource as a string from the
// library
if (assetKey == null) {
String html = library.readAsString(urlContent.url()).get();
String html = library.readAsString(url).get();
if (html != null) {
var mediaType = Asset.getMediaType("", html.getBytes(StandardCharsets.UTF_8));
var assetType = Asset.Type.fromMediaType(mediaType);
var mediaType = Asset.getMediaType(url.getPath(), html.getBytes(StandardCharsets.UTF_8));
var assetType = Asset.Type.fromMediaType(mediaType, url.getPath());

if (assetType == Asset.Type.HTML) {
return new HTMLContent(new HtmlDocumentContent(html));
Expand All @@ -352,8 +352,7 @@ public HTMLContent fetchContent() throws IOException {
} catch (InterruptedException | ExecutionException e) {
throw new IOException(e);
}
throw new IOException(
I18N.getText("msg.error.html.loadingURL", urlContent.url().toExternalForm()));
throw new IOException(I18N.getText("msg.error.html.loadingURL", url.toExternalForm()));
}

/**
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/net/rptools/maptool/model/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.rptools.maptool.model.library.addon.AddOnLibraryImporter;
import net.rptools.maptool.server.proto.AssetDto;
import net.rptools.maptool.server.proto.AssetDtoType;
import net.rptools.maptool.util.HandlebarsUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.exception.TikaException;
Expand All @@ -61,6 +62,8 @@ public enum Type {
IMAGE(false, "", Asset::createImageAsset), // extension is determined from format.
/** The {@code Asset} is an audio file. */
AUDIO(false, "", Asset::createAudioAsset), // extension is determined from format.
/** The {@code Asset} is a Handlebars template. */
HANDLEBARS(true, "hbs", Asset::createHandlebarsAsset),
/** The {@code Asset} is an HTML string. */
HTML(true, "html", Asset::createHTMLAsset),
/** The {@code Asset} is some generic data. */
Expand Down Expand Up @@ -161,7 +164,12 @@ public static Type fromMediaType(MediaType mediaType, String filename) {
case "image" -> Type.IMAGE;
case "text" ->
switch (subType) {
case "html" -> Type.HTML;
case "html" -> {
if (HandlebarsUtil.isAssetFileHandlebars(filename)) {
yield Type.HANDLEBARS;
}
yield Type.HTML;
}
case "markdown", "x-web-markdown" -> Type.MARKDOWN;
case "javascript" -> Type.JAVASCRIPT;
case "css" -> Type.CSS;
Expand Down Expand Up @@ -419,6 +427,18 @@ public static Asset createAssetDetectType(String name, byte[] data, File file)
return factory.apply(name, data);
}

/**
* Creates a Handlebars {@code Asset}.
*
* @param name The name of the {@code Asset}.
* @param data The data for the {@code Asset}.
* @return the Handlebars {@code Asset}.
*/
public static Asset createHandlebarsAsset(String name, byte[] data) {
return new Asset(
null, name, data, Type.HANDLEBARS, Type.HANDLEBARS.getDefaultExtension(), false);
}

/**
* Creates a HTML {@code Asset}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ private void addMetaData(
String path = METADATA_DIR + entry.getName();
try (InputStream inputStream = zip.getInputStream(entry)) {
byte[] bytes = inputStream.readAllBytes();
MediaType mediaType = Asset.getMediaType(entry.getName(), bytes);
Asset asset =
Type.fromMediaType(mediaType).getFactory().apply(namespace + "/" + path, bytes);
var assetName = namespace + "/" + path;
MediaType mediaType = Asset.getMediaType(assetName, bytes);
Asset asset = Type.fromMediaType(mediaType, assetName).getFactory().apply(assetName, bytes);
addAsset(asset);
pathAssetMap.put(path, Pair.with(asset.getMD5Key(), asset.getType()));
}
Expand All @@ -277,9 +277,9 @@ private Map<String, Pair<MD5Key, Type>> processAssets(String namespace, ZipFile
String path = entry.getName().substring(CONTENT_DIRECTORY.length());
try (InputStream inputStream = zip.getInputStream(entry)) {
byte[] bytes = inputStream.readAllBytes();
MediaType mediaType = Asset.getMediaType(entry.getName(), bytes);
Asset asset =
Type.fromMediaType(mediaType).getFactory().apply(namespace + "/" + path, bytes);
var assetName = namespace + "/" + path;
MediaType mediaType = Asset.getMediaType(assetName, bytes);
Asset asset = Type.fromMediaType(mediaType, assetName).getFactory().apply(assetName, bytes);
addAsset(asset);
pathAssetMap.put(path, Pair.with(asset.getMD5Key(), asset.getType()));
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/rptools/maptool/util/HandlebarsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
* @param <T> The type of the bean to apply the template to.
*/
public class HandlebarsUtil<T> {
public static boolean isAssetFileHandlebars(String filename) {
if (filename == null) {
return false;
}
return filename.toLowerCase().endsWith(".hbs");
}

/** The compiled template. */
private final Template template;
Expand Down