From 126a31c5b262323be7255edc8f6517922ed28496 Mon Sep 17 00:00:00 2001 From: Sibo Van Gool Date: Thu, 27 Jan 2022 16:37:28 +0100 Subject: [PATCH] [fixes #825] Implement AssetHandler --- .../communication/AssetHandler.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 core/src/net/sf/openrocket/communication/AssetHandler.java diff --git a/core/src/net/sf/openrocket/communication/AssetHandler.java b/core/src/net/sf/openrocket/communication/AssetHandler.java new file mode 100644 index 000000000..194f250a4 --- /dev/null +++ b/core/src/net/sf/openrocket/communication/AssetHandler.java @@ -0,0 +1,61 @@ +package net.sf.openrocket.communication; + +import net.sf.openrocket.arch.SystemInfo; +import net.sf.openrocket.arch.SystemInfo.Platform; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +/** + * This class handles assets extracted from a GitHub release page. + * + * @author Sibo Van Gool + */ +public class AssetHandler { + private static final Map mapExtensionToOS = new HashMap<>(); // Map file extensions to operating system + private static final Map mapOSToName = new HashMap<>(); // Map operating system to a name + static { + mapExtensionToOS.put(".dmg", Platform.MAC_OS); + mapExtensionToOS.put(".exe", Platform.WINDOWS); + mapExtensionToOS.put(".AppImage", Platform.UNIX); + mapExtensionToOS.put(".jar", null); + + mapOSToName.put(Platform.MAC_OS, "Mac OS"); + mapOSToName.put(Platform.WINDOWS, "Windows"); + mapOSToName.put(Platform.UNIX, "Linux"); + mapOSToName.put(null, "JAR"); + } + + /** + * Maps a list of asset URLs to their respective operating system. + * E.g. "https://github.com/openrocket/openrocket/releases/download/release-15.03/OpenRocket-15.03.dmg" is mapped to + * "Mac OS". + * @param urls list of asset URLs + * @return map with as key the operating system and as value the corresponding asset URL + */ + public static Map mapURLToOSName(List urls) { + Map output = new TreeMap<>(); + if (urls == null) return null; + + for (String url : urls) { + for (String ext : mapExtensionToOS.keySet()) { + if (url.endsWith(ext)) { + output.put(mapOSToName.get(mapExtensionToOS.get(ext)), url); + } + } + } + return output; + } + + /** + * Returns the OS name based on the operating system that the user is running on, or the value stored in preferences. + * @return operating system name + */ + public static String getOSName() { + Platform currentPlatform = SystemInfo.getPlatform(); + // TODO: select right option based on preference + return mapOSToName.get(currentPlatform); + } +}