Skip to content

Commit 34c9752

Browse files
committed
local_gdrive moves cached files to trash
[skip ci]
1 parent 1ec1e0b commit 34c9752

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

scripts/local_gdrive.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ def row_dict_to_api_dict(self, table_row: Dict[str, Any]) -> Dict[str, Any]:
466466
'me': True if owner['id'] == 1 else False,
467467
}]
468468
del ret['owner']
469+
if 'trashed_time' in ret: # even if it's None
470+
if ret['trashed_time']:
471+
ret['trashedTime'] = ret['trashed_time']
472+
ret['trashed'] = True
473+
del ret['trashed_time']
469474
return ret
470475

471476
@locked
@@ -705,8 +710,8 @@ def get_cache_path_for_file(self, file: dict) -> Path | None:
705710
if not isinstance(self.file_cache_dir, Path):
706711
return None
707712

708-
if 'trashed_time' in file:
709-
rm_date = file['trashed_time'] or file['modifiedTime']
713+
if file.get('trashedTime') or file.get('trashed'):
714+
rm_date = file.get('trashedTime') or file['modifiedTime']
710715
rm_date = datetime.fromisoformat(rm_date)
711716
return self.file_cache_dir / 'trash' / str(rm_date.year) / f"{rm_date.month:02d}" / file['name']
712717

@@ -790,7 +795,7 @@ def get_cache_path_for_md5(self, hashval: str) -> Path | None:
790795
if not remote_files:
791796
remote_files = self.get_trashed_items_with_md5(hashval)
792797
if remote_files:
793-
rm_date = max(f['trashed_time'] or f['modifiedTime'] for f in remote_files)
798+
rm_date = max(f['trashedTime'] or f['modifiedTime'] for f in remote_files)
794799
rm_date = datetime.fromisoformat(rm_date)
795800
return self.file_cache_dir / 'trash' / str(rm_date.year) / f"{rm_date.month:02d}" / remote_files[0]['name']
796801
return None
@@ -824,7 +829,7 @@ def trash_file(self, file_id: str):
824829
self.conn.commit()
825830

826831
@locked
827-
def _move_to_trash(self, file_id: str, trashed_time: str = None):
832+
def _move_to_trash(self, file_id: str, trashed_time: str = None) -> Dict[str, Any]:
828833
# If we get a removal event from the API for a file already in the trash,
829834
# only add the timestamp to the trash table if the item had a NULL trashed time before.
830835
self.cursor.execute("SELECT trashed_time FROM trashed_drive_items WHERE id = ?", (file_id,))
@@ -844,11 +849,20 @@ def _move_to_trash(self, file_id: str, trashed_time: str = None):
844849
modified_time, size, owner, md5_checksum, shortcut_target
845850
FROM drive_items
846851
WHERE id = ?
852+
RETURNING *
847853
""", (file_id,))
854+
file = self.row_dict_to_api_dict(dict(self.cursor.fetchone()))
855+
before_cache_path = self.get_cache_path_for_file(file)
848856
self.cursor.execute("DELETE FROM drive_items WHERE id = ?", (file_id,))
849-
850857
if trashed_time:
851858
self.cursor.execute("UPDATE trashed_drive_items SET trashed_time = ? WHERE id = ?", (trashed_time, file_id))
859+
file['trashedTime'] = trashed_time
860+
file['trashed'] = True
861+
if before_cache_path and before_cache_path.exists():
862+
new_cache_path = self.get_cache_path_for_file(file)
863+
assert new_cache_path and new_cache_path != before_cache_path
864+
before_cache_path.rename(new_cache_path)
865+
return file
852866

853867
def move_file(self, file_id: str, folder: str, previous_parents=None, verbose=True):
854868
folder = gdrive_base.folderlink_to_id(folder) if folder.startswith("http") else folder

0 commit comments

Comments
 (0)