-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·612 lines (601 loc) · 31.5 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#! /usr/bin/env python3
import requests, json, os, pickle, base64, string, random, shutil
from math import ceil, sqrt
from getpass import getpass
from pathlib import Path
from datetime import date
from optparse import OptionParser
from colorama import Fore, Back, Style
from multiprocessing import cpu_count, Pool, Lock
from time import strftime, localtime
from aes_256 import encrypt, decrypt, generate_key
status_color = {
'+': Fore.GREEN,
'-': Fore.RED,
'*': Fore.YELLOW,
':': Fore.CYAN,
' ': Fore.WHITE
}
def display(status, data, start='', end='\n'):
print(f"{start}{status_color[status]}[{status}] {Fore.BLUE}[{date.today()} {strftime('%H:%M:%S', localtime())}] {status_color[status]}{Style.BRIGHT}{data}{Fore.RESET}{Style.RESET_ALL}", end=end)
def get_arguments(*args):
parser = OptionParser()
for arg in args:
parser.add_option(arg[0], arg[1], dest=arg[2], help=arg[3])
return parser.parse_args()[0]
cwd = Path.cwd()
default_branch = "main"
githubREPO_API = "https://api.github.com/user/repos"
mbs = 50
individual_segment_size = mbs * 1000 * 1000
segements_per_repository = 39
required_space_times = 3.5
github_repo_size = individual_segment_size * segements_per_repository
thread_count = cpu_count()
lock = Lock()
'''
+ .
/ -
= _
'''
def createRepository(auth_token, name, private):
headers = {
"Authorization": f"token {auth_token}"
}
data = {
"name": name,
"private": private,
}
response = requests.post(githubREPO_API, headers=headers, data=json.dumps(data))
if response.status_code == 201:
return True
return response
def deleteRepository(auth_token, user, repository):
headers = {
"Authorization": f"token {auth_token}"
}
response = requests.delete(f"https://api.github.com/repos/{user}/{repository}", headers=headers)
if response.status_code == 204:
return True
return response
def cloneRepository(auth_token, user, repository, folder=None, verbose=True):
if folder == None:
folder = f".repositories/{repository}"
status = os.system(f"git clone https://{auth_token}@github.com/{user}/{repository.replace(' ', '-')}.git '{folder.replace(' ', '-')}' {'' if verbose else '>/dev/null 2>/dev/null'}")
return status
def cloneRepositories(auth_token, user, repositories):
for repository in repositories:
status = cloneRepository(auth_token, user, repository, f".repositories/{repository}", False)
if status != 0:
return False
return True
def createRepositories(auth_token, user, repositories, private):
for repository in repositories:
createRepository(auth_token, repository, private)
cloneRepository(auth_token, user, repository, f"../.repositories/{repository}", False)
def encryptFiles(key, salt, files):
for small_file in files:
with open(small_file, 'rb') as file:
content = file.read()
encrypted_content = encrypt(content, key, salt)
with open(small_file, 'wb') as file:
file.write(encrypted_content)
def decryptFiles(key, salt, files):
for small_file in files:
with open(small_file, 'rb') as file:
encrypted_content = file.read()
content = decrypt(encrypted_content, key, salt)
with open(small_file, 'wb') as file:
file.write(content)
def zipFile(file, password=None):
if password:
os.system(f"zip -P {password} '{file}.zip' '{file}'")
else:
os.system(f"zip '{file}.zip' '{file}'")
def unzipFile(file, password=None):
if password:
os.system(f"unzip -P {password} '{file}.zip' '{file}'")
else:
os.system(f"unzip '{file}.zip' '{file}'")
def uploadToRepositories(repositores):
os.chdir("../.repositories/")
for repository_name in repositores:
os.chdir(f"{repository_name.replace(' ', '-')}")
os.system("git add .")
os.system("git commit -m 'Added File Segments' >/dev/null 2>/dev/null")
status = os.system(f"git push origin {default_branch} >/dev/null 2>/dev/null")
os.system("..")
os.system(f"rm -rf {repository_name.replace(' ', '-')}")
if status != 0:
return False
os.chdir("..")
return True
def uploadFile(auth_token, file_path, private, user, key_before, zip_key, key_after):
file_name = file_path.split('/')[-1]
file_size = os.path.getsize(file_path)
free_space = shutil.disk_usage(file_path).free
display(':', f"File Size = {Back.MAGENTA}{file_size} Bytes{Back.RESET}")
display(':', f"Free Space = {Back.MAGENTA}{free_space} Bytes{Back.RESET}")
if free_space < file_size * required_space_times:
return False, "Not Enough Space for Processing the File", '', file_size, ''
display('+', f"Copying the File Contents...")
os.chdir(".tmp")
if key_before:
display(':', f"Encrypting before Compressing")
display('+', f"Spliting the File into Files of {mbs}MB each...")
os.system(f"split -b {mbs}M '{file_path}'")
files = os.listdir()
files.sort()
total_files = len(files)
display('+', f"Total Segments = {Back.MAGENTA}{total_files}{Back.RESET}")
display('+', f"Encrypting All the Segments using {thread_count} Threads...")
key, salt_before = generate_key(key_before)
pool = Pool(thread_count)
file_divisions = [files[group*total_files//thread_count: (group+1)*total_files//thread_count] for group in range(thread_count)]
threads = []
for file_division in file_divisions:
threads.append(pool.apply_async(encryptFiles, (key, salt_before, file_division)))
for thread in threads:
thread.get()
pool.close()
pool.join()
display('+', f"Merging All the Segments to a Single File...")
os.system(f"cat {' '.join(files)} > '{file_name}'")
split_size = os.path.getsize(files[0])
display('+', f"Removing Segments...")
os.system(f"rm {' '.join(files)}")
else:
os.system(f"cp '{file_path}' '{file_name}'")
salt_before = ''
split_size = None
display(':', f"Compressing the File...")
zipFile(file_name, zip_key)
display('+', f"Removing the Previous File")
os.system(f"rm '{file_name}'")
display('+', f"Spliting the File into Files of {mbs}MB each...")
os.system(f"split -b {mbs}M '{file_name}.zip'")
os.system(f"rm '{file_name}.zip'")
if key_after:
display(':', f"Encrypting After Compressing")
files = os.listdir()
files.sort()
total_files = len(files)
display('+', f"Total Segments = {Back.MAGENTA}{total_files}{Back.RESET}")
display('+', f"Encrypting All the Segments using {thread_count} Threads...")
key, salt_after = generate_key(key_after)
pool = Pool(thread_count)
file_divisions = [files[group*total_files//thread_count: (group+1)*total_files//thread_count] for group in range(thread_count)]
threads = []
for file_division in file_divisions:
threads.append(pool.apply_async(encryptFiles, (key, salt_after, file_division)))
for thread in threads:
thread.get()
pool.close()
pool.join()
else:
salt_after = ''
display(':', f"Uploading Segments to Github")
files = os.listdir()
files.sort()
total_files = len(files)
files = [[file, index // segements_per_repository] for index, file in enumerate(files)]
repository_count = ceil(total_files / segements_per_repository)
repositories = [f"{base64.b64encode(file_name.encode()).decode().replace('+', '.').replace('/', '-').replace('=', '_')}_{index}" for index in range(repository_count)]
repository_divisions = [repositories[group*repository_count//thread_count: (group+1)*repository_count//thread_count] for group in range(thread_count)]
display('+', f"Repositories Required = {Back.MAGENTA}{repository_count}{Back.RESET}")
display('+', f"Create Repositories using 16 Threads...")
pool = Pool(thread_count)
threads = []
for repository_division in repository_divisions:
threads.append(pool.apply_async(createRepositories, (auth_token, user, repository_division, private)))
for thread in threads:
thread.get()
pool.close()
pool.join()
display('+', f"Uploading Files to Repositories using {int(sqrt(thread_count))} Threads...")
for index, (file, repository_index) in enumerate(files):
os.system(f"mv {file} ../.repositories/{base64.b64encode(file_name.encode()).decode().replace('+', '.').replace('/', '-').replace('=', '_')}_{repository_index}/{index}")
pool = Pool(int(sqrt(thread_count)))
repository_divisions = [repositories[group*repository_count//int(sqrt(thread_count)): (group+1)*repository_count//int(sqrt(thread_count))] for group in range(int(sqrt(thread_count)))]
threads = []
failed = False
for repository_division in repository_divisions:
threads.append(pool.apply_async(uploadToRepositories, (repository_division, )))
for thread in threads:
status = thread.get()
if status == False:
failed = True
if failed:
display('-', f"Failed to Upload Data!")
display('*', f"Reverting...")
for repository in repositories:
deleteRepository(auth_token, user, repository)
exit(0)
pool.close()
pool.join()
os.chdir(str(cwd))
return salt_before, salt_after, repositories, file_size, split_size
def downloadFile(file, user, repositories, key_before, zip_key, key_after, salt_before, salt_after, split_size):
total_repositories = len(repositories)
display('+', f"Cloning {total_repositories} Repositories with {thread_count} Threads")
repository_divisions = [repositories[group*total_repositories//thread_count: (group+1)*(total_repositories)//thread_count] for group in range(thread_count)]
pool = Pool(thread_count)
threads = []
failed = False
for repository_division in repository_divisions:
threads.append(pool.apply_async(cloneRepositories, (auth_token, user, repository_division, )))
for thread in threads:
status = thread.get()
if status == False:
failed = True
if failed:
display('-', f"Failed to Download Data!")
pool.close()
pool.join()
base_name = repositories[0].split('_')[0]
files = [file for file in os.listdir(".repositories") if file.startswith(base_name)]
temporary_repository_folder = cwd / ".tmp" / base_name
temporary_repository_folder.mkdir(exist_ok=True, parents=True)
for repository in files:
os.system(f"mv .repositories/{repository}/* .tmp/{base_name}/.")
os.chdir(f".tmp/{base_name}")
files = [int(file) for file in os.listdir()]
files.sort()
files = [str(file) for file in files]
total_files = len(files)
if key_after:
display('+', f"Total Segments = {Back.MAGENTA}{total_files}{Back.RESET}")
display('+', f"Decrypting All the Segments using {thread_count} Threads...")
key, _ = generate_key(key_after, salt_after)
pool = Pool(thread_count)
file_divisions = [files[group*total_files//thread_count: (group+1)*total_files//thread_count] for group in range(thread_count)]
threads = []
for file_division in file_divisions:
threads.append(pool.apply_async(decryptFiles, (key, salt_after, file_division)))
for thread in threads:
thread.get()
pool.close()
pool.join()
display('+', f"Merging All the Segments to a Single File...")
os.system(f"cat {' '.join(files)} > '{file}.zip'")
display('+', f"Removing Segments...")
os.system(f"rm {' '.join(files)}")
display(':', f"Decompressing the File...")
unzipFile(file, zip_key)
os.system(f"rm '{file}.zip'")
if key_before:
display('+', f"Spliting the File into Files of {split_size} Bytes each...")
os.system(f"split -b {split_size} '{file}'")
os.system(f"rm '{file}'")
files = os.listdir()
files.sort()
total_files = len(files)
display('+', f"Total Segments = {Back.MAGENTA}{total_files}{Back.RESET}")
display('+', f"Decrypting All the Segments using {thread_count} Threads...")
key, _ = generate_key(key_before, salt_before)
pool = Pool(thread_count)
file_divisions = [files[group*total_files//thread_count: (group+1)*total_files//thread_count] for group in range(thread_count)]
threads = []
for file_division in file_divisions:
threads.append(pool.apply_async(decryptFiles, (key, salt_before, file_division)))
for thread in threads:
thread.get()
pool.close()
pool.join()
display('+', f"Merging All the Segments to a Single File...")
os.system(f"cat {' '.join(files)} > '{file}'")
display('+', f"Removing Segments...")
os.system(f"rm {' '.join(files)}")
os.chdir(str(cwd))
os.system(f"mv '.tmp/{base_name}/{file}' downloads/.")
def generateRandom(length):
letters = string.ascii_letters + "0123456789"
return ''.join([random.choice(letters) for _ in range(length)])
def makeFolders():
temporary_folder = cwd / ".tmp"
temporary_folder.mkdir(exist_ok=True)
temporary_repository_folder = cwd / ".repositories"
temporary_repository_folder.mkdir(exist_ok=True)
user_config_folder = cwd / "configs"
user_config_folder.mkdir(exist_ok=True)
downloads_folder = cwd / "downloads"
downloads_folder.mkdir(exist_ok=True)
if __name__ == "__main__":
makeFolders()
arguments = get_arguments(('-u', "--user", "user", "Github Username"),
('-b', "--branch", "branch", f"Branch of Github Repositories (Default={default_branch})"),
('-U', "--upload", "upload", "Path to File that you want to upload"),
('-e', "--encryption", "encryption", "Encrypt File (None/Before/After/Both, Default=None)"),
('-p', "--private", "private", "Private File (True/False, Default=True)"),
('-z', "--zip-password", "zip_password", "Password for Compressed File (True/False, Default=False)"),
('-d', "--download", "download", "Download a File"),
('-v', "--view", "view", "View Uploaded Files"),
('-D', "--delete", "delete", "Delete a File"),
('-t', "--update-token", "update_token", "Update Github API Token (Input = Username for which to update the token)"))
if not arguments.user:
display('-', f"Please Provide a Username")
exit(0)
if not arguments.branch:
display(':', f"Branch Set to {Back.MAGENTA}{default_branch}{Back.RESET}")
arguments.branch = default_branch
else:
default_branch = arguments.branch
try:
with open("authentication_tokens.pickle", 'rb') as file:
authentication_tokens = pickle.load(file)
except FileNotFoundError:
authentication_tokens = {}
except:
display('-', f"Error while Reading Authentication Tokens")
exit(0)
if arguments.update_token:
auth_token = input(f"Enter Updated Github API Authentication Token for {arguments.update_token} : ")
auth_token_password = getpass(f"Enter the Password for Accessing Token : ")
salt = authentication_tokens[arguments.user]["salt"]
key, _ = generate_key(auth_token_password, salt)
encrypted_auth_token = encrypt(auth_token.encode(), key, salt)
authentication_tokens[arguments.user]["token"] = encrypted_auth_token
with open("authentication_tokens.pickle", 'wb') as file:
pickle.dump(authentication_tokens, file)
exit(0)
elif arguments.update_token:
display('-', f"User {Back.YELLOW}{arguments.update_token}{Back.RESET} not found in Cached Configs!")
exit(0)
if arguments.user not in authentication_tokens.keys():
auth_token = input(f"Enter Github API Authentication Token for {arguments.user} : ")
github_password = getpass(f"Enter your Github Account Password for User {arguments.user} : ")
config_repository_status = cloneRepository(auth_token, arguments.user, "storage_config", f"configs/{arguments.user}")
salt = None
if config_repository_status != 0:
public_before_zip = getpass(f"Enter AES-Encryption Password before File Zipping for Public Files (Enter Empty for Autogeneration) : ")
if public_before_zip == '':
public_before_zip = generateRandom(50)
public_zip = getpass(f"Enter Password for Public ZIP Files (Enter Empty for Autogeneration) : ")
if public_zip == '':
public_zip = generateRandom(50)
public_after_zip = getpass(f"Enter AES-Encryption Password afer File Zipping for Public Files (Enter Empty for Autogeneration) : ")
if public_after_zip == '':
public_after_zip = generateRandom(50)
private_before_zip = getpass(f"Enter AES-Encryption Password before File Zipping for private Files (Enter Empty for Autogeneration) : ")
if private_before_zip == '':
private_before_zip = generateRandom(50)
private_zip = getpass(f"Enter Password for private ZIP Files (Enter Empty for Autogeneration) : ")
if private_zip == '':
private_zip = generateRandom(50)
private_after_zip = getpass(f"Enter AES-Encryption Password afer File Zipping for private Files (Enter Empty for Autogeneration) : ")
if private_after_zip == '':
private_after_zip = generateRandom(50)
else:
with open(f"configs/{arguments.user}/public_config", 'rb') as file:
public_config = pickle.load(file)
public_before_zip = public_config["public_before_zip"]
public_zip = public_config["public_zip"]
public_after_zip = public_config["public_after_zip"]
config_salt = public_config["salt"]
key, _ = generate_key(github_password, config_salt)
with open(f"configs/{arguments.user}/private_config", 'rb') as file:
content = file.read()
private_config = pickle.loads(decrypt(content, key, config_salt))
private_before_zip = private_config["private_before_zip"]
private_zip = private_config["private_zip"]
private_after_zip = private_config["private_after_zip"]
salt = private_config["salt"]
auth_storing_status = input(f"Do you want to Store the Token (Y/n) : ")
if 'y' in auth_storing_status.lower():
auth_token_password = getpass(f"Enter the Password to Securely Store the Token : ")
if salt == None:
key, salt = generate_key(auth_token_password)
else:
key, _ = generate_key(auth_token_password, salt)
encrypted_auth_token = encrypt(auth_token.encode(), key, salt)
encrypted_github_password = encrypt(github_password.encode(), key, salt)
encrypted_private_before_zip = encrypt(private_before_zip.encode(), key, salt)
encrypted_private_zip = encrypt(private_zip.encode(), key, salt)
encrypted_private_after_zip = encrypt(private_after_zip.encode(), key, salt)
authentication_tokens[arguments.user] = {
"token": encrypted_auth_token,
"github_password": encrypted_github_password,
"public_before_zip": public_before_zip,
"public_zip": public_zip,
"public_after_zip": public_after_zip,
"private_before_zip": encrypted_private_before_zip,
"private_zip": encrypted_private_zip,
"private_after_zip": encrypted_private_after_zip,
"salt": salt
}
with open("authentication_tokens.pickle", 'wb') as file:
pickle.dump(authentication_tokens, file)
else:
encrypted_auth_token = authentication_tokens[arguments.user]["token"]
encrypted_github_password = authentication_tokens[arguments.user]["github_password"]
public_before_zip = authentication_tokens[arguments.user]["public_before_zip"]
public_zip = authentication_tokens[arguments.user]["public_zip"]
public_after_zip = authentication_tokens[arguments.user]["public_after_zip"]
encrypted_private_before_zip = authentication_tokens[arguments.user]["private_before_zip"]
encrypted_private_zip = authentication_tokens[arguments.user]["private_zip"]
encrypted_private_after_zip = authentication_tokens[arguments.user]["private_after_zip"]
salt = authentication_tokens[arguments.user]["salt"]
auth_token_password = getpass(f"Enter the Password for Accessing Token : ")
try:
key, _ = generate_key(auth_token_password, salt)
auth_token = decrypt(encrypted_auth_token, key, salt).decode()
github_password = decrypt(encrypted_github_password, key, salt).decode()
private_before_zip = decrypt(encrypted_private_before_zip, key, salt).decode()
private_zip = decrypt(encrypted_private_zip, key, salt).decode()
private_after_zip = decrypt(encrypted_private_after_zip, key, salt).decode()
except Exception as err:
display('-', f"Wrong Password!")
exit(0)
users_present = os.listdir("configs")
if arguments.user not in users_present:
config_repository_status = cloneRepository(auth_token, arguments.user, "storage_config", f"configs/{arguments.user}")
if config_repository_status != 0:
createRepository(auth_token, "storage_config", False)
cloneRepository(auth_token, arguments.user, "storage_config", f"configs/{arguments.user}")
config_files = os.listdir(f"configs/{arguments.user}")
config_files.remove(".git")
config_files.sort()
if config_files != ["private_config", "public_config"]:
for file in config_files:
os.system(f"rm 'configs/{arguments.user}/{file}'")
public_config = {
"public_before_zip": public_before_zip,
"public_zip": public_zip,
"public_after_zip": public_after_zip,
}
private_config = {
"private_before_zip": private_before_zip,
"private_zip": private_zip,
"private_after_zip": private_after_zip,
}
key, config_salt = generate_key(github_password)
public_config["salt"] = config_salt
private_config["salt"] = salt
with open(f"configs/{arguments.user}/public_config", 'wb') as file:
pickle.dump(public_config, file)
with open(f"configs/{arguments.user}/private_config", 'wb') as file:
content = encrypt(pickle.dumps(private_config), key, config_salt)
file.write(content)
os.chdir(f"configs/{arguments.user}")
os.system("git add .")
os.system(f"git commit -m 'Added Configuration Files'")
os.system(f"git push origin {arguments.branch}")
else:
os.chdir(f"configs/{arguments.user}")
os.system(f"git pull")
with open(f"public_config", 'rb') as file:
public_config = pickle.load(file)
public_before_zip = public_config["public_before_zip"]
public_zip = public_config["public_zip"]
public_after_zip = public_config["public_after_zip"]
config_salt = public_config["salt"]
key, _ = generate_key(github_password, config_salt)
with open(f"private_config", 'rb') as file:
content = file.read()
private_config = pickle.loads(decrypt(content, key, config_salt))
os.chdir(str(cwd))
if arguments.view:
public_size, private_size = 0, 0
if "files" in public_config.keys():
print(f"{Fore.GREEN}Public Files{Fore.RESET}")
public_size += sum([int(public_config['files'][file]['file_size']) for file in public_config["files"]])
print('\n'.join([f"* {Fore.CYAN}{file}{Fore.RESET} : {Fore.YELLOW}{public_config['files'][file]['file_size']} Bytes{Fore.RESET}" for file in public_config["files"]]))
print(f"Total Size of Public Files = {Fore.BLUE}{public_size}{Fore.RESET}")
print()
if "files" in private_config.keys():
private_size += sum([int(private_config['files'][file]['file_size']) for file in private_config["files"]])
print(f"{Fore.GREEN}Private Files{Fore.RESET}")
print('\n'.join([f"* {Fore.CYAN}{file}{Fore.RESET} : {Fore.YELLOW}{private_config['files'][file]['file_size']} Bytes{Fore.RESET}" for file in private_config["files"]]))
print(f"Total Size of Private Files = {Fore.BLUE}{private_size}{Fore.RESET}")
print()
print(f"Total Size Files = {Fore.BLUE}{public_size + private_size}{Fore.RESET}")
if arguments.upload and os.path.exists(arguments.upload):
if os.path.isdir(arguments.upload):
display('-', f"Directories not Supported Currently!")
exit(0)
else:
private = False if arguments.private == "False" else True
zip_password = True if arguments.zip_password == "True" else False
if arguments.encryption:
if arguments.encryption.lower() == "before":
key_before = private_before_zip if private else public_before_zip
key_after = None
elif arguments.encryption.lower() == "after":
key_before = None
key_after = private_after_zip if private else public_after_zip
elif arguments.encryption.lower() == "both":
key_before = private_before_zip if private else public_before_zip
key_after = private_after_zip if private else public_after_zip
else:
key_before = None
key_after = None
else:
key_before = None
key_after = None
if zip_password:
key_zip = private_zip if private else public_zip
else:
key_zip = None
salt_before, salt_after, repositories, file_size, split_size = uploadFile(auth_token, arguments.upload, private, arguments.user, key_before, key_zip, key_after)
if salt_before == False:
display('-', f"Error Occurred : {Back.YELLOW}{salt_after}{Back.RESET}")
exit(0)
file_name = arguments.upload.split('/')[-1]
if private:
if "files" not in private_config:
private_config["files"] = {}
private_config["files"][file_name] = {"salt_before": salt_before, "salt_after": salt_after, "repositories": repositories, "before_zip": key_before, "zip": key_zip, "after_zip": key_after, "file_size": file_size, "split_size": split_size}
else:
if "files" not in public_config:
public_config["files"] = {}
public_config["files"][file_name] = {"salt_before": salt_before, "salt_after": salt_after, "repositories": repositories, "before_zip": key_before, "zip": key_zip, "after_zip": key_after, "file_size": file_size, "split_size": split_size}
with open(f"configs/{arguments.user}/public_config", 'wb') as file:
pickle.dump(public_config, file)
key, _ = generate_key(github_password, config_salt)
with open(f"configs/{arguments.user}/private_config", 'wb') as file:
content = encrypt(pickle.dumps(private_config), key, config_salt)
file.write(content)
os.chdir(f"configs/{arguments.user}")
os.system("git add .")
os.system(f"git commit -m 'Added {'Private' if private else 'Public'} File '")
os.system(f"git push origin {arguments.branch}")
elif arguments.upload:
display('-', f"File {Back.YELLOW}{arguments.upload}{Back.RESET} not found!")
exit(0)
os.chdir(str(cwd))
if arguments.download:
if "files" in public_config.keys() and arguments.download in public_config["files"].keys():
private = False
repositories = public_config["files"][arguments.download]["repositories"]
key_before = public_config["files"][arguments.download]["before_zip"]
zip_key = public_config["files"][arguments.download]["zip"]
key_after = public_config["files"][arguments.download]["after_zip"]
salt_before = public_config["files"][arguments.download]["salt_before"]
salt_after = public_config["files"][arguments.download]["salt_after"]
file_size = public_config["files"][arguments.download]["file_size"]
split_size = public_config["files"][arguments.download]["split_size"]
elif "files" in private_config.keys() and arguments.download in private_config["files"].keys():
private = True
repositories = private_config["files"][arguments.download]["repositories"]
key_before = private_config["files"][arguments.download]["before_zip"]
zip_key = private_config["files"][arguments.download]["zip"]
key_after = private_config["files"][arguments.download]["after_zip"]
salt_before = private_config["files"][arguments.download]["salt_before"]
salt_after = private_config["files"][arguments.download]["salt_after"]
file_size = private_config["files"][arguments.download]["file_size"]
split_size = private_config["files"][arguments.download]["split_size"]
else:
display('-', f"No File Named {Back.YELLOW}{arguments.download}{Back.RESET} Found")
exit(0)
free_space = shutil.disk_usage("downloads").free
if free_space < file_size * required_space_times:
display('-', f"Not Enough Space for Downloading the File {Back.MAGENTA}{arguments.download}{Back.RESET}! Size = {Back.YELLOW}{file_size} Bytes{Back.RESET}")
exit(0)
downloadFile(arguments.download, arguments.user, repositories, key_before, zip_key, key_after, salt_before, salt_after, split_size)
if arguments.delete:
if "files" in public_config.keys() and arguments.delete in public_config["files"].keys():
private = False
display('*', f"Deleting Public File : {Back.MAGENTA}{arguments.delete}{Back.RESET}")
for repository in public_config["files"][arguments.delete]["repositories"]:
deleteRepository(auth_token, arguments.user, repository)
public_config["files"].pop(arguments.delete)
elif "files" in private_config.keys() and arguments.delete in private_config["files"].keys():
private = True
display('*', f"Deleting Private File : {Back.MAGENTA}{arguments.delete}{Back.RESET}")
for repository in private_config["files"][arguments.delete]["repositories"]:
deleteRepository(auth_token, arguments.user, repository)
private_config["files"].pop(arguments.delete)
else:
display('-', f"No File Named {Back.YELLOW}{arguments.download}{Back.RESET} Found")
exit(0)
with open(f"configs/{arguments.user}/public_config", 'wb') as file:
pickle.dump(public_config, file)
key, _ = generate_key(github_password, config_salt)
with open(f"configs/{arguments.user}/private_config", 'wb') as file:
content = encrypt(pickle.dumps(private_config), key, config_salt)
file.write(content)
os.chdir(f"configs/{arguments.user}")
os.system("git add .")
os.system(f"git commit -m 'Deleted {'Private' if private else 'Public'} File '")
os.system(f"git push origin {arguments.branch}")
os.chdir(str(cwd))