Skip to content

Commit

Permalink
i really don't like hardcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFJcurve committed Nov 13, 2024
1 parent 706f268 commit 4668107
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
6 changes: 3 additions & 3 deletions backend/code/file_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
TODO
This file helps in creating and interpreting a .hackthehill file
"""

import hashlib
Expand All @@ -8,7 +8,7 @@

from pathlib import Path

from config import SOURCES_FOLDER
from config import SOURCES_FOLDER, HASH_EXTENSION


def hash_file_blocks(file_path: str, block_size: int = 512):
Expand All @@ -26,7 +26,7 @@ def hash_file_blocks(file_path: str, block_size: int = 512):
be used to hide the nature of the file in communication
"""

hackthehill_file = Path(file_path).stem + ".hackthehill"
hackthehill_file = Path(file_path).stem + HASH_EXTENSION
file_size: int = os.path.getsize(file_path)
num_blocks = (file_size + block_size -
1) // block_size # Round up division
Expand Down
12 changes: 6 additions & 6 deletions backend/code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from flask_cors import CORS
from flask import Flask, request, jsonify, send_file

from config import UPLOADS_FOLDER
from config import UPLOADS_FOLDER, HASH_EXTENSION, SOURCES_FOLDER, WEBSITE_DATA

app = Flask(__name__)
CORS(app)
Expand Down Expand Up @@ -46,14 +46,14 @@ def receive_file():

hash_file_blocks(file_path)

with open("./sources/" + Path(file_path).stem + ".hackthehill", 'r', encoding="utf-8") as f:
hackthehill_file = os.path.join(SOURCES_FOLDER, Path(file_path).stem + HASH_EXTENSION)

with open(hackthehill_file, 'r', encoding="utf-8") as f:
file_hash = custom_hash(f.read())

fileData[file_hash] = {'path': file_path, 'hackthehill': "./sources/" +
Path(file_path).stem + ".hackthehill"}
# print(fileData)
fileData[file_hash] = {'path': file_path, 'hackthehill': hackthehill_file}

with open("website_data.json", "w", encoding="utf-8") as f:
with open(WEBSITE_DATA, "w", encoding="utf-8") as f:
f.write(json.dumps(fileData, indent=2))

return jsonify({"status": "File uploaded", "file_path": file_path, "data": fileData}), 200
Expand Down
25 changes: 13 additions & 12 deletions backend/code/p2p_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from code.utils import get_filename_by_file_id
from code.file_tokenizer import get_block_content
from config import DISCOVERY_PORT, CHAT_PORT, MAX_UDP_PACKET, DISCOVERY_ADDRESS, UPLOADS_FOLDER
from config import DISCOVERY_PORT, CHAT_PORT, MAX_UDP_PACKET, DISCOVERY_ADDRESS, UPLOADS_FOLDER, \
HASH_EXTENSION, SOURCES_FOLDER


class P2PClient:
Expand Down Expand Up @@ -114,8 +115,8 @@ def response_file_fingerprint(self, message):
return

file_name = files[0]
with open(os.path.join('sources', Path(file_name).stem + ".hackthehill"),
"r", encoding='utf-8') as f:
hackthehill_file = os.path.join(SOURCES_FOLDER, Path(file_name).stem + HASH_EXTENSION)
with open(hackthehill_file, "r", encoding='utf-8') as f:
response = json.dumps({
'file_name': file_name,
'user_id': self.user_id,
Expand Down Expand Up @@ -163,17 +164,17 @@ def save_fingerprint_file(self, message):
TODO
"""

with open(os.path.join('sources', Path(message['file_name']).stem + '.hackthehill'),
'w', encoding="utf-8") as f:
hackthehill_file = os.path.join(SOURCES_FOLDER,
Path(message['file_name']).stem + HASH_EXTENSION)
with open(hackthehill_file, 'w', encoding="utf-8") as f:
f.write(message['content'])

def save_block(self, message):
"""
TODO
"""

tmp_file_path = os.path.join('uploads', Path(
message['file_name']).stem + '.tmp')
tmp_file_path = os.path.join(UPLOADS_FOLDER, Path(message['file_name']).stem + '.tmp')
with open(tmp_file_path, 'w+', encoding="utf-8") as f:
file_content = f.read()
if len(file_content) > 0:
Expand All @@ -193,8 +194,9 @@ def get_all_blocks(self, message):
"""

file_id = message['file_id']
with open(os.path.join('sources', Path(message['file_name']).stem + '.hackthehill'),
'r', encoding="utf-8") as f:
hackthehill_file = os.path.join(SOURCES_FOLDER,
Path(message['file_name']).stem + HASH_EXTENSION)
with open(hackthehill_file, 'r', encoding="utf-8") as f:
d = json.loads(f.read())
for block_index in range(int(d['header']['number_of_blocks'])):
self.request_block(file_id, block_index)
Expand All @@ -221,7 +223,7 @@ def listen_for_messages(self):
elif message["type"] == "response_block":
self.save_block(message)
self.tmp_to_file(os.path.join(
'uploads', Path(message['file_name']).stem+'.tmp'))
'uploads', Path(message['file_name']).stem + '.tmp'))
else:
print("Invalid message type: " + message["type"])
else:
Expand All @@ -234,8 +236,7 @@ def tmp_to_file(self, tmp_file_path):
with open(tmp_file_path, 'r', encoding="utf-8") as f:
content = json.loads(f.read())

file_path = os.path.join('sources', Path(
tmp_file_path).stem + '.hackthehill')
file_path = os.path.join(SOURCES_FOLDER, Path(tmp_file_path).stem + HASH_EXTENSION)

with open(file_path, 'r', encoding="utf-8") as f:
file_with_extension = json.loads(f.read())['header']['file_name']
Expand Down
3 changes: 3 additions & 0 deletions backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
CHAT_PORT = 5001
MAX_UDP_PACKET = 65507
DISCOVERY_ADDRESS = '192.168.181.255'
HASH_EXTENSION = '.hackthehill'

BACKEND_FOLDER = os.path.dirname(os.path.abspath(__file__))

UPLOADS_FOLDER = os.path.join(BACKEND_FOLDER, "uploads")
SOURCES_FOLDER = os.path.join(BACKEND_FOLDER, "sources")
CODE_FOLDER = os.path.join(BACKEND_FOLDER, "code")
TEST_FOLDER = os.path.join(BACKEND_FOLDER, "test")

WEBSITE_DATA = os.path.join(CODE_FOLDER, "website_data.json")
4 changes: 2 additions & 2 deletions backend/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from code.file_tokenizer import hash_file_blocks
from code.utils import find_file, custom_hash, get_filename_by_file_id

from config import SOURCES_FOLDER, CODE_FOLDER, UPLOADS_FOLDER
from config import SOURCES_FOLDER, CODE_FOLDER, UPLOADS_FOLDER, HASH_EXTENSION


class TestUtils(unittest.TestCase):
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_get_filename_by_file_id_with_matching_id_returns_tuple(self):
"test_get_filename_by_file_id_with_matching_id_returns_tuple.txt")
hackthehill_file = os.path.join(
SOURCES_FOLDER,
"test_get_filename_by_file_id_with_matching_id_returns_tuple.hackthehill")
"test_get_filename_by_file_id_with_matching_id_returns_tuple" + HASH_EXTENSION)

message = "test_get_filename_by_file_id_with_matching_id_returns_tuple"

Expand Down

0 comments on commit 4668107

Please sign in to comment.