file_manager: refactor notify_filelist_changed()

This brings more consistency to the notification.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-08-10 17:47:37 -04:00
parent 6dc43f7e12
commit 7441da4b57
2 changed files with 36 additions and 30 deletions

View File

@ -343,7 +343,7 @@ class FileRequestHandler(AuthorizedFileHandler):
base = self.request.path.lstrip("/").split("/")[2] base = self.request.path.lstrip("/").split("/")[2]
filename = self.path.lstrip("/") filename = self.path.lstrip("/")
file_manager = self.server.lookup_plugin('file_manager') file_manager = self.server.lookup_plugin('file_manager')
file_manager.notify_filelist_changed(filename, 'removed', base) file_manager.notify_filelist_changed('delete_file', filename, base)
self.finish({'result': filename}) self.finish({'result': filename})
class FileUploadHandler(AuthorizedRequestHandler): class FileUploadHandler(AuthorizedRequestHandler):

View File

@ -134,7 +134,7 @@ class FileManager:
os.mkdir(dir_path) os.mkdir(dir_path)
except Exception as e: except Exception as e:
raise self.server.error(str(e)) raise self.server.error(str(e))
self.notify_filelist_changed(url_path, "add_directory", base) self.notify_filelist_changed("create_dir", url_path, base)
elif method == 'DELETE' and base in FULL_ACCESS_ROOTS: elif method == 'DELETE' and base in FULL_ACCESS_ROOTS:
# Remove a directory # Remove a directory
if directory.strip("/") == base: if directory.strip("/") == base:
@ -156,7 +156,7 @@ class FileManager:
os.rmdir(dir_path) os.rmdir(dir_path)
except Exception as e: except Exception as e:
raise self.server.error(str(e)) raise self.server.error(str(e))
self.notify_filelist_changed(url_path, "delete_directory", base) self.notify_filelist_changed("delete_dir", url_path, base)
else: else:
raise self.server.error("Operation Not Supported", 405) raise self.server.error("Operation Not Supported", 405)
return "ok" return "ok"
@ -215,7 +215,7 @@ class FileManager:
# make sure the destination is not in use # make sure the destination is not in use
if os.path.exists(dest_path): if os.path.exists(dest_path):
await self._handle_operation_check(dest_path) await self._handle_operation_check(dest_path)
action = "" action = op_result = ""
if path == "/server/files/move": if path == "/server/files/move":
if source_base not in FULL_ACCESS_ROOTS: if source_base not in FULL_ACCESS_ROOTS:
raise self.server.error( raise self.server.error(
@ -224,22 +224,25 @@ class FileManager:
# if moving the file, make sure the source is not in use # if moving the file, make sure the source is not in use
await self._handle_operation_check(source_path) await self._handle_operation_check(source_path)
try: try:
shutil.move(source_path, dest_path) op_result = shutil.move(source_path, dest_path)
except Exception as e: except Exception as e:
raise self.server.error(str(e)) raise self.server.error(str(e))
action = "file_move" action = "move_item"
elif path == "/server/files/copy": elif path == "/server/files/copy":
try: try:
if os.path.isdir(source_path): if os.path.isdir(source_path):
shutil.copytree(source_path, dest_path) op_result = shutil.copytree(source_path, dest_path)
else: else:
shutil.copy2(source_path, dest_path) op_result = shutil.copy2(source_path, dest_path)
except Exception as e: except Exception as e:
raise self.server.error(str(e)) raise self.server.error(str(e))
action = "file_copy" action = "copy_item"
if op_result != dest_path:
dst_url_path = os.path.join(
dst_url_path, os.path.basename(op_result))
self.notify_filelist_changed( self.notify_filelist_changed(
dst_url_path, action, dest_base, action, dst_url_path, dest_base,
{'prev_file': src_url_path, 'prev_root': source_base}) {'path': src_url_path, 'root': source_base})
return "ok" return "ok"
def _list_directory(self, path): def _list_directory(self, path):
@ -249,20 +252,21 @@ class FileManager:
flist = {'dirs': [], 'files': []} flist = {'dirs': [], 'files': []}
for fname in os.listdir(path): for fname in os.listdir(path):
full_path = os.path.join(path, fname) full_path = os.path.join(path, fname)
modified = time.ctime(os.path.getmtime(full_path)) path_info = self._get_path_info(full_path)
if os.path.isdir(full_path): if os.path.isdir(full_path):
flist['dirs'].append({ path_info['dirname'] = fname
'dirname': fname, flist['dirs'].append(path_info)
'modified': modified
})
elif os.path.isfile(full_path): elif os.path.isfile(full_path):
size = os.path.getsize(full_path) path_info['filename'] = fname
flist['files'].append( flist['files'].append(path_info)
{'filename': fname,
'modified': modified,
'size': size})
return flist return flist
def _get_path_info(self, path):
modified = time.ctime(os.path.getmtime(path))
size = os.path.getsize(path)
path_info = {'modified': modified, 'size': size}
return path_info
def _shell_proc_callback(self, result): def _shell_proc_callback(self, result):
try: try:
proc_resp = json.loads(result.strip()) proc_resp = json.loads(result.strip())
@ -323,9 +327,7 @@ class FileManager:
continue continue
full_path = os.path.join(root, name) full_path = os.path.join(root, name)
r_path = full_path[len(path) + 1:] r_path = full_path[len(path) + 1:]
size = os.path.getsize(full_path) new_list[r_path] = self._get_path_info(full_path)
modified = time.ctime(os.path.getmtime(full_path))
new_list[r_path] = {'size': size, 'modified': modified}
self.file_lists[base] = new_list self.file_lists[base] = new_list
if base == 'gcodes': if base == 'gcodes':
ioloop = IOLoop.current() ioloop = IOLoop.current()
@ -374,7 +376,8 @@ class FileManager:
except self.server.error: except self.server.error:
# Attempt to start print failed # Attempt to start print failed
start_print = False start_print = False
self.notify_filelist_changed(upload['filename'], 'added', "gcodes") self.notify_filelist_changed(
'upload_file', upload['filename'], "gcodes")
return {'result': upload['filename'], 'print_started': start_print} return {'result': upload['filename'], 'print_started': start_print}
def _do_standard_upload(self, request, root): def _do_standard_upload(self, request, root):
@ -383,7 +386,7 @@ class FileManager:
raise self.server.error("Unknown root path: %s" % (root)) raise self.server.error("Unknown root path: %s" % (root))
upload = self._get_upload_info(request, path) upload = self._get_upload_info(request, path)
self._write_file(upload) self._write_file(upload)
self.notify_filelist_changed(upload['filename'], 'added', root) self.notify_filelist_changed('upload_file', upload['filename'], root)
return {'result': upload['filename']} return {'result': upload['filename']}
def _get_argument(self, request, name, default=None): def _get_argument(self, request, name, default=None):
@ -502,11 +505,14 @@ class FileManager:
raise self.server.error("Invalid file path: %s" % (path)) raise self.server.error("Invalid file path: %s" % (path))
os.remove(full_path) os.remove(full_path)
def notify_filelist_changed(self, fname, action, base, params={}): def notify_filelist_changed(self, action, fname, base, source_item={}):
self._update_file_list(base) self._update_file_list(base)
result = {'filename': fname, 'action': action, 'root': base} file_info = dict(self.file_lists[base].get(
if params: fname, {'size': 0, 'modified': ""}))
result.update(params) file_info.update({'path': fname, 'root': base})
result = {'action': action, 'item': file_info}
if source_item:
result.update({'source_item': source_item})
self.server.send_event("file_manager:filelist_changed", result) self.server.send_event("file_manager:filelist_changed", result)
def load_plugin(config): def load_plugin(config):