From 37a40a4665e9119e49de9b4b58432dc1b3ed5f2a Mon Sep 17 00:00:00 2001 From: grimsi <9295182+grimsi@users.noreply.github.com> Date: Thu, 12 Jun 2025 10:53:30 +0200 Subject: [PATCH] Only add each torrent once to tracker --- .../torrentdownload/TorrentDownloadPlugin.kt | 31 +++++++++++++------ .../gameyfinplugins/torrentdownload/Util.kt | 15 +++++++++ .../src/main/resources/MANIFEST.MF | 2 +- 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/Util.kt diff --git a/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/TorrentDownloadPlugin.kt b/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/TorrentDownloadPlugin.kt index 1be98e8..620a3cd 100644 --- a/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/TorrentDownloadPlugin.kt +++ b/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/TorrentDownloadPlugin.kt @@ -16,12 +16,14 @@ import de.grimsi.gameyfin.pluginapi.download.DownloadProvider import de.grimsi.gameyfin.pluginapi.download.FileDownload import org.pf4j.Extension import org.pf4j.PluginWrapper +import org.slf4j.LoggerFactory import java.net.InetAddress import java.net.URI import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.Executors import kotlin.io.path.* +import kotlin.time.measureTimedValue class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wrapper) { @@ -146,15 +148,16 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin @Extension class TorrentDownloadProvider : DownloadProvider { - override fun download(path: Path): Download { - val torrentFile = createTorrentFile(path) + private val log = LoggerFactory.getLogger(TorrentDownloadProvider::class.java) - tracker.announce(TrackedTorrent.load(torrentFile.toFile())) - communicationManager.addTorrent( - torrentFile.toString(), - getRootPath(path).toString(), - FullyPieceStorageFactory.INSTANCE - ) + override fun download(path: Path): Download { + log.info("Creating torrent for '${path.name}'...") + + val (torrentFile, timeTaken) = measureTimedValue { + createTorrent(path) + } + + log.info("Created torrent '${torrentFile.name}' in ${timeTaken.asHumanReadable()}") return FileDownload( data = torrentFile.inputStream(), @@ -163,7 +166,7 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin ) } - private fun createTorrentFile(gameFilesPath: Path): Path { + private fun createTorrent(gameFilesPath: Path): Path { val torrentFile = TORRENT_FILE_DIRECTORY.resolve("${gameFilesPath.nameWithoutExtension}-${gameFilesPath.hashCode()}.torrent") @@ -173,6 +176,14 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin Files.createFile(torrentFile) Files.write(torrentFile, torrentFileContent(gameFilesPath)) + + tracker.announce(TrackedTorrent.load(torrentFile.toFile())) + communicationManager.addTorrent( + torrentFile.toString(), + getRootPath(gameFilesPath).toString(), + FullyPieceStorageFactory.INSTANCE + ) + return torrentFile } @@ -195,4 +206,4 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin } } } -} +} \ No newline at end of file diff --git a/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/Util.kt b/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/Util.kt new file mode 100644 index 0000000..7e2f88d --- /dev/null +++ b/plugins/torrentdownload/src/main/kotlin/de/grimsi/gameyfinplugins/torrentdownload/Util.kt @@ -0,0 +1,15 @@ +package de.grimsi.gameyfinplugins.torrentdownload + +import kotlin.time.Duration + + +fun Duration.asHumanReadable(): String { + return this.toComponents { days, hours, minutes, seconds, _ -> + buildString { + if (days > 0) append("${days}d ") + if (hours > 0) append("${hours}h ") + if (minutes > 0 || hours > 0) append("${minutes}m ") + append("${seconds}s") + }.trim() + } +} \ No newline at end of file diff --git a/plugins/torrentdownload/src/main/resources/MANIFEST.MF b/plugins/torrentdownload/src/main/resources/MANIFEST.MF index e829e14..15b0b6f 100644 --- a/plugins/torrentdownload/src/main/resources/MANIFEST.MF +++ b/plugins/torrentdownload/src/main/resources/MANIFEST.MF @@ -1,4 +1,4 @@ -Plugin-Version: 1.0.0-alpha1 +Plugin-Version: 1.0.0-alpha2 Plugin-Class: de.grimsi.gameyfinplugins.torrentdownload.TorrentDownloadPlugin Plugin-Id: de.grimsi.gameyfinplugins.torrentdownload Plugin-Name: Torrent Download