- Added functions in api and cid store to delete nodes and get files

This commit is contained in:
landandair
2025-04-16 17:44:39 -05:00
parent 0ab31cefa4
commit d0c360e6bb
3 changed files with 76 additions and 3 deletions

View File

@@ -272,10 +272,10 @@ class CidStore:
if node:
if node.type == Cid.TYPE_SRC:
node_dict[hash] = node.dump()
print(node_dict)
return node_dict
def is_storage_hash(self, hash_str):
"""Checks to see if its a chunk hash"""
node = self.get_node_obj(hash_str)
if node:
if node.type == Cid.TYPE_CHUNK:
@@ -297,6 +297,7 @@ class CidStore:
return parent_hashes
def get_children(self, node_hash):
"""Gets all children associated with node"""
child_hashes = []
node = self.get_node_obj(node_hash)
if node:
@@ -313,6 +314,35 @@ class CidStore:
data_hash = self.get_data_hash(b'', children_data, include_source=False)
return data_hash
def remove_hash(self, hash_id):
node = self.get_node_obj(hash_id)
if node:
if node.type != node.TYPE_SRC:
self.index.pop(hash_id)
self.clean_data()
return True
else:
logger.warning("User attempted to delete root node")
return False
def clean_data(self):
for hash_id in tuple(self.index.keys()):
node = self.get_node_obj(hash_id)
if node:
if node.parent not in self.index and node.type != node.TYPE_SRC:
if node.type == node.TYPE_CHUNK:
path = self.get_data_path(hash_id)
os.remove(path)
self.remove_hash(hash_id)
def clean_hash_data(self, hash_id):
node = self.get_node_obj(hash_id)
if node:
if node.parent not in self.index:
for child in self.get_children(hash_id):
self.clean_hash_data(child)
self.remove_hash(hash_id)
@dataclass
class Cid:

View File

@@ -1,4 +1,5 @@
from threading import Thread
import io
import flask
from flask_classful import FlaskView, request, route
@@ -24,12 +25,20 @@ class RNFSView(FlaskView):
@route('/getNode/<id>', methods=['GET'], endpoint='getNode')
def get_node(self, id=None):
"""Return node information associated with hash provided TODO:Make this a file download if the data is not json"""
"""Return node information associated with hash provided"""
if id == 'root':
id = None
print(id)
return self.info.get_node_info(id)
@route('/getFile/<id>', methods=['GET'], endpoint='getFile')
def get_file(self, id):
"""Get file node and return the associated data and name"""
data = self.info.get_file_data(id)
if data:
bytes_io = io.BytesIO(data)
return flask.send_file(bytes_io, download_name=self.info.get_node_name(id), as_attachment=True)
return data
@route('/getSrc', methods=['GET'], endpoint='getSrc')
def get_src(self):
"""Get source dest hash useful for knowing which data tree you can add to"""
@@ -90,6 +99,15 @@ class RNFSView(FlaskView):
</form>
''' # Html for making basic file upload
@route('/deleteNode/<id>', methods=['GET'], endpoint='deleteNode')
def remove_node(self, id):
"""Remove a node hash from the data store"""
ret = self.info.delete_node(id)
if ret:
return 'success'
else:
return 'Not Found'
def start_server_thread(server_info):
t = Thread(target=start_server, args=[server_info], daemon=True)

View File

@@ -35,6 +35,28 @@ class ServerCommandState:
info = dumps({})
return info
def get_file_data(self, node_hash):
node = self.cid_store.get_node_obj(node_hash)
if node and self.cid_store.check_is_stored(node_hash):
if node.type == node.TYPE_FILE:
data = b''
for child in node.children:
data_chunk = self.cid_store.get_node(child)
if data_chunk:
data += data_chunk
if node.hash == self.cid_store.get_data_hash(node.parent, data, include_source=True):
return data
else:
logger.warning(f"File hash for {node.name} does not match data")
else:
self.rns_interface.make_hash_desire_request(node_hash)
return None
def get_node_name(self, node_hash):
node = self.cid_store.get_node_obj(node_hash)
if node:
return node.name
def get_src_dest(self):
return self.cid_store.source_hash
@@ -44,6 +66,9 @@ class ServerCommandState:
def make_dir(self, name, parent=None):
self.cid_store.add_dir(name, parent)
def delete_node(self, id):
return self.cid_store.remove_hash(id)
def updated_hash_callback(self, node_hash):
"""Called when the cid storage has added any nodes from a dictionary(json file)"""
node = self.cid_store.get_node_obj(node_hash)