|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SiteBase |
| 5 | + * PHP Version 8.3 |
| 6 | + * |
| 7 | + * @category CMS / Framework |
| 8 | + * @package Degami\Sitebase |
| 9 | + * @author Mirko De Grandis <degami@github.com> |
| 10 | + * @license MIT https://opensource.org/licenses/mit-license.php |
| 11 | + * @link https://github.com/degami/sitebase |
| 12 | + */ |
| 13 | + |
| 14 | +namespace App\Site\Commands\Media; |
| 15 | + |
| 16 | +use App\App; |
| 17 | +use App\Base\Abstracts\Commands\BaseCommand; |
| 18 | +use App\Site\Models\MediaElement; |
| 19 | +use Degami\Basics\Exceptions\BasicException; |
| 20 | +use DI\DependencyException; |
| 21 | +use DI\NotFoundException; |
| 22 | +use Exception; |
| 23 | +use InvalidArgumentException; |
| 24 | +use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException; |
| 25 | +use Symfony\Component\Console\Input\InputInterface; |
| 26 | +use Symfony\Component\Console\Output\OutputInterface; |
| 27 | +use Symfony\Component\Console\Command\Command; |
| 28 | +use SplFileInfo; |
| 29 | +use Symfony\Component\Console\Input\InputArgument; |
| 30 | + |
| 31 | +class FetchFromUrl extends BaseCommand |
| 32 | +{ |
| 33 | + protected function configure() |
| 34 | + { |
| 35 | + $this->setDescription('Fetch Media Element from url') |
| 36 | + ->addArgument('url', InputArgument::REQUIRED, 'Url to fetch') |
| 37 | + ->addArgument('target', InputArgument::OPTIONAL, 'Target path'); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @throws BasicException |
| 42 | + * @throws DependencyException |
| 43 | + * @throws NotFoundException |
| 44 | + * @throws PhpfastcacheSimpleCacheException |
| 45 | + * @throws \Throwable |
| 46 | + */ |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 48 | + { |
| 49 | + $media_path = App::getDir(App::MEDIA); |
| 50 | + $url = $input->getArgument('url'); |
| 51 | + $target_path = $input->getArgument('target'); |
| 52 | + if (is_null($target_path)) { |
| 53 | + $parsed = parse_url($url); |
| 54 | + $file_name = basename($parsed['path']); |
| 55 | + $target_path = $media_path . DS . $file_name; |
| 56 | + } else { |
| 57 | + try { |
| 58 | + /** @var MediaElement $element */ |
| 59 | + $element = MediaElement::loadBy('path', $target_path); |
| 60 | + if ($element->isDirectory()) { |
| 61 | + $parsed = parse_url($url); |
| 62 | + $file_name = basename($parsed['path']); |
| 63 | + $target_path = $element->getPath() . DS . $file_name; |
| 64 | + } else { |
| 65 | + $this->getIo()->error("$target_path is existing and is not a folder"); |
| 66 | + return Command::FAILURE; |
| 67 | + } |
| 68 | + } catch (Exception $e) { |
| 69 | + if (!($e instanceof \App\Base\Exceptions\NotFoundException)) { |
| 70 | + throw $e; |
| 71 | + } |
| 72 | + // path is not existing |
| 73 | + } |
| 74 | + } |
| 75 | + if (!str_starts_with($target_path, $media_path)) { |
| 76 | + throw new Exception("Target path must be under ".$media_path); |
| 77 | + } |
| 78 | + |
| 79 | + $this->getIo()->info("Downloading $url to $target_path"); |
| 80 | + $mediaElement = MediaElement::createFromUrl($url, $target_path); |
| 81 | + $mediaElement->persist(); |
| 82 | + |
| 83 | + $this->getIo()->success("Media Element downloaded to ".$mediaElement->getPath()); |
| 84 | + |
| 85 | + return Command::SUCCESS; |
| 86 | + } |
| 87 | +} |
0 commit comments