add: torrent folder name to files list

This commit is contained in:
Roardom
2023-07-30 03:51:43 +00:00
parent 39be1d1ee8
commit 4c4583bc86
5 changed files with 71 additions and 0 deletions
+17
View File
@@ -252,6 +252,23 @@ class Bencode
return $result;
}
public static function get_name($t): ?string
{
$name = null;
if (
\array_key_exists('info', $t)
&& \is_array($t['info'])
&& \array_key_exists('name', $t['info'])
&& \is_string($t['info']['name'])
&& \array_key_exists('files', $t['info'])
) {
$name = $t['info']['name'];
}
return $name;
}
public static function is_v2_or_hybrid($t): bool
{
return isset($t['piece layers']);
@@ -141,6 +141,7 @@ class TorrentController extends BaseController
$torrent->info_hash = $infohash;
$torrent->file_name = $fileName;
$torrent->num_file = $meta['count'];
$torrent->folder = Bencode::get_name($decodedTorrent);
$torrent->announce = $decodedTorrent['announce'];
$torrent->size = $meta['size'];
$torrent->nfo = ($request->hasFile('nfo')) ? TorrentTools::getNfo($request->file('nfo')) : '';
@@ -368,6 +368,7 @@ class TorrentController extends Controller
'info_hash' => Bencode::get_infohash($decodedTorrent),
'file_name' => $fileName,
'num_file' => $meta['count'],
'folder' => Bencode::get_name($decodedTorrent),
'announce' => $decodedTorrent['announce'],
'size' => $meta['size'],
'nfo' => $request->hasFile('nfo') ? TorrentTools::getNfo($request->file('nfo')) : '',
@@ -0,0 +1,31 @@
<?php
use App\Helpers\Bencode;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
public function up(): void
{
Schema::table('torrents', function (Blueprint $table): void {
$table->string('folder')->nullable()->after('num_file');
});
$directory = public_path().'/files/torrents/';
DB::table('torrents')
->lazyById()
->each(function (object $torrent) use ($directory): void {
if (file_exists($directory.$torrent->file_name)) {
$dict = Bencode::bdecode_file($directory.$torrent->file_name);
DB::table('torrents')
->where('id', $torrent->id)
->update([
'folder' => Bencode::get_name($dict),
]);
}
});
}
};
@@ -177,6 +177,27 @@
</li>
</menu>
<div class="dialog__form" x-show="tab === 'hierarchy'">
@if ($torrent->folder !== null)
<span style="display: grid; grid-template-areas: 'icon folder count . size'; grid-template-columns: 24px auto auto 1fr auto; align-items: center; padding-bottom: 8px;">
<i
class="{{ config('other.font-awesome') }} fa-folder"
style="grid-area: icon; padding-right: 4px"
></i>
<span style="padding-right: 4px; word-break: break-all">
{{ $torrent->folder }}
</span>
<span style="grid-area: count; padding-right: 4px;">
({{ $torrent->files()->count() }})
</span>
<span
class="text-info"
style="grid-area: size; white-space: nowrap; text-align: right;"
title="{{ $torrent->size }}&nbsp;B"
>
{{ App\Helpers\StringHelper::formatBytes($torrent->size, 2) }}
</span>
</span>
@endif
@foreach ($files = $torrent->files->sortBy('name')->values()->sortBy(fn ($f) => dirname($f->name)."/~~~", SORT_NATURAL)->values() as $file)
@php $prevNodes = explode("/", $files[$loop->index - 1]->name ?? " ") @endphp
@foreach ($nodes = explode("/", $file->name) as $node)