diff --git a/app/Http/Resources/TorrentResource.php b/app/Http/Resources/TorrentResource.php index 5c53b073..cf985aac 100644 --- a/app/Http/Resources/TorrentResource.php +++ b/app/Http/Resources/TorrentResource.php @@ -70,6 +70,7 @@ class TorrentResource extends BaseResource 'description' => $this->whenHas('description'), 'images' => $this->whenHas('images'), 'download_url' => $this->whenHas('download_url'), + 'active_status' => $this->whenHas('active_status'), 'user' => new UserResource($this->whenLoaded('user')), 'extra' => new TorrentExtraResource($this->whenLoaded('extra')), 'tags' => TagResource::collection($this->whenLoaded('tags')), @@ -97,14 +98,6 @@ class TorrentResource extends BaseResource return self::NAME; } - private function getTorrentRep(): TorrentRepository - { - if (!isset(self::$torrentRep)) { - self::$torrentRep = new TorrentRepository(); - } - return self::$torrentRep; - } - protected function hex_esc($matches) { return sprintf("%02x", ord($matches[0])); } diff --git a/app/Repositories/ToolRepository.php b/app/Repositories/ToolRepository.php index 87a61255..e4fb5964 100644 --- a/app/Repositories/ToolRepository.php +++ b/app/Repositories/ToolRepository.php @@ -153,6 +153,8 @@ class ToolRepository extends BaseRepository $result_code = 0; do_log("No tar command, use zip."); } + File::delete($backupWeb['filename']); + File::delete($backupDatabase['filename']); if (!$transfer) { return compact('result_code', 'filename'); } @@ -216,7 +218,7 @@ class ToolRepository extends BaseRepository $transferResult = $this->transfer($backupResult['filename'], $backupResult['result_code'], $setting); $backupResult['transfer_result'] = $transferResult; do_log("[BACKUP_ALL_DONE]: " . json_encode($backupResult)); - $this->cleanupBackupFiles(); + $this->cleanupBackupFiles(basename($backupResult['filename'])); return $backupResult; } @@ -336,20 +338,26 @@ class ToolRepository extends BaseRepository } } - private function cleanupBackupFiles(): void + private function cleanupBackupFiles($basename): void { + $nameParts = explode('.', $basename); + $firstPart = $nameParts[0]; + $lastPart = $nameParts[count($nameParts) - 1]; $retentionCount = Setting::getBackupRetentionCount(); if ($retentionCount <= 0) { $retentionCount = self::BACKUP_RETENTION_COUNT_DEFAULT; } $path = self::getBackupExportPath(); - $allFiles = collect(File::allFiles($path)); + $allFiles = collect(File::allFiles($path))->filter(function (\Symfony\Component\Finder\SplFileInfo $file) use ($firstPart, $lastPart) { + $name = basename($file->getRealPath()); + return str_starts_with($name, $firstPart) && str_ends_with($name, $lastPart); + }); // 按创建时间降序排序 $allFiles = $allFiles->sortByDesc(fn (\Symfony\Component\Finder\SplFileInfo $file) => $file->getCTime()); $filesToDelete = $allFiles->slice($retentionCount); do_log(sprintf( - "retentionCount: %s, path: %s, fileCount: %s, toDeleteCount: %s", - $retentionCount, $path, $allFiles->count(), $filesToDelete->count() + "retentionCount: %s, path: %s, fileCount: %s", + $retentionCount, $path, $allFiles->count() )); foreach ($filesToDelete as $file) { $realPath = $file->getRealPath(); diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index c446bb24..b3c4c37a 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -62,7 +62,28 @@ class TorrentRepository extends BaseRepository const BUY_STATUS_NOT_YET = -1; const BUY_STATUS_UNKNOWN = -2; + private static array $defaultLoadRelationships = [ + 'basic_category', 'basic_category.search_box', + 'basic_audiocodec', 'basic_codec', 'basic_medium', + 'basic_source', 'basic_processing', 'basic_standard', 'basic_team', + ]; + private static array $allowIncludes = ['user', 'extra', 'tags']; + + private static array $allowIncludeCounts = ['thank_users', 'reward_logs', 'claims']; + + private static array $allowIncludeFields = [ + 'has_bookmarked', 'has_claimed', 'has_thanked', 'has_rewarded', + 'description', 'download_url', 'active_status' + ]; + + private static array $allowFilters = [ + 'title', 'category', 'source', 'medium', 'codec', 'audiocodec', 'standard', 'processing', 'team', + 'owner', 'visible', 'added', 'size', 'sp_state', 'leechers', 'seeders', 'times_completed', + 'bookmark', + ]; + + private static array $allowSorts = ['id', 'comments', 'size', 'seeders', 'leechers', 'times_completed']; /** * fetch torrent list @@ -83,31 +104,15 @@ class TorrentRepository extends BaseRepository } $categoryIdList = $searchBox->categories()->pluck('id')->toArray(); //query this info default - $query = Torrent::query()->with([ - 'basic_category', 'basic_category.search_box', - 'basic_audiocodec', 'basic_codec', 'basic_medium', - 'basic_source', 'basic_processing', 'basic_standard', 'basic_team', - ]) + $query = Torrent::query()->with(self::$defaultLoadRelationships) ->whereIn('category', $categoryIdList) ->orderBy("pos_state", "DESC"); - $allowIncludes = ['user', 'extra', 'tags']; - $allowIncludeCounts = ['thank_users', 'reward_logs', 'claims']; - $allowIncludeFields = [ - 'has_bookmarked', 'has_claimed', 'has_thanked', 'has_rewarded', - 'description', 'download_url' - ]; - $allowFilters = [ - 'title', 'category', 'source', 'medium', 'codec', 'audiocodec', 'standard', 'processing', 'team', - 'owner', 'visible', 'added', 'size', 'sp_state', 'leechers', 'seeders', 'times_completed', - 'bookmark', - ]; - $allowSorts = ['id', 'comments', 'size', 'seeders', 'leechers', 'times_completed']; $apiQueryBuilder = ApiQueryBuilder::for(TorrentResource::NAME, $query, $request) - ->allowIncludes($allowIncludes) - ->allowIncludeCounts($allowIncludeCounts) - ->allowIncludeFields($allowIncludeFields) - ->allowFilters($allowFilters) - ->allowSorts($allowSorts) + ->allowIncludes(self::$allowIncludes) + ->allowIncludeCounts(self::$allowIncludeCounts) + ->allowIncludeFields(self::$allowIncludeFields) + ->allowFilters(self::$allowFilters) + ->allowSorts(self::$allowSorts) ->registerCustomFilter('title', function (Builder $query, Request $request) { $title = $request->input(ApiQueryBuilder::PARAM_NAME_FILTER.".title"); $title = trim(str_replace('.', '', $title)); @@ -161,21 +166,11 @@ class TorrentRepository extends BaseRepository public function getDetail($id, Authenticatable $user) { //query this info default - $query = Torrent::query()->with([ - 'basic_category', 'basic_category.search_box', - 'basic_audiocodec', 'basic_codec', 'basic_medium', - 'basic_source', 'basic_processing', 'basic_standard', 'basic_team', - ]); - $allowIncludes = ['user', 'extra', 'tags']; - $allowIncludeCounts = ['thank_users', 'reward_logs', 'claims']; - $allowIncludeFields = [ - 'has_bookmarked', 'has_claimed', 'has_thanked', 'has_rewarded', - 'description', 'download_url' - ]; + $query = Torrent::query()->with(self::$defaultLoadRelationships); $apiQueryBuilder = ApiQueryBuilder::for(TorrentResource::NAME, $query) - ->allowIncludes($allowIncludes) - ->allowIncludeCounts($allowIncludeCounts) - ->allowIncludeFields($allowIncludeFields) + ->allowIncludes(self::$allowIncludes) + ->allowIncludeCounts(self::$allowIncludeCounts) + ->allowIncludeFields(self::$allowIncludeFields) ; do_log("before query torrent detail"); $torrent = $apiQueryBuilder->build()->findOrFail($id); @@ -186,7 +181,7 @@ class TorrentRepository extends BaseRepository private function appendIncludeFields(ApiQueryBuilder $apiQueryBuilder, Authenticatable $user, $torrentList) { - $torrentIdArr = $bookmarkData = $claimData = $thankData = $rewardData =[]; + $torrentIdArr = $bookmarkData = $claimData = $thankData = $rewardData = $activeData = []; foreach ($torrentList as $torrent) { $torrentIdArr[] = $torrent->id; } @@ -203,6 +198,10 @@ class TorrentRepository extends BaseRepository if ($hasFieldHasRewarded = $apiQueryBuilder->hasIncludeField('has_rewarded')) { $rewardData = $user->reward_torrent_logs()->whereIn('torrentid', $torrentIdArr)->get()->keyBy('torrentid'); } + if ($hasFieldActiveStatus = $apiQueryBuilder->hasIncludeField('active_status')) { + $torrentModule = new \Nexus\Torrent\Torrent(); + $activeData = $torrentModule->listLeechingSeedingStatus($user->id, $torrentIdArr); + } do_log("after prepare has data"); foreach ($torrentList as $torrent) { @@ -219,6 +218,9 @@ class TorrentRepository extends BaseRepository if ($hasFieldHasRewarded) { $torrent->has_rewarded = $rewardData->has($id); } + if ($hasFieldActiveStatus) { + $torrent->active_status = $activeData[$id] ?? null; + } if ($apiQueryBuilder->hasIncludeField('description') && $apiQueryBuilder->hasInclude('extra')) { $descriptionArr = format_description($torrent->extra->descr ?? ''); diff --git a/include/constants.php b/include/constants.php index 93cf3442..8398bb3c 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ $row['to_go'] == 0 ? 'yes' : 'no', - 'progress' => $progress, + 'progress' => floatval($progress), 'active_status' => $activeStatus, ]; } diff --git a/public/announce.php b/public/announce.php index 4f717667..b0478f18 100644 --- a/public/announce.php +++ b/public/announce.php @@ -135,10 +135,7 @@ $isDonor = is_donor($az); $az['__is_donor'] = $isDonor; $log = "user: $userid, isDonor: $isDonor, seeder: $seeder, ip: $ip, ipv4: $ipv4, ipv6: $ipv6"; //check tracker url -$trackerUrl = \App\Models\TrackerUrl::getById($az['tracker_url_id']); -if ($trackerUrl === false) { - warn("please select tracker url at user control panel"); -} +$trackerUrl = get_tracker_schema_and_host($az['tracker_url_id'], true); $currentUrl = getSchemeAndHttpHost(); if (!str_contains($trackerUrl, $currentUrl)) { do_log("announce check tracker url, trackerUrl: $trackerUrl does not contains: $currentUrl");