-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatamosher.py
More file actions
160 lines (135 loc) · 5.41 KB
/
Copy pathDatamosher.py
File metadata and controls
160 lines (135 loc) · 5.41 KB
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
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import string, random, os, shutil
import time
def isCorrupted(file_path):
"""Check if the file is corrupted or otherwise fucked."""
# ToDo: implement a more reliable media file checker using ffmpeg
try:
with open(file_path, 'rb') as file:
file.read() # Try to read the file
return False # File is readable
except (IOError, OSError) as e:
print(f"File corruption detected: {e}")
return True # File is fucked
except Exception as e:
print(f"Unexpected error: {e}")
return True # CORRUPTION!
def getFileInfo(fileSource, state='checkIfExists'):
"""Check if file already exists and figure what to do about it."""
fileName, fileExtension = os.path.splitext(fileSource)
if state == 'replace':
newFileName = fileName
else:
newFileName = fileName + ' Mosh'
for i in range(1, 99):
if os.path.exists(newFileName + fileExtension):
newFileName = f"{fileName} Mosh {i}"
else:
break
return fileName, fileExtension, newFileName
def handleFile(fileSource, newFileName, operation):
"""FIle operations"""
tmp_txt = newFileName + '.txt'
shutil.copyfile(fileSource, tmp_txt)
with open(tmp_txt, 'rb') as txtFile:
fileContent = txtFile.read()
fileContent = operation(fileContent)
with open(tmp_txt, 'wb') as txtFile:
txtFile.write(fileContent)
final_path = newFileName + os.path.splitext(fileSource)[1]
os.rename(tmp_txt, final_path)
return final_path
def handle_choice(fileSource, choice):
"""Handles user choice."""
operations = {
'1': replaceMosh,
'2': insertMosh,
'3': copyPartMosh
}
if choice in operations:
return operations[choice](fileSource)
else:
print("Bad choice. Please select 1, 2, or 3.")
return None
def replaceMosh(fileSource):
"""Replace random bytes in the file with other random bytes."""
replacable = [''.join(random.sample(string.ascii_lowercase + string.digits, 2)) for _ in range(random.randint(1, 20))]
replacee = [''.join(random.sample(string.ascii_lowercase + string.digits, 2)) for _ in range(len(replacable))]
fileName, fileExtension, newFileName = getFileInfo(fileSource)
def operation(fileContent):
for old, new in zip(replacable, replacee):
fileContent = fileContent.replace(str.encode(old), str.encode(new))
return fileContent
return handleFile(fileSource, newFileName, operation)
def insertMosh(fileSource):
"""Add random characters to the file at random positions."""
fileName, fileExtension, newFileName = getFileInfo(fileSource)
def operation(fileContent):
length = len(fileContent)
for i in range(random.randint(10, 200)):
if length == 0:
position = 0
else:
position = random.randint(0, length - 1)
randomChar = ''.join(random.sample(string.ascii_lowercase + string.digits, 2)) * random.randint(200, 40000)
fileContent = fileContent[:position] + str.encode(randomChar) + fileContent[position:]
length = len(fileContent)
return fileContent
return handleFile(fileSource, newFileName, operation)
def copyPartMosh(fileSource):
"""Repeat random chunks of the file at random positions."""
fileName, fileExtension, newFileName = getFileInfo(fileSource)
def operation(fileContent):
length = len(fileContent)
for _ in range(random.randint(10, 30)):
if length <= 200:
position1 = 0
else:
position1 = random.randint(0, length - 200)
position2 = min(position1 + random.randint(200, 4000), length)
chunk = fileContent[position1:position2]
repeated_chunk = chunk * random.randint(10, 200)
insert_position = random.randint(0, length)
fileContent = fileContent[:insert_position] + repeated_chunk + fileContent[insert_position:]
length = len(fileContent)
return fileContent
return handleFile(fileSource, newFileName, operation)
def main():
root = Tk()
root.withdraw()
fileSource = askopenfilename()
root.destroy()
if not fileSource:
print("No file selected. Exiting.")
return
choice = input('1 for ReplaceMosh\n2 for InsertMosh\n3 for Random Chunk copying\n:')
newFilePath = handle_choice(fileSource, choice)
if not newFilePath:
return
while True:
if isCorrupted(newFilePath):
print("File is corrupted. Retrying...")
try:
os.remove(newFilePath) # Delete the corrupted mosh file
except Exception as e:
print(f"Error deleting file: {e}")
return # Fail gracefully if must
# retry: create a new mosh version from original
newFilePath = handle_choice(fileSource, choice)
if not newFilePath:
return
time.sleep(0.5)
else:
print("File is not corrupted. We did it bois.")
break # Exit the program if the file is not corrupted
if __name__ == "__main__":
main()
'''
if isCorrupted(newFileName + '.jpg') == False:
while isCorrupted(newFileName + '.jpg') == False:
mosh(fileSource)
time.sleep(3)
else:
print('yay')
'''