Spaces:
Sleeping
Sleeping
Seth
commited on
Commit
·
d88cd5a
1
Parent(s):
f5f5eec
update
Browse files- backend/app/main.py +31 -13
backend/app/main.py
CHANGED
|
@@ -694,11 +694,17 @@ async def get_assets(
|
|
| 694 |
]
|
| 695 |
|
| 696 |
@app.delete("/api/assets/{asset_id}")
|
| 697 |
-
async def delete_asset(asset_id
|
| 698 |
"""Delete an asset from both filesystem and database"""
|
| 699 |
try:
|
| 700 |
from app.models import Asset
|
| 701 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
# Get asset from database
|
| 703 |
conn = get_direct_psycopg2_connection()
|
| 704 |
if not conn:
|
|
@@ -709,8 +715,8 @@ async def delete_asset(asset_id: int, db: Session = Depends(get_db)):
|
|
| 709 |
cursor.execute("""
|
| 710 |
SELECT id, name, file_path
|
| 711 |
FROM assets
|
| 712 |
-
WHERE id = %s
|
| 713 |
-
""", (
|
| 714 |
row = cursor.fetchone()
|
| 715 |
|
| 716 |
if not row:
|
|
@@ -730,7 +736,7 @@ async def delete_asset(asset_id: int, db: Session = Depends(get_db)):
|
|
| 730 |
# Continue with database deletion even if file deletion fails
|
| 731 |
|
| 732 |
# Delete from database
|
| 733 |
-
cursor.execute("DELETE FROM assets WHERE id = %s", (
|
| 734 |
conn.commit()
|
| 735 |
cursor.close()
|
| 736 |
conn.close()
|
|
@@ -738,7 +744,7 @@ async def delete_asset(asset_id: int, db: Session = Depends(get_db)):
|
|
| 738 |
return {
|
| 739 |
"success": True,
|
| 740 |
"message": f"Asset '{row[1]}' deleted successfully",
|
| 741 |
-
"asset_id":
|
| 742 |
}
|
| 743 |
except Exception as db_error:
|
| 744 |
if conn:
|
|
@@ -750,7 +756,7 @@ async def delete_asset(asset_id: int, db: Session = Depends(get_db)):
|
|
| 750 |
raise HTTPException(status_code=500, detail=str(e))
|
| 751 |
|
| 752 |
@app.get("/api/assets/{asset_id}/pdf-pages")
|
| 753 |
-
async def get_pdf_pages(asset_id
|
| 754 |
"""Convert PDF to images and return page URLs"""
|
| 755 |
try:
|
| 756 |
from app.models import Asset
|
|
@@ -764,6 +770,12 @@ async def get_pdf_pages(asset_id: int, db: Session = Depends(get_db)):
|
|
| 764 |
import base64
|
| 765 |
from io import BytesIO
|
| 766 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
# Get asset from database
|
| 768 |
conn = get_direct_psycopg2_connection()
|
| 769 |
if not conn:
|
|
@@ -774,8 +786,8 @@ async def get_pdf_pages(asset_id: int, db: Session = Depends(get_db)):
|
|
| 774 |
cursor.execute("""
|
| 775 |
SELECT id, name, file_path, file_type
|
| 776 |
FROM assets
|
| 777 |
-
WHERE id = %s
|
| 778 |
-
""", (
|
| 779 |
row = cursor.fetchone()
|
| 780 |
cursor.close()
|
| 781 |
conn.close()
|
|
@@ -807,7 +819,7 @@ async def get_pdf_pages(asset_id: int, db: Session = Depends(get_db)):
|
|
| 807 |
})
|
| 808 |
|
| 809 |
return {
|
| 810 |
-
"asset_id":
|
| 811 |
"asset_name": row[1],
|
| 812 |
"total_pages": len(page_images),
|
| 813 |
"pages": page_images
|
|
@@ -826,14 +838,20 @@ async def get_pdf_pages(asset_id: int, db: Session = Depends(get_db)):
|
|
| 826 |
raise HTTPException(status_code=500, detail=str(e))
|
| 827 |
|
| 828 |
@app.get("/api/assets/{asset_id}/download")
|
| 829 |
-
async def download_asset(asset_id
|
| 830 |
"""Download or preview an asset file"""
|
| 831 |
try:
|
| 832 |
from app.models import Asset
|
| 833 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
# Try to get asset from database
|
| 835 |
try:
|
| 836 |
-
db_asset = db.query(Asset).filter(Asset.id ==
|
| 837 |
except Exception as orm_error:
|
| 838 |
# If ORM fails, use direct psycopg2
|
| 839 |
if "Could not determine version" in str(orm_error):
|
|
@@ -844,8 +862,8 @@ async def download_asset(asset_id: int, db: Session = Depends(get_db)):
|
|
| 844 |
cursor.execute("""
|
| 845 |
SELECT id, name, file_path, file_type
|
| 846 |
FROM assets
|
| 847 |
-
WHERE id = %s
|
| 848 |
-
""", (
|
| 849 |
row = cursor.fetchone()
|
| 850 |
cursor.close()
|
| 851 |
conn.close()
|
|
|
|
| 694 |
]
|
| 695 |
|
| 696 |
@app.delete("/api/assets/{asset_id}")
|
| 697 |
+
async def delete_asset(asset_id, db: Session = Depends(get_db)):
|
| 698 |
"""Delete an asset from both filesystem and database"""
|
| 699 |
try:
|
| 700 |
from app.models import Asset
|
| 701 |
|
| 702 |
+
# Convert asset_id to int (Python int can handle arbitrarily large integers)
|
| 703 |
+
try:
|
| 704 |
+
asset_id_int = int(asset_id)
|
| 705 |
+
except (ValueError, TypeError):
|
| 706 |
+
raise HTTPException(status_code=400, detail=f"Invalid asset ID: {asset_id}")
|
| 707 |
+
|
| 708 |
# Get asset from database
|
| 709 |
conn = get_direct_psycopg2_connection()
|
| 710 |
if not conn:
|
|
|
|
| 715 |
cursor.execute("""
|
| 716 |
SELECT id, name, file_path
|
| 717 |
FROM assets
|
| 718 |
+
WHERE id = %s::bigint
|
| 719 |
+
""", (asset_id_int,))
|
| 720 |
row = cursor.fetchone()
|
| 721 |
|
| 722 |
if not row:
|
|
|
|
| 736 |
# Continue with database deletion even if file deletion fails
|
| 737 |
|
| 738 |
# Delete from database
|
| 739 |
+
cursor.execute("DELETE FROM assets WHERE id = %s::bigint", (asset_id_int,))
|
| 740 |
conn.commit()
|
| 741 |
cursor.close()
|
| 742 |
conn.close()
|
|
|
|
| 744 |
return {
|
| 745 |
"success": True,
|
| 746 |
"message": f"Asset '{row[1]}' deleted successfully",
|
| 747 |
+
"asset_id": str(asset_id_int) # Return as string
|
| 748 |
}
|
| 749 |
except Exception as db_error:
|
| 750 |
if conn:
|
|
|
|
| 756 |
raise HTTPException(status_code=500, detail=str(e))
|
| 757 |
|
| 758 |
@app.get("/api/assets/{asset_id}/pdf-pages")
|
| 759 |
+
async def get_pdf_pages(asset_id, db: Session = Depends(get_db)):
|
| 760 |
"""Convert PDF to images and return page URLs"""
|
| 761 |
try:
|
| 762 |
from app.models import Asset
|
|
|
|
| 770 |
import base64
|
| 771 |
from io import BytesIO
|
| 772 |
|
| 773 |
+
# Convert asset_id to int (Python int can handle arbitrarily large integers)
|
| 774 |
+
try:
|
| 775 |
+
asset_id_int = int(asset_id)
|
| 776 |
+
except (ValueError, TypeError):
|
| 777 |
+
raise HTTPException(status_code=400, detail=f"Invalid asset ID: {asset_id}")
|
| 778 |
+
|
| 779 |
# Get asset from database
|
| 780 |
conn = get_direct_psycopg2_connection()
|
| 781 |
if not conn:
|
|
|
|
| 786 |
cursor.execute("""
|
| 787 |
SELECT id, name, file_path, file_type
|
| 788 |
FROM assets
|
| 789 |
+
WHERE id = %s::bigint
|
| 790 |
+
""", (asset_id_int,))
|
| 791 |
row = cursor.fetchone()
|
| 792 |
cursor.close()
|
| 793 |
conn.close()
|
|
|
|
| 819 |
})
|
| 820 |
|
| 821 |
return {
|
| 822 |
+
"asset_id": str(asset_id_int), # Return as string
|
| 823 |
"asset_name": row[1],
|
| 824 |
"total_pages": len(page_images),
|
| 825 |
"pages": page_images
|
|
|
|
| 838 |
raise HTTPException(status_code=500, detail=str(e))
|
| 839 |
|
| 840 |
@app.get("/api/assets/{asset_id}/download")
|
| 841 |
+
async def download_asset(asset_id, db: Session = Depends(get_db)):
|
| 842 |
"""Download or preview an asset file"""
|
| 843 |
try:
|
| 844 |
from app.models import Asset
|
| 845 |
|
| 846 |
+
# Convert asset_id to int (Python int can handle arbitrarily large integers)
|
| 847 |
+
try:
|
| 848 |
+
asset_id_int = int(asset_id)
|
| 849 |
+
except (ValueError, TypeError):
|
| 850 |
+
raise HTTPException(status_code=400, detail=f"Invalid asset ID: {asset_id}")
|
| 851 |
+
|
| 852 |
# Try to get asset from database
|
| 853 |
try:
|
| 854 |
+
db_asset = db.query(Asset).filter(Asset.id == asset_id_int).first()
|
| 855 |
except Exception as orm_error:
|
| 856 |
# If ORM fails, use direct psycopg2
|
| 857 |
if "Could not determine version" in str(orm_error):
|
|
|
|
| 862 |
cursor.execute("""
|
| 863 |
SELECT id, name, file_path, file_type
|
| 864 |
FROM assets
|
| 865 |
+
WHERE id = %s::bigint
|
| 866 |
+
""", (asset_id_int,))
|
| 867 |
row = cursor.fetchone()
|
| 868 |
cursor.close()
|
| 869 |
conn.close()
|