51c8971e2684b672f7127178098515e13b123474
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / FileUploadService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 app.directive('fileModel', ['$parse', function ($parse) {
24     console.log("////////fileModel");
25     return {
26         restrict: 'A',
27         link: function(scope, element, attrs) {   
28         console.log("link");            
29             var model = $parse(attrs.fileModel);
30                 //alert("uploadFileToUrl directive model :: " + model);
31             var modelSetter = model.assign;
32             
33             element.bind('change', function(){
34                 console.log("change");
35                 scope.$apply(function(){
36                 console.log("apply");                   
37                     modelSetter(scope, element[0].files[0]);
38                 });
39             });
40         }
41     };
42 }]);
43
44
45 app.service('fileUpload', ['$http', '$q', function ($http, $q) {
46     console.log("fileUpload");
47     this.uploadFileToUrl = function(file, uploadUrl){
48         console.log("uploadFileToUrl");
49         //alert("uploadFileToUrl file :: " + file + " :: url::" + uploadUrl);
50         
51         var def = $q.defer();
52         var pars = [];
53         
54         var fd = new FormData();
55         fd.append('requestFile', file);
56         $http.post(uploadUrl, fd, {
57             transformRequest: angular.identity,            
58             headers: {'Content-Type': undefined}
59         })
60         .success(function(data){
61         console.log("success");         
62                 pars = data;
63                 def.resolve(data);              
64         })
65         .error(function(data){
66         console.log("error");                         
67                 def.reject("Upload file not successful");
68         });
69         
70         return def.promise;
71     };
72     
73     this.uploadFile = function(path,inputFile,uploadURL){
74         console.log("uploadFile");
75         var def = $q.defer();
76         var pars = [];
77         
78         var fd = new FormData();
79         fd.append('requestFile', inputFile);
80         fd.append('path',path)
81         $http.post(uploadURL, fd, {
82             transformRequest: angular.identity,            
83             headers: {'Content-Type': undefined}
84         })
85         .success(function(data){ 
86         console.log("success");         
87                 pars = data;
88                 def.resolve(data);              
89         })
90         .error(function(data){
91         console.log("error");                         
92                 def.reject("Upload file not successful");
93         });
94         
95         return def.promise;
96         
97     };
98 }]);