package eu.friedcutlet.yokan.services.files internal data class SafDocumentPath(val documentId: String, val children: List) /** URI pathSegments have already decoded opaque document IDs; never split those IDs as paths. */ internal fun safDocumentPath(segments: List): SafDocumentPath? { if (segments.getOrNull(0) != "tree" || segments.getOrNull(1).isNullOrBlank()) return null val explicitDocument = segments.getOrNull(2) == "document" val id = segments.getOrNull(if (explicitDocument) 3 else 1)?.takeIf(String::isNotBlank) ?: return null val children = segments.drop(if (explicitDocument) 4 else 2) if (children.size > 64 || children.any { it.isBlank() || it == "." || it == ".." || '/' in it || '\\' in it || '\u0000' in it }) return null return SafDocumentPath(id, children) }