Skip to content

Commit

Permalink
Daemon.jsonrpc_file_delete: new --what parameter to control what to…
Browse files Browse the repository at this point in the history
… delete

The `--what` parameter can be used to specify what to delete:
the media file only, the blobs only, or both, deleting completely
the downloaded stream.
```
lbrynet file delete --claim_name=my-claim --what=file
lbrynet file delete --claim_name=my-claim --what=blobs
lbrynet file delete --claim_name=my-claim --what=both
```

If no argument is used, this is equivalent to `--what=blobs`.
```
lbrynet file delete --claim_name=my-claim
```

The old argument, `--delete_from_download_dir`, works as `--what=both`.
```
lbrynet file delete --claim_name=my-claim --delete_from_download_dir
lbrynet file delete --claim_name=my-claim --what=both
```
  • Loading branch information
belikor committed Sep 17, 2021
1 parent 561566e commit f665edb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
40 changes: 29 additions & 11 deletions lbry/extras/daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2120,32 +2120,35 @@ async def jsonrpc_file_set_status(self, status, **kwargs):
return msg

@requires(FILE_MANAGER_COMPONENT)
async def jsonrpc_file_delete(self, delete_from_download_dir=False, delete_all=False, **kwargs):
async def jsonrpc_file_delete(self, delete_from_download_dir=False, delete_all=False, what=None, **kwargs):
"""
Delete a LBRY file
Delete the blobs and file from a downloaded LBRY claim
Usage:
file_delete [--delete_from_download_dir] [--delete_all] [--sd_hash=<sd_hash>] [--file_name=<file_name>]
[--stream_hash=<stream_hash>] [--rowid=<rowid>] [--claim_id=<claim_id>] [--txid=<txid>]
[--nout=<nout>] [--claim_name=<claim_name>] [--channel_claim_id=<channel_claim_id>]
[--channel_name=<channel_name>]
[--channel_name=<channel_name>] [--what=<what>]
Options:
--delete_from_download_dir : (bool) delete file from download directory,
instead of just deleting blobs
together with the blobs
--delete_all : (bool) if there are multiple matching files,
allow the deletion of multiple files.
Otherwise do not delete anything.
--sd_hash=<sd_hash> : (str) delete by file sd hash
--file_name=<file_name> : (str) delete by file name in downloads folder
--file_name=<file_name> : (str) delete by file name in downloads folder
--stream_hash=<stream_hash> : (str) delete by file stream hash
--rowid=<rowid> : (int) delete by file row id
--claim_id=<claim_id> : (str) delete by file claim id
--txid=<txid> : (str) delete by file claim txid
--nout=<nout> : (int) delete by file claim nout
--claim_name=<claim_name> : (str) delete by file claim name
--channel_claim_id=<channel_claim_id> : (str) delete by file channel claim id
--channel_name=<channel_name> : (str) delete by file channel claim name
--channel_name=<channel_name> : (str) delete by file channel claim name
--what=<what> : (str) choose to delete 'file', 'blobs', or 'both';
if only the file is deleted, the blobs can still share
the content in the network
Returns:
(bool) true if deletion was successful
Expand All @@ -2155,20 +2158,35 @@ async def jsonrpc_file_delete(self, delete_from_download_dir=False, delete_all=F

if len(streams) > 1:
if not delete_all:
log.warning("There are %i files to delete, use narrower filters to select one",
log.warning("There are %i streams to delete, use narrower filters to select one",
len(streams))
return False
else:
log.warning("Deleting %i files",
log.warning("Deleting %i streams",
len(streams))

if not streams:
log.warning("There is no file to delete")
log.warning("There is no stream to delete")
return False
else:
# Compatibility for the old behavior when `what` is not provided
# and the only options are `delete_from_download_dir=True` or `False`
# TODO: deprecate `delete_from_download_dir`
if not what:
if delete_from_download_dir:
what = "both"
else:
what = "blobs"
for stream in streams:
message = f"Deleted file {stream.file_name}"
await self.file_manager.delete(stream, delete_file=delete_from_download_dir)
if what == "file":
message = f"Deleted file '{stream.file_name}' (only file)"
await self.file_manager.delete(stream, delete_file=True, delete_blobs=False)
elif what == "blobs":
message = f"Deleted file '{stream.file_name}' (only blobs)"
await self.file_manager.delete(stream, delete_file=False, delete_blobs=True)
elif what == "both":
message = f"Deleted file '{stream.file_name}' (both file and blobs)"
await self.file_manager.delete(stream, delete_file=True, delete_blobs=True)
log.info(message)
result = True
return result
Expand Down
4 changes: 2 additions & 2 deletions lbry/file/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,6 @@ def get_filtered(self, *args, **kwargs) -> typing.List[ManagedDownloadSource]:
"""
return sum((manager.get_filtered(*args, **kwargs) for manager in self.source_managers.values()), [])

async def delete(self, source: ManagedDownloadSource, delete_file=False):
async def delete(self, source: ManagedDownloadSource, delete_file=False, delete_blobs=True):
for manager in self.source_managers.values():
await manager.delete(source, delete_file)
await manager.delete(source, delete_file, delete_blobs)
6 changes: 4 additions & 2 deletions lbry/file/source_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ async def create(self, file_path: str, key: Optional[bytes] = None,
iv_generator: Optional[typing.Generator[bytes, None, None]] = None) -> ManagedDownloadSource:
raise NotImplementedError()

async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False):
self.remove(source)
async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False,
delete_source: Optional[bool] = True):
if delete_source:
self.remove(source)
if delete_file and source.output_file_exists:
os.remove(source.full_path)

Expand Down
20 changes: 11 additions & 9 deletions lbry/stream/stream_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,19 @@ async def create(self, file_path: str, key: Optional[bytes] = None,
self.reflect_stream(stream)
return stream

async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False):
async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False,
delete_source: Optional[bool] = True):
if not isinstance(source, ManagedStream):
return
if source.identifier in self.running_reflector_uploads:
self.running_reflector_uploads[source.identifier].cancel()
source.stop_tasks()
if source.identifier in self.streams:
del self.streams[source.identifier]
blob_hashes = [source.identifier] + [b.blob_hash for b in source.descriptor.blobs[:-1]]
await self.blob_manager.delete_blobs(blob_hashes, delete_from_db=False)
await self.storage.delete_stream(source.descriptor)
if delete_source:
if source.identifier in self.running_reflector_uploads:
self.running_reflector_uploads[source.identifier].cancel()
source.stop_tasks()
if source.identifier in self.streams:
del self.streams[source.identifier]
blob_hashes = [source.identifier] + [b.blob_hash for b in source.descriptor.blobs[:-1]]
await self.blob_manager.delete_blobs(blob_hashes, delete_from_db=False)
await self.storage.delete_stream(source.descriptor)
if delete_file and source.output_file_exists:
os.remove(source.full_path)

Expand Down
5 changes: 3 additions & 2 deletions lbry/torrent/torrent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def stop(self):
super().stop()
log.info("finished stopping the torrent manager")

async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False):
await super().delete(source, delete_file)
async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False,
delete_source: Optional[bool] = True):
await super().delete(source, delete_file, delete_source)
self.torrent_session.remove_torrent(source.identifier, delete_file)

async def create(self, file_path: str, key: Optional[bytes] = None,
Expand Down

0 comments on commit f665edb

Please sign in to comment.