25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

123 satır
3.6 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2017 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Moves a list of remote media from one media store to another.
  17. The input should be a list of media files to be moved, one per line. Each line
  18. should be formatted::
  19. <origin server>|<file id>
  20. This can be extracted from postgres with::
  21. psql --tuples-only -A -c "select media_origin, filesystem_id from
  22. matrix.remote_media_cache where ..."
  23. To use, pipe the above into::
  24. PYTHON_PATH=. synapse/_scripts/move_remote_media_to_new_store.py <source repo> <dest repo>
  25. """
  26. import argparse
  27. import logging
  28. import os
  29. import shutil
  30. import sys
  31. from synapse.media.filepath import MediaFilePaths
  32. logger = logging.getLogger()
  33. def main(src_repo: str, dest_repo: str) -> None:
  34. src_paths = MediaFilePaths(src_repo)
  35. dest_paths = MediaFilePaths(dest_repo)
  36. for line in sys.stdin:
  37. line = line.strip()
  38. parts = line.split("|")
  39. if len(parts) != 2:
  40. print("Unable to parse input line %s" % line, file=sys.stderr)
  41. sys.exit(1)
  42. move_media(parts[0], parts[1], src_paths, dest_paths)
  43. def move_media(
  44. origin_server: str,
  45. file_id: str,
  46. src_paths: MediaFilePaths,
  47. dest_paths: MediaFilePaths,
  48. ) -> None:
  49. """Move the given file, and any thumbnails, to the dest repo
  50. Args:
  51. origin_server:
  52. file_id:
  53. src_paths:
  54. dest_paths:
  55. """
  56. logger.info("%s/%s", origin_server, file_id)
  57. # check that the original exists
  58. original_file = src_paths.remote_media_filepath(origin_server, file_id)
  59. if not os.path.exists(original_file):
  60. logger.warning(
  61. "Original for %s/%s (%s) does not exist",
  62. origin_server,
  63. file_id,
  64. original_file,
  65. )
  66. else:
  67. mkdir_and_move(
  68. original_file, dest_paths.remote_media_filepath(origin_server, file_id)
  69. )
  70. # now look for thumbnails
  71. original_thumb_dir = src_paths.remote_media_thumbnail_dir(origin_server, file_id)
  72. if not os.path.exists(original_thumb_dir):
  73. return
  74. mkdir_and_move(
  75. original_thumb_dir,
  76. dest_paths.remote_media_thumbnail_dir(origin_server, file_id),
  77. )
  78. def mkdir_and_move(original_file: str, dest_file: str) -> None:
  79. dirname = os.path.dirname(dest_file)
  80. if not os.path.exists(dirname):
  81. logger.debug("mkdir %s", dirname)
  82. os.makedirs(dirname)
  83. logger.debug("mv %s %s", original_file, dest_file)
  84. shutil.move(original_file, dest_file)
  85. if __name__ == "__main__":
  86. parser = argparse.ArgumentParser(
  87. description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
  88. )
  89. parser.add_argument("-v", action="store_true", help="enable debug logging")
  90. parser.add_argument("src_repo", help="Path to source content repo")
  91. parser.add_argument("dest_repo", help="Path to source content repo")
  92. args = parser.parse_args()
  93. logging.basicConfig(
  94. level=logging.DEBUG if args.v else logging.INFO,
  95. format="%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s",
  96. )
  97. main(args.src_repo, args.dest_repo)