Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
import re
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Diretório onde os arquivos serão armazenados
|
| 8 |
+
STORAGE_DIR = "/home/user/storage"
|
| 9 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
|
| 10 |
+
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
|
| 11 |
+
|
| 12 |
+
# Função para inicializar o diretório de armazenamento
|
| 13 |
+
def initialize_storage():
|
| 14 |
+
if not os.path.exists(STORAGE_DIR):
|
| 15 |
+
os.makedirs(STORAGE_DIR)
|
| 16 |
+
|
| 17 |
+
# Função para verificar o tipo de arquivo
|
| 18 |
+
def allowed_file(filename):
|
| 19 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 20 |
+
|
| 21 |
+
# Função para normalizar o nome do arquivo
|
| 22 |
+
def secure_filename(filename):
|
| 23 |
+
filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
|
| 24 |
+
return filename
|
| 25 |
+
|
| 26 |
+
# Função para verificar o conteúdo do arquivo
|
| 27 |
+
def is_image(file_path):
|
| 28 |
+
try:
|
| 29 |
+
with Image.open(file_path) as img:
|
| 30 |
+
img.verify()
|
| 31 |
+
return True
|
| 32 |
+
except (IOError, SyntaxError) as e:
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
+
# Função para fazer upload do arquivo
|
| 36 |
+
def upload_file(file):
|
| 37 |
+
if file:
|
| 38 |
+
# Normalizar nome do arquivo
|
| 39 |
+
filename = secure_filename(file.name)
|
| 40 |
+
|
| 41 |
+
# Verificar extensão do arquivo
|
| 42 |
+
if not allowed_file(filename):
|
| 43 |
+
return "Tipo de arquivo não permitido."
|
| 44 |
+
|
| 45 |
+
# Verificar tamanho do arquivo
|
| 46 |
+
if os.path.getsize(file.name) > MAX_FILE_SIZE:
|
| 47 |
+
return "Arquivo excede o tamanho máximo permitido de 10MB."
|
| 48 |
+
|
| 49 |
+
# Salvar arquivo
|
| 50 |
+
file_path = os.path.join(STORAGE_DIR, filename)
|
| 51 |
+
shutil.copyfile(file.name, file_path)
|
| 52 |
+
|
| 53 |
+
# Verificar conteúdo do arquivo
|
| 54 |
+
if not is_image(file_path):
|
| 55 |
+
os.remove(file_path)
|
| 56 |
+
return "O conteúdo do arquivo não é uma imagem válida."
|
| 57 |
+
|
| 58 |
+
public_url = f"/files/{filename}"
|
| 59 |
+
delete_link = f"/delete?file={filename}"
|
| 60 |
+
return f"Arquivo disponível em: {public_url}\nLink para deletar: {delete_link}"
|
| 61 |
+
return "Falha no upload do arquivo."
|
| 62 |
+
|
| 63 |
+
# Função para deletar o arquivo
|
| 64 |
+
def delete_file(file_name):
|
| 65 |
+
file_path = os.path.join(STORAGE_DIR, file_name)
|
| 66 |
+
if os.path.exists(file_path):
|
| 67 |
+
os.remove(file_path)
|
| 68 |
+
return f"Arquivo {file_name} deletado com sucesso."
|
| 69 |
+
return f"Arquivo {file_name} não encontrado."
|
| 70 |
+
|
| 71 |
+
# Inicializar o diretório de armazenamento
|
| 72 |
+
initialize_storage()
|
| 73 |
+
|
| 74 |
+
# Interface Gradio
|
| 75 |
+
with gr.Blocks() as app:
|
| 76 |
+
with gr.Row():
|
| 77 |
+
gr.Markdown("## Gerenciamento de Arquivos na Nuvem")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
upload = gr.File(label="Selecione um arquivo para upload")
|
| 80 |
+
output = gr.Textbox(label="Status")
|
| 81 |
+
with gr.Row():
|
| 82 |
+
delete_file_name = gr.Textbox(label="Nome do arquivo para deletar")
|
| 83 |
+
delete_button = gr.Button("Deletar arquivo")
|
| 84 |
+
delete_output = gr.Textbox(label="Status de deleção")
|
| 85 |
+
|
| 86 |
+
upload.upload(upload_file, inputs=[upload], outputs=[output])
|
| 87 |
+
delete_button.click(delete_file, inputs=[delete_file_name], outputs=[delete_output])
|
| 88 |
+
|
| 89 |
+
app.launch()
|