cloud-storage / app.py
13ze's picture
Update app.py
573bc06 verified
import os
import gradio as gr
# Configuração do diretório de armazenamento
STORAGE_DIR = "storage"
# Criar o diretório de armazenamento se não existir
if not os.path.exists(STORAGE_DIR):
os.makedirs(STORAGE_DIR)
# Função para upload de arquivos
def upload_file(file):
# Salvar o arquivo no diretório de armazenamento
file_path = os.path.join(STORAGE_DIR, file.name)
with open(file_path, "wb") as f:
f.write(file) # Escrever o conteúdo diretamente
return "Arquivo enviado com sucesso!"
# Função para listar arquivos
def list_files():
file_tree = {}
for root, dirs, files in os.walk(STORAGE_DIR):
for name in files:
folder_path = os.path.relpath(root, STORAGE_DIR)
if folder_path not in file_tree:
file_tree[folder_path] = []
file_tree[folder_path].append(name)
return file_tree
# Função para deletar arquivos
def delete_file(file_path):
abs_path = os.path.join(STORAGE_DIR, file_path)
if os.path.isfile(abs_path):
os.remove(abs_path)
return f"Arquivo {file_path} deletado com sucesso!"
else:
return f"Arquivo {file_path} não encontrado!"
# Interface Gradio
def file_explorer_ui():
with gr.Blocks() as demo:
gr.Markdown("# Gerenciamento de Armazenamento em Nuvem")
with gr.Row():
with gr.Column():
upload = gr.File(label="Enviar Arquivo")
upload_btn = gr.Button("Enviar")
upload_output = gr.Textbox()
with gr.Column():
delete_file_input = gr.Textbox(label="Caminho do arquivo para deletar")
delete_file_btn = gr.Button("Deletar")
delete_output = gr.Textbox()
with gr.Row():
list_files_btn = gr.Button("Listar Arquivos")
file_list_output = gr.JSON()
upload_btn.click(upload_file, inputs=upload, outputs=upload_output)
delete_file_btn.click(delete_file, inputs=delete_file_input, outputs=delete_output)
list_files_btn.click(list_files, outputs=file_list_output)
return demo
# Rodar a aplicação
file_explorer_ui().launch(share=True)