Download from a rails app in production to ios does not have a filesize

148 Views Asked by At

I have a rails app in production on a server using nginx and unicorn to serve files for an iOS app, i need an authentication process in order to send the files to a client, the code i use to do it is:

def download_pdf
  if can_purchase
    route = @asset_file.asset_zip.url
    send_file route, :type=>"application/zip", :x_sendfile=>true
  else
    render :status=>400, :json=>{:message=>"The user cannot download this issue."}
  end
end

The url of the asset file is a location inside the Rails app directory on /downloads/zip/filename.zip. On the iOS app i have a delegate to listen for the event dispatched when a chunk of downloaded data is written so i can update a progress view. The code is very simple and it goes like this:

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
 didWriteData:(int64_t)bytesWritten 
 totalBytesWritten:(int64_t)totalBytesWritten 
 totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

  if (totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown) {
      NSLog(@"Unknown transfer size");
  }
  else{
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
              self.zipDownloadInfo.downloadProgress = 
              (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
              self.progressBarZip.progress = self.zipDownloadInfo.downloadProgress;
    }];
  }
}

When i was on development stage this worked like a charm, but when i uploaded to my server the download process does not displays the size of the download, but it does downloads the file correctly anyway. Now i am using the X-Accel-Redirect header directive for nginx. My unicorn.conf file is pretty standard and my nginx.conf configuration files is:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/xml text/css text/comma-separated-values;
    upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

    include /etc/nginx/conf.d/*.conf;
    server {
    listen   80;
    root /home/rails/public;
    server_name _;
    index index.htm index.html;

    location / {
            try_files $uri/index.html $uri.html $uri @app;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                    try_files $uri @app;
            }

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
}

}
}

UPDATE I found a blog post that points out that i need to set the X-Accel_Mapping header, now i have done it but i get a not found route error, this are the lines i added to nginx.conf

    location /downloads/zip/ {
            internal;
            root /home/rails/;
    }

     location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
            proxy_set_header X-Sendfile-Type X-Accel-Redirect;
            proxy_set_header X-Accel-Mapping /downloads/zip/=/downloads/zip/;
    }
0

There are 0 best solutions below