Merge branch 'newlayout' into newlayout-stage
[mediagoblin.git] / mediagoblin / 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 logging
18
19 from celery.task import Task
20
21 from mediagoblin.db.util import ObjectId
22 from mediagoblin import mg_globals as mgg
23
24 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
25
26 from mediagoblin.media_types import get_media_manager
27
28 _log = logging.getLogger(__name__)
29
30 THUMB_SIZE = 180, 180
31 MEDIUM_SIZE = 640, 640
32
33
34 def create_pub_filepath(entry, filename):
35 return mgg.public_store.get_unique_filepath(
36 ['media_entries',
37 unicode(entry._id),
38 filename])
39
40
41 ################################
42 # Media processing initial steps
43 ################################
44
45 class ProcessMedia(Task):
46 """
47 DEPRECATED -- This now resides in the individual media plugins
48
49 Pass this entry off for processing.
50 """
51 def run(self, media_id):
52 """
53 Pass the media entry off to the appropriate processing function
54 (for now just process_image...)
55 """
56 entry = mgg.database.MediaEntry.one(
57 {'_id': ObjectId(media_id)})
58
59 # Try to process, and handle expected errors.
60 try:
61 #__import__(entry.media_type)
62 manager = get_media_manager(entry.media_type)
63 _log.debug('Processing {0}'.format(entry))
64 manager['processor'](entry)
65 except BaseProcessingFail, exc:
66 mark_entry_failed(entry._id, exc)
67 return
68 except ImportError, exc:
69 _log.error(
70 'Entry {0} failed to process due to an import error: {1}'\
71 .format(
72 entry.title,
73 exc))
74
75 mark_entry_failed(entry._id, exc)
76
77 entry.state = u'processed'
78 entry.save()
79
80 def on_failure(self, exc, task_id, args, kwargs, einfo):
81 """
82 If the processing failed we should mark that in the database.
83
84 Assuming that the exception raised is a subclass of
85 BaseProcessingFail, we can use that to get more information
86 about the failure and store that for conveying information to
87 users about the failure, etc.
88 """
89 entry_id = args[0]
90 mark_entry_failed(entry_id, exc)
91
92
93 def mark_entry_failed(entry_id, exc):
94 """
95 Mark a media entry as having failed in its conversion.
96
97 Uses the exception that was raised to mark more information. If
98 the exception is a derivative of BaseProcessingFail then we can
99 store extra information that can be useful for users telling them
100 why their media failed to process.
101
102 Args:
103 - entry_id: The id of the media entry
104
105 """
106 # Was this a BaseProcessingFail? In other words, was this a
107 # type of error that we know how to handle?
108 if isinstance(exc, BaseProcessingFail):
109 # Looks like yes, so record information about that failure and any
110 # metadata the user might have supplied.
111 mgg.database['media_entries'].update(
112 {'_id': entry_id},
113 {'$set': {u'state': u'failed',
114 u'fail_error': exc.exception_path,
115 u'fail_metadata': exc.metadata}})
116 else:
117 _log.warn("No idea what happened here, but it failed: %r", exc)
118 # Looks like no, so just mark it as failed and don't record a
119 # failure_error (we'll assume it wasn't handled) and don't record
120 # metadata (in fact overwrite it if somehow it had previous info
121 # here)
122 mgg.database['media_entries'].update(
123 {'_id': entry_id},
124 {'$set': {u'state': u'failed',
125 u'fail_error': None,
126 u'fail_metadata': {}}})
127
128
129 class BaseProcessingFail(Exception):
130 """
131 Base exception that all other processing failure messages should
132 subclass from.
133
134 You shouldn't call this itself; instead you should subclass it
135 and provid the exception_path and general_message applicable to
136 this error.
137 """
138 general_message = u''
139
140 @property
141 def exception_path(self):
142 return u"%s:%s" % (
143 self.__class__.__module__, self.__class__.__name__)
144
145 def __init__(self, **metadata):
146 self.metadata = metadata or {}
147
148
149 class BadMediaFail(BaseProcessingFail):
150 """
151 Error that should be raised when an inappropriate file was given
152 for the media type specified.
153 """
154 general_message = _(u'Invalid file given for media type.')