mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
upload preivew + es command
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Repositories\SearchRepository;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class EsCreateIndex extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'es:create_index';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create index in Elasticsearch';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new SearchRepository();
|
||||
$result = $rep->createIndex();
|
||||
$log = sprintf("[%s], %s, result: \n%s", nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Repositories\SearchRepository;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class EsDeleteIndex extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'es:delete_index';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Delete index in Elasticsearch';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new SearchRepository();
|
||||
$result = $rep->deleteIndex();
|
||||
$log = sprintf("[%s], %s, result: \n%s", nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Repositories\SearchRepository;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class EsImport extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'es:import {--torrent_id=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Import torrent to Elasticsearch, options: --torrent_id';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new SearchRepository();
|
||||
$torrentId = $this->option('torrent_id');
|
||||
$this->info("torrent_id: $torrentId");
|
||||
$result = $rep->import($torrentId);
|
||||
$log = sprintf("[%s], %s, result: \n%s", nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Repositories\SearchRepository;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class EsInfo extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'es:info';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Show Elasticsearch info';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$rep = new SearchRepository();
|
||||
$result = $rep->getEsInfo();
|
||||
$log = sprintf("[%s], %s, result: \n%s", nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,12 @@ namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
class Locale
|
||||
{
|
||||
public static $languageMaps = [
|
||||
public static array $languageMaps = [
|
||||
'en' => 'en',
|
||||
'chs' => 'zh_CN',
|
||||
'cht' => 'zh_TW',
|
||||
@@ -27,7 +28,11 @@ class Locale
|
||||
$locale = self::$languageMaps[$language->site_lang_folder] ?? 'en';
|
||||
do_log("set locale: " . $locale);
|
||||
App::setLocale($locale);
|
||||
return $next($request);
|
||||
|
||||
/** @var Response $response */
|
||||
$response = $next($request);
|
||||
$response->header('Request-Id', nexus()->getRequestId());
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class MessageResource extends JsonResource
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'subject' => $this->subject,
|
||||
'msg' => strip_all_tags($this->msg),
|
||||
'msg' => htmlspecialchars_decode(strip_all_tags($this->msg)),
|
||||
'added_human' => $this->added->diffForHumans(),
|
||||
'added' => format_datetime($this->added),
|
||||
'send_user' => new UserResource($this->whenLoaded('send_user')),
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ class Peer extends NexusModel
|
||||
'started' => 'datetime',
|
||||
'last_action' => 'datetime',
|
||||
'prev_action' => 'datetime',
|
||||
'finishedat' => 'datetime',
|
||||
'finishedat' => 'datetime:U',
|
||||
];
|
||||
|
||||
public static $connectableText = [
|
||||
|
||||
@@ -6,14 +6,14 @@ use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\TorrentTag;
|
||||
use App\Models\User;
|
||||
use Elasticsearch\Client;
|
||||
use Elasticsearch\ClientBuilder;
|
||||
use Elastic\Elasticsearch\Client;
|
||||
use Elastic\Elasticsearch\ClientBuilder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class SearchRepository extends BaseRepository
|
||||
{
|
||||
private Client $es;
|
||||
private ?Client $es = null;
|
||||
|
||||
private bool $enabled = false;
|
||||
|
||||
@@ -114,7 +114,6 @@ class SearchRepository extends BaseRepository
|
||||
$elasticsearchEnabled = nexus_env('ELASTICSEARCH_ENABLED');
|
||||
if ($elasticsearchEnabled) {
|
||||
$this->enabled = true;
|
||||
$this->es = $this->getEs();
|
||||
} else {
|
||||
$this->enabled = false;
|
||||
}
|
||||
@@ -122,12 +121,15 @@ class SearchRepository extends BaseRepository
|
||||
|
||||
private function getEs(): Client
|
||||
{
|
||||
$config = nexus_config('nexus.elasticsearch');
|
||||
$es = ClientBuilder::create()->setHosts($config['hosts']);
|
||||
if (!empty($config['ssl_verification'])) {
|
||||
$es->setSSLVerification($config['ssl_verification']);
|
||||
if (is_null($this->es)) {
|
||||
$config = nexus_config('nexus.elasticsearch');
|
||||
$es = ClientBuilder::create()->setHosts($config['hosts']);
|
||||
if (!empty($config['ssl_verification'])) {
|
||||
$es->setSSLVerification($config['ssl_verification']);
|
||||
}
|
||||
$this->es = $es;
|
||||
}
|
||||
return $es->build();
|
||||
return $this->es;
|
||||
}
|
||||
|
||||
private function getTorrentRawMappingFields(): array
|
||||
@@ -167,7 +169,7 @@ class SearchRepository extends BaseRepository
|
||||
|
||||
public function getEsInfo(): callable|array
|
||||
{
|
||||
return $this->es->info();
|
||||
return $this->getEs()->info();
|
||||
}
|
||||
|
||||
public function createIndex()
|
||||
@@ -176,20 +178,17 @@ class SearchRepository extends BaseRepository
|
||||
$properties = $params['body']['mappings']['properties'];
|
||||
$properties = array_merge($properties, $this->getTorrentRawMappingFields());
|
||||
$params['body']['mappings']['properties'] = $properties;
|
||||
return $this->es->indices()->create($params);
|
||||
return $this->getEs()->indices()->create($params);
|
||||
}
|
||||
|
||||
public function deleteIndex()
|
||||
{
|
||||
$params = ['index' => self::INDEX_NAME];
|
||||
return $this->es->indices()->delete($params);
|
||||
return $this->getEs()->indices()->delete($params);
|
||||
}
|
||||
|
||||
public function import($torrentId = null)
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return true;
|
||||
}
|
||||
$page = 1;
|
||||
$size = 1000;
|
||||
$fields = $this->getTorrentBaseFields();
|
||||
@@ -235,19 +234,19 @@ class SearchRepository extends BaseRepository
|
||||
}
|
||||
|
||||
//index user
|
||||
$result = $this->es->bulk($userBodyBulk);
|
||||
$result = $this->getEs()->bulk($userBodyBulk);
|
||||
$this->logEsResponse("$log, bulk index user done!", $result);
|
||||
|
||||
//index torrent
|
||||
$result = $this->es->bulk($torrentBodyBulk);
|
||||
$result = $this->getEs()->bulk($torrentBodyBulk);
|
||||
$this->logEsResponse("$log, bulk index torrent done!", $result);
|
||||
|
||||
//index tag
|
||||
$result = $this->es->bulk($tagBodyBulk);
|
||||
$result = $this->getEs()->bulk($tagBodyBulk);
|
||||
$this->logEsResponse("$log, bulk index tag done!", $result);
|
||||
|
||||
//index bookmark
|
||||
$result = $this->es->bulk($bookmarkBodyBulk);
|
||||
$result = $this->getEs()->bulk($bookmarkBodyBulk);
|
||||
$this->logEsResponse("$log, bulk index bookmark done!", $result);
|
||||
|
||||
$page++;
|
||||
@@ -648,7 +647,7 @@ class SearchRepository extends BaseRepository
|
||||
'index' => self::INDEX_NAME,
|
||||
'body' => $query,
|
||||
];
|
||||
$response = $this->es->search($esParams);
|
||||
$response = $this->getEs()->search($esParams);
|
||||
$result = [
|
||||
'total' => 0,
|
||||
'data' => [],
|
||||
@@ -711,7 +710,7 @@ class SearchRepository extends BaseRepository
|
||||
$data = $this->buildTorrentBody($torrent);
|
||||
$params = $data['index'];
|
||||
$params['body']['doc'] = $data['body'];
|
||||
$result = $this->es->update($params);
|
||||
$result = $this->getEs()->update($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -733,7 +732,7 @@ class SearchRepository extends BaseRepository
|
||||
$params = ['body' => []];
|
||||
$params['body'][] = ['index' => $data['index']];
|
||||
$params['body'][] = $data['body'];
|
||||
$result = $this->es->bulk($params);
|
||||
$result = $this->getEs()->bulk($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -752,7 +751,7 @@ class SearchRepository extends BaseRepository
|
||||
'index' => self::INDEX_NAME,
|
||||
'id' => $this->getTorrentId($id),
|
||||
];
|
||||
return $this->es->get($params);
|
||||
return $this->getEs()->get($params);
|
||||
}
|
||||
|
||||
public function deleteTorrent(int $id): bool
|
||||
@@ -765,7 +764,7 @@ class SearchRepository extends BaseRepository
|
||||
'index' => self::INDEX_NAME,
|
||||
'id' => $this->getTorrentId($id),
|
||||
];
|
||||
$result = $this->es->delete($params);
|
||||
$result = $this->getEs()->delete($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -798,7 +797,7 @@ class SearchRepository extends BaseRepository
|
||||
]
|
||||
]
|
||||
];
|
||||
$result = $this->es->deleteByQuery($params);
|
||||
$result = $this->getEs()->deleteByQuery($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, delete torrent tag fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -820,7 +819,7 @@ class SearchRepository extends BaseRepository
|
||||
do_log("$log, no tags, return true");
|
||||
return true;
|
||||
}
|
||||
$result = $this->es->bulk($bulk);
|
||||
$result = $this->getEs()->bulk($bulk);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, insert torrent tag fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -841,7 +840,7 @@ class SearchRepository extends BaseRepository
|
||||
$data = $this->buildUserBody($user);
|
||||
$params = $data['index'];
|
||||
$params['body']['doc'] = $data['body'];
|
||||
$result = $this->es->update($params);
|
||||
$result = $this->getEs()->update($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -865,7 +864,7 @@ class SearchRepository extends BaseRepository
|
||||
$body = $this->buildBookmarkBody($bookmark->torrent, $bookmark, true);
|
||||
$bulk['body'][] = ['index' => $body['index']];
|
||||
$bulk['body'][] = $body['body'];
|
||||
$result = $this->es->bulk($bulk);
|
||||
$result = $this->getEs()->bulk($bulk);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
@@ -884,7 +883,7 @@ class SearchRepository extends BaseRepository
|
||||
'index' => self::INDEX_NAME,
|
||||
'id' => $this->getBookmarkId($id),
|
||||
];
|
||||
$result = $this->es->delete($params);
|
||||
$result = $this->getEs()->delete($params);
|
||||
if ($this->isEsResponseError($result)) {
|
||||
do_log("$log, fail: " . nexus_json_encode($result), 'error');
|
||||
return false;
|
||||
|
||||
@@ -296,7 +296,7 @@ class TrackerRepository extends BaseRepository
|
||||
return User::query()->where($field, $value)->first();
|
||||
});
|
||||
if (!$user) {
|
||||
throw new TrackerException("Invalid $field: $value.");
|
||||
throw new TrackerException("Invalid user $field: $value.");
|
||||
}
|
||||
$user->checkIsNormal();
|
||||
|
||||
@@ -861,11 +861,11 @@ class TrackerRepository extends BaseRepository
|
||||
|
||||
private function getTorrentByInfoHash($infoHash)
|
||||
{
|
||||
$cacheKey = bin2hex($infoHash) . __METHOD__;
|
||||
return Cache::remember($cacheKey, 60, function () use ($infoHash) {
|
||||
$cacheKey = __METHOD__ . bin2hex($infoHash);
|
||||
return Cache::remember($cacheKey, 60, function () use ($infoHash, $cacheKey) {
|
||||
$fieldRaw = 'id, owner, sp_state, seeders, leechers, added, banned, hr, visible, last_action, times_completed';
|
||||
$torrent = Torrent::query()->where('info_hash', $infoHash)->selectRaw($fieldRaw)->first();
|
||||
do_log("[getTorrentByInfoHash] cache miss, from database: " . last_query() . ", and get: " . $torrent->id);
|
||||
do_log("[getTorrentByInfoHash] cache miss [$cacheKey], from database, and get: " . ($torrent->id ?? ''));
|
||||
return $torrent;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user