ValdeciRodrigues/Python-code-assistant
0
1# logic/file_reader.py2import os3import zipfile4 5def read_uploaded_file(file):6 filepath = file.name7 8 def safe_read(path):9 try:10 with open(path, "r", encoding="utf-8", errors="ignore") as f:11 return f.read()12 except Exception as e:13 return f"Erro ao ler {path}: {str(e)}"14 15 if filepath.endswith(".zip"):16 output = ""17 try:18 with zipfile.ZipFile(filepath, "r") as zip_ref:19 zip_ref.extractall("unzipped")20 for name in zip_ref.namelist():21 if name.endswith((".py", ".txt")):22 full_path = os.path.join("unzipped", name)23 if os.path.isfile(full_path):24 content = safe_read(full_path)25 output += f"\n# ===== {name} =====\n{content}\n"26 return output.strip() if output else "Nenhum arquivo .py ou .txt encontrado no ZIP."27 except zipfile.BadZipFile:28 return "Erro: Arquivo ZIP inválido ou corrompido."29 elif filepath.endswith((".py", ".txt")):30 return safe_read(filepath)31 else:32 return "Tipo de arquivo não suportado. Envie .py, .txt ou .zip."33 