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/>.
19 from celery
.task
import Task
21 from mediagoblin
.db
.util
import ObjectId
22 from mediagoblin
import mg_globals
as mgg
24 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
26 from mediagoblin
.media_types
import get_media_manager
28 _log
= logging
.getLogger(__name__
)
31 MEDIUM_SIZE
= 640, 640
34 def create_pub_filepath(entry
, filename
):
35 return mgg
.public_store
.get_unique_filepath(
41 ################################
42 # Media processing initial steps
43 ################################
45 class ProcessMedia(Task
):
47 DEPRECATED -- This now resides in the individual media plugins
49 Pass this entry off for processing.
51 def run(self
, media_id
):
53 Pass the media entry off to the appropriate processing function
54 (for now just process_image...)
56 entry
= mgg
.database
.MediaEntry
.one(
57 {'_id': ObjectId(media_id
)})
59 # Try to process, and handle expected errors.
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
)
68 except ImportError, exc
:
70 'Entry {0} failed to process due to an import error: {1}'\
75 mark_entry_failed(entry
._id
, exc
)
77 entry
.state
= u
'processed'
80 def on_failure(self
, exc
, task_id
, args
, kwargs
, einfo
):
82 If the processing failed we should mark that in the database.
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.
90 mark_entry_failed(entry_id
, exc
)
93 def mark_entry_failed(entry_id
, exc
):
95 Mark a media entry as having failed in its conversion.
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.
103 - entry_id: The id of the media entry
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(
113 {'$set': {u
'state': u
'failed',
114 u
'fail_error': exc
.exception_path
,
115 u
'fail_metadata': exc
.metadata
}})
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
122 mgg
.database
['media_entries'].update(
124 {'$set': {u
'state': u
'failed',
126 u
'fail_metadata': {}}})
129 class BaseProcessingFail(Exception):
131 Base exception that all other processing failure messages should
134 You shouldn't call this itself; instead you should subclass it
135 and provid the exception_path and general_message applicable to
138 general_message
= u
''
141 def exception_path(self
):
143 self
.__class
__.__module
__, self
.__class
__.__name
__)
145 def __init__(self
, **metadata
):
146 self
.metadata
= metadata
or {}
149 class BadMediaFail(BaseProcessingFail
):
151 Error that should be raised when an inappropriate file was given
152 for the media type specified.
154 general_message
= _(u
'Invalid file given for media type.')