You are here

function file_entity_validate_size_extensions in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.pages.inc \file_entity_validate_size_extensions()

File

./file_entity.pages.inc, line 209
Supports file operations including View, Edit, and Delete.

Code

function file_entity_validate_size_extensions(stdClass $file, $file_limit = 0, $user_limit = 0) {
  global $user;
  $errors = array();

  // Current file extension.
  $current_extension = pathinfo($file->filename, PATHINFO_EXTENSION);

  // Get list of extensions.
  $extensions = explode("\n", variable_get('file_entity_max_filesize_extensions'));
  if ($extensions) {
    foreach ($extensions as $position => $text) {
      $matches = array();
      preg_match('/(.*)\\|(.*)/', $text, $matches);
      if (is_array($matches) && count($matches) == 3) {
        $extension = $matches[1];
        $filesize = $matches[2];
        if (strtolower($extension) == strtolower($current_extension)) {
          $file_limit = parse_size($filesize);
        }
      }
    }
  }
  if ($file_limit && $file->filesize > $file_limit) {
    $errors[] = t('The file is %filesize exceeding the maximum file size of %maxsize.', array(
      '%filesize' => format_size($file->filesize),
      '%maxsize' => format_size($file_limit),
    ));
  }

  // Save a query by only calling file_space_used() when a limit is provided.
  if ($user_limit && file_space_used($user->uid) + $file->filesize > $user_limit) {
    $errors[] = t('The file is %filesize which would exceed your disk quota of %quota.', array(
      '%filesize' => format_size($file->filesize),
      '%quota' => format_size($user_limit),
    ));
  }
  return $errors;
}