|
57 | 57 | from geonode.base.models import Configuration, ExtraMetadata, LinkedResource |
58 | 58 | from geonode.thumbs.exceptions import ThumbnailError |
59 | 59 | from geonode.thumbs.thumbnails import create_thumbnail |
60 | | -from geonode.thumbs.utils import _decode_base64, BASE64_PATTERN |
| 60 | +from geonode.thumbs.utils import _decode_base64, BASE64_PATTERN, remove_thumb |
61 | 61 | from geonode.groups.conf import settings as groups_settings |
62 | 62 | from geonode.base.models import HierarchicalKeyword, Region, ResourceBase, TopicCategory, ThesaurusKeyword |
63 | 63 | from geonode.base.api.filters import ( |
@@ -721,6 +721,60 @@ def set_thumbnail_from_bbox(self, request, resource_id, *args, **kwargs): |
721 | 721 | logger.error(e) |
722 | 722 | return Response(data={"message": e.args[0], "success": False}, status=500, exception=True) |
723 | 723 |
|
| 724 | + @extend_schema( |
| 725 | + methods=["post"], |
| 726 | + responses={200}, |
| 727 | + description="API endpoint allowing to delete a thumbnail for an existing dataset.", |
| 728 | + ) |
| 729 | + @action( |
| 730 | + detail=False, |
| 731 | + url_path="(?P<resource_id>\d+)/delete_thumbnail", # noqa |
| 732 | + url_name="delete-thumbnail", |
| 733 | + methods=["post"], |
| 734 | + permission_classes=[IsAuthenticated, UserHasPerms(perms_dict={"default": {"POST": ["base.add_resourcebase"]}})], |
| 735 | + ) |
| 736 | + |
| 737 | + def delete_thumbnail(self, request, resource_id, *args, **kwargs): |
| 738 | + |
| 739 | + try: |
| 740 | + resource = ResourceBase.objects.get(id=int(resource_id)) |
| 741 | + |
| 742 | + if not isinstance(resource.get_real_instance(), (Dataset, Map)): |
| 743 | + return Response( |
| 744 | + {"message": "Endpoint only available for Datasets and Maps.", "success": False}, |
| 745 | + status=status.HTTP_400_BAD_REQUEST |
| 746 | + ) |
| 747 | + |
| 748 | + # Check if thumbnail exists |
| 749 | + if not resource.thumbnail_url: |
| 750 | + return Response( |
| 751 | + {"message": "The thumbnail URL field is already empty.", "success": False}, |
| 752 | + status=status.HTTP_200_OK |
| 753 | + ) |
| 754 | + |
| 755 | + # request_body = request.data if request.data else json.loads(request.body) |
| 756 | + thumb_parsed_url = urlparse(resource.thumbnail_url) |
| 757 | + thumb_filename = thumb_parsed_url.path.split("/")[-1] |
| 758 | + # remove_thumb will call the thumb_path function and then the storage_manager which will delete it |
| 759 | + remove_thumb(thumb_filename) |
| 760 | + |
| 761 | + # Clear the field in the database |
| 762 | + resource.thumbnail_url = "" |
| 763 | + resource.save(update_fields=["thumbnail_url"]) |
| 764 | + |
| 765 | + return Response( |
| 766 | + {"message": "Thumbnail deleted successfully.", "success": True}, status=status.HTTP_200_OK |
| 767 | + ) |
| 768 | + |
| 769 | + except ResourceBase.DoesNotExist: |
| 770 | + return Response({"message": "Resource not found.", "success": False}, status=status.HTTP_404_NOT_FOUND) |
| 771 | + except ValueError: |
| 772 | + return Response({"message": "Invalid resource ID.", "success": False}, status=status.HTTP_400_BAD_REQUEST) |
| 773 | + except Exception as e: |
| 774 | + logger.error(e) |
| 775 | + return Response({"message": "Unexpected error occurred.", "success": False}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
| 776 | + |
| 777 | + |
724 | 778 | @extend_schema( |
725 | 779 | methods=["post"], responses={200}, description="Instructs the Async dispatcher to execute a 'INGEST' operation." |
726 | 780 | ) |
|
0 commit comments