Revert bad merge change
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / CatalogPackageManagerUploadDropZone.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 /**
20 * Created by onvelocity on 10/27/15.
21 */
22
23 import guid from '../libraries/guid'
24 import DropZone from 'dropzone'
25 import Utils from '../libraries/utils'
26 import CatalogPackageManagerActions from '../actions/CatalogPackageManagerActions'
27
28 /**
29 * This class is responsible for wiring the DropZone.js to our React actions.
30 */
31
32 const ACTIONS = {
33 onboard: 'onboard',
34 update: 'update'
35 };
36
37 function getCatalogPackageManagerServerOrigin() {
38 // return Utils.getSearchParams(window.location).upload_server + ':4567';
39 return window.location.origin;
40 }
41
42 function initializeDropZone(element = '#dropzone', button = false, action = ACTIONS.onboard) {
43 let Auth = 'Basic ' + window.sessionStorage.getItem("auth");
44 let dev_download_server = Utils.getSearchParams(window.location).dev_download_server;
45 DropZone.autoDiscover = false;
46 return new DropZone(element, {
47 paramName: 'package',
48 url() {
49 if (action === ACTIONS.update) {
50 return getCatalogPackageManagerServerOrigin() + '/api/update';
51 }
52 return getCatalogPackageManagerServerOrigin() + '/composer/upload?api_server=' + Utils.getSearchParams(window.location).api_server + '&upload_server=' + Utils.getSearchParams(window.location).upload_server + ( dev_download_server ? '&dev_download_server=' + dev_download_server : '');
53 },
54 headers: {
55 'Authorization': Auth
56 },
57 maxFilesize: 10000000000,
58 clickable: button,
59 acceptedFiles: 'application/octet-stream,.gz,.tar.gz,.tar,.qcow,.qcow2,.iso,application/yaml,.yaml,application/json,.json,application/zip,.zip,application/x-rar-compressed,.rar,application/x-7z-compressed,.7z,application/x-bzip,.bz,application/x-bzip2,.bz2,application/x-gtar,.gtar',
60 autoProcessQueue: true,
61 previewTemplate: '',
62 sending(file, xhr, formData) {
63 // NOTE ie11 does not get this form data
64 formData.append('id', file.id);
65 },
66 error(file, errorMessage) {
67 const response = {
68 state: file,
69 data: {
70 status: 'upload-error',
71 message: errorMessage
72 }
73 };
74 CatalogPackageManagerActions.uploadCatalogPackageError(response);
75 },
76 success(file) {
77 const data = JSON.parse(file.xhr.responseText);
78 data.status = 'upload-success';
79 const response = {
80 state: file,
81 data: data
82 };
83 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
84 },
85 addedfile(file) {
86 file.id = file.id || guid();
87 file.riftAction = action;
88 CatalogPackageManagerActions.uploadCatalogPackage(file);
89 },
90 thumbnail(file, dataUrl) {
91 const response = {
92 state: file,
93 data: {
94 status: 'upload-thumbnail',
95 dataUrl: dataUrl
96 }
97 };
98 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
99 },
100 uploadprogress(file, progress, bytesSent) {
101 const response = {
102 state: file,
103 data: {
104 status: 'upload-progress',
105 progress: progress,
106 bytesSent: bytesSent
107 }
108 };
109 CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
110 }
111 });
112 }
113
114 export default class CatalogPackageManagerUploadDropZone {
115
116 constructor(element, button, action) {
117 this.dropZone = initializeDropZone(element, button, action);
118 }
119
120 static get ACTIONS() {
121 return ACTIONS;
122 }
123
124 on(eventName, eventCallback) {
125 this.dropZone.on(eventName, eventCallback);
126 }
127
128 }