pdf-util / pages /File_Manager.py
venkatl's picture
v0.1.2
b3a633e
Raw
History Blame Contribute Delete
1.68 kB
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}")