-
Notifications
You must be signed in to change notification settings - Fork 1
/
engines.py
112 lines (89 loc) · 3.08 KB
/
engines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""Database singlethon class"""
import os
from fastapi import UploadFile
from minio import Minio
from sqlmodel import create_engine, Session
from config import settings, secrets
class Singlethon:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs)
return cls._instance
class DatabaseEngine(Singlethon):
"""Database Engine."""
def __init__(self):
sql_file_path = os.path.join(settings.DATABASE.FOLDER_PATH, settings.DATABASE.NAME)
sqlite_url = f"sqlite:///{sql_file_path}"
self.engine = create_engine(sqlite_url, echo=False)
def get(self, statement, first: bool):
"""Get elements of the sql statement."""
with Session(self.engine) as sess:
results = sess.exec(
statement=statement
).all()
return results[0] if first else results
def add(self, obj, batch: bool = False):
"""Add object."""
if not batch:
with Session(self.engine) as sess:
sess.add(obj)
sess.commit()
sess.refresh(obj)
def delete(self, obj, batch: bool = False) -> bool:
"""Delete object."""
if not batch:
with Session(self.engine) as sess:
if not obj:
return False
sess.delete(obj)
sess.commit()
return True
class MinIOEngine(Singlethon):
"""MinIO Engine"""
def __init__(self):
self.client = Minio(
endpoint=settings.BUCKET.MINIO_SERVER,
access_key=secrets.development.MINIO_USERNAME,
secret_key=secrets.development.MINIO_PASSWORD,
secure=False
)
self.bucket_name = settings.BUCKET.MINIO_BUCKET
def add(self, file: UploadFile, short_name: str):
try:
_ = self.put_object(
bucket_name=self.bucket_name,
object_name=short_name,
data=file.file,
length=file.size,
metadata={
'filename': file.filename,
'content_type': file.content_type,
'headers': file.headers,
'size': file.size
}
)
except Exception as exc:
print("Error:", exc)
def get(self, short_name: str):
try:
return self.client.get_object(
bucket_name=self.bucket_name,
object_name=short_name
)
except Exception as exc:
print("Error", exc)
def create_bucket(self):
try:
if not self.client.bucket_exists(self.bucket_name):
self.client.make_bucket(self.bucket_name)
except Exception as exc:
print(exc)
def delete(self, short_url):
try:
self.client.remove_object(
bucket_name=self.bucket_name,
object_name=short_url
)
except Exception as exc:
print(exc)