2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 (function(window, angular) {
23 angular.module('abs').service('fileUploader', ['$http', '$q', 'fileManagerConfig', function ($http, $q, fileManagerConfig) {
25 function deferredHandler(data, deferred, errorMessage) {
26 if (!data || typeof data !== 'object') {
27 return deferred.reject('Bridge response error, please check the docs');
29 if (data.result && data.result.error) {
30 return deferred.reject(data);
33 return deferred.reject(data);
36 return deferred.reject(errorMessage);
38 deferred.resolve(data);
41 this.requesting = false;
42 this.upload = function(fileList, path) {
43 if (! window.FormData) {
44 throw new Error('Unsupported browser version');
47 var form = new window.FormData();
48 var deferred = $q.defer();
49 form.append('destination', '/' + path.join('/'));
51 for (var i = 0; i < fileList.length; i++) {
52 var fileObj = fileList.item(i);
53 fileObj instanceof window.File && form.append('file-' + i, fileObj);
56 self.requesting = true;
57 $http.post(fileManagerConfig.uploadUrl, form, {
58 transformRequest: angular.identity,
60 'Content-Type': undefined
62 }).success(function(data) {
63 deferredHandler(data, deferred);
64 }).error(function(data) {
65 deferredHandler(data, deferred, 'Unknown error uploading files');
66 })['finally'](function() {
67 self.requesting = false;
70 return deferred.promise;