mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
add register file integration
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"])
|
||||
|
||||
@@ -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<HTMLFormElement>) => {
|
||||
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 (
|
||||
<div className="container">
|
||||
|
||||
Reference in New Issue
Block a user