|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +from datetime import time |
| 4 | +from asgiref.sync import sync_to_async |
| 5 | +from aiohttp import ClientSession |
| 6 | +from django.db import transaction |
| 7 | +from django.utils.decorators import classonlymethod |
| 8 | +from rest_framework import permissions, status |
| 9 | +from rest_framework.response import Response |
| 10 | +from rest_framework.views import APIView |
| 11 | + |
| 12 | +from files.models import UserFile |
| 13 | +from procollab.settings import ( |
| 14 | + DEBUG, |
| 15 | + SELECTEL_ACCOUNT_ID, |
| 16 | + SELECTEL_CONTAINER_NAME, |
| 17 | + SELECTEL_CONTAINER_PASSWORD, |
| 18 | + SELECTEL_CONTAINER_USERNAME, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class FileUploadView(APIView): |
| 23 | + permission_classes = [permissions.AllowAny] |
| 24 | + |
| 25 | + @classonlymethod |
| 26 | + def as_view(cls, **initkwargs): |
| 27 | + view = super().as_view(**initkwargs) |
| 28 | + view._is_coroutine = asyncio.coroutines._is_coroutine |
| 29 | + return view |
| 30 | + |
| 31 | + @transaction.atomic |
| 32 | + async def post(self, request): |
| 33 | + file = request.FILES["file"] |
| 34 | + if DEBUG is True: |
| 35 | + return Response( |
| 36 | + {"message": "Files doesn't save in development mode, sorry <3"}, |
| 37 | + status=status.HTTP_406_NOT_ACCEPTABLE, |
| 38 | + ) |
| 39 | + |
| 40 | + link = f"https://api.selcdn.ru/v1/SEL_{SELECTEL_ACCOUNT_ID}/{SELECTEL_CONTAINER_NAME}/" |
| 41 | + |
| 42 | + user = request.user |
| 43 | + |
| 44 | + # creates UserFile object in the database |
| 45 | + self._save_to_db(user, link) |
| 46 | + token = await self._get_token() |
| 47 | + async with ClientSession(headers={"X-Auth-Token": token}) as server: |
| 48 | + if len(file.split(".")) > 1: |
| 49 | + extension = file.filename.split(".")[1] |
| 50 | + else: |
| 51 | + extension = "" |
| 52 | + |
| 53 | + # looks like /hashed_email/hashed_filename_hashed_time.extension |
| 54 | + url = ( |
| 55 | + link + f"/{SELECTEL_CONTAINER_NAME}/{abs(hash(user.email))}/" |
| 56 | + f"{abs(hash(file.filename))}_{abs(hash(time.time()))}.{extension}" |
| 57 | + ) |
| 58 | + |
| 59 | + async with server.put( |
| 60 | + url, |
| 61 | + data=file.open(mode="rb").read(), |
| 62 | + ) as response: |
| 63 | + if response.status != 201: |
| 64 | + return await Response( |
| 65 | + "Failed to upload file", status_code=status.HTTP_409_CONFLICT |
| 66 | + ) |
| 67 | + return await Response({"url": url}, status=status.HTTP_201_CREATED) |
| 68 | + |
| 69 | + @sync_to_async |
| 70 | + def _save_to_db(self, user, link): |
| 71 | + """creates userfile object for file uploads""" |
| 72 | + return UserFile.objects.create(user=user, link=link) |
| 73 | + |
| 74 | + async def _get_token(self): |
| 75 | + """returns auth token for sentry""" |
| 76 | + async with ClientSession() as server: |
| 77 | + data = { |
| 78 | + "auth": { |
| 79 | + "identity": { |
| 80 | + "methods": ["password"], |
| 81 | + "password": { |
| 82 | + "user": { |
| 83 | + "id": SELECTEL_CONTAINER_USERNAME, |
| 84 | + "password": SELECTEL_CONTAINER_PASSWORD, |
| 85 | + } |
| 86 | + }, |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + async with server.post( |
| 91 | + "https://api.selcdn.ru/v3/auth/tokens", |
| 92 | + data=json.dumps(data), |
| 93 | + ) as response: |
| 94 | + if response.status != 201: |
| 95 | + return Response( |
| 96 | + "Failed to get token", status_code=status.HTTP_409_CONFLICT |
| 97 | + ) |
| 98 | + return response.json() |
0 commit comments