zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import PropTypes from 'prop-types';
import React, { Component, Fragment } from "react";
import { Modal, Button, FormGroup, ControlLabel, FormControl, HelpBlock } from "react-bootstrap";
import Select from 'react-select';
import "./SelectUrlDialog.scss";
 
export default class SelectUrlDialog extends Component {
    static defaultProps = {
        platform: null,
        show: false,
        ddbUrl: null
    };
    static propTypes = {
        onHide: PropTypes.func.isRequired,
        onSubmit: PropTypes.func.isRequired,
        ddbUrl: PropTypes.string,
        show: PropTypes.bool.isRequired,
        apiURL: PropTypes.string.isRequired,
      }
 
    constructor(props){
        super(props);
 
        this.resetState();
    }
 
    resetState() {
        
        this.state = {
            error: "",
            organizations: [],
            datasets: [],
            folders: [],
            loadingOrganizations: true,
            loadingDatasets: false,
            loadingFolders: false,
            hasLogin: false,
            selectedOrganization: null,
            selectedDataset: null,
            selectedFolder: null,
            info: null,
        
            verDs: null,
            verCount: 0,
            verSize: 0,
            verFolder: null,
 
            // verifyStatus: null (not started), 'loading', 'success', 'error'
            verifyStatus: null
        };
    }
 
    // Format bytes to readable string
    formatBytes(bytes, decimals=2) {
        if(bytes == 0) return '0 bytes';
        var k = 1024,
            dm = decimals || 2,
            sizes = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
    }
    
    handleOnShow = () => {
 
        this.resetState();
        
        $.get(`${this.props.apiURL}/organizations`)
            .done(result => {
                
                var orgs = result.map(org => {
                    return { label: org.name !== org.slug ? `${org.name} (${org.slug})` : org.slug, value: org.slug };
                });
 
                if (orgs.length > 0) {
                    this.setState({organizations: orgs, loadingOrganizations: false});
 
                    // Search for user organization
                    var userOrg = orgs.find(org => org.value === this.state.info.username);
 
                    this.handleSelectOrganization(userOrg != null ? userOrg : orgs[0]);
                } else 
                    this.setState({organizations: orgs, loadingOrganizations: false});
            })
            .fail((error) => {
                this.setState({error: "Cannot load organizations. Check your internet connection.", organizations: []});
            })
            .always(() => {
                this.setState({loadingOrganizations: false});
            });
 
        $.get(`${this.props.apiURL}/info`)
            .done(result => {                
                this.setState({info: result});
            })
            .fail((error) => {
                this.setState({info: null});
            });
    }
 
    handleVerify = () => {
 
        this.setState({verifyStatus: 'loading'});
 
        $.post(`${this.props.apiURL}/verifyurl`, { url: this.state.ddbUrl }).done(result => {
            
            if (result != null) {
                this.setState({
                    verifyStatus: result.count > 0 ? 'success' : 'error',
                    verCount: result.count,
                    verDs: result.ds,
                    verSize: result.size,
                    verFolder: result.folder
                });
            } else {
                this.setState({verifyStatus: 'error'});
            }
 
            
        })
        .fail((error) => {
            this.setState({verifyStatus: 'error'});
        });
 
    }
 
    
    handleSelectOrganization = (e) => {
 
        if (this.state.selectedOrganization !== null && e.value === this.state.selectedOrganization.value) return;
 
        this.setState({
            loadingDatasets: true, 
            selectedOrganization: e, 
            selectedDataset: null, 
            selectedFolder: null,
            verifyStatus: null,
            datasets: [],
            folders: []
        });
 
        $.get(`${this.props.apiURL}/organizations/${e.value}/datasets`)
            .done(result => {                
                
                var dss = result.map(ds => {
                    return { label: ds.name !== ds.slug ? 
                        `${ds.name} (${ds.slug}) - ${ds.entries} entries (${this.formatBytes(ds.size)})`: 
                        `${ds.name} - ${ds.entries} entries (${this.formatBytes(ds.size)})`, name: ds.name, value: ds.slug };
                });
 
                if (dss.length > 0) {
                    this.setState({datasets: dss, loadingDatasets: false});
                    this.handleSelectDataset(dss[0]);
                } else 
                    this.setState({datasets: dss, loadingDatasets: false});
 
            })
            .fail((error) => {
                this.setState({error: "Cannot load datasets. Check your internet connection."});
            })
            .always(() => {
                this.setState({loadingDatasets: false});
            });
    };
 
    handleSelectDataset = (e) => {
 
        if (this.state.selectedDataset !== null && e.value === this.state.selectedDataset.value) return;
 
        this.setState({
            selectedDataset: e, 
            selectedFolder: null, 
            loadingFolders: true,
            verifyStatus: null,
            folders: []
        });
 
        $.get(`${this.props.apiURL}/organizations/${this.state.selectedOrganization.value}/datasets/${e.value}/folders`)
            .done(result => {
                
                var folders = result.map(folder => {
                    return { label: folder, value: '/' + folder };
                });
 
                folders.unshift({label: '/', value: '/'});
                folders.sort();
 
                if (folders.length > 0) {
                    this.setState({folders: folders, loadingFolders: false});
                    this.handleSelectFolder(folders[0]);
                } else 
                    this.setState({folders: folders, loadingFolders: false});
 
            })
            .fail((error) => {
                this.setState({error: "Cannot load folders. Check your internet connection."});
            })
            .always(() => {
                this.setState({loadingFolders: false});
            });
    };
 
    handleSelectFolder = e => {
        
        if (this.state.selectedFolder !== null && e.value === this.state.selectedFolder.value) return;
 
        this.setState({selectedFolder: e, verifyStatus: null});
        
        if (this.state.info == null || this.state.info.hubUrl == null) {
            console.warn("Cannot generate ddb url, no hub url");
            return;
        }
 
        var url = `${this.state.info.hubUrl}/${this.state.selectedOrganization.value}/${this.state.selectedDataset.value}${e.value}`
            .replace('http://', 'ddb+unsafe://')
            .replace('https://', 'ddb://');
 
        this.setState({ddbUrl: url});
 
    }
 
    handleChange = (e) => {
        this.setState({ddbUrl: e.target.value, verifyStatus: null});
    };
 
    handleSubmit = e => {
    
        this.props.onSubmit(
            {
                name: this.state.verDs != null ? this.state.verDs : "DroneDB",
                url: this.state.ddbUrl,
                images_count: this.state.verCount
            });
    };
 
    render() {
        const {
            onHide,
            ddbUrl,
            show
        } = this.props;
 
        return (
            <Modal className={"folder-select"} onHide={onHide} show={show} onShow={this.handleOnShow}>
                <Modal.Header closeButton>
                    <Modal.Title>
                        Import from DroneDB
                    </Modal.Title>
                </Modal.Header>
                <Modal.Body bsClass="my-modal">        
                    {this.state.organizations!= null && this.state.organizations.length > 0 ?         
                    <div style={{'marginBottom': '20px'}}>
                        <p>Import images from your DroneDB account</p>
                        <div className={"select-row"}>
                            <div className={"icon-cell"}>
                                <i className={"fas fa-sitemap"}></i>
                            </div>
                            <div className={"select-cell"}>
                                <Select
                                    className="basic-single"
                                    classNamePrefix="select"
                                    isLoading={this.state.loadingOrganizations}
                                    isClearable={false}
                                    isSearchable={true}
                                    value={this.state.selectedOrganization}
                                    onChange={this.handleSelectOrganization}
                                    options={this.state.organizations}
                                    placeholder={this.state.loadingOrganizations ? "Fetching organizations..." : "Please select an organization"}
                                    name="organizations"
                                />
                            </div>
                        </div>
                        <div className={"select-row"}>
                            <div className={"icon-cell"}>
                                <i className={"fas fa-database"}></i>
                            </div>
                            <div className={"select-cell"}>                
                                <Select
                                    className="basic-single"
                                    classNamePrefix="select"
                                    isLoading={this.state.loadingDatasets}
                                    isClearable={false}
                                    isSearchable={true}
                                    value={this.state.selectedDataset}
                                    isDisabled={this.state.selectedOrganization === null}
                                    onChange={this.handleSelectDataset}
                                    options={this.state.datasets}
                                    placeholder={this.state.loadingDatasets ? "Fetching datasets..." : (this.state.datasets.length > 0 ? "Please select a dataset" : "No datasets found")}
                                    name="datasets"
                                />
                            </div>
                        </div>
                        <div className={"select-row"}>
                            <div className={"icon-cell"}>
                                <i className={"fas fa-folder"}></i>
                            </div>
                            <div className={"select-cell"}>
                                <Select
                                    className="basic-single"
                                    classNamePrefix="select"
                                    isLoading={this.state.loadingFolders}
                                    isClearable={false}
                                    isSearchable={true}
                                    value={this.state.selectedFolder}
                                    isDisabled={this.state.selectedDataset === null || this.state.selectedOrganization === null}
                                    onChange={this.handleSelectFolder}
                                    options={this.state.folders}
                                    placeholder={this.state.loadingFolders ? "Fetching folders..." : "Please select a folder"}
                                    name="folders"
                                />    
                            </div>
                        </div>
                    </div> : <div className="text-center">
                        {this.state.loadingOrganizations ? <i className="fa fa-circle-notch fa-spin fa-fw"></i> : 
                            <div className={"alert alert-info"}>                                    
                                <span><a href="/plugins/dronedb"><strong>Setup your DroneDB credentials</strong></a> to browse your organizations, datasets and folders!</span>
                            </div>}
                        </div>
                    }
 
                    <p>DroneDB URL</p>
                    <div className={"select-row"}>
                        <div className={"icon-cell"}>
                            <i className={"fas fa-globe"}></i>
                        </div>
                        <div className={"select-cell"}>
                            <FormControl
                                type="url"
                                placeholder={"https://hub.dronedb.app/r/username/dataset"}
                                value={this.state.ddbUrl || ''}
                                onChange={this.handleChange} />
                        </div>
                        <div className={"icon-cell"}>
                            { this.state.verifyStatus==='loading' && <i className={"fas fa-spinner fa-spin"}></i> }
                            { this.state.verifyStatus==='success' && <i className={"fas fa-check"}></i> }
                            { this.state.verifyStatus==='error' && <i className={"fas fa-times"}></i> }
                        </div>
                    </div>
                    
                    {this.state.verifyStatus != null && this.state.verifyStatus == "success" ?
                                <div className={"alert alert-success"}>                                    
                                    <span>Found <strong>{this.state.verCount}</strong> files ({this.formatBytes(this.state.verSize)})</span>
                                </div>                                    
                        : ""}    
                        
                </Modal.Body>
                <Modal.Footer>
                    <Button onClick={onHide}>Close</Button>
                    <Button bsStyle="success" disabled={this.state.ddbUrl == null || this.state.ddbUrl.length == 0} onClick={this.handleVerify}>
                        <i className={"fas fa-check"} />Verify</Button>
                    <Button bsStyle="primary" disabled={this.state.verifyStatus !== 'success'} onClick={this.handleSubmit}>
                        <i className={"fa fa-upload"} />Import</Button>
                </Modal.Footer>
            </Modal>
        );
    }
}