allow spaces in extension names

This commit is contained in:
sirjonasxx 2021-08-20 02:35:42 +02:00
parent 35e9adefd5
commit 344bdea382
7 changed files with 99 additions and 8 deletions

View File

@ -51,7 +51,7 @@ public class StoreExtensionItem implements ContentItem {
.append("<div class=\"overview_item ").append(displayColor(i)).append(" content_item\">")
.append("<div class=\"overview_item_logo\">")
.append("<img src=\"").append(repository.getResourceUrl(String.format("store/extensions/%s/icon.png", storeExtension.getTitle()))).append("\" alt=\"\">")
.append("<img src=\"").append(repository.getResourceUrl("store", "extensions", storeExtension.getTitle(), "icon.png")).append("\" alt=\"\">")
.append("</div>")
.append("<div class=\"overview_item_info\">")

View File

@ -49,7 +49,7 @@ public class CategoryItem implements ContentItem {
.append("<div class=\"overview_item ").append(i % 2 == 0 ? "item_lightblue" : "item_darkblue").append(" content_item\">")
.append("<div class=\"overview_item_logo\">")
.append("<img src=\"").append(repository.getResourceUrl(String.format("assets/icons/%s", category.getIcon()))).append("\" alt=\"\">")
.append("<img src=\"").append(repository.getResourceUrl("assets", "icons", category.getIcon())).append("\" alt=\"\">")
.append("</div>")
.append("<div class=\"overview_item_info\">")

View File

@ -7,12 +7,14 @@ import gearth.services.internal_extensions.extensionstore.application.entities.C
import gearth.services.internal_extensions.extensionstore.application.entities.HOverview;
import gearth.services.internal_extensions.extensionstore.repository.models.ExtCategory;
import gearth.services.internal_extensions.extensionstore.repository.models.StoreExtension;
import gearth.services.internal_extensions.extensionstore.tools.EncodingUtil;
import netscape.javascript.JSObject;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
@ -48,7 +50,7 @@ public class StoreExtensionDetailsItem implements ContentItem {
try {
JSONObject habboData = new JSONObject(IOUtils.toString(
new URL(HABBO_API_URL.replace("{hotel}", mainAuthor.getHotel()).replace("{user}",
mainAuthor.getUsername())).openStream(), StandardCharsets.UTF_8));
EncodingUtil.encodeURIComponent(mainAuthor.getUsername()))).openStream(), StandardCharsets.UTF_8));
if (habboData.has("figureString")) {
avatarImageUrl = OUTFIT_URL.replace("{figureString}", habboData.getString("figureString"));
@ -101,7 +103,7 @@ public class StoreExtensionDetailsItem implements ContentItem {
}
contentBuilder.append("\n*Screenshot: *").append("\n")
.append("--img:").append(gExtensionStore.getRepository().getResourceUrl(String.format("store/extensions/%s/screenshot.png", storeExtension.getTitle())));
.append("--img:").append(gExtensionStore.getRepository().getResourceUrl("store", "extensions", storeExtension.getTitle(), "screenshot.png"));
return contentBuilder.toString();
}

View File

@ -159,7 +159,7 @@ public class StoreExtensionDetailsOverview extends HOverview {
return new Header() {
@Override
public String iconUrl() {
return storeRepository.getResourceUrl(String.format("store/extensions/%s/icon.png", extension.getTitle()));
return storeRepository.getResourceUrl("store", "extensions", extension.getTitle(), "icon.png");
}
@Override

View File

@ -32,7 +32,7 @@ public class CategorizedOverview extends QueriedExtensionOverview {
return new Header() {
@Override
public String iconUrl() {
return storeRepository.getResourceUrl(String.format("assets/icons/%s", category.getIcon()));
return storeRepository.getResourceUrl("assets", "icons", category.getIcon());
}
@Override

View File

@ -2,7 +2,10 @@ package gearth.services.internal_extensions.extensionstore.repository;
import gearth.services.internal_extensions.extensionstore.repository.models.*;
import gearth.services.internal_extensions.extensionstore.repository.querying.ExtensionOrdering;
import gearth.services.internal_extensions.extensionstore.tools.EncodingUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -112,8 +115,9 @@ public class StoreRepository {
return repoVersion;
}
public String getResourceUrl(String resource) {
return String.format("https://raw.githubusercontent.com/%s/repo/%s/%s", source, repoVersion, resource);
public String getResourceUrl(String... resource) {
return String.format("https://raw.githubusercontent.com/%s/repo/%s/%s", source, repoVersion,
Arrays.stream(resource).map(EncodingUtil::encodeURIComponent).collect(Collectors.joining("/")));
}
}

View File

@ -0,0 +1,85 @@
package gearth.services.internal_extensions.extensionstore.tools;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Utility class for JavaScript compatible UTF-8 encoding and decoding.
*
* @see http://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-output
* @author John Topley
*/
public class EncodingUtil
{
/**
* Decodes the passed UTF-8 String using an algorithm that's compatible with
* JavaScript's <code>decodeURIComponent</code> function. Returns
* <code>null</code> if the String is <code>null</code>.
*
* @param s The UTF-8 encoded String to be decoded
* @return the decoded String
*/
public static String decodeURIComponent(String s)
{
if (s == null)
{
return null;
}
String result = null;
try
{
result = URLDecoder.decode(s, "UTF-8");
}
// This exception should never occur.
catch (UnsupportedEncodingException e)
{
result = s;
}
return result;
}
/**
* Encodes the passed String as UTF-8 using an algorithm that's compatible
* with JavaScript's <code>encodeURIComponent</code> function. Returns
* <code>null</code> if the String is <code>null</code>.
*
* @param s The String to be encoded
* @return the encoded String
*/
public static String encodeURIComponent(String s)
{
String result = null;
try
{
result = URLEncoder.encode(s, "UTF-8")
.replaceAll("\\+", "%20")
.replaceAll("%21", "!")
.replaceAll("%27", "'")
.replaceAll("%28", "(")
.replaceAll("%29", ")")
.replaceAll("%7E", "~");
}
// This exception should never occur.
catch (UnsupportedEncodingException e)
{
result = s;
}
return result;
}
/**
* Private constructor to prevent this class from being instantiated.
*/
private EncodingUtil()
{
super();
}
}