Switching the hook 'get_media_manager' to a more "directed" tuple-hook
[mediagoblin.git] / mediagoblin / media_types / image / __init__.py
CommitLineData
93bdab9d 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
93bdab9d
JW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
33ddf4b1
CAW
16import datetime
17
f84425c0 18from mediagoblin.media_types import MediaManagerBase
ec4261a4
JW
19from mediagoblin.media_types.image.processing import process_image, \
20 sniff_handler
58a94757
RE
21from mediagoblin.tools import pluginapi
22
23
24ACCEPTED_EXTENSIONS = ["jpg", "jpeg", "png", "gif", "tiff"]
25MEDIA_TYPE = 'mediagoblin.media_types.image'
26
27
28def setup_plugin():
e6991972 29 config = pluginapi.get_config('mediagoblin.media_types.image')
93bdab9d
JW
30
31
f84425c0
E
32class ImageMediaManager(MediaManagerBase):
33 human_readable = "Image"
34 processor = staticmethod(process_image)
f84425c0
E
35 display_template = "mediagoblin/media_displays/image.html"
36 default_thumb = "images/media_thumbs/image.png"
58a94757 37
f84425c0 38 media_fetch_order = [u'medium', u'original', u'thumb']
58a94757 39
33ddf4b1
CAW
40 def get_original_date(self):
41 """
42 Get the original date and time from the EXIF information. Returns
43 either a datetime object or None (if anything goes wrong)
44 """
45 if not self.entry.media_data or not self.entry.media_data.exif_all:
46 return None
47
48 try:
49 # Try wrapped around all since exif_all might be none,
50 # EXIF DateTimeOriginal or printable might not exist
51 # or strptime might not be able to parse date correctly
52 exif_date = self.entry.media_data.exif_all[
53 'EXIF DateTimeOriginal']['printable']
54 original_date = datetime.datetime.strptime(
55 exif_date,
56 '%Y:%m:%d %H:%M:%S')
57 return original_date
58 except (KeyError, ValueError):
59 return None
60
ddbf6af1 61
58a94757
RE
62def get_media_type_and_manager(ext):
63 if ext in ACCEPTED_EXTENSIONS:
e6991972 64 return MEDIA_TYPE, ImageMediaManager
58a94757
RE
65
66
67hooks = {
68 'setup': setup_plugin,
e6991972 69 'get_media_type_and_manager': get_media_type_and_manager,
58a94757 70 'sniff_handler': sniff_handler,
6403bc92 71 ('media_manager', MEDIA_TYPE): lambda: ImageMediaManager,
58a94757 72}