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.
 
 
 
 
 
 

240 lines
8.3 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2020-2021 The Matrix.org Foundation C.I.C.
  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. import functools
  16. import os
  17. import re
  18. from typing import Callable, List
  19. NEW_FORMAT_ID_RE = re.compile(r"^\d\d\d\d-\d\d-\d\d")
  20. def _wrap_in_base_path(func: Callable[..., str]) -> Callable[..., str]:
  21. """Takes a function that returns a relative path and turns it into an
  22. absolute path based on the location of the primary media store
  23. """
  24. @functools.wraps(func)
  25. def _wrapped(self, *args, **kwargs):
  26. path = func(self, *args, **kwargs)
  27. return os.path.join(self.base_path, path)
  28. return _wrapped
  29. class MediaFilePaths:
  30. """Describes where files are stored on disk.
  31. Most of the functions have a `*_rel` variant which returns a file path that
  32. is relative to the base media store path. This is mainly used when we want
  33. to write to the backup media store (when one is configured)
  34. """
  35. def __init__(self, primary_base_path: str):
  36. self.base_path = primary_base_path
  37. def default_thumbnail_rel(
  38. self,
  39. default_top_level: str,
  40. default_sub_type: str,
  41. width: int,
  42. height: int,
  43. content_type: str,
  44. method: str,
  45. ) -> str:
  46. top_level_type, sub_type = content_type.split("/")
  47. file_name = "%i-%i-%s-%s-%s" % (width, height, top_level_type, sub_type, method)
  48. return os.path.join(
  49. "default_thumbnails", default_top_level, default_sub_type, file_name
  50. )
  51. default_thumbnail = _wrap_in_base_path(default_thumbnail_rel)
  52. def local_media_filepath_rel(self, media_id: str) -> str:
  53. return os.path.join("local_content", media_id[0:2], media_id[2:4], media_id[4:])
  54. local_media_filepath = _wrap_in_base_path(local_media_filepath_rel)
  55. def local_media_thumbnail_rel(
  56. self, media_id: str, width: int, height: int, content_type: str, method: str
  57. ) -> str:
  58. top_level_type, sub_type = content_type.split("/")
  59. file_name = "%i-%i-%s-%s-%s" % (width, height, top_level_type, sub_type, method)
  60. return os.path.join(
  61. "local_thumbnails", media_id[0:2], media_id[2:4], media_id[4:], file_name
  62. )
  63. local_media_thumbnail = _wrap_in_base_path(local_media_thumbnail_rel)
  64. def local_media_thumbnail_dir(self, media_id: str) -> str:
  65. """
  66. Retrieve the local store path of thumbnails of a given media_id
  67. Args:
  68. media_id: The media ID to query.
  69. Returns:
  70. Path of local_thumbnails from media_id
  71. """
  72. return os.path.join(
  73. self.base_path,
  74. "local_thumbnails",
  75. media_id[0:2],
  76. media_id[2:4],
  77. media_id[4:],
  78. )
  79. def remote_media_filepath_rel(self, server_name: str, file_id: str) -> str:
  80. return os.path.join(
  81. "remote_content", server_name, file_id[0:2], file_id[2:4], file_id[4:]
  82. )
  83. remote_media_filepath = _wrap_in_base_path(remote_media_filepath_rel)
  84. def remote_media_thumbnail_rel(
  85. self,
  86. server_name: str,
  87. file_id: str,
  88. width: int,
  89. height: int,
  90. content_type: str,
  91. method: str,
  92. ) -> str:
  93. top_level_type, sub_type = content_type.split("/")
  94. file_name = "%i-%i-%s-%s-%s" % (width, height, top_level_type, sub_type, method)
  95. return os.path.join(
  96. "remote_thumbnail",
  97. server_name,
  98. file_id[0:2],
  99. file_id[2:4],
  100. file_id[4:],
  101. file_name,
  102. )
  103. remote_media_thumbnail = _wrap_in_base_path(remote_media_thumbnail_rel)
  104. # Legacy path that was used to store thumbnails previously.
  105. # Should be removed after some time, when most of the thumbnails are stored
  106. # using the new path.
  107. def remote_media_thumbnail_rel_legacy(
  108. self, server_name: str, file_id: str, width: int, height: int, content_type: str
  109. ):
  110. top_level_type, sub_type = content_type.split("/")
  111. file_name = "%i-%i-%s-%s" % (width, height, top_level_type, sub_type)
  112. return os.path.join(
  113. "remote_thumbnail",
  114. server_name,
  115. file_id[0:2],
  116. file_id[2:4],
  117. file_id[4:],
  118. file_name,
  119. )
  120. def remote_media_thumbnail_dir(self, server_name: str, file_id: str) -> str:
  121. return os.path.join(
  122. self.base_path,
  123. "remote_thumbnail",
  124. server_name,
  125. file_id[0:2],
  126. file_id[2:4],
  127. file_id[4:],
  128. )
  129. def url_cache_filepath_rel(self, media_id: str) -> str:
  130. if NEW_FORMAT_ID_RE.match(media_id):
  131. # Media id is of the form <DATE><RANDOM_STRING>
  132. # E.g.: 2017-09-28-fsdRDt24DS234dsf
  133. return os.path.join("url_cache", media_id[:10], media_id[11:])
  134. else:
  135. return os.path.join("url_cache", media_id[0:2], media_id[2:4], media_id[4:])
  136. url_cache_filepath = _wrap_in_base_path(url_cache_filepath_rel)
  137. def url_cache_filepath_dirs_to_delete(self, media_id: str) -> List[str]:
  138. "The dirs to try and remove if we delete the media_id file"
  139. if NEW_FORMAT_ID_RE.match(media_id):
  140. return [os.path.join(self.base_path, "url_cache", media_id[:10])]
  141. else:
  142. return [
  143. os.path.join(self.base_path, "url_cache", media_id[0:2], media_id[2:4]),
  144. os.path.join(self.base_path, "url_cache", media_id[0:2]),
  145. ]
  146. def url_cache_thumbnail_rel(
  147. self, media_id: str, width: int, height: int, content_type: str, method: str
  148. ) -> str:
  149. # Media id is of the form <DATE><RANDOM_STRING>
  150. # E.g.: 2017-09-28-fsdRDt24DS234dsf
  151. top_level_type, sub_type = content_type.split("/")
  152. file_name = "%i-%i-%s-%s-%s" % (width, height, top_level_type, sub_type, method)
  153. if NEW_FORMAT_ID_RE.match(media_id):
  154. return os.path.join(
  155. "url_cache_thumbnails", media_id[:10], media_id[11:], file_name
  156. )
  157. else:
  158. return os.path.join(
  159. "url_cache_thumbnails",
  160. media_id[0:2],
  161. media_id[2:4],
  162. media_id[4:],
  163. file_name,
  164. )
  165. url_cache_thumbnail = _wrap_in_base_path(url_cache_thumbnail_rel)
  166. def url_cache_thumbnail_directory(self, media_id: str) -> str:
  167. # Media id is of the form <DATE><RANDOM_STRING>
  168. # E.g.: 2017-09-28-fsdRDt24DS234dsf
  169. if NEW_FORMAT_ID_RE.match(media_id):
  170. return os.path.join(
  171. self.base_path, "url_cache_thumbnails", media_id[:10], media_id[11:]
  172. )
  173. else:
  174. return os.path.join(
  175. self.base_path,
  176. "url_cache_thumbnails",
  177. media_id[0:2],
  178. media_id[2:4],
  179. media_id[4:],
  180. )
  181. def url_cache_thumbnail_dirs_to_delete(self, media_id: str) -> List[str]:
  182. "The dirs to try and remove if we delete the media_id thumbnails"
  183. # Media id is of the form <DATE><RANDOM_STRING>
  184. # E.g.: 2017-09-28-fsdRDt24DS234dsf
  185. if NEW_FORMAT_ID_RE.match(media_id):
  186. return [
  187. os.path.join(
  188. self.base_path, "url_cache_thumbnails", media_id[:10], media_id[11:]
  189. ),
  190. os.path.join(self.base_path, "url_cache_thumbnails", media_id[:10]),
  191. ]
  192. else:
  193. return [
  194. os.path.join(
  195. self.base_path,
  196. "url_cache_thumbnails",
  197. media_id[0:2],
  198. media_id[2:4],
  199. media_id[4:],
  200. ),
  201. os.path.join(
  202. self.base_path, "url_cache_thumbnails", media_id[0:2], media_id[2:4]
  203. ),
  204. os.path.join(self.base_path, "url_cache_thumbnails", media_id[0:2]),
  205. ]