Began work on metadata validation
[mediagoblin.git] / mediagoblin / gmg_commands / addmedia.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 os
18
19 from mediagoblin.gmg_commands import util as commands_util
20 from mediagoblin.submit.lib import (
21 submit_media, get_upload_file_limits,
22 FileUploadLimit, UserUploadLimit, UserPastUploadLimit)
23
24 from mediagoblin import mg_globals
25
26
27 def parser_setup(subparser):
28 subparser.add_argument(
29 'username',
30 help="Name of user this media entry belongs to")
31 subparser.add_argument(
32 'filename',
33 help="Local file on filesystem")
34 subparser.add_argument(
35 "-d", "--description",
36 help="Description for this media entry")
37 subparser.add_argument(
38 "-t", "--title",
39 help="Title for this media entry")
40 subparser.add_argument(
41 "-l", "--license",
42 help=(
43 "License this media entry will be released under. "
44 "Should be a URL."))
45 subparser.add_argument(
46 "-T", "--tags",
47 help=(
48 "Comma separated list of tags for this media entry."))
49 subparser.add_argument(
50 "-s", "--slug",
51 help=(
52 "Slug for this media entry. "
53 "Will be autogenerated if unspecified."))
54
55 subparser.add_argument(
56 '--celery',
57 action='store_true',
58 help="Don't process eagerly, pass off to celery")
59
60
61 def addmedia(args):
62 # Run eagerly unless explicetly set not to
63 if not args.celery:
64 os.environ['CELERY_ALWAYS_EAGER'] = 'true'
65
66 app = commands_util.setup_app(args)
67
68 # get the user
69 user = app.db.User.query.filter_by(username=args.username.lower()).first()
70 if user is None:
71 print "Sorry, no user by username '%s'" % args.username
72 return
73
74 # check for the file, if it exists...
75 filename = os.path.split(args.filename)[-1]
76 abs_filename = os.path.abspath(args.filename)
77 if not os.path.exists(abs_filename):
78 print "Can't find a file with filename '%s'" % args.filename
79 return
80
81 upload_limit, max_file_size = get_upload_file_limits(user)
82
83 def maybe_unicodeify(some_string):
84 # this is kinda terrible
85 if some_string is None:
86 return None
87 else:
88 return unicode(some_string)
89
90 try:
91 submit_media(
92 mg_app=app,
93 user=user,
94 submitted_file=file(abs_filename, 'r'), filename=filename,
95 title=maybe_unicodeify(args.title),
96 description=maybe_unicodeify(args.description),
97 license=maybe_unicodeify(args.license),
98 tags_string=maybe_unicodeify(args.tags) or u"",
99 upload_limit=upload_limit, max_file_size=max_file_size)
100 except FileUploadLimit:
101 print "This file is larger than the upload limits for this site."
102 except UserUploadLimit:
103 print "This file will put this user past their upload limits."
104 except UserPastUploadLimit:
105 print "This user is already past their upload limits."