@@ -2453,6 +2453,7 @@ def __init__(
2453
2453
logger .debug ('Decrypting efile_path with the MainKey' )
2454
2454
self ._file_path = AES (self ._mainkey ).decrypt (self ._efile_path ) # pylint: disable=no-member
2455
2455
self ._file_path = Path (self ._file_path .decode ())
2456
+ self ._original_file_path = self ._file_path
2456
2457
else :
2457
2458
logger .warning (
2458
2459
f'''We can\' t decrypt real file path of ID{ self ._id } because '''
@@ -2600,7 +2601,7 @@ async def refresh_metadata(
2600
2601
metadata. You can use it as alternative to
2601
2602
the "drb" argument here.
2602
2603
2603
- _updated_metadata (``bytes ``, optional):
2604
+ _updated_metadata (``str ``, optional):
2604
2605
Updated metadata by itself. This is for
2605
2606
internal use, specify only ``drb``.
2606
2607
@@ -2634,6 +2635,21 @@ async def refresh_metadata(
2634
2635
# all updated metadata from the LocalBox
2635
2636
_updated_metadata , updates = None , {}
2636
2637
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
+ )
2637
2653
# =============================================== #
2638
2654
2639
2655
logger .debug (
@@ -2655,7 +2671,7 @@ async def refresh_metadata(
2655
2671
mainkey = self ._mainkey if self ._mainkey else self ._lb ._mainkey
2656
2672
self ._file_path = Path (AES (mainkey ).decrypt (v ).decode ())
2657
2673
else :
2658
- logger .debug (
2674
+ logger .warning (
2659
2675
'''Updated metadata contains efile_path, however, '''
2660
2676
'''DecryptedLocalBoxFile that you trying to update '''
2661
2677
'''doesn\' t have a MainKey and wasn\' t decrypted with '''
@@ -2739,9 +2755,9 @@ async def update_metadata(
2739
2755
if 'efile_path' in changes :
2740
2756
raise ValueError ('The "changes" should not contain efile_path' )
2741
2757
2742
- changes = changes .copy ()
2758
+ current_changes = changes .copy ()
2743
2759
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 } ...' )
2745
2761
2746
2762
dlb = dlb if dlb else self ._lb
2747
2763
try :
@@ -2754,21 +2770,33 @@ async def update_metadata(
2754
2770
except (ValueError , TypeError ):
2755
2771
updates = {}
2756
2772
2757
- new_file_path = changes .pop ('file_path' , None )
2773
+ new_file_path = current_changes .pop ('file_path' , None )
2758
2774
if isinstance (new_file_path , bytes ):
2759
2775
new_file_path = new_file_path .decode ()
2760
2776
2761
2777
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 ))
2763
2779
2764
2780
await dlb ._tgbox_db .FILES .execute ((
2765
2781
'UPDATE FILES SET PPATH_HEAD=? WHERE ID=?' ,
2766
- (directory .part_id , self ._id )
2782
+ (self . _directory .part_id , self ._id )
2767
2783
))
2768
2784
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
+ ))
2770
2798
2771
- updates .update (changes )
2799
+ updates .update (current_changes )
2772
2800
2773
2801
for k ,v in tuple (updates .items ()):
2774
2802
if not v :
@@ -2785,7 +2813,7 @@ async def update_metadata(
2785
2813
2786
2814
if drb :
2787
2815
drbf = await drb .get_file (self ._id )
2788
- await drbf .update_metadata (changes )
2816
+ await drbf .update_metadata (changes , dlb = dlb )
2789
2817
2790
2818
def get_sharekey (self , reqkey : Optional [RequestKey ] = None ) -> ShareKey :
2791
2819
"""
0 commit comments