Merge branch 'master' into OPW-Moderation-Update
[mediagoblin.git] / mediagoblin / submit / lib.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 import uuid
19 from werkzeug.utils import secure_filename
20 from werkzeug.datastructures import FileStorage
21
22 from mediagoblin.db.models import MediaEntry
23 from mediagoblin.processing import mark_entry_failed
24 from mediagoblin.processing.task import ProcessMedia
25
26
27 _log = logging.getLogger(__name__)
28
29
30 def check_file_field(request, field_name):
31 """Check if a file field meets minimal criteria"""
32 retval = (field_name in request.files
33 and isinstance(request.files[field_name], FileStorage)
34 and request.files[field_name].stream)
35 if not retval:
36 _log.debug("Form did not contain proper file field %s", field_name)
37 return retval
38
39
40 def new_upload_entry(user):
41 """
42 Create a new MediaEntry for uploading
43 """
44 entry = MediaEntry()
45 entry.uploader = user.id
46 entry.license = user.license_preference
47 return entry
48
49
50 def prepare_queue_task(app, entry, filename):
51 """
52 Prepare a MediaEntry for the processing queue and get a queue file
53 """
54 # We generate this ourselves so we know what the task id is for
55 # retrieval later.
56
57 # (If we got it off the task's auto-generation, there'd be
58 # a risk of a race condition when we'd save after sending
59 # off the task)
60 task_id = unicode(uuid.uuid4())
61 entry.queued_task_id = task_id
62
63 # Now store generate the queueing related filename
64 queue_filepath = app.queue_store.get_unique_filepath(
65 ['media_entries',
66 task_id,
67 secure_filename(filename)])
68
69 # queue appropriately
70 queue_file = app.queue_store.get_file(
71 queue_filepath, 'wb')
72
73 # Add queued filename to the entry
74 entry.queued_media_file = queue_filepath
75
76 return queue_file
77
78
79 def run_process_media(entry, feed_url=None,
80 reprocess_action="initial", reprocess_info=None):
81 """Process the media asynchronously
82
83 :param entry: MediaEntry() instance to be processed.
84 :param feed_url: A string indicating the feed_url that the PuSH servers
85 should be notified of. This will be sth like: `request.urlgen(
86 'mediagoblin.user_pages.atom_feed',qualified=True,
87 user=request.user.username)`
88 :param reprocess_action: What particular action should be run.
89 :param reprocess_info: A dict containing all of the necessary reprocessing
90 info for the given media_type"""
91 try:
92 ProcessMedia().apply_async(
93 [entry.id, feed_url, reprocess_action, reprocess_info], {},
94 task_id=entry.queued_task_id)
95 except BaseException as exc:
96 # The purpose of this section is because when running in "lazy"
97 # or always-eager-with-exceptions-propagated celery mode that
98 # the failure handling won't happen on Celery end. Since we
99 # expect a lot of users to run things in this way we have to
100 # capture stuff here.
101 #
102 # ... not completely the diaper pattern because the
103 # exception is re-raised :)
104 mark_entry_failed(entry.id, exc)
105 # re-raise the exception
106 raise