Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions file-tracker/file_tracker/client_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import os

Expand All @@ -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:
Expand All @@ -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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the operations could be class based (members of a single class) instead of function based to avoid the if-elif block, but that refactor can be done later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

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():
Expand Down
12 changes: 8 additions & 4 deletions file-tracker/file_tracker/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions file-tracker/file_tracker/operation_response.py
Original file line number Diff line number Diff line change
@@ -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())