feature/implementacion de gestor de informacion y archivos minIO
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import atexit
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from django.shortcuts import render
|
||||
from ..organization.models import Organizacion
|
||||
from rest_framework import viewsets
|
||||
@@ -8,6 +12,7 @@ from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
from django.http import FileResponse, Http404
|
||||
from api.utils.storage_service import storage_service
|
||||
|
||||
from .serializers import VucemSerializer, CredencialesImportadorSerializer, CredencialesImportadorSimpleSerializer
|
||||
from rest_framework import serializers
|
||||
@@ -140,25 +145,52 @@ class VucemView(viewsets.ModelViewSet):
|
||||
|
||||
@action(detail=True, methods=["get"], permission_classes=[IsAuthenticated])
|
||||
def download_cer(self, request, pk=None):
|
||||
"""
|
||||
Descarga directa del archivo cer.
|
||||
"""
|
||||
vucem = self.get_object()
|
||||
if not vucem.cer:
|
||||
return Response({"detail": "No hay archivo cer disponible."}, status=404)
|
||||
response = FileResponse(vucem.cer.open('rb'), as_attachment=True, filename=vucem.cer.name.split('/')[-1])
|
||||
|
||||
ruta = str(vucem.cer)
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||
tmp_path = tmp.name
|
||||
|
||||
success = storage_service.download_file(ruta, tmp_path)
|
||||
if not success:
|
||||
raise Http404("No se pudo descargar el archivo")
|
||||
|
||||
filename = os.path.basename(ruta)
|
||||
response = FileResponse(open(tmp_path, 'rb'), as_attachment=True, filename=filename)
|
||||
atexit.register(lambda: os.unlink(tmp_path) if os.path.exists(tmp_path) else None)
|
||||
|
||||
return response
|
||||
|
||||
@action(detail=True, methods=["get"], permission_classes=[IsAuthenticated])
|
||||
def download_key(self, request, pk=None):
|
||||
"""
|
||||
Descarga directa del archivo key.
|
||||
"""
|
||||
vucem = self.get_object()
|
||||
if not vucem.key:
|
||||
return Response({"detail": "No hay archivo key disponible."}, status=404)
|
||||
response = FileResponse(vucem.key.open('rb'), as_attachment=True, filename=vucem.key.name.split('/')[-1])
|
||||
|
||||
ruta = str(vucem.key)
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||
tmp_path = tmp.name
|
||||
|
||||
success = storage_service.download_file(ruta, tmp_path)
|
||||
if not success:
|
||||
raise Http404("No se pudo descargar el archivo")
|
||||
|
||||
filename = os.path.basename(ruta)
|
||||
response = FileResponse(open(tmp_path, 'rb'), as_attachment=True, filename=filename)
|
||||
atexit.register(lambda: os.unlink(tmp_path) if os.path.exists(tmp_path) else None)
|
||||
|
||||
return response
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
if instance.key:
|
||||
storage_service.delete_file(str(instance.key))
|
||||
if instance.cer:
|
||||
storage_service.delete_file(str(instance.cer))
|
||||
instance.delete()
|
||||
|
||||
|
||||
class CredencialesImportadorViewSet(viewsets.ModelViewSet):
|
||||
|
||||
Reference in New Issue
Block a user