Skip to content

Commit dd845a9

Browse files
committed
Fix bugs in Update Metadata, restore original PPATH_HEAD
1 parent f8bcbc9 commit dd845a9

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

tgbox/api/local.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,7 @@ def __init__(
24532453
logger.debug('Decrypting efile_path with the MainKey')
24542454
self._file_path = AES(self._mainkey).decrypt(self._efile_path) # pylint: disable=no-member
24552455
self._file_path = Path(self._file_path.decode())
2456+
self._original_file_path = self._file_path
24562457
else:
24572458
logger.warning(
24582459
f'''We can\'t decrypt real file path of ID{self._id} because '''
@@ -2600,7 +2601,7 @@ async def refresh_metadata(
26002601
metadata. You can use it as alternative to
26012602
the "drb" argument here.
26022603
2603-
_updated_metadata (``bytes``, optional):
2604+
_updated_metadata (``str``, optional):
26042605
Updated metadata by itself. This is for
26052606
internal use, specify only ``drb``.
26062607
@@ -2634,6 +2635,21 @@ async def refresh_metadata(
26342635
# all updated metadata from the LocalBox
26352636
_updated_metadata, updates = None, {}
26362637

2638+
# We also restore original PPATH_HEAD
2639+
self._file_path = self._original_file_path
2640+
#
2641+
if isinstance(self._lb, DecryptedLocalBox):
2642+
self._directory = await self._lb._make_local_path(self._file_path)
2643+
2644+
await self._lb._tgbox_db.FILES.execute((
2645+
'UPDATE FILES SET PPATH_HEAD=? WHERE ID=?',
2646+
(self._directory.part_id, self._id)
2647+
))
2648+
else:
2649+
logger.warning(
2650+
f'''We can not restore the original PPATH_HEAD of the ID{self._id} '''
2651+
'''because it wasn\'t decrypted with the DecryptedLocalBox.'''
2652+
)
26372653
# =============================================== #
26382654

26392655
logger.debug(
@@ -2655,7 +2671,7 @@ async def refresh_metadata(
26552671
mainkey = self._mainkey if self._mainkey else self._lb._mainkey
26562672
self._file_path = Path(AES(mainkey).decrypt(v).decode())
26572673
else:
2658-
logger.debug(
2674+
logger.warning(
26592675
'''Updated metadata contains efile_path, however, '''
26602676
'''DecryptedLocalBoxFile that you trying to update '''
26612677
'''doesn\'t have a MainKey and wasn\'t decrypted with '''
@@ -2739,9 +2755,9 @@ async def update_metadata(
27392755
if 'efile_path' in changes:
27402756
raise ValueError('The "changes" should not contain efile_path')
27412757

2742-
changes = changes.copy()
2758+
current_changes = changes.copy()
27432759

2744-
logger.debug(f'Applying changes {changes} to the ID{self._id}...')
2760+
logger.debug(f'Applying changes {current_changes} to the ID{self._id}...')
27452761

27462762
dlb = dlb if dlb else self._lb
27472763
try:
@@ -2754,21 +2770,33 @@ async def update_metadata(
27542770
except (ValueError, TypeError):
27552771
updates = {}
27562772

2757-
new_file_path = changes.pop('file_path', None)
2773+
new_file_path = current_changes.pop('file_path', None)
27582774
if isinstance(new_file_path, bytes):
27592775
new_file_path = new_file_path.decode()
27602776

27612777
if new_file_path:
2762-
directory = await dlb._make_local_path(Path(new_file_path))
2778+
self._directory = await dlb._make_local_path(Path(new_file_path))
27632779

27642780
await dlb._tgbox_db.FILES.execute((
27652781
'UPDATE FILES SET PPATH_HEAD=? WHERE ID=?',
2766-
(directory.part_id, self._id)
2782+
(self._directory.part_id, self._id)
27672783
))
27682784
efile_path = AES(dlb._mainkey).encrypt(new_file_path.encode())
2769-
changes['efile_path'] = efile_path
2785+
current_changes['efile_path'] = efile_path
2786+
2787+
elif new_file_path is not None:
2788+
# User requested us to remove updated file
2789+
# path from the LocalBox, so we need to
2790+
# restore the original PPATH_HEAD
2791+
self._file_path = self._original_file_path
2792+
self._directory = await dlb._make_local_path(self._original_file_path)
2793+
2794+
await dlb._tgbox_db.FILES.execute((
2795+
'UPDATE FILES SET PPATH_HEAD=? WHERE ID=?',
2796+
(self._directory.part_id, self._id)
2797+
))
27702798

2771-
updates.update(changes)
2799+
updates.update(current_changes)
27722800

27732801
for k,v in tuple(updates.items()):
27742802
if not v:
@@ -2785,7 +2813,7 @@ async def update_metadata(
27852813

27862814
if drb:
27872815
drbf = await drb.get_file(self._id)
2788-
await drbf.update_metadata(changes)
2816+
await drbf.update_metadata(changes, dlb=dlb)
27892817

27902818
def get_sharekey(self, reqkey: Optional[RequestKey] = None) -> ShareKey:
27912819
"""

tgbox/api/remote.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,17 +2047,17 @@ async def update_metadata(
20472047
if 'file_path' in changes and not dlb:
20482048
raise ValueError('You can\'t change file_path without specifying dlb!')
20492049

2050-
changes = changes.copy()
2050+
current_changes = changes.copy()
20512051

2052-
logger.debug(f'Applying changes {changes} to the ID{self._id}...')
2052+
logger.debug(f'Applying changes {current_changes} to the ID{self._id}...')
20532053
try:
20542054
message_caption = urlsafe_b64decode(self._message.message)
20552055
updates = AES(self._filekey).decrypt(message_caption)
20562056
updates = PackedAttributes.unpack(updates)
20572057
except (ValueError, TypeError):
20582058
updates = {}
20592059

2060-
new_file_path = changes.pop('file_path', None)
2060+
new_file_path = current_changes.pop('file_path', None)
20612061
if isinstance(new_file_path, bytes):
20622062
new_file_path = new_file_path.decode()
20632063

@@ -2069,15 +2069,15 @@ async def update_metadata(
20692069
(directory.part_id, self._id)
20702070
))
20712071
efile_path = AES(dlb._mainkey).encrypt(new_file_path.encode())
2072-
changes['efile_path'] = efile_path
2072+
current_changes['efile_path'] = efile_path
20732073

20742074
# If new_file_path is empty string then it's should be
20752075
# a request to remove updated file_path attribute
20762076
# from the RemoteBox file and restore default
20772077
if new_file_path is not None:
20782078
updates.pop('efile_path', None)
20792079

2080-
updates.update(changes)
2080+
updates.update(current_changes)
20812081

20822082
for k,v in tuple(updates.items()):
20832083
if not v:

0 commit comments

Comments
 (0)