Move SSD tree file guards to typed Kotlin

ober

2275f67a1eca2e86d993d750b7330e81b6593333

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index bb7ab30..f87afe5 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -4331,7 +4331,8 @@
                 boxTypeFallbackName boxTypeLabelMatches defaultBoxTypeLabel
                 isGenericGuessLabel isSsdPdfName
                 shouldIncludeSsdPdfChoiceName shouldIncludeSsdPdfFile
-                shouldStopSsdPdfTreeScan documentFilePresent documentFileIsDirectory
+                shouldStopSsdPdfTreeScan documentFilePresent deleteDocumentFileIfPresent
+                documentFileIsDirectory
                 documentFileIsRegular ssdPdfDocumentIsDirectory
                 ssdPdfDocumentNotDirectory
                 shouldIncludeSsdPdfDocument
@@ -4430,6 +4431,8 @@
           (kotlin-member-get isDirectory))
         (extern (documentFileIsFile (file : DocumentFile)) : Bool
           (kotlin-member-get isFile))
+        (extern (documentFileDelete (file : DocumentFile)) : Bool
+          (kotlin-member-call delete))
         (extern (stringLines (text : String)) : (List String)
           (kotlin-member-call lines))
         (extern (stringSplitTwoChars (text : String)
@@ -4546,6 +4549,12 @@
               (>= currentChoices limit)))
         (def (documentFilePresent (file : (Nullable DocumentFile))) : Bool
           (not (nullable-null? file)))
+        (def (deleteDocumentFileIfPresent (file : (Nullable DocumentFile))) : Unit
+          (if (nullable-null? file)
+            (begin)
+            (begin
+              (documentFileDelete (nullable-get file))
+              (begin))))
         (def (documentFileIsDirectory (file : DocumentFile)) : Bool
           (documentFileIsDirectoryRaw file))
         (def (documentFileIsRegular (file : DocumentFile)) : Bool
@@ -9344,10 +9353,11 @@
        "        val fileName = treePathLeaf(parts)"
        "        var dir = parent"
        "        treePathDirectoryParts(parts).forEach { dir = ensureTreeDir(dir, it) }"
-       "        dir.findFile(fileName)?.delete()"
+       "        deleteDocumentFileIfPresent(dir.findFile(fileName))"
        "        val mime = treeFileMime(fileName)"
-       "        val doc = dir.createFile(mime, fileName)"
-       "            ?: throw IllegalStateException(unableToCreateFileMessage(fileName))"
+       "        val createdFile = dir.createFile(mime, fileName)"
+       "        if (!documentFilePresent(createdFile)) throw IllegalStateException(unableToCreateFileMessage(fileName))"
+       "        val doc = checkNotNull(createdFile)"
        "        val outputStream = context.contentResolver.openOutputStream(doc.uri)"
        "        if (!outputStreamPresent(outputStream)) return"
        "        checkNotNull(outputStream).use { output ->"