Skip to content

Commit

Permalink
Merge pull request #1188 from shikorism/develop
Browse files Browse the repository at this point in the history
Release 2024.6.1
  • Loading branch information
shibafu528 authored Jun 9, 2024
2 parents 2bbed2b + 55b10ad commit f280f64
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 213 deletions.
83 changes: 83 additions & 0 deletions app/Console/Commands/CleanLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;

class CleanLock extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tissue:lock:clean';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete obsolete global lock file';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$lockDir = storage_path('global_lock');
if (!is_dir($lockDir)) {
return Command::SUCCESS;
}
$dh = opendir($lockDir);
if (!$dh) {
$this->error("Can't open directory: {$lockDir}");

return Command::FAILURE;
}
try {
while (($file = readdir($dh)) !== false) {
$path = $lockDir . DIRECTORY_SEPARATOR . $file;
if (!is_file($path) || str_starts_with($file, '.')) {
continue;
}

// あまり古くないファイルはスキップ
$mtime = filemtime($path);
if ($mtime === false || !Carbon::createFromTimestamp($mtime)->isBefore(Carbon::now()->subDays(7))) {
continue;
}

// サーバープロセスで使用中だと困るので排他ロックを取ってから削除する
$fp = fopen($path, 'r');
if ($fp === false) {
$this->error("Error (can't open lock file): {$file}");
continue;
}
try {
if (!flock($fp, LOCK_EX)) {
$this->error("Error (can't lock file): {$file}");
continue;
}

if (unlink($path)) {
$this->info("Delete: {$file}");
} else {
$this->error("Error (can't delete lock file): {$file}");
}
} finally {
if (!fclose($fp)) {
$this->error("Error (can't close lock file): {$file}");
}
}
}
} finally {
closedir($dh);
}

return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('tissue:lock:clean')->hourly();
}

/**
Expand Down
13 changes: 13 additions & 0 deletions app/Exceptions/GlobalLockException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace App\Exceptions;

use App\Utilities\GlobalLock;

/**
* {@see GlobalLock} のロック処理に失敗した時にスローされる例外
*/
class GlobalLockException extends \RuntimeException
{
}
2 changes: 1 addition & 1 deletion app/MetadataResolver/CienResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Carbon\Carbon;
use GuzzleHttp\Client;

class CienResolver extends MetadataResolver
class CienResolver implements Resolver
{
/**
* @var Client
Expand Down
23 changes: 23 additions & 0 deletions app/MetadataResolver/FxTwitterResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace App\MetadataResolver;

use GuzzleHttp\Client;

class FxTwitterResolver implements TwitterResolver
{
public function __construct(private Client $client, private OGPResolver $ogpResolver)
{
}

public function resolve(string $url): Metadata
{
$url = preg_replace('/(www\.)?((mobile|m)\.)?(twitter|x)\.com/u', 'fxtwitter.com', $url);

$res = $this->client->get($url);
$html = (string) $res->getBody();

return $this->ogpResolver->parse($html);
}
}
17 changes: 10 additions & 7 deletions app/MetadataResolver/MetadataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MetadataResolver implements Resolver
'~store\.steampowered\.com/app/\d+~' => SteamResolver::class,
'~ss\.kb10uy\.org/posts/\d+$~' => Kb10uyShortStoryServerResolver::class,
'~www\.hentai-foundry\.com/pictures/user/.+/\d+/.+~' => HentaiFoundryResolver::class,
'~(www\.)?((mobile|m)\.)?twitter\.com/(#!/)?[0-9a-zA-Z_]{1,15}/status(es)?/([0-9]+)(/photo/[1-4])?/?(\\?.+)?$~' => TwitterResolver::class,
'~(www\.)?((mobile|m)\.)?(twitter|x)\.com/(#!/)?[0-9a-zA-Z_]{1,15}/status(es)?/([0-9]+)(/photo/[1-4])?/?(\\?.+)?$~' => TwitterResolver::class,
'~www\.mgstage\.com/~' => MGStageResolver::class,
];

Expand All @@ -46,13 +46,17 @@ class MetadataResolver implements Resolver

public $defaultResolver = OGPResolver::class;

public function __construct(private Client $client)
{
}

public function resolve(string $url): Metadata
{
foreach ($this->rules as $pattern => $class) {
if (preg_match($pattern, $url) === 1) {
try {
/** @var Resolver $resolver */
$resolver = app($class);
$resolver = app($class, ['client' => $this->client]);

return $resolver->resolve($url);
} catch (UnsupportedContentException $e) {
Expand All @@ -67,7 +71,7 @@ public function resolve(string $url): Metadata

if (isset($this->defaultResolver)) {
/** @var Resolver $resolver */
$resolver = app($this->defaultResolver);
$resolver = app($this->defaultResolver, ['client' => $this->client]);

return $resolver->resolve($url);
}
Expand All @@ -84,8 +88,7 @@ public function resolveWithAcceptHeader(string $url): Metadata
// Acceptヘッダには */* を足さないことにする。
$acceptTypes = array_diff(array_keys($this->mimeTypes), ['*/*']);

$client = app(Client::class);
$res = $client->request('GET', $url, [
$res = $this->client->request('GET', $url, [
'headers' => [
'Accept' => implode(', ', $acceptTypes)
]
Expand All @@ -97,14 +100,14 @@ public function resolveWithAcceptHeader(string $url): Metadata

if (isset($this->mimeTypes[$mimeType])) {
$class = $this->mimeTypes[$mimeType];
$parser = app($class);
$parser = app($class, ['client' => $this->client]);

return $parser->parse($res->getBody());
}

if (isset($this->mimeTypes['*/*'])) {
$class = $this->mimeTypes['*/*'];
$parser = app($class);
$parser = app($class, ['client' => $this->client]);

return $parser->parse($res->getBody());
}
Expand Down
29 changes: 18 additions & 11 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace App\Providers;

use App\MetadataResolver\FxTwitterResolver;
use App\MetadataResolver\MetadataResolver;
use App\MetadataResolver\TwitterApiResolver;
use App\MetadataResolver\TwitterOGPResolver;
use App\MetadataResolver\Resolver;
use App\MetadataResolver\TwitterResolver;
use App\Services\MetadataResolveService;
use App\Utilities\ApplyProviderPolicyMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Pagination\LengthAwarePaginator;
Expand Down Expand Up @@ -78,9 +80,7 @@ public function boot()
*/
public function register()
{
$this->app->singleton(MetadataResolver::class, function ($app) {
return new MetadataResolver();
});
$this->app->singleton(MetadataResolver::class);
$this->app->singleton('parsedown', function () {
return Parsedown::instance();
});
Expand All @@ -91,18 +91,25 @@ public function register()
]
]);
});
$this->app->when(MetadataResolver::class)->needs(Client::class)->give(function ($app) {
$stack = HandlerStack::create();
$stack->push($app->make(ApplyProviderPolicyMiddleware::class));

return new Client([
RequestOptions::HEADERS => [
'User-Agent' => 'TissueBot/1.0'
],
'handler' => $stack,
]);
});
$this->app->when(MetadataResolveService::class)
->needs('$circuitBreakCount')
->give((int) config('metadata.circuit_break_count', 5));
$this->app->when(MetadataResolveService::class)
$this->app->when(ApplyProviderPolicyMiddleware::class)
->needs('$ignoreAccessInterval')
->give((bool) config('metadata.ignore_access_interval', false));
$this->app->bind(TwitterResolver::class, function ($app) {
if (empty(config('twitter.bearer_token'))) {
return $app->make(TwitterOGPResolver::class);
} else {
return $app->make(TwitterApiResolver::class);
}
return $app->make(FxTwitterResolver::class);
});
}
}
Loading

0 comments on commit f280f64

Please sign in to comment.