You are here

function filehash_file_validate in File Hash 7

Same name and namespace in other branches
  1. 8 filehash.module \filehash_file_validate()

Implements hook_file_validate().

File

./filehash.module, line 97
Generate hashes for each uploaded file.

Code

function filehash_file_validate($file) {
  $errors = array();

  // Zero size may indicate unreadable remote media source; bypass dedupe.
  if ($file->filesize && variable_get('filehash_dedupe', 0)) {
    foreach (filehash_algos() as $algo) {
      if ($fid = db_query("SELECT fid FROM {filehash} WHERE {$algo} = :hash LIMIT 1", array(
        ':hash' => hash_file($algo, $file->uri),
      ))
        ->fetchField()) {
        $duplicate = file_load($fid);
        if (file_download_access($duplicate->uri)) {
          $errors[] = t('This file has already been uploaded as %filename.', array(
            '%filename' => $duplicate->filename,
          ));
        }
        else {
          $errors[] = t('Sorry, duplicate files are not permitted.');
        }
        break;
      }
    }
  }
  return $errors;
}