update
swadhinbiswas committed 2 years ago
3ebef22
1 parent f4b98dd
commit 3ebef22
16 files changed
+200 -8
Showing 16 changed files+200-8
backend/functions/__init__.pybackend/functions/imagesizeing.pybackend/model/__init__.pybackend/model/usemodel.pybackend/requirements.txtbackend/schemas/usershema.pybackend/services/filehandelar.pybackend/services/getpicture.pybackend/services/imageservice.pybackend/services/test.pybackend/setup.pybackend/uploads/Screenshot from 2024-03-21 20-46-43.pngbackend/uploads/icons8-adobe-premiere-pro-96.pngbackend/uploads/icons8-cd-collection-96.pngtest.pyuploads/Goodies - Error.png
▼backend/functions/__init__.py
+0-0
0 | 0 |
▼backend/functions/imagesizeing.py
+72-0
| @@ -0,0 +1,72 @@ | ||
1 | +from PIL import Image | |
2 | + | |
3 | +class Getpicture: | |
4 | + def crop(image:str, x:int, y:int, width:int, height:int): | |
5 | + img = Image.open(image) | |
6 | + img = img.crop((x, y, x+width, y+height)) | |
7 | + img.save(image) | |
8 | + return image | |
9 | + def resize(image:str, width:int, height:int): | |
10 | + img = Image.open(image) | |
11 | + img = img.resize((width, height)) | |
12 | + img.save(image) | |
13 | + return image | |
14 | + def rotate(image:str, degree:int): | |
15 | + img = Image.open(image) | |
16 | + img = img.rotate(degree) | |
17 | + img.save(image) | |
18 | + return image | |
19 | + def flip(image:str, direction:str): | |
20 | + img = Image.open(image) | |
21 | + if direction == "horizontal": | |
22 | + img = img.transpose(Image.FLIP_LEFT_RIGHT) | |
23 | + elif direction == "vertical": | |
24 | + img = img.transpose(Image.FLIP_TOP_BOTTOM) | |
25 | + img.save(image) | |
26 | + return image | |
27 | + def convert(image:str, format:str): | |
28 | + img = Image.open(image) | |
29 | + img.save(image, format) | |
30 | + return image | |
31 | + def getinfo(image:str): | |
32 | + img = Image.open(image) | |
33 | + return img.info | |
34 | + def getformat(image:str): | |
35 | + img = Image.open(image) | |
36 | + return img.format | |
37 | + def getmode(image:str): | |
38 | + img = Image.open(image) | |
39 | + return img.mode | |
40 | + def getsize(image:str): | |
41 | + img = Image.open(image) | |
42 | + return img.size | |
43 | + def getdata(image:str): | |
44 | + img = Image.open(image) | |
45 | + return img.getdata() | |
46 | + def getcolors(image:str): | |
47 | + img = Image.open(image) | |
48 | + return img.getcolors() | |
49 | + def getextrema(image:str): | |
50 | + img = Image.open(image) | |
51 | + return img.getextrema() | |
52 | + def getbbox(image:str): | |
53 | + img = Image.open(image) | |
54 | + return img.getbbox() | |
55 | + def getbands(image:str): | |
56 | + img = Image.open(image) | |
57 | + return img.getbands() | |
58 | + def getim(image:str): | |
59 | + img = Image.open(image) | |
60 | + return img.getim() | |
61 | + def getpalette(image:str): | |
62 | + img = Image.open(image) | |
63 | + return img.getpalette() | |
64 | + def getpixel(image:str, x:int, y:int): | |
65 | + img = Image.open(image) | |
66 | + return img.getpixel((x, y)) | |
67 | + def getprojection(image:str): | |
68 | + img = Image.open(image) | |
69 | + return img.getprojection() | |
70 | + def getcolors(image:str): | |
71 | + img = Image.open(image) | |
72 | + return img.getcolors() | |
0 | 73 | |
▼backend/model/__init__.py
+1-0
| @@ -0,0 +1 @@ | ||
1 | + | |
0 | 2 | |
▼backend/model/usemodel.py
+4-2
| @@ -3,7 +3,7 @@ from typing import Coroutine, List,Any,Final, Optional | ||
3 | 3 | from uuid import UUID,uuid4 |
4 | 4 | from beanie import Document,Indexed |
5 | 5 | from pymongo.client_session import ClientSession |
6 | -from zmq import has | |
6 | +from pydantic import EmailStr | |
7 | 7 | |
8 | 8 | class UseModel(Document): |
9 | 9 | |
| @@ -11,7 +11,7 @@ class UseModel(Document): | ||
11 | 11 | username: Indexed(str,unique=True) # type: ignore |
12 | 12 | frist_name: Optional[str]=None |
13 | 13 | last_name: Optional[str]=None |
14 | - email:Indexed(str,unique=True) # type: ignore | |
14 | + email:Indexed(EmailStr,unique=True) # type: ignore | |
15 | 15 | hashed_password: str |
16 | 16 | userpicture: Optional[str]=None |
17 | 17 | gallary:Optional[List[str]]=None |
| @@ -23,6 +23,8 @@ class UseModel(Document): | ||
23 | 23 | |
24 | 24 | def __str__(self) -> str: |
25 | 25 | return self.__repr__() |
26 | + def __hash__(self) -> EmailStr: | |
27 | + return hash(self.email) | |
26 | 28 | |
27 | 29 | def __eq__(self, o: object) -> bool: |
28 | 30 | if not isinstance(o,UseModel): |
29 | 31 | |
▼backend/requirements.txt
+2-1
| @@ -18,4 +18,5 @@ sniffio | ||
18 | 18 | starlette |
19 | 19 | typing_extensions |
20 | 20 | uvicorn |
21 | -setuptools | |
21 | +setuptools | |
22 | +python-multipart | |
22 | 23 | |
▼backend/schemas/usershema.py
+2-2
| @@ -4,9 +4,9 @@ from uuid import UUID, uuid4 | ||
4 | 4 | from typing import Optional,List |
5 | 5 | |
6 | 6 | class UseBase(BaseModel): |
7 | - username:str=Field(...,description="Name of the user") | |
7 | + username:str=Field(...,description="Name of the user",min_length=5,max_length=50) | |
8 | 8 | email:Optional[str]=Field(...,description="Email of the user") |
9 | - password:Optional[str]=Field(...,description="Password of the user") | |
9 | + password:str=Field(...,description="Password of the user",min_length=8,max_length=50) | |
10 | 10 | userpicture:Optional[str]=Field(...,description="User picture") |
11 | 11 | gallary:Optional[List[str]]=Field([],description="List of image ids") |
12 | 12 | tags:Optional[List[str]]=Field([],description="List of tags") |
13 | 13 | |
▼backend/services/filehandelar.py
+63-0
| @@ -0,0 +1,63 @@ | ||
1 | +from fastapi import UploadFile,File | |
2 | +from fastapi.responses import FileResponse | |
3 | +from fastapi import APIRouter,FastAPI | |
4 | +from pathlib import Path | |
5 | +from schemas.imageschema import ImageOut | |
6 | +from services import imageservice | |
7 | + | |
8 | +appex=FastAPI() | |
9 | + | |
10 | + | |
11 | + | |
12 | +upload_folder = Path("backend/uploads") | |
13 | +""" | |
14 | +Routes: | |
15 | + - /uploadfile/ [POST] | |
16 | + - /downloadfile/{filename} [GET] | |
17 | + - /listfiles/ [GET] | |
18 | + - /deletefile/{filename} [DELETE] | |
19 | + | |
20 | +""" | |
21 | + | |
22 | + | |
23 | +@appex.post("/uploadfile/",summary="Image Upload") | |
24 | +async def create_upload_file(file: UploadFile = File(...)): | |
25 | + contents = await file.read() | |
26 | + with open(upload_folder / file.filename, "wb") as f: | |
27 | + f.write(contents) | |
28 | + name=file.filename | |
29 | + | |
30 | + return {"filename": name} | |
31 | + | |
32 | +@appex.get("/downloadfile/{filename}") | |
33 | +async def download_file(filename: str): | |
34 | + return FileResponse(upload_folder / filename) | |
35 | + | |
36 | +@appex.get("/listfiles/") | |
37 | +async def list_files(): | |
38 | + return {"files": [f.name for f in upload_folder.iterdir()]} | |
39 | + | |
40 | +@appex.delete("/deletefile/{filename}") | |
41 | +async def delete_file(filename: str): | |
42 | + (upload_folder / filename).unlink() | |
43 | + return {"filename": filename} | |
44 | + | |
45 | + | |
46 | +@appex.post("/createimage/",response_model=ImageOut) | |
47 | +async def create_image(image:ImageOut): | |
48 | + return await imageservice.ImageService.create_image(image) | |
49 | + | |
50 | +@appex.get("/getimage/{imagename}",response_model=ImageOut) | |
51 | +async def get_image(imagename:str): | |
52 | + return await imageservice.ImageService.get_by_imagename(imagename) | |
53 | + | |
54 | +@appex.put("/updateimage/{imagename}",response_model=ImageOut) | |
55 | +async def update_image(imagename:str,image:ImageOut): | |
56 | + return await imageservice.ImageService.update_image(imagename,image) | |
57 | +@appex.delete("/deleteimage/{imagename}") | |
58 | +async def delete_image(imagename:str): | |
59 | + return await imageservice.ImageService.delete_image(imagename) | |
60 | + | |
61 | +if __name__ == "__main__": | |
62 | + import uvicorn | |
63 | + uvicorn.run(appex, host="0.0.0.0", port=8000) | |
0 | 64 | |
▼backend/services/getpicture.py
+0-0
0 | 0 |
▼backend/services/imageservice.py
+26-3
| @@ -26,6 +26,29 @@ class ImageService: | ||
26 | 26 | raise pymongo.errors.OperationFailure("Image not found") |
27 | 27 | await image.update({"$set":image.dict(exclude_unset=True)}) |
28 | 28 | return image |
29 | - | |
30 | - | |
31 | - | |
29 | + @staticmethod | |
30 | + def delete_image(imagename:str)->Optional[ImageModel]: | |
31 | + image= ImageModel.find_one(ImageModel.imagename==imagename) | |
32 | + if image is None: | |
33 | + raise pymongo.errors.OperationFailure("Image not found") | |
34 | + image.delete() | |
35 | + @staticmethod | |
36 | + async def get_all_images()->List[ImageModel]: | |
37 | + images=await ImageModel.find().to_list() | |
38 | + return images | |
39 | + @staticmethod | |
40 | + async def get_images_by_tag(tag:str)->List[ImageModel]: | |
41 | + images=await ImageModel.find(ImageModel.tags==tag).to_list() | |
42 | + return images | |
43 | + @staticmethod | |
44 | + async def get_images_by_tags(tags:List[str])->List[ImageModel]: | |
45 | + images=await ImageModel.find(ImageModel.tags.all(tags)).to_list() | |
46 | + return images | |
47 | + @staticmethod | |
48 | + async def get_many(limit:int,skip:int)->List[ImageModel]: | |
49 | + images=await ImageModel.find().skip(skip).limit(limit).to_list() | |
50 | + return images | |
51 | + @staticmethod | |
52 | + async def get_by_search(search:str)->List[ImageModel]: | |
53 | + images=await ImageModel.find({"$text":{"$search":search}}).to_list() | |
54 | + return images | |
32 | 55 | |
▼backend/services/test.py
+0-0
0 | 0 |
▼backend/setup.py
+3-0
| @@ -0,0 +1,3 @@ | ||
1 | +from setuptools import setup,find_packages | |
2 | + | |
3 | +setup(name='backend',version='0.1',packages=find_packages()) | |
0 | 4 | |
▼backend/uploads/Screenshot from 2024-03-21 20-46-43.png
+0-0
Binary file not shown.
▼backend/uploads/icons8-adobe-premiere-pro-96.png
+0-0
Binary file not shown.
▼backend/uploads/icons8-cd-collection-96.png
+0-0
Binary file not shown.
▼test.py
+27-0
| @@ -0,0 +1,27 @@ | ||
1 | +from fastapi import UploadFile,File | |
2 | +from fastapi.responses import FileResponse | |
3 | +from fastapi import FastAPI | |
4 | + | |
5 | + | |
6 | + | |
7 | +appex=FastAPI() | |
8 | + | |
9 | +@appex.get("/") | |
10 | +async def read_root(): | |
11 | + return {"Hello": "World"} | |
12 | + | |
13 | +@appex.post("/uploadfile/") | |
14 | +async def create_upload_file(file: UploadFile = File(...)): | |
15 | + return {"filename": file.filename} | |
16 | + | |
17 | +@appex.get("/downloadfile/") | |
18 | +async def download_file(): | |
19 | + file_path = "file.txt" | |
20 | + return FileResponse(file_path, media_type="application/octet-stream",filename="file.txt") | |
21 | + | |
22 | + | |
23 | + | |
24 | + | |
25 | +if __name__ == "__main__": | |
26 | + import uvicorn | |
27 | + uvicorn.run(appex, host="0.0.0.0", port=8000) | |
0 | 28 | |
▼uploads/Goodies - Error.png
+0-0
Binary file not shown.