Commit 90112667 authored by Laurent Bachelier's avatar Laurent Bachelier

Add ability to force a mimetype

This will help with special files (for instance events) to have a
correct available views list.
parent 18b83156
......@@ -63,6 +63,7 @@ class File(IObject):
self.path = path
self.view = None
self.mimetype = None
self.perms = {}
IObject.__init__(self, storage)
......@@ -159,6 +160,8 @@ class File(IObject):
return posixpath.basename(self.path)
def get_mimetype(self):
if self.mimetype:
return self.mimetype
return mimetypes.guess_type(self.get_name())[0]
def get_hash(self):
......@@ -169,6 +172,7 @@ class File(IObject):
def _postread(self):
self.view = self.data['info'].get('view', None)
self.mimetype = self.data['info'].get('mimetype', None)
if self.view in ["", "None"]:
self.view = None
self.perms.clear()
......@@ -180,6 +184,10 @@ class File(IObject):
self.data['info']['view'] = self.view
else:
self.data['info'].pop('view', None)
if self.mimetype is not None:
self.data['info']['mimetype'] = self.mimetype
else:
self.data['info'].pop('mimetype', None)
self.data['info']['path'] = self.path
self.data['perms'] = self.perms
......
......@@ -292,3 +292,14 @@ class StorageTest(TestCase):
assert Chafouin(4 * 1024 + 128).get_human_size() == '4.1 KiB'
assert Chafouin(4 * 1024 * 1024).get_human_size() == '4.0 MiB'
assert Chafouin(42 * 1024 * 1024 * 1024).get_human_size() == '42.0 GiB'
def test_getMimetype(self):
with open(os.path.join(self.root, 'penguin.txt'), 'w') as f:
f.write('')
f = self.storage.get_file('/penguin.txt')
assert f.get_mimetype() == 'text/plain'
f.mimetype = 'text/penguin'
f.save()
f = self.storage.get_file('/penguin.txt')
assert f.get_mimetype() == 'text/penguin'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment