@@ -132,23 +132,27 @@ async def download_file(token: str):
132132 },
133133)
134134async def upload_file (
135- path : str = Query (..., description = "Absolute destination path for the file." ),
135+ dir : str = Query (..., description = "Destination directory for the file." ),
136136 url : Optional [str ] = Query (None , description = "URL to download the file from. If omitted, expects a multipart file upload." ),
137137 file : Optional [UploadFile ] = File (None , description = "The file to upload (if no URL provided)." ),
138138):
139139 if url :
140140 import httpx
141+ from urllib .parse import urlparse
141142
142143 async with httpx .AsyncClient (follow_redirects = True ) as client :
143144 resp = await client .get (url )
144145 resp .raise_for_status ()
145146 content = resp .content
147+ filename = os .path .basename (urlparse (url ).path ) or "download"
146148 elif file :
147149 content = await file .read ()
150+ filename = file .filename or "upload"
148151 else :
149152 raise HTTPException (status_code = 400 , detail = "Provide either 'url' or a file upload." )
150153
151- os .makedirs (os .path .dirname (path ), exist_ok = True )
154+ os .makedirs (dir , exist_ok = True )
155+ path = os .path .join (dir , filename )
152156 with open (path , "wb" ) as f :
153157 f .write (content )
154158 return {"path" : path , "size" : len (content )}
@@ -164,14 +168,14 @@ async def upload_file(
164168 },
165169)
166170async def create_upload_link (
167- path : str = Query (..., description = "Absolute destination path for the uploaded file." ),
171+ dir : str = Query (..., description = "Destination directory for the uploaded file." ),
168172 request : Request = None ,
169173):
170174 import time
171175 import uuid
172176
173177 token = uuid .uuid4 ().hex
174- _upload_links [token ] = (path , time .time () + 300 )
178+ _upload_links [token ] = (dir , time .time () + 300 )
175179
176180 base_url = str (request .base_url ).rstrip ("/" )
177181 return {"url" : f"{ base_url } /files/upload/{ token } " }
@@ -211,11 +215,13 @@ async def upload_file_via_link(
211215 if not entry :
212216 raise HTTPException (status_code = 404 , detail = "Invalid or expired upload link" )
213217
214- path , expiry = entry
218+ dir , expiry = entry
215219 if time .time () > expiry :
216220 raise HTTPException (status_code = 404 , detail = "Upload link expired" )
217221
218- os .makedirs (os .path .dirname (path ), exist_ok = True )
222+ filename = file .filename or "upload"
223+ os .makedirs (dir , exist_ok = True )
224+ path = os .path .join (dir , filename )
219225 content = await file .read ()
220226 with open (path , "wb" ) as f :
221227 f .write (content )
0 commit comments