1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 os
.path
import splitext
18 from cgi
import FieldStorage
20 from werkzeug
.utils
import secure_filename
22 from mediagoblin
.util
import (
23 render_to_response
, redirect
, cleaned_markdown_conversion
)
24 from mediagoblin
.decorators
import require_active_login
25 from mediagoblin
.submit
import forms
as submit_forms
, security
26 from mediagoblin
.process_media
import process_media_initial
27 from mediagoblin
.messages
import add_message
, SUCCESS
31 def submit_start(request
):
33 First view for submitting a file.
35 submit_form
= submit_forms
.SubmitStartForm(request
.POST
)
37 if request
.method
== 'POST' and submit_form
.validate():
38 if not (request
.POST
.has_key('file')
39 and isinstance(request
.POST
['file'], FieldStorage
)
40 and request
.POST
['file'].file):
41 submit_form
.file.errors
.append(
42 u
'You must provide a file.')
43 elif not security
.check_filetype(request
.POST
['file']):
44 submit_form
.file.errors
.append(
45 u
'The file doesn\'t seem to be an image!')
47 filename
= request
.POST
['file'].filename
49 # create entry and save in database
50 entry
= request
.db
.MediaEntry()
53 or unicode(splitext(filename
)[0]))
55 entry
['description'] = request
.POST
.get('description')
56 entry
['description_html'] = cleaned_markdown_conversion(
59 entry
['media_type'] = u
'image' # heh
60 entry
['uploader'] = request
.user
['_id']
62 # Save, just so we can get the entry id for the sake of using
63 # it to generate the file path
64 entry
.save(validate
=False)
66 # Generate a slug from the title
69 # Now store generate the queueing related filename
70 queue_filepath
= request
.app
.queue_store
.get_unique_filepath(
72 unicode(entry
['_id']),
73 secure_filename(filename
)])
76 queue_file
= request
.app
.queue_store
.get_file(
80 queue_file
.write(request
.POST
['file'].file.read())
82 # Add queued filename to the entry
83 entry
['queued_media_file'] = queue_filepath
84 entry
.save(validate
=True)
86 # queue it for processing
87 process_media_initial
.delay(unicode(entry
['_id']))
89 add_message(request
, SUCCESS
, 'Woohoo! Submitted!')
91 return redirect(request
, "mediagoblin.user_pages.user_home",
92 user
= request
.user
['username'])
94 return render_to_response(
96 'mediagoblin/submit/start.html',
97 {'submit_form': submit_form
})
100 def submit_success(request
):
101 return render_to_response(
102 request
, 'mediagoblin/submit/success.html', {})