-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDz.js
More file actions
151 lines (128 loc) · 5.37 KB
/
Dz.js
File metadata and controls
151 lines (128 loc) · 5.37 KB
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
import React, { useEffect, useContext } from "react";
import './Dz.css'
import { FETCH_PATH } from './environment'
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
import Alert from '@mui/material/Alert';
import { PageContext } from './Page';
import { getCookieValue } from './Cookies';
import { AlertTitle } from "@mui/material";
import WarningAmberIcon from "@mui/icons-material/WarningAmber";
function FileSizeWarning() {
return (
<Alert
severity="warning"
icon={<WarningAmberIcon fontSize="inherit" />}
sx={{ borderRadius: 2, mb: 2 }}
>
<AlertTitle>Notice</AlertTitle>
Files of over <strong>128 MB</strong> trigger an experimental processing pipeline.
Please report any issues to <a href="mailto:validate@buildingsmart.org">validate@buildingsmart.org</a>.
</Alert>
);
}
function Dz() {
const MAX_FILE_SIZE_IN_MB = 512;
const EXPERIMENTAL_FILE_SIZE_IN_MB = 256;
const TOAST_DURATION = 5000; // ms
const context = useContext(PageContext);
const [showExperimentalWarning, setShowExperimentalWarning] = React.useState(false);
const [showErrorToast, setShowErrorToast] = React.useState({
open: false,
fileName: '',
fileSize: 0
});
const { open, fileName, fileSize } = showErrorToast;
const handleSnackbarClose = (event, reason) => {
if (reason === 'clickaway') {
return;
}
setShowErrorToast({ ...showErrorToast, open: false });
};
useEffect(() => {
window.Dropzone.autoDiscover = false;
var dz = new window.Dropzone("#ifc_dropzone", {
uploadMultiple: true,
acceptedFiles: ".ifc",
parallelUploads: 100,
maxFiles: 100,
maxFileSize: MAX_FILE_SIZE_IN_MB,
autoProcessQueue: false,
addRemoveLinks: true,
withCredentials: true,
headers: { 'x-csrf-token': getCookieValue('csrftoken') }
});
function showWarningWhenFileExceedsExperimentalTreshold() {
setShowExperimentalWarning(
dz.files.some(f => f.size > EXPERIMENTAL_FILE_SIZE_IN_MB * 1024 * 1024)
);
}
dz.on("success", function (file, response) {
if (window.location.href.split("/").at(-1) !== "dashboard"){
window.location = response.url;
}
else{
window.location.reload();
dz.removeAllFiles();
}
});
dz.on("error", function (file, message) {
//console.log(file.name, file.size, message);
// block files that are too big
if (message.indexOf('too big') !== -1) {
dz.removeFile(file);
setShowErrorToast({
...showErrorToast,
open: true,
fileName: file.name,
fileSize: Math.ceil(file.size / (1024 * 1024))
});
}
});
dz.on("totaluploadprogress", function (progress) {
const pb = document.querySelector(".dropzone .progress-bar");
const w = (pb.parentNode.offsetWidth - 2) / 100. * progress;
pb.style.width = `${w}px`;
});
dz.on("addedfiles", showWarningWhenFileExceedsExperimentalTreshold);
dz.on("removedfile", showWarningWhenFileExceedsExperimentalTreshold);
var submitButton = document.querySelector("#submit");
submitButton.addEventListener("click", function () {
const pb = document.querySelector(".dropzone .progress-bar");
pb.style.display = 'block';
pb.style.width = `0px`;
dz.processQueue();
});
}, []);
return (
<div>
<div className="submit-area" id="ifc_tab">
{showExperimentalWarning && <FileSizeWarning />}
<form action={context.sandboxId?`${FETCH_PATH}/api/sandbox/${context.sandboxId}`:`${FETCH_PATH}/api/`} className="dropzone" id="ifc_dropzone">
<div className="progress-bar"></div>
<div className="dz-message" data-dz-message><span><i className="material-icons">file_upload</i> Click or drop files here to upload for validation</span></div>
</form>
<Button className="submit-button" variant="contained" id="submit">Upload & Validate</Button>
<Snackbar
open={open}
autoHideDuration={TOAST_DURATION}
onClose={handleSnackbarClose}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
>
<Alert
onClose={handleSnackbarClose}
severity='error'
variant='filled'
sx={{ width: '100%' }}
>
Oops! File Size Limit Reached <br />
<br />
You've attempted to upload a file larger than {MAX_FILE_SIZE_IN_MB} MB.<br />
Need assistance with larger files? Don't worry, we've got you covered! Contact our support team for tailored solutions.<br />
</Alert>
</Snackbar>
</div>
</div>
);
}
export default Dz;