Commit | Line | Data |
---|---|---|
a062149e JW |
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 json | |
4504dbba | 18 | import logging |
4504dbba JW |
19 | |
20 | from os.path import splitext | |
62d14bf5 | 21 | from werkzeug.exceptions import BadRequest, Forbidden |
74af60bb | 22 | from werkzeug.wrappers import Response |
a062149e | 23 | |
4504dbba | 24 | from mediagoblin.decorators import require_active_login |
4504dbba | 25 | from mediagoblin.meddleware.csrf import csrf_exempt |
111a609d | 26 | from mediagoblin.media_types import sniff_media |
42c83752 JW |
27 | from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable, \ |
28 | json_response | |
2ef2f46e | 29 | from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \ |
6c1467d5 | 30 | run_process_media, new_upload_entry |
a062149e | 31 | |
4504dbba JW |
32 | _log = logging.getLogger(__name__) |
33 | ||
34 | ||
35 | @csrf_exempt | |
36 | @api_auth | |
37 | @require_active_login | |
38 | def post_entry(request): | |
39 | _log.debug('Posting entry') | |
09e528ac JW |
40 | |
41 | if request.method == 'OPTIONS': | |
42 | return json_response({'status': 200}) | |
43 | ||
4504dbba | 44 | if request.method != 'POST': |
09e528ac | 45 | _log.debug('Must POST against post_entry') |
cfa92229 | 46 | raise BadRequest() |
4504dbba | 47 | |
2ef2f46e | 48 | if not check_file_field(request, 'file'): |
09e528ac | 49 | _log.debug('File field not found') |
cfa92229 | 50 | raise BadRequest() |
4504dbba | 51 | |
111a609d | 52 | media_file = request.files['file'] |
4504dbba JW |
53 | |
54 | media_type, media_manager = sniff_media(media_file) | |
55 | ||
6c1467d5 | 56 | entry = new_upload_entry(request.user) |
4504dbba | 57 | entry.media_type = unicode(media_type) |
111a609d | 58 | entry.title = unicode(request.form.get('title') |
4504dbba JW |
59 | or splitext(media_file.filename)[0]) |
60 | ||
111a609d JW |
61 | entry.description = unicode(request.form.get('description')) |
62 | entry.license = unicode(request.form.get('license', '')) | |
4504dbba | 63 | |
4504dbba JW |
64 | entry.generate_slug() |
65 | ||
4504dbba | 66 | # queue appropriately |
b228d897 | 67 | queue_file = prepare_queue_task(request.app, entry, media_file.filename) |
4504dbba JW |
68 | |
69 | with queue_file: | |
111a609d | 70 | queue_file.write(request.files['file'].stream.read()) |
4504dbba | 71 | |
4504dbba | 72 | # Save now so we have this data before kicking off processing |
b39d1f23 | 73 | entry.save() |
4504dbba | 74 | |
111a609d | 75 | if request.form.get('callback_url'): |
5354f954 JW |
76 | metadata = request.db.ProcessingMetaData() |
77 | metadata.media_entry = entry | |
111a609d | 78 | metadata.callback_url = unicode(request.form['callback_url']) |
5354f954 JW |
79 | metadata.save() |
80 | ||
4504dbba JW |
81 | # Pass off to processing |
82 | # | |
83 | # (... don't change entry after this point to avoid race | |
84 | # conditions with changes to the document via processing code) | |
c7b3d070 SS |
85 | feed_url = request.urlgen( |
86 | 'mediagoblin.user_pages.atom_feed', | |
87 | qualified=True, user=request.user.username) | |
88 | run_process_media(entry, feed_url) | |
4504dbba JW |
89 | |
90 | return json_response(get_entry_serializable(entry, request.urlgen)) | |
91 | ||
a062149e JW |
92 | |
93 | @api_auth | |
78fa73bc | 94 | @require_active_login |
a062149e | 95 | def api_test(request): |
a062149e JW |
96 | user_data = { |
97 | 'username': request.user.username, | |
98 | 'email': request.user.email} | |
99 | ||
74af60bb SS |
100 | # TODO: This is the *only* thing using Response() here, should that |
101 | # not simply use json_response()? | |
a062149e | 102 | return Response(json.dumps(user_data)) |
42c83752 JW |
103 | |
104 | ||
105 | def get_entries(request): | |
106 | entries = request.db.MediaEntry.query | |
107 | ||
c92aa0d0 | 108 | # TODO: Make it possible to fetch unprocessed media, or media in-processing |
42c83752 JW |
109 | entries = entries.filter_by(state=u'processed') |
110 | ||
c92aa0d0 JW |
111 | # TODO: Add sort order customization |
112 | entries = entries.order_by(request.db.MediaEntry.created.desc()) | |
113 | ||
114 | # TODO: Fetch default and upper limit from config | |
115 | entries = entries.limit(int(request.GET.get('limit') or 10)) | |
116 | ||
42c83752 JW |
117 | entries_serializable = [] |
118 | ||
119 | for entry in entries: | |
85726f73 | 120 | entries_serializable.append(get_entry_serializable(entry, request.urlgen)) |
42c83752 JW |
121 | |
122 | return json_response(entries_serializable) |