You are here

function filehash_file_load in File Hash 8

Same name and namespace in other branches
  1. 7 filehash.module \filehash_file_load()

Implements hook_ENTITY_TYPE_load().

File

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

Code

function filehash_file_load($files) {
  $algos = filehash_algos();
  if (!$algos) {
    return;
  }
  $result = \Drupal::database()
    ->select('filehash')
    ->fields('filehash')
    ->condition('fid', array_keys($files), 'IN')
    ->execute();
  foreach ($result as $record) {
    foreach ($algos as $algo) {
      $files[$record->fid]->filehash[$algo] = $record->{$algo};
    }
  }

  // Generate hash if it does not already exist for the file.
  foreach ($files as $fid => $file) {
    foreach ($algos as $algo) {
      if (empty($file->filehash[$algo])) {
        filehash_hash($files[$fid]);
        filehash_save($files[$fid]);
        break;
      }
    }
  }
}