2932c45581a68d7eef662b7b06ed54791383c571
[mediagoblin.git] / mediagoblin / media_types / image / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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
20 from celery.task import Task
21 from celery import registry
22
23 from mediagoblin.db.util import ObjectId
24 from mediagoblin import mg_globals as mgg
25
26 from mediagoblin.processing import BaseProcessingFail, \
27 mark_entry_failed, BadMediaFail, create_pub_filepath, THUMB_SIZE, \
28 MEDIUM_SIZE
29
30 ################################
31 # Media processing initial steps
32 ################################
33
34
35 def process_image(entry):
36 """
37 Code to process an image
38 """
39 workbench = mgg.workbench_manager.create_workbench()
40 # Conversions subdirectory to avoid collisions
41 conversions_subdir = os.path.join(
42 workbench.dir, 'conversions')
43 os.mkdir(conversions_subdir)
44
45 queued_filepath = entry['queued_media_file']
46 queued_filename = workbench.localized_file(
47 mgg.queue_store, queued_filepath,
48 'source')
49
50 extension = os.path.splitext(queued_filename)[1]
51
52 try:
53 thumb = Image.open(queued_filename)
54 except IOError:
55 raise BadMediaFail()
56
57 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
58
59 # Copy the thumb to the conversion subdir, then remotely.
60 thumb_filename = 'thumbnail' + extension
61 thumb_filepath = create_pub_filepath(entry, thumb_filename)
62 tmp_thumb_filename = os.path.join(
63 conversions_subdir, thumb_filename)
64 with file(tmp_thumb_filename, 'w') as thumb_file:
65 thumb.save(thumb_file)
66 mgg.public_store.copy_local_to_storage(
67 tmp_thumb_filename, thumb_filepath)
68
69 # If the size of the original file exceeds the specified size of a `medium`
70 # file, a `medium.jpg` files is created and later associated with the media
71 # entry.
72 medium = Image.open(queued_filename)
73 medium_processed = False
74
75 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
76 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
77
78 medium_filename = 'medium' + extension
79 medium_filepath = create_pub_filepath(entry, medium_filename)
80 tmp_medium_filename = os.path.join(
81 conversions_subdir, medium_filename)
82
83 with file(tmp_medium_filename, 'w') as medium_file:
84 medium.save(medium_file)
85
86 mgg.public_store.copy_local_to_storage(
87 tmp_medium_filename, medium_filepath)
88
89 medium_processed = True
90
91 # we have to re-read because unlike PIL, not everything reads
92 # things in string representation :)
93 queued_file = file(queued_filename, 'rb')
94
95 with queued_file:
96 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
97
98 with mgg.public_store.get_file(original_filepath, 'wb') \
99 as original_file:
100 original_file.write(queued_file.read())
101
102 mgg.queue_store.delete_file(queued_filepath)
103 entry['queued_media_file'] = []
104 media_files_dict = entry.setdefault('media_files', {})
105 media_files_dict['thumb'] = thumb_filepath
106 media_files_dict['original'] = original_filepath
107 if medium_processed:
108 media_files_dict['medium'] = medium_filepath
109
110 # clean up workbench
111 workbench.destroy_self()