You are here

public function TrackDaFilesController::tracking in Track da files 8

1 string reference to 'TrackDaFilesController::tracking'
track_da_files.routing.yml in ./track_da_files.routing.yml
track_da_files.routing.yml

File

src/Controller/TrackDaFilesController.php, line 533

Class

TrackDaFilesController
Class TrackDaFilesController.

Namespace

Drupal\track_da_files\Controller

Code

public function tracking($filedir, $filename) {
  $current_uri = Url::fromRoute('<current>');
  $file_uri = file_build_uri($current_uri
    ->getInternalPath());
  $roles = \Drupal::config('track_da_files.settings')
    ->get('specific_roles');
  $account = \Drupal::currentUser();
  $track = track_da_files_roles($account);
  if (!isset($_GET['file'])) {

    // Our menu hook wasn't called, so we should ignore this.
    return;
  }
  $scheme = file_uri_scheme($file_uri);
  $file_uri = strtok($file_uri, '?');
  $file_name = basename($file_uri);
  $parts = parse_url($current_uri
    ->getInternalPath());
  $file_relative_path = str_replace('system/tdf/', '', $parts['path']);
  $uri = $scheme . '://' . $file_relative_path;

  // Retrieve entity id information in query parameters.
  if (isset($_GET['id'])) {
    $id = $_GET['id'];
  }
  if (isset($_GET['type'])) {
    $type = $_GET['type'];
  }
  if (isset($_GET['force'])) {
    $force = $_GET['force'];
  }
  $query = "SELECT f.fid, f.filename, f.filemime, f.filesize FROM {file_managed} f WHERE f.uri = :uri";
  $result = db_query($query, array(
    ':uri' => $uri,
  ))
    ->fetch();
  $filemime = '';
  $filesize = '';
  if (!empty($result)) {
    $filename = isset($result->filename) ? $result->filename : '';
    $fid = isset($result->fid) ? $result->fid : '';
    $filemime = isset($result->filemime) ? $result->filemime : '';
    $filesize = isset($result->filesize) ? $result->filesize : '';
  }

  // If uri exists and valid uri scheme interaction with database begins.
  if (!empty($fid) && $track) {
    if (!empty($id) && !empty($type)) {
      track_da_files_register_new_display($uri, $fid, $id, $type);
    }
    else {
      track_da_files_register_new_display($uri, $fid);
    }
  }
  $headers = array(
    'Content-Type' => $filemime,
    'Content-Length' => $filesize,
  );
  if (isset($force) && $force == 1) {
    $headers['Content-Disposition'] = 'attachment; filename="' . $filename . '"';
  }
  $response = new Response();
  return new BinaryFileResponse($uri, 200, $headers);
}