Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A gem that uses [angular-file-upload](https://github.com/nervgh/angular-file-upl
Add this line to your application's Gemfile:

```ruby
gem 'angularjs-file-upload-rails', '~> 1.1.0'
gem 'angularjs-file-upload-rails', '~> 1.1.5'
```

And then execute:
Expand Down Expand Up @@ -42,7 +42,7 @@ read more about the options in [angular-file-upload-wiki](https://github.com/ner
\* *assuming that you have setup an ```angularjs``` correctly in your rails app

```ruby
gem 'angularjs-file-upload-rails', '~> 1.1.0'
gem 'angularjs-file-upload-rails', '~> 1.1.5'
gem 'carrierwave'
gem 'rails', '4.1.5'
```
Expand Down
54 changes: 33 additions & 21 deletions app/assets/javascripts/angularjs-file-upload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
angular-file-upload v1.1.0
angular-file-upload v1.1.5
https://github.com/nervgh/angular-file-upload
*/
(function(angular, factory) {
Expand Down Expand Up @@ -344,7 +344,7 @@ module
FileUploader.prototype._getFilters = function(filters) {
if (angular.isUndefined(filters)) return this.filters;
if (angular.isArray(filters)) return filters;
var names = filters.split(/\s*,/);
var names = filters.match(/[^\s,]+/g);
return this.filters.filter(function(filter) {
return names.indexOf(filter.name) !== -1;
}, this);
Expand Down Expand Up @@ -400,12 +400,14 @@ module
/**
* Transforms the server response
* @param {*} response
* @param {Object} headers
* @returns {*}
* @private
*/
FileUploader.prototype._transformResponse = function(response) {
FileUploader.prototype._transformResponse = function(response, headers) {
var headersGetter = this._headersGetter(headers);
angular.forEach($http.defaults.transformResponse, function(transformFn) {
response = transformFn(response);
response = transformFn(response, headersGetter);
});
return response;
};
Expand All @@ -421,17 +423,10 @@ module

if (!headers) return parsed;

function trim(string) {
return string.replace(/^\s+/, '').replace(/\s+$/, '');
}
function lowercase(string) {
return string.toLowerCase();
}

angular.forEach(headers.split('\n'), function(line) {
i = line.indexOf(':');
key = lowercase(trim(line.substr(0, i)));
val = trim(line.substr(i + 1));
key = line.slice(0, i).trim().toLowerCase();
val = line.slice(i + 1).trim();

if (key) {
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
Expand All @@ -440,6 +435,20 @@ module

return parsed;
};
/**
* Returns function that returns headers
* @param {Object} parsedHeaders
* @returns {Function}
* @private
*/
FileUploader.prototype._headersGetter = function(parsedHeaders) {
return function(name) {
if (name) {
return parsedHeaders[name.toLowerCase()] || null;
}
return parsedHeaders;
};
};
/**
* The XMLHttpRequest transport
* @param {FileItem} item
Expand All @@ -458,7 +467,7 @@ module
});
});

form.append(item.alias, item._file);
form.append(item.alias, item._file, item.file.name);

xhr.upload.onprogress = function(event) {
var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
Expand All @@ -467,7 +476,7 @@ module

xhr.onload = function() {
var headers = that._parseHeaders(xhr.getAllResponseHeaders());
var response = that._transformResponse(xhr.response);
var response = that._transformResponse(xhr.response, headers);
var gist = that._isSuccessCode(xhr.status) ? 'Success' : 'Error';
var method = '_on' + gist + 'Item';
that[method](item, response, xhr.status, headers);
Expand All @@ -476,14 +485,14 @@ module

xhr.onerror = function() {
var headers = that._parseHeaders(xhr.getAllResponseHeaders());
var response = that._transformResponse(xhr.response);
var response = that._transformResponse(xhr.response, headers);
that._onErrorItem(item, response, xhr.status, headers);
that._onCompleteItem(item, response, xhr.status, headers);
};

xhr.onabort = function() {
var headers = that._parseHeaders(xhr.getAllResponseHeaders());
var response = that._transformResponse(xhr.response);
var response = that._transformResponse(xhr.response, headers);
that._onCancelItem(item, response, xhr.status, headers);
that._onCompleteItem(item, response, xhr.status, headers);
};
Expand Down Expand Up @@ -519,7 +528,9 @@ module

angular.forEach(item.formData, function(obj) {
angular.forEach(obj, function(value, key) {
form.append(angular.element('<input type="hidden" name="' + key + '" value="' + value + '" />'));
var element = angular.element('<input type="hidden" name="' + key + '" />');
element.val(value);
form.append(element);
});
});

Expand Down Expand Up @@ -549,8 +560,8 @@ module
} catch (e) {}

var xhr = {response: html, status: 200, dummy: true};
var response = that._transformResponse(xhr.response);
var headers = {};
var response = that._transformResponse(xhr.response, headers);

that._onSuccessItem(item, response, xhr.status, headers);
that._onCompleteItem(item, response, xhr.status, headers);
Expand Down Expand Up @@ -1155,7 +1166,7 @@ module
* Event handler
*/
FileDrop.prototype.onDragLeave = function(event) {
if (event.target !== this.element[0]) return;
if (event.currentTarget !== this.element[0]) return;
this._preventAndStop(event);
angular.forEach(this.uploader._directives.over, this._removeOverClass, this);
};
Expand Down Expand Up @@ -1316,5 +1327,6 @@ module
}
};
}])

return module;
}));
}));
2 changes: 1 addition & 1 deletion lib/angularjs-file-upload/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AngularjsFileUpload
VERSION = '1.1.0'
VERSION = '1.1.5'
end