fedora = $fedora; $this->mimeTypeGuesser = $mime_type_guesser; $this->languageManager = $language_manager; $this->logger = $logger; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { // Construct guzzle client to middleware that adds JWT. $stack = HandlerStack::create(); $stack->push(static::addJwt($container->get('jwt.authentication.jwt'))); $fedora = FedoraApi::createWithHandler($configuration['root'], $stack); // Return it. return new static( $fedora, $container->get('file.mime_type.guesser'), $container->get('language_manager'), $container->get('logger.channel.fedora_flysystem') ); } /** * Guzzle middleware to add a header to outgoing requests. * * @param \Drupal\jwt\Authentication\Provider\JwtAuth $jwt * JWT. */ public static function addJwt(JwtAuth $jwt) { return function (callable $handler) use ($jwt) { return function ( RequestInterface $request, array $options ) use ( $handler, $jwt ) { $request = $request->withHeader('Authorization', 'Bearer ' . $jwt->generateToken()); return $handler($request, $options); }; }; } /** * {@inheritdoc} */ public function getAdapter() { return new FedoraAdapter($this->fedora, $this->mimeTypeGuesser, $this->logger); } /** * {@inheritdoc} */ public function ensure($force = FALSE) { // Check fedora root for sanity. try { $response = $this->fedora->getResourceHeaders(''); $statusCode = $response->getStatusCode(); $message = '%url returned %status'; } catch (ConnectException $e) { // Fedora is unavailable. $message = '%url is unavailable, cannot connect.'; $statusCode = 500; } if ($statusCode != 200) { return [ [ 'severity' => RfcLogLevel::ERROR, 'message' => $message, 'context' => [ '%url' => $this->fedora->getBaseUri(), '%status' => $statusCode, ], ], ]; } return []; } /** * {@inheritdoc} */ public function getExternalUrl($uri) { $path = str_replace('\\', '/', $this->getTarget($uri)); $arguments = [ 'scheme' => $this->getScheme($uri), 'filepath' => $path, ]; // Force file urls to be language neutral. $undefined = $this->languageManager->getLanguage('und'); return Url::fromRoute( 'flysystem.serve', $arguments, ['absolute' => TRUE, 'language' => $undefined] )->toString(); } }