diff --git a/src/App.vue b/src/App.vue index 53f12166..2f0a83ad 100644 --- a/src/App.vue +++ b/src/App.vue @@ -67,7 +67,9 @@ + SAVE CONFIG + mdi-file-uploadUpload & Start mdi-alert-circle-outlineEmergency Stop @@ -82,6 +84,31 @@ + + Uploading {{ uploadSnackbar.filename }}
+ {{ Math.round(uploadSnackbar.percent) }} % @ {{ formatFilesize(Math.round(uploadSnackbar.speed)) }}/s
+ + +
+ @@ -96,6 +123,7 @@ import ConnectingDialog from "@/components/ConnectingDialog"; import SelectPrinterDialog from "@/components/SelectPrinterDialog"; import PrinterSelecter from "@/components/PrinterSelecter" + import axios from "axios"; export default { props: { @@ -113,6 +141,18 @@ export default { activeClass: 'active', routes: routes.filter((element) => element.title !== "Printers"), boolNaviHeightmap: false, + uploadSnackbar: { + status: false, + filename: "", + percent: 0, + speed: 0, + total: 0, + cancelTokenSource: "", + lastProgress: { + time: 0, + loaded: 0 + } + } }), created () { this.$vuetify.theme.dark = true; @@ -125,6 +165,8 @@ export default { ...mapState({ isConnected: state => state.socket.isConnected, hostname: state => state.printer.hostname, + apiHost: state => state.socket.hostname, + apiPort: state => state.socket.port, klippy_state: state => state.server.klippy_state, printer_state: state => state.printer.print_stats.state, loadings: state => state.socket.loadings, @@ -180,6 +222,86 @@ export default { this.$store.commit('socket/addLoading', { name: 'topbarSaveConfig' }); this.$socket.sendObj('printer.gcode.script', { script: "SAVE_CONFIG" }, 'socket/removeLoading', { name: 'topbarSaveConfig' }); }, + btnUploadAndStart: function() { + this.$refs.fileUploadAndStart.click() + }, + async uploadAndStart() { + if (this.$refs.fileUploadAndStart.files.length) { + this.$store.commit('socket/addLoading', { name: 'btnUploadAndStart' }) + let successFiles = [] + for (const file of this.$refs.fileUploadAndStart.files) { + const result = await this.doUploadAndStart(file) + successFiles.push(result) + } + + this.$store.commit('socket/removeLoading', { name: 'gcodeUpload' }) + for(const file of successFiles) { + this.$toast.success("Upload of "+file+" successful!") + } + + this.$refs.fileUploadAndStart.value = '' + this.$router.push("/"); + } + }, + doUploadAndStart(file) { + let toast = this.$toast + let formData = new FormData() + let filename = file.name.replace(" ", "_") + + this.uploadSnackbar.filename = filename + this.uploadSnackbar.status = true + this.uploadSnackbar.percent = 0 + this.uploadSnackbar.speed = 0 + this.uploadSnackbar.lastProgress.loaded = 0 + this.uploadSnackbar.lastProgress.time = 0 + + formData.append('file', file, filename) + formData.append('print', true) + + return new Promise(resolve => { + this.uploadSnackbar.cancelTokenSource = axios.CancelToken.source(); + axios.post('//' + this.apiHost + ':' + this.apiPort + '/server/files/upload', + formData, { + cancelToken: this.uploadSnackbar.cancelTokenSource.token, + headers: { 'Content-Type': 'multipart/form-data' }, + onUploadProgress: (progressEvent) => { + this.uploadSnackbar.percent = (progressEvent.loaded * 100) / progressEvent.total + if (this.uploadSnackbar.lastProgress.time) { + const time = progressEvent.timeStamp - this.uploadSnackbar.lastProgress.time + const data = progressEvent.loaded - this.uploadSnackbar.lastProgress.loaded + + if (time) this.uploadSnackbar.speed = data / (time / 1000) + } + + this.uploadSnackbar.lastProgress.time = progressEvent.timeStamp + this.uploadSnackbar.lastProgress.loaded = progressEvent.loaded + this.uploadSnackbar.total = progressEvent.total + } + } + ).then((result) => { + this.uploadSnackbar.status = false + resolve(result.data.result) + }).catch(() => { + this.uploadSnackbar.status = false + this.$store.commit('socket/removeLoading', { name: 'btnUploadAndStart' }) + toast.error("Cannot upload the file!") + }) + }) + }, + cancelUpload: function() { + this.uploadSnackbar.cancelTokenSource.cancel() + this.uploadSnackbar.status = false + }, + formatFilesize(fileSizeInBytes) { + let i = -1 + let byteUnits = [' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB'] + do { + fileSizeInBytes = fileSizeInBytes / 1024 + i++ + } while (fileSizeInBytes > 1024) + + return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits[i] + }, drawFavicon(val) { let favicon16 = document.querySelector("link[rel*='icon'][sizes='16x16']") let favicon32 = document.querySelector("link[rel*='icon'][sizes='32x32']")