Fix race condition deploying and uploading files
Fix race condition deploying and uploading files

Fix race condition that may happen when deploying new code and enqueuing
files for upload to S3 at the same time. Refer to this code here:

https://github.com/CartoDB/cartodb/blob/e57f09f3b6fb18f0eef32499d4137178c4cd166f/lib/tasks/import.rake#L43-L51

and check the annotated version below. What happens is:

```ruby

filepath = data_import_item.data_source # => "/home/ubuntu/www/production.cartodb.com/releases/20170221104214/public/uploads/1a45870296e683c05b3b/permits_2013.csv"
filename = data_import_item.data_source.slice(data_import_item.data_source.rindex('/') + 1,
                                              data_import_item.data_source.length) # => "permits_2013.csv"

uploads_path = file_upload_helper.get_uploads_path # => "/home/ubuntu/www/production.cartodb.com/releases/20170221145321/public/uploads"

token = data_import_item.data_source.slice(/#{uploads_path}\/(.)+\//) # => nil
                                    .sub("#{uploads_path}/", '') # => NoMethodError: undefined method `sub' for nil:NilClass
                                    .sub('/', '')
```

So, essentially we have a race condition. In pseudo-code:
1. Version X of the rails code stores an uploaded file in dir Y/public/uploads
1. A deployment takes place, replacing X with Y. Pending requests are dealt by version X. New requests are dealt by version Y.
1. The async cron job in charge of uploading the file to S3 is run, searching for the file in dir Y/public/uploads => Exception

This commit fixes it by using `File.realpath` (to get the "real path"
when using symlinks) and by using `File.basename` and `File.dirname`
instead of parsing the paths through a regexp.

CartoDB/cartodb,splashblot/dronedb,CartoDB/cartodb,splashblot/dronedb,CartoDB/cartodb,splashblot/dronedb,CartoDB/cartodb,splashblot/dronedb,CartoDB/cartodb,splashblot/dronedb