0

In a content type I have option for pdf file upload, the uploaded files are stored in private folder.

The uploaded file has absolute url of http://example.com/system/files/abc.pdf

At the moment anyone with the absolute url can download the file (no matter the user roles, access and permissions)

I want to redirect visitors to the front page of the website (instead of downloading) if they use the absolute url of pdf in browser to download it.

So far I tried something with JavaScript something like:

$(document).ready(function() {
  if(window.location.href.indexOf(".pdf") > -1) {
    $(location).attr('href', window.location.protocol + "//" + window.location.host); // Redirect to front page
  }
});

Usually this would do the trick if it was rendered webpage where javaScript is loaded, but it doesn't work with pdf files (as javaScript is not loaded while clicking the absolute url of the pdf file).

I have already tried private files download permission module, but that doesn't do what I want, as my downloadable links are accessible to Anonymous users as well.

Any hints, suggestions, how to accomplish this?

0

It should be as easy as adding an implementation of hook_file_download() to a custom module:

function MYMODULE_file_download($file) {
  // Add whatever other conditions you need to this, e.g. test the
  // file extension for .pdf
  if (user_is_anonymous()) {
    // Deny
    return -1;
  }

  // Allow
  return file_get_content_headers($file);
}
  • couldn't thank you more !!! have a nice weekend :-) – user12947 Aug 6 '15 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy