From 64cb4cec688928798cc40e6f0a0e863d7f69fd89 Mon Sep 17 00:00:00 2001 From: Adam <607975+adam-vessey@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:30:49 -0400 Subject: [PATCH] Merge commit from fork * Rework Homarus CLI invocation to use better escaping. * Bump PHP version in docs. * Point at the newly released/merged code. --- Homarus/composer.json | 2 +- Homarus/src/Controller/HomarusController.php | 55 +++++++++++++++----- README.md | 4 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Homarus/composer.json b/Homarus/composer.json index 4680307c..b7d42870 100644 --- a/Homarus/composer.json +++ b/Homarus/composer.json @@ -6,7 +6,7 @@ "require": { "ext-ctype": "*", "ext-iconv": "*", - "islandora/crayfish-commons": "^4.0", + "islandora/crayfish-commons": "^4.1", "lexik/jwt-authentication-bundle": "^2.18", "symfony/dotenv": "5.4.*", "symfony/flex": "^1.3.1", diff --git a/Homarus/src/Controller/HomarusController.php b/Homarus/src/Controller/HomarusController.php index 222df306..d8e18e37 100644 --- a/Homarus/src/Controller/HomarusController.php +++ b/Homarus/src/Controller/HomarusController.php @@ -5,6 +5,7 @@ use Islandora\Crayfish\Commons\CmdExecuteService; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\HeaderBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -112,12 +113,20 @@ public function convert(Request $request) $content_types = $request->getAcceptableContentTypes(); [$content_type, $format] = $this->getFfmpegFormat($content_types); - $cmd_params = ""; - if ($format == "mp4") { - $cmd_params = " -vcodec libx264 -preset medium -acodec aac " . - "-strict -2 -ab 128k -ac 2 -async 1 -movflags " . - "faststart -y"; - } + $cmd_params = match ($format) { + 'mp4' => [ + '-vcodec', 'libx264', + '-preset', 'medium', + '-acodec', 'aac', + '-strict', '-2', + '-ab', '128k', + '-ac', '2', + '-async', '1', + '-movflags', 'faststart', + '-y', + ], + default => [], + }; $temp_file_path = $this->tempDirectory . basename(parse_url($source, PHP_URL_PATH)) . "." . $format; $this->log->debug('Tempfile: ' . $temp_file_path); @@ -139,13 +148,31 @@ public function convert(Request $request) // Add -loglevel error so large files can be processed. $args .= ' -loglevel error'; $this->log->debug("X-Islandora-Args:", ['args' => $args]); - $token = $request->headers->get('Authorization'); - $headers = "'Authorization: $token'"; - $cmd_string = "$this->executable -headers $headers -i $source $args $cmd_params -f $format $temp_file_path"; - $this->log->debug('Ffmpeg Command:', ['cmd' => $cmd_string]); + + $arg_array = array_filter(explode(' ', $args)); + + $header_bag = new HeaderBag(); + if ($token = $request->headers->get('Authorization')) { + $header_bag->set('Authorization', $token); + } + + $cmd = array_merge( + [ + $this->executable, + '-headers', $header_bag, + '-i', $source, + ], + $arg_array, + $cmd_params, + [ + '-f', $format, + $temp_file_path, + ], + ); + $this->log->debug('Ffmpeg Command:', ['cmd' => $cmd]); // Return response. - return $this->generateDerivativeResponse($cmd_string, $source, $temp_file_path, $content_type); + return $this->generateDerivativeResponse($cmd, $source, $temp_file_path, $content_type); } /** @@ -176,7 +203,7 @@ private function getFfmpegFormat(array $content_types): array } /** - * @param string $cmd_string + * @param array $cmd * @param string $source * @param string $path * @param string $content_type @@ -184,13 +211,13 @@ private function getFfmpegFormat(array $content_types): array * @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse */ public function generateDerivativeResponse( - string $cmd_string, + array $cmd, string $source, string $path, string $content_type ) { try { - $this->cmd->execute($cmd_string, $source); + $this->cmd->execute($cmd, $source); return (new BinaryFileResponse( $path, 200, diff --git a/README.md b/README.md index 7d8a7ef7..c19fc986 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ![Crayfish](https://cloud.githubusercontent.com/assets/2371345/15409657/2dfb463a-1dec-11e6-9089-06df94ef3f37.png) Crayfish -[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.3-8892BF.svg?style=flat-square)](https://php.net/) +[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.0-8892BF.svg?style=flat-square)](https://php.net/) [![Build Status](https://github.com/islandora/crayfish/actions/workflows/build-dev.yml/badge.svg)](https://github.com/Islandora/Crayfish/actions) [![Contribution Guidelines](http://img.shields.io/badge/CONTRIBUTING-Guidelines-blue.svg)](./CONTRIBUTING.md) [![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](./LICENSE) @@ -14,7 +14,7 @@ A collection of Islandora 8 microservices, lovingly known as Crayfish. Some of The minimum requirements for any microservice are -* PHP 7.3+ +* PHP 8.0+ * [Composer](https://getcomposer.org/) Many microservices have extra installation requirements. Please see the README of each microservice for additional details.