1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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/>.
17 from celery
.task
import Task
19 from mediagoblin
.db
.util
import ObjectId
20 from mediagoblin
import mg_globals
as mgg
22 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
24 from mediagoblin
.media_types
import get_media_manager
28 MEDIUM_SIZE
= 640, 640
31 def create_pub_filepath(entry
, filename
):
32 return mgg
.public_store
.get_unique_filepath(
38 ################################
39 # Media processing initial steps
40 ################################
42 class ProcessMedia(Task
):
44 DEPRECATED -- This now resides in the individual media plugins
46 Pass this entry off for processing.
48 def run(self
, media_id
):
50 Pass the media entry off to the appropriate processing function
51 (for now just process_image...)
53 entry
= mgg
.database
.MediaEntry
.one(
54 {'_id': ObjectId(media_id
)})
56 # Try to process, and handle expected errors.
58 #__import__(entry['media_type'])
59 manager
= get_media_manager(entry
['media_type'])
60 manager
['processor'](entry
)
61 except BaseProcessingFail
, exc
:
62 mark_entry_failed(entry
._id
, exc
)
64 except ImportError, exc
:
65 mark_entry_failed(entry
[u
'_id'], exc
)
67 entry
['state'] = u
'processed'
70 def on_failure(self
, exc
, task_id
, args
, kwargs
, einfo
):
72 If the processing failed we should mark that in the database.
74 Assuming that the exception raised is a subclass of
75 BaseProcessingFail, we can use that to get more information
76 about the failure and store that for conveying information to
77 users about the failure, etc.
80 mark_entry_failed(entry_id
, exc
)
83 def mark_entry_failed(entry_id
, exc
):
85 Mark a media entry as having failed in its conversion.
87 Uses the exception that was raised to mark more information. If
88 the exception is a derivative of BaseProcessingFail then we can
89 store extra information that can be useful for users telling them
90 why their media failed to process.
93 - entry_id: The id of the media entry
96 # Was this a BaseProcessingFail? In other words, was this a
97 # type of error that we know how to handle?
98 if isinstance(exc
, BaseProcessingFail
):
99 # Looks like yes, so record information about that failure and any
100 # metadata the user might have supplied.
101 mgg
.database
['media_entries'].update(
103 {'$set': {u
'state': u
'failed',
104 u
'fail_error': exc
.exception_path
,
105 u
'fail_metadata': exc
.metadata
}})
107 # Looks like no, so just mark it as failed and don't record a
108 # failure_error (we'll assume it wasn't handled) and don't record
109 # metadata (in fact overwrite it if somehow it had previous info
111 mgg
.database
['media_entries'].update(
113 {'$set': {u
'state': u
'failed',
115 u
'fail_metadata': {}}})
118 class BaseProcessingFail(Exception):
120 Base exception that all other processing failure messages should
123 You shouldn't call this itself; instead you should subclass it
124 and provid the exception_path and general_message applicable to
127 general_message
= u
''
130 def exception_path(self
):
132 self
.__class
__.__module
__, self
.__class
__.__name
__)
134 def __init__(self
, **metadata
):
135 self
.metadata
= metadata
or {}
138 class BadMediaFail(BaseProcessingFail
):
140 Error that should be raised when an inappropriate file was given
141 for the media type specified.
143 general_message
= _(u
'Invalid file given for media type.')