file_manager: Allow the self.file_paths to contain paths to files

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-07-22 11:48:56 -04:00 committed by Eric Callahan
parent f0fa1295a7
commit bf1a666e20
1 changed files with 19 additions and 7 deletions

View File

@ -250,10 +250,15 @@ class FileManager:
def _update_file_list(self, base='gcodes'): def _update_file_list(self, base='gcodes'):
# Use os.walk find files in sd path and subdirs # Use os.walk find files in sd path and subdirs
path = self.file_paths.get(base, "") path = self.file_paths.get(base, None)
if path is None: if path is None:
logging.info("No sd_path set, cannot update") msg = "No known path for root: %s" % (base)
return logging.info(msg)
raise self.server.error(msg)
elif not os.path.isdir(path):
msg = "Cannot generate file list for root: %s" % (base)
logging.info(msg)
raise self.server.error(msg)
logging.info("Updating File List...") logging.info("Updating File List...")
new_list = {} new_list = {}
for root, dirs, files in os.walk(path, followlinks=True): for root, dirs, files in os.walk(path, followlinks=True):
@ -299,6 +304,13 @@ class FileManager:
raise self.server.error( raise self.server.error(
400, "Bad Request, can only process a single file upload") 400, "Bad Request, can only process a single file upload")
upload = f_list[0] upload = f_list[0]
if os.path.isfile(file_path):
# If the root path points to a file, write directly to it. This
# is the case for printer.cfg
filename = root
full_path = file_path
dir_path = ""
else:
filename = "_".join(upload['filename'].strip().split()).lstrip("/") filename = "_".join(upload['filename'].strip().split()).lstrip("/")
if dir_path: if dir_path:
filename = os.path.join(dir_path, filename) filename = os.path.join(dir_path, filename)