refactor list of know file types/mimes
This commit is contained in:
parent
41ef49293e
commit
3777999a0b
|
@ -5,12 +5,12 @@ import cherrypy
|
|||
from urllib.parse import urlparse
|
||||
from datetime import datetime, timedelta
|
||||
from photoapp.thumb import ThumbGenerator
|
||||
from photoapp.types import Photo, PhotoSet, Tag, TagItem, PhotoStatus, User
|
||||
from photoapp.types import Photo, PhotoSet, Tag, TagItem, PhotoStatus, User, mime2ext
|
||||
from photoapp.dbsession import DatabaseSession
|
||||
from photoapp.common import pwhash
|
||||
from photoapp.api import PhotosApi, LibraryManager
|
||||
from photoapp.dbutils import SAEnginePlugin, SATool, db, get_db_engine, date_format
|
||||
from photoapp.utils import mime2ext, auth, require_auth, photoset_auth_filter, slugify
|
||||
from photoapp.utils import auth, require_auth, photoset_auth_filter, slugify
|
||||
from photoapp.storage import uri_to_storage
|
||||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||
from sqlalchemy import desc, func, and_, or_
|
||||
|
|
|
@ -3,31 +3,79 @@ from sqlalchemy.orm import relationship
|
|||
from sqlalchemy.schema import UniqueConstraint
|
||||
from photoapp.dbutils import Base
|
||||
from datetime import datetime
|
||||
import enum as py_enum
|
||||
import uuid
|
||||
import enum
|
||||
|
||||
|
||||
class fcategory(py_enum.Enum):
|
||||
image = 0
|
||||
raw = 1
|
||||
video = 2
|
||||
|
||||
|
||||
ftypes = dict(jpg=dict(category=fcategory.image,
|
||||
extensions={"jpeg"},
|
||||
mimes={"image/jpeg"}),
|
||||
png=dict(category=fcategory.image,
|
||||
mimes={"image/png"}),
|
||||
gif=dict(category=fcategory.image,
|
||||
mimes={"image/gif"}),
|
||||
cr2=dict(category=fcategory.raw,
|
||||
mimes={"image/x-canon-cr2"}),
|
||||
xmp=dict(category=fcategory.raw,
|
||||
mimes={"application/octet-stream-xmp"}),
|
||||
psd=dict(category=fcategory.raw,
|
||||
mimes={"image/vnd.adobe.photoshop"}),
|
||||
mp4=dict(category=fcategory.video,
|
||||
mimes={"audio/mp4",
|
||||
"video/mp4"}),
|
||||
mov=dict(category=fcategory.video,
|
||||
mimes={"video/quicktime"}))
|
||||
|
||||
# set various defaults in ftypes
|
||||
# it should look like:
|
||||
# ftypes = {
|
||||
# <default_extension>: {
|
||||
# "category": <fcategory key>, # required, fcategory type
|
||||
# "extensions": {<*default_extension|optional extensions>}, # optional, defaults to set([<default_extension>])
|
||||
# "mimes": { # required, mimes mapped to this type
|
||||
# <mime string>
|
||||
# },
|
||||
# "type": <str(*image|video)>, # optional, defaults to str("image")
|
||||
# },
|
||||
# }
|
||||
for extension, entry in ftypes.items():
|
||||
entry["extensions"] = entry.get("extensions", set()) | set([extension])
|
||||
entry["type"] = entry.get("type", "image")
|
||||
|
||||
|
||||
# file extensions we allow
|
||||
known_extensions = ["jpg", "png", "cr2", "xmp", "mp4", "mov"]
|
||||
|
||||
# categorizaiton of media type based on extension
|
||||
regular_images = ["jpg", "png"] # we can pull metadata out of these
|
||||
files_raw = ["cr2", "xmp"] # treated as black boxes
|
||||
files_video = ["mp4", "mov"]
|
||||
|
||||
# extensions with well-known aliases
|
||||
mapped_extensions = {"jpg": {"jpeg", }} # target: aliases
|
||||
known_extensions = set.union(*[i["extensions"] for i in ftypes.values()])
|
||||
|
||||
# allowed file types (based on magic identification)
|
||||
# TODO enforce this
|
||||
known_mimes = {"image/png",
|
||||
"image/jpeg",
|
||||
"image/gif",
|
||||
"application/octet-stream-xmp",
|
||||
"image/x-canon-cr2",
|
||||
"audio/mp4",
|
||||
"video/mp4",
|
||||
"video/quicktime"}
|
||||
known_mimes = set.union(*[i["mimes"] for i in ftypes.values()])
|
||||
|
||||
# categorizaiton of media type based on extension
|
||||
# we can pull metadata out of these
|
||||
# jpg, png, gif etc
|
||||
regular_images = set([extension for extension, ftype in ftypes.items() if ftype["category"] == fcategory.image])
|
||||
# "derived" files, treated as black boxes, we can't open them because proprietary
|
||||
# cr2, xmp, etc
|
||||
files_raw = set([extension for extension, ftype in ftypes.items() if ftype["category"] == fcategory.raw])
|
||||
# video types
|
||||
# mp4, mov, etc
|
||||
files_video = set([extension for extension, ftype in ftypes.items() if ftype["category"] == fcategory.video])
|
||||
|
||||
|
||||
def mime2ext(mime):
|
||||
"""
|
||||
Given a mime type return the canonical file extension
|
||||
"""
|
||||
for ext, ftype in ftypes.items():
|
||||
if mime in ftype["mimes"]:
|
||||
return ext
|
||||
raise Exception(f"Unknown mime: '{mime}'")
|
||||
|
||||
|
||||
def genuuid():
|
||||
|
@ -35,10 +83,10 @@ def genuuid():
|
|||
|
||||
|
||||
def map_extension(ext):
|
||||
for target, aliases in mapped_extensions.items():
|
||||
if ext in aliases:
|
||||
return target
|
||||
return ext
|
||||
for known_ext, ftype in ftypes.items():
|
||||
if ext in ftype["extensions"]:
|
||||
return known_ext
|
||||
raise Exception(f"Unknown extension: '{ext}'")
|
||||
|
||||
|
||||
def generate_storage_path(timestamp, sha, extension):
|
||||
|
|
|
@ -25,20 +25,6 @@ def shasum(fpin):
|
|||
return sha.hexdigest()
|
||||
|
||||
|
||||
def mime2ext(mime):
|
||||
"""
|
||||
Given a mime type return the canonical file extension
|
||||
"""
|
||||
return {"image/png": "png",
|
||||
"image/jpeg": "jpg",
|
||||
"image/gif": "gif",
|
||||
"application/octet-stream-xmp": "xmp",
|
||||
"image/x-canon-cr2": "cr2",
|
||||
"audio/mp4": "mp4",
|
||||
"video/mp4": "mp4",
|
||||
"video/quicktime": "mov"}[mime]
|
||||
|
||||
|
||||
def get_extension(fname):
|
||||
parts = os.path.basename(fname).split(".")
|
||||
if len(parts) == 1:
|
||||
|
|
|
@ -1,30 +1,31 @@
|
|||
appdirs==1.4.3
|
||||
backports.functools-lru-cache==1.5
|
||||
boto3==1.9.183
|
||||
botocore==1.12.183
|
||||
certifi==2019.6.16
|
||||
appdirs==1.4.4
|
||||
boto3==1.14.56
|
||||
botocore==1.17.56
|
||||
certifi==2020.6.20
|
||||
chardet==3.0.4
|
||||
cheroot==6.5.2
|
||||
CherryPy==18.1.2
|
||||
contextlib2==0.5.5
|
||||
docutils==0.14
|
||||
idna==2.8
|
||||
jaraco.functools==1.20
|
||||
Jinja2==2.10.1
|
||||
jmespath==0.9.4
|
||||
MarkupSafe==1.0
|
||||
more-itertools==4.3.0
|
||||
Pillow==5.2.0
|
||||
portend==2.3
|
||||
PyMySQL==0.9.3
|
||||
python-dateutil==2.8.0
|
||||
python-magic==0.4.15
|
||||
pytz==2018.5
|
||||
requests==2.22.0
|
||||
s3transfer==0.2.1
|
||||
six==1.11.0
|
||||
SQLAlchemy==1.3.5
|
||||
tabulate==0.8.3
|
||||
tempora==1.13
|
||||
urllib3==1.25.3
|
||||
zc.lockfile==1.3.0
|
||||
cheroot==8.4.5
|
||||
CherryPy==18.6.0
|
||||
docutils==0.15.2
|
||||
idna==2.10
|
||||
jaraco.classes==3.1.0
|
||||
jaraco.collections==3.0.0
|
||||
jaraco.functools==3.0.1
|
||||
jaraco.text==3.2.0
|
||||
Jinja2==2.11.2
|
||||
jmespath==0.10.0
|
||||
MarkupSafe==1.1.1
|
||||
more-itertools==8.5.0
|
||||
Pillow==7.2.0
|
||||
portend==2.6
|
||||
PyMySQL==0.10.0
|
||||
python-dateutil==2.8.1
|
||||
python-magic==0.4.18
|
||||
pytz==2020.1
|
||||
requests==2.24.0
|
||||
s3transfer==0.3.3
|
||||
six==1.15.0
|
||||
SQLAlchemy==1.3.19
|
||||
tabulate==0.8.7
|
||||
tempora==4.0.0
|
||||
urllib3==1.25.10
|
||||
zc.lockfile==2.0
|
||||
|
|
Loading…
Reference in New Issue