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/>.
17 from mediagoblin
import messages
18 import mediagoblin
.mg_globals
as mg_globals
20 from os
.path
import splitext
22 from celery
import registry
27 _log
= logging
.getLogger(__name__
)
29 from werkzeug
.utils
import secure_filename
30 from werkzeug
.datastructures
import FileStorage
32 from mediagoblin
.db
.util
import ObjectId
33 from mediagoblin
.tools
.text
import convert_to_tag_list_of_dicts
34 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
35 from mediagoblin
.tools
.response
import render_to_response
, redirect
36 from mediagoblin
.decorators
import require_active_login
37 from mediagoblin
.submit
import forms
as submit_forms
38 from mediagoblin
.processing
import mark_entry_failed
39 from mediagoblin
.processing
.task
import ProcessMedia
40 from mediagoblin
.messages
import add_message
, SUCCESS
41 from mediagoblin
.media_types
import sniff_media
, \
42 InvalidFileType
, FileTypeNotSupported
46 def submit_start(request
):
48 First view for submitting a file.
50 submit_form
= submit_forms
.SubmitStartForm(request
.form
)
52 if request
.method
== 'POST' and submit_form
.validate():
53 if not ('file' in request
.files
54 and isinstance(request
.files
['file'], FileStorage
)
55 and request
.files
['file'].stream
):
56 submit_form
.file.errors
.append(
57 _(u
'You must provide a file.'))
60 filename
= request
.files
['file'].filename
62 # Sniff the submitted media to determine which
63 # media plugin should handle processing
64 media_type
, media_manager
= sniff_media(
65 request
.files
['file'])
67 # create entry and save in database
68 entry
= request
.db
.MediaEntry()
70 entry
.media_type
= unicode(media_type
)
72 unicode(request
.form
['title'])
73 or unicode(splitext(filename
)[0]))
75 entry
.description
= unicode(request
.form
.get('description'))
77 entry
.license
= unicode(request
.form
.get('license', "")) or None
79 entry
.uploader
= request
.user
._id
81 # Process the user's folksonomy "tags"
82 entry
.tags
= convert_to_tag_list_of_dicts(
83 request
.form
.get('tags'))
85 # Generate a slug from the title
88 # We generate this ourselves so we know what the taks id is for
91 # (If we got it off the task's auto-generation, there'd be
92 # a risk of a race condition when we'd save after sending
94 task_id
= unicode(uuid
.uuid4())
96 # Now store generate the queueing related filename
97 queue_filepath
= request
.app
.queue_store
.get_unique_filepath(
100 secure_filename(filename
)])
102 # queue appropriately
103 queue_file
= request
.app
.queue_store
.get_file(
104 queue_filepath
, 'wb')
107 queue_file
.write(request
.files
['file'].stream
.read())
109 # Add queued filename to the entry
110 entry
.queued_media_file
= queue_filepath
112 entry
.queued_task_id
= task_id
114 # Save now so we have this data before kicking off processing
115 entry
.save(validate
=True)
117 # Pass off to processing
119 # (... don't change entry after this point to avoid race
120 # conditions with changes to the document via processing code)
121 process_media
= registry
.tasks
[ProcessMedia
.name
]
123 process_media
.apply_async(
124 [unicode(entry
._id
)], {},
126 except BaseException
as exc
:
127 # The purpose of this section is because when running in "lazy"
128 # or always-eager-with-exceptions-propagated celery mode that
129 # the failure handling won't happen on Celery end. Since we
130 # expect a lot of users to run things in this way we have to
131 # capture stuff here.
133 # ... not completely the diaper pattern because the
134 # exception is re-raised :)
135 mark_entry_failed(entry
._id
, exc
)
136 # re-raise the exception
139 if mg_globals
.app_config
["push_urls"]:
140 feed_url
= request
.urlgen(
141 'mediagoblin.user_pages.atom_feed',
143 user
=request
.user
.username
)
145 'hub.mode': 'publish',
147 hubdata
= urllib
.urlencode(hubparameters
)
149 "Content-type": "application/x-www-form-urlencoded",
150 "Connection": "close"}
151 for huburl
in mg_globals
.app_config
["push_urls"]:
152 hubrequest
= urllib2
.Request(huburl
, hubdata
, hubheaders
)
154 hubresponse
= urllib2
.urlopen(hubrequest
)
155 except urllib2
.HTTPError
as exc
:
156 # This is not a big issue, the item will be fetched
157 # by the PuSH server next time we hit it
159 "push url %r gave error %r", huburl
, exc
.code
)
160 except urllib2
.URLError
as exc
:
162 "push url %r is unreachable %r", huburl
, exc
.reason
)
164 add_message(request
, SUCCESS
, _('Woohoo! Submitted!'))
166 return redirect(request
, "mediagoblin.user_pages.user_home",
167 user
=request
.user
.username
)
168 except Exception as e
:
170 This section is intended to catch exceptions raised in
171 mediagoblin.media_types
173 if isinstance(e
, InvalidFileType
) or \
174 isinstance(e
, FileTypeNotSupported
):
175 submit_form
.file.errors
.append(
180 return render_to_response(
182 'mediagoblin/submit/start.html',
183 {'submit_form': submit_form
,
184 'app_config': mg_globals
.app_config
})
186 @require_active_login
187 def add_collection(request
, media
=None):
189 View to create a new collection
191 submit_form
= submit_forms
.AddCollectionForm(request
.form
)
193 if request
.method
== 'POST' and submit_form
.validate():
195 collection
= request
.db
.Collection()
196 collection
.id = ObjectId()
198 collection
.title
= unicode(request
.form
['title'])
200 collection
.description
= unicode(request
.form
.get('description'))
201 collection
.creator
= request
.user
._id
202 collection
.generate_slug()
204 # Make sure this user isn't duplicating an existing collection
205 existing_collection
= request
.db
.Collection
.find_one({
206 'creator': request
.user
._id
,
207 'title':collection
.title
})
209 if existing_collection
:
210 messages
.add_message(
211 request
, messages
.ERROR
, _('You already have a collection called "%s"!' % collection
.title
))
213 collection
.save(validate
=True)
215 add_message(request
, SUCCESS
, _('Collection "%s" added!' % collection
.title
))
217 return redirect(request
, "mediagoblin.user_pages.user_home",
218 user
=request
.user
.username
)
220 except Exception as e
:
223 return render_to_response(
225 'mediagoblin/submit/collection.html',
226 {'submit_form': submit_form
,
227 'app_config': mg_globals
.app_config
})