"Unity Rising" track-winning project @ Unihack 5 (2023). A community-based accessibility mapping platform for Timisoara, for rating & flagging places based on their accessibility.
0
stars
0
forks
source link
Add image functionality to user profile pictures and uploaded pins #4
[x] The feature of a user to upload and edit his profile picture
[x] The feature of a pin to have an uploaded picture
How?
In POST /users and POST /pin you'll introduce an optional image parameter. This will be of type FileResponse. You'll do something like:
from pathlib import Path
# Define the directory path for profile pictures
profile_pics_directory = Path("../resources/profilePics")
# Ensure the directory exists, create it if not
profile_pics_directory.mkdir(parents=True, exist_ok=True)
@app.post("/create_user/")
async def create_user(username: str, profile_pic: UploadFile = File(...)):
# Save the image to the specified directory
profile_pic_path = profile_pics_directory / f"{username}.png"
with open(profile_pic_path, "wb") as image:
image.write(profile_pic.file.read())
# Your logic to save user data and profile pic path to the database
return {"username": username, "profile_pic_path": str(profile_pic_path)}
after uploading, you should save the path in the users table (imageUrl parameter). Be sure to check that the image was uploaded successfully.
For PATCH /users, you'll do the same with an image parameter, and change the imageUrl after it's uploaded.
This issue tracks:
How? In
POST /users
andPOST /pin
you'll introduce an optionalimage
parameter. This will be of type FileResponse. You'll do something like:after uploading, you should save the path in the users table (
imageUrl
parameter). Be sure to check that the image was uploaded successfully.For
PATCH /users
, you'll do the same with an image parameter, and change theimageUrl
after it's uploaded.Prerequisites
You'll have to mount a folder as a static file server for fastapi, in
app.py
: https://fastapi.tiangolo.com/reference/staticfiles/?h=staticfileWarning: use a library like
pathlib
for paths so we don't have nasty path errors on different operating systems.Bonus Have some sort of library that would check whether or not the user uploads a .jpeg or .png. Or you can just check for the extension