store_api.py 5.9 KB
Newer Older
1
from os.path import splitext
Guba Sándor committed
2
import json
3
import logging
Őry Máté committed
4
from urlparse import urljoin
Kálmán Viktor committed
5 6
from datetime import datetime

Őry Máté committed
7
from django.http import Http404
8
from django.conf import settings
Őry Máté committed
9
from requests import get, post, codes
10
from requests.exceptions import Timeout  # noqa
Őry Máté committed
11
from sizefield.utils import filesizeformat
Guba Sándor committed
12

13 14
logger = logging.getLogger(__name__)

Guba Sándor committed
15

Őry Máté committed
16
class StoreApiException(Exception):
17 18 19
    pass


Őry Máté committed
20 21 22 23 24 25
class NotOkException(StoreApiException):
    def __init__(self, status, *args, **kwargs):
        self.status = status
        super(NotOkException, self).__init__(*args, **kwargs)


26 27 28 29
class NoStoreException(StoreApiException):
    pass


Őry Máté committed
30 31 32 33 34 35 36 37 38 39 40 41 42
class Store(object):

    def __init__(self, user, default_timeout=0.5):
        self.request_args = {'verify': settings.STORE_VERIFY_SSL}
        if settings.STORE_SSL_AUTH:
            self.request_args['cert'] = (settings.STORE_CLIENT_CERT,
                                         settings.STORE_CLIENT_KEY)
        if settings.STORE_BASIC_AUTH:
            self.request_args['auth'] = (settings.STORE_CLIENT_USER,
                                         settings.STORE_CLIENT_PASSWORD)
        self.username = "u-%d" % user.pk
        self.default_timeout = default_timeout
        self.store_url = settings.STORE_URL
43 44
        if not self.store_url:
            raise NoStoreException
Őry Máté committed
45 46 47 48 49 50

    def _request(self, url, method=get, timeout=None,
                 raise_status_code=True, **kwargs):
        url = urljoin(self.store_url, url)
        if timeout is None:
            timeout = self.default_timeout
51
        payload = json.dumps(kwargs) if kwargs else None
Őry Máté committed
52 53 54 55 56 57 58 59
        try:
            headers = {'content-type': 'application/json'}
            response = method(url, data=payload, headers=headers,
                              timeout=timeout, **self.request_args)
        except Exception:
            logger.exception("Error in store %s loading %s",
                             unicode(method), url)
            raise
Kálmán Viktor committed
60
        else:
Őry Máté committed
61 62 63 64 65 66 67 68
            if raise_status_code and response.status_code != codes.ok:
                if response.status_code == 404:
                    raise Http404()
                else:
                    raise NotOkException(response.status_code)
            return response

    def _request_cmd(self, cmd, **kwargs):
69
        return self._request(self.username, post, CMD=cmd, **kwargs)
Őry Máté committed
70 71 72 73 74 75 76 77

    def list(self, path, process=True):
        r = self._request_cmd("LIST", PATH=path)
        result = r.json()
        if process:
            return self._process_list(result)
        else:
            return result
Kálmán Viktor committed
78

Őry Máté committed
79 80 81 82 83 84 85 86 87
    def toplist(self, process=True):
        r = self._request_cmd("TOPLIST")
        result = r.json()
        if process:
            return self._process_list(result)
        else:
            return result

    def request_download(self, path):
88 89
        r = self._request_cmd("DOWNLOAD", PATH=path, timeout=10)
        return r.json()['LINK']
Őry Máté committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    def request_upload(self, path):
            r = self._request_cmd("UPLOAD", PATH=path)
            return r.json()['LINK']

    def remove(self, path):
        self._request_cmd("REMOVE", PATH=path)

    def new_folder(self, path):
        self._request_cmd("NEW_FOLDER", PATH=path)

    def rename(self, old_path, new_name):
        self._request_cmd("RENAME", PATH=old_path, NEW_NAME=new_name)

    def get_quota(self):  # no CMD? :o
        r = self._request(self.username)
106 107
        quota = r.json()
        quota.update({
Guba Sándor committed
108 109 110
            'readable_used': filesizeformat(float(quota['used'])),
            'readable_soft': filesizeformat(float(quota['soft'])),
            'readable_hard': filesizeformat(float(quota['hard'])),
111 112
        })
        return quota
Őry Máté committed
113 114

    def set_quota(self, quota):
115
        self._request("/quota/" + self.username, post, QUOTA=quota)
Őry Máté committed
116 117 118 119 120 121 122 123 124

    def user_exist(self):
        try:
            self._request(self.username)
            return True
        except NotOkException:
            return False

    def create_user(self, password, keys, quota):
125 126
        self._request("/new/" + self.username, method=post,
                      SMBPASSWD=password, KEYS=keys, QUOTA=quota)
Őry Máté committed
127 128 129 130 131 132 133 134 135 136 137 138 139

    @staticmethod
    def _process_list(content):
        for d in content:
            d['human_readable_date'] = datetime.utcfromtimestamp(float(
                d['MTIME']))
            delta = (datetime.utcnow() -
                     d['human_readable_date']).total_seconds()
            d['is_new'] = 0 < delta < 5
            d['human_readable_size'] = (
                "directory" if d['TYPE'] == "D" else
                filesizeformat(float(d['SIZE'])))

140
            if d['DIR'] == ".":
Őry Máté committed
141 142 143 144 145 146 147 148 149
                d['directory'] = "/"
            else:
                d['directory'] = "/" + d['DIR'] + "/"

            d['path'] = d['directory']
            d['path'] += d['NAME']
            if d['TYPE'] == "D":
                d['path'] += "/"

150
            d['ext'] = splitext(d['path'])[1]
151
            d['icon'] = ("folder-open" if not d['TYPE'] == "F"
152 153
                         else file_icons.get(d['ext'], "file-o"))

Őry Máté committed
154
        return sorted(content, key=lambda k: k['TYPE'])
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196


file_icons = {
    '.txt': "file-text-o",
    '.pdf': "file-pdf-o",

    '.jpg': "file-image-o",
    '.jpeg': "file-image-o",
    '.png': "file-image-o",
    '.gif': "file-image-o",

    '.avi': "file-video-o",
    '.mkv': "file-video-o",
    '.mp4': "file-video-o",
    '.mov': "file-video-o",

    '.mp3': "file-sound-o",
    '.flac': "file-sound-o",
    '.wma': "file-sound-o",

    '.pptx': "file-powerpoint-o",
    '.ppt': "file-powerpoint-o",
    '.doc': "file-word-o",
    '.docx': "file-word-o",
    '.xlsx': "file-excel-o",
    '.xls': "file-excel-o",

    '.rar': "file-archive-o",
    '.zip': "file-archive-o",
    '.7z': "file-archive-o",
    '.tar': "file-archive-o",
    '.gz': "file-archive-o",

    '.py': "file-code-o",
    '.html': "file-code-o",
    '.js': "file-code-o",
    '.css': "file-code-o",
    '.c': "file-code-o",
    '.cpp': "file-code-o",
    '.h': "file-code-o",
    '.sh': "file-code-o",
}