56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
BASE_DIRECTORY = './embedding_data'
|
||
|
|
|
||
|
|
class FileSystemStorage:
|
||
|
|
def __init__(self, base_directory=BASE_DIRECTORY):
|
||
|
|
self.base_dir = base_directory
|
||
|
|
|
||
|
|
def create_group(self, auth_token, group_id):
|
||
|
|
path = os.path.join(self.base_dir, group_id)
|
||
|
|
if os.path.exists(path):
|
||
|
|
raise FileExistsError(f"{group_id}")
|
||
|
|
else:
|
||
|
|
try:
|
||
|
|
os.makedirs(path)
|
||
|
|
except OSError as e:
|
||
|
|
logger.error(f"Error creating directory {path}: {e}")
|
||
|
|
raise
|
||
|
|
|
||
|
|
def get(self, auth_token, group_id, item_id):
|
||
|
|
try:
|
||
|
|
path = os.path.join(self.base_dir, group_id, item_id)
|
||
|
|
with open(path, 'rb') as f:
|
||
|
|
return f.read()
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error reading from {path}: {e}")
|
||
|
|
raise
|
||
|
|
|
||
|
|
def save(self, auth_token, group_id, item_id, content):
|
||
|
|
try:
|
||
|
|
path = os.path.join(self.base_dir, group_id, item_id)
|
||
|
|
with open(path, 'wb') as f:
|
||
|
|
f.write(content)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error saving to {path}: {e}")
|
||
|
|
raise
|
||
|
|
|
||
|
|
def remove(slef, auth_token, group_id, item_id):
|
||
|
|
try:
|
||
|
|
path = os.path.join(self.base_dir, group_id, item_id)
|
||
|
|
os.remove(path)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error remove item {path}: {e}")
|
||
|
|
raise
|
||
|
|
|
||
|
|
def remove_group(self, auth_token, group_id):
|
||
|
|
try:
|
||
|
|
path = os.path.join(self.base_dir, group_id)
|
||
|
|
shutil.rmtree(path)
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"Error remove group {path}: {e}")
|
||
|
|
raise
|