diff --git a/docs/changelog.rst b/docs/changelog.rst index f15c73605..4c40a71ab 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -11,6 +11,8 @@ paperless-ng 1.1.2 * 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`. + +* Fixed an issue with the metadata tab not reporting anything in case of missing files. paperless-ng 1.1.1 ################## diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 84b3bc185..20eba6628 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -577,8 +577,11 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): 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") - shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png"), doc.source_path) - shutil.copy(os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), doc.archive_path) + source_file = os.path.join(os.path.dirname(__file__), "samples", "documents", "thumbnails", "0000001.png") + 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/") self.assertEqual(response.status_code, 200) @@ -591,6 +594,8 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertGreater(len(meta['archive_metadata']), 0) self.assertEqual(meta['media_filename'], "file.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): 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_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): doc = Document.objects.create(title="test", mime_type="application/pdf") diff --git a/src/documents/views.py b/src/documents/views.py index 9d1d87235..b2e5b4cd3 100755 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -225,6 +225,12 @@ class DocumentViewSet(RetrieveModelMixin, else: 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) def metadata(self, request, pk=None): try: @@ -234,7 +240,7 @@ class DocumentViewSet(RetrieveModelMixin, meta = { "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, "media_filename": doc.filename, "has_archive_version": doc.has_archive_version, @@ -245,7 +251,7 @@ class DocumentViewSet(RetrieveModelMixin, } 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( doc.archive_path, "application/pdf") else: