File size: 1,682 Bytes
f60f447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3a633e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import requests
import streamlit as st
from io import BytesIO
import os

PDF_STORAGE_DIR = 'pdf_storage'
os.makedirs(PDF_STORAGE_DIR, exist_ok=True)

# Download PDF
def download_pdf(url):
    response = requests.get(url, verify= False)
    pdf_file_name = url.split('/')[-1]
    with open(os.path.join(PDF_STORAGE_DIR, pdf_file_name), 'wb+') as destination:
        for chunk in response.iter_content(1024):
            destination.write(chunk)
    return pdf_file_name

# Upload PDF
def upload_pdf(pdf_file):
    with open(os.path.join(PDF_STORAGE_DIR, pdf_file.name), 'wb+') as destination:
        for chunk in pdf_file.chunks():
            destination.write(chunk)
    return pdf_file.name

# Delete PDF
def delete_pdf(pdf_file_name):
    os.remove(os.path.join(PDF_STORAGE_DIR, pdf_file_name))
    return f"{pdf_file_name} deleted successfully!"

st.title("PDF File Manager")

tab1, tab2, tab3 = st.tabs(["Download PDF", "Upload PDF", "Manage PDFs"])

with tab1:
    url = st.text_input("Enter PDF URL")
    if st.button("Download PDF"):
        if not url:
            st.error("Please enter a URL")
        else:
            pdf_file_name = download_pdf(url)
            st.success(f"PDF downloaded: {pdf_file_name}")

with tab2:
    pdf_files = os.listdir(PDF_STORAGE_DIR)
    pdf_file_name = st.selectbox("Select PDF file to delete", pdf_files)
    if st.button("Delete PDF"):
        output = delete_pdf(pdf_file_name)
        st.success(output)

with tab3:
    st.write("# Upload PDF")  
    # Upload PDF
    pdf_file = st.file_uploader("Select PDF file")
    if pdf_file:
        pdf_file_name = upload_pdf(pdf_file)
        st.success(f"PDF uploaded: {pdf_file_name}")