prepare public symlink
This commit is contained in:
@@ -63,10 +63,31 @@ class SystemStatusController extends Controller
|
||||
'rsync_version' => $this->resolveBinaryVersion($rsyncPath, ['--version']),
|
||||
'proc_functions' => $procFunctionStatus,
|
||||
'storage_writable' => is_writable(storage_path()),
|
||||
'storage_public_linked' => $this->isPublicStorageLinked(),
|
||||
'updates_writable' => is_writable(storage_path('app/updates')) || @mkdir(storage_path('app/updates'), 0755, true),
|
||||
]);
|
||||
}
|
||||
|
||||
private function isPublicStorageLinked(): bool
|
||||
{
|
||||
$publicStorage = public_path('storage');
|
||||
$storagePublic = storage_path('app/public');
|
||||
|
||||
if (!is_link($publicStorage)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$target = readlink($publicStorage);
|
||||
if ($target === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$resolvedTarget = realpath(dirname($publicStorage) . DIRECTORY_SEPARATOR . $target);
|
||||
$expectedTarget = realpath($storagePublic);
|
||||
|
||||
return $resolvedTarget !== false && $expectedTarget !== false && $resolvedTarget === $expectedTarget;
|
||||
}
|
||||
|
||||
private function resolveBinary(string $name): ?string
|
||||
{
|
||||
$process = new Process(['sh', '-lc', "command -v {$name}"]);
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class SystemUpdateController extends Controller
|
||||
@@ -113,7 +114,7 @@ class SystemUpdateController extends Controller
|
||||
$append('Syncing files...');
|
||||
$usedRsync = false;
|
||||
$rsyncPath = trim((string) shell_exec('command -v rsync'));
|
||||
$protectedPaths = ['custom', 'public/custom'];
|
||||
$protectedPaths = ['storage', 'public/storage', 'custom', 'public/custom'];
|
||||
if ($rsyncPath !== '') {
|
||||
$usedRsync = true;
|
||||
$rsync = new Process([
|
||||
@@ -149,6 +150,8 @@ class SystemUpdateController extends Controller
|
||||
File::copyDirectory($sourceDir, base_path());
|
||||
}
|
||||
|
||||
$this->ensurePublicStorageLink();
|
||||
|
||||
$append('Installing composer dependencies...');
|
||||
$composer = new Process(['composer', 'install', '--no-dev', '--optimize-autoloader'], base_path());
|
||||
$composer->setTimeout(600);
|
||||
@@ -212,4 +215,39 @@ class SystemUpdateController extends Controller
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function ensurePublicStorageLink(): void
|
||||
{
|
||||
$storagePublic = storage_path('app/public');
|
||||
$publicStorage = public_path('storage');
|
||||
|
||||
if (file_exists($storagePublic) && !is_dir($storagePublic)) {
|
||||
@rename($storagePublic, $storagePublic.'.bak.'.date('Ymd_His'));
|
||||
}
|
||||
if (!is_dir($storagePublic) && !@mkdir($storagePublic, 0775, true) && !is_dir($storagePublic)) {
|
||||
throw new RuntimeException('Failed to prepare storage/app/public directory.');
|
||||
}
|
||||
|
||||
if (is_link($publicStorage)) {
|
||||
$target = readlink($publicStorage);
|
||||
$resolved = $target !== false ? realpath(dirname($publicStorage).DIRECTORY_SEPARATOR.$target) : false;
|
||||
$expected = realpath($storagePublic);
|
||||
if ($resolved !== $expected) {
|
||||
@unlink($publicStorage);
|
||||
}
|
||||
} elseif (is_dir($publicStorage)) {
|
||||
File::copyDirectory($publicStorage, $storagePublic);
|
||||
File::deleteDirectory($publicStorage);
|
||||
} elseif (file_exists($publicStorage)) {
|
||||
@rename($publicStorage, $publicStorage.'.bak.'.date('Ymd_His'));
|
||||
}
|
||||
|
||||
if (!is_link($publicStorage) && !@symlink($storagePublic, $publicStorage)) {
|
||||
throw new RuntimeException('Failed to recreate public/storage symlink.');
|
||||
}
|
||||
|
||||
foreach (['avatars', 'logos', 'favicons', 'rank-badges'] as $dir) {
|
||||
File::ensureDirectoryExists($storagePublic.DIRECTORY_SEPARATOR.$dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use RuntimeException;
|
||||
|
||||
class UploadController extends Controller
|
||||
{
|
||||
@@ -14,6 +16,7 @@ class UploadController extends Controller
|
||||
if (!$user) {
|
||||
return response()->json(['message' => 'Unauthorized'], 401);
|
||||
}
|
||||
$this->ensurePublicStorageReady();
|
||||
|
||||
$data = $request->validate([
|
||||
'file' => [
|
||||
@@ -45,6 +48,7 @@ class UploadController extends Controller
|
||||
if (!$user || !$user->roles()->where('name', 'ROLE_ADMIN')->exists()) {
|
||||
return response()->json(['message' => 'Forbidden'], 403);
|
||||
}
|
||||
$this->ensurePublicStorageReady();
|
||||
|
||||
$data = $request->validate([
|
||||
'file' => ['required', 'file', 'mimes:jpg,jpeg,png,gif,webp,svg,ico', 'max:5120'],
|
||||
@@ -64,6 +68,7 @@ class UploadController extends Controller
|
||||
if (!$user || !$user->roles()->where('name', 'ROLE_ADMIN')->exists()) {
|
||||
return response()->json(['message' => 'Forbidden'], 403);
|
||||
}
|
||||
$this->ensurePublicStorageReady();
|
||||
|
||||
$data = $request->validate([
|
||||
'file' => ['required', 'file', 'mimes:png,ico', 'max:2048'],
|
||||
@@ -76,4 +81,49 @@ class UploadController extends Controller
|
||||
'url' => Storage::url($path),
|
||||
]);
|
||||
}
|
||||
|
||||
private function ensurePublicStorageReady(): void
|
||||
{
|
||||
$storagePublic = storage_path('app/public');
|
||||
$publicStorage = public_path('storage');
|
||||
|
||||
if (file_exists($storagePublic) && !is_dir($storagePublic)) {
|
||||
@rename($storagePublic, $storagePublic.'.bak.'.date('Ymd_His'));
|
||||
}
|
||||
if (!is_dir($storagePublic) && !@mkdir($storagePublic, 0775, true) && !is_dir($storagePublic)) {
|
||||
throw new RuntimeException('Failed to create storage/app/public directory.');
|
||||
}
|
||||
|
||||
if (is_link($publicStorage)) {
|
||||
$target = readlink($publicStorage);
|
||||
$resolved = $target !== false ? realpath(dirname($publicStorage).DIRECTORY_SEPARATOR.$target) : false;
|
||||
$expected = realpath($storagePublic);
|
||||
if ($resolved === $expected) {
|
||||
$this->ensureUploadSubdirs($storagePublic);
|
||||
return;
|
||||
}
|
||||
@unlink($publicStorage);
|
||||
} elseif (is_dir($publicStorage)) {
|
||||
File::copyDirectory($publicStorage, $storagePublic);
|
||||
File::deleteDirectory($publicStorage);
|
||||
} elseif (file_exists($publicStorage)) {
|
||||
@rename($publicStorage, $publicStorage.'.bak.'.date('Ymd_His'));
|
||||
}
|
||||
|
||||
if (!@symlink($storagePublic, $publicStorage) && !is_link($publicStorage)) {
|
||||
throw new RuntimeException('Failed to create public/storage symlink.');
|
||||
}
|
||||
|
||||
$this->ensureUploadSubdirs($storagePublic);
|
||||
}
|
||||
|
||||
private function ensureUploadSubdirs(string $storagePublic): void
|
||||
{
|
||||
foreach (['avatars', 'favicons', 'logos', 'rank-badges'] as $dir) {
|
||||
$path = $storagePublic.DIRECTORY_SEPARATOR.$dir;
|
||||
if (!is_dir($path)) {
|
||||
@mkdir($path, 0775, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user