Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/core/web.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ On JS and Wasm, a `PlatformFile` wraps a browser handle, not a path. Files the u

## What a web PlatformFile is

| Source | Wraps | Read | Write, delete | List |
| --- | --- | --- | --- | --- |
| File picker | A snapshot of a browser `File` | Yes | No | No |
| Directory picker | A virtual tree of the selected files | Files only | No | `list()`, `walk()`, `listEntries()` |
| Web file saver | A browser save destination | Yes | Yes | No |
| `FileKit.originPrivateDir` (experimental) | A live origin private file system entry | Yes | Yes | `listEntries()` |
| `PlatformFile(blob, name)` | A browser `File` made from a `Blob` or `File` | Yes | No | No |
| Source | Wraps | Read | Write | Delete | List |
| --- | --- | --- | --- | --- | --- |
| File picker | A snapshot of a browser `File` | Yes | No | No | No |
| Directory picker | A virtual tree of the selected files | Files only | No | No | `list()`, `walk()`, `listEntries()` |
| Web file saver | A browser save destination | Yes | Yes | No | No |
| `FileKit.originPrivateDir` (experimental) | A live origin private file system entry | Yes | Yes | Yes | `listEntries()` |
| `PlatformFile(blob, name)` | A browser `File` made from a `Blob` or `File` | Yes | No | No | No |

The `path` of a picked file is its name, or for a file inside a picked folder, its relative path starting with the folder's name. Path constructors such as `PlatformFile("/path")`, `FileKit.filesDir`, and `exists()` don't exist on the web: keep that code in `nonWebMain`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public suspend fun FileKit.openFileSaver(
): PlatformFile? {
val fileName = buildFileSaverSuggestedName(suggestedName, defaultExtension)
val extensions = buildFileSaverAllowedFileTypes(defaultExtension, allowedExtensions)
?.filter { it.length + 1 <= MAX_BROWSER_FILE_TYPE_SUFFIX_LENGTH }
?.joinToString(separator = "\u0000")
.orEmpty()
val result = invokeBrowserSavePicker(fileName, extensions, browserSavePickerTestOverride).await()
Expand All @@ -49,7 +50,7 @@ public suspend fun FileKit.openFileSaver(
}

"selected" -> {
createSaveDestination(handle = result.handle, name = fileName)
createSaveDestination(handle = result.handle, name = result.name)
}

"error" -> {
Expand All @@ -68,10 +69,13 @@ public suspend fun FileKit.openFileSaver(
}
}

private const val MAX_BROWSER_FILE_TYPE_SUFFIX_LENGTH = 16

@OptIn(ExperimentalWasmJsInterop::class)
private external interface BrowserSavePickerResult : JsAny {
val kind: String
val handle: JsAny?
val name: String
val errorName: String
val errorMessage: String
}
Expand Down Expand Up @@ -113,7 +117,7 @@ private fun invokeBrowserSavePicker(

try {
crossOriginFrame = window.self !== window.top &&
window.parent.location.origin !== window.location.origin;
window.top.location.origin !== window.location.origin;
} catch (_) {
crossOriginFrame = true;
}
Expand Down Expand Up @@ -147,7 +151,7 @@ private fun invokeBrowserSavePicker(
}

return Promise.resolve(api.call(globalThis, options)).then(
function(handle) { return { kind: "selected", handle: handle }; },
function(handle) { return { kind: "selected", handle: handle, name: handle.name }; },
handleError
);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ class FileSaverWebTest {
val file = FileKit.openFileSaver(
suggestedName = "report",
defaultExtension = "pdf",
allowedExtensions = setOf("txt", ".pdf"),
allowedExtensions = setOf("txt", ".pdf", "sublime-workspace"),
)
val selectedFile = assertNotNull(file)
val destination = assertIs<WebFile.SaveDestination>(selectedFile.webFile)

assertEquals("report.pdf", selectedFile.name)
assertEquals("selected.txt", selectedFile.name)
assertEquals("txt", selectedFile.extension)
assertEquals(false, destination.isDownload)
assertEquals("report.pdf", lastPickerSuggestedName())
assertEquals(
Expand All @@ -162,6 +163,19 @@ class FileSaverWebTest {
}
}

@Test
fun openFileSaver_whenAllExtensionsExceedBrowserLimit_omitsPickerTypes() = runTest {
withPickerOverride(selectedPickerOverride()) {
FileKit.openFileSaver(
suggestedName = "report",
defaultExtension = "sublime-workspace",
)

assertEquals("report.sublime-workspace", lastPickerSuggestedName())
assertFalse(hasPickerTypes())
}
}

@Test
fun openFileSaver_withoutExtensions_omitsPickerTypes() = runTest {
withPickerOverride(selectedPickerOverride()) {
Expand Down
Loading