Files
nexusphp/app/Http/Resources/TorrentResource.php

113 lines
4.2 KiB
PHP
Raw Normal View History

2021-05-15 19:29:44 +08:00
<?php
namespace App\Http\Resources;
use App\Models\Attachment;
2025-04-17 01:39:40 +07:00
use App\Models\SearchBox;
2021-05-18 02:37:39 +08:00
use App\Models\Torrent;
2025-04-17 01:39:40 +07:00
use App\Repositories\TorrentRepository;
2021-05-15 19:29:44 +08:00
use Carbon\CarbonInterface;
2025-04-17 01:39:40 +07:00
use Elasticsearch\Endpoints\Search;
2021-05-15 19:29:44 +08:00
use Illuminate\Http\Resources\Json\JsonResource;
2025-04-17 01:39:40 +07:00
use Illuminate\Support\Facades\Auth;
2022-04-17 16:38:44 +08:00
use Nexus\Nexus;
2025-04-17 01:39:40 +07:00
use Illuminate\Http\Request;
2021-05-15 19:29:44 +08:00
2025-04-17 01:39:40 +07:00
class TorrentResource extends BaseResource
2021-05-15 19:29:44 +08:00
{
2025-04-17 01:39:40 +07:00
const NAME = 'torrent';
protected static TorrentRepository $torrentRep;
2021-05-15 19:29:44 +08:00
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$out = [
'id' => $this->id,
'name' => $this->name,
'filename' => $this->filename,
2025-04-17 01:39:40 +07:00
'hash' => preg_replace_callback('/./s', [$this, "hex_esc"], $this->info_hash),
'cover' => $this->cover,
2021-05-15 19:29:44 +08:00
'small_descr' => $this->small_descr,
2025-04-17 01:39:40 +07:00
'category' => $this->category,
'category_info' => new CategoryResource($this->whenLoaded('basic_category')),
'size' => $this->size,
2021-05-15 19:29:44 +08:00
'size_human' => mksize($this->size),
2025-04-17 01:39:40 +07:00
'added' => format_datetime($this->added),
'added_human' => gettime($this->added),
'numfiles' => $this->numfiles ?: 0,
'leechers' => $this->leechers ?: 0,
'seeders' => $this->seeders ?: 0,
'times_completed' => $this->times_completed ?: 0,
'views' => $this->views ?: 0,
'hits' => $this->hits ?: 0,
'comments' => $this->comments ?: 0,
'pos_state' => $this->pos_state,
'pos_state_until' => format_datetime($this->pos_state_until),
'pos_state_until_human' => gettime($this->pos_state_until),
'sp_state' => $this->sp_state,
'sp_state_real' => $this->sp_state_real,
2022-03-31 16:28:08 +08:00
'promotion_info' => $this->promotionInfo,
2025-04-17 01:39:40 +07:00
'hr' => $this->hr ?: 0,
'pick_type' => $this->picktype,
'pick_time' => $this->picktime,
2022-03-31 16:28:08 +08:00
'pick_info' => $this->pickInfo,
'anonymous' => $this->anonymous,
2025-04-17 01:39:40 +07:00
'last_action' => format_datetime($this->last_action),
'last_action_human' => gettime($this->last_action),
'thank_users_count' => $this->whenCounted('thank_users'),
'reward_logs_count' => $this->whenCounted('reward_logs'),
'claims_count' => $this->whenCounted('claims'),
'has_bookmarked' => $this->whenHas('has_bookmarked'),
'has_claimed' => $this->whenHas('has_claimed'),
'has_thanked' => $this->whenHas('has_thanked'),
'has_rewarded' => $this->whenHas('has_rewarded'),
'description' => $this->whenHas('description'),
'images' => $this->whenHas('images'),
'download_url' => $this->whenHas('download_url'),
'user' => new UserResource($this->whenLoaded('user')),
'extra' => new TorrentExtraResource($this->whenLoaded('extra')),
'tags' => TagResource::collection($this->whenLoaded('tags')),
2022-03-30 15:37:11 +08:00
'thanks' => ThankResource::collection($this->whenLoaded('thanks')),
'reward_logs' => RewardResource::collection($this->whenLoaded('reward_logs')),
2021-05-15 19:29:44 +08:00
];
2025-04-17 01:39:40 +07:00
$subCategories = [];
foreach (SearchBox::$taxonomies as $field => $info) {
$relation = "basic_$field";
if ($this->resource->{$field} > 0 && $this->resource->relationLoaded($relation)) {
$subCategories[$field] = [
'label' => $this->resource->getSubCategoryLabel($field),
'value' => $this->resource->{$relation}->name ?? '',
];
2022-05-03 23:58:27 +08:00
}
2022-04-17 16:38:44 +08:00
}
2025-04-17 01:39:40 +07:00
$out['sub_categories'] = empty($subCategories) ? null : $subCategories;
return $out;
2022-04-17 16:38:44 +08:00
2025-04-17 01:39:40 +07:00
}
2021-05-15 19:29:44 +08:00
2025-04-17 01:39:40 +07:00
protected function getResourceName(): string
{
return self::NAME;
2021-05-15 19:29:44 +08:00
}
2025-04-17 01:39:40 +07:00
private function getTorrentRep(): TorrentRepository
{
if (!isset(self::$torrentRep)) {
self::$torrentRep = new TorrentRepository();
}
return self::$torrentRep;
}
2021-05-15 19:29:44 +08:00
2025-04-17 01:39:40 +07:00
protected function hex_esc($matches) {
return sprintf("%02x", ord($matches[0]));
}
2021-05-15 19:29:44 +08:00
}