addmedia.py 3.8 KB

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