Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,20 @@ 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):
|
|
@@ -32,6 +46,84 @@ def is_image(file_path):
|
|
| 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:
|
|
|
|
| 9 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
|
| 10 |
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
|
| 11 |
|
| 12 |
+
# Função para inicializar o diretório import gradio as gr
|
| 13 |
+
import os
|
| 14 |
+
import shutil
|
| 15 |
+
import re
|
| 16 |
+
from PIL import Image
|
| 17 |
+
|
| 18 |
+
# Diretório onde os arquivos serão armazenados
|
| 19 |
+
STORAGE_DIR = "/home/user/storage"
|
| 20 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
|
| 21 |
+
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
|
| 22 |
+
|
| 23 |
+
# Nome do espaço HuggingFace
|
| 24 |
+
SPACE_NAME = os.getenv('SPACE_NAME')
|
| 25 |
+
|
| 26 |
# Função para inicializar o diretório de armazenamento
|
| 27 |
def initialize_storage():
|
| 28 |
if not os.path.exists(STORAGE_DIR):
|
|
|
|
| 46 |
except (IOError, SyntaxError) as e:
|
| 47 |
return False
|
| 48 |
|
| 49 |
+
# Função para fazer upload do arquivo
|
| 50 |
+
def upload_file(file):
|
| 51 |
+
if file:
|
| 52 |
+
# Normalizar nome do arquivo
|
| 53 |
+
filename = secure_filename(file.name)
|
| 54 |
+
|
| 55 |
+
# Verificar extensão do arquivo
|
| 56 |
+
if not allowed_file(filename):
|
| 57 |
+
return "Tipo de arquivo não permitido."
|
| 58 |
+
|
| 59 |
+
# Verificar tamanho do arquivo
|
| 60 |
+
if os.path.getsize(file.name) > MAX_FILE_SIZE:
|
| 61 |
+
return "Arquivo excede o tamanho máximo permitido de 10MB."
|
| 62 |
+
|
| 63 |
+
# Salvar arquivo
|
| 64 |
+
file_path = os.path.join(STORAGE_DIR, filename)
|
| 65 |
+
shutil.copyfile(file.name, file_path)
|
| 66 |
+
|
| 67 |
+
# Verificar conteúdo do arquivo
|
| 68 |
+
if not is_image(file_path):
|
| 69 |
+
os.remove(file_path)
|
| 70 |
+
return "O conteúdo do arquivo não é uma imagem válida."
|
| 71 |
+
|
| 72 |
+
base_url = f"https://{SPACE_NAME}.hf.space/files/{filename}"
|
| 73 |
+
delete_url = f"https://{SPACE_NAME}.hf.space/delete?file={filename}"
|
| 74 |
+
return f"Arquivo disponível em: {base_url}\nLink para deletar: {delete_url}"
|
| 75 |
+
return "Falha no upload do arquivo."
|
| 76 |
+
|
| 77 |
+
# Função para deletar o arquivo
|
| 78 |
+
def delete_file(file_name):
|
| 79 |
+
file_path = os.path.join(STORAGE_DIR, file_name)
|
| 80 |
+
if os.path.exists(file_path):
|
| 81 |
+
os.remove(file_path)
|
| 82 |
+
return f"Arquivo {file_name} deletado com sucesso."
|
| 83 |
+
return f"Arquivo {file_name} não encontrado."
|
| 84 |
+
|
| 85 |
+
# Inicializar o diretório de armazenamento
|
| 86 |
+
initialize_storage()
|
| 87 |
+
|
| 88 |
+
# Interface Gradio
|
| 89 |
+
with gr.Blocks() as app:
|
| 90 |
+
with gr.Row():
|
| 91 |
+
gr.Markdown("## Gerenciamento de Arquivos na Nuvem")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
upload = gr.File(label="Selecione um arquivo para upload")
|
| 94 |
+
output = gr.Textbox(label="Status")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
delete_file_name = gr.Textbox(label="Nome do arquivo para deletar")
|
| 97 |
+
delete_button = gr.Button("Deletar arquivo")
|
| 98 |
+
delete_output = gr.Textbox(label="Status de deleção")
|
| 99 |
+
|
| 100 |
+
upload.upload(upload_file, inputs=[upload], outputs=[output])
|
| 101 |
+
delete_button.click(delete_file, inputs=[delete_file_name], outputs=[delete_output])
|
| 102 |
+
|
| 103 |
+
app.launch()
|
| 104 |
+
de armazenamento
|
| 105 |
+
def initialize_storage():
|
| 106 |
+
if not os.path.exists(STORAGE_DIR):
|
| 107 |
+
os.makedirs(STORAGE_DIR)
|
| 108 |
+
|
| 109 |
+
# Função para verificar o tipo de arquivo
|
| 110 |
+
def allowed_file(filename):
|
| 111 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 112 |
+
|
| 113 |
+
# Função para normalizar o nome do arquivo
|
| 114 |
+
def secure_filename(filename):
|
| 115 |
+
filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename)
|
| 116 |
+
return filename
|
| 117 |
+
|
| 118 |
+
# Função para verificar o conteúdo do arquivo
|
| 119 |
+
def is_image(file_path):
|
| 120 |
+
try:
|
| 121 |
+
with Image.open(file_path) as img:
|
| 122 |
+
img.verify()
|
| 123 |
+
return True
|
| 124 |
+
except (IOError, SyntaxError) as e:
|
| 125 |
+
return False
|
| 126 |
+
|
| 127 |
# Função para fazer upload do arquivo
|
| 128 |
def upload_file(file):
|
| 129 |
if file:
|