diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-12-03 19:07:24 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-12-03 20:12:51 +0200 |
commit | 4e9b1ddfae711ce38d743a3faccf49447ab10f75 (patch) | |
tree | 104858cae517e80b1507c0519fc21885c2a605f2 /ick2/blobapi.py | |
parent | 107ffc0a60de703d84957cf6d8948ed7d61d7362 (diff) | |
download | ick2-4e9b1ddfae711ce38d743a3faccf49447ab10f75.tar.gz |
Add: blob service
Diffstat (limited to 'ick2/blobapi.py')
-rw-r--r-- | ick2/blobapi.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/ick2/blobapi.py b/ick2/blobapi.py new file mode 100644 index 0000000..1772a27 --- /dev/null +++ b/ick2/blobapi.py @@ -0,0 +1,62 @@ +# Copyright 2017 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import ick2 + + +class BlobAPI: + + def __init__(self): + self._blobs = ick2.BlobStore() + + def get_blob_directory(self): + return self._blobs.get_blob_directory() + + def set_blob_directory(self, dirname): + self._blobs.set_blob_directory(dirname) + + def find_missing_route(self, missing_path): + path = '/blobs/<blob_id>' + return [ + { + 'method': 'PUT', + 'path': path, + 'callback': self.put_blob, + }, + { + 'method': 'GET', + 'path': path, + 'callback': self.get_blob, + }, + ] + + def put_blob(self, content_type, body, blob_id, **kwargs): + ick2.log.log( + 'info', msg_text='blob uploaded', blob_id=blob_id, kwargs=kwargs) + self._blobs.store_blob(blob_id, body) + return ick2.OK('') + + def get_blob(self, content_type, body, blob_id, **kwargs): + ick2.log.log( + 'info', msg_text='blob requested', blob_id=blob_id, kwargs=kwargs) + try: + blob = self._blobs.get_blob(blob_id) + except EnvironmentError as e: + return ick2.not_found(str(e)) + headers = { + 'Content-Type': 'application/octet-stream', + } + return ick2.OK(blob, headers=headers) |