Commit | Line | Data |
---|---|---|
b7a8760b CAW |
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 | ||
e49b7e02 BP |
17 | from __future__ import print_function |
18 | ||
b7a8760b CAW |
19 | import os |
20 | ||
e49b7e02 BP |
21 | import six |
22 | ||
d88fcb03 | 23 | from mediagoblin.db.models import LocalUser |
b7a8760b CAW |
24 | from mediagoblin.gmg_commands import util as commands_util |
25 | from mediagoblin.submit.lib import ( | |
210a6ceb | 26 | submit_media, get_upload_file_limits, |
b7a8760b CAW |
27 | FileUploadLimit, UserUploadLimit, UserPastUploadLimit) |
28 | ||
29 | from mediagoblin import mg_globals | |
30 | ||
31 | ||
32 | def parser_setup(subparser): | |
33 | subparser.add_argument( | |
34 | 'username', | |
35 | help="Name of user this media entry belongs to") | |
36 | subparser.add_argument( | |
37 | 'filename', | |
38 | help="Local file on filesystem") | |
39 | subparser.add_argument( | |
40 | "-d", "--description", | |
41 | help="Description for this media entry") | |
42 | subparser.add_argument( | |
43 | "-t", "--title", | |
44 | help="Title for this media entry") | |
45 | subparser.add_argument( | |
46 | "-l", "--license", | |
47 | help=( | |
48 | "License this media entry will be released under. " | |
86d5cb8a | 49 | "Should be a URL.")) |
b7a8760b CAW |
50 | subparser.add_argument( |
51 | "-T", "--tags", | |
52 | help=( | |
53 | "Comma separated list of tags for this media entry.")) | |
54 | subparser.add_argument( | |
55 | "-s", "--slug", | |
56 | help=( | |
57 | "Slug for this media entry. " | |
58 | "Will be autogenerated if unspecified.")) | |
a3c48024 SP |
59 | subparser.add_argument( |
60 | "-c", "--collection-slug", | |
61 | help=( | |
2d0870e0 AB |
62 | "Slug of the collection for this media entry. " |
63 | "Should already exist.")) | |
b7a8760b CAW |
64 | |
65 | subparser.add_argument( | |
66 | '--celery', | |
67 | action='store_true', | |
68 | help="Don't process eagerly, pass off to celery") | |
69 | ||
70 | ||
71 | def addmedia(args): | |
72 | # Run eagerly unless explicetly set not to | |
73 | if not args.celery: | |
74 | os.environ['CELERY_ALWAYS_EAGER'] = 'true' | |
75 | ||
76 | app = commands_util.setup_app(args) | |
77 | ||
78 | # get the user | |
b4997540 | 79 | user = app.db.LocalUser.query.filter( |
d88fcb03 JT |
80 | LocalUser.username==args.username.lower() |
81 | ).first() | |
b7a8760b | 82 | if user is None: |
e49b7e02 | 83 | print("Sorry, no user by username '%s'" % args.username) |
b7a8760b | 84 | return |
b4997540 | 85 | |
b7a8760b | 86 | # check for the file, if it exists... |
862e05a5 CAW |
87 | filename = os.path.split(args.filename)[-1] |
88 | abs_filename = os.path.abspath(args.filename) | |
89 | if not os.path.exists(abs_filename): | |
e49b7e02 | 90 | print("Can't find a file with filename '%s'" % args.filename) |
b7a8760b CAW |
91 | return |
92 | ||
862e05a5 CAW |
93 | def maybe_unicodeify(some_string): |
94 | # this is kinda terrible | |
95 | if some_string is None: | |
96 | return None | |
9d85dcdf BP |
97 | if six.PY2: |
98 | return six.text_type(some_string, 'utf-8') | |
99 | return some_string | |
862e05a5 | 100 | |
b7a8760b CAW |
101 | try: |
102 | submit_media( | |
103 | mg_app=app, | |
104 | user=user, | |
9d85dcdf | 105 | submitted_file=open(abs_filename, 'rb'), filename=filename, |
862e05a5 CAW |
106 | title=maybe_unicodeify(args.title), |
107 | description=maybe_unicodeify(args.description), | |
a3c48024 | 108 | collection_slug=args.collection_slug, |
862e05a5 | 109 | license=maybe_unicodeify(args.license), |
6c067857 | 110 | tags_string=maybe_unicodeify(args.tags) or u"") |
b7a8760b | 111 | except FileUploadLimit: |
e49b7e02 | 112 | print("This file is larger than the upload limits for this site.") |
b7a8760b | 113 | except UserUploadLimit: |
e49b7e02 | 114 | print("This file will put this user past their upload limits.") |
b7a8760b | 115 | except UserPastUploadLimit: |
e49b7e02 | 116 | print("This user is already past their upload limits.") |