13ze commited on
Commit
8afd771
·
verified ·
1 Parent(s): 28e3cfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -89
app.py CHANGED
@@ -9,17 +9,6 @@ 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 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
 
@@ -101,81 +90,3 @@ with gr.Blocks() as app:
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:
130
- # Normalizar nome do arquivo
131
- filename = secure_filename(file.name)
132
-
133
- # Verificar extensão do arquivo
134
- if not allowed_file(filename):
135
- return "Tipo de arquivo não permitido."
136
-
137
- # Verificar tamanho do arquivo
138
- if os.path.getsize(file.name) > MAX_FILE_SIZE:
139
- return "Arquivo excede o tamanho máximo permitido de 10MB."
140
-
141
- # Salvar arquivo
142
- file_path = os.path.join(STORAGE_DIR, filename)
143
- shutil.copyfile(file.name, file_path)
144
-
145
- # Verificar conteúdo do arquivo
146
- if not is_image(file_path):
147
- os.remove(file_path)
148
- return "O conteúdo do arquivo não é uma imagem válida."
149
-
150
- public_url = f"/files/{filename}"
151
- delete_link = f"/delete?file={filename}"
152
- return f"Arquivo disponível em: {public_url}\nLink para deletar: {delete_link}"
153
- return "Falha no upload do arquivo."
154
-
155
- # Função para deletar o arquivo
156
- def delete_file(file_name):
157
- file_path = os.path.join(STORAGE_DIR, file_name)
158
- if os.path.exists(file_path):
159
- os.remove(file_path)
160
- return f"Arquivo {file_name} deletado com sucesso."
161
- return f"Arquivo {file_name} não encontrado."
162
-
163
- # Inicializar o diretório de armazenamento
164
- initialize_storage()
165
-
166
- # Interface Gradio
167
- with gr.Blocks() as app:
168
- with gr.Row():
169
- gr.Markdown("## Gerenciamento de Arquivos na Nuvem")
170
- with gr.Row():
171
- upload = gr.File(label="Selecione um arquivo para upload")
172
- output = gr.Textbox(label="Status")
173
- with gr.Row():
174
- delete_file_name = gr.Textbox(label="Nome do arquivo para deletar")
175
- delete_button = gr.Button("Deletar arquivo")
176
- delete_output = gr.Textbox(label="Status de deleção")
177
-
178
- upload.upload(upload_file, inputs=[upload], outputs=[output])
179
- delete_button.click(delete_file, inputs=[delete_file_name], outputs=[delete_output])
180
-
181
- app.launch()
 
9
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
10
  MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB
11
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Nome do espaço HuggingFace
13
  SPACE_NAME = os.getenv('SPACE_NAME')
14
 
 
90
  delete_button.click(delete_file, inputs=[delete_file_name], outputs=[delete_output])
91
 
92
  app.launch()