DiscreteFileProvider

class DiscreteFileProvider(fileSystem: FileSystem, rootDir: Path, initialFiles: List<File> = emptyList(), mimeTypeDetector: MimeTypeDetector = MimeTypeDetector { "text/plain" }) : ResourceProvider

A ResourceProvider that exposes a finite, discrete set of files (by relative path) from a given rootDir. It does NOT return any resource templates.

Example usage:

val fileSystem: FileSystem = FileSystem.SYSTEM
val provider = DiscreteFileProvider(
fileSystem = fileSystem,
rootDir = "/some/local/folder".toPath(),
knownFiles = listOf("notes.txt", "report.pdf")
)

Once created, listResources will show the above files, each accessible via a file:// URI like file://notes.txt.

If you need to add or remove files at runtime, call addFile or removeFile.

Constructors

Link copied to clipboard
constructor(fileSystem: FileSystem, rootDir: Path, initialFiles: List<File> = emptyList(), mimeTypeDetector: MimeTypeDetector = MimeTypeDetector { "text/plain" })

Properties

Link copied to clipboard
var onResourceChange: suspend (uri: String) -> Unit
Link copied to clipboard
var onResourcesListChanged: suspend () -> Unit
Link copied to clipboard
open override val supportsSubscriptions: Boolean = true

Functions

Link copied to clipboard
suspend fun addFile(file: File): Boolean

Adds a file to the known list, triggering onResourcesListChanged() if the file wasn't in the list before.

Link copied to clipboard
open fun attachCallbacks(onResourceChange: suspend (String) -> Unit, onResourcesListChanged: suspend () -> Unit)
Link copied to clipboard
open suspend override fun listResources(): List<Resource>

Lists the currently known, discrete resources. Each resource is associated with a file://relativePath URI.

Link copied to clipboard
Link copied to clipboard
open suspend override fun readResource(uri: String): ResourceContents?

Reads file contents if uri starts with file:// and matches one of the known files. If the file doesn't exist or is a directory, returns null.

Link copied to clipboard
suspend fun removeFile(relativePath: String): Boolean

Removes a file from the known list, triggering onResourcesListChanged() if removed.