Skip to content

acarlsen/kmp-mega

Repository files navigation

GitHub Workflow Status (with event) Maven Central Kotlin version License: MIT badge badge badge badge

Mega client SDK implemented for Kotlin Multiplatform. Basically a re-implementation of the go-mega library in Kotlin.

This library is currently focused on file access, so not all features available in official SDK's are available.

Features:

  • Login (also with 2FA)
  • Get quota
  • Fetch filesystem
  • Download file
  • Upload file
  • Create folder
  • Delete file/folder
  • Move file/folder
  • Rename file/folder

Whats missing:

  • Shared folder/file support not working (decryption fails)
  • Setting modified time of uploaded file
  • Link support
  • Get user support
  • Empty trash option

Platform Support

  • Android
  • Desktop
  • iOS
  • MacOS Native

To include in your project

Add the repository:

repositories {
    mavenCentral()
}

Put in your dependencies block:

implementation("dev.carlsen.mega:mega:1.0.0-beta08")

How to use

// Initialize the SDK
val mega = Mega()

// Log in to your MEGA account
mega.login("email@example.com", "password")

// Access your files
val fs = mega.getFileSystem()
val rootNode = fs.root
val files = mega.getChildren(rootNode!!)

// Download a file
val file = files.first { it.name == "example.pdf" }
SystemFileSystem.sink(Path("download.pdf")).use { fileOutputSink ->
    mega.downloadFile(
        src = file,
        fileOutputSink = ProgressCountingSink(
            delegate = fileOutputSink,
            totalBytes = file.size,
            onProgress = { b, t ->
                println("Downloaded $b of $t bytes")
            }
        ).buffered(),
        cancellationToken = CancellationToken.default()
    )
}

// Upload a file
val fileToUpload = Path("documents/report.pdf")
SystemFileSystem.source(fileToUpload).use { fileSource ->
    mega.uploadFile(
        destNode = rootNode,
        name = "uploaded-report.pdf",
        fileSize = fileSource.size(),
        fileInputSource = fileSource.buffered(),
        cancellationToken = CancellationToken.default()
    )
}

// Create a folder
val newFolder = mega.createFolder(rootNode, "My New Folder")

// Delete a node (with optional permanent deletion)
mega.delete(file, destroy = true)

// Enable logging
mega.logger.addListener(object : LogListener {
    override fun onLogMessage(level: LogLevel, message: String, throwable: Throwable?) {
        println("[$level] $message")
        throwable?.printStackTrace()
    }
})

// Cancel operations with cancellation token
val cancellationToken = CancellationToken()
try {
    mega.uploadFile(
        destNode = rootNode,
        name = "large-file.zip",
        fileSize = largeFileSource.size(),
        fileInputSource = largeFileSource,
        cancellationToken = cancellationToken
    )
} catch (e: Exception) {
    // Handle cancellation
}

// Call cancel() from another thread to stop the operation
cancellationToken.cancel()

// Logout when done
mega.logout()

About

MEGA client SDK library for Kotlin Multiplatform

Topics

Resources

License

Stars

7 stars

Watchers

2 watching

Forks

Contributors