Skip to content

Commit

Permalink
Merge commit from fork
Browse files Browse the repository at this point in the history
* Rework Homarus CLI invocation to use better escaping.

* Bump PHP version in docs.

* Point at the newly released/merged code.
  • Loading branch information
adam-vessey authored Jan 6, 2025
1 parent 6ba190c commit 64cb4ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Homarus/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 41 additions & 14 deletions Homarus/src/Controller/HomarusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -176,21 +203,21 @@ private function getFfmpegFormat(array $content_types): array
}

/**
* @param string $cmd_string
* @param array $cmd
* @param string $source
* @param string $path
* @param string $content_type
*
* @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,
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit 64cb4ce

Please # to comment.