Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| from streamlit_tree_select import tree_select | |
| # 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 listar arquivos em formato de árvore | |
| def list_files_tree(directory): | |
| file_tree = {} | |
| for root, dirs, files in os.walk(directory): | |
| folder_path = os.path.relpath(root, directory) | |
| if folder_path not in file_tree: | |
| file_tree[folder_path] = {'folders': [], 'files': []} | |
| for d in dirs: | |
| file_tree[folder_path]['folders'].append(d) | |
| for f in files: | |
| file_tree[folder_path]['files'].append(f) | |
| return file_tree | |
| # Função para gerar árvore de arquivos | |
| def generate_file_tree(directory): | |
| tree_data = {} | |
| file_tree = list_files_tree(directory) | |
| for folder, contents in file_tree.items(): | |
| path_parts = folder.split(os.sep) | |
| current_level = tree_data | |
| for part in path_parts: | |
| if part not in current_level: | |
| current_level[part] = {} | |
| current_level = current_level[part] | |
| for f in contents['files']: | |
| current_level[f] = None | |
| return tree_data | |
| # Interface do Streamlit | |
| st.title("Gerenciamento de Armazenamento em Nuvem") | |
| # Upload de arquivo | |
| uploaded_file = st.file_uploader("Escolha um arquivo para enviar") | |
| if uploaded_file is not None: | |
| file_path = os.path.join(STORAGE_DIR, uploaded_file.name) | |
| with open(file_path, "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| st.success(f"Arquivo {uploaded_file.name} enviado com sucesso!") | |
| # Exibir arquivos | |
| st.header("Arquivos no Armazenamento") | |
| file_tree_data = generate_file_tree(STORAGE_DIR) | |
| selected_file = tree_select(file_tree_data) | |
| if selected_file: | |
| file_url = os.path.join(STORAGE_DIR, selected_file) | |
| if selected_file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.webp')): | |
| st.image(file_url, caption=selected_file, use_column_width=True) | |
| else: | |
| st.markdown(f"[{selected_file}]({file_url})") | |
| # Deletar arquivo | |
| st.header("Deletar Arquivo") | |
| file_to_delete = st.text_input("Digite o caminho do arquivo para deletar (ex: subpasta/nome_do_arquivo.txt)") | |
| if st.button("Deletar"): | |
| if file_to_delete: | |
| abs_path = os.path.join(STORAGE_DIR, file_to_delete) | |
| if os.path.isfile(abs_path): | |
| os.remove(abs_path) | |
| st.success(f"Arquivo {file_to_delete} deletado com sucesso!") | |
| else: | |
| st.error(f"Arquivo {file_to_delete} não encontrado!") | |