5

i am serving .pdf files using nginx web server. and setting filename of .pdf file using rewrite rule , depending on the title args.

what i am trying to do is, add my domain name example.com in the filename

1. if title var contains example.com in it then use title var data for filename.

e.g.

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=[example.com]ebook
Filename : [example.com]ebook.pdf

2. if title var does not contains example.com in it then make filename as [example.com]title var.pdf

e.g.

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf?title=ebook
Filename : [example.com]ebook.pdf

3. if title is not set then use filename as [example.com]hash.pdf filename.

e.g.

URL : http://example.com/pdf/0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf
Filename : [example.com]0D2E8C9DC2F732A6ACC6150811515F697E3C376C.pdf

How can i achieve something like this. ?


my current config looks like this

#pdf download block
location ~* /pdf/([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]+)\.pdf$ {

#if title is not set then use hash.pdf as filename
if ( $arg_title  = '' ) {
    add_header Content-Disposition 'attachment; filename="[example.com]$1$2$3$4$5$6.pdf"';
}

add_header Content-Disposition 'attachment; filename="[example.com]$arg_title.pdf"';
alias   /var/www/example.com/pdf/$1/$2/$3/$4/$5/$1$2$3$4$5$6.pdf;
expires 1y;
}

above code works fine only if title var is not set at all, but if i set title var as [example.com]ebook.pdf it adds domain name one more time and the final filename becomes

[example.com][example.com]ebook.pdf 
  • Your question is really difficult to understand. Please restate what you're trying to achieve, both big picture (ie what are you trying to achieve and why) and details, such as what the "title" you're referring to is. – Tim Mar 20 '16 at 19:19
  • @tim sorry for my bad english, i have updated the question., thanks –  Mar 21 '16 at 02:04
  • Are you trying to build a semi-secure e-book delivery system using a reverse proxy? Have you considered using a product made to do what you're trying to achieve? – Tim Mar 21 '16 at 02:31
  • nope. i am just trying to serve files using nginx rewrite and setting file name depending the title variable in the url. –  Mar 21 '16 at 04:38
  • Looks like a regular expression problem, maybe you should add that tag. I can't help but good luck :) – Tim Mar 21 '16 at 05:07
  • do you want to serve changed filenames from disk, or do you want keep disk location same and want to have downloaded filename changed? – Andrei Mikhaltsov Mar 22 '16 at 13:50
  • @AndreiMikhaltsov Downloaded filename changed. so it stores on user PC so it can remember where it downloaded from. –  Mar 22 '16 at 14:10
  • 1
    What you're trying to do is bad from the usability perspective -- with ***attachment*** as `Content-Disposition` you're ***forcing people to download your PDF*** -- users will have find a location and name for it, and then bother finding a programme to view it in. I don't download any PDFs personally. If the browser doesn't let me view a PDF, I go to the next one. – cnst Mar 26 '16 at 21:53

1 Answers1

5

As far as I understood, we can assume that if there is an [example.com] substring in the beginning of the $arg_title variable, we can just safely cut it out. So here is the config that you need:

location ~* /pdf/([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F]+)\.pdf$ {

 #First of all we should put pdf path from the request into a variable,
 #since $1,$2,$n would contain other data later:
set $hashpath $1/$2/$3/$4/$5/$1$2$3$4$5$6;

 #Same applies to hash title:
set $hashtitle $1$2$3$4$5$6;

 #Now we try to get the title argument without getting [example.com] in the beginning:
if ( $arg_title ~ "^(\[example\.com\])?(.*)$")
{
   #This way if title contained [example.com], it would go to $1 variable, while $2 has our new cleaned title:
  set $pdftitle $2;
}

 #now we check if title was empty:
if ( $arg_title  = '' ) {
 #and assign our title that $hashtitle variable we got from the request earlier:
    set $pdftitle $hashtitle;
}

 #also we check if title has only [example.com] without anything else:
if ( $arg_title  = '[example.com]' ) {
    set $pdftitle $hashtitle;
}

 #After that we can safely put [example.com] in front of our filename, without worrying that there would be a double:
add_header Content-Disposition 'attachment; filename="[example.com]$pdftitle.pdf"';

alias /var/www/example.com/pdf/$hashpath.pdf;
expires 1y;
}

Works fine with nginx 1.9.12 here:

title=[example.com]test

title=just_test

title is not set

with actual file located here

Anubioz
  • 3,677
  • 18
  • 23
  • thanks for your answer, my files location on HDD is like alias /var/www/example.com/pdf/$1/$2/$3/$4/$5/$1$2$3$4$5$6.pdf; –  Mar 28 '16 at 15:10
  • I've updated my answer for your particular environment! – Anubioz Mar 28 '16 at 15:53