import streamlit as st
from pymongo import MongoClient
import datetime
import os

# MongoDB 연결 설정
client = MongoClient("mongodb://localhost:27017")
db = client["file_storage"]
collection = db["files"]

# uploads 폴더 생성
UPLOAD_FOLDER = "uploads"
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

# 파일 업로드 페이지
def main():
    st.title("파일 업로드 페이지")

    # 파일 업로드
    uploaded_file = st.file_uploader("파일 선택", type=["txt", "csv", "xlsx"])

    if uploaded_file is not None:
        # 파일 정보 추출
        file_name = uploaded_file.name
        file_size = len(uploaded_file.getvalue())
        upload_date = datetime.datetime.now()

        # 파일 저장
        file_path = os.path.join(UPLOAD_FOLDER, file_name)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getvalue())

        # 파일 정보 MongoDB에 저장
        file_info = {
            "file_name": file_name,
            "file_size": file_size,
            "upload_date": upload_date
        }
        collection.insert_one(file_info)

        st.success("파일이 업로드되었습니다.")

    # 파일 리스트 출력
    st.subheader("파일 리스트")
    files = collection.find()
    for file in files:
        st.write("파일 이름:", file["file_name"])
        st.write("파일 크기:", file["file_size"])
        st.write("업로드 날짜:", file["upload_date"])
        st.write("---")

if __name__ == "__main__":
    main()
import streamlit as st
from pymongo import MongoClient
import datetime
import os
import pandas as pd

# MongoDB 연결 설정
client = MongoClient("mongodb://localhost:27017")
db = client["file_storage"]
collection = db["files"]

# uploads 폴더 생성
UPLOAD_FOLDER = "uploads"
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

# 파일 업로드 페이지
def main():
    st.title("파일 업로드 페이지")

    # 파일 업로드
    uploaded_file = st.file_uploader("파일 선택", type=["txt", "csv", "xlsx", "png", "jpg"])

    if uploaded_file is not None:
        # 파일 정보 추출
        file_name = uploaded_file.name
        file_size = len(uploaded_file.getvalue())
        upload_date = datetime.datetime.now()

        # 파일 저장
        file_path = os.path.join(UPLOAD_FOLDER, file_name)
        with open(file_path, "wb") as f:
            f.write(uploaded_file.getvalue())

        # 파일 정보 MongoDB에 저장
        file_info = {
            "file_name": file_name,
            "file_size": file_size,
            "upload_date": upload_date
        }
        collection.insert_one(file_info)

        st.success("파일이 업로드되었습니다.")

    # 파일 리스트 출력
    st.subheader("파일 리스트")
    files = collection.find()
    file_data = []
    for file in files:
        file_data.append({
            "파일 이름": file["file_name"],
            "파일 크기": file["file_size"],
            "업로드 날짜": file["upload_date"]
        })

    if file_data:
        df = pd.DataFrame(file_data)
        st.table(df)
    else:
        st.write("업로드된 파일이 없습니다.")

if __name__ == "__main__":
    main()