[CLAMP-1] Initial ONAP CLAMP seed code commit
[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
24 app.directive('fileModel', ['$parse', function ($parse) {
25     console.log("////////fileModel");
26     return {
27         restrict: 'A',
28         link: function(scope, element, attrs) {   
29         console.log("link");            
30             var model = $parse(attrs.fileModel);
31                 //alert("uploadFileToUrl directive model :: " + model);
32             var modelSetter = model.assign;
33             
34             element.bind('change', function(){
35                 console.log("change");
36                 scope.$apply(function(){
37                 console.log("apply");                   
38                     modelSetter(scope, element[0].files[0]);
39                 });
40             });
41         }
42     };
43 }]);
44
45
46 app.service('fileUpload', ['$http', '$q', function ($http, $q) {
47     console.log("fileUpload");
48     this.uploadFileToUrl = function(file, uploadUrl){
49         console.log("uploadFileToUrl");
50         //alert("uploadFileToUrl file :: " + file + " :: url::" + uploadUrl);
51         
52         var def = $q.defer();
53         var pars = [];
54         
55         var fd = new FormData();
56         fd.append('requestFile', file);
57         $http.post(uploadUrl, fd, {
58             transformRequest: angular.identity,            
59             headers: {'Content-Type': undefined}
60         })
61         .success(function(data){
62         console.log("success");         
63                 pars = data;
64                 def.resolve(data);              
65         })
66         .error(function(data){
67         console.log("error");                         
68                 def.reject("Upload file not successful");
69         });
70         
71         return def.promise;
72     };
73     
74     this.uploadFile = function(path,inputFile,uploadURL){
75         console.log("uploadFile");
76         var def = $q.defer();
77         var pars = [];
78         
79         var fd = new FormData();
80         fd.append('requestFile', inputFile);
81         fd.append('path',path)
82         $http.post(uploadURL, fd, {
83             transformRequest: angular.identity,            
84             headers: {'Content-Type': undefined}
85         })
86         .success(function(data){ 
87         console.log("success");         
88                 pars = data;
89                 def.resolve(data);              
90         })
91         .error(function(data){
92         console.log("error");                         
93                 def.reject("Upload file not successful");
94         });
95         
96         return def.promise;
97         
98     };
99 }]);