Browse Source

Continue after par2 renaming (#1921)

* record new files generated based on par2

* record new files generated based on par2

* test first par2 based renaming, then deobfuscate obfuscated names

* remove commented-out line

* corrected contents zip-file

* try again, github

* try again, github
pull/1923/head
Sander 4 years ago
committed by GitHub
parent
commit
1f4c7239d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      sabnzbd/deobfuscate_filenames.py
  2. BIN
      tests/data/deobfuscate_par2_based/20mb_with_par2_package.zip
  3. 26
      tests/test_deobfuscate_filenames.py

21
sabnzbd/deobfuscate_filenames.py

@ -43,7 +43,7 @@ MIN_FILE_SIZE = 10 * 1024 * 1024
def decode_par2(parfile):
"""Parse a par2 file and rename files listed in the par2 to their real name"""
"""Parse a par2 file and rename files listed in the par2 to their real name. Resturn list of generated files"""
# Check if really a par2 file
if not is_parfile(parfile):
logging.info("Par2 file %s was not really a par2 file")
@ -55,7 +55,7 @@ def decode_par2(parfile):
# Parse all files in the folder
dirname = os.path.dirname(parfile)
result = False
new_files = [] # list of new files generated
for fn in os.listdir(dirname):
filepath = os.path.join(dirname, fn)
# Only check files
@ -68,9 +68,10 @@ def decode_par2(parfile):
if file_md5of16k in md5of16k:
new_path = os.path.join(dirname, md5of16k[file_md5of16k])
# Make sure it's a unique name
renamer(filepath, get_unique_filename(new_path))
result = True
return result
unique_filename = get_unique_filename(new_path)
renamer(filepath, unique_filename)
new_files.append(unique_filename)
return new_files
def is_probably_obfuscated(myinputfilename):
@ -154,16 +155,14 @@ def deobfuscate_list(filelist, usefulname):
for par2_file in par2_files:
# Analyse data and analyse result
logging.debug("Deobfuscate par2: handling %s", par2_file)
if decode_par2(par2_file):
new_files = decode_par2(par2_file)
if new_files:
logging.debug("Deobfuscate par2 repair/verify finished")
par2_renaming_done = True
filelist += new_files
filelist = [f for f in filelist if os.path.isfile(f)]
else:
logging.debug("Deobfuscate par2 repair/verify did not find anything to rename")
if par2_renaming_done:
# TODO really needed to quit here? We could also proceed with the other actions. Anyway:
return # done
# let's see if there are files with uncommon/unpopular (so: obfuscated) extensions
# if so, let's give them a better extension based on their internal content/info
# Example: if 'kjladsflkjadf.adsflkjads' is probably a PNG, rename to 'kjladsflkjadf.adsflkjads.png'

BIN
tests/data/deobfuscate_par2_based/20mb_with_par2_package.zip

Binary file not shown.

26
tests/test_deobfuscate_filenames.py

@ -21,6 +21,7 @@ Testing SABnzbd deobfuscate module
import random
import shutil
import zipfile
from sabnzbd.deobfuscate_filenames import *
from tests.testhelper import *
@ -324,3 +325,28 @@ class TestDeobfuscateFinalResult:
# Rename back
os.rename(test_output, test_input)
assert os.path.exists(test_input)
def test_deobfuscate_par2_plus_deobfuscate(self):
# test for first par2 based renaming, then deobfuscate obfuscated names
work_dir = os.path.join(SAB_DATA_DIR, "testdir" + str(random.randint(10000, 99999)))
os.mkdir(work_dir)
source_zip_file = os.path.join(SAB_DATA_DIR, "deobfuscate_par2_based", "20mb_with_par2_package.zip")
with zipfile.ZipFile(source_zip_file, "r") as zip_ref:
zip_ref.extractall(work_dir)
assert os.path.isfile(os.path.join(work_dir, "rename.par2")) # the par2 that will do renaming
assert os.path.isfile(os.path.join(work_dir, "aaaaaaaaaaa")) # a 20MB no-name file ...
list_of_files = []
for (dirpath, dirnames, filenames) in os.walk(work_dir):
list_of_files += [os.path.join(dirpath, file) for file in filenames]
# deobfuscate will do:
# first par2 based renaming aaaaaaaaaaa to twentymb.bin,
# then deobfuscate twentymb.bin to the job name (with same extension)
deobfuscate_list(list_of_files, "My Great Download")
assert os.path.isfile(os.path.join(work_dir, "My Great Download.bin")) # the twentymb.bin should be renamed
assert not os.path.isfile(os.path.join(work_dir, "twentymb.bin")) # should be gone
shutil.rmtree(work_dir)

Loading…
Cancel
Save