Spaces:
Running
Running
File size: 4,211 Bytes
91a7855 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# import streamlit as st
# import os
# import shutil
# def main():
# st.title("File Explorer")
# # File browsing
# st.header("Browse Files")
# current_dir = st.text_input("Current Directory", value=os.getcwd())
# try:
# files = os.listdir(current_dir)
# selected_file = st.selectbox("Select a file/folder", files)
# full_path = os.path.join(current_dir, selected_file)
# if selected_file:
# if os.path.isfile(full_path):
# st.write(f"You selected file: {selected_file}")
# # Download
# with open(full_path, "rb") as f:
# st.download_button("Download", f, file_name=selected_file)
# # Read
# if selected_file.endswith((".txt",".py",".md",".csv")):
# try:
# with open(full_path,"r") as f_read:
# content = f_read.read()
# st.text_area("File content", content, height=300)
# except UnicodeDecodeError:
# st.write("Cannot display binary file content.")
# elif os.path.isdir(full_path):
# st.write(f"You selected directory: {selected_file}")
# except FileNotFoundError:
# st.error("Invalid directory path.")
# except PermissionError:
# st.error("Permission denied to access this directory.")
# except OSError as e:
# st.error(f"OS error: {e}")
# # File Uploading
# st.header("Upload Files")
# uploaded_files = st.file_uploader("Choose files to upload", accept_multiple_files=True)
# upload_dir = st.text_input("Upload Directory", value=os.getcwd())
# if uploaded_files:
# for uploaded_file in uploaded_files:
# try:
# with open(os.path.join(upload_dir, uploaded_file.name), "wb") as f:
# f.write(uploaded_file.getbuffer())
# st.success(f"Saved File: {uploaded_file.name} to {upload_dir}")
# except Exception as e:
# st.error(f"Error saving file: {e}")
# if __name__ == "__main__":
# main()
from flask import Flask, url_for, redirect
from flask import request as req
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
from werkzeug.utils import secure_filename
import os
from PIL import Image
app.config['UPLOAD_FOLDER'] = "static"
from pyngrok import ngrok
# Open a HTTP tunnel on the default port 80
# <NgrokTunnel: "https://<public_sub>.ngrok.io" -> "http://localhost:80">
http_tunnel = ngrok.connect("1337", "http")
print(http_tunnel)
@app.route('/upload', methods=['GET','POST'])
def index():
# If a post method then handle file upload
if req.method == 'POST':
if 'file' not in req.files:
return redirect('/')
file = req.files['file']
if file :
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if ("camera" in file.filename or "capture" in file.filename or "IMG" in file.filename or "Screenshot" in file.filename) :
img=Image.open(f"static/{filename}")
img.thumbnail((512, 512),Image.Resampling.LANCZOS)
img.save(f"static/{filename}")
return filename
# Get Files in the directory and create list items to be displayed to the user
file_list = ''
for f in os.listdir(app.config['UPLOAD_FOLDER']):
# Create link html
link = url_for("static", filename=f)
file_list = file_list + '<li><a href="%s">%s</a></li>' % (link, f)
# Format return HTML - allow file upload and list all available files
return_html = '''
<!doctype html>
<title>Upload File</title>
<h1>Upload File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file><br>
<input type=submit value=Upload>
</form>
<hr>
<h1>Files</h1>
<ol>%s</ol>
''' % file_list
return return_html
if __name__ == '__main__':
config = {
'host': 'localhost',
'port': 1337,
'debug': True,
}
app.run(**config) |