Skip to content

Commit

Permalink
Fix media storage with absolute local paths
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Nov 3, 2024
1 parent 9357ba2 commit 5abe31f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import eu.iamgio.quarkdown.ast.base.inline.ReferenceImage
import eu.iamgio.quarkdown.ast.iterator.AstIteratorHook
import eu.iamgio.quarkdown.ast.iterator.ObservableAstIterator
import eu.iamgio.quarkdown.context.MutableContext
import eu.iamgio.quarkdown.log.Log
import eu.iamgio.quarkdown.media.storage.MutableMediaStorage
import java.io.File

Expand All @@ -27,7 +28,14 @@ class MediaStorerHook(

override fun attach(iterator: ObservableAstIterator) {
// Registers the media, wrapped in a link, to the media storage.
fun register(link: LinkNode) = storage.register(link.url, workingDirectory)
fun register(link: LinkNode) {
try {
storage.register(link.url, workingDirectory)
} catch (e: IllegalArgumentException) {
// If the media cannot be resolved, it is ignored and not stored.
Log.warn("Media cannot be resolved: ${link.url}")
}
}

// Images are instantly registered.
iterator.on<Image> { register(it.link) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.iamgio.quarkdown.media

import eu.iamgio.quarkdown.util.IOUtils
import eu.iamgio.quarkdown.util.toURLOrNull
import java.io.File

Expand All @@ -25,7 +26,7 @@ data class ResolvableMedia(
// If the path is a URL, it is remote.
path.toURLOrNull()?.let { return RemoteMedia(it) }

val file = workingDirectory?.let { File(it, path) } ?: File(path)
val file = IOUtils.resolvePath(path, workingDirectory)

if (!file.exists()) throw IllegalArgumentException("Media path cannot be resolved: $path")
if (file.isDirectory) throw IllegalArgumentException("Media is a directory: $path")
Expand Down
26 changes: 26 additions & 0 deletions core/src/main/kotlin/eu/iamgio/quarkdown/util/IOUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eu.iamgio.quarkdown.util

import java.io.File
import kotlin.io.path.Path

/**
* Utility methods for file-based operations.
*/
object IOUtils {
/**
* Resolves a [File] located in [path], either relative or absolute.
* If the path is relative, the location is determined from the [workingDirectory].
* @param path path of the file, either relative or absolute
* @param workingDirectory directory from which the file is resolved, in case the path is relative
* @return a [File] instance of the file
*/
fun resolvePath(
path: String,
workingDirectory: File?,
): File =
if (workingDirectory != null && !Path(path).isAbsolute) {
File(workingDirectory, path)
} else {
File(path)
}
}
10 changes: 2 additions & 8 deletions stdlib/src/main/kotlin/eu/iamgio/quarkdown/stdlib/Data.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import eu.iamgio.quarkdown.function.value.StringValue
import eu.iamgio.quarkdown.function.value.data.Range
import eu.iamgio.quarkdown.function.value.data.subList
import eu.iamgio.quarkdown.function.value.wrappedAsValue
import eu.iamgio.quarkdown.util.IOUtils
import java.io.File
import kotlin.io.path.Path

/**
* `Data` stdlib module exporter.
Expand All @@ -37,13 +37,7 @@ internal fun file(
requireExistance: Boolean = true,
): File {
val workingDirectory = context.attachedPipeline?.options?.workingDirectory

val file =
if (workingDirectory != null && !Path(path).isAbsolute) {
File(workingDirectory, path)
} else {
File(path)
}
val file = IOUtils.resolvePath(path, workingDirectory)

if (requireExistance && !file.exists()) {
throw IllegalArgumentException("File $file does not exist.")
Expand Down

0 comments on commit 5abe31f

Please sign in to comment.