epub_reader_framework-2.0.0-alpha2/src/EpubReaderFrameworkHelpers.php

src/EpubReaderFrameworkHelpers.php
<?php

namespace Drupal\epub_reader_framework;

use Drupal\epub_reader_framework\Batch\ReaderChapterExtractorBatchProcessor;
use Drupal\file\FileInterface;
use Drupal\node\NodeInterface;

/**
 * General utility functions for the epub reader framework.
 */
class EpubReaderFrameworkHelpers {

  /**
   * Get the EPUB file for a publication.
   *
   * @param \Drupal\node\NodeInterface $reader_publication
   *   The reader publication.
   *
   * @return bool|\Drupal\file\FileInterface
   *   The EPUB file or false.
   */
  public static function getEpubFile(NodeInterface $reader_publication) {
    if ($reader_publication->hasField('field_reader_file') && $media_file = $reader_publication->field_reader_file->entity) {
      /** @var \Drupal\media\MediaInterface $media_file */
      if ($media_file->hasField('field_media_file') && $file = $media_file->field_media_file->entity) {
        /** @var \Drupal\file\FileInterface $file */
        return $file;
      }
    }
    return FALSE;
  }

  /**
   * Get the EPUB file path for a publication.
   *
   * @param \Drupal\node\NodeInterface $reader_publication
   *   The reader publication.
   * @param bool|\Drupal\file\FileInterface $file
   *   The EPUB file or FALSE to attempt to automatically determine it.
   *
   * @return bool|string
   *   The path to the extracted content or false.
   */
  public static function getEpubExtractedFilesPath(NodeInterface $reader_publication, $file = FALSE) {
    if (!$file || !$file instanceof FileInterface) {
      $file = self::getEpubFile($reader_publication);
    }
    if ($file && $file instanceof FileInterface) {

      // Determine storage location.
      /** @var \Drupal\Core\StreamWrapper\StreamWrapperManager $stream_wrapper_manager */
      $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
      $stream_wrappers = $stream_wrapper_manager->getWrappers();
      if (isset($stream_wrappers['private']) && $stream_wrappers['private']) {
        $path_uri = 'private://';
      }
      else {
        $path_uri = \Drupal::config('system.file')->get('default_scheme') . '://';
      }

      // Build the path per publication, per file.
      $path_uri .= 'reader-content/publication-' . $reader_publication->id() . '/file-' . $file->id() . '/';

      return $path_uri;
    }

    return FALSE;
  }

  /**
   * Start the extraction batch process.
   *
   * @param \Drupal\node\NodeInterface $reader_publication
   *   The reader publication node.
   */
  public static function startExtractionBatch(NodeInterface $reader_publication): void {
    $batch = [
      'operations' => [
        [
          [
            ReaderChapterExtractorBatchProcessor::class,
            'operationCallback',
          ],
          [
            $reader_publication->id(),
          ],
        ],
      ],
      'finished' => [
        ReaderChapterExtractorBatchProcessor::class,
        'finishedCallback',
      ],
      'title' => t('Extracting chapters from the EPUB'),
      'error_message' => t('Chapter extraction has encountered an error.'),
      'init_message' => t('Chapter extraction is starting.'),
      'progress_message' => t('Currently extracting chapters.'),
    ];
    batch_set($batch);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc