Folder creation for onboarded packages
[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, refresh) {
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 let packagePath = refresh ? path + ((path[path.length - 1] == '/') ? '' : '/') : path + '/' + fileName;
68 $.ajax({
69 beforeSend: Utils.addAuthorizationStub,
70 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id + '&package_path=' + packagePath + '&url=' + url,
71 success: function(data) {
72 resolve({
73 data:data,
74 path: path,
75 fileName: fileName,
76 refresh: refresh
77 });
78 },
79 error: function(error) {
80 if (typeof error == 'string') {
81 error = JSON.parse(error);
82 }
83 reject(error);
84 }
85 }).fail(function(xhr){
86 //Authentication and the handling of fail states should be wrapped up into a connection class.
87 Utils.checkAuthentication(xhr.status);
88 });
89 });
90 },
91 success: FileManagerActions.addFileSuccess,
92 error: FileManagerActions.addFileError
93 }
94 },
95 deleteFile: function() {
96 return {
97 remote: function(state, id, type, path) {
98 return new Promise(function(resolve, reject) {
99 $.ajax({
100 method: 'DELETE',
101 beforeSend: Utils.addAuthorizationStub,
102 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id + '&package_path=' + path ,
103 success: function(data) {
104 resolve({
105 data: data,
106 path: path
107 });
108 },
109 error: function(error) {
110 if (typeof error == 'string') {
111 error = JSON.parse(error);
112 }
113 reject(error);
114 }
115 }).fail(function(xhr){
116 //Authentication and the handling of fail states should be wrapped up into a connection class.
117 Utils.checkAuthentication(xhr.status);
118 });
119 });
120 },
121 success: FileManagerActions.deleteFileSuccess,
122 error: FileManagerActions.deleteFileError
123 }
124 },
125 updateFile: function() {
126 return {
127 remote: function(state, file) {
128 return new Promise(function(resolve, reject) {
129 console.log('Getting File Manager');
130 if(file) {
131 console.log('Updating single file');
132 }
133 if(!file) {
134 console.log('Update all files')
135 }
136 resolve({});
137 });
138 },
139 success: FileManagerActions.getFilelistSuccess,
140 error: FileManagerActions.getFilelistError
141 }
142 },
143 openDownloadMonitoringSocket: function() {
144 return {
145 remote: function(state, packageID) {
146 return new Promise(function(resolve, reject) {
147 //api/operational/download-jobs/job/
148 $.ajax({
149 url: '/socket-polling?api_server=' + API_SERVER ,
150 type: 'POST',
151 beforeSend: Utils.addAuthorizationStub,
152 data: {
153 url: 'http://localhost:8000/composer/api/file-manager/jobs/' + packageID + '?api_server=' + API_SERVER,
154 },
155 success: function(data, textStatus, jqXHR) {
156 Utils.checkAndResolveSocketRequest(data, resolve, reject);
157 }
158 }).fail(function(xhr){
159 //Authentication and the handling of fail states should be wrapped up into a connection class.
160 Utils.checkAuthentication(xhr.status);
161 });
162 });
163 },
164 success: FileManagerActions.openDownloadMonitoringSocketSuccess,
165 error: FileManagerActions.openDownloadMonitoringSocketError
166 }
167 },
168 openFileMonitoringSocket: function() {
169 return {
170 remote: function(state, id, type) {
171 return new Promise(function(resolve, reject) {
172 //api/operational/download-jobs/job/
173 $.ajax({
174 url: '/socket-polling?api_server=' + API_SERVER ,
175 type: 'POST',
176 beforeSend: Utils.addAuthorizationStub,
177 data: {
178 url: 'http://localhost:8000/composer/api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id
179 },
180 success: function(data, textStatus, jqXHR) {
181 Utils.checkAndResolveSocketRequest(data, resolve, reject);
182 }
183 }).fail(function(xhr){
184 //Authentication and the handling of fail states should be wrapped up into a connection class.
185 Utils.checkAuthentication(xhr.status);
186 });
187 });
188 },
189 success: FileManagerActions.getFilelistSocketSuccess,
190 error: FileManagerActions.getFilelistError
191 }
192 }
193 };
194
195 export default FileManagerSource;