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 werkzeug
.utils
import secure_filename
20 from werkzeug
.datastructures
import FileStorage
22 from mediagoblin
.processing
import mark_entry_failed
23 from mediagoblin
.processing
.task
import process_media
26 _log
= logging
.getLogger(__name__
)
29 def check_file_field(request
, field_name
):
30 """Check if a file field meets minimal criteria"""
31 retval
= (field_name
in request
.files
32 and isinstance(request
.files
[field_name
], FileStorage
)
33 and request
.files
[field_name
].stream
)
35 _log
.debug("Form did not contain proper file field %s", field_name
)
39 def prepare_queue_task(app
, entry
, filename
):
41 Prepare a MediaEntry for the processing queue and get a queue file
43 # We generate this ourselves so we know what the taks id is for
46 # (If we got it off the task's auto-generation, there'd be
47 # a risk of a race condition when we'd save after sending
49 task_id
= unicode(uuid
.uuid4())
50 entry
.queued_task_id
= task_id
52 # Now store generate the queueing related filename
53 queue_filepath
= app
.queue_store
.get_unique_filepath(
56 secure_filename(filename
)])
59 queue_file
= app
.queue_store
.get_file(
62 # Add queued filename to the entry
63 entry
.queued_media_file
= queue_filepath
68 def run_process_media(entry
, feed_url
=None):
69 """Process the media asynchronously
71 :param entry: MediaEntry() instance to be processed.
72 :param feed_url: A string indicating the feed_url that the PuSH servers
73 should be notified of. This will be sth like: `request.urlgen(
74 'mediagoblin.user_pages.atom_feed',qualified=True,
75 user=request.user.username)`"""
77 process_media
.apply_async(
78 [entry
.id, feed_url
], {},
79 task_id
=entry
.queued_task_id
)
80 except BaseException
as exc
:
81 # The purpose of this section is because when running in "lazy"
82 # or always-eager-with-exceptions-propagated celery mode that
83 # the failure handling won't happen on Celery end. Since we
84 # expect a lot of users to run things in this way we have to
87 # ... not completely the diaper pattern because the
88 # exception is re-raised :)
89 mark_entry_failed(entry
.id, exc
)
90 # re-raise the exception