1df676ab7fb7eba197db3bb98aeaab1776f2ccda
[mediagoblin.git] / mediagoblin / submit / views.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 mediagoblin.mg_globals as mg_globals
18 import uuid
19 from os.path import splitext
20 from cgi import FieldStorage
21
22 from celery import registry
23 import urllib,urllib2
24 import logging
25
26 _log = logging.getLogger(__name__)
27
28 from werkzeug.utils import secure_filename
29
30 from mediagoblin.db.util import ObjectId
31 from mediagoblin.tools.text import convert_to_tag_list_of_dicts
32 from mediagoblin.tools.translate import pass_to_ugettext as _
33 from mediagoblin.tools.response import render_to_response, redirect
34 from mediagoblin.decorators import require_active_login
35 from mediagoblin.submit import forms as submit_forms
36 from mediagoblin.processing import mark_entry_failed
37 from mediagoblin.processing.task import ProcessMedia
38 from mediagoblin.messages import add_message, SUCCESS
39 from mediagoblin.media_types import get_media_type_and_manager, \
40 InvalidFileType, FileTypeNotSupported
41
42
43 @require_active_login
44 def submit_start(request):
45 """
46 First view for submitting a file.
47 """
48 submit_form = submit_forms.SubmitStartForm(request.POST)
49
50 if request.method == 'POST' and submit_form.validate():
51 if not ('file' in request.POST
52 and isinstance(request.POST['file'], FieldStorage)
53 and request.POST['file'].file):
54 submit_form.file.errors.append(
55 _(u'You must provide a file.'))
56 else:
57 try:
58 filename = request.POST['file'].filename
59 media_type, media_manager = get_media_type_and_manager(filename)
60
61 # create entry and save in database
62 entry = request.db.MediaEntry()
63 entry.id = ObjectId()
64 entry.media_type = unicode(media_type)
65 entry.title = (
66 unicode(request.POST['title'])
67 or unicode(splitext(filename)[0]))
68
69 entry.description = unicode(request.POST.get('description'))
70
71 entry.license = unicode(request.POST.get('license', "")) or None
72
73 entry.uploader = request.user._id
74
75 # Process the user's folksonomy "tags"
76 entry.tags = convert_to_tag_list_of_dicts(
77 request.POST.get('tags'))
78
79 # Generate a slug from the title
80 entry.generate_slug()
81
82 # We generate this ourselves so we know what the taks id is for
83 # retrieval later.
84
85 # (If we got it off the task's auto-generation, there'd be
86 # a risk of a race condition when we'd save after sending
87 # off the task)
88 task_id = unicode(uuid.uuid4())
89
90 # Now store generate the queueing related filename
91 queue_filepath = request.app.queue_store.get_unique_filepath(
92 ['media_entries',
93 task_id,
94 secure_filename(filename)])
95
96 # queue appropriately
97 queue_file = request.app.queue_store.get_file(
98 queue_filepath, 'wb')
99
100 with queue_file:
101 queue_file.write(request.POST['file'].file.read())
102
103 # Add queued filename to the entry
104 entry.queued_media_file = queue_filepath
105
106 entry.queued_task_id = task_id
107
108 # Save now so we have this data before kicking off processing
109 entry.save(validate=True)
110
111 # Pass off to processing
112 #
113 # (... don't change entry after this point to avoid race
114 # conditions with changes to the document via processing code)
115 process_media = registry.tasks[ProcessMedia.name]
116 try:
117 process_media.apply_async(
118 [unicode(entry._id)], {},
119 task_id=task_id)
120 except BaseException as exc:
121 # The purpose of this section is because when running in "lazy"
122 # or always-eager-with-exceptions-propagated celery mode that
123 # the failure handling won't happen on Celery end. Since we
124 # expect a lot of users to run things in this way we have to
125 # capture stuff here.
126 #
127 # ... not completely the diaper pattern because the
128 # exception is re-raised :)
129 mark_entry_failed(entry._id, exc)
130 # re-raise the exception
131 raise
132
133 if mg_globals.app_config["push_urls"]:
134 feed_url=request.urlgen(
135 'mediagoblin.user_pages.atom_feed',
136 qualified=True,user=request.user.username)
137 hubparameters = {
138 'hub.mode': 'publish',
139 'hub.url': feed_url}
140 hubdata = urllib.urlencode(hubparameters)
141 hubheaders = {
142 "Content-type": "application/x-www-form-urlencoded",
143 "Connection": "close"}
144 for huburl in mg_globals.app_config["push_urls"]:
145 hubrequest = urllib2.Request(huburl, hubdata, hubheaders)
146 try:
147 hubresponse = urllib2.urlopen(hubrequest)
148 except urllib2.HTTPError as exc:
149 # This is not a big issue, the item will be fetched
150 # by the PuSH server next time we hit it
151 _log.warning(
152 "push url %r gave error %r", huburl, exc.code)
153 except urllib2.URLError as exc:
154 _log.warning(
155 "push url %r is unreachable %r", huburl, exc.reason)
156
157 add_message(request, SUCCESS, _('Woohoo! Submitted!'))
158
159 return redirect(request, "mediagoblin.user_pages.user_home",
160 user=request.user.username)
161 except Exception as e:
162 '''
163 This section is intended to catch exceptions raised in
164 mediagobling.media_types
165 '''
166
167 if isinstance(e, InvalidFileType) or \
168 isinstance(e, FileTypeNotSupported):
169 submit_form.file.errors.append(
170 e)
171 else:
172 raise
173
174 return render_to_response(
175 request,
176 'mediagoblin/submit/start.html',
177 {'submit_form': submit_form,
178 'app_config': mg_globals.app_config})