Only add each torrent once to tracker

This commit is contained in:
grimsi
2025-06-12 10:53:30 +02:00
parent d3759b8865
commit 37a40a4665
3 changed files with 37 additions and 11 deletions
@@ -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
}
}
}
}
}
@@ -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()
}
}
@@ -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