minio file uploud
This commit is contained in:
@@ -2,6 +2,7 @@ from fastapi import APIRouter, HTTPException, File, UploadFile, Form
|
|||||||
import requests
|
import requests
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
|
import file_Storage
|
||||||
from reportlab.pdfgen import canvas
|
from reportlab.pdfgen import canvas
|
||||||
from reportlab.lib.pagesizes import A4
|
from reportlab.lib.pagesizes import A4
|
||||||
from reportlab.lib.utils import ImageReader
|
from reportlab.lib.utils import ImageReader
|
||||||
@@ -12,10 +13,13 @@ from supertokens_python.recipe.session.framework.fastapi import verify_session
|
|||||||
from supertokens_python.recipe.session import SessionContainer
|
from supertokens_python.recipe.session import SessionContainer
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
|
|
||||||
|
import file_Storage.minioConnection
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class medCertUploud(BaseModel):
|
class medCertUploud(BaseModel):
|
||||||
|
app_id: str
|
||||||
fullName: str
|
fullName: str
|
||||||
docfname: str
|
docfname: str
|
||||||
startDate: str
|
startDate: str
|
||||||
@@ -25,32 +29,36 @@ class medCertUploud(BaseModel):
|
|||||||
@router.post("/minio/upload/file/", tags=["Minio"])
|
@router.post("/minio/upload/file/", tags=["Minio"])
|
||||||
async def upload_File_to_user(file: UploadFile = File(...), app_id: str= Form(...)):
|
async def upload_File_to_user(file: UploadFile = File(...), app_id: str= Form(...)):
|
||||||
extension = file.filename.split(".")
|
extension = file.filename.split(".")
|
||||||
return {
|
content = file.file #.read()
|
||||||
"app_id": app_id,
|
# fs = await file.read()
|
||||||
"file_name": file.filename,
|
uploudFile(app_id, file.filename, extension[1], content)
|
||||||
"file_extension": extension,
|
|
||||||
"file_size": file.size,
|
|
||||||
"content":file
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get List of all files by patient
|
|
||||||
@router.post("/files/upload/file/", tags=["Minio"])
|
|
||||||
async def upload_File_to_user( file: UploadFile = File(...)):
|
|
||||||
extension = file.filename.split(".")
|
|
||||||
print(file.file)
|
|
||||||
print(file.filename)
|
|
||||||
print(extension)
|
|
||||||
print(file.size)
|
|
||||||
uploudFile(file.filename, extension[1], file.file, file.size)
|
|
||||||
|
|
||||||
return {"message": "Successfully Uploaded File"}
|
return {"message": "Successfully Uploaded File"}
|
||||||
|
# return {
|
||||||
|
# "app_id": app_id,
|
||||||
|
# "file name": file.filename,
|
||||||
|
# "extension": extension[0],
|
||||||
|
# "file contents": file.file.read(),
|
||||||
|
# }
|
||||||
|
|
||||||
|
# # Get List of all files by patient
|
||||||
|
# @router.post("/minio/upload/file2/", tags=["Minio"])
|
||||||
|
# async def upload_File_to_user( file: UploadFile = File(...)):
|
||||||
|
# extension = file.filename.split(".")
|
||||||
|
# # print(file.file)
|
||||||
|
# # print(file.filename)
|
||||||
|
# # print(extension[1])
|
||||||
|
# # print(file.size)
|
||||||
|
# uploudFile(file.filename, extension[1], file.file, file.size)
|
||||||
|
|
||||||
|
# return {"message": "Successfully Uploaded File"}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Get List of all files by patient
|
# Get List of all files by patient
|
||||||
@router.post("/files/generate/med-cert/", tags=["Minio"])
|
@router.post("/minio/generate/med-cert/", tags=["Minio"])
|
||||||
async def upload_File_to_user(requestItem: medCertUploud, session: SessionContainer = Depends(verify_session())):
|
async def upload_File_to_user(requestItem: medCertUploud, session: SessionContainer = Depends(verify_session())):
|
||||||
uploudMedCert(requestItem.fullName,
|
uploudMedCert(requestItem.app_id,
|
||||||
|
requestItem.fullName,
|
||||||
requestItem.docfname,
|
requestItem.docfname,
|
||||||
requestItem.startDate,
|
requestItem.startDate,
|
||||||
requestItem.endDate,
|
requestItem.endDate,
|
||||||
@@ -61,39 +69,33 @@ async def upload_File_to_user(requestItem: medCertUploud, session: SessionContai
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
def uploudFile(fileName, extension, content, size):
|
def uploudFile(app_id, fileName, extension, content):
|
||||||
client = Minio("minio:9000",
|
client = file_Storage.minioConnection.minioConnect()
|
||||||
access_key="user1",
|
|
||||||
secret_key="C@rtoon1995",
|
|
||||||
secure=False
|
|
||||||
)
|
|
||||||
found = client.bucket_exists("mih")
|
found = client.bucket_exists("mih")
|
||||||
if not found:
|
if not found:
|
||||||
client.make_bucket("mih")
|
client.make_bucket("mih")
|
||||||
else:
|
else:
|
||||||
print("Bucket already exists")
|
print("Bucket already exists")
|
||||||
|
fname = app_id + "/" + fileName
|
||||||
client.put_object("mih",
|
client.put_object("mih",
|
||||||
fileName,
|
fname,
|
||||||
content,
|
content,
|
||||||
length=size,
|
length=-1,
|
||||||
content_type=f"application/{extension}",)
|
part_size=10*1024*1024,
|
||||||
|
content_type=f"application/{extension}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#"minio""localhost:9000"
|
#"minio""localhost:9000"
|
||||||
def uploudMedCert(fullName, docfname, startDate, endDate, returnDate):
|
def uploudMedCert(app_id, fullName, docfname, startDate, endDate, returnDate):
|
||||||
client = Minio("minio:9000",
|
client = file_Storage.minioConnection.minioConnect()
|
||||||
access_key="user1",
|
|
||||||
secret_key="C@rtoon1995",
|
|
||||||
secure=False
|
|
||||||
)
|
|
||||||
generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate)
|
generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate)
|
||||||
found = client.bucket_exists("mih")
|
found = client.bucket_exists("mih")
|
||||||
if not found:
|
if not found:
|
||||||
client.make_bucket("mih")
|
client.make_bucket("mih")
|
||||||
else:
|
else:
|
||||||
print("Bucket already exists")
|
print("Bucket already exists")
|
||||||
fileName = f"Med-Cert-{fullName}-{startDate}.pdf"
|
fileName = f"{app_id}/Med-Cert-{fullName}-{startDate}.pdf"
|
||||||
client.fput_object("mih", fileName, "temp.pdf")
|
client.fput_object("mih", fileName, "temp.pdf")
|
||||||
|
|
||||||
def generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate):
|
def generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate):
|
||||||
@@ -131,4 +133,11 @@ def generateMedCertPDF(fullName, docfname, startDate, endDate, returnDate):
|
|||||||
myCanvas.save()
|
myCanvas.save()
|
||||||
|
|
||||||
#Def get
|
#Def get
|
||||||
#uploudFile("Yasien Meth","Dr D Oct","18-06-2024","20-06-2024","21-06-2024")
|
#uploudFile("Yasien Meth","Dr D Oct","18-06-2024","20-06-2024","21-06-2024")
|
||||||
|
|
||||||
|
def byteToMb(size):
|
||||||
|
sizekb = size /1000
|
||||||
|
sizemb = sizekb / 1000
|
||||||
|
return sizemb
|
||||||
|
|
||||||
|
# print(byteToMb(229574))
|
||||||
@@ -18,7 +18,7 @@ class fileRequest(BaseModel):
|
|||||||
class fileInsertRequest(BaseModel):
|
class fileInsertRequest(BaseModel):
|
||||||
file_path: str
|
file_path: str
|
||||||
file_name: str
|
file_name: str
|
||||||
patient_id: int
|
app_id: str
|
||||||
|
|
||||||
# # Get List of all files
|
# # Get List of all files
|
||||||
# @router.get("/files/patients/", tags="patients_files")
|
# @router.get("/files/patients/", tags="patients_files")
|
||||||
@@ -89,25 +89,26 @@ async def read_all_patient_files_by_app_id(app_id: str, session: SessionContaine
|
|||||||
# db.close()
|
# db.close()
|
||||||
# return items
|
# return items
|
||||||
|
|
||||||
# # Insert Patient note into table
|
# Insert Patient note into table
|
||||||
# @router.post("/files/insert/", tags="patients_notes", status_code=201)
|
@router.post("/files/insert/", tags=["Patients Files"], status_code=201)
|
||||||
# async def insertPatientFiles(itemRequest : fileInsertRequest, session: SessionContainer = Depends(verify_session())):
|
async def insert_Patient_Files(itemRequest : fileInsertRequest, session: SessionContainer = Depends(verify_session())):
|
||||||
# today = date.today()
|
today = date.today()
|
||||||
# db = database.dbConnection.dbConnect()
|
db = database.dbConnection.dbConnect()
|
||||||
# cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
# query = "insert into patient_files "
|
query = "insert into patient_files "
|
||||||
# query += "(file_path, file_name, patient_id, insert_date) "
|
query += "(file_path, file_name, insert_date, app_id) "
|
||||||
# query += "values (%s, %s, %s, %s)"
|
query += "values (%s, %s, %s, %s)"
|
||||||
# notetData = (itemRequest.file_path,
|
notetData = (itemRequest.file_path,
|
||||||
# itemRequest.file_name,
|
itemRequest.file_name,
|
||||||
# itemRequest.patient_id,
|
today,
|
||||||
# today)
|
itemRequest.app_id,
|
||||||
# try:
|
)
|
||||||
# cursor.execute(query, notetData)
|
try:
|
||||||
# except Exception as error:
|
cursor.execute(query, notetData)
|
||||||
# #raise HTTPException(status_code=404, detail="Failed to Create Record")
|
except Exception as error:
|
||||||
# return {"message": error}
|
#raise HTTPException(status_code=404, detail="Failed to Create Record")
|
||||||
# db.commit()
|
return {"message": error}
|
||||||
# cursor.close()
|
db.commit()
|
||||||
# db.close()
|
cursor.close()
|
||||||
# return {"message": "Successfully Created file Record"}
|
db.close()
|
||||||
|
return {"message": "Successfully Created file Record"}
|
||||||
|
|||||||
Reference in New Issue
Block a user