|
| 1 | +import requests |
| 2 | +import time |
| 3 | +from files.exceptions import SelectelUploadError |
| 4 | + |
| 5 | +from procollab.settings import ( |
| 6 | + DEBUG, |
| 7 | + SELECTEL_ACCOUNT_ID, |
| 8 | + SELECTEL_CONTAINER_NAME, |
| 9 | + SELECTEL_CONTAINER_PASSWORD, |
| 10 | + SELECTEL_CONTAINER_USERNAME, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class FileAPI: |
| 15 | + def __init__(self, file, user) -> None: |
| 16 | + self.file = file |
| 17 | + self.user = user |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def delete(url: str) -> int: |
| 21 | + """Deletes file from selcdn""" |
| 22 | + token = FileAPI._get_selectel_swift_token() |
| 23 | + response = requests.delete(url, headers={"X-Auth-Token": token}) |
| 24 | + return response.status_code |
| 25 | + |
| 26 | + def upload(self): |
| 27 | + return self._upload_via_selectel_swift() |
| 28 | + |
| 29 | + def _upload_via_selectel_swift(self) -> tuple[int, str]: |
| 30 | + token = self._get_selectel_swift_token() |
| 31 | + url = self._generate_selectel_swift_file_url() |
| 32 | + |
| 33 | + with self.file.open(mode="rb") as file_object: |
| 34 | + response = requests.put( |
| 35 | + url, |
| 36 | + headers={"X-Auth-Token": token, "Content-Type": file_object.content_type}, |
| 37 | + data=file_object.read(), |
| 38 | + ) |
| 39 | + |
| 40 | + return response.status_code, url |
| 41 | + |
| 42 | + def _generate_selectel_swift_link(sefl): |
| 43 | + link = f"https://api.selcdn.ru/v1/SEL_{SELECTEL_ACCOUNT_ID}/{SELECTEL_CONTAINER_NAME}/" |
| 44 | + if DEBUG: |
| 45 | + link += "debug/" |
| 46 | + return link |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def _get_selectel_swift_token(): |
| 50 | + """Returns auth token for selcdn""" |
| 51 | + data = { |
| 52 | + "auth": { |
| 53 | + "identity": { |
| 54 | + "methods": ["password"], |
| 55 | + "password": { |
| 56 | + "user": { |
| 57 | + "id": SELECTEL_CONTAINER_USERNAME, |
| 58 | + "password": SELECTEL_CONTAINER_PASSWORD, |
| 59 | + } |
| 60 | + }, |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + r = requests.post("https://api.selcdn.ru/v3/auth/tokens", json=data) |
| 65 | + if r.status_code not in [200, 201]: |
| 66 | + raise SelectelUploadError("Couldn't generate a token for selcdn") |
| 67 | + return r.headers["x-subject-token"] |
| 68 | + |
| 69 | + def _get_file_extension(self) -> str: |
| 70 | + if len(self.file.name.split(".")) > 1: |
| 71 | + return "." + self.file.name.split(".")[1] |
| 72 | + return "" |
| 73 | + |
| 74 | + def _generate_selectel_swift_file_url(self) -> str: |
| 75 | + """ |
| 76 | + Generates url for selcdn |
| 77 | +
|
| 78 | + Returns: |
| 79 | + url: str looks like /hashedEmail/hashedFilename_hashedTime.extension |
| 80 | + """ |
| 81 | + link = self._generate_selectel_swift_link() |
| 82 | + extension = self._get_file_extension() |
| 83 | + return ( |
| 84 | + link |
| 85 | + + f"{abs(hash(self.user.email))}/{abs(hash(self.file.name))}_{abs(hash(time.time()))}{extension}" |
| 86 | + ) |
0 commit comments