summaryrefslogtreecommitdiff
path: root/src/leap/soledad/server/_incoming.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/soledad/server/_incoming.py')
-rw-r--r--src/leap/soledad/server/_incoming.py45
1 files changed, 38 insertions, 7 deletions
diff --git a/src/leap/soledad/server/_incoming.py b/src/leap/soledad/server/_incoming.py
index 6fd91a08..6fdeb7ae 100644
--- a/src/leap/soledad/server/_incoming.py
+++ b/src/leap/soledad/server/_incoming.py
@@ -17,38 +17,61 @@
"""
A twisted resource that saves externally delivered documents into user's db.
"""
+from twisted.web.server import NOT_DONE_YET
from twisted.web.resource import Resource
from ._config import get_config
+from io import BytesIO
+from leap.soledad.server._blobs import BlobsServerState
from leap.soledad.common.couch.state import CouchServerState
from leap.soledad.common.document import ServerDocument
from leap.soledad.common.crypto import ENC_JSON_KEY
from leap.soledad.common.crypto import ENC_SCHEME_KEY
from leap.soledad.common.crypto import EncryptionSchemes
+from leap.soledad.common import preamble
__all__ = ['IncomingResource']
-def _default_backend():
+def _get_backend_from_config():
conf = get_config()
- return CouchServerState(conf['couch_url'], create_cmd=conf['create_cmd'])
+ if conf['blobs']:
+ return BlobsServerState("filesystem", conf['blobs_path'])
+ return CouchServerState(conf['couch_url'])
class IncomingResource(Resource):
isLeaf = True
def __init__(self, backend_factory=None):
- self.factory = backend_factory or _default_backend()
+ self.factory = backend_factory or _get_backend_from_config()
self.formatter = IncomingFormatter()
def render_PUT(self, request):
uuid, doc_id = request.postpath
scheme = EncryptionSchemes.PUBKEY
db = self.factory.open_database(uuid)
- doc = ServerDocument(doc_id)
- doc.content = self.formatter.format(request.content.read(), scheme)
- db.put_doc(doc)
- return '{"success": true}'
+ if hasattr(db, 'put_doc'):
+ doc = ServerDocument(doc_id)
+ doc.content = self.formatter.format(request.content.read(), scheme)
+ db.put_doc(doc)
+ self._finish(request)
+ else:
+ raw_content = request.content.read()
+ preamble = self.formatter.preamble(raw_content, doc_id)
+ request.content = BytesIO(preamble + raw_content)
+ d = db.write_blob(uuid, doc_id, request, namespace='incoming')
+ d.addCallback(lambda _: self._finish(request))
+ return NOT_DONE_YET
+
+ def _finish(self, request):
+ request.write('{"success": true}')
+ request.finish()
+
+ def _error(self, e, request):
+ request.write('{"success": false}')
+ request.setResponseCode(500)
+ request.finish()
class IncomingFormatter(object):
@@ -65,3 +88,11 @@ class IncomingFormatter(object):
self.ERROR_DECRYPTING_KEY: False,
ENC_SCHEME_KEY: EncryptionSchemes.NONE,
ENC_JSON_KEY: raw_content}
+
+ def preamble(self, raw_content, doc_id):
+ rev = '0'
+ scheme = preamble.ENC_SCHEME.external
+ method = preamble.ENC_METHOD.pgp
+ size = len(raw_content)
+ return preamble.Preamble(doc_id, rev, scheme, method,
+ content_size=size).encode()