From ad96e6366753dc01d5cb83a411e0b457f8bb4fc2 Mon Sep 17 00:00:00 2001 From: JanMalch <25508038+JanMalch@users.noreply.github.com> Date: Tue, 23 Jul 2024 20:49:40 +0200 Subject: [PATCH] feat: add URL factory for the JVM platform --- ktor-revfile-core/api/ktor-revfile-core.api | 2 ++ .../ktor/revfile/UriRegisterableFile.jvm.kt | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ktor-revfile-core/api/ktor-revfile-core.api b/ktor-revfile-core/api/ktor-revfile-core.api index 9401420..33a26e0 100644 --- a/ktor-revfile-core/api/ktor-revfile-core.api +++ b/ktor-revfile-core/api/ktor-revfile-core.api @@ -61,6 +61,8 @@ public final class io/github/janmalch/ktor/revfile/UriRegisterableFile_jvmKt { public static synthetic fun resource$default (Lio/github/janmalch/ktor/revfile/WriteableRevFileRegistry;Ljava/lang/String;Ljava/lang/String;Lio/ktor/http/ContentType;Ljava/lang/ClassLoader;ILjava/lang/Object;)Lio/github/janmalch/ktor/revfile/RevisionedFile; public static final fun uri (Lio/github/janmalch/ktor/revfile/WriteableRevFileRegistry;Ljava/net/URI;Ljava/lang/String;Lio/ktor/http/ContentType;)Lio/github/janmalch/ktor/revfile/RevisionedFile; public static synthetic fun uri$default (Lio/github/janmalch/ktor/revfile/WriteableRevFileRegistry;Ljava/net/URI;Ljava/lang/String;Lio/ktor/http/ContentType;ILjava/lang/Object;)Lio/github/janmalch/ktor/revfile/RevisionedFile; + public static final fun url (Lio/github/janmalch/ktor/revfile/WriteableRevFileRegistry;Ljava/net/URL;Ljava/lang/String;Lio/ktor/http/ContentType;)Lio/github/janmalch/ktor/revfile/RevisionedFile; + public static synthetic fun url$default (Lio/github/janmalch/ktor/revfile/WriteableRevFileRegistry;Ljava/net/URL;Ljava/lang/String;Lio/ktor/http/ContentType;ILjava/lang/Object;)Lio/github/janmalch/ktor/revfile/RevisionedFile; } public abstract interface class io/github/janmalch/ktor/revfile/WriteableRevFileRegistry : io/github/janmalch/ktor/revfile/ReadableRevFileRegistry { diff --git a/ktor-revfile-core/src/jvmMain/kotlin/io/github/janmalch/ktor/revfile/UriRegisterableFile.jvm.kt b/ktor-revfile-core/src/jvmMain/kotlin/io/github/janmalch/ktor/revfile/UriRegisterableFile.jvm.kt index 9a91de0..425ecce 100644 --- a/ktor-revfile-core/src/jvmMain/kotlin/io/github/janmalch/ktor/revfile/UriRegisterableFile.jvm.kt +++ b/ktor-revfile-core/src/jvmMain/kotlin/io/github/janmalch/ktor/revfile/UriRegisterableFile.jvm.kt @@ -3,6 +3,7 @@ package io.github.janmalch.ktor.revfile import io.ktor.http.* import io.ktor.http.content.* import java.net.URI +import java.net.URL private data class UriEntry( override val originalName: String, @@ -23,6 +24,15 @@ fun WriteableRevFileRegistry.uri( content = URIFileContent(uri, contentType), ).let(this::register) +/** + * Creates a new [RevisionedFile] from the given URL and adds it to this [WriteableRevFileRegistry]. + */ +fun WriteableRevFileRegistry.url( + url: URL, + name: String = url.toString().substringAfterLast('/'), + contentType: ContentType = ContentType.defaultForFilePath(url.path), +): RevisionedFile = uri(uri = url.toURI(), name = name, contentType = contentType) + /** * Creates a new [RevisionedFile] from the given JVM resource and adds it to this [WriteableRevFileRegistry]. * @@ -52,16 +62,16 @@ fun WriteableRevFileRegistry.uri( * ``` * * @param resource the name of the resource to load - * @param name the name to be used for the file - * @param contentType optionally force a content type, will be inferred otherwise + * @param name the name to be used for the revisioned file + * @param contentType the content type to be used for the revisioned file * @param classLoader the `ClassLoader` to use for loading the resource * @see ClassLoader.getResource - * @throws IllegalArgumentException if the resource cannot be loaded. + * @throws IllegalArgumentException if the resource cannot be loaded */ fun WriteableRevFileRegistry.resource( resource: String, name: String = resource.substringAfterLast('/'), - contentType: ContentType? = null, + contentType: ContentType = ContentType.defaultForFilePath(resource), // TODO: see if this class loader makes any sense classLoader: ClassLoader = checkNotNull(this::class.java.classLoader) { "Failed to get a class loader for loading resource '$resource'."