60 lines
2.5 KiB
PHP
Executable File
60 lines
2.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class StorageLinkCommand extends Command
|
|
{
|
|
protected static $defaultName = 'storage:link';
|
|
protected static $defaultDescription = 'Create the symbolic link from "public/storage" to "storage/uploads"';
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$root = dirname(__DIR__, 3);
|
|
$target = $root . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'uploads';
|
|
$link = $root . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'storage';
|
|
|
|
if (!is_dir($target)) {
|
|
if (!mkdir($target, 0755, true)) {
|
|
$output->writeln('<error>Could not create directory: ' . $target . '</error>');
|
|
return Command::FAILURE;
|
|
}
|
|
$output->writeln('<info>Created directory: ' . $target . '</info>');
|
|
}
|
|
|
|
if (file_exists($link) || is_link($link)) {
|
|
$output->writeln('<error>The "public/storage" already exists (as a file, directory or link).</error>');
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
// On Windows, try Junction first as it doesn't require admin privileges
|
|
$command = sprintf('mklink /J "%s" "%s"', $link, $target);
|
|
exec($command, $cmdOutput, $returnVar);
|
|
|
|
if ($returnVar === 0) {
|
|
$output->writeln('<info>The [public/storage] link has been connected to [storage/uploads] (Junction).</info>');
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
// Fallback to symlink if junction failed (though unlikely)
|
|
if (@symlink($target, $link)) {
|
|
$output->writeln('<info>The [public/storage] link has been connected to [storage/uploads] (Symlink).</info>');
|
|
return Command::SUCCESS;
|
|
}
|
|
} else {
|
|
// On Linux/Unix
|
|
if (@symlink($target, $link)) {
|
|
$output->writeln('<info>The [public/storage] link has been connected to [storage/uploads].</info>');
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|
|
|
|
$output->writeln('<error>The links could not be created. Please check permissions.</error>');
|
|
return Command::FAILURE;
|
|
}
|
|
}
|