mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 19:56:19 -04:00
add register file integration
This commit is contained in:
@@ -298,10 +298,11 @@ class FileFilterView(APIView):
|
|||||||
if peer_id:
|
if peer_id:
|
||||||
queryset = queryset.filter(peer_users__in=peer_id)
|
queryset = queryset.filter(peer_users__in=peer_id)
|
||||||
else:
|
else:
|
||||||
active_peer_ids = PeerUser.objects.filter(
|
# active_peer_ids = PeerUser.objects.filter(
|
||||||
last_poll__gte=timezone.now() - timedelta(hours=1)
|
# last_poll__gte=timezone.now() - timedelta(hours=1)
|
||||||
).values_list("id", flat=True)
|
# ).values_list("id", flat=True)
|
||||||
queryset = queryset.filter(peer_users__in=active_peer_ids).distinct()
|
# queryset = queryset.filter(peer_users__in=active_peer_ids).distinct()
|
||||||
|
pass
|
||||||
|
|
||||||
serializer = FileSerializer(queryset, many=True)
|
serializer = FileSerializer(queryset, many=True)
|
||||||
# Loop through each serialized file to sort its peer_users
|
# Loop through each serialized file to sort its peer_users
|
||||||
|
|||||||
@@ -15,14 +15,24 @@ CORS(app)
|
|||||||
@app.route("/copy-file", methods=["POST"])
|
@app.route("/copy-file", methods=["POST"])
|
||||||
def copy_file():
|
def copy_file():
|
||||||
try:
|
try:
|
||||||
data = request.json
|
files = request.files
|
||||||
source_file = data["file_path"]
|
id = request.form["file_id"]
|
||||||
id = data["file_id"]
|
print(id)
|
||||||
if not os.path.exists(source_file):
|
if "file" not in files:
|
||||||
return f"Source file does not exist at {source_file}", 400
|
return "File must be sent in request", 400
|
||||||
filename = os.path.basename(source_file)
|
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)
|
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
|
return "File Moved successfully", 200
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occured: {e}")
|
print(f"An error occured: {e}")
|
||||||
@@ -88,7 +98,7 @@ def list_files():
|
|||||||
files = os.listdir("./uploads")
|
files = os.listdir("./uploads")
|
||||||
newFiles = []
|
newFiles = []
|
||||||
for file in files:
|
for file in files:
|
||||||
newFiles.append(file.split("_")[1])
|
newFiles.append("_".join(file.split("_", 1)[1:]))
|
||||||
return {"files": newFiles}, 200
|
return {"files": newFiles}, 200
|
||||||
|
|
||||||
@app.route("/request-tests", methods=["GET"])
|
@app.route("/request-tests", methods=["GET"])
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export default function RegisterFile() {
|
|||||||
fetch(`http://localhost:8000/api/files/filter?peer_id=1`, {
|
fetch(`http://localhost:8000/api/files/filter?peer_id=1`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: getAuthHeaders()
|
headers: getAuthHeaders()
|
||||||
}) // replace with local api endpoint
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
setServerFiles(data)
|
setServerFiles(data)
|
||||||
@@ -46,10 +46,37 @@ export default function RegisterFile() {
|
|||||||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
// handle file upload
|
// 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 formData = new FormData(event.target as HTMLFormElement);
|
||||||
const file = formData.get('file');
|
// register file with central server
|
||||||
console.log(file, typeof file)
|
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 (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
|
|||||||
Reference in New Issue
Block a user