Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homarus #52

Merged
merged 6 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Homarus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Homarus

### Apache Config
```
# managed by Ansible

Alias "/homarus" "/var/www/html/Crayfish/Homarus/src"
<Directory "/var/www/html/Crayfish/Homarus/src">
FallbackResource /homarus/index.php
Require all granted
DirectoryIndex index.php
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</Directory>
```

### Testing
* Clone this microservice into `/var/www/html/Crayfish`
* Install the depencies using composer `composer install`
* Place the above confir in `/etc/apache2/conf-available` as `Homarus.conf`.
* Enable the conf: `sudo a2enconf Homarus`. Restart Apache: `sudo systemctl restart apache2`
dannylamb marked this conversation as resolved.
Show resolved Hide resolved
* Create a test video by going here: `http://localhost:8080/fcrepo/rest/` and uploading a binary
* Test via curl as below or using Postman

```
curl -H "Authorization: Bearer islandora" -H "Accept: video/x-msvideo" -H "Apix-Ldp-Resource:http://localhost:8080/fcrepo/rest/testvideo" http://localhost:8000/homarus/convert --output output.avi
```
25 changes: 25 additions & 0 deletions Homarus/cfg/config.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
homarus:
dannylamb marked this conversation as resolved.
Show resolved Hide resolved
# path to the convert executable
executable: ffmpeg
formats:
valid:
default: video/mp4

fedora_resource:
base_url: http://localhost:8080/fcrepo/rest

log:
# Valid log levels are:
# DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY, NONE
# log level none won't open logfile
level: DEBUG
file: /var/log/islandora/homarus.log

syn:
# toggles JWT security for service
enable: True
# Path to the syn config file for authentication.
# example can be found here:
# https://github.com/Islandora-CLAW/Syn/blob/master/conf/syn-settings.example.xml
config: ../syn-settings.xml
43 changes: 43 additions & 0 deletions Homarus/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "islandora/homarus",
"description": "FFmpeg as a web service",
"type": "project",
"require": {
"islandora/crayfish-commons": "dev-master"
},
"license": "MIT",
"authors": [
{
"name": "Islandora Foundation",
"email": "[email protected]",
"role": "Owner"
},
{
"name": "Jonathan Green",
dannylamb marked this conversation as resolved.
Show resolved Hide resolved
"email": "[email protected]",
"role": "Maintainer"
}
],
"autoload": {
"psr-4": {
"Islandora\\Homarus\\": "src/"
}
},
"scripts": {
"check": [
"phpcs --standard=PSR2 src tests",
"phpcpd --names *.php src"
],
"test": [
"@check",
"phpunit"
]
},
"require-dev": {
"symfony/browser-kit": "^3.0",
"symfony/css-selector": "^3.0",
"phpunit/phpunit": "^5.0",
"squizlabs/php_codesniffer": "^2.0",
"sebastian/phpcpd": "^3.0"
}
}
30 changes: 30 additions & 0 deletions Homarus/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Houdini Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="clover.xml"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<exclude>
<file>./src/index.php</file>
<file>./src/app.php</file>
</exclude>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
132 changes: 132 additions & 0 deletions Homarus/src/Controller/HomarusController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
namespace Islandora\Homarus\Controller;

use GuzzleHttp\Psr7\StreamWrapper;
use Islandora\Crayfish\Commons\CmdExecuteService;
use Islandora\Crayfish\Commons\ApixFedoraResourceRetriever;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Class HomarusController
* @package Islandora\Homarus\Controller
* @param $log
*/
class HomarusController {

/**
* @var \Islandora\Crayfish\Commons\CmdExecuteService
*/
protected $cmd;


/**
* @var \Monolog\Logger
*/
protected $log;

/**
* Controller constructor.
* @param \Islandora\Crayfish\Commons\CmdExecuteService $cmd
* @param array $formats
* @param string $default_format
* @param string $executable
* @param $log
*/
public function __construct(
CmdExecuteService $cmd,
$formats,
$default_format,
$executable,
$log,
$mime_to_format
) {
$this->cmd = $cmd;
$this->formats = $formats;
$this->default_format = $default_format;
$this->executable = $executable;
$this->log = $log;
$this->mime_to_format = $mime_to_format;
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpFoundation\StreamedResponse
*/
public function convert(Request $request) {
$this->log->info('Ffmpeg Convert request.');

// Short circuit if there's no Apix-Ldp-Resource header.
if (!$request->headers->has("Apix-Ldp-Resource"))
{
$this->log->debug("Malformed request, no Apix-Ldp-Resource header present");
return new Response(
"Malformed request, no Apix-Ldp-Resource header present",
400
);
} else {
$source = $request->headers->get('Apix-Ldp-Resource');
}

// Find the format
$content_types = $request->getAcceptableContentTypes();
$content_type = $this->get_content_type($content_types);
$format = $this->get_ffmpeg_format($content_type);

$cmd_params = "";
if($format == "mp4") {
$cmd_params = " -vcodec libx264 -preset medium -acodec aac -strict -2 -ab 128k -ac 2 -async 1 -movflags frag_keyframe+empty_moov ";
}

// Arguments to ffmpeg command are sent as a custom header
$args = $request->headers->get('X-Islandora-Args');
$this->log->debug("X-Islandora-Args:", ['args' => $args]);

$cmd_string = "$this->executable -i $source $cmd_params -f $format -";
$this->log->info('Ffempg Command:', ['cmd' => $cmd_string]);

// Return response.
try {
return new StreamedResponse(
$this->cmd->execute($cmd_string, $source),
200,
['Content-Type' => $content_type]
);
} catch (\RuntimeException $e) {
$this->log->error("RuntimeException:", ['exception' => $e]);
return new Response($e->getMessage(), 500);
}
}


dannylamb marked this conversation as resolved.
Show resolved Hide resolved
private function get_content_type($content_types) {
$content_type = null;
foreach ($content_types as $type) {
if (in_array($type, $this->formats)) {
$content_type = $type;
break;
}
}

if ($content_type === null) {
$content_type = $this->default_format;
$this->log->info('Falling back to default content type');
}
return $content_type;
}

private function get_ffmpeg_format($content_type){
foreach ($this->mime_to_format as $format) {
if (strpos($format, $content_type) !== false) {
$this->log->info("does it get here");
$format_info = explode("_", $format);
break;
}
}
return $format_info[1];
}

}
29 changes: 29 additions & 0 deletions Homarus/src/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';

use Islandora\Crayfish\Commons\Provider\IslandoraServiceProvider;
use Islandora\Crayfish\Commons\Provider\YamlConfigServiceProvider;
use Islandora\Homarus\Controller\HomarusController;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;


$app = new Application();

$app->register(new IslandoraServiceProvider());
$app->register(new YamlConfigServiceProvider(__DIR__ . '/../cfg/config.yaml'));

$app['homarus.controller'] = function ($app) {
return new HomarusController(
$app['crayfish.cmd_execute_service'],
$app['crayfish.homarus.mime_types.valid'],
$app['crayfish.homarus.mime_types.default_video'],
$app['crayfish.homarus.executable'],
$app['monolog'],
$app['crayfish.homarus.mime_to_format']
);
};

$app->get('/convert', "homarus.controller:convert");
dannylamb marked this conversation as resolved.
Show resolved Hide resolved

return $app;
4 changes: 4 additions & 0 deletions Homarus/src/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$app = require_once(__DIR__ . '/app.php');
$app->run();
18 changes: 18 additions & 0 deletions Homarus/static/convert.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@prefix apix:<http://fedora.info/definitions/v4/api-extension#> .
@prefix owl:<http://www.w3.org/2002/07/owl#> .
@prefix ebucore:<http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#> .
@prefix ldp:<http://www.w3.org/ns/ldp#> .
@prefix islandora:<http://islandora.ca/CLAW#> .
@prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#> .

<> a apix:Extension;
rdfs:label "Ffmpeg Service";
rdfs:comment "ffmpeg as a microservice";
apix:exposesService islandora:ConvertService;
apix:exposesServiceAt "svc:convert";
apix:bindsTo <#class> .

<#class> owl:intersectionOf (
ldp:NonRDFSource
[ a owl:Restriction; owl:onProperty ebucore:hasMimeType; owl:hasValue "video/mp4", "video/x-msvideo", "video/ogg" ]
) .
18 changes: 18 additions & 0 deletions Homarus/static/identify.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@prefix apix:<http://fedora.info/definitions/v4/api-extension#> .
dannylamb marked this conversation as resolved.
Show resolved Hide resolved
@prefix owl:<http://www.w3.org/2002/07/owl#> .
@prefix ebucore:<http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#> .
@prefix ldp:<http://www.w3.org/ns/ldp#> .
@prefix islandora:<http://islandora.ca/CLAW#> .
@prefix rdfs:<http://www.w3.org/2000/01/rdf-schema#> .

<> a apix:Extension;
rdfs:label "Ffmpeg Identification Service";
rdfs:comment "ffmpeg's identify as a microservice";
apix:exposesService islandora:IdentifyService;
apix:exposesServiceAt "svc:identify";
apix:bindsTo <#class> .

<#class> owl:intersectionOf (
ldp:NonRDFSource
[ a owl:Restriction; owl:onProperty ebucore:hasMimeType; owl:hasValue "video/mp4", "video/x-msvideo", "video/ogg" ]
) .