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