wqadded new way to get mime type

This commit is contained in:
2023-12-21 13:36:13 +01:00
parent f8383567fe
commit e6c59507c4
5 changed files with 53 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ pytest:
image: python:3.12-alpine image: python:3.12-alpine
script: script:
- sh tests/start_tests_gitlab.sh - sh tests/start_tests_gitlab.sh
- sed -i "s#<source>/builds/DasMoorhuhn/autopicture-v3/src</source>#<source>${CI_PROJECT_DIR}</source>#g" coverage.xml # - sed -i "s#<source>/builds/DasMoorhuhn/autopicture-v3/src</source>#<source>${CI_PROJECT_DIR}</source>#g" coverage.xml
coverage: '/Code coverage: \d+(?:\.\d+)?/' coverage: '/Code coverage: \d+(?:\.\d+)?/'
artifacts: artifacts:
name: "$CI_JOB_NAME" name: "$CI_JOB_NAME"

View File

@@ -4,6 +4,7 @@ python-magic==0.4.*
progressbar==2.5 progressbar==2.5
virtualenv==20.25.* virtualenv==20.25.*
requests==2.31.* requests==2.31.*
filetype>=1.0.7
pytest==7.4.* pytest==7.4.*
pytest-cov==4.1.* pytest-cov==4.1.*
pytest-factoryboy==2.5.* pytest-factoryboy==2.5.*

View File

@@ -1,17 +1,28 @@
import magic import magic
import sys import sys
import filetype
from PIL import Image from PIL import Image
from PIL import ExifTags from PIL import ExifTags
sys.path.append("../") sys.path.append("../")
from src.exif_data import ExifData from src.exif_data import ExifData
from src.mime_types import MimeTypes
video_formats = ["MP4", "MOV", "M4V", "MKV", "AVI", "WMV", "AVCHD", "WEBM", "MPEG"] video_formats = ["MP4", "MOV", "M4V", "MKV", "AVI", "WMV", "AVCHD", "WEBM", "MPEG"]
picture_formats = ["JPG", "JPEG", "PNG", "TIFF"] picture_formats = ["JPG", "JPEG", "PNG", "TIFF"]
key_words = ["DateTime", "Make"] key_words = ["DateTime", "Make"]
def check_file_type(path:str):
# file_type = magic.from_file(mime=True, filename=path)
file_type = ""
if filetype.is_image(path):
file_type = "image"
elif filetype.is_video(path):
file_type = "video"
return MimeTypes(file_type)
def is_file_video(path:str): def is_file_video(path:str):
mime = magic.Magic(mime=True) mime = magic.Magic(mime=True)
file = mime.from_file(path) file = mime.from_file(path)
@@ -30,6 +41,7 @@ def get_image_meta_data(image_path):
image_extension = str(image_path).split("/")[-1].split(".") image_extension = str(image_path).split("/")[-1].split(".")
# TODO: Sort out videos # TODO: Sort out videos
if is_file_video(path=image_path): return False if is_file_video(path=image_path): return False
# if not is_file_picture(path=image_path): return False
img = Image.open(f"{image_path}") img = Image.open(f"{image_path}")
values = [] values = []
for tag, text in img.getexif().items(): for tag, text in img.getexif().items():

14
src/mime_types.py Normal file
View File

@@ -0,0 +1,14 @@
class MimeTypes:
def __init__(self, file_type):
self.image = False
self.video = False
self.unsupported_file_type = False
self.__proceed(file_type)
def __proceed(self, file_type):
if file_type == "image":
self.image = True
elif file_type == "video":
self.video = True
else:
self.unsupported_file_type = True

24
tests/test_mime_types.py Normal file
View File

@@ -0,0 +1,24 @@
import unittest
from src.mime_types import MimeTypes
class TestMimeTypes(unittest.TestCase):
def test_mime_type_image(self):
mime_type = MimeTypes(file_type="image")
assert mime_type.image
assert not mime_type.video
assert not mime_type.unsupported_file_type
def test_mime_type_video(self):
mime_type = MimeTypes(file_type="video")
assert not mime_type.image
assert mime_type.video
assert not mime_type.unsupported_file_type
def test_mime_type_unsupported_file_type(self):
mime_type = MimeTypes(file_type="not_a_valid_file_type")
assert not mime_type.image
assert not mime_type.video
assert mime_type.unsupported_file_type