Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / policy-models / Editor / src / js / services / fileuploader.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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
21 (function(window, angular) {
22     'use strict';
23     angular.module('abs').service('fileUploader', ['$http', '$q', 'fileManagerConfig', function ($http, $q, fileManagerConfig) {
24
25         function deferredHandler(data, deferred, errorMessage) {
26             if (!data || typeof data !== 'object') {
27                 return deferred.reject('Bridge response error, please check the docs');
28             }
29             if (data.result && data.result.error) {
30                 return deferred.reject(data);
31             }
32             if (data.error) {
33                 return deferred.reject(data);
34             }
35             if (errorMessage) {
36                 return deferred.reject(errorMessage);
37             }
38             deferred.resolve(data);
39         }
40
41         this.requesting = false; 
42         this.upload = function(fileList, path) {
43             if (! window.FormData) {
44                 throw new Error('Unsupported browser version');
45             }
46             var self = this;
47             var form = new window.FormData();
48             var deferred = $q.defer();
49             form.append('destination', '/' + path.join('/'));
50
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);
54             }
55
56             self.requesting = true;
57             $http.post(fileManagerConfig.uploadUrl, form, {
58                 transformRequest: angular.identity,
59                 headers: {
60                     'Content-Type': undefined
61                 }
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;
68             });
69
70             return deferred.promise;
71         };
72     }]);
73 })(window, angular);