Update app.py
Browse files
app.py
CHANGED
|
@@ -67,9 +67,13 @@ app = FastAPI(
|
|
| 67 |
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
| 70 |
# Create downloads directory if it doesn't exist
|
| 71 |
DOWNLOADS_DIR = "downloads"
|
|
|
|
| 72 |
os.makedirs(DOWNLOADS_DIR, exist_ok=True)
|
|
|
|
| 73 |
|
| 74 |
# Mount the downloads directory with cache control headers
|
| 75 |
class CacheControlStaticFiles(StaticFiles):
|
|
@@ -99,23 +103,23 @@ class DownloadResponse(BaseModel):
|
|
| 99 |
@app.post("/download", response_model=DownloadResponse)
|
| 100 |
async def download_file(request: DownloadRequest):
|
| 101 |
try:
|
| 102 |
-
# Create a unique subdirectory for this download
|
| 103 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 104 |
-
|
| 105 |
-
os.makedirs(
|
| 106 |
|
| 107 |
# Log the current working directory and download directory
|
| 108 |
logger.info(f"Current working directory: {os.getcwd()}")
|
| 109 |
-
logger.info(f"
|
| 110 |
|
| 111 |
# Create cookies file from environment variable
|
| 112 |
-
cookie_path = os.path.join(
|
| 113 |
logger.info(f"Creating cookies file at: {cookie_path}")
|
| 114 |
env_to_cookies_from_env(cookie_path)
|
| 115 |
|
| 116 |
-
# Change to download directory
|
| 117 |
original_dir = os.getcwd()
|
| 118 |
-
os.chdir(
|
| 119 |
|
| 120 |
# Log the command being executed
|
| 121 |
cmd = ["gamdl", request.url]
|
|
@@ -137,16 +141,27 @@ async def download_file(request: DownloadRequest):
|
|
| 137 |
|
| 138 |
# Get the downloaded filename
|
| 139 |
files = [f for f in os.listdir() if f != "cookies.txt"]
|
| 140 |
-
logger.info(f"Files in download directory: {files}")
|
| 141 |
|
| 142 |
if not files:
|
| 143 |
-
raise Exception("No files found in download directory after download attempt")
|
| 144 |
|
| 145 |
downloaded_file = files[0]
|
| 146 |
logger.info(f"Downloaded file: {downloaded_file}")
|
| 147 |
|
| 148 |
# Get the file size
|
| 149 |
file_size = os.path.getsize(downloaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
# Generate the download URL and URL encode the filename
|
| 152 |
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
|
@@ -155,7 +170,7 @@ async def download_file(request: DownloadRequest):
|
|
| 155 |
logger.info(f"Generated download URL: {download_url}")
|
| 156 |
|
| 157 |
# Move back to original directory
|
| 158 |
-
|
| 159 |
|
| 160 |
return DownloadResponse(
|
| 161 |
success=True,
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
+
|
| 71 |
+
|
| 72 |
# Create downloads directory if it doesn't exist
|
| 73 |
DOWNLOADS_DIR = "downloads"
|
| 74 |
+
TEMP_DIR = "temp"
|
| 75 |
os.makedirs(DOWNLOADS_DIR, exist_ok=True)
|
| 76 |
+
os.makedirs(TEMP_DIR, exist_ok=True)
|
| 77 |
|
| 78 |
# Mount the downloads directory with cache control headers
|
| 79 |
class CacheControlStaticFiles(StaticFiles):
|
|
|
|
| 103 |
@app.post("/download", response_model=DownloadResponse)
|
| 104 |
async def download_file(request: DownloadRequest):
|
| 105 |
try:
|
| 106 |
+
# Create a unique subdirectory for this download in the temporary directory
|
| 107 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 108 |
+
temp_subdir = os.path.join(TEMP_DIR, timestamp)
|
| 109 |
+
os.makedirs(temp_subdir, exist_ok=True)
|
| 110 |
|
| 111 |
# Log the current working directory and download directory
|
| 112 |
logger.info(f"Current working directory: {os.getcwd()}")
|
| 113 |
+
logger.info(f"Temporary download directory: {temp_subdir}")
|
| 114 |
|
| 115 |
# Create cookies file from environment variable
|
| 116 |
+
cookie_path = os.path.join(temp_subdir, "cookies.txt")
|
| 117 |
logger.info(f"Creating cookies file at: {cookie_path}")
|
| 118 |
env_to_cookies_from_env(cookie_path)
|
| 119 |
|
| 120 |
+
# Change to temporary download directory
|
| 121 |
original_dir = os.getcwd()
|
| 122 |
+
os.chdir(temp_subdir)
|
| 123 |
|
| 124 |
# Log the command being executed
|
| 125 |
cmd = ["gamdl", request.url]
|
|
|
|
| 141 |
|
| 142 |
# Get the downloaded filename
|
| 143 |
files = [f for f in os.listdir() if f != "cookies.txt"]
|
| 144 |
+
logger.info(f"Files in temporary download directory: {files}")
|
| 145 |
|
| 146 |
if not files:
|
| 147 |
+
raise Exception("No files found in temporary download directory after download attempt")
|
| 148 |
|
| 149 |
downloaded_file = files[0]
|
| 150 |
logger.info(f"Downloaded file: {downloaded_file}")
|
| 151 |
|
| 152 |
# Get the file size
|
| 153 |
file_size = os.path.getsize(downloaded_file)
|
| 154 |
+
|
| 155 |
+
# Check for encryption or corruption issues
|
| 156 |
+
if process.stderr:
|
| 157 |
+
raise Exception(f"Error in downloaded file: {process.stderr}")
|
| 158 |
+
|
| 159 |
+
# Move the completed download to the final directory
|
| 160 |
+
final_download_subdir = os.path.join(DOWNLOADS_DIR, timestamp)
|
| 161 |
+
os.makedirs(final_download_subdir, exist_ok=True)
|
| 162 |
+
shutil.move(downloaded_file, final_download_subdir)
|
| 163 |
+
downloaded_file_path = os.path.join(final_download_subdir, downloaded_file)
|
| 164 |
+
logger.info(f"Moved downloaded file to: {downloaded_file_path}")
|
| 165 |
|
| 166 |
# Generate the download URL and URL encode the filename
|
| 167 |
space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
|
|
|
|
| 170 |
logger.info(f"Generated download URL: {download_url}")
|
| 171 |
|
| 172 |
# Move back to original directory
|
| 173 |
+
os.chdir(original_dir)
|
| 174 |
|
| 175 |
return DownloadResponse(
|
| 176 |
success=True,
|