files in chat download
This commit is contained in:
SDE
2023-09-17 19:17:48 +03:00
parent 2d25fd3e38
commit 9b7fbe8fd3
5 changed files with 59 additions and 8 deletions

View File

@@ -107,6 +107,9 @@ def get_update_chat_Dict(data):
def send_msg(data):
from AuthApp.models import User
import base64
import os
res_Dict = {}
msg = None
required_update_tickets_list_wo_managers = False
@@ -178,13 +181,27 @@ def send_msg(data):
if 'text' in data:
msg_create_kwargs.update({'text': data['text']})
msg = Message.objects.create(**msg_create_kwargs)
if 'files' in data:
files_list = []
for file in data['files']:
files_list.append(json.loads(file))
msg_create_kwargs.update({'files': files_list})
file_data =json.loads(file)
if not os.path.exists(f'chat_file_storage/{msg.id}'):
os.makedirs(f'chat_file_storage/{msg.id}')
f = open(f'chat_file_storage/{msg.id}/{file_data["file_name"]}', 'wb+')
head, content = file_data['file'].split(',')
content = base64.b64decode(content)
f.write(content)
f.close()
del file_data['file']
files_list.append(file_data)
msg.files = files_list
msg.save(update_fields=['files'])
msg = Message.objects.create(**msg_create_kwargs)
# if ticket:
# msgs = get_messages_for_ticket(ticket)

11
ChatServiceApp/urls.py Normal file
View File

@@ -0,0 +1,11 @@
# coding=utf-8
from django.urls import path
# from AuthApp.js_views import *
# from AuthApp.import_funcs import *
from .views import *
from django.contrib.auth import views
from RoutesApp.js_views import new_route_view_ajax
urlpatterns = [
path('get_file/<str:msg_id>/<str:file_name>', get_file_from_message, name='get_file_from_message'),
]

View File

@@ -1,3 +1,26 @@
from django.shortcuts import render
import json
# Create your views here.
from django.http import HttpResponse, Http404, FileResponse
from django.template import loader, RequestContext
from django.contrib.auth.decorators import login_required
from .models import *
from django.conf import settings
def get_file_from_message(request, msg_id, file_name):
from django.http import FileResponse
try:
msg = Message.objects.get(id=msg_id)
if request.user not in (msg.sender, msg.receiver):
raise Http404
for file in msg.files:
if file['file_name'] == file_name:
f = open(f'chat_file_storage/{msg.id}/{file["file_name"]}', 'rb')
return FileResponse(f)
except Exception as e:
raise Http404