CREATE: static file + upload image

This commit is contained in:
2022-08-10 13:50:11 +07:00
parent a9b02389bd
commit 8dd498fe8d

View File

@@ -437,33 +437,33 @@ async def score_all_post(
return posts return posts
@post.post("/uploadfiles/") # @post.post("/uploadfiles/")
async def create_upload_files( # async def create_upload_files(
files: List[UploadFile] = File(...), # files: List[UploadFile] = File(...),
# current_user: UserModel = Depends(get_current_user) # # current_user: UserModel = Depends(get_current_user)
): # ):
try: # try:
file_name = [] # file_name = []
i = 0 # i = 0
file_location = f"../media/" # file_location = f"../media/"
for file in files: # for file in files:
current_time = datetime.datetime.now(tz=tz) # current_time = datetime.datetime.now(tz=tz)
file_save = file_location + current_time + str(i) + file.filename # file_save = file_location + current_time + str(i) + file.filename
file_name.append(current_time + str(i) + file.filename) # file_name.append(current_time + str(i) + file.filename)
i = i + 1 # i = i + 1
with open(file_save, "wb+") as file_object: # with open(file_save, "wb+") as file_object:
file_object.write(file.file.read()) # file_object.write(file.file.read())
return {"filenames": file_name} # return {"filenames": file_name}
except Exception as e: # except Exception as e:
return JSONResponse( # return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST, # status_code=status.HTTP_400_BAD_REQUEST,
content={'message': str(e)} # content={'message': str(e)}
) # )
@post.post("/upload_post/") @post.post("/upload_file/")
async def create_upload_post( async def create_upload_post(
post: List[UploadFile] = File(...), # post: List[UploadFile] = File(...),
image: List[UploadFile] = File(...), image: List[UploadFile] = File(...),
# current_user: UserModel = Depends(get_current_user) # current_user: UserModel = Depends(get_current_user)
): ):
@@ -471,22 +471,22 @@ async def create_upload_post(
file_name = [] file_name = []
i = 0 i = 0
now = datetime.datetime.now(tz=tz) # now = datetime.datetime.now(tz=tz)
current_time = now.strftime("%H_%M_%S_%d-%m-%Y_") # current_time = now.strftime("%H_%M_%S_%d-%m-%Y_")
folder_save = f"./post/" + current_time + str(i) folder_save = f"./post/"
Path(folder_save).mkdir(parents=True) # Path(folder_save).mkdir(parents=True)
for file in post: # for file in post:
file_save = folder_save + "/" + file.filename # file_save = folder_save + "/" + file.filename
file_name.append(current_time + str(i) + "/" + file.filename) # file_name.append(current_time + str(i) + "/" + file.filename)
i = i + 1 # i = i + 1
with open(file_save, "wb+") as file_object: # with open(file_save, "wb+") as file_object:
file_object.write(file.file.read()) # file_object.write(file.file.read())
folder_save = folder_save + "/images" folder_save = folder_save + "/images"
Path(folder_save).mkdir(parents=True, exist_ok=True) Path(folder_save).mkdir(parents=True, exist_ok=True)
for file in image: for file in image:
file_save = folder_save + "/" + file.filename file_save = folder_save + "/" + file.filename
i = i + 1 file_name.append(file_save)
with open(file_save, "wb+") as file_object: with open(file_save, "wb+") as file_object:
file_object.write(file.file.read()) file_object.write(file.file.read())
return {"filenames": file_name} return {"filenames": file_name}
@@ -511,34 +511,34 @@ async def create_upload_post(
# ) # )
@post.get("/showfile/{name}") # @post.get("/showfile/{name}")
async def show_file(name: str # async def show_file(name: str
# , current_user: UserModel = Depends(get_current_user) # # , current_user: UserModel = Depends(get_current_user)
): # ):
file_path = f"../media/" + name # file_path = f"../media/" + name
if os.path.exists(file_path): # if os.path.exists(file_path):
return FileResponse(file_path) # return FileResponse(file_path)
return {"erro": "File not found!"} # return {"erro": "File not found!"}
@post.get("/showvideo/{file_name}", response_class=FileResponse) # @post.get("/showvideo/{file_name}", response_class=FileResponse)
async def main(file_name: str # async def main(file_name: str
# , current_user: UserModel = Depends(get_current_user) # # , current_user: UserModel = Depends(get_current_user)
): # ):
file_path = f"../media/" + file_name # file_path = f"../media/" + file_name
return file_path # return file_path
@post.get("/video") # @post.get("/video")
async def video_endpoint(video_name # async def video_endpoint(video_name
# , current_user: UserModel = Depends(get_current_user) # # , current_user: UserModel = Depends(get_current_user)
): # ):
def iterfile(): # def iterfile():
video_path = f"../media/" + video_name # video_path = f"../media/" + video_name
with open(video_path, mode="rb") as file_like: # with open(video_path, mode="rb") as file_like:
yield from file_like # yield from file_like
return StreamingResponse(iterfile(), media_type="video/mp4") # return StreamingResponse(iterfile(), media_type="video/mp4")
@post.post("/edit_post/{id}", response_description="score all post") @post.post("/edit_post/{id}", response_description="score all post")