Feature #571 - Closing storage objects - Removed closing(), renamed
[mediagoblin.git] / mediagoblin / process_media / __init__.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
19 from celery.task import Task
20 from celery import registry
21
22 from mediagoblin.db.util import ObjectId
23 from mediagoblin import mg_globals as mgg
24 from mediagoblin.process_media.errors import BaseProcessingFail, BadMediaFail
25
26
27 THUMB_SIZE = 180, 180
28 MEDIUM_SIZE = 640, 640
29
30
31 def create_pub_filepath(entry, filename):
32 return mgg.public_store.get_unique_filepath(
33 ['media_entries',
34 unicode(entry['_id']),
35 filename])
36
37
38 ################################
39 # Media processing initial steps
40 ################################
41
42 class ProcessMedia(Task):
43 """
44 Pass this entry off for processing.
45 """
46 def run(self, media_id):
47 """
48 Pass the media entry off to the appropriate processing function
49 (for now just process_image...)
50 """
51 entry = mgg.database.MediaEntry.one(
52 {'_id': ObjectId(media_id)})
53
54 # Try to process, and handle expected errors.
55 try:
56 process_image(entry)
57 except BaseProcessingFail, exc:
58 mark_entry_failed(entry[u'_id'], exc)
59 return
60
61 entry['state'] = u'processed'
62 entry.save()
63
64 def on_failure(self, exc, task_id, args, kwargs, einfo):
65 """
66 If the processing failed we should mark that in the database.
67
68 Assuming that the exception raised is a subclass of BaseProcessingFail,
69 we can use that to get more information about the failure and store that
70 for conveying information to users about the failure, etc.
71 """
72 entry_id = args[0]
73 mark_entry_failed(entry_id, exc)
74
75
76 process_media = registry.tasks[ProcessMedia.name]
77
78
79 def mark_entry_failed(entry_id, exc):
80 """
81 Mark a media entry as having failed in its conversion.
82
83 Uses the exception that was raised to mark more information. If the
84 exception is a derivative of BaseProcessingFail then we can store extra
85 information that can be useful for users telling them why their media failed
86 to process.
87
88 Args:
89 - entry_id: The id of the media entry
90
91 """
92 # Was this a BaseProcessingFail? In other words, was this a
93 # type of error that we know how to handle?
94 if isinstance(exc, BaseProcessingFail):
95 # Looks like yes, so record information about that failure and any
96 # metadata the user might have supplied.
97 mgg.database['media_entries'].update(
98 {'_id': entry_id},
99 {'$set': {u'state': u'failed',
100 u'fail_error': exc.exception_path,
101 u'fail_metadata': exc.metadata}})
102 else:
103 # Looks like no, so just mark it as failed and don't record a
104 # failure_error (we'll assume it wasn't handled) and don't record
105 # metadata (in fact overwrite it if somehow it had previous info
106 # here)
107 mgg.database['media_entries'].update(
108 {'_id': entry_id},
109 {'$set': {u'state': u'failed',
110 u'fail_error': None,
111 u'fail_metadata': {}}})
112
113
114 def process_image(entry):
115 """
116 Code to process an image
117 """
118 workbench = mgg.workbench_manager.create_workbench()
119
120 queued_filepath = entry['queued_media_file']
121 queued_filename = workbench.localized_file(
122 mgg.queue_store, queued_filepath,
123 'source')
124
125 try:
126 thumb = Image.open(queued_filename)
127 except IOError:
128 raise BadMediaFail()
129
130 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
131 # ensure color mode is compatible with jpg
132 if thumb.mode != "RGB":
133 thumb = thumb.convert("RGB")
134
135 thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
136 thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
137
138 with thumb_file:
139 thumb.save(thumb_file, "JPEG", quality=90)
140
141 # If the size of the original file exceeds the specified size of a `medium`
142 # file, a `medium.jpg` files is created and later associated with the media
143 # entry.
144 medium = Image.open(queued_filename)
145 medium_processed = False
146
147 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
148 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
149
150 if medium.mode != "RGB":
151 medium = medium.convert("RGB")
152
153 medium_filepath = create_pub_filepath(entry, 'medium.jpg')
154 medium_file = mgg.public_store.get_file(medium_filepath, 'w')
155
156 with medium_file:
157 medium.save(medium_file, "JPEG", quality=90)
158 medium_processed = True
159
160 # we have to re-read because unlike PIL, not everything reads
161 # things in string representation :)
162 queued_file = file(queued_filename, 'rb')
163
164 with queued_file:
165 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
166
167 with mgg.public_store.get_file(original_filepath, 'wb') as original_file:
168 original_file.write(queued_file.read())
169
170 mgg.queue_store.delete_file(queued_filepath)
171 entry['queued_media_file'] = []
172 media_files_dict = entry.setdefault('media_files', {})
173 media_files_dict['thumb'] = thumb_filepath
174 media_files_dict['original'] = original_filepath
175 if medium_processed:
176 media_files_dict['medium'] = medium_filepath
177
178 # clean up workbench
179 workbench.destroy_self()