diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 2bd47c6da..ddbe474a7 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -264,6 +264,12 @@ class Consumer(object): Tag.objects.get_or_create(slug=t, defaults={"name": t})[0]) return tuple(r) + def get_suffix(suffix): + suffix = suffix.lower() + if suffix == "jpeg": + return "jpg" + return suffix + # First attempt: " - - <tags>.<suffix>" m = re.match(self.REGEX_SENDER_TITLE_TAGS, parseable) if m: @@ -271,17 +277,22 @@ class Consumer(object): get_sender(m.group(1)), m.group(2), get_tags(m.group(3)), - m.group(4) + get_suffix(m.group(4)) ) # Second attempt: "<sender> - <title>.<suffix>" m = re.match(self.REGEX_SENDER_TITLE, parseable) if m: - return get_sender(m.group(1)), m.group(2), (), m.group(3) + return ( + get_sender(m.group(1)), + m.group(2), + (), + get_suffix(m.group(3)) + ) # That didn't work, so we assume sender and tags are None m = re.match(self.REGEX_TITLE, parseable) - return None, m.group(1), (), m.group(2) + return None, m.group(1), (), get_suffix(m.group(2)) def _store(self, text, doc): diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index 6db501e02..04f92f98c 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -7,15 +7,23 @@ class TestAttachment(TestCase): TAGS = ("tag1", "tag2", "tag3") CONSUMER = Consumer() + SUFFIXES = ( + "pdf", "png", "jpg", "jpeg", "gif", + "PDF", "PNG", "JPG", "JPEG", "GIF", + "PdF", "PnG", "JpG", "JPeG", "GiF", + ) def _test_guess_attributes_from_name(self, path, sender, title, tags): - for suffix in ("pdf", "png", "jpg", "jpeg", "gif"): + for suffix in self.SUFFIXES: f = path.format(suffix) results = self.CONSUMER._guess_attributes_from_name(f) self.assertEqual(results[0].name, sender, f) self.assertEqual(results[1], title, f) self.assertEqual(tuple([t.slug for t in results[2]]), tags, f) - self.assertEqual(results[3], suffix, f) + if suffix.lower() == "jpeg": + self.assertEqual(results[3], "jpg", f) + else: + self.assertEqual(results[3], suffix.lower(), f) def test_guess_attributes_from_name0(self): self._test_guess_attributes_from_name( diff --git a/src/documents/views.py b/src/documents/views.py index bcce6f677..26642c9fc 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -13,7 +13,7 @@ from .models import Sender, Tag, Document from .serialisers import SenderSerializer, TagSerializer, DocumentSerializer -class PdfView(DetailView): +class FetchView(DetailView): model = Document diff --git a/src/paperless/urls.py b/src/paperless/urls.py index b7ffe17dc..fd1af065d 100644 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -21,7 +21,7 @@ from django.contrib import admin from rest_framework.routers import DefaultRouter from documents.views import ( - PdfView, PushView, SenderViewSet, TagViewSet, DocumentViewSet) + FetchView, PushView, SenderViewSet, TagViewSet, DocumentViewSet) router = DefaultRouter() router.register(r'senders', SenderViewSet) @@ -38,7 +38,7 @@ urlpatterns = [ url(r"^api/", include(router.urls, namespace="drf")), # File downloads - url(r"^fetch/(?P<pk>\d+)$", PdfView.as_view(), name="fetch"), + url(r"^fetch/(?P<pk>\d+)$", FetchView.as_view(), name="fetch"), # The Django admin url(r"", admin.site.urls),