fix: link old
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Photo;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PhotoOrigin
|
||||
{
|
||||
public function token(): string
|
||||
{
|
||||
return trim((string) config('coruna.photo_origin.token', ''));
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return rtrim(trim((string) config('coruna.photo_origin.url', '')), '/');
|
||||
}
|
||||
|
||||
public function pullEnabled(): bool
|
||||
{
|
||||
return $this->baseUrl() !== '' && $this->token() !== '';
|
||||
}
|
||||
|
||||
public function serveEnabled(): bool
|
||||
{
|
||||
return $this->token() !== '';
|
||||
}
|
||||
|
||||
public function tokenMatches(?string $provided): bool
|
||||
{
|
||||
$expected = $this->token();
|
||||
if ($expected === '' || ! is_string($provided) || $provided === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hash_equals($expected, $provided);
|
||||
}
|
||||
|
||||
public function isSafeRelPath(string $path): bool
|
||||
{
|
||||
$path = str_replace('\\', '/', $path);
|
||||
if ($path === '' || str_contains($path, '..')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_starts_with($path, 'c2/photos/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the local file when present; otherwise pull from the old host and write through.
|
||||
*/
|
||||
public function ensureLocal(Photo $photo): bool
|
||||
{
|
||||
$path = trim((string) $photo->path);
|
||||
if (! $this->isSafeRelPath($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
if ($disk->exists($path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->pullEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$resp = Http::timeout(max(5, (int) config('coruna.photo_origin.timeout', 60)))
|
||||
->withHeaders(['X-Photo-Origin-Token' => $this->token()])
|
||||
->get($this->baseUrl().'/hooks/photo-origin/'.$photo->id);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('photo origin pull failed', [
|
||||
'photo_id' => $photo->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $resp->successful()) {
|
||||
Log::info('photo origin pull miss', [
|
||||
'photo_id' => $photo->id,
|
||||
'status' => $resp->status(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$bytes = $resp->body();
|
||||
if ($bytes === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$disk->put($path, $bytes);
|
||||
|
||||
return $disk->exists($path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user