Support new package file management scheme
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / FileManagerUploadDropZone.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 import FileManagerActions from '../components/filemanager/FileManagerActions.js';
28
29 /**
30 * This class is responsible for wiring the DropZone.js to our React actions.
31 */
32
33 const ACTIONS = {
34 onboard: 'onboard',
35 update: 'update'
36 };
37
38 function getCatalogPackageManagerServerOrigin() {
39 // return Utils.getSearchParams(window.location).upload_server + ':4567';
40 return window.location.origin;
41 }
42
43 function initializeDropZone(element = '#dropzone', button = false, action = ACTIONS.onboard, getUploadProps) {
44 let Auth = 'Basic ' + window.sessionStorage.getItem("auth");
45 let dev_download_server = Utils.getSearchParams(window.location).dev_download_server;
46 DropZone.autoDiscover = false;
47 return new DropZone(element, {
48 paramName: 'package',
49 url() {
50 let {packageType, packageId, assetGroup, path} = getUploadProps();
51 if (action === ACTIONS.update) {
52 return getCatalogPackageManagerServerOrigin() + '/api/update';
53 }
54 let url = getCatalogPackageManagerServerOrigin()
55 + '/composer/api/file-manager?api_server='
56 + Utils.getSearchParams(window.location).api_server
57 + '&package_type=' + packageType
58 + '&package_id=' + packageId
59 + '&package_path=' + path
60 + '&asset_type=' + assetGroup.id
61 + ( dev_download_server ? '&dev_download_server=' + dev_download_server : '');
62 return url;
63 },
64 headers: {
65 'Authorization': Auth
66 },
67 maxFilesize: 10000000000,
68 clickable: button,
69 autoProcessQueue: true,
70 previewTemplate: '',
71 sending(file, xhr, formData) {
72 // NOTE ie11 does not get this form data
73 let {packageType, packageId, assetGroup, path} = getUploadProps();
74 formData.append('id', file.id);
75 FileManagerActions.addFileSuccess({
76 path: assetGroup.folder + (path ? '/' + path: ''),
77 fileName: file.name
78 });
79 },
80 error(file, errorMessage) {
81 const response = {
82 state: file,
83 data: {
84 status: 'upload-error',
85 message: errorMessage
86 }
87 };
88 CatalogPackageManagerActions.uploadCatalogPackageError(response);
89 },
90 success(file) {
91 const data = JSON.parse(file.xhr.responseText);
92 data.status = 'upload-success';
93 const response = {
94 state: file,
95 data: data
96 };
97 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
98 },
99 addedfile(file) {
100 file.id = file.id || guid();
101 file.riftAction = action;
102 //CatalogPackageManagerActions.uploadCatalogPackage(file);
103 },
104 thumbnail(file, dataUrl) {
105 const response = {
106 state: file,
107 data: {
108 status: 'upload-thumbnail',
109 dataUrl: dataUrl
110 }
111 };
112 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
113 },
114 uploadprogress(file, progress, bytesSent) {
115 // FileManagerActions.addFileSuccess({
116 // path: path,
117 // fileName: file.name
118 // });
119 const response = {
120 state: file,
121 data: {
122 status: 'upload-progress',
123 progress: progress,
124 bytesSent: bytesSent
125 }
126 };
127 //CatalogPackageManagerActions.uploadCatalogPackageStatusUpdated(response);
128 }
129 });
130 }
131
132 export default class FileManagerUploadDropZone {
133
134 constructor(element, button, action, getUploadProps) {
135 this.dropZone = initializeDropZone(element, button, action, getUploadProps);
136 }
137
138 static get ACTIONS() {
139 return ACTIONS;
140 }
141
142 on(eventName, eventCallback) {
143 this.dropZone.on(eventName, eventCallback);
144 }
145
146 }