aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/addmedia.py
blob: b741b96f9a3080e9dbf8238ba779e52721a0e810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import os

import six

from mediagoblin.gmg_commands import util as commands_util
from mediagoblin.submit.lib import (
    submit_media, get_upload_file_limits,
    FileUploadLimit, UserUploadLimit, UserPastUploadLimit)

from mediagoblin import mg_globals


def parser_setup(subparser):
    subparser.add_argument(
        'username',
        help="Name of user this media entry belongs to")
    subparser.add_argument(
        'filename',
        help="Local file on filesystem")
    subparser.add_argument(
        "-d", "--description",
        help="Description for this media entry")
    subparser.add_argument(
        "-t", "--title",
        help="Title for this media entry")
    subparser.add_argument(
        "-l", "--license",
        help=(
            "License this media entry will be released under. "
            "Should be a URL."))
    subparser.add_argument(
        "-T", "--tags",
        help=(
            "Comma separated list of tags for this media entry."))
    subparser.add_argument(
        "-s", "--slug",
        help=(
            "Slug for this media entry. "
            "Will be autogenerated if unspecified."))

    subparser.add_argument(
        '--celery',
        action='store_true',
        help="Don't process eagerly, pass off to celery")


def addmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    # get the user
    user = app.db.User.query.filter_by(username=args.username.lower()).first()
    if user is None:
        print("Sorry, no user by username '%s'" % args.username)
        return
    
    # check for the file, if it exists...
    filename = os.path.split(args.filename)[-1]
    abs_filename = os.path.abspath(args.filename)
    if not os.path.exists(abs_filename):
        print("Can't find a file with filename '%s'" % args.filename)
        return

    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        else:
            return six.text_type(some_string)

    try:
        submit_media(
            mg_app=app,
            user=user,
            submitted_file=open(abs_filename, 'r'), filename=filename,
            title=maybe_unicodeify(args.title),
            description=maybe_unicodeify(args.description),
            license=maybe_unicodeify(args.license),
            tags_string=maybe_unicodeify(args.tags) or u"",
            upload_limit=upload_limit, max_file_size=max_file_size)
    except FileUploadLimit:
        print("This file is larger than the upload limits for this site.")
    except UserUploadLimit:
        print("This file will put this user past their upload limits.")
    except UserPastUploadLimit:
        print("This user is already past their upload limits.")