abhicodes commited on
Commit
77251b2
·
verified ·
1 Parent(s): d494edb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -24
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import os
 
3
  import json
4
  from pymongo.mongo_client import MongoClient
5
  from pymongo.server_api import ServerApi
@@ -26,20 +27,22 @@ theme = gr.themes.Default(
26
  neutral_hue="slate",
27
  )
28
 
29
- def upload(file):
30
- root_directory = '/tmp/gradio'
31
- folder_contents_dict = get_folder_contents_dict(root_directory)
32
- print(folder_contents_dict)
33
-
34
- base_url = "https://abhicodes-file-sharing-system.hf.space/file=/tmp/gradio"
35
-
36
- urls = [f"{base_url}/{folder}/{file}" for folder, files in folder_contents_dict.items() for file in files]
37
-
38
- print(json.dumps(urls, indent=4))
39
-
40
- references_collection.update_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"$set": {"urls": urls}})
41
 
42
- gr.Info("Uploaded Successfully")
 
 
 
 
 
 
 
43
 
44
  def get_folder_contents_dict(root_dir):
45
  folder_contents = {}
@@ -55,7 +58,6 @@ def generate_markdown(json_data):
55
  urls = json_data.get("urls", [])
56
  markdown_lines = []
57
  for url in urls:
58
- # Encode URL components
59
  encoded_url = urllib.parse.quote(url, safe=':/')
60
  filename = url.split('/')[-1]
61
  encoded_filename = urllib.parse.quote(filename)
@@ -67,39 +69,85 @@ def get_uploads(secret):
67
  result = references_collection.find_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"_id": 0})
68
  if result:
69
  markdown_output = generate_markdown(result)
 
70
  return markdown_output
71
  else:
72
- return {"error": "No result found"}
73
  else:
74
- return {"error": "Unauthorized"}
75
 
76
  js = '''
77
  function test() {
78
- console.log("Test");
79
  document.title = "File Sharing System";
80
  var link = document.createElement('link');
81
  link.type = 'image/x-icon';
82
  link.rel = 'shortcut icon';
83
  link.href = 'https://cdn3d.iconscout.com/3d/premium/thumb/cloud-storage-5402862-4521475.png';
84
  document.getElementsByTagName('head')[0].appendChild(link);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
  '''
87
 
88
- with gr.Blocks(theme=theme, js=js) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  gr.Markdown('''<h1 style="text-align:center;">File Storing and Sharing System</h1>''')
90
  with gr.Column():
91
- gr.Markdown('''<div style="display:flex;justify-content:center;align-items:center"><img src="https://cdn3d.iconscout.com/3d/premium/thumb/cloud-storage-5402862-4521475.png" alt="logo" width="100"/>
92
- <div style="width:100px;"></div>
93
- 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.''')
94
-
 
 
95
  with gr.Row():
96
  with gr.Column():
97
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/FB6tMdT/upload.png" alt="upload" width="50">Uploader</h1>''')
98
  input = gr.File(label="Input", file_count="multiple")
99
- input.upload(fn=upload, inputs=input)
100
  with gr.Column():
101
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/K9Bq3j7/download.png" alt="download" width="50">Downloader</h1>''')
102
- secret_key = gr.Text(placeholder="Enter your Secret Key", type="password", autofocus=True)
103
  uploads = gr.Markdown(label="Uploads")
104
  get_upload_button = gr.Button("Get Uploads", variant='primary')
105
  get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads)
 
1
  import gradio as gr
2
  import os
3
+ import shutil
4
  import json
5
  from pymongo.mongo_client import MongoClient
6
  from pymongo.server_api import ServerApi
 
27
  neutral_hue="slate",
28
  )
29
 
30
+ def upload(file, secret):
31
+ if secret == os.environ.get('SECRET_KEY'):
32
+ root_directory = '/tmp/gradio'
33
+ folder_contents_dict = get_folder_contents_dict(root_directory)
34
+ base_url = "https://abhicodes-file-sharing-system.hf.space/file=/tmp/gradio"
35
+ urls = [f"{base_url}/{folder}/{file}" for folder, files in folder_contents_dict.items() for file in files]
36
+ references_collection.update_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"$set": {"urls": urls}})
 
 
 
 
 
37
 
38
+ gr.Info("Uploaded Successfully")
39
+
40
+ else:
41
+ for ele in file:
42
+ dir_to_remove = os.path.dirname(ele)
43
+ shutil.rmtree(dir_to_remove)
44
+
45
+ gr.Warning("Unauthorized to upload")
46
 
47
  def get_folder_contents_dict(root_dir):
48
  folder_contents = {}
 
58
  urls = json_data.get("urls", [])
59
  markdown_lines = []
60
  for url in urls:
 
61
  encoded_url = urllib.parse.quote(url, safe=':/')
62
  filename = url.split('/')[-1]
63
  encoded_filename = urllib.parse.quote(filename)
 
69
  result = references_collection.find_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"_id": 0})
70
  if result:
71
  markdown_output = generate_markdown(result)
72
+ gr.Info("Recieved uploaded files")
73
  return markdown_output
74
  else:
75
+ return '''<p style="color:red;font-size:20px;text-align:center;font-weight:bold;">No result found</p>'''
76
  else:
77
+ return '''<p style="color:red;font-size:20px;text-align:center;font-weight:bold;">Invalid Secret Key</p>'''
78
 
79
  js = '''
80
  function test() {
 
81
  document.title = "File Sharing System";
82
  var link = document.createElement('link');
83
  link.type = 'image/x-icon';
84
  link.rel = 'shortcut icon';
85
  link.href = 'https://cdn3d.iconscout.com/3d/premium/thumb/cloud-storage-5402862-4521475.png';
86
  document.getElementsByTagName('head')[0].appendChild(link);
87
+
88
+ function updateHeroContainerStyle() {
89
+ if (window.innerWidth <= 600) {
90
+ const ele = document.querySelector(".hero-container");
91
+ if (ele) {
92
+ ele.style.flexDirection = 'column';
93
+ ele.style.padding = '5px';
94
+ ele.style.margin = '0px';
95
+ ele.style.gap = '0px';
96
+ }
97
+ }
98
+ else {
99
+ const ele = document.querySelector(".hero-container");
100
+ if (ele) {
101
+ ele.style.flexDirection = 'row';
102
+ ele.style.padding = '5px';
103
+ ele.style.margin = '0px 20px 0px 20px';
104
+ ele.style.gap = '50px';
105
+ }
106
+ }
107
+ }
108
+ updateHeroContainerStyle();
109
+ window.addEventListener('resize', updateHeroContainerStyle);
110
  }
111
  '''
112
 
113
+ css = '''
114
+ .hero-container {
115
+ display: flex;
116
+ flex-direction: row;
117
+ gap:50px;
118
+ justify-content: center;
119
+ align-items: center;
120
+ padding: 5px;
121
+ border-radius: 10px;
122
+ max-width: 100%;
123
+ margin: 20px;
124
+ margin-top: 0px;
125
+ margin-bottom: 0px;
126
+ }
127
+ .logo {
128
+ width: 80px;
129
+ }
130
+ .description {
131
+ text-align: justify;
132
+ }
133
+ '''
134
+
135
+ with gr.Blocks(theme=theme, js=js, css=css) as demo:
136
  gr.Markdown('''<h1 style="text-align:center;">File Storing and Sharing System</h1>''')
137
  with gr.Column():
138
+ gr.Markdown(''' <div class="hero-container">
139
+ <img src="https://cdn3d.iconscout.com/3d/premium/thumb/cloud-storage-5402862-4521475.png" alt="logo" class="logo">
140
+ <p class="description">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.</p>
141
+ </div> ''')
142
+
143
+ secret_key = gr.Text(label="🔐 Secret Key", placeholder="Enter your secret key here...", type="password", autofocus=True)
144
  with gr.Row():
145
  with gr.Column():
146
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/FB6tMdT/upload.png" alt="upload" width="50">Uploader</h1>''')
147
  input = gr.File(label="Input", file_count="multiple")
148
+ input.upload(fn=upload, inputs=[input, secret_key])
149
  with gr.Column():
150
  gr.Markdown('''<h1 style="text-align:center;display:flex;justify-content:center;align-items:center"><img src="https://i.ibb.co/K9Bq3j7/download.png" alt="download" width="50">Downloader</h1>''')
 
151
  uploads = gr.Markdown(label="Uploads")
152
  get_upload_button = gr.Button("Get Uploads", variant='primary')
153
  get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads)