Merge "Fix tosca parser"
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / FileUploadService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * 
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                 element.bind('change', function() {
33                         console.log("change");
34                         scope.$apply(function() {
35                                 console.log("apply");
36                                 modelSetter(scope, element[0].files[0]);
37                         });
38                 });
39         }
40         };
41 } ]);
42 app.service('fileUpload', [ '$http', '$q', function($http, $q) {
43         console.log("fileUpload");
44         this.uploadFileToUrl = function(file, uploadUrl) {
45                 console.log("uploadFileToUrl");
46                 var def = $q.defer();
47                 var pars = [];
48                 var fd = new FormData();
49                 fd.append('requestFile', file);
50                 $http.post(uploadUrl, fd, {
51                 transformRequest : angular.identity,
52                 headers : {
53                         'Content-Type' : undefined
54                 }
55                 }).success(function(data) {
56                         console.log("success");
57                         pars = data;
58                         def.resolve(data);
59                 }).error(function(data) {
60                         console.log("error");
61                         def.reject("Upload file not successful");
62                 });
63                 return def.promise;
64         };
65         this.uploadFile = function(path, inputFile, uploadURL) {
66                 console.log("uploadFile");
67                 var def = $q.defer();
68                 var pars = [];
69                 var fd = new FormData();
70                 fd.append('requestFile', inputFile);
71                 fd.append('path', path)
72                 $http.post(uploadURL, fd, {
73                 transformRequest : angular.identity,
74                 headers : {
75                         'Content-Type' : undefined
76                 }
77                 }).success(function(data) {
78                         console.log("success");
79                         pars = data;
80                         def.resolve(data);
81                 }).error(function(data) {
82                         console.log("error");
83                         def.reject("Upload file not successful");
84                 });
85                 return def.promise;
86         };
87 } ]);