tecuts commited on
Commit
058a4f4
·
verified ·
1 Parent(s): 0333263

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -69,12 +69,20 @@ app = FastAPI(
69
  DOWNLOADS_DIR = "downloads"
70
  os.makedirs(DOWNLOADS_DIR, exist_ok=True)
71
 
72
- # Mount the downloads directory
73
- app.mount("/files", StaticFiles(directory=DOWNLOADS_DIR), name="files")
74
-
75
-
76
 
 
 
 
 
 
 
 
 
 
 
 
77
 
 
78
 
79
  class DownloadRequest(BaseModel):
80
  url: str
@@ -86,7 +94,6 @@ class DownloadResponse(BaseModel):
86
  download_url: str = None
87
  file_size: int = None # Include file size in the response
88
 
89
-
90
  @app.post("/download", response_model=DownloadResponse)
91
  async def download_file(request: DownloadRequest):
92
  try:
@@ -140,7 +147,7 @@ async def download_file(request: DownloadRequest):
140
  file_size = os.path.getsize(downloaded_file)
141
 
142
  # Generate the download URL and URL encode the filename
143
- space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space")
144
  encoded_filename = quote(downloaded_file) # URL encode the filename
145
  download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
146
  logger.info(f"Generated download URL: {download_url}")
 
69
  DOWNLOADS_DIR = "downloads"
70
  os.makedirs(DOWNLOADS_DIR, exist_ok=True)
71
 
 
 
 
 
72
 
73
+ # Mount the downloads directory with cache control headers
74
+ class CacheControlStaticFiles(StaticFiles):
75
+ def __init__(self, directory: str, name: str = None):
76
+ super().__init__(directory=directory, name=name)
77
+
78
+ async def get_response(self, path: str, scope):
79
+ response = await super().get_response(path, scope)
80
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
81
+ response.headers["Pragma"] = "no-cache"
82
+ response.headers["Expires"] = "0"
83
+ return response
84
 
85
+ app.mount("/files", CacheControlStaticFiles(directory=DOWNLOADS_DIR), name="files")
86
 
87
  class DownloadRequest(BaseModel):
88
  url: str
 
94
  download_url: str = None
95
  file_size: int = None # Include file size in the response
96
 
 
97
  @app.post("/download", response_model=DownloadResponse)
98
  async def download_file(request: DownloadRequest):
99
  try:
 
147
  file_size = os.path.getsize(downloaded_file)
148
 
149
  # Generate the download URL and URL encode the filename
150
+ space_url = os.getenv("SPACE_URL", "https://tecuts-vob.hf.space/")
151
  encoded_filename = quote(downloaded_file) # URL encode the filename
152
  download_url = f"{space_url}/files/{timestamp}/{encoded_filename}"
153
  logger.info(f"Generated download URL: {download_url}")