From cffc91079179ac59921a16bd066e5f049d24d2bd Mon Sep 17 00:00:00 2001 From: sanjayk03-dev Date: Sun, 2 Jun 2024 04:14:24 +0530 Subject: [PATCH] download macros function --- src/pug/templates/control-view.pug | 4 ++++ src/py/bbctrl/Web.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/pug/templates/control-view.pug b/src/pug/templates/control-view.pug index 512bf5f..5d8cc7a 100644 --- a/src/pug/templates/control-view.pug +++ b/src/pug/templates/control-view.pug @@ -319,6 +319,10 @@ script#control-view-template(type="text/x-template") @click="deleteGCode = true", :disabled="!state.selected || !is_ready",style="height:100px;width:100px;font-weight:normal") img(src="images/delete_gcode.png" style="height: 30px;") + + a(download, title="Download Macros", + :href="'/api/macros/download/' + state.macros_list.map(item => item.file_name).join(',')") + button.pure-button(style="height:100px;width:100px") Download Macros message.error-message(:show.sync="deleteGCode") h3(slot="header") Select files to delete: diff --git a/src/py/bbctrl/Web.py b/src/py/bbctrl/Web.py index f44517e..9c306a0 100644 --- a/src/py/bbctrl/Web.py +++ b/src/py/bbctrl/Web.py @@ -11,6 +11,8 @@ import re import bbctrl from urllib.request import urlopen import iw_parse +import io +import zipfile def call_get_output(cmd): @@ -608,6 +610,28 @@ class StaticFileHandler(tornado.web.StaticFileHandler): self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') +class MacrosDownloadHandler(bbctrl.APIHandler): + def get(self,filename): + if not filename: + raise HTTPError(400, 'Missing filename') + files = filename.split(',') + self.get_log('Macros Download').info('files ' + files.join(' ')) + + buffer = io.BytesIO() + zip_file = zipfile.ZipFile(buffer, mode="w") + + for filename in files: + filepath = self.get_upload(filename).encode('utf8') + filename = os.path.basename(filepath) + zip_file.write(filepath, filename) + zip_file.close() + buffer.seek(0) + # Send the zip file for download + self.set_header('Content-Type', 'application/octet-stream') + self.set_header('Content-Disposition', 'attachment; filename="macros.zip"') + self.write(buffer.getvalue()) + self.finish() + class Web(tornado.web.Application): def __init__(self, args, ioloop): @@ -646,6 +670,7 @@ class Web(tornado.web.Application): (r'/api/firmware/update', FirmwareUpdateHandler), (r'/api/upgrade', UpgradeHandler), (r'/api/file(/[^/]+)?', bbctrl.FileHandler), + (r'/api/macros/download',MacrosDownloadHandler), (r'/api/path/([^/]+)((/positions)|(/speeds))?', PathHandler), (r'/api/home(/[xyzabcXYZABC]((/set)|(/clear))?)?', HomeHandler), (r'/api/start', StartHandler),