addmedia.py 3.7 KB

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