Skip to content

Commit

Permalink
adding api to save file in my laptop
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard0803 committed Jun 5, 2024
1 parent e24d1e0 commit d55fcbd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Binary file added api_save_file/files/77.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api_save_file/files/biscoito-passatempo.webp
Binary file not shown.
7 changes: 7 additions & 0 deletions api_save_file/nginx_server_config
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;
}
}
57 changes: 57 additions & 0 deletions api_save_file/server.py
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
)

0 comments on commit d55fcbd

Please sign in to comment.