Added sniffing logic for image media type
[mediagoblin.git] / mediagoblin / media_types / image / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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/>.
16
17 import Image
18 import os
19 import logging
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import BadMediaFail, \
23 create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE
24 from mediagoblin.tools.exif import exif_fix_image_orientation, \
25 extract_exif, clean_exif, get_gps_data, get_useful
26
27 _log = logging.getLogger(__name__)
28
29 SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg']
30
31 def sniff_handler(media_file, **kw):
32 if not kw.get('media') == None: # That's a double negative!
33 name, ext = os.path.splitext(kw['media'].filename)
34 clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase
35
36 _log.debug('name: {0}\next: {1}\nlower_ext: {2}'.format(
37 name,
38 ext,
39 clean_ext))
40
41 if clean_ext in SUPPORTED_FILETYPES:
42 _log.info('Found file extension in supported filetypes')
43 return True
44 else:
45 _log.debug('Media present, extension not found in {1}'.format(
46 SUPPORTED_FILETYPES))
47 else:
48 _log.warning('Need additional information (keyword argument \'media\')'
49 ' to be able to handle sniffing')
50
51 return False
52
53 def process_image(entry):
54 """
55 Code to process an image
56 """
57 workbench = mgg.workbench_manager.create_workbench()
58 # Conversions subdirectory to avoid collisions
59 conversions_subdir = os.path.join(
60 workbench.dir, 'conversions')
61 os.mkdir(conversions_subdir)
62
63 queued_filepath = entry.queued_media_file
64 queued_filename = workbench.localized_file(
65 mgg.queue_store, queued_filepath,
66 'source')
67
68 filename_bits = os.path.splitext(queued_filename)
69 basename = os.path.split(filename_bits[0])[1]
70 extension = filename_bits[1].lower()
71
72 # EXIF extraction
73 exif_tags = extract_exif(queued_filename)
74 gps_data = get_gps_data(exif_tags)
75
76 try:
77 thumb = Image.open(queued_filename)
78 except IOError:
79 raise BadMediaFail()
80
81 thumb = exif_fix_image_orientation(thumb, exif_tags)
82
83 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
84
85 # Copy the thumb to the conversion subdir, then remotely.
86 thumb_filename = 'thumbnail' + extension
87 thumb_filepath = create_pub_filepath(entry, thumb_filename)
88
89 tmp_thumb_filename = os.path.join(
90 conversions_subdir, thumb_filename)
91
92 with file(tmp_thumb_filename, 'w') as thumb_file:
93 thumb.save(thumb_file)
94
95 mgg.public_store.copy_local_to_storage(
96 tmp_thumb_filename, thumb_filepath)
97
98 # If the size of the original file exceeds the specified size of a `medium`
99 # file, a `medium.jpg` files is created and later associated with the media
100 # entry.
101 medium = Image.open(queued_filename)
102
103 # Fix orientation
104 medium = exif_fix_image_orientation(medium, exif_tags)
105
106 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
107 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
108
109 medium_filename = 'medium' + extension
110 medium_filepath = create_pub_filepath(entry, medium_filename)
111
112 tmp_medium_filename = os.path.join(
113 conversions_subdir, medium_filename)
114
115 with file(tmp_medium_filename, 'w') as medium_file:
116 medium.save(medium_file)
117
118 mgg.public_store.copy_local_to_storage(
119 tmp_medium_filename, medium_filepath)
120
121 # we have to re-read because unlike PIL, not everything reads
122 # things in string representation :)
123 queued_file = file(queued_filename, 'rb')
124
125 with queued_file:
126 #create_pub_filepath(entry, queued_filepath[-1])
127 original_filepath = create_pub_filepath(entry, basename + extension)
128
129 with mgg.public_store.get_file(original_filepath, 'wb') \
130 as original_file:
131 original_file.write(queued_file.read())
132
133 # Remove queued media file from storage and database
134 mgg.queue_store.delete_file(queued_filepath)
135 entry.queued_media_file = []
136
137 # Insert media file information into database
138 media_files_dict = entry.setdefault('media_files', {})
139 media_files_dict['thumb'] = thumb_filepath
140 media_files_dict['original'] = original_filepath
141 media_files_dict['medium'] = medium_filepath
142
143 # Insert exif data into database
144 media_data = entry.setdefault('media_data', {})
145 media_data['exif'] = {
146 'clean': clean_exif(exif_tags)}
147 media_data['exif']['useful'] = get_useful(
148 media_data['exif']['clean'])
149 media_data['gps'] = gps_data
150
151 # clean up workbench
152 workbench.destroy_self()
153
154 if __name__ == '__main__':
155 import sys
156 import pprint
157
158 pp = pprint.PrettyPrinter()
159
160 result = extract_exif(sys.argv[1])
161 gps = get_gps_data(result)
162 clean = clean_exif(result)
163 useful = get_useful(clean)
164
165 print pp.pprint(
166 clean)