How to change filename on the fly depending on the title args - nginx rewrite

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 

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