rclone_python icon indicating copy to clipboard operation
rclone_python copied to clipboard

Feature Request: Add callback to to allow client to do file operation before upload.

Open pete312 opened this issue 2 years ago • 0 comments

Nice utility.

Suggestion: A client that is using the copy operation, might want to do some logistics or translations. A good feature would be to allow a this as a callback on event a pre and post transfer event.

Example: We might want a zip translation that the client would write and the client may choose to remove the origional file on successful transfer.

from pathlib import Path
import zipfile

def zipit(*args,  **kwargs) -> Tuple[Path, Path]:  ## returns the translated filefrom and fileto filepaths.

   event = kwargs.get('event')   #  rclone_python could chose to issue more events than what is covered here.
   operation = kwargs.get('operation')   #  this is the client name which has been defined as 'gdrive_upload' 
   if operation not in ['gdrive_upload', 'upload']:
       return None  # zipit chooses not to handle this one
   
   # the assumption here is that these are Path objects
   from_file:Path = kwargs.get('current_from_file') 
   to_file:Path = kwargs.get('current_to_file') 

   if event == 'before_transfer':
        with zipfile.ZipFile(f'{from_file}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
           zipf.write(from_file.read_bytes())
        # since both the from and to path are different (the both have added a .zip extension ) the new file to transfer is returned. 
        return  Path(f"{from_file}.zip"), Path(f"{to_file}.zip")   
   elif event == 'after_transfer':
        assert str(from_file).endswith('.zip'), f"expect a zip, but got {from_file}"
        from_file.unlink()
        
   

rclone.copy( fromdir,  todir,  on_transfer=dict(callback=zipit, operation='gdrive_upload') )

# this one is not handled by zipit, but may be in future. 
rclone.copy( todir, fromdir  on_transfer=dict(callback=zipit, operation='gdrive_download') ) 

Here a client is translating the source file into a zip then removing that zip after the upload but has not got around to writing the unzip part of it.

pete312 avatar Feb 03 '24 17:02 pete312