mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
swip admin torrent
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
class InsufficientPermissionException extends NexusException
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ use App\Models\Torrent;
|
|||||||
use App\Models\TorrentTag;
|
use App\Models\TorrentTag;
|
||||||
use App\Repositories\TagRepository;
|
use App\Repositories\TagRepository;
|
||||||
use App\Repositories\TorrentRepository;
|
use App\Repositories\TorrentRepository;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Resources\Form;
|
use Filament\Resources\Form;
|
||||||
use Filament\Resources\Resource;
|
use Filament\Resources\Resource;
|
||||||
@@ -149,7 +150,12 @@ class TorrentResource extends Resource
|
|||||||
->icon('heroicon-o-arrow-circle-up')
|
->icon('heroicon-o-arrow-circle-up')
|
||||||
->action(function (Collection $records, array $data) {
|
->action(function (Collection $records, array $data) {
|
||||||
$idArr = $records->pluck('id')->toArray();
|
$idArr = $records->pluck('id')->toArray();
|
||||||
Torrent::query()->whereIn('id', $idArr)->update(['pos_state' => $data['pos_state']]);
|
try {
|
||||||
|
$torrentRep = new TorrentRepository();
|
||||||
|
$torrentRep->setPosState($idArr, $data['pos_state']);
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
Filament::notify('danger', class_basename($exception));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
->deselectRecordsAfterCompletion();
|
->deselectRecordsAfterCompletion();
|
||||||
}
|
}
|
||||||
@@ -161,7 +167,12 @@ class TorrentResource extends Resource
|
|||||||
->icon('heroicon-o-minus-circle')
|
->icon('heroicon-o-minus-circle')
|
||||||
->action(function (Collection $records) {
|
->action(function (Collection $records) {
|
||||||
$idArr = $records->pluck('id')->toArray();
|
$idArr = $records->pluck('id')->toArray();
|
||||||
TorrentTag::query()->whereIn('torrent_id', $idArr)->delete();
|
try {
|
||||||
|
$torrentRep = new TorrentRepository();
|
||||||
|
$torrentRep->syncTags($idArr);
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
Filament::notify('danger', class_basename($exception));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
->deselectRecordsAfterCompletion();
|
->deselectRecordsAfterCompletion();
|
||||||
|
|
||||||
@@ -179,32 +190,21 @@ class TorrentResource extends Resource
|
|||||||
if (empty($data['tags'])) {
|
if (empty($data['tags'])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$insert = $torrentIdArr = [];
|
$idArr = $records->pluck('id')->toArray();
|
||||||
$time = now()->toDateTimeString();
|
try {
|
||||||
foreach ($records as $torrent) {
|
$torrentRep = new TorrentRepository();
|
||||||
$torrentIdArr[] = $torrent->id;
|
$torrentRep->syncTags($idArr, $data['tags']);
|
||||||
foreach ($data['tags'] as $tagId) {
|
} catch (\Exception $exception) {
|
||||||
$insert[] = [
|
Filament::notify('danger', class_basename($exception));
|
||||||
'torrent_id' => $torrent->id,
|
|
||||||
'tag_id' => $tagId,
|
|
||||||
'created_at' => $time,
|
|
||||||
'updated_at' => $time,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
TorrentTag::query()->whereIn('torrent_id', $torrentIdArr)->delete();
|
|
||||||
TorrentTag::query()->insert($insert);
|
|
||||||
})
|
})
|
||||||
->deselectRecordsAfterCompletion();
|
->deselectRecordsAfterCompletion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return $actions;
|
return $actions;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getActions()
|
private static function getActions(): array
|
||||||
{
|
{
|
||||||
$actions = [];
|
$actions = [];
|
||||||
$userClass = Auth::user()->class;
|
$userClass = Auth::user()->class;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
|
use App\Exceptions\InsufficientPermissionException;
|
||||||
use App\Exceptions\NexusException;
|
use App\Exceptions\NexusException;
|
||||||
use App\Models\AudioCodec;
|
use App\Models\AudioCodec;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
@@ -22,10 +23,13 @@ use App\Models\Team;
|
|||||||
use App\Models\Torrent;
|
use App\Models\Torrent;
|
||||||
use App\Models\TorrentOperationLog;
|
use App\Models\TorrentOperationLog;
|
||||||
use App\Models\TorrentSecret;
|
use App\Models\TorrentSecret;
|
||||||
|
use App\Models\TorrentTag;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Hashids\Hashids;
|
use Hashids\Hashids;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Nexus\Database\NexusDB;
|
use Nexus\Database\NexusDB;
|
||||||
|
|
||||||
@@ -89,7 +93,7 @@ class TorrentRepository extends BaseRepository
|
|||||||
$with = ['user', 'tags'];
|
$with = ['user', 'tags'];
|
||||||
$torrents = $query->with($with)->paginate();
|
$torrents = $query->with($with)->paginate();
|
||||||
$userArr = $user->toArray();
|
$userArr = $user->toArray();
|
||||||
foreach($torrents as &$item) {
|
foreach ($torrents as &$item) {
|
||||||
$item->download_url = $this->getDownloadUrl($item->id, $userArr);
|
$item->download_url = $this->getDownloadUrl($item->id, $userArr);
|
||||||
}
|
}
|
||||||
return $torrents;
|
return $torrents;
|
||||||
@@ -99,8 +103,12 @@ class TorrentRepository extends BaseRepository
|
|||||||
{
|
{
|
||||||
$with = [
|
$with = [
|
||||||
'user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team',
|
'user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team',
|
||||||
'thanks' => function ($query) use ($user) {$query->where('userid', $user->id);},
|
'thanks' => function ($query) use ($user) {
|
||||||
'reward_logs' => function ($query) use ($user) {$query->where('userid', $user->id);},
|
$query->where('userid', $user->id);
|
||||||
|
},
|
||||||
|
'reward_logs' => function ($query) use ($user) {
|
||||||
|
$query->where('userid', $user->id);
|
||||||
|
},
|
||||||
];
|
];
|
||||||
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users', 'reward_logs'])->visible()->findOrFail($id);
|
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users', 'reward_logs'])->visible()->findOrFail($id);
|
||||||
$result->download_url = $this->getDownloadUrl($id, $user->toArray());
|
$result->download_url = $this->getDownloadUrl($id, $user->toArray());
|
||||||
@@ -201,8 +209,7 @@ class TorrentRepository extends BaseRepository
|
|||||||
->groupBy('peer_id')
|
->groupBy('peer_id')
|
||||||
->with(['user', 'relative_torrent'])
|
->with(['user', 'relative_torrent'])
|
||||||
->get()
|
->get()
|
||||||
->groupBy('seeder')
|
->groupBy('seeder');
|
||||||
;
|
|
||||||
if ($peers->has(Peer::SEEDER_YES)) {
|
if ($peers->has(Peer::SEEDER_YES)) {
|
||||||
$seederList = $peers->get(Peer::SEEDER_YES)->sort(function ($a, $b) {
|
$seederList = $peers->get(Peer::SEEDER_YES)->sort(function ($a, $b) {
|
||||||
$x = $a->uploaded;
|
$x = $a->uploaded;
|
||||||
@@ -300,7 +307,7 @@ class TorrentRepository extends BaseRepository
|
|||||||
public function getSnatchUploadSpeed($snatch)
|
public function getSnatchUploadSpeed($snatch)
|
||||||
{
|
{
|
||||||
if ($snatch->seedtime <= 0) {
|
if ($snatch->seedtime <= 0) {
|
||||||
$speed = mksize(0);
|
$speed = mksize(0);
|
||||||
} else {
|
} else {
|
||||||
$speed = mksize($snatch->uploaded / ($snatch->seedtime + $snatch->leechtime));
|
$speed = mksize($snatch->uploaded / ($snatch->seedtime + $snatch->leechtime));
|
||||||
}
|
}
|
||||||
@@ -450,7 +457,7 @@ class TorrentRepository extends BaseRepository
|
|||||||
$formId = "$id-form";
|
$formId = "$id-form";
|
||||||
$rows[] = sprintf(
|
$rows[] = sprintf(
|
||||||
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div>%s</div></div>',
|
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div>%s</div></div>',
|
||||||
$id, $rowStyle, $labelStyle,nexus_trans('torrent.approval.status_label'), implode('', $radios)
|
$id, $rowStyle, $labelStyle, nexus_trans('torrent.approval.status_label'), implode('', $radios)
|
||||||
);
|
);
|
||||||
$rows[] = sprintf(
|
$rows[] = sprintf(
|
||||||
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div><textarea name="params[comment]" rows="4" cols="40"></textarea></div></div>',
|
'<div class="%s-row" style="%s"><div style="%s">%s: </div><div><textarea name="params[comment]" rows="4" cols="40"></textarea></div></div>',
|
||||||
@@ -566,5 +573,42 @@ class TorrentRepository extends BaseRepository
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function syncTags($id, array $tagIdArr = [])
|
||||||
|
{
|
||||||
|
if (Auth::user()->class < Setting::get('authority.torrentmanage')) {
|
||||||
|
throw new InsufficientPermissionException();
|
||||||
|
}
|
||||||
|
$idArr = Arr::wrap($id);
|
||||||
|
return NexusDB::transaction(function () use ($idArr, $tagIdArr) {
|
||||||
|
$insert = [];
|
||||||
|
$time = now()->toDateTimeString();
|
||||||
|
foreach ($idArr as $torrentId) {
|
||||||
|
foreach ($tagIdArr as $tagId) {
|
||||||
|
$insert[] = [
|
||||||
|
'torrent_id' => $torrentId,
|
||||||
|
'tag_id' => $tagId,
|
||||||
|
'created_at' => $time,
|
||||||
|
'updated_at' => $time,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TorrentTag::query()->whereIn('torrent_id', $idArr)->delete();
|
||||||
|
if (!empty($insert)) {
|
||||||
|
TorrentTag::query()->insert($insert);
|
||||||
|
}
|
||||||
|
return count($insert);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPosState($id, $posState): int
|
||||||
|
{
|
||||||
|
if (Auth::user()->class < Setting::get('authority.torrentsticky')) {
|
||||||
|
throw new InsufficientPermissionException();
|
||||||
|
}
|
||||||
|
$idArr = Arr::wrap($id);
|
||||||
|
return Torrent::query()->whereIn('id', $idArr)->update(['pos_state' => $posState]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user