Index: main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py	(revision 8436)
+++ main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py	(revision 8466)
@@ -19,4 +19,5 @@
 """
 import datetime
+import imghdr
 import os
 import pytz
@@ -588,2 +589,52 @@
         dt = pytz.utc.localize(dt)
     return tz.normalize(dt.tzinfo.normalize(dt).astimezone(tz))
+
+def get_fileformat(path, bytestream=None):
+    """Try to determine the file format of a given media file.
+
+    Although checks done here are not done very thoroughly, they make
+    no assumptions about the filetype by looking at its filename
+    extension or similar. Instead they check header data to comply
+    with common known rules (Magic Words).
+
+    If bytestream is not `None` the `path` is ignored.
+
+    Returns filetype as string (something like ``'jpg'``) if
+    file-format can be recognized, ``None`` else.
+
+    Tested recognized filetypes currently are `jpg`, `png`, and `pdf`.
+
+    More filetypes (though untested in waeup.kofa) are automatically
+    recognized because we deploy the stdlib `imghdr` library. See this
+    module's docs for a complete list of filetypes recognized.
+    """
+    if path is None and bytestream is None:
+        return None
+
+    img_type = None
+    if bytestream is not None:
+        img_type = imghdr.what(path, bytestream)
+    else:
+        img_type = imghdr.what(path)
+    for name, replacement in (('jpeg', 'jpg'), ('tiff', 'tif')):
+        if img_type == name:
+            img_type = replacement
+    return img_type
+
+def check_pdf(bytestream, file):
+    """Tell whether a file or bytestream is a PDF file.
+
+    Works as a test/plugin for the stdlib `imghdr` library.
+    """
+    if file is not None:
+        file.seek(0)
+        bytestream = file.read(4)
+        file.seek(0)
+
+    if bytestream.startswith('%PDF'):
+        return 'pdf'
+    return None
+
+# register check_pdf as header check function with `imghdr`
+if check_pdf not in imghdr.tests:
+    imghdr.tests.append(check_pdf)
Index: main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py
===================================================================
--- main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py	(revision 8436)
+++ main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py	(revision 8466)
@@ -317,4 +317,59 @@
         return
 
+class GetFileFormatTestCase(unittest.TestCase):
+    # Tests for the get_fileformat helper.
+
+    def setUp(self):
+        self.valid_jpg_path = os.path.join(
+            os.path.dirname(__file__), 'sample_jpg_valid.jpg')
+        self.valid_jpg = open(self.valid_jpg_path, 'rb').read()
+        self.valid_png_path = os.path.join(
+            os.path.dirname(__file__), 'sample_png_valid.png')
+        self.valid_png = open(self.valid_png_path, 'rb').read()
+        self.valid_pdf_path = os.path.join(
+            os.path.dirname(__file__), 'sample_pdf_valid.pdf')
+        self.valid_pdf = open(self.valid_pdf_path, 'rb').read()
+        return
+
+    def test_none(self):
+        # ``None`` is not a file and not a valid file format
+        self.assertEqual(helpers.get_fileformat(None), None)
+        return
+
+    def test_path_and_bytestream(self):
+        # get_fileformat accepts bytestreams and paths as arg.
+        self.assertEqual(
+            helpers.get_fileformat(None, self.valid_jpg), 'jpg')
+        self.assertEqual(
+            helpers.get_fileformat(self.valid_jpg_path), 'jpg')
+        # path is ignored when giving a bytestream
+        self.assertEqual(
+            helpers.get_fileformat('blah', self.valid_jpg), 'jpg')
+        return
+
+    def test_jpg(self):
+        # we recognize jpeg images.
+        self.assertEqual(
+            helpers.get_fileformat(self.valid_jpg_path), 'jpg')
+        self.assertEqual(
+            helpers.get_fileformat(None, self.valid_jpg), 'jpg')
+        return
+
+    def test_png(self):
+        # we recognize png images.
+        self.assertEqual(
+            helpers.get_fileformat(self.valid_png_path), 'png')
+        self.assertEqual(
+            helpers.get_fileformat(None, self.valid_png), 'png')
+        return
+
+    def test_pdf(self):
+        # we recognize pdf documents.
+        self.assertEqual(
+            helpers.get_fileformat(self.valid_pdf_path), 'pdf')
+        self.assertEqual(
+            helpers.get_fileformat(None, self.valid_pdf), 'pdf')
+        return
+
 def test_suite():
     suite = unittest.TestSuite()
@@ -330,4 +385,5 @@
         IfaceNamesTestCase,
         DateTimeHelpersTestCase,
+        GetFileFormatTestCase,
         ]:
         suite.addTests(
