se almacena foto de persona

francisco/photos
Francisco Sandoval 2024-02-14 23:12:58 -03:00
parent 1f13661f2d
commit 0032c1af1e
4 changed files with 76 additions and 7 deletions

View File

@ -19,4 +19,5 @@ SMTP_PASS=aigdvnrbueitklry
SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>' SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>'
# PATH UPLOAD # PATH UPLOAD
GTFS_UPLOADS=/uploads GTFS_UPLOADS=/uploads/gtfs
PHOTOS_UPLOADS=/uploads/photos

View File

@ -17,3 +17,7 @@ SMTP_PROTOCOL=tls
SMTP_USER=francisco.sandoval@outlook.cl SMTP_USER=francisco.sandoval@outlook.cl
SMTP_PASS=aigdvnrbueitklry SMTP_PASS=aigdvnrbueitklry
SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>' SMTP_FROM='"Sistema Transporte" <francisco.sandoval@outlook.cl>'
# PATH UPLOAD
GTFS_UPLOADS=/uploads/gtfs
PHOTOS_UPLOADS=/uploads/photos

View File

@ -20,7 +20,7 @@ class GtfsArchivoViewSet(viewsets.ModelViewSet):
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
fs = FileSystemStorage(location = request.data['ruta_archivo']) fs = FileSystemStorage(location = config('GTFS_UPLOADS','/tmp'))
fileUp = fs.save(request.data['archivo'], request.data['binario']) fileUp = fs.save(request.data['archivo'], request.data['binario'])
request.data['ruta_archivo'] = fileUp request.data['ruta_archivo'] = fileUp

View File

@ -1,6 +1,12 @@
from rest_framework import viewsets from rest_framework import viewsets
from .. import models, serializers from rest_framework.decorators import action
from django.http import HttpResponse from api import models, serializers
from django.http import HttpResponse, FileResponse
from django.core.files.storage import FileSystemStorage
from pathlib import Path
from decouple import config
import os
import mimetypes
import logging import logging
class PersonaViewSet(viewsets.ModelViewSet): class PersonaViewSet(viewsets.ModelViewSet):
@ -12,9 +18,67 @@ class PersonaViewSet(viewsets.ModelViewSet):
def create(self, request): def create(self, request):
try: try:
super().create(request) fs = FileSystemStorage(location = config('PHOTOS_UPLOADS','/tmp'))
fs.save(request.data['photo'], request.data['photo'])
return super().create(request)
except Exception as e: except Exception as e:
# logging.warning(e.detail['rut'][0])
if e.detail['rut']: if e.detail['rut']:
return HttpResponse(e.detail['rut'][0], status=400) return HttpResponse(e.detail['rut'][0], status=400)
return HttpResponse(e, status=400) return HttpResponse(e, status=400)
def partial_update(self, request, pk=None):
try:
# indicar el nombre con que se guardara el archivo
archivo = request.data['photo']
extension = Path(archivo.name).suffix
nombre_archivo = f'{pk}{extension}'
# guardar el archivo en la carpeta
fs = FileSystemStorage(location = config('PHOTOS_UPLOADS','/tmp'))
# Sobrescribir el archivo si ya existe
if fs.exists(nombre_archivo):
fs.delete(nombre_archivo)
fs.save(nombre_archivo, archivo)
# proceder con guardar registro
return super().partial_update(request, pk)
except Exception as e:
return HttpResponse(e, status=400)
@action(detail=False, methods=['get'])
def photo(self, request, *args, **kwargs):
file_location = None
try:
rut = request.GET['id']
folder = config('PHOTOS_UPLOADS','/tmp')
file_location = os.path.join(folder, 'desconocido.jpg')
file_query = os.path.join(folder, f'{rut}.png')
if os.path.isfile(file_query):
file_location = file_query
else:
file_query = os.path.join(folder, f'{rut}.jpg')
if os.path.isfile(file_query):
file_location = file_query
else:
file_query = os.path.join(folder, f'{rut}.jpeg')
if os.path.isfile(file_query):
file_location = file_query
tipo_mime = mimetypes.guess_type(file_location)[0]
nombre_archivo = os.path.basename(file_location)
archivo = open(file_location, 'rb')
response = FileResponse(archivo)
response['Access-Control-Expose-Headers'] = 'Content-Disposition'
response['Content-Type'] = tipo_mime
response['Content-Disposition'] = f'inline; filename="{nombre_archivo}"'
return response
except Exception as err:
logging.error({ 'error': err, 'file_location': file_location })
return HttpResponse('Error al descargar archivo', status=500)