You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
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=. ./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.rest.media.v1.filepath import MediaFilePaths
  32. logger = logging.getLogger()
  33. def main(src_repo, dest_repo):
  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(origin_server, file_id, src_paths, dest_paths):
  44. """Move the given file, and any thumbnails, to the dest repo
  45. Args:
  46. origin_server (str):
  47. file_id (str):
  48. src_paths (MediaFilePaths):
  49. dest_paths (MediaFilePaths):
  50. """
  51. logger.info("%s/%s", origin_server, file_id)
  52. # check that the original exists
  53. original_file = src_paths.remote_media_filepath(origin_server, file_id)
  54. if not os.path.exists(original_file):
  55. logger.warning(
  56. "Original for %s/%s (%s) does not exist",
  57. origin_server,
  58. file_id,
  59. original_file,
  60. )
  61. else:
  62. mkdir_and_move(
  63. original_file, dest_paths.remote_media_filepath(origin_server, file_id)
  64. )
  65. # now look for thumbnails
  66. original_thumb_dir = src_paths.remote_media_thumbnail_dir(origin_server, file_id)
  67. if not os.path.exists(original_thumb_dir):
  68. return
  69. mkdir_and_move(
  70. original_thumb_dir,
  71. dest_paths.remote_media_thumbnail_dir(origin_server, file_id),
  72. )
  73. def mkdir_and_move(original_file, dest_file):
  74. dirname = os.path.dirname(dest_file)
  75. if not os.path.exists(dirname):
  76. logger.debug("mkdir %s", dirname)
  77. os.makedirs(dirname)
  78. logger.debug("mv %s %s", original_file, dest_file)
  79. shutil.move(original_file, dest_file)
  80. if __name__ == "__main__":
  81. parser = argparse.ArgumentParser(
  82. description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
  83. )
  84. parser.add_argument("-v", action="store_true", help="enable debug logging")
  85. parser.add_argument("src_repo", help="Path to source content repo")
  86. parser.add_argument("dest_repo", help="Path to source content repo")
  87. args = parser.parse_args()
  88. logging_config = {
  89. "level": logging.DEBUG if args.v else logging.INFO,
  90. "format": "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s",
  91. }
  92. logging.basicConfig(**logging_config)
  93. main(args.src_repo, args.dest_repo)