NOTICKET: Merging OSM/master to OSM/projects
[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 'jquery'
22 import alt from '../../alt'
23 import utils from '../../libraries/utils'
24 import FileManagerActions from './FileManagerActions'
25 let Utils = require('utils/utils.js');
26 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
27 let HOST = API_SERVER;
28 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
29 const FileManagerSource = {
30
31 getFilelist: function() {
32 return {
33 remote: function(state, id, type) {
34 return new Promise(function(resolve, reject) {
35 console.log('Getting File Manager');
36 $.ajax({
37 beforeSend: Utils.addAuthorizationStub,
38 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id,
39 success: function(data) {
40 resolve(JSON.parse(data));
41 },
42 error: function(error) {
43 if (typeof error == 'string') {
44 error = JSON.parse(error);
45 }
46 reject(error);
47 }
48 }).fail(function(xhr){
49 //Authentication and the handling of fail states should be wrapped up into a connection class.
50 Utils.checkAuthentication(xhr.status);
51 });
52 });
53 },
54 success: FileManagerActions.getFilelistSuccess,
55 error: FileManagerActions.getFilelistError
56 }
57 },
58 addFile: function() {
59 return {
60 remote: function(state, id, type, path, url, refresh) {
61 return new Promise(function(resolve, reject) {
62 console.log('Adding file');
63 console.log(id, type, path, url);
64 let splitUrl = url.split('/');
65 let fileName = splitUrl[splitUrl.length -1];
66 let packagePath = refresh ? path + ((path[path.length - 1] == '/') ? '' : '/') : path + '/' + fileName;
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=' + packagePath + '&url=' + url,
70 success: function(data) {
71 resolve({
72 data:data,
73 path: path,
74 fileName: fileName,
75 refresh: refresh
76 });
77 },
78 error: function(error) {
79 if (typeof error == 'string') {
80 error = JSON.parse(error);
81 }
82 reject(error);
83 }
84 }).fail(function(xhr){
85 //Authentication and the handling of fail states should be wrapped up into a connection class.
86 Utils.checkAuthentication(xhr.status);
87 });
88 });
89 },
90 success: FileManagerActions.addFileSuccess,
91 error: FileManagerActions.addFileError
92 }
93 },
94 deleteFile: function() {
95 return {
96 remote: function(state, id, type, path) {
97 return new Promise(function(resolve, reject) {
98 $.ajax({
99 method: 'DELETE',
100 beforeSend: Utils.addAuthorizationStub,
101 url: 'api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id + '&package_path=' + path ,
102 success: function(data) {
103 if (data.output.status == 'True') {
104 resolve({
105 data: data,
106 path: path
107 });
108 } else {
109 reject({
110 data: data,
111 path: path
112 })
113 }
114 },
115 error: function(error) {
116 if (typeof error == 'string') {
117 error = JSON.parse(error);
118 }
119 reject({
120 path: path,
121 data: error
122 });
123 }
124 }).fail(function(xhr){
125 //Authentication and the handling of fail states should be wrapped up into a connection class.
126 Utils.checkAuthentication(xhr.status);
127 });
128 });
129 },
130 success: FileManagerActions.deleteFileSuccess,
131 error: FileManagerActions.deleteFileError
132 }
133 },
134 updateFile: function() {
135 return {
136 remote: function(state, file) {
137 return new Promise(function(resolve, reject) {
138 console.log('Getting File Manager');
139 if(file) {
140 console.log('Updating single file');
141 }
142 if(!file) {
143 console.log('Update all files')
144 }
145 resolve({});
146 });
147 },
148 success: FileManagerActions.getFilelistSuccess,
149 error: FileManagerActions.getFilelistError
150 }
151 },
152 openDownloadMonitoringSocket: function() {
153 return {
154 remote: function(state, packageID) {
155 return new Promise(function(resolve, reject) {
156 //api/operational/download-jobs/job/
157 $.ajax({
158 url: '/socket-polling?api_server=' + API_SERVER,
159 type: 'POST',
160 beforeSend: Utils.addAuthorizationStub,
161 data: {
162 url: 'composer/api/file-manager/jobs/' + packageID + '?api_server=' + API_SERVER,
163 },
164 success: function(data, textStatus, jqXHR) {
165 Utils.checkAndResolveSocketRequest(data, resolve, reject);
166 }
167 }).fail(function(xhr){
168 //Authentication and the handling of fail states should be wrapped up into a connection class.
169 Utils.checkAuthentication(xhr.status);
170 });
171 });
172 },
173 success: FileManagerActions.openDownloadMonitoringSocketSuccess,
174 error: FileManagerActions.openDownloadMonitoringSocketError
175 }
176 },
177 openFileMonitoringSocket: function() {
178 return {
179 remote: function(state, id, type) {
180 return new Promise(function(resolve, reject) {
181 //api/operational/download-jobs/job/
182 $.ajax({
183 url: '/socket-polling?api_server=' + API_SERVER,
184 type: 'POST',
185 beforeSend: Utils.addAuthorizationStub,
186 data: {
187 url: 'composer/api/file-manager?api_server=' + utils.getSearchParams(window.location).api_server +'&package_type=' + type + '&package_id=' + id
188 },
189 success: function(data, textStatus, jqXHR) {
190 Utils.checkAndResolveSocketRequest(data, resolve, reject);
191 }
192 }).fail(function(xhr){
193 //Authentication and the handling of fail states should be wrapped up into a connection class.
194 Utils.checkAuthentication(xhr.status);
195 });
196 });
197 },
198 success: FileManagerActions.getFilelistSocketSuccess,
199 error: FileManagerActions.getFilelistError
200 }
201 }
202 };
203
204 export default FileManagerSource;