-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
220 lines (194 loc) · 6.8 KB
/
db.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from datetime import datetime
import subprocess
import uuid
import psycopg2
from psycopg2.extras import RealDictCursor
class FileEntry:
def __init__(self,id,name,type,date_modified,size,thumbnail,date_created,parent_id,path):
self.id = id,
self.name = name
self.type = type
self.date_modified = date_modified
self.size = size
self.thumbnail = thumbnail
self.date_created = date_created,
self.parent_id = parent_id
self.path = path
staticmethod
def new(name,type,parent_id,path,size=0,thumbnail=None):
now = datetime.now()
return FileEntry(
id = str(uuid.uuid4()).replace("-",""),
name = name,
type = type,
size = size,
date_created = now,
thumbnail = thumbnail,
date_modified = now,
parent_id = parent_id,
path=path
)
def to_json(self):
return {
"id" : self.id,
"name" : self.name,
"type" : self.type,
"size" : self.size,
"date_created" : self.date_created,
"date_modified" : self.date_modified,
"thumbnail" : self.thumbnail,
"parent_id" : self.parent_id,
"path" : self.path
}
staticmethod
def from_db(db_row:list):
return FileEntry(
id = db_row['id'],
name = db_row['display_name'],
parent_id = db_row['parent_id'],
size = db_row['size'],
type = db_row['type'],
thumbnail = db_row['thumbnail'],
date_created = db_row['date_created'],
date_modified = db_row['date_modified'],
path = db_row['path']
)
def __str__(self) -> str:
return f"id: {self.id} display_name: {self.name} parent_id: {self.parent_id} size: {self.size} type: {self.type} thumbnail: {self.thumbnail} date_created: {self.date_created} date_modified: {self.date_modified}"
class FileDB:
def __init__(self,tablename="filesystem"):
self.connection = FileDB.connect()
self.cursor = self.connection.cursor(cursor_factory=RealDictCursor)
self.tablename = tablename
def close(self):
self.cursor.close()
self.connection.close()
print(subprocess.Popen(['pg_ctl','stop','-D',"F:\\Postgresql\\data"]).wait())
staticmethod
def connect():
#TODO Check this line at end
print(subprocess.Popen(['pg_ctl','start','-D',"F:\\Postgresql\\data"]).wait())
return psycopg2.connect(
database="fileserver_db", user='postgres', password='mandar', host='127.0.0.1', port= '5432'
)
#Checked
def create_file_entry(self,file:FileEntry):
data = (
file.size,
file.type,
file.id,
file.name,
file.parent_id,
file.thumbnail,
file.date_created,
file.date_modified,
file.path
)
try:
self.cursor.execute(f"INSERT INTO {self.tablename} (size,type,id,display_name,parent_id,thumbnail,date_created,date_modified,path) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",data)
if file.type=="file":
self.cursor.execute(f"UPDATE {self.tablename} SET size=size+%s WHERE id=%s",(file.size,file.parent_id,))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
#Checked
def get_directory(self,directory_id):
try:
self.cursor.execute(f"SELECT * FROM {self.tablename} WHERE parent_id=%s",(directory_id,))
rows = self.cursor.fetchall()
return [FileEntry.from_db(x).to_json() for x in rows]
except Exception as e:
print(str(e))
#Checked
def get_db_entry(self,id)->FileEntry:
try:
self.cursor.execute(f"SELECT * FROM {self.tablename} WHERE id=%s",(id,))
rows = self.cursor.fetchall()
if len(rows)>0:
return FileEntry.from_db(rows[0])
return None
except Exception as e:
print(str(e))
#Checked
def delete_directory(self,directory_id):
now = datetime.now()
try:
d = self.get_db_entry(id=directory_id)
self.cursor.execute(f"DELETE FROM {self.tablename} WHERE id=%s",(d.id,))
self.cursor.execute(f"DELETE FROM {self.tablename} WHERE parent_id=%s",(d.id,))
self.cursor.execute(f"UPDATE {self.tablename} SET date_modified=%s,size=size-%s WHERE id=%s",(now,d.size,d.parent_id))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
#Checked
def delete_file(self,file_id):
now = datetime.now()
try:
d = self.get_db_entry(id=file_id)
self.cursor.execute(f"DELETE FROM {self.tablename} WHERE id=%s",(file_id,))
self.cursor.execute(f"UPDATE {self.tablename} SET date_modified=%s,size=size-%s WHERE id=%s",(now,d.size,d.parent_id,))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
#Checked
def rename_file_folder(self,file_id,new_name):
now = datetime.now()
try:
self.cursor.execute(f"UPDATE {self.tablename} SET display_name=%s, date_modified=%s WHERE id=%s",(new_name,now,file_id))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
#Checked
def move_file_folder(self,file_id,new_parent_id):
now = datetime.now()
try:
self.cursor.execute(f"UPDATE {self.tablename} SET parent_id=%s, date_modified=%s WHERE id=%s",(new_parent_id,now,file_id))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
#Checked
def change_last_modified(self,file_id):
now = datetime.now()
try:
self.cursor.execute(f"UPDATE {self.tablename} SET date_modified=%s WHERE id=%s",(now,file_id))
self.connection.commit()
except Exception as e:
self.connection.rollback()
raise e
def create_mock_data(db:FileDB):
folder = FileEntry.new(
name = "NewFolder",
type = "folder",
parent_id = "files",
size = 0
)
files = [FileEntry.new(
name = f"NewFile{x}.jpg",
type = "file",
parent_id = folder.id,
size = 100*x
) for x in range(5)]
db.create_file_entry(folder)
for f in files:
db.create_file_entry(f)
if __name__=='__main__':
db = FileDB()
# DO SOMETHING
print(db.get_directory(directory_id="eaad924951744459be64b81c4184bef2"))
db.close()
# column_name | data_type
# ---------------+-----------
# type | text
# size | integer
# parent_id | text
# thumbnail | text
# date_created | text
# id | text
# date_modified | text
# display_name | text
# path | text