summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Shyba <victor1984@riseup.net>2017-07-13 02:02:39 -0300
committerdrebs <drebs@leap.se>2017-07-21 10:58:47 -0300
commit5d521e5a1c5fdd4cf926062dc26147dbcd110b05 (patch)
tree97a2e5e7bb20c3b01e89ca5ab79cc1cc124502e9
parent64107009a869a6ddb6cea129e65735d9740e697b (diff)
[feature] add possibility to count on blob manager
By using `only_count=True` parameter, instead of returning a full list, server just count the amount of blobs. -- Resolves: #8871
-rw-r--r--src/leap/soledad/server/_blobs.py11
-rw-r--r--src/leap/soledad/server/interfaces.py5
-rw-r--r--testing/tests/server/test_blobs_server.py13
3 files changed, 28 insertions, 1 deletions
diff --git a/src/leap/soledad/server/_blobs.py b/src/leap/soledad/server/_blobs.py
index 2a0e1d57..5abb0b02 100644
--- a/src/leap/soledad/server/_blobs.py
+++ b/src/leap/soledad/server/_blobs.py
@@ -141,6 +141,13 @@ class FilesystemBlobsBackend(object):
def get_blob_size(user, blob_id, namespace=''):
raise NotImplementedError
+ def count(self, user, request, namespace=''):
+ base_path = self._get_path(user, custom_preffix=namespace)
+ count = 0
+ for _, _, filenames in os.walk(base_path):
+ count += len(filenames)
+ return json.dumps({"count": count})
+
def list_blobs(self, user, request, namespace='', order_by=None):
blob_ids = []
base_path = self._get_path(user, custom_preffix=namespace)
@@ -227,7 +234,9 @@ class BlobsResource(resource.Resource):
def render_GET(self, request):
logger.info("http get: %s" % request.path)
user, blob_id, namespace = self._validate(request)
- if not blob_id:
+ if not blob_id and request.args.get('only_count', False):
+ return self._handler.count(user, request, namespace)
+ elif not blob_id:
order = request.args.get('order_by', [None])[0]
return self._handler.list_blobs(user, request, namespace,
order_by=order)
diff --git a/src/leap/soledad/server/interfaces.py b/src/leap/soledad/server/interfaces.py
index ee73aac8..1f97624b 100644
--- a/src/leap/soledad/server/interfaces.py
+++ b/src/leap/soledad/server/interfaces.py
@@ -49,6 +49,11 @@ class IBlobsBackend(Interface):
Get the size of the given blob id.
"""
+ def count(user, request, namespace=''):
+ """
+ Counts the total number of blobs.
+ """
+
def list_blobs(user, request, namespace='', order_by=None):
"""
Returns a json-encoded list of ids from user's blobs storage,
diff --git a/testing/tests/server/test_blobs_server.py b/testing/tests/server/test_blobs_server.py
index 0db64256..0696e14b 100644
--- a/testing/tests/server/test_blobs_server.py
+++ b/testing/tests/server/test_blobs_server.py
@@ -128,6 +128,19 @@ class BlobServerTestCase(unittest.TestCase):
@defer.inlineCallbacks
@pytest.mark.usefixtures("method_tmpdir")
+ def test_list_with_count_parameter(self):
+ manager = BlobManager('', self.uri, self.secret,
+ self.secret, 'user')
+ deferreds = []
+ for i in range(10):
+ deferreds.append(manager._encrypt_and_upload(str(i), BytesIO("1")))
+ yield defer.gatherResults(deferreds)
+
+ result = yield manager.remote_list(only_count=True)
+ self.assertEquals({"count": len(deferreds)}, result)
+
+ @defer.inlineCallbacks
+ @pytest.mark.usefixtures("method_tmpdir")
def test_list_restricted_by_namespace(self):
manager = BlobManager('', self.uri, self.secret,
self.secret, 'user')