From e7c369002ee6af2e4f95234cecd2ca66ad695d83 Mon Sep 17 00:00:00 2001 From: Roardom Date: Sun, 1 Dec 2024 19:51:20 +0000 Subject: [PATCH] fix: order torrent creation from most to least important --- .../Controllers/API/TorrentController.php | 28 +++++---- app/Http/Controllers/TorrentController.php | 60 ++++++++++--------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/API/TorrentController.php b/app/Http/Controllers/API/TorrentController.php index f8b4a30db5..2618d1a205 100644 --- a/app/Http/Controllers/API/TorrentController.php +++ b/app/Http/Controllers/API/TorrentController.php @@ -323,10 +323,17 @@ public function store(Request $request): \Illuminate\Http\JsonResponse // Populate the status/seeders/leechers/times_completed fields for the external tracker $torrent->refresh(); - Unit3dAnnounce::addTorrent($torrent); + // Backup the files contained in the torrent + $files = TorrentTools::getTorrentFiles($decodedTorrent); - if ($torrent->getAttribute('featured')) { - Unit3dAnnounce::addFeaturedTorrent($torrent->id); + foreach ($files as &$file) { + $file['torrent_id'] = $torrent->id; + } + + // Can't insert them all at once since some torrents have more files than mysql supports placeholders. + // Divide by 3 since we're inserting 3 fields: name, size and torrent_id + foreach (collect($files)->chunk(intdiv(65_000, 3)) as $files) { + TorrentFile::insert($files->toArray()); } // Set torrent to featured @@ -337,19 +344,16 @@ public function store(Request $request): \Illuminate\Http\JsonResponse $featuredTorrent->save(); } - // Backup the files contained in the torrent - $files = TorrentTools::getTorrentFiles($decodedTorrent); + // Tracker updates come after database updates in case tracker's offline - foreach ($files as &$file) { - $file['torrent_id'] = $torrent->id; - } + Unit3dAnnounce::addTorrent($torrent); - // Can't insert them all at once since some torrents have more files than mysql supports placeholders. - // Divide by 3 since we're inserting 3 fields: name, size and torrent_id - foreach (collect($files)->chunk(intdiv(65_000, 3)) as $files) { - TorrentFile::insert($files->toArray()); + if ($torrent->getAttribute('featured')) { + Unit3dAnnounce::addFeaturedTorrent($torrent->id); } + // TMDB updates come after tracker updates in case TMDB's offline + $tmdbScraper = new TMDBScraper(); if ($torrent->category->tv_meta && $torrent->tmdb) { diff --git a/app/Http/Controllers/TorrentController.php b/app/Http/Controllers/TorrentController.php index 3050088cad..52f8121e6d 100644 --- a/app/Http/Controllers/TorrentController.php +++ b/app/Http/Controllers/TorrentController.php @@ -415,12 +415,6 @@ public function store(StoreTorrentRequest $request): \Illuminate\Http\RedirectRe // Populate the status/seeders/leechers/times_completed fields for the external tracker $torrent->refresh(); - Unit3dAnnounce::addTorrent($torrent); - - if ($torrent->getAttribute('featured')) { - Unit3dAnnounce::addFeaturedTorrent($torrent->id); - } - $category = Category::findOrFail($request->integer('category_id')); // Backup the files contained in the torrent @@ -436,6 +430,38 @@ public function store(StoreTorrentRequest $request): \Illuminate\Http\RedirectRe TorrentFile::insert($files->toArray()); } + // Cover Image for No-Meta Torrents + if ($request->hasFile('torrent-cover')) { + $image_cover = $request->file('torrent-cover'); + + abort_if(\is_array($image_cover), 400); + + $filename_cover = 'torrent-cover_'.$torrent->id.'.jpg'; + $path_cover = public_path('/files/img/'.$filename_cover); + Image::make($image_cover->getRealPath())->fit(400, 600)->encode('jpg', 90)->save($path_cover); + } + + // Banner Image for No-Meta Torrents + if ($request->hasFile('torrent-banner')) { + $image_cover = $request->file('torrent-banner'); + + abort_if(\is_array($image_cover), 400); + + $filename_cover = 'torrent-banner_'.$torrent->id.'.jpg'; + $path_cover = public_path('/files/img/'.$filename_cover); + Image::make($image_cover->getRealPath())->fit(960, 540)->encode('jpg', 90)->save($path_cover); + } + + // Tracker updates come after initial database updates in case tracker's offline + + Unit3dAnnounce::addTorrent($torrent); + + if ($torrent->getAttribute('featured')) { + Unit3dAnnounce::addFeaturedTorrent($torrent->id); + } + + // TMDB updates come after tracker updates in case TMDB's offline + // TMDB Meta if ($torrent->tmdb != 0) { switch (true) { @@ -461,28 +487,6 @@ public function store(StoreTorrentRequest $request): \Illuminate\Http\RedirectRe Keyword::upsert($keywords->toArray(), ['torrent_id', 'name']); } - // Cover Image for No-Meta Torrents - if ($request->hasFile('torrent-cover')) { - $image_cover = $request->file('torrent-cover'); - - abort_if(\is_array($image_cover), 400); - - $filename_cover = 'torrent-cover_'.$torrent->id.'.jpg'; - $path_cover = public_path('/files/img/'.$filename_cover); - Image::make($image_cover->getRealPath())->fit(400, 600)->encode('jpg', 90)->save($path_cover); - } - - // Banner Image for No-Meta Torrents - if ($request->hasFile('torrent-banner')) { - $image_cover = $request->file('torrent-banner'); - - abort_if(\is_array($image_cover), 400); - - $filename_cover = 'torrent-banner_'.$torrent->id.'.jpg'; - $path_cover = public_path('/files/img/'.$filename_cover); - Image::make($image_cover->getRealPath())->fit(960, 540)->encode('jpg', 90)->save($path_cover); - } - // check for trusted user and update torrent if ($user->group->is_trusted && !$request->boolean('mod_queue_opt_in')) { $appurl = config('app.url');