You are here

public static function Minisite::validateArchive in Mini site 8

Validate archive.

Can be used at early stages before Minisite instance is created (i.e. when uploading a file) to validate the archive.

Parameters

\Drupal\file\FileInterface $file: The archive file to validate.

string $content_extensions: Space-separated string list of allowed file extensions in the archive.

Throws

\Drupal\minisite\Exception\ArchiveException Throws one of the descendants of this exception based on validation failures.

Overrides MinisiteInterface::validateArchive

2 calls to Minisite::validateArchive()
Minisite::setArchiveFile in src/Minisite.php
Set archive file.
minisite_validate_archive in ./minisite.module
Validation callback.

File

src/Minisite.php, line 300

Class

Minisite
Class Minisite.

Namespace

Drupal\minisite

Code

public static function validateArchive(FileInterface $file, $content_extensions) {

  // Does physical file exist?
  if (!is_readable($file
    ->getFileUri())) {
    throw new MissingArchiveException($file
      ->getFileUri());
  }

  // Does it have a correct extension?
  FileValidator::validateFileExtension($file
    ->getFilename(), static::supportedArchiveExtensions());

  // Is it a valid archive?
  // File URI is different from file name is this is a file being uploaded,
  // so we must provide both to correctly instantiate the archiver.
  $archiver = self::getArchiver($file
    ->getFileUri(), $file
    ->getFilename());
  if (!$archiver) {
    throw new InvalidFormatArchiveException($file
      ->getFilename());
  }
  try {
    $files = $archiver
      ->listContents();
  } catch (\Exception $exception) {
    throw new InvalidFormatArchiveException($file
      ->getFilename());
  }

  // Does it have correct structure?
  ArchiveValidator::validate($files, $content_extensions);
}