RIFT-15487: composer assets download file fix
[osm/UI.git] / skyquake / plugins / composer / src / src / components / filemanager / FileManagerSource.js
1
2 /*
3 *
4 * Copyright 2016 RIFT.IO Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19 'use strict';
20
21 import _ from 'lodash'
22 import $ from 'jquery'
23 import alt from '../../alt'
24 import utils from '../../libraries/utils'
25 import FileManagerActions from './FileManagerActions'
26 let Utils = require('utils/utils.js');
27 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
28 let HOST = API_SERVER;
29 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
30 const FileManagerSource = {
31
32 getFilelist: function() {
33 return {
34 remote: function(state, id, type) {
35 return new Promise(function(resolve, reject) {
36 console.log('Getting File Manager');
37 $.ajax({
38 beforeSend: Utils.addAuthorizationStub,
39 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id,
40 success: function(data) {
41 resolve(JSON.parse(data));
42 },
43 error: function(error) {
44 if (typeof error == 'string') {
45 error = JSON.parse(error);
46 }
47 reject(error);
48 }
49 }).fail(function(xhr){
50 //Authentication and the handling of fail states should be wrapped up into a connection class.
51 Utils.checkAuthentication(xhr.status);
52 });
53 });
54 },
55 success: FileManagerActions.getFilelistSuccess,
56 error: FileManagerActions.getFilelistError
57 }
58 },
59 addFile: function() {
60 return {
61 remote: function(state, id, type, path, url) {
62 return new Promise(function(resolve, reject) {
63 console.log('Adding file');
64 console.log(id, type, path, url);
65 let splitUrl = url.split('/');
66 let fileName = splitUrl[splitUrl.length -1];
67 $.ajax({
68 beforeSend: Utils.addAuthorizationStub,
69 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id + '&package_path=' + path + '/' + fileName + '&url=' + url,
70 success: function(data) {
71 resolve({
72 data:data,
73 path: path,
74 fileName: fileName
75 });
76 },
77 error: function(error) {
78 if (typeof error == 'string') {
79 error = JSON.parse(error);
80 }
81 reject(error);
82 }
83 }).fail(function(xhr){
84 //Authentication and the handling of fail states should be wrapped up into a connection class.
85 Utils.checkAuthentication(xhr.status);
86 });
87 });
88 },
89 success: FileManagerActions.addFileSuccess,
90 error: FileManagerActions.addFileError
91 }
92 },
93 deleteFile: function() {
94 return {
95 remote: function(state, id, type, path) {
96 return new Promise(function(resolve, reject) {
97 $.ajax({
98 method: 'DELETE',
99 beforeSend: Utils.addAuthorizationStub,
100 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id + '&package_path=' + path ,
101 success: function(data) {
102 resolve({
103 data: data,
104 path: path
105 });
106 },
107 error: function(error) {
108 if (typeof error == 'string') {
109 error = JSON.parse(error);
110 }
111 reject(error);
112 }
113 }).fail(function(xhr){
114 //Authentication and the handling of fail states should be wrapped up into a connection class.
115 Utils.checkAuthentication(xhr.status);
116 });
117 });
118 },
119 success: FileManagerActions.deleteFileSuccess,
120 error: FileManagerActions.deleteFileError
121 }
122 },
123 updateFile: function() {
124 return {
125 remote: function(state, file) {
126 return new Promise(function(resolve, reject) {
127 console.log('Getting File Manager');
128 if(file) {
129 console.log('Updating single file');
130 }
131 if(!file) {
132 console.log('Update all files')
133 }
134 resolve({});
135 });
136 },
137 success: FileManagerActions.getFilelistSuccess,
138 error: FileManagerActions.getFilelistError
139 }
140 },
141 openDownloadMonitoringSocket: function() {
142 return {
143 remote: function(state, packageID) {
144 return new Promise(function(resolve, reject) {
145 //api/operational/download-jobs/job/
146 $.ajax({
147 url: '/socket-polling?api_server=' + API_SERVER ,
148 type: 'POST',
149 beforeSend: Utils.addAuthorizationStub,
150 data: {
151 url: 'http://localhost:8000/composer/api/file-manager/jobs/' + packageID + '?api_server=' + API_SERVER,
152 },
153 success: function(data, textStatus, jqXHR) {
154 Utils.checkAndResolveSocketRequest(data, resolve, reject);
155 }
156 }).fail(function(xhr){
157 //Authentication and the handling of fail states should be wrapped up into a connection class.
158 Utils.checkAuthentication(xhr.status);
159 });
160 });
161 },
162 success: FileManagerActions.openDownloadMonitoringSocketSuccess,
163 error: FileManagerActions.openDownloadMonitoringSocketError
164 }
165 },
166 openFileMonitoringSocket: function() {
167 return {
168 remote: function(state, id, type) {
169 return new Promise(function(resolve, reject) {
170 //api/operational/download-jobs/job/
171 $.ajax({
172 url: '/socket-polling?api_server=' + API_SERVER ,
173 type: 'POST',
174 beforeSend: Utils.addAuthorizationStub,
175 data: {
176 url: 'http://localhost:8000/composer/api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id
177 },
178 success: function(data, textStatus, jqXHR) {
179 Utils.checkAndResolveSocketRequest(data, resolve, reject);
180 }
181 }).fail(function(xhr){
182 //Authentication and the handling of fail states should be wrapped up into a connection class.
183 Utils.checkAuthentication(xhr.status);
184 });
185 });
186 },
187 success: FileManagerActions.getFilelistSocketSuccess,
188 error: FileManagerActions.getFilelistError
189 }
190 }
191 };
192
193 export default FileManagerSource;