-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from K3A-Team/feat/sofiane/shared-storage
Feat/sofiane/shared storage
- Loading branch information
Showing
5 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import List | ||
import uuid | ||
import datetime | ||
class SharedStorage: | ||
""" | ||
Represents a shared storage in the file management system. | ||
""" | ||
def __init__( | ||
self, | ||
name: str, | ||
imagePath: str, | ||
rootFolderId: str, | ||
ownerId: str, | ||
readId : List[str], | ||
writeId : List[str], | ||
id : str = None, | ||
|
||
): | ||
self.id = id or str(uuid.uuid4()) | ||
self.name = name | ||
self.ownerId = ownerId | ||
self.imagePath = imagePath | ||
self.rootFolderId = rootFolderId | ||
self.readId = readId | ||
self.writeId = writeId | ||
|
||
def to_dict(self) -> dict: | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"imagePath": self.imagePath, | ||
"rootFolderId": self.rootFolderId, | ||
"ownerId": self.ownerId, | ||
"readId": self.readId, | ||
"writeId": self.writeId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from fastapi import APIRouter, Depends | ||
from Core.Shared.Storage import * | ||
from Core.Shared.Security import * | ||
from Core.Shared.Utils import * | ||
from Core.Shared.ErrorResponses import * | ||
from Middlewares.authProtectionMiddlewares import LoginProtected | ||
from handlers.sharedStorageHandlers import getSharedStorage , createSharedStorage , getUserSharedStoragesHandler | ||
|
||
|
||
# Create a new APIRouter instance for storage-related routes | ||
sharedStorageRouter = APIRouter() | ||
|
||
@sharedStorageRouter.get("/storage") | ||
async def getUserSharedStorage( userID: str = Depends(LoginProtected)): | ||
try: | ||
sharedContent = await getUserSharedStoragesHandler(userID) | ||
return { | ||
"success": True, | ||
"content": sharedContent | ||
} | ||
except Exception as e: | ||
return {"success": False, "message": str(e)} | ||
|
||
@sharedStorageRouter.post("/") | ||
async def createSharedStorageRoute( storageName , image: UploadFile = File(...), userId : str = Depends(LoginProtected),): | ||
try: | ||
storageId = await createSharedStorage(userId , storageName , image) | ||
return { | ||
"success": True, | ||
"storageId": storageId | ||
} | ||
except Exception as e: | ||
return {"success": False, "message": str(e)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from Core.Shared.Database import Database | ||
from Core.Shared.Storage import Storage | ||
from Models.Entities.SharedStorage import SharedStorage | ||
from Models.Entities.Folder import Folder | ||
from fastapi import UploadFile ,HTTPException | ||
from fastapi import File | ||
from Core.Shared.Utils import storeInStorageHandler | ||
|
||
from Core.Shared.Database import db | ||
|
||
|
||
def validate_image(file: UploadFile = File(...)) -> UploadFile: | ||
valid_image_mime_types = ["image/jpeg", "image/png", "image/gif" , "image/jpg"] | ||
if file.content_type not in valid_image_mime_types: | ||
raise HTTPException(status_code=400, detail="Invalid image type") | ||
return file | ||
|
||
async def createSharedStorage(userId , storageName , image: UploadFile = File(...)): | ||
""" | ||
Create a shared storage for a given user. | ||
This function creates a shared storage for a given user. | ||
Args: | ||
userId (str): The unique identifier of the user. | ||
storageName (str): The name of the storage. | ||
Returns: | ||
str: The shared storage's identifier. | ||
""" | ||
print("Creating shared storage") | ||
image = validate_image(image) | ||
|
||
# Create root folder | ||
rootFolder = Folder( | ||
name=storageName, | ||
ownerId=userId, | ||
parent=None, | ||
) | ||
|
||
#Create the image | ||
await Database.createFolder(rootFolder) | ||
print("Before storage") | ||
imageUrl , fileId = await storeInStorageHandler(image) | ||
print("Creating shared storage") | ||
storage = SharedStorage( | ||
name=storageName, | ||
imagePath=imageUrl, | ||
rootFolderId=rootFolder.id, | ||
ownerId=userId, | ||
readId=[userId], | ||
writeId=[userId] | ||
) | ||
Database.createStorage(storage) | ||
|
||
|
||
return storage.to_dict() | ||
|
||
async def getSharedStorage(storageId: str): | ||
""" | ||
Retrieve the shared storage with the given identifier. | ||
This function retrieves the shared storage with the given identifier. | ||
Args: | ||
storageId (str): The unique identifier of the shared storage. | ||
Returns: | ||
dict: The shared storage's data. | ||
""" | ||
|
||
storage = await Database.read("sharedStorage", storageId) | ||
|
||
|
||
if storage is None: | ||
raise HTTPException(status_code=404, detail="Shared storage not found") | ||
|
||
return storage | ||
|
||
async def getUserSharedStoragesHandler(userId): | ||
|
||
sharedStorages = await Database.getUserSharedStorages(userId) | ||
|
||
# Only keep imagePath , id , rootFodlerPath and name | ||
filtered_shared_storages = [ | ||
{ | ||
"imagePath": storage.get("imagePath"), | ||
"id": storage.get("id"), | ||
"rootFolderId": storage.get("rootFolderId"), | ||
"name": storage.get("name") | ||
} | ||
for storage in sharedStorages | ||
] | ||
|
||
|
||
return filtered_shared_storages |