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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.vinceglb.filekit

import io.github.vinceglb.filekit.mimeType.MimeType
import kotlinx.serialization.Serializable
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

/**
* Represents a file on a specific platform.
Expand Down Expand Up @@ -36,6 +38,14 @@ public expect val PlatformFile.nameWithoutExtension: String
*/
public expect fun PlatformFile.size(): Long

/**
* Returns the last modification time of this file.
*
* @return The [Instant] of the last modification.
*/
@OptIn(ExperimentalTime::class)
public expect fun PlatformFile.lastModified(): Instant

/**
* Reads the content of the file as a byte array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import kotlinx.serialization.Serializable
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.w3c.files.Blob
import org.w3c.files.File
import org.w3c.files.FilePropertyBag
import org.w3c.files.FileReader
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

/**
* Represents a file on the Web (JS) platform.
Expand Down Expand Up @@ -77,3 +81,20 @@ public actual suspend fun PlatformFile.readBytes(): ByteArray = withContext(Disp
public actual fun PlatformFile.mimeType(): MimeType? =
takeIf { file.type.isNotBlank() }
?.let { MimeType.parse(file.type) }

@OptIn(ExperimentalTime::class, ExperimentalWasmJsInterop::class)
public actual fun PlatformFile.lastModified(): Instant {
val ts = file.unsafeCast<io.github.vinceglb.filekit.File>()
return Instant.fromEpochMilliseconds(ts.lastModified.toLong())
}

@OptIn(ExperimentalWasmJsInterop::class)
private open external class File(
fileBits: JsArray<JsAny?>, // BufferSource|Blob|String
fileName: String,
options: FilePropertyBag = definedExternally,
) : Blob,
JsAny {
val name: String
val lastModified: JsNumber
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ public expect fun PlatformFile.exists(): Boolean
@OptIn(ExperimentalTime::class)
public expect fun PlatformFile.createdAt(): Instant?

/**
* Returns the last modification time of this file.
*
* @return The [Instant] of the last modification.
*/
@OptIn(ExperimentalTime::class)
public expect fun PlatformFile.lastModified(): Instant

public actual suspend fun PlatformFile.readBytes(): ByteArray =
withContext(Dispatchers.IO) {
this@readBytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import kotlinx.serialization.Serializable
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.w3c.files.Blob
import org.w3c.files.File
import org.w3c.files.FilePropertyBag
import org.w3c.files.FileReader
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

/**
* Represents a file on the Web (Wasm) platform.
Expand Down Expand Up @@ -79,3 +83,20 @@ public actual suspend fun PlatformFile.readBytes(): ByteArray = withContext(Disp
public actual fun PlatformFile.mimeType(): MimeType? =
takeIf { file.type.isNotBlank() }
?.let { MimeType.parse(file.type) }

@OptIn(ExperimentalTime::class, ExperimentalWasmJsInterop::class)
public actual fun PlatformFile.lastModified(): Instant {
val ts = file.unsafeCast<io.github.vinceglb.filekit.File>()
return Instant.fromEpochMilliseconds(ts.lastModified.toDouble().toLong())
}

@OptIn(ExperimentalWasmJsInterop::class)
private open external class File(
fileBits: JsArray<JsAny?>, // BufferSource|Blob|String
fileName: String,
options: FilePropertyBag = definedExternally,
) : Blob,
JsAny {
val name: String
val lastModified: JsNumber
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.vinceglb.filekit.sample.shared.ui.screens.filedetails.componen

import io.github.vinceglb.filekit.PlatformFile
import io.github.vinceglb.filekit.extension
import io.github.vinceglb.filekit.lastModified
import io.github.vinceglb.filekit.mimeType
import io.github.vinceglb.filekit.name
import io.github.vinceglb.filekit.nameWithoutExtension
Expand Down Expand Up @@ -29,4 +30,8 @@ internal actual fun PlatformFile.toMetadataItems(): List<FileMetadataItem> = lis
label = "Mime Type",
value = this.mimeType().toString(),
),
FileMetadataItem(
label = "Updated At",
value = this.lastModified().toString(),
),
)