From a9234f82b730d159ee281617d7ef2bf906d9824b Mon Sep 17 00:00:00 2001 From: ocueye Date: Wed, 20 Nov 2024 02:20:15 +0000 Subject: [PATCH] first commit --- .gitignore | 1 + start.py | 116 ++++++++++++++++++++++++++++++++++++ uploads/1/Submission+2.html | 5 ++ webpage/index.html | 87 +++++++++++++++++++++++++++ webpage/popup.html | 64 ++++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 .gitignore create mode 100644 start.py create mode 100644 uploads/1/Submission+2.html create mode 100644 webpage/index.html create mode 100644 webpage/popup.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb9df04 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.venv \ No newline at end of file diff --git a/start.py b/start.py new file mode 100644 index 0000000..3461173 --- /dev/null +++ b/start.py @@ -0,0 +1,116 @@ +import os +import sys +import cherrypy +import time +import shutil + +def popup(mesage): + out = load("popup.html") + return out.replace("",mesage) + +def load(file): + path = os.path.dirname(os.path.realpath(sys.argv[0])) + with open(f"{path}/webpage/{file}") as f: + return f.read() +# Maximum time in minutes before a file is removed +MAINTENANCE_INTERVAL_MINUTES = 1 + + +def clear_cache(): + """ + Deletes files in the 'uploads' directory that have not been modified + within the last MAINTENANCE_INTERVAL_MINUTES. + """ + upload_dir = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "uploads") + print(upload_dir) + if not os.path.exists(upload_dir): + print("notfound") + return # If the directory doesn't exist, nothing to clean. + + for file in os.listdir(upload_dir): + print(file) + item_path = os.path.join(upload_dir, file) + last_mod_time = os.path.getmtime(item_path) + time_diff_minutes = (time.time() - last_mod_time) / 60 + + if time_diff_minutes >= MAINTENANCE_INTERVAL_MINUTES: + try: + shutil.rmtree(item_path) + print(f"Deleted {item_path}") + except Exception as e: + print(f"Error deleting {item_path}: {e}") + else: + print(f"File {file} is {time_diff_minutes:.2f} minutes old, not deleting.") + + +class FileUploadApp: + upload_dir = "uploads/" # Directory to save uploaded files + + def __init__(self): + # Ensure the upload directory exists + if not os.path.exists(self.upload_dir): + os.makedirs(self.upload_dir) + + @cherrypy.expose + def index(self): + # HTML form for file upload and file retrieval + return load("index.html") + + @cherrypy.expose + def upload(self, file): + clear_cache() + try: + # Find the lowest unused number as the identifier + existing_ids = set(int(folder) for folder in os.listdir(self.upload_dir) if folder.isdigit()) + ident = 1 # Start checking from 1 + while ident in existing_ids: + ident += 1 + + # Create the directory for the new upload + upload_path = os.path.join(self.upload_dir, str(ident)) + os.makedirs(upload_path) + file_path = os.path.join(upload_path, file.filename) + + # Save the file to the upload directory + with open(file_path, "wb") as f: + while chunk := file.file.read(8192): + f.write(chunk) + + return popup(f"File '{file.filename}' uploaded successfully! Your code is {ident}.") + except Exception as e: + return popup(f"An error occurred during upload: {str(e)}") + + @cherrypy.expose + def download(self, code): + try: + # Check if the directory for the code exists + download_path = os.path.join(self.upload_dir, code) + if not os.path.exists(download_path) or not os.path.isdir(download_path): + raise FileNotFoundError("Invalid code or file not found.") + + # Get the first file in the directory + files = os.listdir(download_path) + if not files: + raise FileNotFoundError("No files found for this code.") + + file_path = os.path.join(download_path, files[0]) + absolute_file_path = os.path.abspath(file_path) # Convert to absolute path + + # Serve the file as a downloadable attachment + return cherrypy.lib.static.serve_file(absolute_file_path, "application/octet-stream", "attachment") + except Exception as e: + return popup(f"An error occurred: {str(e)}") + + +if __name__ == "__main__": + config = { + "/": { + "tools.staticdir.root": os.path.abspath(os.getcwd()) + }, + "/uploads": { + "tools.staticdir.on": True, + "tools.staticdir.dir": os.path.abspath("uploads"), + } + } + + cherrypy.quickstart(FileUploadApp(), "/", config) diff --git a/uploads/1/Submission+2.html b/uploads/1/Submission+2.html new file mode 100644 index 0000000..49b6425 --- /dev/null +++ b/uploads/1/Submission+2.html @@ -0,0 +1,5 @@ +

Virtual insanity is a song about monsters and the monster is humans desire to innovate. And relates to my theme as this innovation causes humanity to be isolated from each other.

+

 

+

This is shown to us in the quote “and now every mother can chose the color of her child, that’s not nature’s way” and” And now there is no sound, for we all live underground”. The first quote shows that the Monster is humans desire to innovate by telling us that we are messing with things that can have massive consequences for nature and therefore ourselves. the next quote proves that as when he says that they live underground. He is referring to how technology is isolating us from others. It could also have the more literal meaning of how an incident involving technology forced humanity underground.

+

 

+

The second quote I have to prove this is “Always seem to be governed by this love we have For useless twisting of our new technology “This quote talks about how we get caught up in making more technology that we fail to take into account other important things like social issues, poverty, and the environment. This means the technology is a monster as it distracts us from fixing other important issues. This relates to my theme as this means technology isolates us from important issues.

\ No newline at end of file diff --git a/webpage/index.html b/webpage/index.html new file mode 100644 index 0000000..ba7aed1 --- /dev/null +++ b/webpage/index.html @@ -0,0 +1,87 @@ + + + + index.html + + + + + + + + + File Upload + + + +
+

Here to there

+

A simple file transfer webserver

+
+
+
+

Upload a File

+
+ +

+ +
+
+

+
+

Retrieve a File

+
+

id:

+ + +
+
+
+ + + + + + \ No newline at end of file diff --git a/webpage/popup.html b/webpage/popup.html new file mode 100644 index 0000000..40a4ac0 --- /dev/null +++ b/webpage/popup.html @@ -0,0 +1,64 @@ + + + + info + + + + + + + + + File Upload + + + + + + + + + + \ No newline at end of file