BinaryONe commited on
Commit
ca92c29
·
1 Parent(s): 84c57d4

Changes in Database

Browse files
FileStream/Database/Elements.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ def NewTG_Files(details:dict) -> dict:
4
+ return {
5
+ "user_id": details['user_id'] if details['user_id'] else None,
6
+ "user_type": details['user_type'] if details['user_type'] else None,
7
+ "message_id": details['message_id'] if details['message_id'] else None,
8
+ "location":details['location'] if details['location'] else None,
9
+ "IMDB_id": "Unknown",
10
+ "poster": "Unknown",
11
+ "title": "Unknown",
12
+ "type": "Unknown",
13
+ "description":"Unknown",
14
+ "genre":"Unknown",
15
+ "quality":"Unknown",
16
+ "time": details['time'] if details['time'] else None,
17
+ "privacy_type": details['privacy_type'] if details['privacy_type'] else None,
18
+ "file_ids":{},
19
+ "file": {
20
+ "file_id":details['file']['file_id'] if details['file']['file_id'] else None,
21
+ "caption":"",
22
+ "file_unique_id": details['file']['file_unique_id'] if details['file']['file_unique_id'] else None,
23
+ "file_name":details['file']['file_name'] if details['file']['file_name'] else None,
24
+ "file_size":details['file']['file_size'] if details['file']['file_size'] else None,
25
+ "mime_type":details['file']['mime_type']if details['file']['mime_type'] else None,
26
+ "taged_users": {}
27
+ },
28
+ }
29
+
30
+
31
+ def UserSchema(id: str) -> dict:
32
+
33
+ return dict(
34
+ telegram_id=id,
35
+ access="USER",
36
+ tele_status={
37
+ "status": "ACTIVE",
38
+ "activity": None,
39
+ "joined": Time_ISTKolNow()
40
+ },
41
+ file={
42
+ "links": 0,
43
+ "private_files": 0,
44
+ "public_files": 0,
45
+ },
46
+ site_id="None",
47
+ site_status={
48
+ "status": None,
49
+ "activity": None,
50
+ "password": None,
51
+ "links": 0,
52
+ "joined": "None"
53
+ },
54
+ )
FileStream/Database/__init__.py CHANGED
@@ -1 +1,4 @@
1
  from .database import Database
 
 
 
 
1
  from .database import Database
2
+ from FileStream.config import Telegram
3
+
4
+ DB_Controller = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME)
FileStream/Database/database.py CHANGED
@@ -10,6 +10,8 @@ from bson.json_util import dumps
10
  from FileStream.Exceptions import FileNotFound
11
  from FileStream.Tools import Time_ISTKolNow
12
 
 
 
13
 
14
  class Database:
15
 
@@ -21,67 +23,13 @@ class Database:
21
  self.files = self.db.Public_Files
22
  self.pfile = self.db.Private_Files
23
  self.web_upload = self.db.Web_Files
24
-
25
 
26
  #---------------------[ SCHEMAS ]------------------------------#
27
  #---------------------[ NEW USER ]---------------------#
28
-
29
- def new_user(self, id):
30
- return dict(
31
- telegram_id=id,
32
- access="USER",
33
- tele_status={
34
- "status": "ACTIVE",
35
- "activity": None,
36
- "joined": Time_ISTKolNow()
37
- },
38
- file={
39
- "links": 0,
40
- "private_files": 0,
41
- "public_files": 0,
42
- },
43
- site_id="None",
44
- site_status={
45
- "status": None,
46
- "activity": None,
47
- "password": None,
48
- "links": 0,
49
- "joined": "None"
50
- },
51
- )
52
- #------------------------------------------------
53
-
54
- def NewTG_Files(self, details):
55
- return {
56
- "user_id": details['user_id'] if details['user_id'] else None,
57
- "user_type": details['user_type'] if details['user_type'] else None,
58
- "message_id": details['message_id'] if details['message_id'] else None,
59
- "location":details['location'] if details['location'] else None,
60
- "IMDB_id": "Unknown",
61
- "poster": "Unknown",
62
- "title": "Unknown",
63
- "type": "Unknown",
64
- "description":"Unknown",
65
- "genre":"Unknown",
66
- "quality":"Unknown",
67
- "time": details['time'] if details['time'] else None,
68
- "privacy_type": details['privacy_type'] if details['privacy_type'] else None,
69
- "file_ids":{},
70
- "file": {
71
- "file_id":details['file']['file_id'] if details['file']['file_id'] else None,
72
- "caption":"",
73
- "file_unique_id": details['file']['file_unique_id'] if details['file']['file_unique_id'] else None,
74
- "file_name":details['file']['file_name'] if details['file']['file_name'] else None,
75
- "file_size":details['file']['file_size'] if details['file']['file_size'] else None,
76
- "mime_type":details['file']['mime_type']if details['file']['mime_type'] else None,
77
- "taged_users": {}
78
- },
79
- }
80
-
81
  # ---------------------[ ADD USER ]---------------------#
82
 
83
  async def add_user(self, id):
84
- user = self.new_user(id)
85
  await self.users.insert_one(user)
86
 
87
  async def add_admin(self, id):
@@ -89,7 +37,7 @@ class Database:
89
  if user:
90
  await self.users.update_one({"_id": user['_id']}, {"$set": {"access":"ADMIN" }})
91
  else:
92
- user = self.new_user(id)
93
  user['access']="ADMIN"
94
  await self.users.insert_one(user)
95
 
@@ -147,13 +95,25 @@ class Database:
147
  return False
148
 
149
  # ---------------------[ ADD FILE TO DB ]---------------------#
150
- async def add_file(self, file_info):
151
  file_info["time"] = Time_ISTKolNow()
152
- fetch_old = await self.get_file_by_fileuniqueid(file_info["user_id"], file_info['file']["file_unique_id"])
153
- if fetch_old:
154
- return fetch_old["_id"]
155
- await self.count_links(file_info["user_id"], "+")
156
- return (await self.files.insert_one(file_info)).inserted_id
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  # ---------------------[ ADD FILE TO Temp DB ]---------------------#
159
  async def add_temp_file(self, file_info):
@@ -251,18 +211,38 @@ class Database:
251
 
252
  async def get_file_by_fileuniqueid_only(self, file_unique_id, privacy_type:str):
253
  if privacy_type=="TEMPORARY":
 
 
254
  return await self.files.find_one({"file.file_unique_id": file_unique_id})
 
 
255
  else:
256
- return await self.files.find_one({"file.file_unique_id": file_unique_id})
257
 
258
- async def get_file_by_fileuniqueid(self, id, file_unique_id):
259
- count = await self.files.count_documents({"user_id":id,"file.file_unique_id":file_unique_id})
260
- if count == 0:
261
- return False
262
- elif count == 1:
263
- return await self.files.find_one({"user_id": id,"file.file_unique_id": file_unique_id})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
  else:
265
- return self.files.find({"user_id": id,"file.file_unique_id": file_unique_id})
 
266
 
267
  # ---------------------[ UPDATE FILE IN DB ]---------------------#
268
 
 
10
  from FileStream.Exceptions import FileNotFound
11
  from FileStream.Tools import Time_ISTKolNow
12
 
13
+ from .Elements import UserSchema, NewTG_Files
14
+
15
 
16
  class Database:
17
 
 
23
  self.files = self.db.Public_Files
24
  self.pfile = self.db.Private_Files
25
  self.web_upload = self.db.Web_Files
 
26
 
27
  #---------------------[ SCHEMAS ]------------------------------#
28
  #---------------------[ NEW USER ]---------------------#
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # ---------------------[ ADD USER ]---------------------#
30
 
31
  async def add_user(self, id):
32
+ user = UserSchema(id)
33
  await self.users.insert_one(user)
34
 
35
  async def add_admin(self, id):
 
37
  if user:
38
  await self.users.update_one({"_id": user['_id']}, {"$set": {"access":"ADMIN" }})
39
  else:
40
+ user = UserSchema(id)
41
  user['access']="ADMIN"
42
  await self.users.insert_one(user)
43
 
 
95
  return False
96
 
97
  # ---------------------[ ADD FILE TO DB ]---------------------#
98
+ async def add_file(self, file_info:dict, db_type:str):
99
  file_info["time"] = Time_ISTKolNow()
100
+ if db_type =="PUBLIC":
101
+ fetch_old = await self.get_file_by_fileuniqueid(file_info["user_id"], file_info['file']["file_unique_id"], db_type ="PUBLIC")
102
+ if fetch_old:
103
+ return fetch_old["_id"]
104
+ await self.count_links(file_info["user_id"], "+")
105
+ return (await self.files.insert_one(file_info)).inserted_id
106
+ elif db_type=="PRIVATE":
107
+ pass
108
+ elif db_type == "TEMPORARY":
109
+ fetch_old = await self.get_file_by_fileuniqueid(file_info["user_id"], file_info['file']["file_unique_id"], db_type ="TEMPORARY")
110
+ if fetch_old:
111
+ return fetch_old["_id"]
112
+ await self.count_links(file_info["user_id"], "+")
113
+ return (await self.tfiles.insert_one(file_info)).inserted_id
114
+ else:
115
+ return {}
116
+
117
 
118
  # ---------------------[ ADD FILE TO Temp DB ]---------------------#
119
  async def add_temp_file(self, file_info):
 
211
 
212
  async def get_file_by_fileuniqueid_only(self, file_unique_id, privacy_type:str):
213
  if privacy_type=="TEMPORARY":
214
+ return await self.tfiles.find_one({"file.file_unique_id": file_unique_id})
215
+ elif privacy_type=="PUBLIC":
216
  return await self.files.find_one({"file.file_unique_id": file_unique_id})
217
+ elif privacy_type=="PRIVATE":
218
+ return await self.pfiles.find_one({"file.file_unique_id": file_unique_id})
219
  else:
220
+ return {}
221
 
222
+ async def get_file_by_fileuniqueid(self, id, file_unique_id, privacy_type:str):
223
+ if privacy_type=="TEMPORARY":
224
+ count = await self.tfiles.count_documents({"user_id":id,"file.file_unique_id":file_unique_id})
225
+ if count == 0:
226
+ return False
227
+ elif count == 1:
228
+ return await self.tfiles.find_one({"user_id": id,"file.file_unique_id": file_unique_id})
229
+ else:
230
+ return self.tfiles.find({"user_id": id,"file.file_unique_id": file_unique_id})
231
+
232
+ elif privacy_type=="PUBLIC":
233
+ count = await self.files.count_documents({"user_id":id,"file.file_unique_id":file_unique_id})
234
+ if count == 0:
235
+ return False
236
+ elif count == 1:
237
+ return await self.files.find_one({"user_id": id,"file.file_unique_id": file_unique_id})
238
+ else:
239
+ return self.files.find({"user_id": id,"file.file_unique_id": file_unique_id})
240
+
241
+ elif privacy_type=="PRIVATE":
242
+ return await self.pfiles.find_one({"file.file_unique_id": file_unique_id})
243
  else:
244
+ return {}
245
+
246
 
247
  # ---------------------[ UPDATE FILE IN DB ]---------------------#
248
 
FileStream/bot/plugins/FileHandlers/callback.py CHANGED
@@ -151,7 +151,7 @@ async def cb_data(bot: Client, update: CallbackQuery):
151
 
152
  file_info = get_file_info(message, instruction)
153
  # Here we are Adding the File Into the database First
154
- inserted_id = await db.add_file(file_info)
155
  await get_file_ids(False, inserted_id, message)
156
  #All the Time Get_file_ids should be called before update privacy or else tagged_users will be {}
157
  await db.update_privacy(file_info)
@@ -214,7 +214,7 @@ async def cb_data(bot: Client, update: CallbackQuery):
214
  print("Line No 212 ")
215
  file_info = get_file_info(message, instruction)
216
  # Here we are Adding the File Into the database First
217
- inserted_id = await db.add_temp_file(file_info)
218
  await get_file_ids(False, inserted_id, message)
219
  #All the Time Get_file_ids should be called before update privacy or else tagged_users will be {}
220
  await db.update_privacy(file_info)
@@ -257,7 +257,7 @@ async def cb_data(bot: Client, update: CallbackQuery):
257
  }
258
  file_info = get_file_info(message, instruction)
259
  # Here we are Adding the File Into the database First
260
- db_id = await db.add_file(file_info)
261
  await get_file_ids(False, db_id, message)
262
  if True:
263
  file_info = await db.get_file(db_id)
 
151
 
152
  file_info = get_file_info(message, instruction)
153
  # Here we are Adding the File Into the database First
154
+ inserted_id = await db.add_file(file_info=file_info, db_type="PUBLIC")
155
  await get_file_ids(False, inserted_id, message)
156
  #All the Time Get_file_ids should be called before update privacy or else tagged_users will be {}
157
  await db.update_privacy(file_info)
 
214
  print("Line No 212 ")
215
  file_info = get_file_info(message, instruction)
216
  # Here we are Adding the File Into the database First
217
+ inserted_id = await db.add_file(file_info=file_info, db_type="TEMPORARY")
218
  await get_file_ids(False, inserted_id, message)
219
  #All the Time Get_file_ids should be called before update privacy or else tagged_users will be {}
220
  await db.update_privacy(file_info)
 
257
  }
258
  file_info = get_file_info(message, instruction)
259
  # Here we are Adding the File Into the database First
260
+ db_id = await db.add_file(file_info=file_info, db_type="PRIVATE")
261
  await get_file_ids(False, db_id, message)
262
  if True:
263
  file_info = await db.get_file(db_id)
FileStream/bot/plugins/FileHandlers/stream.py CHANGED
@@ -100,7 +100,7 @@ async def channel_receive_handler(bot: Client, message: Message):
100
  await is_channel_exist(bot, message)
101
 
102
  try:
103
- inserted_id = await db.add_file(get_file_info(message))
104
  await get_file_ids(False, inserted_id, MULTI_CLIENTS, message)
105
  reply_markup, stream_link = await gen_link(_id=inserted_id)
106
  await bot.edit_message_reply_markup(
 
100
  await is_channel_exist(bot, message)
101
 
102
  try:
103
+ inserted_id = await db.add_file(file_info=get_file_info(message), db_type="TEMPORARY")
104
  await get_file_ids(False, inserted_id, MULTI_CLIENTS, message)
105
  reply_markup, stream_link = await gen_link(_id=inserted_id)
106
  await bot.edit_message_reply_markup(
FileStream/utils/FileProcessors/custom_ul.py CHANGED
@@ -225,7 +225,7 @@ class TeleUploader:
225
 
226
  # Here we are Adding the File Into the database First
227
  #await db.add_webfile(file_info)
228
- inserted_id = await db.add_file(file_info)
229
  await get_file_ids(False, inserted_id, MessageFile)
230
  reply_markup, stream_text = await gen_link(_id=inserted_id)
231
  await message.edit_text(
 
225
 
226
  # Here we are Adding the File Into the database First
227
  #await db.add_webfile(file_info)
228
+ inserted_id = await db.add_file(file_info=file_info,db_type="TEMPORARY")
229
  await get_file_ids(False, inserted_id, MessageFile)
230
  reply_markup, stream_text = await gen_link(_id=inserted_id)
231
  await message.edit_text(