Skip to content

Commit

Permalink
Merge 7b58ff6 into 19a895b
Browse files Browse the repository at this point in the history
  • Loading branch information
rclement committed Apr 18, 2018
2 parents 19a895b + 7b58ff6 commit 915a19f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
9 changes: 8 additions & 1 deletion flask_fs/backends/gridfs.py
Expand Up @@ -54,7 +54,14 @@ def read(self, filename):
return f.read()

def write(self, filename, content):
return self.fs.put(self.as_binary(content), filename=filename)
kwargs = {
'filename': filename
}

if hasattr(content, 'content_type') and content.content_type is not None:
kwargs['content_type'] = content.content_type

return self.fs.put(self.as_binary(content), **kwargs)

def delete(self, filename):
regex = '^{0}'.format(re.escape(filename))
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Expand Up @@ -52,8 +52,12 @@ def jpgfile():


class Utils(object):
def filestorage(self, filename, content):
return FileStorage(self.file(content), filename)
def filestorage(self, filename, content, content_type=None):
return FileStorage(
self.file(content),
filename,
content_type=content_type
)

def file(self, content):
if isinstance(content, six.binary_type):
Expand Down
37 changes: 37 additions & 0 deletions tests/test_gridfs_backend.py
Expand Up @@ -10,13 +10,26 @@
from flask_fs.storage import Config

import pytest
import mimetypes
import six


TEST_DB = 'fstest'


class GridFsBackendTest(BackendTestCase):
hasher = 'md5'

@pytest.fixture
def pngimage(self, pngfile):
with open(pngfile, 'rb') as f:
yield f

@pytest.fixture
def jpgimage(self, jpgfile):
with open(jpgfile, 'rb') as f:
yield f

@pytest.fixture(autouse=True)
def setup(self):
self.client = MongoClient()
Expand Down Expand Up @@ -58,3 +71,27 @@ def test_delete_with_versions(self, faker):

self.backend.delete(filename)
assert not self.file_exists(filename)

def test_write_pngimage(self, pngimage, utils):
filename = 'test.png'
content = six.binary_type(pngimage.read())
content_type = mimetypes.guess_type(filename)[0]
f = utils.filestorage(filename, content, content_type)
self.backend.write(filename, f)

with self.backend.open(filename, 'rb') as f:
assert f.content_type == content_type

self.assert_bin_equal(filename, content)

def test_write_jpgimage(self, jpgimage, utils):
filename = 'test.jpg'
content = six.binary_type(jpgimage.read())
content_type = mimetypes.guess_type(filename)[0]
f = utils.filestorage(filename, content, content_type)
self.backend.write(filename, f)

with self.backend.open(filename, 'rb') as f:
assert f.content_type == content_type

self.assert_bin_equal(filename, content)

0 comments on commit 915a19f

Please sign in to comment.