|
""" |
|
Copyright 2024-2025 Infosys Ltd.” |
|
|
|
Use of this source code is governed by MIT license that can be found in the LICENSE file or at |
|
MIT license https://opensource.org/licenses/MIT |
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
|
""" |
|
|
|
from io import BytesIO |
|
from fairness.config.logger import CustomLogger |
|
from fairness.dao.WorkBench.databaseconnection import DataBase_WB |
|
from fairness.exception.custom_exception import CustomHTTPException |
|
from fastapi import HTTPException |
|
from gridfs.errors import NoFile, FileExists |
|
from gridfs import GridFS |
|
import shutil |
|
import os |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
from fairness.config.logger import CustomLogger |
|
from fairness.dao.databaseconnection import DataBase |
|
from fairness.Telemetry.Telemetry_call import SERVICE_UPLOAD_FILE_METADATA |
|
import datetime |
|
import time |
|
import requests |
|
|
|
log = CustomLogger() |
|
|
|
|
|
|
|
class FileStoreReportDb: |
|
|
|
|
|
|
|
|
|
def __init__(self,db=None) -> None: |
|
if db is not None: |
|
log.info("inside filestore") |
|
self.fs = GridFS(db) |
|
self.db_type = os.getenv('DB_TYPE').lower() |
|
else: |
|
ModelWorkBench = DataBase_WB() |
|
self.fs = GridFS(ModelWorkBench.db) |
|
self.db_type = os.getenv('DB_TYPE').lower() |
|
|
|
|
|
def save_file(self,file, filename,contentType,tenet,container_name=None): |
|
if self.db_type == 'mongo': |
|
|
|
if file is None: |
|
raise HTTPException(status_code=500, detail="File content cannot be None") |
|
|
|
|
|
if filename is None or contentType is None or tenet is None: |
|
raise ValueError("Filename, contentType, and tenet cannot be None") |
|
|
|
localTime=time.time() |
|
time.sleep(1/1000) |
|
with self.fs.new_file(_id=str(localTime), |
|
filename=filename, |
|
contentType=contentType, |
|
tenet = tenet, |
|
) as f: |
|
f.write(file) |
|
|
|
return f._id |
|
else: |
|
if file is None: |
|
raise HTTPException(status_code=500, detail="File content cannot be None") |
|
|
|
|
|
if filename is None or contentType is None or tenet is None: |
|
raise ValueError("Filename, contentType, and tenet cannot be None") |
|
|
|
upload_file_api = os.getenv('AZURE_UPLOAD_API') |
|
|
|
log.info(f"container_name:{container_name}") |
|
|
|
response =requests.post(url =upload_file_api, files ={"file":(filename, file)}, data ={"container_name":container_name}).json() |
|
if response is None: |
|
raise HTTPException(f"An error occurred: file not found") |
|
blob_name =response["blob_name"] |
|
log.info(f"{blob_name} blob name") |
|
return blob_name |
|
|
|
|
|
def save_filecreate(self,file): |
|
|
|
|
|
if file is None: |
|
raise ValueError("File content cannot be None") |
|
|
|
localTime=time.time() |
|
with self.fs.new_file(_id=localTime, |
|
filename=file.filename, |
|
contentType=file.content_type, |
|
tenet = "fairness", |
|
) as f: |
|
file.file.seek(0) |
|
shutil.copyfileobj(file.file, f) |
|
return f._id |
|
|
|
|
|
def read_file(self,unique_id: str,container_name=None): |
|
if self.db_type == 'mongo': |
|
|
|
|
|
|
|
log.info(unique_id) |
|
|
|
file_metadata = self.fs.find_one({"_id": unique_id}) |
|
|
|
if file_metadata is None: |
|
log.info("inside file not found") |
|
raise HTTPException(status_code=500, detail=f"No file found with unique ID {unique_id}") |
|
|
|
file_content = self.fs.get(file_metadata._id).read() |
|
|
|
return {"data": file_content, "name":file_metadata.filename, "extension": file_metadata.filename.split('.')[-1],"contentType":file_metadata.content_type} |
|
else: |
|
download_file_api = os.getenv('AZURE_GET_API') |
|
if container_name is None: |
|
container_name = os.getenv('Dt_containerName') |
|
|
|
|
|
if not download_file_api: |
|
raise HTTPException(status_code=500, detail=f"Environment variable AZURE_READ_API is not set") |
|
|
|
|
|
if not container_name or not unique_id: |
|
raise ValueError("container_name and unique_id must not be empty") |
|
|
|
response = requests.get(url=download_file_api, params={"container_name": container_name, "blob_name": unique_id}) |
|
if response is None: |
|
raise HTTPException("occurred: file not found") |
|
|
|
if response.status_code != 200: |
|
raise Exception(f"Request to {download_file_api} failed with status code {response.status_code}") |
|
|
|
return {'data': response.content, 'type': unique_id.split('.')[-1], 'name': unique_id.split('/')[-1],'contentType':response.headers['Content-Type'], 'extension': unique_id.split('.')[-1]} |
|
|
|
def read_chunked_file(self,unique_id: str,container_name=None): |
|
if self.db_type == 'mongo': |
|
|
|
file_metadata = self.fs.find_one({"_id": unique_id}) |
|
|
|
if file_metadata is None: |
|
raise FileNotFoundError(f"No file found with unique ID {unique_id}") |
|
|
|
|
|
file_content = self.fs.get(file_metadata._id).read() |
|
|
|
return {"data": file_content, "name":file_metadata.filename, "extension": file_metadata.filename.split('.')[-1],"contentType":file_metadata.content_type} |
|
else: |
|
download_file_api = os.getenv('AZURE_GET_API') |
|
container_name = os.getenv('Model_CONTAINER_NAME') |
|
|
|
|
|
if not download_file_api: |
|
raise ValueError("Environment variable AZURE_READ_API is not set") |
|
|
|
|
|
if not container_name or not unique_id: |
|
raise ValueError("container_name and unique_id must not be empty") |
|
|
|
file_bytes=BytesIO() |
|
content_type="" |
|
with requests.get(url=download_file_api, params={"container_name": container_name, "blob_name": unique_id}, stream=True) as r: |
|
r.raise_for_status() |
|
content_type=r.headers['Content-Type'] |
|
for chunk in r.iter_content(chunk_size=8192): |
|
if chunk: |
|
file_bytes.write(chunk) |
|
file_bytes.seek(0) |
|
|
|
|
|
|
|
return {'data': file_bytes.getvalue(), 'type': unique_id.split('.')[-1], 'name': unique_id.split('/')[-1],'contentType':content_type, 'extension': unique_id.split('.')[-1]} |
|
|
|
|
|
|
|
def read_modelfile(self,unique_id: str,container_name=None): |
|
if self.db_type == 'mongo': |
|
|
|
|
|
log.info(f"{unique_id}") |
|
|
|
file_metadata = self.fs.find_one({"_id": unique_id}) |
|
|
|
if file_metadata is None: |
|
raise FileNotFoundError(f"No file found with unique ID {unique_id}") |
|
|
|
|
|
file_content = self.fs.get(file_metadata._id).read() |
|
|
|
return {"data": file_content, "name":file_metadata.filename, "extension": file_metadata.filename.split('.')[-1],"contentType":file_metadata.content_type} |
|
else: |
|
download_file_api = os.getenv('AZURE_GET_API') |
|
container_name = os.getenv('Model_CONTAINER_NAME') |
|
|
|
|
|
if not download_file_api: |
|
raise ValueError("Environment variable AZURE_READ_API is not set") |
|
|
|
|
|
if not container_name or not unique_id: |
|
raise ValueError("container_name and unique_id must not be empty") |
|
response = requests.get(url=download_file_api, params={"container_name": container_name, "blob_name": unique_id}) |
|
|
|
if response.status_code != 200: |
|
raise Exception(f"Request to {download_file_api} failed with status code {response.status_code}") |
|
|
|
return {'data': response.content, 'type': unique_id.split('.')[-1], 'name': unique_id.split('/')[-1],'contentType':response.headers['Content-Type'], 'extension': unique_id.split('.')[-1]} |
|
|
|
|
|
def delete_file(self,unique_id: str, file_type: str): |
|
|
|
|
|
if unique_id is None or not isinstance(unique_id, str): |
|
raise ValueError("Unique ID must be a non-empty string") |
|
|
|
|
|
if file_type is None or not isinstance(file_type, str): |
|
raise ValueError("File type must be a non-empty string") |
|
|
|
try: |
|
|
|
file_metadata = self.fs.find_one({"uniqueId": unique_id, "type": file_type}) |
|
|
|
|
|
if file_metadata is None: |
|
raise FileNotFoundError(f"No file found with unique ID {unique_id} and type {file_type}") |
|
|
|
|
|
FileStoreReportDb.fs.delete(file_metadata._id) |
|
|
|
return {"message": "File deleted successfully"} |
|
except NoFile: |
|
raise FileNotFoundError(f"No file found with unique ID {unique_id} and type {file_type}") |
|
|
|
|
|
def save_local_file(self,filePath,fileType): |
|
|
|
if os.path.exists(filePath): |
|
if os.path.getsize(filePath)<=0: |
|
raise ValueError("File content cannot be None") |
|
else: |
|
raise FileNotFoundError("No file found with the name "+filePath.split("/")[-1]+" locally") |
|
with open(filePath,'rb') as f: |
|
file_id=self.fs.put(f,filename=filePath.split('/')[-1],content_type=fileType) |
|
return file_id |
|
|
|
def getfilename(self,unique_id: str): |
|
try: |
|
|
|
file_metadata = self.fs.find_one({"_id": unique_id}) |
|
log.info(f"file_metadata {file_metadata}") |
|
if file_metadata: |
|
|
|
filename = file_metadata.filename |
|
log.info(f"{filename} filename") |
|
return filename |
|
except NoFile: |
|
raise FileNotFoundError(f"No file found with unique ID {unique_id}") |