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