2015-08-26 21:08:42 +00:00
|
|
|
import cStringIO as StringIO
|
|
|
|
import hashlib
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2015-08-26 21:08:42 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from uuid import uuid4
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2015-08-26 21:08:42 +00:00
|
|
|
from storage.basestorage import BaseStorageV2
|
|
|
|
|
|
|
|
_FAKE_STORAGE_MAP = defaultdict(StringIO.StringIO)
|
|
|
|
|
|
|
|
class FakeStorage(BaseStorageV2):
|
2014-04-03 21:31:46 +00:00
|
|
|
def _init_path(self, path=None, create=False):
|
|
|
|
return path
|
|
|
|
|
|
|
|
def get_content(self, path):
|
2015-05-29 22:08:17 +00:00
|
|
|
if not path in _FAKE_STORAGE_MAP:
|
|
|
|
raise IOError('Fake file %s not found' % path)
|
|
|
|
|
2015-08-26 21:08:42 +00:00
|
|
|
_FAKE_STORAGE_MAP.get(path).seek(0)
|
|
|
|
return _FAKE_STORAGE_MAP.get(path).read()
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def put_content(self, path, content):
|
2015-08-26 21:08:42 +00:00
|
|
|
_FAKE_STORAGE_MAP.pop(path, None)
|
|
|
|
_FAKE_STORAGE_MAP[path].write(content)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def stream_read(self, path):
|
2015-08-26 21:08:42 +00:00
|
|
|
io_obj = _FAKE_STORAGE_MAP[path]
|
|
|
|
io_obj.seek(0)
|
|
|
|
while True:
|
|
|
|
buf = io_obj.read(self.buffer_size)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
yield buf
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
def stream_read_file(self, path):
|
|
|
|
return StringIO(_FAKE_STORAGE_MAP[path])
|
|
|
|
|
2014-09-11 19:33:10 +00:00
|
|
|
def stream_write(self, path, fp, content_type=None, content_encoding=None):
|
2015-08-26 21:08:42 +00:00
|
|
|
out_fp = _FAKE_STORAGE_MAP[path]
|
|
|
|
out_fp.seek(0)
|
|
|
|
self.stream_write_to_fp(fp, out_fp)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def remove(self, path):
|
2015-05-29 22:08:17 +00:00
|
|
|
_FAKE_STORAGE_MAP.pop(path, None)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def exists(self, path):
|
2015-05-29 22:08:17 +00:00
|
|
|
return path in _FAKE_STORAGE_MAP
|
2014-09-09 19:54:03 +00:00
|
|
|
|
|
|
|
def get_checksum(self, path):
|
2015-08-26 21:08:42 +00:00
|
|
|
return hashlib.sha256(_FAKE_STORAGE_MAP[path].read()).hexdigest()[:7]
|
|
|
|
|
|
|
|
def initiate_chunked_upload(self):
|
|
|
|
new_uuid = str(uuid4())
|
|
|
|
_FAKE_STORAGE_MAP[new_uuid].seek(0)
|
|
|
|
return new_uuid, {}
|
|
|
|
|
|
|
|
def stream_upload_chunk(self, uuid, offset, length, in_fp, _):
|
|
|
|
upload_storage = _FAKE_STORAGE_MAP[uuid]
|
|
|
|
upload_storage.seek(offset)
|
2015-08-27 18:55:33 +00:00
|
|
|
return self.stream_write_to_fp(in_fp, upload_storage, length), {}
|
2015-08-26 21:08:42 +00:00
|
|
|
|
|
|
|
def complete_chunked_upload(self, uuid, final_path, _):
|
|
|
|
_FAKE_STORAGE_MAP[final_path] = _FAKE_STORAGE_MAP[uuid]
|
|
|
|
_FAKE_STORAGE_MAP.pop(uuid, None)
|
|
|
|
|
|
|
|
def cancel_chunked_upload(self, uuid, _):
|
|
|
|
_FAKE_STORAGE_MAP.pop(uuid, None)
|