Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ tests.py
*.sublime-workspace
*.sublime-project
*.backup

# pycharm
.idea
6 changes: 3 additions & 3 deletions cloud.mail.ru_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ In some methods (like file.add, folder.rename, etc), which may cause a conflict
- `conflict`: see [About conflict](#about-conflict) section
- `token`: `csrf` token, see method `tokens/csrf`

#### *clocloXXX-upload.cloud.mail.ru/upload/*
*where XXX in url is uploading server, can be obtain by calling `/dispatcher`*
#### *cld-uploaderXXX.cloud.mail.ru/upload-web*
*where XXX in url is uploading server, can be obtained by calling `/dispatcher`*
- Method: `PUT`
- Description: upload file to server
- CloudAPI Response example:
Expand All @@ -142,7 +142,7 @@ In some methods (like file.add, folder.rename, etc), which may cause a conflict
- *where second element - size of the file*
- PyAPI: `>>> cm.api.file.add(local_path: str, cloud_path: str)`
- `local_path`: path to file in the local env
- CloudAPI: `PUT clocloXXX-upload.cloud.mail.ru/upload/`
- CloudAPI: `PUT cld-uploader12.cloud.mail.ru/upload-web`
- multipart/form-data:
- Just your file
- api(...) call example:
Expand Down
15 changes: 15 additions & 0 deletions cloud_mail_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ def __init__(self, login: str, password: str):
def is_cookies_valid(self) -> bool:
return self.api.tokens.csrf(True)["body"] != "user"

def load_csrf(self) -> bool:
"""
Download csrf-token from mail.ru server and put it in session
"""

resp = self.api.tokens.csrf(True)

if isinstance(resp["body"], dict):
self.session.headers["X-CSRF-Token"] = resp["body"]["token"]
return True

return False

def auth(self) -> bool:
response = self.session.post(
self.api.config["endpoints"]["MAILRU_AUTH_ENDPOINT"],
Expand All @@ -41,6 +54,8 @@ def auth(self) -> bool:
}
)

self.load_csrf()

return self.is_cookies_valid()


Expand Down
2 changes: 1 addition & 1 deletion cloud_mail_api/api/api_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"file/_upload_file": {
"method": "PUT",
"endpoint": "https://cloclo21-upload.cloud.mail.ru/upload/",
"endpoint": "https://cld-uploader12.cloud.mail.ru/upload-web/",
"no_path": true,
"location": {
"package": "cloud_mail_api.api",
Expand Down
22 changes: 12 additions & 10 deletions cloud_mail_api/api/file.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import mimetypes
from typing import Tuple

def file(
Expand All @@ -19,18 +20,19 @@ def file_upload_file(
url: str,
http_method: str,
local_path: str) -> Tuple[str, int]:

files = {
"file": (
os.path.basename(local_path),
open(local_path, "rb"),
"application/octet-stream"
)
with open(local_path, "rb") as fd:
data = fd.read()

mime = mimetypes.guess_type(local_path)[0]

# Not sure whether this params are required, however they are present on cloud.mail.ru website
params = {
"x-email": api.client_instance.login,
"cloud_domain": 2
}

response = api.client_instance.session.put(url, files=files)
if response.status_code == 403:
response = api.client_instance.session.put(url, files=files)
response = api.client_instance.session.put(url, params=params, headers={"Content-Type": mime}, data=data)

return response.text, int(response.request.headers["Content-Length"])

def _add(
Expand Down