File size: 2,350 Bytes
15f5485
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import ntpath

import dropbox
from dropbox.files import WriteMode

from tools.config import config, general
from tools.oauthtool import implicit_flow
from tools.persistence import KVStub

_name = "dropbox"
kvstore = KVStub()


def upload(file: str) -> str:
    # TODO: Use another way to find out if the access token is valid
    # FIXME: Check for authorization failure
    if "access_token" not in kvstore.keys():
        authorization_successful = _authorize()
        if not authorization_successful:
            return None  # unable to upload without successful authorization

    token = kvstore["access_token"]
    dropbox_client = dropbox.Dropbox(token)
    dropbox_filepath = "/" + config[general]["screenshot_dir"] + "/" + ntpath.basename(file)
    file_object = open(file, 'rb')

    try:
        dropbox_client.files_upload(
            file_object.read(),
            dropbox_filepath,
            mode=WriteMode("overwrite", None),
            client_modified=None,
            mute=False
        )
    except dropbox.exceptions.ApiError as e:
        # FIXME: 'ApiError' object has no attribute 'error_msg'
        logging.error("Upload failed. Error message: {0}".format(e.error_msg))
        return None

    try:
        url = dropbox_client.sharing_create_shared_link_with_settings(dropbox_filepath, None).url
    except dropbox.exceptions.ApiError as e:
        logging.error("Could not create shared link. Error message: {0}".format(e.error_msg))
        return None

    return _change_url_suffix(url)


def _authorize():
    authorization_endpoint = "https://www.dropbox.com/oauth2/authorize"
    app_key = config[_name]["app_key"]

    # Start OAuth2 implicit flow
    auth_response = implicit_flow(authorization_endpoint, app_key)

    # Check if authorization was successful
    if "error" in auth_response and auth_response["error"] is not None:
        logging.error("Authentication failed. Error message: {0}".format(auth_response["error_description"]))
        return False

    kvstore["access_token"] = auth_response["access_token"]
    kvstore.sync()

    return True


def _change_url_suffix(url):
    # Modifies URL that it directly refers to the uploaded file.
    suffix = "dl=0"
    raw_suffix = "raw=1"

    if url.endswith(suffix):
        url = url.replace(suffix, raw_suffix)

    return url