API: torrents upload/list

This commit is contained in:
xiaomlove
2025-04-17 01:39:40 +07:00
parent 0d3a46231d
commit 2b029eba10
72 changed files with 2332 additions and 507 deletions

View File

@@ -3,14 +3,21 @@
namespace App\Http\Resources;
use App\Models\Attachment;
use App\Models\SearchBox;
use App\Models\Torrent;
use App\Repositories\TorrentRepository;
use Carbon\CarbonInterface;
use Elasticsearch\Endpoints\Search;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;
use Nexus\Nexus;
use Illuminate\Http\Request;
class TorrentResource extends JsonResource
class TorrentResource extends BaseResource
{
protected $imageTypes = ['image', 'attachment'];
const NAME = 'torrent';
protected static TorrentRepository $torrentRep;
/**
* Transform the resource into an array.
@@ -24,72 +31,82 @@ class TorrentResource extends JsonResource
'id' => $this->id,
'name' => $this->name,
'filename' => $this->filename,
'hash' => preg_replace_callback('/./s', [$this, "hex_esc"], $this->info_hash),
'cover' => $this->cover,
'small_descr' => $this->small_descr,
'comments' => $this->comments,
'category' => $this->category,
'category_info' => new CategoryResource($this->whenLoaded('basic_category')),
'size' => $this->size,
'size_human' => mksize($this->size),
'added' => $this->added->toDateTimeString(),
'added_human' => $this->added->format('Y-m-d H:i'),
'ttl' => $this->added->diffForHumans(['syntax' => CarbonInterface::DIFF_ABSOLUTE]),
'leechers' => $this->leechers,
'seeders' => $this->seeders,
'times_completed' => $this->times_completed,
'numfiles' => $this->numfiles,
'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,
'promotion_info' => $this->promotionInfo,
'hr' => $this->hr,
'hr' => $this->hr ?: 0,
'pick_type' => $this->picktype,
'pick_time' => $this->picktime,
'pick_info' => $this->pickInfo,
'download_url' => $this->download_url,
'user' => new UserResource($this->whenLoaded('user')),
'anonymous' => $this->anonymous,
'basic_category' => new CategoryResource($this->whenLoaded('basic_category')),
'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')),
'thanks' => ThankResource::collection($this->whenLoaded('thanks')),
'reward_logs' => RewardResource::collection($this->whenLoaded('reward_logs')),
];
if ($this->cover) {
$cover = $this->cover;
} else {
$descriptionArr = format_description($this->descr);
$cover = get_image_from_description($descriptionArr, true);
}
$out['cover'] = resize_image($cover, 100, 100);
if ($request->routeIs('torrents.show')) {
if (!isset($descriptionArr)) {
$descriptionArr = format_description($this->descr);
$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 ?? '',
];
}
$baseInfo = [
['label' => nexus_trans('torrent.show.size'), 'value' => mksize($this->size)],
];
foreach (Torrent::getBasicInfo() as $relation => $text) {
if ($info = $this->whenLoaded($relation)) {
$baseInfo[] = ['label' => $text, 'value' => $info->name];
}
}
$out['base_info'] = $baseInfo;
$out['description'] = $descriptionArr;
$out['images'] = get_image_from_description($descriptionArr);
$out['thank_users_count'] = $this->thank_users_count;
$out['peers_count'] = $this->peers_count;
$out['reward_logs_count'] = $this->reward_logs_count;
}
if (nexus()->isPlatformAdmin()) {
$out['details_url'] = sprintf('%s/details.php?id=%s', getSchemeAndHttpHost(), $this->id);
}
// $out['upload_peers_count'] = $this->upload_peers_count;
// $out['download_peers_count'] = $this->download_peers_count;
// $out['finish_peers_count'] = $this->finish_peers_count;
$out['sub_categories'] = empty($subCategories) ? null : $subCategories;
return $out;
}
protected function getResourceName(): string
{
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]));
}
}