Skip to content

Commit

Permalink
feat: add URL factory for the JVM platform
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMalch committed Jul 23, 2024
1 parent 17efec3 commit ad96e63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ktor-revfile-core/api/ktor-revfile-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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].
*
Expand Down Expand Up @@ -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'."
Expand Down

0 comments on commit ad96e63

Please sign in to comment.