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=========================================================
20 (function(window, angular) {
22 angular.module('abs').service('policyUploader', ['$http', '$q', 'policyManagerConfig', function ($http, $q, policyManagerConfig) {
24 function deferredHandler(data, deferred, errorMessage) {
25 if (!data || typeof data !== 'object') {
26 return deferred.reject('Bridge response error, please check the docs');
28 if (data.result && data.result.error) {
29 return deferred.reject(data);
32 return deferred.reject(data);
35 return deferred.reject(errorMessage);
37 deferred.resolve(data);
40 this.requesting = false;
41 this.upload = function(fileList, path) {
42 if (! window.FormData) {
43 throw new Error('Unsupported browser version');
46 var form = new window.FormData();
47 var deferred = $q.defer();
48 form.append('destination', '/' + path.join('/'));
50 for (var i = 0; i < fileList.length; i++) {
51 var fileObj = fileList.item(i);
52 fileObj instanceof window.File && form.append('file-' + i, fileObj);
55 self.requesting = true;
56 $http.post(policyManagerConfig.uploadUrl, form, {
57 transformRequest: angular.identity,
59 'Content-Type': undefined
61 }).success(function(data) {
62 deferredHandler(data, deferred);
63 }).error(function(data) {
64 deferredHandler(data, deferred, 'Unknown error uploading files');
65 })['finally'](function() {
66 self.requesting = false;
69 return deferred.promise;