diff --git a/utils/get_manifest_conn.py b/utils/get_manifest_conn.py index e348345..10c5fe9 100644 --- a/utils/get_manifest_conn.py +++ b/utils/get_manifest_conn.py @@ -3,7 +3,6 @@ import logging import os import json -from swiftclient import ClientException import pickle from dotenv import load_dotenv from fastapi.responses import JSONResponse @@ -11,8 +10,6 @@ # Load .env file load_dotenv() container_name = os.getenv("CONTAINER_NAME") -# Connect to Swift -conn = get_swift_connection() #config logger logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()]) @@ -21,18 +18,15 @@ async def get_manifest_conn(slug:str,request: Request): """ - #Retrieve file content from the specified Swift container by filename, and format JSON data. - - - # Access swift_session, swift_token, and swift_storage_url,redis from app state + Retrieve file content from the specified Swift container by filename, and format JSON data. + """ + #Access swift_session, swift_token, and swift_storage_url,redis from app state swift_session = request.app.state.swift_session swift_token = request.app.state.swift_token swift_storage_url = request.app.state.swift_storage_url if not swift_token or not swift_storage_url: raise HTTPException(status_code=401, detail="Authentication is not complete or failed.") - """ - try: #Access Swift and Redis objects from the app's state redis = request.app.state.redis @@ -41,12 +35,6 @@ async def get_manifest_conn(slug:str,request: Request): #Check Redis cache if (cached_profile := await redis.get(f"manifest_{slug}")) is not None: return pickle.loads(cached_profile) - - #Fetch from Swift storage - _,manifest = conn.get_object(container_name, manifest_name) - manifest_data = json.loads(manifest) - - """ file_url = f"{swift_storage_url}/{container_name}/{manifest_name}" headers = { "X-Auth-Token": swift_token @@ -62,7 +50,6 @@ async def get_manifest_conn(slug:str,request: Request): # If not valid JSON, raise an error logger.error("Retrieved content is not valid JSON format.") raise HTTPException(status_code=400, detail="The file content is not valid JSON format.") - """ # Cache the manifest in Redis logger.info(f"Caching manifest_{slug} in Redis.") await redis.set(f"manifest_{slug}", pickle.dumps(manifest_data)) diff --git a/utils/upload_manifest.py b/utils/upload_manifest.py index 4171bd2..256d61f 100644 --- a/utils/upload_manifest.py +++ b/utils/upload_manifest.py @@ -1,25 +1,19 @@ import os from dotenv import load_dotenv from sqlalchemy.ext.asyncio import AsyncSession -from fastapi import Request, UploadFile, HTTPException,File +from fastapi import Request, UploadFile, HTTPException from utils.validator import Validator import json from utils import back_task -from swiftclient.exceptions import ClientException from redis.asyncio import Redis from contextlib import asynccontextmanager import logging -from swift_config.swift_config import get_swift_connection -import io -from urllib.parse import urlparse,urlunparse +from urllib.parse import urlparse import botocore # Load .env file load_dotenv() container_name = os.getenv("CONTAINER_NAME") -# Connect to Swift -conn = get_swift_connection() - #config logger logging.basicConfig(level=logging.INFO,handlers=[logging.StreamHandler()]) logger = logging.getLogger(__name__) @@ -117,7 +111,7 @@ async def upload_manifest_backend( for canvas_item in canvas_content: canvas_id = "/".join(canvas_item['id'].split("/")[-2:]) - canvas_name = f'{slug}/{canvas_id}/canvas.json' + #canvas_name = f'{slug}/{canvas_id}/canvas.json' canvas_parsed_url = urlparse(canvas_item['id']) target_parsed_url = urlparse(canvas_item['items'][0]['items'][0]['target']) annotation_page_parsed_url = urlparse(canvas_item['items'][0]['id']) @@ -131,32 +125,10 @@ async def upload_manifest_backend( canvas_item['items'][0]['id'] = annotation_page_id canvas_item['items'][0]['items'][0]['id'] = annotation_id new_manifest_items.append(canvas_item) - updated_canvas_content = json.dumps(canvas_item) - """ - #upload canvas to swift - conn.put_object( - container_name, - canvas_name, - contents=updated_canvas_content, - content_type='application/json' - ) - - """ + #updated_canvas_content = json.dumps(canvas_item) # Upload manifest to Swift manifest['items'] = new_manifest_items updated_manifest = json.dumps(manifest) - - """ - conn.put_object( - container_name, - manifest_name, - contents=updated_manifest, - content_type='application/json' - ) - - - - """ upload_url = f"{swift_storage_url}/{container_name}/{manifest_name}" headers = { "X-Auth-Token": swift_token,