diff --git a/src/main/java/org/araymond/joalcore/core/sharing/application/SharedTorrentService.java b/src/main/java/org/araymond/joalcore/core/sharing/application/SharedTorrentService.java index 94402612..26167b75 100644 --- a/src/main/java/org/araymond/joalcore/core/sharing/application/SharedTorrentService.java +++ b/src/main/java/org/araymond/joalcore/core/sharing/application/SharedTorrentService.java @@ -31,18 +31,7 @@ public SharedTorrentService(SharedTorrentRepository torrents, DomainEventPublish public void create(TorrentMetadata metadata) { var events = new ArrayList(); - var overallContributions = this.overallContributions.load(metadata.infoHash()) - .orElseGet(() -> { - Contribution overall = Contribution.ZERO; - if (config.skipDownload().get()) { - // return a fully Downloaded contribution when the torrent is not yet known and skip download is true - overall = new Contribution(new DownloadAmount(metadata.size().bytes()), new UploadAmount(0)); - } - this.overallContributions.save(metadata.infoHash(), overall); - - return overall; - }); - + var overallContributions = loadOverallContribution(metadata); var left = new Left(Math.max(metadata.size().bytes() - overallContributions.downloaded().bytes(), 0)); var torrent = new SharedTorrent(metadata.infoHash(), overallContributions, left); @@ -56,6 +45,20 @@ public void create(TorrentMetadata metadata) { publish(events); } + private Contribution loadOverallContribution(TorrentMetadata metadata) { + return this.overallContributions.load(metadata.infoHash()) + .orElseGet(() -> { + Contribution overall = Contribution.ZERO; + if (config.skipDownload().get()) { + // return a fully Downloaded contribution when the torrent is not yet known and skip download is true + overall = new Contribution(new DownloadAmount(metadata.size().bytes()), new UploadAmount(0)); + } + this.overallContributions.save(metadata.infoHash(), overall); + + return overall; + }); + } + public void registerPeers(InfoHash infoHash, Swarm.TrackerUniqueIdentifier identifier, Peers peers) { var torrent = torrents.findByTorrentInfoHash(infoHash).orElseThrow(() -> new UnknownSharedTorrentException("No torrent found for %s".formatted(infoHash)));