-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding api to save file in my laptop
- Loading branch information
1 parent
e24d1e0
commit d55fcbd
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
server { | ||
listen 80; | ||
server_name 191.176.88.10; | ||
location / { | ||
proxy_pass http://0.0.0.0:8000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
import uvicorn | ||
from fastapi import FastAPI, status, requests, UploadFile | ||
from fastapi.responses import JSONResponse | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
|
||
app = FastAPI() | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
@app.on_event("startup") | ||
def startup_event(): | ||
os.makedirs("files", exist_ok=True) | ||
|
||
@app.get("/") | ||
def root(): | ||
return JSONResponse( | ||
status_code=status.HTTP_200_OK, | ||
content={ | ||
"message": "Hello World" | ||
} | ||
) | ||
|
||
@app.post("/uploadfile/") | ||
async def save_file(file: UploadFile): | ||
file_object = file.file | ||
file_object.seek(0) | ||
|
||
content_ = await file.read() | ||
content = BytesIO(content_) | ||
|
||
with open(f"files/{file.filename}", "wb") as f: | ||
f.write(content.getvalue()) | ||
|
||
return JSONResponse( | ||
status_code=status.HTTP_200_OK, | ||
content={ | ||
"filename": f"{file.filename} saved with success" | ||
} | ||
) | ||
|
||
if __name__ == '__main__': | ||
uvicorn.run( | ||
app="server:app", | ||
host="127.0.0.1", | ||
port=8000, | ||
reload=True | ||
) |