summaryrefslogtreecommitdiff
path: root/server/src/leap/soledad/server/_blobs.py
blob: c9b780056a0ad0d0eb2a5bed1776fa5e326368ce (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
# _blobs.py
# Copyright (C) 2017 LEAP
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.

"""
Blobs Server implementation.

This is a very simplistic implementation for the time being.
Clients should be able to opt-in util the feature is complete.

A more performant BlobsBackend can (and should) be implemented for production
environments.
"""
import commands
import os
import base64

from twisted.logger import Logger
from twisted.web import static
from twisted.web import resource
from twisted.web.client import FileBodyProducer
from twisted.web.server import NOT_DONE_YET

from zope.interface import Interface, implementer


__all__ = ['BlobsResource']


logger = Logger()


# TODO some error handling needed
# [ ] sanitize path

# for the future:
# [ ] isolate user avatar in a safer way
# [ ] catch timeout in the server (and delete incomplete upload)
# [ ] chunking (should we do it on the client or on the server?)


class BlobAlreadyExists(Exception):
    pass


class IBlobsBackend(Interface):

    """
    An interface for a BlobsBackend.
    """

    def read_blob(user, blob_id, request):
        """
        Read blob with a given blob_id, and write it to the passed request.

        :returns: a deferred that fires upon finishing.
        """

    def tag_header(user, blob_id, request):
        """
        Adds a header 'Tag' with the last 20 bytes of the encoded file,
        which contains the tag.

        :returns: a deferred that fires upon finishing.
        """

    def write_blob(user, blob_id, request):
        """
        Write blob to the storage, reading it from the passed request.

        :returns: a deferred that fires upon finishing.
        """

    # other stuff for the API

    def delete_blob(user, blob_id):
        pass

    def get_blob_size(user, blob_id):
        pass

    def get_total_storage(user):
        pass


@implementer(IBlobsBackend)
class FilesystemBlobsBackend(object):

    def __init__(self, blobs_path='/tmp/blobs/', quota=200 * 1024):
        self.quota = quota
        if not os.path.isdir(blobs_path):
            os.makedirs(blobs_path)
        self.path = blobs_path

    def tag_header(self, user, blob_id, request):
        with open(self._get_path(user, blob_id)) as doc_file:
                doc_file.seek(-16, 2)
                tag = base64.urlsafe_b64encode(doc_file.read())
                request.responseHeaders.setRawHeaders('Tag', [tag])

    def read_blob(self, user, blob_id, request):
        logger.info('reading blob: %s - %s' % (user, blob_id))
        path = self._get_path(user, blob_id)
        logger.debug('blob path: %s' % path)
        _file = static.File(path, defaultType='application/octet-stream')
        return _file.render_GET(request)

    def write_blob(self, user, blob_id, request):
        path = self._get_path(user, blob_id)
        try:
            os.makedirs(os.path.split(path)[0])
        except:
            pass
        if os.path.isfile(path):
            # XXX return some 5xx code
            raise BlobAlreadyExists()
        used = self.get_total_storage(user)
        if used > self.quota:
            logger.error("Error 507: Quota exceeded for user: %s" % user)
            request.setResponseCode(507)
            request.write('Quota Exceeded!')
            request.finish()
            return NOT_DONE_YET
        logger.info('writing blob: %s - %s' % (user, blob_id))
        fbp = FileBodyProducer(request.content)
        d = fbp.startProducing(open(path, 'wb'))
        d.addCallback(lambda _: request.finish())
        return NOT_DONE_YET

    def get_total_storage(self, user):
        return self._get_disk_usage(os.path.join(self.path, user))

    def delete_blob(user, blob_id):
        raise NotImplementedError

    def get_blob_size(user, blob_id):
        raise NotImplementedError

    def _get_disk_usage(self, start_path):
        if not os.path.isdir(start_path):
            return 0
        cmd = 'du -c %s | tail -n 1' % start_path
        size = commands.getoutput(cmd).split()[0]
        return int(size)

    def _get_path(self, user, blob_id):
        parts = [user]
        parts += [blob_id[0], blob_id[0:3], blob_id[0:6]]
        parts += [blob_id]
        return os.path.join(self.path, *parts)


class BlobsResource(resource.Resource):

    isLeaf = True

    # Allowed factory classes are defined here
    blobsFactoryClass = FilesystemBlobsBackend

    def __init__(self, blobs_path):
        # TODO pass the backend as configurable option
        """
        __init__(self, backend, opts={})
        factorykls = getattr(self, backend + 'Class')(**opts)
        self._handler = kls()
        """
        resource.Resource.__init__(self)
        self._handler = self.blobsFactoryClass(blobs_path)
        assert IBlobsBackend.providedBy(self._handler)

    # TODO double check credentials, we can have then
    # under request.

    def render_GET(self, request):
        logger.info("http get: %s" % request.path)
        user, blob_id = request.postpath
        self._handler.tag_header(user, blob_id, request)
        return self._handler.read_blob(user, blob_id, request)

    def render_PUT(self, request):
        logger.info("http put: %s" % request.path)
        user, blob_id = request.postpath
        return self._handler.write_blob(user, blob_id, request)


if __name__ == '__main__':
    # A dummy blob server
    # curl -X PUT --data-binary @/tmp/book.pdf localhost:9000/user/someid
    # curl -X GET -o /dev/null localhost:9000/user/somerandomstring
    from twisted.python import log
    import sys
    log.startLogging(sys.stdout)

    from twisted.web.server import Site
    from twisted.internet import reactor

    # parse command line arguments
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('--port', default=9000, type=int)
    parser.add_argument('--path', default='/tmp/blobs/user')
    args = parser.parse_args()

    root = BlobsResource(args.path)
    # I picture somethink like
    # BlobsResource(backend="filesystem", backend_opts={'path': '/tmp/blobs'})

    factory = Site(root)
    reactor.listenTCP(args.port, factory)
    reactor.run()