diff --git a/file-tracker/file_tracker/client_connection.py b/file-tracker/file_tracker/client_connection.py index 2609dc5c..6203b263 100644 --- a/file-tracker/file_tracker/client_connection.py +++ b/file-tracker/file_tracker/client_connection.py @@ -1,4 +1,3 @@ -import json import logging import os @@ -8,7 +7,8 @@ RTCPeerConnection, RTCSessionDescription, ) -from file_operations import ls, tail +from file_operations import OperationError, ls, tail +from operation_response import OperationResponse, OperationStatus class ClientConnection: @@ -34,14 +34,24 @@ def on_datachannel(channel): @channel.on("message") async def on_message(message): - if message == "ls": - contents = await ls(self.path) - channel.send(json.dumps(contents)) - - elif message.startswith("tail:"): - filename = message.split(":")[1] - content = await tail(os.path.join(self.path, filename)) - channel.send(json.dumps(content)) + response = OperationResponse() + try: + if message == "ls": + response.message = ls(self.path) + + elif message.startswith("tail:"): + filename = message.split(":")[1] + response.message = tail( + os.path.join(self.path, filename)) + else: + response = OperationResponse( + status=OperationStatus.INVALID, + message="Unknown command") + except OperationError as e: + response = OperationResponse(status=OperationStatus.ERROR, + message=str(e)) + finally: + channel.send(response.to_json_string()) @channel.on("close") async def on_close(): diff --git a/file-tracker/file_tracker/file_operations.py b/file-tracker/file_tracker/file_operations.py index 7f0e5a57..e97e3d2a 100644 --- a/file-tracker/file_tracker/file_operations.py +++ b/file-tracker/file_tracker/file_operations.py @@ -2,21 +2,25 @@ from collections import deque -async def ls(path): +class OperationError(Exception): + pass + + +def ls(path): contents = [] dir = os.listdir(path) for file in dir: file_path = os.path.join(path, file) if os.path.isdir(file_path): - contents.append({file: await ls(file_path)}) + contents.append({file: ls(file_path)}) else: contents.append(file) return contents -async def tail(filename, lines=10): +def tail(filename, lines=10): if not os.path.exists(filename): - return ["Error: File does not exist."] + raise OperationError(f"File not found: {filename}") with open(filename, 'rb') as f: f.seek(0, 2) # Seek to the end of the file block_size = 1024 diff --git a/file-tracker/file_tracker/operation_response.py b/file-tracker/file_tracker/operation_response.py new file mode 100644 index 00000000..d86eaae6 --- /dev/null +++ b/file-tracker/file_tracker/operation_response.py @@ -0,0 +1,21 @@ +import enum +import json + + +class OperationStatus(enum.Enum): + SUCCESS = "success" + INVALID = "invalid" + ERROR = "error" + + +class OperationResponse: + + def __init__(self, status=OperationStatus.SUCCESS, message=None): + self.status = status + self.message = message + + def to_dict(self): + return {"status": self.status.value, "message": self.message} + + def to_json_string(self): + return json.dumps(self.to_dict())