Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete files #985

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions assets/assets/routers/files_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import minio
import sqlalchemy.orm
import sqlalchemy_filters.exceptions
from badgerdoc_storage import storage as bd_storage

from assets import db, exceptions, schemas, utils

Expand Down Expand Up @@ -148,16 +149,17 @@ async def delete_files(
)
continue

storage = bd_storage.get_storage(x_current_tenant)
f_name = file.original_name
minio_file_name = "files/" + str(file_id)
f_original_ext = file.original_ext
minio_ = utils.minio_utils.delete_one_from_minio(
minio_ = utils.minio_utils.delete_one_from_storage(
bucket_name, minio_file_name, storage
)
minio_originals = 1
if f_original_ext:
minio_origin_file_name = "files/origins/" + str(file_id)
minio_originals = utils.minio_utils.delete_one_from_minio(
minio_originals = utils.minio_utils.delete_one_from_storage(
bucket_name, minio_origin_file_name, storage
)
db_ = db.service.delete_file_from_db(session, file_id)
Expand Down
21 changes: 15 additions & 6 deletions assets/assets/utils/minio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ def upload_thumbnail(
return True


def delete_one_from_minio(bucket: str, obj: str, client: minio.Minio) -> bool:
def delete_one_from_storage(
bucket: str, obj: str, storage: bd_storage.BadgerDocStorage
) -> bool:
try:
objects = client.list_objects(bucket, obj, recursive=True)
names = [a.object_name for a in objects]
if not names:
objects = storage.list_objects(obj)
if not objects:
logger_.error(f"{obj} does not exist in bucket {bucket}")
return False
for name in names:
client.remove_object(bucket, name)
for name in objects:
remove_recursive(name, storage)
except urllib3.exceptions.MaxRetryError as e:
logger_.error(f"Connection error - detail: {e}")
return False
Expand All @@ -255,6 +256,14 @@ def delete_one_from_minio(bucket: str, obj: str, client: minio.Minio) -> bool:
return True


def remove_recursive(path: str, storage: bd_storage.BadgerDocStorage) -> None:
if not path.endswith("/"):
storage.remove(path)
return None
for _path in storage.list_objects(path):
remove_recursive(_path, storage)


def stream_minio(
path: str, bucket: str, storage: minio.Minio
) -> urllib3.response.HTTPResponse:
Expand Down
8 changes: 7 additions & 1 deletion lib/badgerdoc_storage/src/badgerdoc_storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,13 @@ def create_tenant_dir(self) -> bool:
return True

def remove(self, file: str) -> None:
raise NotImplementedError("Method not implemented")
bucket_name = self.__get_bucket_name()
try:
self.s3_resource.Object(bucket_name, file).delete()
except ClientError as err:
raise BadgerDocStorageError(
"Unable to delete file: %s", file
) from err


class BadgerDocAzureStorage:
Expand Down
Loading