1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
27 _log
= logging
.getLogger(__name__
)
29 SUPPORTED_FILETYPES
= ['png', 'gif', 'jpg', 'jpeg']
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
36 _log
.debug('name: {0}\next: {1}\nlower_ext: {2}'.format(
41 if clean_ext
in SUPPORTED_FILETYPES
:
42 _log
.info('Found file extension in supported filetypes')
45 _log
.debug('Media present, extension not found in {0}'.format(
48 _log
.warning('Need additional information (keyword argument \'media\')'
49 ' to be able to handle sniffing')
53 def process_image(entry
):
55 Code to process an image
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
)
63 queued_filepath
= entry
.queued_media_file
64 queued_filename
= workbench
.localized_file(
65 mgg
.queue_store
, queued_filepath
,
68 filename_bits
= os
.path
.splitext(queued_filename
)
69 basename
= os
.path
.split(filename_bits
[0])[1]
70 extension
= filename_bits
[1].lower()
73 exif_tags
= extract_exif(queued_filename
)
74 gps_data
= get_gps_data(exif_tags
)
77 thumb
= Image
.open(queued_filename
)
81 thumb
= exif_fix_image_orientation(thumb
, exif_tags
)
83 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
85 # Copy the thumb to the conversion subdir, then remotely.
86 thumb_filename
= 'thumbnail' + extension
87 thumb_filepath
= create_pub_filepath(entry
, thumb_filename
)
89 tmp_thumb_filename
= os
.path
.join(
90 conversions_subdir
, thumb_filename
)
92 with
file(tmp_thumb_filename
, 'w') as thumb_file
:
93 thumb
.save(thumb_file
)
95 mgg
.public_store
.copy_local_to_storage(
96 tmp_thumb_filename
, thumb_filepath
)
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
101 medium
= Image
.open(queued_filename
)
104 medium
= exif_fix_image_orientation(medium
, exif_tags
)
106 if medium
.size
[0] > MEDIUM_SIZE
[0] or medium
.size
[1] > MEDIUM_SIZE
[1]:
107 medium
.thumbnail(MEDIUM_SIZE
, Image
.ANTIALIAS
)
109 medium_filename
= 'medium' + extension
110 medium_filepath
= create_pub_filepath(entry
, medium_filename
)
112 tmp_medium_filename
= os
.path
.join(
113 conversions_subdir
, medium_filename
)
115 with
file(tmp_medium_filename
, 'w') as medium_file
:
116 medium
.save(medium_file
)
118 mgg
.public_store
.copy_local_to_storage(
119 tmp_medium_filename
, medium_filepath
)
121 # we have to re-read because unlike PIL, not everything reads
122 # things in string representation :)
123 queued_file
= file(queued_filename
, 'rb')
126 #create_pub_filepath(entry, queued_filepath[-1])
127 original_filepath
= create_pub_filepath(entry
, basename
+ extension
)
129 with mgg
.public_store
.get_file(original_filepath
, 'wb') \
131 original_file
.write(queued_file
.read())
133 # Remove queued media file from storage and database
134 mgg
.queue_store
.delete_file(queued_filepath
)
135 entry
.queued_media_file
= []
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
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
152 workbench
.destroy_self()
154 if __name__
== '__main__':
158 pp
= pprint
.PrettyPrinter()
160 result
= extract_exif(sys
.argv
[1])
161 gps
= get_gps_data(result
)
162 clean
= clean_exif(result
)
163 useful
= get_useful(clean
)