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 de.grimsi.gameyfin.pluginapi.download.FileDownload
import org.pf4j.Extension import org.pf4j.Extension
import org.pf4j.PluginWrapper import org.pf4j.PluginWrapper
import org.slf4j.LoggerFactory
import java.net.InetAddress import java.net.InetAddress
import java.net.URI import java.net.URI
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.io.path.* import kotlin.io.path.*
import kotlin.time.measureTimedValue
class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wrapper) { class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin(wrapper) {
@@ -146,15 +148,16 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
@Extension @Extension
class TorrentDownloadProvider : DownloadProvider { class TorrentDownloadProvider : DownloadProvider {
override fun download(path: Path): Download { private val log = LoggerFactory.getLogger(TorrentDownloadProvider::class.java)
val torrentFile = createTorrentFile(path)
tracker.announce(TrackedTorrent.load(torrentFile.toFile())) override fun download(path: Path): Download {
communicationManager.addTorrent( log.info("Creating torrent for '${path.name}'...")
torrentFile.toString(),
getRootPath(path).toString(), val (torrentFile, timeTaken) = measureTimedValue {
FullyPieceStorageFactory.INSTANCE createTorrent(path)
) }
log.info("Created torrent '${torrentFile.name}' in ${timeTaken.asHumanReadable()}")
return FileDownload( return FileDownload(
data = torrentFile.inputStream(), 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 = val torrentFile =
TORRENT_FILE_DIRECTORY.resolve("${gameFilesPath.nameWithoutExtension}-${gameFilesPath.hashCode()}.torrent") TORRENT_FILE_DIRECTORY.resolve("${gameFilesPath.nameWithoutExtension}-${gameFilesPath.hashCode()}.torrent")
@@ -173,6 +176,14 @@ class TorrentDownloadPlugin(wrapper: PluginWrapper) : ConfigurableGameyfinPlugin
Files.createFile(torrentFile) Files.createFile(torrentFile)
Files.write(torrentFile, torrentFileContent(gameFilesPath)) Files.write(torrentFile, torrentFileContent(gameFilesPath))
tracker.announce(TrackedTorrent.load(torrentFile.toFile()))
communicationManager.addTorrent(
torrentFile.toString(),
getRootPath(gameFilesPath).toString(),
FullyPieceStorageFactory.INSTANCE
)
return torrentFile return torrentFile
} }
@@ -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-Class: de.grimsi.gameyfinplugins.torrentdownload.TorrentDownloadPlugin
Plugin-Id: de.grimsi.gameyfinplugins.torrentdownload Plugin-Id: de.grimsi.gameyfinplugins.torrentdownload
Plugin-Name: Torrent Download Plugin-Name: Torrent Download