metadata tab not showing anything if files are missing #534

This commit is contained in:
jonaswinkler 2021-02-13 16:41:03 +01:00
parent 8b2965d55b
commit 5f0e800f6e
3 changed files with 32 additions and 4 deletions

View File

@ -11,6 +11,8 @@ paperless-ng 1.1.2
* Always show top left corner of thumbnails, even for extra wide documents. * Always show top left corner of thumbnails, even for extra wide documents.
* Added a management command for executing the sanity checker directly. See :ref:`utilities-sanity-checker`. * Added a management command for executing the sanity checker directly. See :ref:`utilities-sanity-checker`.
* Fixed an issue with the metadata tab not reporting anything in case of missing files.
paperless-ng 1.1.1 paperless-ng 1.1.1
################## ##################

View File

@ -577,8 +577,11 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
def test_get_metadata(self): def test_get_metadata(self):
doc = Document.objects.create(title="test", filename="file.pdf", mime_type="image/png", archive_checksum="A", archive_filename="archive.pdf") doc = Document.objects.create(title="test", filename="file.pdf", mime_type="image/png", archive_checksum="A", archive_filename="archive.pdf")
shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png"), doc.source_path) source_file = os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png")
shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), doc.archive_path) archive_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
shutil.copy(source_file, doc.source_path)
shutil.copy(archive_file, doc.archive_path)
response = self.client.get(f"/api/documents/{doc.pk}/metadata/") response = self.client.get(f"/api/documents/{doc.pk}/metadata/")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -591,6 +594,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertGreater(len(meta['archive_metadata']), 0) self.assertGreater(len(meta['archive_metadata']), 0)
self.assertEqual(meta['media_filename'], "file.pdf") self.assertEqual(meta['media_filename'], "file.pdf")
self.assertEqual(meta['archive_media_filename'], "archive.pdf") self.assertEqual(meta['archive_media_filename'], "archive.pdf")
self.assertEqual(meta['original_size'], os.stat(source_file).st_size)
self.assertEqual(meta['archive_size'], os.stat(archive_file).st_size)
def test_get_metadata_invalid_doc(self): def test_get_metadata_invalid_doc(self):
response = self.client.get(f"/api/documents/34576/metadata/") response = self.client.get(f"/api/documents/34576/metadata/")
@ -612,6 +617,21 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertIsNone(meta['archive_metadata']) self.assertIsNone(meta['archive_metadata'])
self.assertIsNone(meta['archive_media_filename']) self.assertIsNone(meta['archive_media_filename'])
def test_get_metadata_missing_files(self):
doc = Document.objects.create(title="test", filename="file.pdf", mime_type="application/pdf", archive_filename="file.pdf", archive_checksum="B", checksum="A")
response = self.client.get(f"/api/documents/{doc.pk}/metadata/")
self.assertEqual(response.status_code, 200)
meta = response.data
self.assertTrue(meta['has_archive_version'])
self.assertIsNone(meta['original_metadata'])
self.assertIsNone(meta['original_size'])
self.assertIsNone(meta['archive_metadata'])
self.assertIsNone(meta['archive_size'])
def test_get_empty_suggestions(self): def test_get_empty_suggestions(self):
doc = Document.objects.create(title="test", mime_type="application/pdf") doc = Document.objects.create(title="test", mime_type="application/pdf")

View File

@ -225,6 +225,12 @@ class DocumentViewSet(RetrieveModelMixin,
else: else:
return [] return []
def get_filesize(self, filename):
if os.path.isfile(filename):
return os.stat(filename).st_size
else:
return None
@action(methods=['get'], detail=True) @action(methods=['get'], detail=True)
def metadata(self, request, pk=None): def metadata(self, request, pk=None):
try: try:
@ -234,7 +240,7 @@ class DocumentViewSet(RetrieveModelMixin,
meta = { meta = {
"original_checksum": doc.checksum, "original_checksum": doc.checksum,
"original_size": os.stat(doc.source_path).st_size, "original_size": self.get_filesize(doc.source_path),
"original_mime_type": doc.mime_type, "original_mime_type": doc.mime_type,
"media_filename": doc.filename, "media_filename": doc.filename,
"has_archive_version": doc.has_archive_version, "has_archive_version": doc.has_archive_version,
@ -245,7 +251,7 @@ class DocumentViewSet(RetrieveModelMixin,
} }
if doc.has_archive_version: if doc.has_archive_version:
meta['archive_size'] = os.stat(doc.archive_path).st_size, meta['archive_size'] = self.get_filesize(doc.archive_path)
meta['archive_metadata'] = self.get_metadata( meta['archive_metadata'] = self.get_metadata(
doc.archive_path, "application/pdf") doc.archive_path, "application/pdf")
else: else: