chore: refactor block files

This commit is contained in:
swve 2023-02-19 23:37:47 +01:00
parent 642e55ce2e
commit 0b2e407bf3
9 changed files with 30 additions and 87 deletions

View file

@ -16,31 +16,31 @@ class PhotoFile(BaseModel):
lecture_id: str
async def create_picture_file(request: Request,picture_file: UploadFile, lecture_id: str):
async def create_image_file(request: Request,image_file: UploadFile, lecture_id: str):
photos = request.app.db["files"]
# generate file_id
file_id = str(f"file_{uuid4()}")
# get file format
file_format = picture_file.filename.split(".")[-1]
file_format = image_file.filename.split(".")[-1]
# validate file format
if file_format not in ["jpg", "jpeg", "png", "gif"]:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Picture file format not supported")
status_code=status.HTTP_409_CONFLICT, detail="Image file format not supported")
# create file
file = await picture_file.read()
file = await image_file.read()
# get file size
file_size = len(file)
# get file type
file_type = picture_file.content_type
file_type = image_file.content_type
# get file name
file_name = picture_file.filename
file_name = image_file.filename
# create file object
uploadable_file = PhotoFile(
@ -53,11 +53,11 @@ async def create_picture_file(request: Request,picture_file: UploadFile, lecture
)
# create folder for lecture
if not os.path.exists(f"content/uploads/files/pictures/{lecture_id}"):
os.mkdir(f"content/uploads/files/pictures/{lecture_id}")
if not os.path.exists(f"content/uploads/files/images/{lecture_id}"):
os.mkdir(f"content/uploads/files/images/{lecture_id}")
# upload file to server
with open(f"content/uploads/files/pictures/{lecture_id}/{file_id}.{file_format}", 'wb') as f:
with open(f"content/uploads/files/images/{lecture_id}/{file_id}.{file_format}", 'wb') as f:
f.write(file)
f.close()
@ -71,7 +71,7 @@ async def create_picture_file(request: Request,picture_file: UploadFile, lecture
return uploadable_file
async def get_picture_object(request: Request,file_id: str):
async def get_image_object(request: Request,file_id: str):
photos = request.app.db["files"]
photo_file = photos.find_one({"file_id": file_id})
@ -85,7 +85,7 @@ async def get_picture_object(request: Request,file_id: str):
status_code=status.HTTP_409_CONFLICT, detail="Photo file does not exist")
async def get_picture_file(request: Request,file_id: str, current_user: PublicUser):
async def get_image_file(request: Request,file_id: str, current_user: PublicUser):
photos = request.app.db["files"]
photo_file = photos.find_one({"file_id": file_id})
@ -104,7 +104,7 @@ async def get_picture_file(request: Request,file_id: str, current_user: PublicUs
file_format = photo_file.file_format
lecture_id = photo_file.lecture_id
file = open(
f"content/uploads/files/pictures/{lecture_id}/{file_id}.{file_format}", 'rb')
f"content/uploads/files/images/{lecture_id}/{file_id}.{file_format}", 'rb')
return StreamingResponse(file, media_type=photo_file.file_type)
else:

View file

@ -95,7 +95,7 @@ async def get_document_file(request: Request, file_id: str, current_user: Public
if document_file:
# check media type
if document_file.format not in ["jpg", "jpeg", "png", "gif"]:
if document_file.format not in ["pdf"]:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Document file format not supported")