Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Updates derivative action for Flysystem (#24)
Browse files Browse the repository at this point in the history
* Updating form and testing tokens

* Adding token to composer.json

* Token replacement for file upload location

* working

* Tests

* Whoops

* whitespace
  • Loading branch information
dannylamb authored and whikloj committed Aug 15, 2018
1 parent 9f257bb commit 9b3c456
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"irc": "irc://irc.freenode.net/islandora"
},
"require": {
"islandora/islandora": "dev-8.x-1.x"
"islandora/islandora": "dev-8.x-1.x",
"drupal/token" : "^1.3"
}
}
6 changes: 6 additions & 0 deletions config/schema/islandora_image.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ action.configuration.generate_image_derivative:
args:
type: text
label: 'Convert Arguments'
scheme:
type: text
label: 'Flysystem scheme'
path:
type: text
label: 'File path with extension'
71 changes: 50 additions & 21 deletions src/Plugin/Action/GenerateImageDerivative.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\islandora_image\Plugin\Action;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
Expand All @@ -11,6 +12,7 @@
use Drupal\islandora\EventGenerator\EventGeneratorInterface;
use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\jwt\Authentication\Provider\JwtAuth;
use Drupal\token\Token;
use Stomp\StatefulStomp;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -39,6 +41,13 @@ class GenerateImageDerivative extends EmitEvent {
*/
protected $mediaSource;

/**
* Token replacement service.
*
* @var \Drupal\token\Token
*/
protected $token;

/**
* Constructs a EmitEvent action.
*
Expand All @@ -62,6 +71,8 @@ class GenerateImageDerivative extends EmitEvent {
* Islandora utility functions.
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source
* Media source service.
* @param \Drupal\token\Token $token
* Token service.
*/
public function __construct(
array $configuration,
Expand All @@ -73,7 +84,8 @@ public function __construct(
StatefulStomp $stomp,
JwtAuth $auth,
IslandoraUtils $utils,
MediaSourceService $media_source
MediaSourceService $media_source,
Token $token
) {
parent::__construct(
$configuration,
Expand All @@ -87,6 +99,7 @@ public function __construct(
);
$this->utils = $utils;
$this->mediaSource = $media_source;
$this->token = $token;
}

/**
Expand All @@ -103,7 +116,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('islandora.stomp'),
$container->get('jwt.authentication.jwt'),
$container->get('islandora.utils'),
$container->get('islandora.media_source_service')
$container->get('islandora.media_source_service'),
$container->get('token')
);
}

Expand All @@ -118,13 +132,15 @@ public function defaultConfiguration() {
'derivative_term_uri' => '',
'mimetype' => 'image/jpeg',
'args' => '',
'scheme' => file_default_scheme(),
'path' => '[date:custom:Y]-[date:custom:m]/[node:nid].jpg',
];
}

/**
* Override this to return arbitrary data as an array to be json encoded.
*/
protected function generateData($entity) {
protected function generateData(EntityInterface $entity) {
$data = parent::generateData($entity);

// Find media belonging to node that has the source term, and set its file
Expand All @@ -139,18 +155,12 @@ protected function generateData($entity) {
throw new \RuntimeException("Could not locate source media", 500);
}

$source_field = $this->mediaSource->getSourceFieldName($source_media->bundle());
if (!$source_field) {
throw new \RuntimeException("Could not locate source field for media {$source_media->id()}", 500);
}

$files = $source_media->get($source_field)->referencedEntities();
$file = reset($files);
if (!$file) {
$source_file = $this->mediaSource->getSourceFile($source_media);
if (!$source_file) {
throw new \RuntimeException("Could not locate source file for media {$source_media->id()}", 500);
}

$data['source_uri'] = $file->url('canonical', ['absolute' => TRUE]);
$data['source_uri'] = $source_file->url('canonical', ['absolute' => TRUE]);

// Find the term for the derivative and use it to set the destination url
// in the data array.
Expand All @@ -168,23 +178,24 @@ protected function generateData($entity) {
->setAbsolute()
->toString();

// Generate a filename for the derivative.
// Generate an upload file path for the derivative.
$parts = explode('/', $data['mimetype']);
$extension = $parts[1];

$parts = explode('#', $this->configuration['derivative_term_uri']);
if (count($parts) > 1) {
$name = $entity->uuid() . ' - ' . $parts[1];
}
else {
$name = $entity->uuid() . ' - Derivative';
}
$data['filename'] = "$name.$extension";
$token_data = [
'node' => $entity,
'media' => $source_media,
'term' => $derivative_term,
];
$path = $this->token->replace($data['path'], $token_data);
$data['file_upload_uri'] = $data['scheme'] . '://' . $path;

// Get rid of some config so we just pass along
// what islandora-connector-houdini needs.
unset($data['source_term_uri']);
unset($data['derivative_term_uri']);
unset($data['path']);
unset($data['scheme']);

return $data;
}
Expand All @@ -193,6 +204,9 @@ protected function generateData($entity) {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$schemes = $this->utils->getFilesystemSchemes();
$scheme_options = array_combine($schemes, $schemes);

$form = parent::buildConfigurationForm($form, $form_state);
$form['event']['#disabled'] = 'disabled';

Expand Down Expand Up @@ -227,6 +241,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#rows' => '8',
'#description' => t('Additional command line arguments for ImageMagick convert (e.g. -resize 50%'),
];
$form['scheme'] = [
'#type' => 'select',
'#title' => t('File system'),
'#options' => $scheme_options,
'#default_value' => $this->configuration['scheme'],
'#required' => TRUE,
];
$form['path'] = [
'#type' => 'textfield',
'#title' => t('File path'),
'#default_value' => $this->configuration['path'],
'#description' => t('Path within the upload destination where files will be stored. Includes the filename and optional extension.'),
];
return $form;
}

Expand Down Expand Up @@ -276,6 +303,8 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s

$this->configuration['mimetype'] = $form_state->getValue('mimetype');
$this->configuration['args'] = $form_state->getValue('args');
$this->configuration['scheme'] = $form_state->getValue('scheme');
$this->configuration['path'] = trim($form_state->getValue('path'), '\\/');
}

}
6 changes: 4 additions & 2 deletions tests/src/Functional/GenerateImageDerivativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function testGenerateImageDerivativeFromScratch() {
$this->getSession()->getPage()->fillField("edit-derivative-term", $this->serviceFileTerm->label());
$this->getSession()->getPage()->fillField('edit-mimetype', "image/jpeg");
$this->getSession()->getPage()->fillField('edit-args', "-thumbnail 20x20");
$this->getSession()->getPage()->fillField('edit-scheme', "public");
$this->getSession()->getPage()->fillField('edit-path', "derp.jpeg");
$this->getSession()->getPage()->pressButton(t('Save'));
$this->assertSession()->statusCodeEquals(200);

Expand Down Expand Up @@ -180,8 +182,8 @@ protected function checkMessage() {
"Expected destination uri should reference both node and term"
);
$this->assertTrue(
strpos($content['filename'], "ServiceFile.jpeg") !== FALSE,
"Expected filename should contain the name of the derivative term and proper extension"
strpos($content['file_upload_uri'], "public://derp.jpeg") !== FALSE,
"Expected file upload uri should contain the scheme and path of the derivative"
);

$this->assertTrue($content['mimetype'] == 'image/jpeg', "Expected mimetype 'image/jpeg', received {$content['mimetype']}");
Expand Down

0 comments on commit 9b3c456

Please sign in to comment.