modified the ConfigDownload class to download the config zip

This commit is contained in:
sanjayk03-dev
2024-06-07 05:41:45 +05:30
parent 98914ffd9e
commit 4a526e0012
2 changed files with 57 additions and 37 deletions

View File

@@ -48,7 +48,7 @@ module.exports = {
methods: { methods: {
backup: function() { backup: function() {
document.getElementById("download-target").src = "/api/config/download"; document.getElementById("download-target").src = "/api/config/download/"+ this.state.macros_list.map(item => item.file_name).join(",");
}, },
restore_config: function() { restore_config: function() {

View File

@@ -255,14 +255,43 @@ class ConfigLoadHandler(bbctrl.APIHandler):
class ConfigDownloadHandler(bbctrl.APIHandler): class ConfigDownloadHandler(bbctrl.APIHandler):
def set_default_headers(self): def set_default_headers(self):
fmt = socket.gethostname() + '-%Y%m%d.json' fmt = socket.gethostname() + '-%Y%m%d.zip'
filename = datetime.date.today().strftime(fmt) filename = datetime.date.today().strftime(fmt)
self.set_header('Content-Type', 'application/octet-stream') self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', self.set_header('Content-Disposition',
'attachment; filename="%s"' % filename) 'attachment; filename="%s"' % filename)
def get(self): def get(self,filename):
self.write_json(self.get_ctrl().config.load(), pretty = True) buffer = io.BytesIO()
zip_file = zipfile.ZipFile(buffer, mode="w")
config_path = self.get_path('config.json')
try:
if os.path.exists(config_path):
zip_file.write(config_path,'config.json')
else:
json_bytes = json.dumps({'version': self.version}).encode("utf-8")
zip_file.writestr("config.json",json_bytes)
except Exception: self.log.exception('Internal error: Failed to download config')
if not filename:
zip_file.close()
buffer.seek(0)
self.write(buffer.getvalue())
self.finish()
filename = filename[1:]
files = filename.split(',')
for filename in files:
filename = os.path.basename(url_unescape(filename))
filepath = self.get_upload(filename)
zip_file.write(filepath, filename)
zip_file.close()
buffer.seek(0)
self.write(buffer.getvalue())
self.finish()
class ConfigSaveHandler(bbctrl.APIHandler): class ConfigSaveHandler(bbctrl.APIHandler):
@@ -611,38 +640,29 @@ class StaticFileHandler(tornado.web.StaticFileHandler):
self.set_header('Cache-Control', self.set_header('Cache-Control',
'no-store, no-cache, must-revalidate, max-age=0') 'no-store, no-cache, must-revalidate, max-age=0')
class MacrosDownloadHandler(bbctrl.APIHandler): class MacrosUploadHandler(bbctrl.APIHandler):
def get(self,filename): def post(self):
if not filename: zip_file = self.request.files['file'][0]
raise HTTPError(400, 'Missing filename')
filename = filename[1:]
files = filename.split(',')
buffer = io.BytesIO() if not os.path.exists('./config-temp'):
zip_file = zipfile.ZipFile(buffer, mode="w") os.mkdir('./config-temp')
for filename in files: if not os.path.exists(self.get_upload()):
filename = os.path.basename(url_unescape(filename)) os.mkdir(self.get_upload())
filepath = self.get_upload(filename)
zip_file.write(filepath, filename) # zip_path = os.path.join("./temp", zip_file['filename'])
# with open(zip_path, 'wb') as f:
config_path = self.get_path('config.json') # f.write(zip_file['body'])
try:
if os.path.exists(config_path): # with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_file.write(config_path,'config.json') # zip_ref.extractall("./temp")
else:
json_bytes = json.dumps({'version': self.version}).encode("utf-8") # for root, dirs, files in os.walk("./temp"):
zip_file.writestr("config.json",json_bytes) # for files in files:
except Exception: self.log.exception('Internal error: Failed to upgrade config')
self.write("File processed successfully.")
zip_file.close() self.finish()
buffer.seek(0)
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): class Web(tornado.web.Application):
@@ -676,13 +696,13 @@ class Web(tornado.web.Application):
(r'/api/remote/username', UsernameHandler), (r'/api/remote/username', UsernameHandler),
(r'/api/remote/password', PasswordHandler), (r'/api/remote/password', PasswordHandler),
(r'/api/config/load', ConfigLoadHandler), (r'/api/config/load', ConfigLoadHandler),
(r'/api/config/download', ConfigDownloadHandler), (r'/api/config/download(/[^/]+)?', ConfigDownloadHandler),
(r'/api/config/save', ConfigSaveHandler), (r'/api/config/save', ConfigSaveHandler),
(r'/api/config/reset', ConfigResetHandler), (r'/api/config/reset', ConfigResetHandler),
(r'/api/firmware/update', FirmwareUpdateHandler), (r'/api/firmware/update', FirmwareUpdateHandler),
(r'/api/upgrade', UpgradeHandler), (r'/api/upgrade', UpgradeHandler),
(r'/api/file(/[^/]+)?', bbctrl.FileHandler), (r'/api/file(/[^/]+)?', bbctrl.FileHandler),
(r'/api/macros/download(/[^/]+)?',MacrosDownloadHandler), (r'/api/macros/upload',MacrosUploadHandler),
(r'/api/path/([^/]+)((/positions)|(/speeds))?', PathHandler), (r'/api/path/([^/]+)((/positions)|(/speeds))?', PathHandler),
(r'/api/home(/[xyzabcXYZABC]((/set)|(/clear))?)?', HomeHandler), (r'/api/home(/[xyzabcXYZABC]((/set)|(/clear))?)?', HomeHandler),
(r'/api/start', StartHandler), (r'/api/start', StartHandler),