import gradio as gr import os import shutil import json from pymongo.mongo_client import MongoClient from pymongo.server_api import ServerApi import urllib.parse from bson.objectid import ObjectId username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME')) password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD')) restUri = os.getenv('REST_URI') uri = f'mongodb+srv://{username}:{password}{restUri}' client = MongoClient(uri, server_api=ServerApi('1')) db = client['file_storage'] references_collection = db['references'] try: client.admin.command('ping') print("Pinged your deployment. You successfully connected to MongoDB!") except Exception as e: print(e) theme = gr.themes.Default( primary_hue="blue", secondary_hue="violet", neutral_hue="slate", ) def upload(file, secret): if secret == os.environ.get('SECRET_KEY'): root_directory = '/tmp/gradio' folder_contents_dict = get_folder_contents_dict(root_directory) base_url = "https://abhicodes-file-sharing-system.hf.space/file=/tmp/gradio" urls = [f"{base_url}/{folder}/{file}" for folder, files in folder_contents_dict.items() for file in files] references_collection.update_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"$set": {"urls": urls}}) gr.Info("Uploaded Successfully") else: for ele in file: dir_to_remove = os.path.dirname(ele) shutil.rmtree(dir_to_remove) gr.Warning("Unauthorized to upload") def get_folder_contents_dict(root_dir): folder_contents = {} for item in os.listdir(root_dir): item_path = os.path.join(root_dir, item) if os.path.isdir(item_path): contents = os.listdir(item_path) folder_contents[item] = contents return folder_contents def generate_markdown(json_data): urls = json_data.get("urls", []) markdown_lines = [] for url in urls: encoded_url = urllib.parse.quote(url, safe=':/') filename = url.split('/')[-1] encoded_filename = urllib.parse.quote(filename) markdown_lines.append(f'- {encoded_filename}') return "\n".join(markdown_lines) def get_uploads(secret): if secret == os.environ.get('SECRET_KEY'): result = references_collection.find_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"_id": 0}) if result: markdown_output = generate_markdown(result) gr.Info("Recieved uploaded files") return markdown_output else: return '''

No result found

''' else: return '''

Invalid Secret Key

''' js = ''' function test() { document.title = "File Sharing System"; var link = document.createElement('link'); link.type = 'image/x-icon'; link.rel = 'shortcut icon'; link.href = 'https://cdn3d.iconscout.com/3d/premium/thumb/cloud-storage-5402862-4521475.png'; document.getElementsByTagName('head')[0].appendChild(link); function updateHeroContainerStyle() { if (window.innerWidth <= 600) { const ele = document.querySelector(".hero-container"); if (ele) { ele.style.flexDirection = 'column'; ele.style.padding = '5px'; ele.style.margin = '0px'; ele.style.gap = '0px'; } } else { const ele = document.querySelector(".hero-container"); if (ele) { ele.style.flexDirection = 'row'; ele.style.padding = '5px'; ele.style.margin = '0px 20px 0px 20px'; ele.style.gap = '50px'; } } } updateHeroContainerStyle(); window.addEventListener('resize', updateHeroContainerStyle); } ''' css = ''' .hero-container { display: flex; flex-direction: row; gap:50px; justify-content: center; align-items: center; padding: 5px; border-radius: 10px; max-width: 100%; margin: 20px; margin-top: 0px; margin-bottom: 0px; } .logo { width: 80px; } .description { text-align: justify; } ''' with gr.Blocks(theme=theme, js=js, css=css) as demo: gr.Markdown('''

File Storing and Sharing System

''') with gr.Column(): gr.Markdown('''

This project is a file storing and sharing system built using Gradio for the front-end interface and MongoDB for the back-end database. The system allows users to upload files, which are then stored and managed within a specified directory. URLs for accessing these files are generated and stored in a MongoDB collection, making it easy to share and access the uploaded files.

''') secret_key = gr.Text(label="🔐 Secret Key", placeholder="Enter your secret key here...", type="password", autofocus=True) with gr.Row(): with gr.Column(): gr.Markdown('''

uploadUploader

''') input = gr.File(label="Input", file_count="multiple") input.upload(fn=upload, inputs=[input, secret_key]) with gr.Column(): gr.Markdown('''

downloadDownloader

''') uploads = gr.Markdown(label="Uploads") get_upload_button = gr.Button("Get Uploads", variant='primary') get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads) gr.Markdown('''> To know more read the docs at: [Documentation](https://huggingface.co/spaces/abhicodes/file-sharing-system/blob/main/README.md)''') demo.launch(debug=True)