Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -9,6 +9,7 @@ import cloudinary
|
|
9 |
import cloudinary.uploader
|
10 |
from cloudinary.utils import cloudinary_url
|
11 |
import requests
|
|
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
@@ -74,7 +75,7 @@ async def decrypt_and_upload(
|
|
74 |
):
|
75 |
try:
|
76 |
# Download file data from the URL
|
77 |
-
response = requests.get(url)
|
78 |
if response.status_code != 200:
|
79 |
return JSONResponse(content={"error": "Failed to download the file."}, status_code=400)
|
80 |
|
@@ -87,20 +88,24 @@ async def decrypt_and_upload(
|
|
87 |
mediaKeyExpanded[16:48], mediaData[:-10], mediaKeyExpanded[:16]
|
88 |
)
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
|
|
93 |
|
94 |
# Upload decrypted data to Cloudinary
|
95 |
upload_result = cloudinary.uploader.upload(
|
96 |
-
|
97 |
-
resource_type=
|
98 |
-
format=
|
99 |
eager=[{"width": 720, "height": 480, "crop": "pad"}], # Example transformation
|
100 |
eager_async=True
|
101 |
)
|
102 |
file_url = upload_result.get("secure_url")
|
103 |
|
|
|
|
|
|
|
104 |
# Provide Cloudinary URL
|
105 |
return JSONResponse(content={"decrypted_file_url": file_url})
|
106 |
|
|
|
9 |
import cloudinary.uploader
|
10 |
from cloudinary.utils import cloudinary_url
|
11 |
import requests
|
12 |
+
from tempfile import NamedTemporaryFile
|
13 |
|
14 |
app = FastAPI()
|
15 |
|
|
|
75 |
):
|
76 |
try:
|
77 |
# Download file data from the URL
|
78 |
+
response = requests.get(url, stream=True)
|
79 |
if response.status_code != 200:
|
80 |
return JSONResponse(content={"error": "Failed to download the file."}, status_code=400)
|
81 |
|
|
|
88 |
mediaKeyExpanded[16:48], mediaData[:-10], mediaKeyExpanded[:16]
|
89 |
)
|
90 |
|
91 |
+
# Save decrypted data to a temporary file
|
92 |
+
with NamedTemporaryFile(delete=False, suffix=f".{extension.get(media_type, 'bin')}") as temp_file:
|
93 |
+
temp_file.write(decrypted_data)
|
94 |
+
temp_file_path = temp_file.name
|
95 |
|
96 |
# Upload decrypted data to Cloudinary
|
97 |
upload_result = cloudinary.uploader.upload(
|
98 |
+
temp_file_path,
|
99 |
+
resource_type="video" if media_type.startswith("video") else "raw",
|
100 |
+
format=extension.get(media_type, "bin"),
|
101 |
eager=[{"width": 720, "height": 480, "crop": "pad"}], # Example transformation
|
102 |
eager_async=True
|
103 |
)
|
104 |
file_url = upload_result.get("secure_url")
|
105 |
|
106 |
+
# Clean up temporary file
|
107 |
+
os.remove(temp_file_path)
|
108 |
+
|
109 |
# Provide Cloudinary URL
|
110 |
return JSONResponse(content={"decrypted_file_url": file_url})
|
111 |
|