diff --git a/__init__.py b/__init__.py index 8a84135..f2582e8 100644 --- a/__init__.py +++ b/__init__.py @@ -11,10 +11,11 @@ import tempfile import unittest class Storage: - def __init__(self, rootpath, files, piecelen): + def __init__(self, rootpath, files, piecelen, encoding='us-ascii'): self._rootpath = pathlib.Path(rootpath) self._files = files self._piecelen = piecelen + self._encoding = encoding self._buildindex() @@ -30,7 +31,7 @@ class Storage: try: curfile = next(files) fname = pathlib.Path( - *(x.decode('us-ascii') for x in + *(x.decode(self._encoding) for x in curfile['path'])) curfilepath = self._rootpath / fname except StopIteration: @@ -61,11 +62,14 @@ def validate(torrent, basedir): basedir = pathlib.Path(basedir) - print(repr(torrent)) + try: + encoding = torrent['encoding'].decode('us-ascii') + except KeyError: + encoding = 'us-ascii' - torrentdir = basedir / info['name'].decode('us-ascii') + torrentdir = basedir / info['name'].decode(encoding) - stor = Storage(torrentdir, info['files'], info['piece length']) + stor = Storage(torrentdir, info['files'], info['piece length'], encoding) pieces = info['pieces'] for num, i in enumerate(pieces[x:x+20] for x in range(0, len(pieces), @@ -124,6 +128,21 @@ class _TestCases(unittest.TestCase): validate(self.torrent, self.basetempdir) + # encoded names + + sd = self.basetempdir / 'thai' + sd.mkdir() + + self.make_files(sd, { 'thai - สวัสดี.txt': b'hello\n' + }) + + tor = importlib.resources.files(__name__) + tor = tor / 'fixtures' / 'thai.torrent' + with tor.open('rb') as fp: + torrent = bencode.bdecode(fp.read()) + + validate(torrent, self.basetempdir) + def test_verification(self): # Testing for "missing" files # piece size 2 (aka 4 bytes)