118 lines
3.8 KiB
PHP
118 lines
3.8 KiB
PHP
<?php
|
||
define('LARAVEL_START', microtime(true));
|
||
|
||
$projectRoot = dirname(__DIR__);
|
||
|
||
require $projectRoot . '/vendor/autoload.php';
|
||
|
||
$app = require_once $projectRoot . '/bootstrap/app.php';
|
||
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
|
||
$kernel->bootstrap();
|
||
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
$dryRun = !in_array('--delete', $argv ?? []);
|
||
echo "\n=== ۱. Storage Link Fix ===\n\n";
|
||
|
||
$link = $projectRoot . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'storage';
|
||
$target = $projectRoot . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'public';
|
||
|
||
echo "Link : {$link}\n";
|
||
echo "Target : {$target}\n\n";
|
||
|
||
if (!is_dir($target)) {
|
||
echo "ERROR: Target directory not found!\n";
|
||
} else {
|
||
if (is_link($link) || (PHP_OS_FAMILY === 'Windows' && is_dir($link))) {
|
||
if (PHP_OS_FAMILY === 'Windows') {
|
||
exec('rmdir "' . $link . '"', $out, $code);
|
||
} else {
|
||
unlink($link);
|
||
}
|
||
echo "Old link removed.\n";
|
||
}
|
||
|
||
if (!file_exists($link) && !is_dir($link)) {
|
||
if (PHP_OS_FAMILY === 'Windows') {
|
||
exec('mklink /J "' . $link . '" "' . $target . '"', $out, $code);
|
||
if ($code === 0) {
|
||
echo "Junction created successfully! (no admin needed)\n";
|
||
} else {
|
||
echo "ERROR: " . implode("\n", $out) . "\n";
|
||
}
|
||
} else {
|
||
if (symlink($target, $link)) {
|
||
echo "Symlink created successfully!\n";
|
||
} else {
|
||
echo "ERROR: Could not create symlink.\n";
|
||
}
|
||
}
|
||
} else {
|
||
echo "OK: Link already exists and points to correct target.\n";
|
||
}
|
||
}
|
||
echo "\n=== ۲. Orphan Files Cleanup ===\n";
|
||
echo $dryRun ? "Mode: DRY RUN (add --delete to actually delete)\n\n" : "Mode: DELETE\n\n";
|
||
|
||
$referencedFiles = [];
|
||
|
||
$patients = DB::table('patients')->select('photos_before', 'photos_after', 'videos', 'audio_files')->get();
|
||
foreach ($patients as $p) {
|
||
foreach (['photos_before', 'photos_after', 'videos', 'audio_files'] as $field) {
|
||
foreach (json_decode($p->$field ?? '[]', true) ?? [] as $file) {
|
||
$referencedFiles[] = $file;
|
||
}
|
||
}
|
||
}
|
||
|
||
$initials = DB::table('osurg_initials')->select('attachment')->whereNotNull('attachment')->get();
|
||
foreach ($initials as $i) {
|
||
if ($i->attachment) {
|
||
$referencedFiles[] = $i->attachment;
|
||
}
|
||
}
|
||
|
||
$referencedFiles = array_filter($referencedFiles);
|
||
echo "Files referenced in DB: " . count($referencedFiles) . "\n\n";
|
||
|
||
$dirs = ['patients/photos-before', 'patients/photos-after', 'patients/videos', 'patients/audio', 'initials'];
|
||
$totalOrphans = 0;
|
||
$totalSize = 0;
|
||
|
||
foreach ($dirs as $dir) {
|
||
$diskFiles = Storage::disk('public')->files($dir);
|
||
|
||
if (empty($diskFiles)) {
|
||
echo "[{$dir}] No files.\n";
|
||
continue;
|
||
}
|
||
|
||
$orphans = array_values(array_filter($diskFiles, fn($f) => !in_array($f, $referencedFiles)));
|
||
|
||
echo "[{$dir}] total=" . count($diskFiles) . " orphans=" . count($orphans) . "\n";
|
||
|
||
foreach ($orphans as $orphan) {
|
||
$size = Storage::disk('public')->size($orphan);
|
||
$totalSize += $size;
|
||
$totalOrphans++;
|
||
echo " - {$orphan} (" . number_format($size / 1024, 1) . " KB)";
|
||
|
||
if (!$dryRun) {
|
||
Storage::disk('public')->delete($orphan);
|
||
echo " [DELETED]";
|
||
}
|
||
echo "\n";
|
||
}
|
||
}
|
||
|
||
echo "\n─────────────────────────────────\n";
|
||
echo "Orphan files : {$totalOrphans}\n";
|
||
echo "Wasted space : " . number_format($totalSize / 1024 / 1024, 2) . " MB\n";
|
||
|
||
if ($dryRun && $totalOrphans > 0) {
|
||
echo "\nبرای حذف اجرا کن:\n php scripts/fix_storage.php --delete\n";
|
||
}
|
||
|
||
echo "\nDone!\n";
|