[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / js / services / policyuploader.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 (function(window, angular) {
21     'use strict';
22     angular.module('abs').service('policyUploader', ['$http', '$q', 'policyManagerConfig', function ($http, $q, policyManagerConfig) {
23
24         function deferredHandler(data, deferred, errorMessage) {
25             if (!data || typeof data !== 'object') {
26                 return deferred.reject('Bridge response error, please check the docs');
27             }
28             if (data.result && data.result.error) {
29                 return deferred.reject(data);
30             }
31             if (data.error) {
32                 return deferred.reject(data);
33             }
34             if (errorMessage) {
35                 return deferred.reject(errorMessage);
36             }
37             deferred.resolve(data);
38         }
39
40         this.requesting = false; 
41         this.upload = function(fileList, path) {
42             if (! window.FormData) {
43                 throw new Error('Unsupported browser version');
44             }
45             var self = this;
46             var form = new window.FormData();
47             var deferred = $q.defer();
48             form.append('destination', '/' + path.join('/'));
49
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);
53             }
54
55             self.requesting = true;
56             $http.post(policyManagerConfig.uploadUrl, form, {
57                 transformRequest: angular.identity,
58                 headers: {
59                     'Content-Type': undefined
60                 }
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;
67             });
68
69             return deferred.promise;
70         };
71     }]);
72 })(window, angular);