diff --git a/README.md b/README.md index 22bdf82..48631fc 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ I will sign everything with 0x769A1BC78A2DDEE2 ## CHANGELOG +- 2017-01-23 [Tyrel Souza] Everything should return a "filename" even if it's generated. Make the filename be the hash + ext. (fall back to .txt) +- 2017-01-20 [Tyrel Souza] Make path return None if no file - 2017-01-20 [Tyrel Souza] Make path return filename - 2017-01-20 [Tyrel Souza] Add another migration, and redo all the initial migrations. - 2017-01-20 [Tyrel Souza] Make sure migrations is actually there. diff --git a/dbfilestorage/storage.py b/dbfilestorage/storage.py index 16669dd..8814761 100644 --- a/dbfilestorage/storage.py +++ b/dbfilestorage/storage.py @@ -29,7 +29,7 @@ class DBFileStorage(Storage): """ def _open(self, name, mode='rb'): - the_file = DBFile.objects.get(pk=name) + the_file = _get_object(name) return ContentFile(the_file.b64.decode('base64')) @atomic @@ -58,23 +58,24 @@ class DBFileStorage(Storage): # After we get the mimetype by name potentially, mangle it. filehash = hashlib.md5(read_data).hexdigest() + file_ext = os.path.splitext(name)[1] + if not file_ext: + file_ext = ".txt" + name = "".join((filehash, file_ext)) + # create the file, or just return name if the exact file already exists - if not DBFile.objects.filter(pk=name).exists(): + if not DBFile.objects.filter(pk=filehash).exists(): the_file = DBFile( - name=os.path.basename(name), + name=name, filehash=filehash, content_type=ct, b64=b64) the_file.save() - return filehash + return name def get_available_name(self, name, max_length=None): return name - def path(self, name): - dbf = _get_object(name) - return dbf.name - def delete(self, name): assert name, "The name argument is not allowed to be empty." # name is the Pk, so it will be unique, deleting on the QS so @@ -82,11 +83,13 @@ class DBFileStorage(Storage): DBFile.objects.filter(pk=name).delete() def exists(self, name): - return DBFile.objects.filter(pk=name).exists() + return DBFile.objects.filter(Q(name=name)|Q(filehash=name)).exists() def size(self, name): - dbf = DBFile.objects.get(pk=name) + dbf = _get_object(name) return len(dbf.b64) def url(self, name): - return reverse('dbstorage_file', args=(name,)) + dbf = _get_object(name) + if dbf: + return reverse('dbstorage_file', args=(dbf.name,)) diff --git a/docs/conf.py b/docs/conf.py index fb008f8..fcd56c5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -54,9 +54,9 @@ author = u'Tyrel Souza' # built documents. # # The short X.Y version. -version = u'0.4.1' +version = u'0.4.2' # The full version, including alpha/beta/rc tags. -release = u'0.4.1' +release = u'0.4.2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index c2abf4a..6a7158c 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ class CleanCommand(Command): setup( name="django-dbfilestorage", - version="0.4.1", + version="0.4.2", description="Database backed file storage for testing.", long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.", author="Tyrel Souza", diff --git a/tests/tests.py b/tests/tests.py index 89386dd..85e032e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -22,6 +22,8 @@ class DBFileTest(TestCase): self.filename = "kris.jpg" self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename) self.md5 = hashlib.md5(open(self.filepath, 'rb').read()).hexdigest() + self.filehash_ext = self.md5 + ".jpg" + self._upload() def _upload(self): @@ -45,7 +47,7 @@ class DBFileTest(TestCase): """ Test that it won't make a new file if it already exists """ # uploads once in setup already name2 = self._upload() - self.assertEqual(self.md5, name2) + self.assertEqual(self.filehash_ext, name2) def test_equality(self): """ Test that the DB entry matches what is expected from the file """ @@ -74,12 +76,6 @@ class DBFileTest(TestCase): self.assertFalse(DBFile.objects.filter(name="Nothing").exists()) default_storage.delete("Nothing") - def test_path(self): - """ Test the path is just the filename, when passed md5 and name """ - path = default_storage.path(self.md5) - self.assertNotEqual(self.md5, path) - self.assertEqual(self.filename, path) - def test_size(self): """ Ensure we can get the proper size """ size = default_storage.size(self.md5) @@ -92,7 +88,7 @@ class DBFileTest(TestCase): def test_view(self): client = Client() # check it works for both md5 and filename - for param in (self.md5, self.filename): + for param in (self.md5, self.filehash_ext): url = default_storage.url(param) resp = client.get(url) self.assertEqual(resp.status_code, 200)