diff --git a/backend/api/views.py b/backend/api/views.py index 94d9f9d..f6be02d 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -298,10 +298,11 @@ class FileFilterView(APIView): if peer_id: queryset = queryset.filter(peer_users__in=peer_id) else: - active_peer_ids = PeerUser.objects.filter( - last_poll__gte=timezone.now() - timedelta(hours=1) - ).values_list("id", flat=True) - queryset = queryset.filter(peer_users__in=active_peer_ids).distinct() + # active_peer_ids = PeerUser.objects.filter( + # last_poll__gte=timezone.now() - timedelta(hours=1) + # ).values_list("id", flat=True) + # queryset = queryset.filter(peer_users__in=active_peer_ids).distinct() + pass serializer = FileSerializer(queryset, many=True) # Loop through each serialized file to sort its peer_users diff --git a/file_peer/peer_service.py b/file_peer/peer_service.py index ef66322..ff7e469 100644 --- a/file_peer/peer_service.py +++ b/file_peer/peer_service.py @@ -15,14 +15,24 @@ CORS(app) @app.route("/copy-file", methods=["POST"]) def copy_file(): try: - data = request.json - source_file = data["file_path"] - id = data["file_id"] - if not os.path.exists(source_file): - return f"Source file does not exist at {source_file}", 400 - filename = os.path.basename(source_file) + files = request.files + id = request.form["file_id"] + print(id) + if "file" not in files: + return "File must be sent in request", 400 + file = request.files['file'] + filename = file.filename.replace(" ", "_") + # id = data["file_id"] + # if not os.path.exists(source_file): + # return f"Source file does not exist at {source_file}", 400 + # filename = os.path.basename(source_file) destination_path = os.path.join("./uploads/", str(id) + "_" + filename) - shutil.copyfile(source_file, destination_path) + # shutil.copyfile(source_file, destination_path) + if os.path.exists(destination_path): + return "File with name already exists", 200 + + with open(destination_path, "wb") as f: + f.write(file.read()) return "File Moved successfully", 200 except Exception as e: print(f"An error occured: {e}") @@ -88,7 +98,7 @@ def list_files(): files = os.listdir("./uploads") newFiles = [] for file in files: - newFiles.append(file.split("_")[1]) + newFiles.append("_".join(file.split("_", 1)[1:])) return {"files": newFiles}, 200 @app.route("/request-tests", methods=["GET"]) diff --git a/frontend/src/screens/RegisterFile.tsx b/frontend/src/screens/RegisterFile.tsx index f2f763d..f3b95b1 100644 --- a/frontend/src/screens/RegisterFile.tsx +++ b/frontend/src/screens/RegisterFile.tsx @@ -35,7 +35,7 @@ export default function RegisterFile() { fetch(`http://localhost:8000/api/files/filter?peer_id=1`, { method: 'GET', headers: getAuthHeaders() - }) // replace with local api endpoint + }) .then(response => response.json()) .then(data => { setServerFiles(data) @@ -46,10 +46,37 @@ export default function RegisterFile() { const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); // handle file upload + const fileInput = document.getElementById('file'); + // @ts-expect-error - fileInput is an HTMLInputElement + const filename: string = fileInput.files[0].name.replace(/\s/g, "_"); const formData = new FormData(event.target as HTMLFormElement); - const file = formData.get('file'); - console.log(file, typeof file) - + // register file with central server + fetch(`http://localhost:8000/api/register/`, { + method: 'POST', + headers: getAuthHeaders(), + body: JSON.stringify({ + filename: filename, + topic: 1, + semester: 1, + professor: 1, + course: 1, + }) + }) + .then(response => response.json()) + .then(data => { + formData.append("file_id", data.id) + // send file to local + return fetch("http://localhost:8080/copy-file", { + method: 'POST', + body: formData + }) + }) + .then(response => response.text()) + .then(data => { + console.log(data); + window.location.reload(); + }) + .catch(error => console.error(error)); } return (