You are here

function filemime_apply_process in File MIME 7

Batch process callback.

1 string reference to 'filemime_apply_process'
filemime_apply_submit in ./filemime.admin.inc
Retroactively applies MIME type mapping to all previously uploaded files.

File

./filemime.admin.inc, line 63
Admin page callbacks for File MIME module.

Code

function filemime_apply_process(&$context) {
  if (!isset($context['results']['processed'])) {
    $context['results']['processed'] = 0;
    $context['results']['updated'] = 0;
    $context['sandbox']['count'] = filemime_count();
    $context['sandbox']['schemes'] = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
  }
  $files = db_select('file_managed', 'f')
    ->fields('f', array(
    'fid',
    'filemime',
    'uri',
  ))
    ->range($context['results']['processed'], 1)
    ->execute();
  foreach ($files as $file) {

    // Only operate on local stream URIs, which should represent file names.
    $scheme = file_uri_scheme($file->uri);
    if ($scheme && isset($context['sandbox']['schemes'][$scheme])) {
      $filemime = file_get_mimetype($file->uri);
      if ($file->filemime != $filemime) {
        $variables = array(
          '%old' => $file->filemime,
          '%new' => $filemime,
          '%url' => $file->uri,
        );

        // Fully load file object.
        $file = file_load($file->fid);
        $file->filemime = $filemime;
        file_save($file);
        $context['results']['updated']++;
        $context['message'] = t('Updated MIME type from %old to %new for %url.', $variables);
        watchdog('filemime', 'Updated MIME type from %old to %new for %url.', $variables);
      }
    }
    $context['results']['processed']++;
    $context['finished'] = $context['results']['processed'] / $context['sandbox']['count'];
  }
}