fun downloadTorrent()

in backend/src/main/kotlin/org/jetbrains/downloadThis/Torrent.kt [32:50]


fun downloadTorrent(torrentFile: File, destinationDir: File, statusListener: (DownloadStatus) -> Unit) {
    try {
        if (!destinationDir.exists()) {
            require(destinationDir.mkdirs()) { "Can't make dirs" }
        }
        val address = InetAddress.getLocalHost()
        client.downloadTorrent(torrentFile.absolutePath, destinationDir.absolutePath, address)
        client.stop()
    } catch (e: Exception) {
        statusListener(Failed("Can't download torrent", torrentFile.absolutePath, destinationDir))
        return
    }

    statusListener(Finished(
            destinationFile = destinationDir.absolutePath,
            query = torrentFile.absolutePath,
            destinationDir = destinationDir
    ))
}