store_api.py 7.63 KB
Newer Older
Bach Dániel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
Guba Sándor committed
17
import json
18
import logging
Őry Máté committed
19
from urlparse import urljoin
Kálmán Viktor committed
20

21 22
import os
from datetime import datetime
23
from django.conf import settings
24 25
from django.http import Http404
from os.path import splitext
Őry Máté committed
26 27
from requests import get, post, codes
from sizefield.utils import filesizeformat
Guba Sándor committed
28

29 30
from storage.models import Disk

31 32
logger = logging.getLogger(__name__)

Guba Sándor committed
33

Őry Máté committed
34
class StoreApiException(Exception):
35 36 37
    pass


Őry Máté committed
38 39 40 41 42 43
class NotOkException(StoreApiException):
    def __init__(self, status, *args, **kwargs):
        self.status = status
        super(NotOkException, self).__init__(*args, **kwargs)


44 45 46 47
class NoStoreException(StoreApiException):
    pass


Őry Máté committed
48 49
class Store(object):

50
    def __init__(self, user, default_timeout=5):
51
        self.store_url = settings.STORE_URL
52
        if not self.store_url:
53
            raise NoStoreException
54 55 56 57 58 59 60 61

        if user.is_superuser and not user.profile.org_id:
            self.username = 'u-admin'
        elif not user.profile.org_id:
            raise NoStoreException
        else:
            self.username = 'u-%s' % user.profile.org_id

Őry Máté committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
        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.default_timeout = default_timeout

    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
76 77
        kwargs['USER'] = self.username
        payload = json.dumps(kwargs)
Őry Máté committed
78 79 80 81 82 83 84 85
        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
86
        else:
Őry Máté committed
87 88 89 90 91 92 93 94
            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):
95
        return self._request("/user/", post, CMD=cmd, **kwargs)
Őry Máté committed
96 97 98 99 100 101 102 103

    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
104

Őry Máté committed
105 106 107 108 109 110 111 112
    def toplist(self, process=True):
        r = self._request_cmd("TOPLIST")
        result = r.json()
        if process:
            return self._process_list(result)
        else:
            return result

113 114 115 116 117 118 119 120 121
    def get_disk_images(self, path='/'):
        images = []
        file_list = self.list(path, process=False)
        export_formats = [item[0] for item in Disk.EXPORT_FORMATS]
        for item in file_list:
            if os.path.splitext(item['NAME'])[1].strip('.') in export_formats:
                images.append(os.path.join(path, item['NAME']))
        return images

Őry Máté committed
122
    def request_download(self, path):
123 124
        r = self._request_cmd("DOWNLOAD", PATH=path, timeout=10)
        return r.json()['LINK']
Őry Máté committed
125 126

    def request_upload(self, path):
127 128
        r = self._request_cmd("UPLOAD", PATH=path)
        return r.json()['LINK']
Őry Máté committed
129

130 131 132 133
    def request_ssh_download(self, path):
        r = self._request_cmd("SSH_DOWNLOAD", PATH=path)
        return r.json()['LINK'], r.json()['PORT']

134 135
    def request_ssh_upload(self):
        r = self._request_cmd("SSH_UPLOAD")
136 137
        return r.json()['LINK'], r.json()['PORT']

138
    def ssh_upload_finished(self, uploaded_name, path, new_name):
139
        self._request_cmd(
140
            "SSH_UPLOAD_FINISHED",
141 142
            FILENAME=uploaded_name,
            PATH=path,
143 144 145
            NEWNAME=new_name
        )

Őry Máté committed
146 147 148 149 150 151 152 153 154 155
    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
156
        r = self._request("/user/")
157 158
        quota = r.json()
        quota.update({
Guba Sándor committed
159 160 161
            'readable_used': filesizeformat(float(quota['used'])),
            'readable_soft': filesizeformat(float(quota['soft'])),
            'readable_hard': filesizeformat(float(quota['hard'])),
162 163
        })
        return quota
Őry Máté committed
164 165

    def set_quota(self, quota):
166
        self._request("/quota/", post, QUOTA=quota)
Őry Máté committed
167 168 169

    def user_exist(self):
        try:
170
            self._request("/user/")
Őry Máté committed
171 172 173 174 175
            return True
        except NotOkException:
            return False

    def create_user(self, password, keys, quota):
176
        self._request("/new/", method=post,
177
                      SMBPASSWD=password, KEYS=keys, QUOTA=quota)
Őry Máté committed
178 179 180 181 182 183 184 185 186 187 188 189 190

    @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'])))

191
            if d['DIR'] == ".":
Őry Máté committed
192 193 194 195 196 197 198 199 200
                d['directory'] = "/"
            else:
                d['directory'] = "/" + d['DIR'] + "/"

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

201
            d['ext'] = splitext(d['path'])[1]
202
            d['icon'] = ("folder-open" if not d['TYPE'] == "F"
203 204
                         else file_icons.get(d['ext'], "file-o"))

Őry Máté committed
205
        return sorted(content, key=lambda k: k['TYPE'])
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247


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",
}