From ab297c4efe10c70949fac5384a63cbf553ba5da9 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 26 Jun 2017 05:25:55 -0300 Subject: [feature] namespace capability to BlobsBackend Adds an extra parameter called "namespace" on the backend interface and on FileSystemBlobsBackend. This parameter overrides default id partitioning and uses a separate folder for a custom namespace. -- Resolves: #8889 --- src/leap/soledad/server/_blobs.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/leap/soledad/server/_blobs.py') diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py index 10678360..f87c3818 100644 --- a/src/leap/soledad/server/_blobs.py +++ b/src/leap/soledad/server/_blobs.py @@ -66,16 +66,16 @@ class FilesystemBlobsBackend(object): os.makedirs(blobs_path) self.path = blobs_path - def read_blob(self, user, blob_id, request): + def read_blob(self, user, blob_id, request, namespace=''): logger.info('reading blob: %s - %s' % (user, blob_id)) - path = self._get_path(user, blob_id) + path = self._get_path(user, blob_id, namespace) logger.debug('blob path: %s' % path) _file = static.File(path, defaultType='application/octet-stream') return _file.render_GET(request) @defer.inlineCallbacks - def write_blob(self, user, blob_id, request): - path = self._get_path(user, blob_id) + def write_blob(self, user, blob_id, request, namespace=''): + path = self._get_path(user, blob_id, namespace) try: mkdir_p(os.path.split(path)[0]) except OSError: @@ -95,16 +95,16 @@ class FilesystemBlobsBackend(object): fbp = FileBodyProducer(request.content) yield fbp.startProducing(open(path, 'wb')) - def delete_blob(self, user, blob_id): - blob_path = self._get_path(user, blob_id) + def delete_blob(self, user, blob_id, namespace=''): + blob_path = self._get_path(user, blob_id, namespace) os.unlink(blob_path) - def get_blob_size(user, blob_id): + def get_blob_size(user, blob_id, namespace=''): raise NotImplementedError - def list_blobs(self, user, request): + def list_blobs(self, user, request, namespace=''): blob_ids = [] - base_path = self._get_path(user) + base_path = self._get_path(user, custom_preffix=namespace) for _, _, filenames in os.walk(base_path): blob_ids += filenames return json.dumps(blob_ids) @@ -112,8 +112,8 @@ class FilesystemBlobsBackend(object): def get_total_storage(self, user): return self._get_disk_usage(self._get_path(user)) - def add_tag_header(self, user, blob_id, request): - with open(self._get_path(user, blob_id)) as doc_file: + def add_tag_header(self, user, blob_id, request, namespace=''): + with open(self._get_path(user, blob_id, namespace)) as doc_file: doc_file.seek(-16, 2) tag = base64.urlsafe_b64encode(doc_file.read()) request.responseHeaders.setRawHeaders('Tag', [tag]) @@ -140,14 +140,19 @@ class FilesystemBlobsBackend(object): raise Exception(err) return desired_path - def _get_path(self, user, blob_id=False): + def _get_path(self, user, blob_id='', custom_preffix=''): parts = [user] + parts += self._get_preffix(blob_id, custom_preffix) if blob_id: - parts += [blob_id[0], blob_id[0:3], blob_id[0:6]] parts += [blob_id] path = os.path.join(self.path, *parts) return self._validate_path(path, user, blob_id) + def _get_preffix(self, blob_id, custom=''): + if custom or not blob_id: + return [custom] + return [blob_id[0], blob_id[0:3], blob_id[0:6]] + class ImproperlyConfiguredException(Exception): pass -- cgit v1.2.3